#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#include "ant_class.h"

BOOL IO_boucle();

Object *winobj=NULL;

ULONG noir=0;
ULONG blanc=0;
ULONG gris1=0;
ULONG gris2=0;
ULONG gris3=0;
ULONG gris4=0;
ULONG gris5=0;
ULONG gris6=0;
ULONG gris7=0;
ULONG gris8=0;
ULONG gris9=0;
ULONG gris10=0;
ULONG gris11=0;
ULONG gris12=0;
ULONG gris13=0;
ULONG gris14=0;
ULONG grey=0;
ULONG red1=0;
/*ULONG red2=0;
ULONG red3=0;
ULONG red4=0;
ULONG red5=0;
ULONG red6=0;
ULONG red7=0;
ULONG red8=0;
ULONG red9=0;
ULONG red10=0;
ULONG red11=0;
ULONG red12=0;
ULONG red13=0;
ULONG red14=0;*/

struct Window *win=NULL;
struct RastPort *winport=NULL;
struct RastPort winport_noir;
struct RastPort winport_blanc;
struct RastPort winport_gris1;
struct RastPort winport_gris2;
struct RastPort winport_gris3;
struct RastPort winport_gris4;
struct RastPort winport_gris5;
struct RastPort winport_gris6;
struct RastPort winport_gris7;
struct RastPort winport_gris8;
struct RastPort winport_gris9;
struct RastPort winport_gris10;
struct RastPort winport_gris11;
struct RastPort winport_gris12;
struct RastPort winport_gris13;
struct RastPort winport_gris14;
struct RastPort winport_grey;
struct RastPort winport_red1;
/*struct RastPort winport_red2;
struct RastPort winport_red3;
struct RastPort winport_red4;
struct RastPort winport_red5;
struct RastPort winport_red6;
struct RastPort winport_red7;
struct RastPort winport_red8;
struct RastPort winport_red9;
struct RastPort winport_red10;
struct RastPort winport_red11;
struct RastPort winport_red12;
struct RastPort winport_red13;
struct RastPort winport_red14;*/

void Window1_CloseWindow_Event(void)
{
    if(Quitrequest1())
    {
        Emperor_QuitProgram();
    }
}

void Startup(void)
{
    noir = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x0FFFFFFF, 0x0FFFFFFF, 0x0FFFFFFF, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    blanc = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris1 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xeFFFFFFF, 0xeFFFFFFF, 0xeFFFFFFF, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris2 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xdfffffff, 0xdfffffff, 0xdfffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris3 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xcfffffff, 0xcfffffff, 0xcfffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris4 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xbfffffff, 0xbfffffff, 0xbfffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris5 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xafffffff, 0xafffffff, 0xafffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris6 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x9fffffff, 0x9fffffff, 0x9fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris7 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x8fffffff, 0x8fffffff, 0x8fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris8 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x7fffffff, 0x7fffffff, 0x7fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris9 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x6fffffff, 0x6fffffff, 0x6fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris10 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x5fffffff, 0x5fffffff, 0x5fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris11 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x4fffffff, 0x4fffffff, 0x4fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris12 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x3fffffff, 0x3fffffff, 0x3fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris13 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x2fffffff, 0x2fffffff, 0x2fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    gris14 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x1fffffff, 0x1fffffff, 0x1fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red1 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xeFFFFFFF, 0x0FFFFFFF, 0x0FFFFFFF, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    /*red2 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xdfffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red3 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xcfffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red4 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xbfffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red5 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xafffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red6 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x9fffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red7 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x8fffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red8 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x7fffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red9 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x6fffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red10 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x5fffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red11 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x4fffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red12 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x3fffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red13 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x2fffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);
    red14 = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x1fffffff, 0x0fffffff, 0x0fffffff, OBP_Precision, PRECISION_EXACT, TAG_DONE);*/
    SetGadgetAttrs(Integer2, Window1, NULL, INTEGER_Number, 5, TAG_DONE);
    attract=5;
    SetGadgetAttrs(Integer5, Window1, NULL, INTEGER_Number, 1, TAG_DONE);
}

