// TNeuroControlSystem.cpp: implementation of the TNeuroControlSystem class.
//
//////////////////////////////////////////////////////////////////////

#include <math.h>

#include "stdafx.h"
#include "TNeuroControlSystem.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

TNeuroControlSystem neuroControlSystem;

void TNeuroControlSystem::messageHandler
( 
   TControlSystemMessages message,
   void *parm
)
{
   switch( message )
   {
      case CSM_INIT:
         solverPat.destroyPattern();
         solverPat.loadPattern();
         sensors.init();
         TDetControlSystem::messageHandler( message, parm );
         break;

      case CSM_RESET:
         sensors.reset();
         TDetControlSystem::messageHandler( message, parm );
         break;

      case CSM_PRESS_BUTTON:
         sensors.pressButton( ((TCSMPressButtonParm*)parm)->floor );
         TDetControlSystem::messageHandler( message, parm );
         break;

      case CSM_STEP:
         messageHandler( CSM_EFF_CRITERIA_COUNTER_PRESTEP, NULL );

         {
            TEnvStateSet  envStateSet;
            TControlSet   controlSet;

            sensors.getEnvState( envStateSet );
            solver.solve( envStateSet, controlSet );
            actuators.move( controlSet );
         }
         break;

      case CSM_SAVE_SOLVER:
         solver.saveToFile("solver.img");
         break;

      case CSM_LOAD_SOLVER:
         if( parm )
         {
            char name[100];
            
            float ksu = *(float*)parm;
            ksu *= 100;
            ksu = floorf( ksu + 0.5 );
            int ksuI = ksu/10*10 + 10;

            ksuI = ksuI - ksuI%20;
            sprintf( name, "solver.img.%03d", ksuI );
            solver.loadFromFile( name );
         }
         else
            solver.loadFromFile("solver.img");
         break;

      case CSM_SAVE_SOLVER_PAT:
         solverPat.savePattern();
         break;

      case CSM_LEARN_SOLVER_DLG:
         ((LearnDlg*)parm)->learn();
         break;

      case CSM_LEARN_SOLVER:
         solverPat.destroyPattern();
         solverPat.loadPattern();
         solver.learn(solverPat);
         messageHandler( CSM_SAVE_SOLVER, NULL );
         break;

      case CSM_GET_SOLVER_CYCLE:
         *((int*)parm) = solver.GetCycle();
         break;
      
      case CSM_SET_SOLVER_STOP_LEARNING:
         solver.setStopLearning();
         break;

      case CSM_GET_POPULEST_FLOOR:
         *(int*)parm = sensors.getPopulestFloor();
         break;

      case CSM_GET_SOLVER_IMAGE_NAME:
         strcpy( (char*)parm, solver.getName() );
         break;

      default:
         TDetControlSystem::messageHandler( message, parm );
         break;
   }
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TNeuroControlSystem::TNeuroControlSystem()
{

}

TNeuroControlSystem::~TNeuroControlSystem()
{

}
