#include <string.h>

#include "prologin.h"
#include "enumere.h"
#include "eval.h"

int agent_simul;

int                act_prof;
struct action_hist act_stack  [NB_POINTS_ACTION];

double             best_score;
int                best_prof;
struct action_hist best_stack [NB_POINTS_ACTION];

void enumere_coups()
{
    act_prof++;

    act_stack[act_prof].id_agent = agent_simul;

    for(int idir = NORD; idir <= OUEST; idir++)
    {
        act_stack[act_prof].dir = idir;

        position dpos = position_agent(moi(), agent_simul);
        dpos.ligne   += DEPLACEMENT[idir].ligne;
        dpos.colonne += DEPLACEMENT[idir].colonne;

        if(points_action_agent(agent_simul) >= COUT_DEPLACEMENT)
        {
            if(deplacer(agent_simul, idir) == OK)
            {
                act_stack[act_prof].atype = ACTION_DEPLACER;

                enumere_coups();
                annuler();
            }
        }
        if(points_action_agent(agent_simul) >= COUT_GLISSADE)
        {
            if(glisser(agent_simul, idir) == OK)
            {
                act_stack[act_prof].atype = ACTION_GLISSER;

                enumere_coups();
                annuler();
            }
        }
        if(points_action_agent(agent_simul) >= COUT_POUSSER)
        {
            if(agent_sur_case(dpos) != moi()
            && pousser(agent_simul, idir) == OK)
            {
                act_stack[act_prof].atype = ACTION_POUSSER;

                enumere_coups();
                annuler();
            }
        }
    }

    double iscore = eval();
    if(iscore >= best_score)
    {
        best_score = iscore;
        best_prof = act_prof;
        memcpy(best_stack, act_stack, sizeof(best_stack));
    }

    act_prof--;
}