void Window1_Iconify_Event(void)
{
    Emperor_IconifyWindow_Window1();
}
  
void Window1_Uniconify_Event(void)
{
    Emperor_UniconifyWindow_Window1();
}

void Integer1_GadgetUp_Event(void)
{
    ant = atoi(Emperor_GetGadgetAttr(Integer1));
}

void Integer2_GadgetUp_Event(void)
{
    attract = atoi(Emperor_GetGadgetAttr(Integer2));
}

void Integer3_GadgetUp_Event(void)
{
    xmax = atoi(Emperor_GetGadgetAttr(Integer3));
}

void Integer4_GadgetUp_Event(void)
{
    ymax = atoi(Emperor_GetGadgetAttr(Integer4));
}

void Integer5_GadgetUp_Event(void)
{
    zoom = atoi(Emperor_GetGadgetAttr(Integer5));
}

void Button1_GadgetUp_Event(void)
{
    ant=5;
    xmax=100;
    ymax=100;
    attract=5;
    zoom=1;
    SetGadgetAttrs(Integer1, Window1, NULL, INTEGER_Number, 5, TAG_DONE);
    SetGadgetAttrs(Integer2, Window1, NULL, INTEGER_Number, 5, TAG_DONE);
    SetGadgetAttrs(Integer3, Window1, NULL, INTEGER_Number, 100, TAG_DONE);
    SetGadgetAttrs(Integer4, Window1, NULL, INTEGER_Number, 100, TAG_DONE);
    SetGadgetAttrs(Integer5, Window1, NULL, INTEGER_Number, 1, TAG_DONE);
}

