#include "game.h"
#include "carlearn.h"

Game::Game()
	{
	shmCreate();
	data->read=0;
	data->Res[2]=1;
	R.rebuild(CAMVECT,NOMBRE_NEURONES_INTERMEDIAIRES,TAILLERESULTAT);
	neurError=1;
	data->Neuron=0;
	data->action=0;
	data->APon=1;
	data->viewNetwork=0;
	showOutput=1;
	#ifdef __SHOW_NETWORK
	rSDL.setReseau(&R);
	#endif
	}

void Game::shmCreate()
	{
	if ((shmid = shmget(SHMKEY, sizeof(struct shmdata), IPC_CREAT | 0666)) < 0) 
		{
		perror("shmget");
		exit(1);
		}
	if ((data = (struct shmdata *) shmat(shmid, NULL, 0)) == (struct shmdata *) -1) 
		{
		perror("shmat");
		exit(1);
		}
	}

void Game::pilot(struct shmdata *data)
	{
	int i,z1=0,z2=0,center,np=0;
	for(i=0;i<VSIZE;i++)
		{
		np+=(data->V[i]+1)%2;
		if(data->V[i]==1 && z1==0) 
			{
			z1=i;
			while(data->V[i]==1) i++;
			z1+=i;
			z1/=2;
			}
		else if(data->V[i]==1)
			{
			z2=i;
			while(data->V[i]==1) i++;
			z2+=i;
			z2/=2;
			}
		}
	/* printf("zones : %d , %d \n",z1,z2); */
	if(z1&&z2) center=(z1+z2)/2;
	else if(z1) center=z1;
	else if(z2) center=z2;
	else center=VSIZE/2;
	if(np>(VSIZE/2)) 
		{ 
		data->Res[2]=1;
		}
	else
		{ 
		data->Res[2]=0; 
		}
	if(center < (VSIZE/2)-2 ) 
		data->Res[0]=1;
	else if(center > (VSIZE/2)+2 ) 
		data->Res[1]=1;
	}

void Game::learnThis()
	{
	if(showOutput) 
		{
		cout << " LEARN : " ;
		for(int i=0;i<TAILLERESULTAT;i++)
			cout << " [" << data->Res[i] << "]";
		}
	neurError+=R.learnOne(data->V,data->Res);
	neurError/=2;
	}

void Game::neuronThis()
	{
	Utils u;
	std::vector<double> V;
	V=R.forward(data->V);
	if(showOutput) cout << " neuron reply : ";
	for(unsigned i=0;i<V.size();i++) 
		{
		data->Res[i]=u.accept(V[i]);
		if(showOutput) cout << " " << data->Res[i];
		}
	if(showOutput) cout << " bool :";
	for(unsigned i=0;i<V.size();i++) 
		{
		if(showOutput) printf(" %7.4f",(float)V[i]);
		}
	}

void Game::Run()
	{
	unsigned int cpt;
	for(cpt=0;data->read!=666;cpt++)
		{
		#ifdef __SHOW_NETWORK
		rSDL.setState(data->viewNetwork);
		if(!(cpt%16))	rSDL.refresh();
		#endif
		while(data->read==0) usleep(data->delay/2); // on attends que la voiture ait bougé 
		if(data->read==666) break;
		
		if(showOutput) 
			{
			cout << "V = "; 
			for(i=0;i<VSIZE;i++) 
				{
				if(data->V[i]) cout << " ";  
				else cout << "#";
				}
			}
		
		if(data->action==SAVE_STATE)
			{
			cout << "Enregistrement de l'etat du reseau dans le fichier : " << STATE_FILE << endl;
			data->action=0;
			R.saveState(STATE_FILE);
			}
		else if(data->action==RELOAD_STATE)
			{
			cout << "Restauration de l'etat du reseau dans le fichier : " << STATE_FILE << endl;
			data->action=0;
			R.loadState(STATE_FILE);
			}
		if(data->APon==1) 
			{
			pilot(data);		// mode pilot automatique
			if(showOutput) 
				{
				cout << " AP= "; 
				for(i=0;i<TAILLERESULTAT;i++) printf(" %d",data->Res[i]);
				}
			}
		if(data->Neuron==1) 
			{
			if(data->APon)		// mode apprentissage (neurone + pilote)
				learnThis();
			else			// neurone seul
				{
				neuronThis();
				}
			}
		if(showOutput) printf("\n");
			data->read=0;
		}
	cout << " EXIT AutoPilot ... " << endl;
	}
void Game::preLearn()
	{
	// double learnAll(std::vector<bool *> inputs, std::vector<bool *> targets);
	std::vector<bool *> in;
	std::vector<bool *> r;
	int cpt=0;
	double erreur=0;
	
	for(int i=0;i<PREtests;i++)
		{
		in.push_back(VoitureLearnI[i]);
		r.push_back(VoitureLearnR[i]);
		}
	
	while(1)
		{
		++cpt;
		erreur += R.learnAll(in,r);
		if(erreur < LEARNACCEPT)
			break;
		if(cpt%100 == 0)
			{
			erreur/=100;
			cout << " : step " << cpt << " error:" << erreur << endl;
			erreur=0;
			}
		}
	}
void Game::preTest()
	{
	Utils u;
	std::vector<double> V;
	for(int i=0;i<PREtests;i++)
		{
		V=R.forward(VoitureLearnI[i]);
		cout << "pour : ";
		for(int j=0;j<64;j++) cout << VoitureLearnI[i][j] ;
		for(unsigned i=0;i<V.size();i++)
			cout << " [" << u.accept(V[i]) << "]";
		cout << " bool " ;
		for(unsigned i=0;i<V.size();i++)
			cout << " [" << V[i] << "] " ;
		cout <<  "\n";
		}
	}
void Game::toggleShow(bool t)
	{
	showOutput=t;
	}