void Button2_GadgetUp_Event(void)
{
    int random=0, count=0, i=0, ID=0, x=0, y=0, z=0, coordx=0, coordy=0, direction=0,
    generation=0, xx=0, yy=0, temp=0, var_attract=0, xdif=0, ydif=0, fourmi=0, fourmi_temp=0;
    srand((unsigned) time (NULL));
    UBYTE zmax=3, attract=5;
    BOOL collision=FALSE;

    Tableau <Cell> C(xmax, ymax, zmax+1);

    winobj = (Object*) WindowObject,
            WA_Left,    100,
            WA_Top,     100,
            WA_Width,   (xmax*zoom)+30,
            WA_Height,  (ymax*zoom)+30,
            WA_DragBar, TRUE,
            //WA_Title,   "Sortie graphique",
            WA_PubScreen, Screen1,
            //WA_CloseGadget,  TRUE,
            WA_DepthGadget, TRUE,
            WA_SuperBitMap, TRUE,
            WA_IDCMP,  IDCMP_GADGETDOWN | IDCMP_GADGETUP | IDCMP_GADGETHELP | IDCMP_MENUPICK | IDCMP_MENUHELP | IDCMP_CLOSEWINDOW | IDCMP_ACTIVEWINDOW | IDCMP_INACTIVEWINDOW | IDCMP_RAWKEY | IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | IDCMP_NEWSIZE | IDCMP_CHANGEWINDOW | IDCMP_SIZEVERIFY | IDCMP_REFRESHWINDOW | IDCMP_INTUITICKS,
            WA_RMBTrap, TRUE,
    EndWindow;

    win = RA_OpenWindow (winobj);

    winport = win->RPort;
    winport_noir = *win->RPort;
    winport_blanc = *win->RPort;
    winport_gris1 = *win->RPort;
    winport_gris2 = *win->RPort;
    winport_gris3 = *win->RPort;
    winport_gris4 = *win->RPort;
    winport_gris5 = *win->RPort;
    winport_gris6 = *win->RPort;
    winport_gris7 = *win->RPort;
    winport_gris8 = *win->RPort;
    winport_gris9 = *win->RPort;
    winport_gris10 = *win->RPort;
    winport_gris11 = *win->RPort;
    winport_gris12 = *win->RPort;
    winport_gris13 = *win->RPort;
    winport_gris14 = *win->RPort;
    winport_grey = *win->RPort;
    winport_red1 = *win->RPort;
    /*winport_red2 = *win->RPort;
    winport_red3 = *win->RPort;
    winport_red4 = *win->RPort;
    winport_red5 = *win->RPort;
    winport_red6 = *win->RPort;
    winport_red7 = *win->RPort;
    winport_red8 = *win->RPort;
    winport_red9 = *win->RPort;
    winport_red10 = *win->RPort;
    winport_red11 = *win->RPort;
    winport_red12 = *win->RPort;
    winport_red13 = *win->RPort;
    winport_red14 = *win->RPort;*/
    grey = ReadPixel(&winport_grey, 10, 10);
    SetAPen( &winport_noir, noir );
    SetAPen( &winport_blanc, blanc );
    SetAPen( &winport_gris1, gris1 );
    SetAPen( &winport_gris2, gris2 );
    SetAPen( &winport_gris3, gris3 );
    SetAPen( &winport_gris4, gris4 );
    SetAPen( &winport_gris5, gris5 );
    SetAPen( &winport_gris6, gris6 );
    SetAPen( &winport_gris7, gris7 );
    SetAPen( &winport_gris8, gris8 );
    SetAPen( &winport_gris9, gris9 );
    SetAPen( &winport_gris10, gris10 );
    SetAPen( &winport_gris11, gris11 );
    SetAPen( &winport_gris12, gris12 );
    SetAPen( &winport_gris13, gris13 );
    SetAPen( &winport_gris14, gris14 );
    //SetAPen( &winport_grey, grey);
    SetAPen( &winport_red1, red1);
    /*SetAPen( &winport_red2, red2);
    SetAPen( &winport_red3, red3);
    SetAPen( &winport_red4, red4);
    SetAPen( &winport_red5, red5);
    SetAPen( &winport_red6, red6);
    SetAPen( &winport_red7, red7);
    SetAPen( &winport_red8, red8);
    SetAPen( &winport_red9, red9);
    SetAPen( &winport_red10, red10);
    SetAPen( &winport_red11, red11);
    SetAPen( &winport_red12, red12);
    SetAPen( &winport_red13, red13);
    SetAPen( &winport_red14, red14);*/
    Move(&winport_blanc, 18, 18);
    Draw(&winport_blanc, (xmax*zoom)+22, 18);
    Draw(&winport_blanc, (xmax*zoom)+22, (ymax*zoom)+22);
    Draw(&winport_blanc, 18, (ymax*zoom)+22);
    Draw(&winport_blanc, 18, 18);
    Move(&winport_noir, 17, 17);
    Draw(&winport_noir, (xmax*zoom)+21, 17);
    Draw(&winport_noir, (xmax*zoom)+21, (ymax*zoom)+21);
    Draw(&winport_noir, 17, (ymax*zoom)+21);
    Draw(&winport_noir, 17, 17);
    RectFill(&winport_blanc, 19,19, (xmax*zoom)+20,(ymax*zoom)+20);

/*appel au constructeur*/

    for(z=0; z<=zmax; z++)
    {
        for(y=0; y<ymax; y++)
        {
            for(x=0; x<xmax; x++)
            {
                C(x,y,z);
            }
        }
    }

/*répartition aléatoire des fourmis*/

    for(i=0; i<ant; i++)
    {
        ID=i;
        coordx=(rand()%xmax);
        coordy=(rand()%ymax);
        C(coordx, coordy, 0).set_ant(TRUE);
    }

    for(z=0; z<=zmax; z++)
    {
        z=0;
        fourmi=0;
        for(y=0; y<ymax; y++)
        {
            for(x=0; x<xmax; x++)
            {                
                if(C(x,y,z).get_ant())
                {
                    RectFill(&winport_red1, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                    direction=C(x,y,z).get_direction();
                    //fourmi++;
                    var_attract=0;                             //recherche des attractions vers l'avant
                    switch(direction)
                    {
                        case 1:
                            for(yy=y-1; yy<=y; yy++)
                            {
                                for(xx=x-1; xx<=x; xx++)
                                {
                                    if(yy==y && xx==x)
                                    {
                                        break;
                                    }
                                    temp=C(xx,yy,z).get_attract();
                                    if(temp>var_attract)
                                    {
                                        var_attract=temp;
                                        xdif=x-xx;
                                        ydif=y-yy;
                                    }
                                }
                            }
                            break;
                        case 2:
                            yy=y-1;
                            for(xx=x-1; xx<=x+1; xx++)
                            {
                                temp=C(xx,yy,z).get_attract();
                                if(temp>var_attract)
                                {
                                    var_attract=temp;
                                    xdif=x-xx;
                                    ydif=y-yy;
                                }
                            }
                            break;
                        case 3:
                            for(yy=y-1; yy<=y; yy++)
                            {
                                for(xx=x; xx<=x+1; xx++)
                                {
                                    if(yy==y && xx==x)
                                    {
                                        break;
                                    }
                                    temp=C(xx,yy,z).get_attract();
                                    if(temp>var_attract)
                                    {
                                        var_attract=temp;
                                        xdif=x-xx;
                                        ydif=y-yy;
                                    }
                                }
                            }
                            break;
                        case 4:
                            xx=x+1;
                            for(yy=y-1; yy<=y; yy++)
                            {
                                temp=C(xx,yy,z).get_attract();
                                if(temp>var_attract)
                                {
                                    var_attract=temp;
                                    xdif=x-xx;
                                    ydif=y-yy;
                                }
                            }
                            break;
                        case 5:
                            for(yy=y; yy<=y+1; yy++)
                            {
                                for(xx=x; xx<=x+1; xx++)
                                {
                                    if(yy==y && xx==x)
                                    {
                                        break;
                                    }
                                    temp=C(xx,yy,z).get_attract();
                                    if(temp>var_attract)
                                    {
                                        var_attract=temp;
                                        xdif=x-xx;
                                        ydif=y-yy;
                                    }
                                }
                            }
                            break;
                        case 6:
                            yy=y+1;
                            for(xx=x-1; xx<=x+1; xx++)
                            {
                                temp=C(xx,yy,z).get_attract();
                                if(temp>var_attract)
                                {
                                    var_attract=temp;
                                    xdif=x-xx;
                                    ydif=y-yy;
                                }
                            }
                            break;
                        case 7:
                            for(yy=y-1; yy<=y; yy++)
                            {
                                for(xx=x-1; xx<=x; xx++)
                                {
                                    temp=C(xx,yy,z).get_attract();
                                    if(temp>var_attract)
                                    {
                                        var_attract=temp;
                                        xdif=x-xx;
                                        ydif=y-yy;
                                    }
                                }
                            }
                            break;
                        case 8:
                            for(yy=y-1; yy<=y; yy++)
                            {
                                for(xx=x-1; xx<=x; xx++)
                                {
                                    temp=C(xx,yy,z).get_attract();
                                    if(temp>var_attract)
                                    {
                                        var_attract=temp;
                                        xdif=x-xx;
                                        ydif=y-yy;
                                    }
                                }
                            }
                            break;
                        default:

                            break;
                    }

                    /*for(yy=y-1; yy<=y+1; yy++)
                    {
                        for(xx=x-1; xx<=0; xx++)
                        {
                            temp=C(xx,yy,z).get_attract();
                            if(temp>var_attract)
                            {
                                var_attract=temp;
                                xdif=x-xx;
                                ydif=y-yy;
                            }
                        }
                    }*/

                    
                    if(xdif==0 || ydif==0)         //direction aléatoire
                    {
                        do                           //choix d'une direction non occupée
                        {
                            direction=(rand()%8)+1;
                            switch(direction)
                            {
                                case 1:
                                    if(C(x-1,y-1,z+1).get_ant())
                                    {
                                        collision=TRUE;
                                    }
                                    else
                                    {
                                        collision=FALSE;
                                    }
                                    break;
                                case 2:
                                    if(C(x,y-1,z+1).get_ant())
                                    {
                                        collision=TRUE;
                                    }
                                    else
                                    {
                                        collision=FALSE;
                                    }
                                    break;
                                case 3:
                                    if(C(x+1,y-1,z+1).get_ant())
                                    {
                                        collision=TRUE;
                                    }
                                    else
                                    {
                                        collision=FALSE;
                                    }
                                    break;
                                case 8:
                                    if(C(x-1,y,z+1).get_ant())
                                    {
                                        collision=TRUE;
                                    }
                                    else
                                    {
                                        collision=FALSE;
                                    }
                                    break;
                                case 4:
                                    if(C(x+1,y,z+1).get_ant())
                                    {
                                        collision=TRUE;
                                    }
                                    else
                                    {
                                        collision=FALSE;
                                    }
                                    break;
                                case 7:
                                    if(C(x-1,y+1,z+1).get_ant())
                                    {
                                        collision=TRUE;
                                    }
                                    else
                                    {
                                        collision=FALSE;
                                    }
                                    break;
                                case 6:
                                    if(C(x,y+1,z+1).get_ant())
                                    {
                                        collision=TRUE;
                                    }
                                    else
                                    {
                                        collision=FALSE;
                                    }
                                    break;
                                case 5:
                                    if(C(x+1,y+1,z+1).get_ant())
                                    {
                                        collision=TRUE;
                                    }
                                    else
                                    {
                                        collision=FALSE;
                                    }
                                    break;
                                default:
                                    collision=FALSE;
                                    break;
                            }
                        }
                        while(collision);
                        
                        switch(direction)                //direction codée comme  123  et pas 123
                        {                                //                       8x4         4x5
                            case 1:                      //                       765         678
                                C(x-1,y-1,z+1).set_ant(TRUE);       //une fourmi a la prochaine generation
                                C(x-1,y-1,z+1).set_age(generation); //plus vieille d'une generation
                                C(x-1,y-1,z+1).set_direction(direction);
                                break;
                            case 2:
                                C(x,y-1,z+1).set_ant(TRUE);
                                C(x,y-1,z+1).set_age(generation);
                                C(x,y-1,z+1).set_direction(direction);
                                break;
                            case 3:
                                C(x+1,y-1,z+1).set_ant(TRUE);
                                C(x+1,y-1,z+1).set_age(generation);
                                C(x+1,y-1,z+1).set_direction(direction);
                                break;
                            case 8:
                                C(x-1,y,z+1).set_ant(TRUE);
                                C(x-1,y,z+1).set_age(generation);
                                C(x-1,y,z+1).set_direction(direction);
                                break;
                            case 4:
                                C(x+1,y,z+1).set_ant(TRUE);
                                C(x+1,y,z+1).set_age(generation);
                                C(x+1,y,z+1).set_direction(direction);
                                break;
                            case 7:
                                C(x-1,y+1,z+1).set_ant(TRUE);
                                C(x-1,y+1,z+1).set_age(generation);
                                C(x-1,y+1,z+1).set_direction(direction);
                                break;
                            case 6:
                                C(x,y+1,z+1).set_ant(TRUE);
                                C(x,y+1,z+1).set_age(generation);
                                C(x,y+1,z+1).set_direction(direction);
                                break;
                            case 5:
                                C(x+1,y+1,z+1).set_ant(TRUE);
                                C(x+1,y+1,z+1).set_age(generation);
                                C(x+1,y+1,z+1).set_direction(direction);
                                break;
                            default:
                                break;
                        }
                    }
                    else                                      //direction avec attraction
                    {
                        C(x-xdif, y-ydif, z+1).set_ant(TRUE);
                        C(x+1,y+1,z+1).set_age(generation);
                    }
                    C(x,y,z+1).set_ant(FALSE);                   
                    C(x,y,z+1).set_trace(TRUE);
                    C(x,y,z+1).set_attract(20);        //plus de fourmi mais attraction
                }

                if(C(x,y,z).get_trace())
                {
                    temp=(C(x,y,z).get_attract());
                    switch(temp)
                    {
                        case 1:
                            RectFill ( &winport_blanc, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            C(x,y,z+1).set_trace(FALSE);
                            break;
                        case 2:
                            RectFill(&winport_blanc, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 3:
                            RectFill(&winport_gris1, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 4:
                            RectFill(&winport_gris2, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 5:
                            RectFill(&winport_gris3, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 6:
                            RectFill(&winport_gris4, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 7:
                            RectFill(&winport_gris5, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 8:
                            RectFill(&winport_gris6, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 9:
                            RectFill(&winport_gris7, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 10:
                            RectFill(&winport_gris8, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 11:
                            RectFill(&winport_gris9, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 12:
                            RectFill(&winport_gris10, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 13:
                            RectFill(&winport_gris11, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 14:
                            RectFill(&winport_gris12, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 15:
                            RectFill(&winport_gris13, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 16:
                            RectFill(&winport_gris14, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        case 17:
                            RectFill(&winport_noir, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                        default:
                            RectFill(&winport_noir, (x*zoom)+20, (y*zoom)+20, (x*zoom)+zoom+20, (y*zoom)+zoom+20);
                            break;
                    }
                    temp--;
                    C(x,y,z+1).set_attract(temp);
                    //cout <<C(x,y,z).get_attract() <<endl;
                }
            }
        }
        //cout <<"il y a " <<fourmi <<" fourmis" <<endl;
        for(y=0; y<ymax; y++)
        {
            for(x=0; x<xmax; x++)
            {
                C(x,y,z)=C(x,y,z+1);
            }
        }
        generation++;
        Emperor_SetGadgetAttr(String1, inttostring(generation));
        if(IO_boucle())
        {
            goto interruption;
        }
    }
    interruption:
    DoMethod(winobj, WM_CLOSE);
    win=NULL;
    winobj=NULL;
}

void Button3_GadgetUp_Event(void)
{
}

void Button4_GadgetUp_Event(void)
{
}

void Shutdown(void)
{
    ReleasePen(Screen1->ViewPort.ColorMap, noir);
    ReleasePen(Screen1->ViewPort.ColorMap, blanc);
    ReleasePen(Screen1->ViewPort.ColorMap, gris1);
    ReleasePen(Screen1->ViewPort.ColorMap, gris2);
    ReleasePen(Screen1->ViewPort.ColorMap, gris3);
    ReleasePen(Screen1->ViewPort.ColorMap, gris4);
    ReleasePen(Screen1->ViewPort.ColorMap, gris5);
    ReleasePen(Screen1->ViewPort.ColorMap, gris6);
    ReleasePen(Screen1->ViewPort.ColorMap, gris7);
    ReleasePen(Screen1->ViewPort.ColorMap, gris8);
    ReleasePen(Screen1->ViewPort.ColorMap, gris9);
    ReleasePen(Screen1->ViewPort.ColorMap, gris10);
    ReleasePen(Screen1->ViewPort.ColorMap, gris11);
    ReleasePen(Screen1->ViewPort.ColorMap, gris12);
    ReleasePen(Screen1->ViewPort.ColorMap, gris13);
    ReleasePen(Screen1->ViewPort.ColorMap, gris14);
    ReleasePen(Screen1->ViewPort.ColorMap, red1);
}

BOOL IO_boucle()
{
    BOOL pause=FALSE, close=FALSE;
    Emperor_Input=0L;
    do
    {
        while((Emperor_Input = RA_HandleInput(WindowObject1, &Emperor_Code)) != WMHI_LASTMSG)
        {
            switch(Emperor_Input & WMHI_CLASSMASK)
            {
                case WMHI_GADGETUP:
                    switch(Emperor_Input & WMHI_GADGETMASK)
                    {
                        case 15:
                            if(pause==TRUE)
                            {
                                pause=FALSE;
                            }
                            else
                            {
                                pause=TRUE;
                            }
                            break;
                        case 16:
                            SetGadgetAttrs(Button3, Window1, NULL, GA_Selected, FALSE, TAG_DONE);
                            close=TRUE;
                            return close;
                            break;
                        default:
                            pause=FALSE;
                            break;
                    }
                case WMHI_MOUSEMOVE:
                    //printf("%d ", win->MouseX, " en x");
                    //printf("%d ", win->MouseY, " en y");
                    break;
                case WMHI_MOUSEBUTTONS:
                    //printf("souris ");
                    break;
                default:
                    break;   
            }
        }
    }
    while(pause==TRUE);
}

void Menu_Quit1_MenuPick_Event(void)
{
    if(Quitrequest1())
    {
        Emperor_CloseWindow_Window1();
    }
}

void Menu_Information1_MenuPick_Event(void)
{
    Inforequest1();
}
