home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 108.lha / Labyrinthe.c < prev    next >
C/C++ Source or Header  |  1988-03-02  |  21KB  |  751 lines

  1. /************************************************************************
  2. **************************************************************************
  3. **                                                                      **
  4. **  Labyrinthe.c                                                        **
  5. **  ------------                                                        **
  6. **                                                                      **
  7. **  Auteur : Eric Le Saux                                               **
  8. **                                                                      **
  9. **           Club Amiga Montreal (CAM)                                  **
  10. **           C.P. 195 STATION "N"                                       **
  11. **           Montreal (Quebec)                                          **
  12. **           H2X 3M2, CANADA                                            **
  13. **                                                                      **
  14. **  Date   : 7 mars 1988                                                **
  15. **                                                                      **
  16. **  Statut : DOMAINE PUBLIC - Distribution non-commerciale seulement    **
  17. **           PUBLIC DOMAIN  - Freely redistributable                    **
  18. **                                                                      **
  19. **************************************************************************
  20.  ************************************************************************/
  21.  
  22. /*** Fichiers d'entetes *************************************************/
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <intuition/intuition.h>
  27. #include <libraries/dos.h>
  28. #include <libraries/dosextens.h>
  29. #include <graphics/view.h>
  30.  
  31. /*** Fonctions et variables externes ************************************/
  32.  
  33. extern struct Library        *OpenLibrary ();
  34. extern struct TextFont        *OpenFont    ();
  35. extern struct Screen        *OpenScreen  ();
  36. extern struct Window        *OpenWindow  ();
  37. extern struct IntuiMessage    *GetMsg      ();
  38. extern struct Process           *FindTask    ();
  39.  
  40. extern int Enable_Abort;
  41.  
  42. /*** Structures pour la fenetre d'instructions **************************/
  43.  
  44. #define GadgRechercheID 0
  45. #define GadgCreationID  1
  46. #define Gadg_640_400_ID 2
  47. #define Gadg_640_200_ID 3
  48. #define Gadg_320_400_ID 4
  49. #define Gadg_320_200_ID 5
  50.  
  51.  
  52. SHORT BorderVectors1[] = {0,0,86,0,86,61,0,61,0,0};
  53. struct Border Border1 = {-1,-1,3,0,JAM1,5,BorderVectors1,NULL};
  54. struct IntuiText IText3 = {1,0,JAM1,17,40,NULL,(UBYTE *)"Sortie",NULL};
  55. struct IntuiText IText2 = {1,0,JAM1,21,26,NULL,(UBYTE *)"de la",&IText3};
  56. struct IntuiText IText1 = {1,0,JAM1,7,12,NULL,(UBYTE *)"Recherche",&IText2};
  57. struct Gadget GadgRecherche =
  58. {
  59.     NULL,97,78,85,60,
  60.     NULL,RELVERIFY,BOOLGADGET,
  61.     (APTR)&Border1,NULL,&IText1,
  62.     NULL,NULL,GadgRechercheID,NULL
  63. };
  64.  
  65. SHORT BorderVectors2[] = {0,0,86,0,86,61,0,61,0,0};
  66. struct Border Border2 = {-1,-1,3,0,JAM1,5,BorderVectors2,NULL};
  67. struct IntuiText IText6 = {1,0,JAM1,2,40,NULL,(UBYTE *)"Labyrinthe",NULL};
  68. struct IntuiText IText5 = {1,0,JAM1,34,26,NULL,(UBYTE *)"du",&IText6};
  69. struct IntuiText IText4 = {1,0,JAM1,10,12,NULL,(UBYTE *)"Creation",&IText5};
  70. struct Gadget GadgCreation =
  71. {
  72.     &GadgRecherche,97,14,85,60,
  73.     NULL,RELVERIFY,BOOLGADGET,
  74.     (APTR)&Border2,NULL,&IText4,
  75.     NULL,NULL,GadgCreationID,NULL
  76. };
  77.  
  78. SHORT BorderVectors3[] = {0,0,86,0,86,29,0,29,0,0};
  79. struct Border Border3 = {-1,-1,3,0,JAM1,5,BorderVectors3,NULL};
  80. struct IntuiText IText7 = {2,0,JAM1,13,10,NULL,(UBYTE *)"640x400",NULL};
  81. struct Gadget Gadg_640_400 =
  82. {
  83.     &GadgCreation,7,110,85,28,
  84.     NULL,RELVERIFY|TOGGLESELECT,BOOLGADGET,
  85.     (APTR)&Border3,NULL,&IText7,
  86.     NULL,NULL,Gadg_640_400_ID,NULL
  87. };
  88.  
  89. SHORT BorderVectors4[] = {0,0,86,0,86,29,0,29,0,0};
  90. struct Border Border4 = {-1,-1,3,0,JAM1,5,BorderVectors4,NULL};
  91. struct IntuiText IText8 = {2,0,JAM1,13,10,NULL,(UBYTE *)"640x200",NULL};
  92. struct Gadget Gadg_640_200 =
  93. {
  94.     &Gadg_640_400,7,78,85,28,
  95.     NULL,RELVERIFY|TOGGLESELECT,BOOLGADGET,
  96.     (APTR)&Border4,NULL,&IText8,
  97.     NULL,NULL,Gadg_640_200_ID,NULL
  98. };
  99.  
  100. SHORT BorderVectors5[] = {0,0,86,0,86,29,0,29,0,0};
  101. struct Border Border5 = {-1,-1,3,0,JAM1,5,BorderVectors5,NULL};
  102. struct IntuiText IText9 = {2,0,JAM1,13,10,NULL,(UBYTE *)"320x400",NULL};
  103. struct Gadget Gadg_320_400 =
  104. {
  105.     &Gadg_640_200,7,46,85,28,
  106.     NULL,RELVERIFY|TOGGLESELECT,BOOLGADGET,
  107.     (APTR)&Border5,NULL,&IText9,
  108.     NULL,NULL,Gadg_320_400_ID,NULL
  109. };
  110.  
  111. SHORT BorderVectors6[] = {0,0,86,0,86,29,0,29,0,0};
  112. struct Border Border6 = {-1,-1,3,0,JAM1,5,BorderVectors6,NULL};
  113. struct IntuiText IText10 = {2,0,JAM1,13,10,NULL,(UBYTE *)"320x200",NULL};
  114. struct Gadget Gadg_320_200 =
  115. {
  116.     &Gadg_320_400,7,14,85,28,
  117.     NULL,RELVERIFY|TOGGLESELECT,BOOLGADGET,
  118.     (APTR)&Border6,NULL,&IText10,
  119.     NULL,NULL,Gadg_320_200_ID,NULL
  120. };
  121.  
  122. #define GadgetList Gadg_320_200
  123.  
  124. struct Window *FenetreWB;
  125. struct NewWindow FenetreWBData =
  126. {
  127.     169,20,189,143,1,2,
  128.     GADGETUP|CLOSEWINDOW|MOUSEBUTTONS,
  129.     WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|RMBTRAP|ACTIVATE|SMART_REFRESH,
  130.     &GadgetList,NULL,(UBYTE *)" Labyrinthe  ",
  131.     NULL,NULL,5,5,640,200,WBENCHSCREEN
  132. };
  133.  
  134. /*** Variables globales pour le maintient du systeme ********************/
  135.  
  136. SHORT Largeur, Hauteur;
  137. #define Profond 2
  138.  
  139. #define CarMinX 1
  140. #define CarMinY 1
  141. SHORT CarMaxX, CarMaxY;
  142.  
  143. #define AbsMinX 0
  144. #define AbsMinY 0
  145. USHORT  AbsMaxX, AbsMaxY;
  146.  
  147. struct IntuitionBase     *IntuitionBase;
  148. struct GfxBase           *GfxBase;
  149.  
  150. struct TextAttr      FonteAttr = { (UBYTE *) "topaz.font", 8, 0, 0 };
  151. struct TextFont     *Fonte;
  152.  
  153. struct Screen        *Ecran;
  154. struct NewScreen    EcranData =
  155.             {
  156.                0,0,0,0,Profond,
  157.                0,1,HIRES|LACE,CUSTOMSCREEN|SCREENBEHIND,&FonteAttr,
  158.                (UBYTE *) "Labyrinthe -ELS- Club Amiga Montreal", NULL, NULL
  159.             };
  160.  
  161. struct Window         *Fenetre;
  162. struct NewWindow    FenetreData =
  163.             {
  164.                0,0,0,0,-1,-1,NULL,
  165.                BACKDROP|BORDERLESS|RMBTRAP|SMART_REFRESH,
  166.                NULL,NULL,NULL,NULL,NULL,0,0,0,0,CUSTOMSCREEN
  167.             };
  168.  
  169. struct RastPort        *RP;
  170.  
  171.  
  172. /*************************************************************************
  173. **************************************************************************
  174. **
  175. **  Sortie ()
  176. **
  177. **  Ferme ce qui a ete ouvert.
  178. **
  179. */
  180.  
  181. void Sortie ()
  182. {
  183.     if (FenetreWB)     CloseWindow  (FenetreWB);
  184.     if (Fenetre)       CloseWindow  (Fenetre);
  185.     if (Ecran)       CloseScreen  (Ecran);
  186.     if (IntuitionBase) CloseLibrary (IntuitionBase);
  187.     if (GfxBase)       CloseLibrary (GfxBase);
  188.     if (Fonte)       CloseFont    (Fonte);
  189.  
  190.     _exit (0L);
  191. }
  192.  
  193.  
  194. /*************************************************************************
  195. **************************************************************************
  196. **
  197. **  Erreur ()
  198. **
  199. **  Affiche un message d'erreur a travers un requester.
  200. **  Laisse le choix entre l'abandon de la procedure et
  201. **  un autre essai.
  202. **
  203. */
  204.  
  205. #define ErrGfxLib   0
  206. #define ErrIntuiLib 1
  207. #define ErrOpenWin  2
  208. #define ErrOpenScr  3
  209. #define ErrFonte    4
  210.  
  211. struct IntuiText Message = { 2, 1, JAM1, 17, 10, NULL, NULL, NULL };
  212. struct IntuiText Essai   = { 2, 1, JAM1, 5, 4, NULL, (UBYTE *) "Tentative", NULL };
  213. struct IntuiText Abandon = { 2, 1, JAM1, 5, 4, NULL, (UBYTE *) "Abandon", NULL };
  214.  
  215. void Erreur (Err)
  216. SHORT Err;
  217. {
  218.     struct Process *Proc;
  219.     SHORT EssaieEncore;
  220.  
  221.     switch (Err)
  222.     {
  223.        case ErrGfxLib   : Message.IText = (UBYTE *) "Erreur: OpenLibrary: GfxLib";
  224.                           break;
  225.        case ErrIntuiLib : Message.IText = (UBYTE *) "Erreur: OpenLibrary: IntuiLib)";
  226.                           break;
  227.        case ErrOpenWin  : Message.IText = (UBYTE *) "Erreur: OpenWindow";
  228.                           break;
  229.        case ErrOpenScr  : Message.IText = (UBYTE *) "Erreur: OpenScreen";
  230.                           break;
  231.        case ErrFonte    : Message.IText = (UBYTE *) "Erreur: OpenFont: Topaz 8";
  232.                           break;
  233.        default          : Message.IText = (UBYTE *) "Erreur inconnue !";
  234.                           break;
  235.     }
  236.  
  237.    /*=== On envoie le requester sur le Workbench ===*/
  238.  
  239.     Proc = FindTask (0L);
  240.     EssaieEncore = AutoRequest (Proc->pr_WindowPtr, &Message, &Essai, &Abandon,
  241.                                 NULL, NULL, 300L, 60L);
  242.  
  243.     if (EssaieEncore) return;
  244.  
  245.     Sortie ();
  246. }
  247.  
  248.  
  249. /*************************************************************************
  250. **************************************************************************
  251. **
  252. **  SelectCouleurs ()
  253. **
  254. **  On s'arrange pour que les deux premieres couleurs de l'ecran passe
  255. **  en parametre soient dans un ordre decroissant d'intensite.
  256. */
  257.  
  258. void SelectCouleurs (Ecran)
  259. struct Screen *Ecran;
  260. {
  261.     UWORD *Couleurs;
  262.     UWORD Moy0, Moy1;
  263.  
  264.     Couleurs = (UWORD *) (Ecran->ViewPort.ColorMap->ColorTable);
  265.  
  266.     Moy0 = (((Couleurs[0]&0x0F00)>>8)+((Couleurs[0]&0x00F0)>>4)+(Couleurs[0]&0x000F))/3;
  267.     Moy1 = (((Couleurs[1]&0x0F00)>>8)+((Couleurs[1]&0x00F0)>>4)+(Couleurs[1]&0x000F))/3;
  268.  
  269.     if (Moy0 < Moy1)
  270.     {
  271.        Moy0 = Couleurs[0];
  272.        Couleurs[0] = Couleurs[1];
  273.        Couleurs[1] = Moy0;
  274.     }
  275.  
  276.     Couleurs[1] = 0x0000;
  277.     Couleurs[2] = 0x0400;
  278.     Couleurs[3] = 0x0D00;
  279.  
  280.     MakeScreen (Ecran);
  281.     RethinkDisplay ();
  282.  
  283. }
  284.  
  285.  
  286. /*************************************************************************
  287. **************************************************************************
  288. **
  289. **  AfficheCarrefours ()
  290. **
  291. */
  292.  
  293. void AfficheCarrefours ()
  294. {
  295.     SHORT Index;
  296.  
  297.     SetRast (RP, 0L);
  298.     SetAPen (RP, 1L);
  299.  
  300.     RP->LinePtrn = 0xAAAA;
  301.  
  302.     for (Index=0; Index<(FenetreData.Height-1); Index+=2)
  303.     {
  304.        Move (RP, 0L, (LONG)Index);
  305.        Draw (RP, (LONG)FenetreData.Width-1L, (LONG)Index);
  306.     }
  307.  
  308.     RP->LinePtrn = 0xFFFF;
  309. }
  310.  
  311.  
  312. /*************************************************************************
  313. **************************************************************************
  314. **
  315. **  OuvreFenetre ()
  316. **
  317. */
  318.  
  319. #define Mode_320_200 0
  320. #define Mode_320_400 1
  321. #define Mode_640_200 2
  322. #define Mode_640_400 3
  323.  
  324. void OuvreFenetre (Mode)
  325. UBYTE Mode;
  326. {
  327.     USHORT ModeVision;
  328.  
  329.    /*=== On ferme si c'etait ouvert ===*/
  330.  
  331.     if (Fenetre) CloseWindow (Fenetre);
  332.     if (Ecran)   CloseScreen (Ecran);
  333.  
  334.    /*=== On change l'etat des gadgets de resolutions ===*/
  335.  
  336.     RemoveGList (FenetreWB, &GadgetList, 4L);
  337.     GadgetList.Flags &= ~SELECTED;
  338.     GadgetList.NextGadget->Flags &= ~SELECTED;
  339.     GadgetList.NextGadget->NextGadget->Flags &= ~SELECTED;
  340.     GadgetList.NextGadget->NextGadget->NextGadget->Flags &= ~SELECTED;
  341.  
  342.    /*=== On initialise les structures ===*/
  343.  
  344.     switch (Mode)
  345.     {
  346.        case Mode_320_200 : Largeur = 320;
  347.                            Hauteur = 200;
  348.                            ModeVision = NULL;
  349.                            Gadg_320_200.Flags |= SELECTED;
  350.                            break;
  351.        case Mode_320_400 : Largeur = 320;
  352.                            Hauteur = 400;
  353.                            ModeVision = LACE;
  354.                            Gadg_320_400.Flags |= SELECTED;
  355.                            break;
  356.        case Mode_640_200 : Largeur = 640;
  357.                            Hauteur = 200;
  358.                            ModeVision = HIRES;
  359.                            Gadg_640_200.Flags |= SELECTED;
  360.                            break;
  361.        case Mode_640_400 : Largeur = 640;
  362.                            Hauteur = 400;
  363.                            ModeVision = HIRES|LACE;
  364.                            Gadg_640_400.Flags |= SELECTED;
  365.                            break;
  366.     }
  367.  
  368.     CarMaxX = Largeur/2;  CarMaxY = Hauteur/2;
  369.     AbsMaxX = Largeur-1;  AbsMaxY = Hauteur-1;
  370.  
  371.    /*=== On remet les gadgets ===*/
  372.  
  373.     AddGList (FenetreWB, &GadgetList, 0L, 4L, NULL);
  374.     RefreshGList (&GadgetList, FenetreWB, NULL, 4L);
  375.  
  376.    /*=== On ouvre l'ecran ===*/
  377.  
  378.     EcranData.Width  = Largeur;
  379.     EcranData.Height = Hauteur;
  380.     EcranData.ViewModes = ModeVision;
  381.  
  382.     FOREVER
  383.     {
  384.        Ecran = OpenScreen (&EcranData);
  385.        if (!Ecran) Erreur (ErrOpenScr);
  386.               else break;
  387.     }
  388.  
  389.     SelectCouleurs (Ecran);
  390.     ShowTitle (Ecran, (LONG)FALSE);
  391.  
  392.    /*=== On ouvre la fenetre ===*/
  393.  
  394.     FenetreData.Screen = Ecran;
  395.     FenetreData.Width  = Largeur;
  396.     FenetreData.Height = Hauteur;
  397.  
  398.     FOREVER
  399.     {
  400.        Fenetre = OpenWindow (&FenetreData);
  401.        if (!Fenetre) Erreur (ErrOpenWin);
  402.                 else break;
  403.     }
  404.  
  405.    /*=== On pointe le RastPort de la fenetre ===*/
  406.  
  407.     RP = Fenetre->RPort;
  408.  
  409.    /*=== On prepare l'ecran ===*/
  410.  
  411.     AfficheCarrefours ();
  412. }
  413.  
  414.  
  415. /*************************************************************************
  416. **************************************************************************
  417. **
  418. **  Rnd ()
  419. **
  420. **  Retourne un nb dans un intervalle donne, bornes incluses.
  421. **  Le premier argument devrait etre plus petit que le second.
  422. **
  423. */
  424.  
  425. USHORT Rnd (N1, N2)
  426. USHORT N1, N2;
  427. {
  428.     return ( ( ((rand()&0x00FF) * (N2-N1+1)) >> 8 ) + N1 );
  429. }
  430.  
  431.  
  432. /*************************************************************************
  433. **************************************************************************
  434. **
  435. **  Initialise ()
  436. **
  437. */
  438.  
  439. void Initialise ()
  440. {
  441.     ULONG Secondes, Micros;       /* Temps courant */
  442.  
  443.    /*=== La librairie Intuition ===*/
  444.    /*=== Si on n'a pas la bonne version, on ouvre celle presente pour ===*/
  445.    /*=== appeler la fonction AutoRequest ().                          ===*/
  446.  
  447.     FOREVER
  448.     {
  449.        IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 33L); /* v1.2+ */
  450.        if (!IntuitionBase)
  451.        {
  452.           IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 0L);
  453.           if (!IntuitionBase) Sortie ();   /* Plus le choix ! */
  454.           Erreur (ErrIntuiLib);
  455.           Sortie ();
  456.        }
  457.        else break;
  458.     }
  459.  
  460.    /*=== La librairie graphique ===*/
  461.  
  462.     FOREVER
  463.     {
  464.        GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", 0L);
  465.        if (!GfxBase) Erreur (ErrGfxLib);
  466.                 else break;
  467.     }
  468.  
  469.    /*=== La ROMFonte par defaut ===*/
  470.  
  471.     FOREVER
  472.     {
  473.        Fonte = OpenFont (&FonteAttr);
  474.        if (!Fonte) Erreur (ErrFonte);
  475.               else break;
  476.     }
  477.  
  478.    /*=== Ouverture de la fenetre de controle ===*/
  479.  
  480.     FOREVER
  481.     {
  482.        FenetreWB = OpenWindow (&FenetreWBData);
  483.        if (!FenetreWB) Erreur (ErrOpenWin);
  484.                   else break;
  485.     }
  486.  
  487.     SetFont (FenetreWB->RPort, Fonte);
  488.  
  489.    /*=== Init de la fonction aleatoire ===*/
  490.  
  491.     CurrentTime (&Secondes, &Micros);
  492.     srand ( (SHORT) (Secondes&0x00FF) );
  493.     Rnd (0,0); Rnd (0,0); Rnd (0,0);
  494. }
  495.  
  496.  
  497. /*************************************************************************
  498. **************************************************************************
  499. **
  500. **  _abort ()
  501. **
  502. **  On remplace quelques fonctions par defaut.
  503. **
  504. */
  505.  
  506. void _abort () { Sortie (); }
  507. void _cli_parse () {}
  508. void _wb_parse () {}
  509.  
  510. /*************************************************************************
  511. **************************************************************************
  512. **
  513. **  Cvt ()
  514. **
  515. **  Converti de coordonnees carrefour a coordonnees raster.
  516. **
  517. */
  518.  
  519. ULONG Cvt (X)
  520. USHORT X;
  521. {
  522.     return (ULONG) ( (X-1) << 1 );
  523. }
  524.  
  525.  
  526. /*************************************************************************
  527. **************************************************************************
  528. **
  529. **  Laby ()
  530. **
  531. */
  532.  
  533. void Laby (MinX, MaxX, MinY, MaxY)
  534. USHORT MinX, MaxX, MinY, MaxY;
  535. {
  536.     USHORT MoyX, MoyY, dX, dY, Cote;
  537.  
  538.     if ( (MinX==MaxX) || (MinY==MaxY) )
  539.     {
  540.        Move (RP, Cvt(MinX), Cvt(MinY));
  541.        Draw (RP, Cvt(MaxX), Cvt(MaxY));
  542.     }
  543.     else
  544.     {
  545.        Cote = Rnd (1,4);
  546.        MoyX = (MaxX+MinX)/2; dX = (MaxX-MinX+1)/5;
  547.        MoyY = (MaxY+MinY)/2; dY = (MaxY-MinY+1)/5;
  548.        MoyX = Rnd (MoyX-dX,MoyX+dX-1);
  549.        MoyY = Rnd (MoyY-dY,MoyY+dY-1);
  550.  
  551.        Laby ( MinX,MoyX,   MinY,MoyY   );
  552.        Laby ( MoyX+1,MaxX, MinY,MoyY   );
  553.        Laby ( MoyX+1,MaxX, MoyY+1,MaxY );
  554.        Laby ( MinX,MoyX,   MoyY+1,MaxY );
  555.  
  556.        if (Cote != 1) WritePixel (RP, Cvt(MoyX)+1L, Cvt(Rnd(MinY,MoyY)));
  557.        if (Cote != 3) WritePixel (RP, Cvt(MoyX)+1L, Cvt(Rnd(MoyY+1,MaxY)));
  558.        if (Cote != 2) WritePixel (RP, Cvt(Rnd(MinX,MoyX)), Cvt(MoyY)+1L);
  559.        if (Cote != 4) WritePixel (RP, Cvt(Rnd(MoyX+1,MaxX)), Cvt(MoyY)+1L);
  560.     };
  561. }
  562.  
  563.  
  564. /*************************************************************************
  565. **************************************************************************
  566. **
  567. **  Recherche ()
  568. **
  569. */
  570.  
  571. void NewPos (X1,Y1,X2,Y2,Dir,Dist)
  572. USHORT X1,Y1,*X2,*Y2; UBYTE Dir; USHORT Dist;
  573. {
  574.     switch (Dir)
  575.     {
  576.        case 0 : *X2 = X1;      *Y2 = Y1-Dist; break;
  577.        case 1 : *X2 = X1+Dist; *Y2 = Y1;      break;
  578.        case 2 : *X2 = X1;      *Y2 = Y1+Dist; break;
  579.        case 3 : *X2 = X1-Dist; *Y2 = Y1;      break;
  580.     }
  581. }
  582.  
  583. BOOL PasDeMur (X,Y,Dir)
  584. USHORT X,Y; UBYTE Dir;
  585. {
  586.     USHORT nX,nY;
  587.  
  588.     NewPos (X,Y,&nX,&nY,Dir,1);
  589.  
  590.     if ( (nX<AbsMinX) || (nX>AbsMaxX) || (nY<AbsMinY) || (nY>AbsMaxY) )
  591.        return (FALSE);
  592.     else
  593.        return (ReadPixel(RP, (ULONG)nX, (ULONG)nY)!=0);
  594. }
  595.  
  596. #define FilArianne 3
  597. #define Trace      2
  598.  
  599. void Avance (X,Y,Dir)
  600. USHORT *X,*Y; UBYTE Dir;
  601. {
  602.     USHORT nX,nY;
  603.  
  604.     NewPos (*X,*Y,&nX,&nY,Dir,2);
  605.  
  606.     if ( ReadPixel(RP,(ULONG)nX,(ULONG)nY) == FilArianne )
  607.        SetAPen (RP, (ULONG)Trace);
  608.     else
  609.        SetAPen (RP, (ULONG)FilArianne);
  610.  
  611.     Draw (RP,(ULONG)nX,(ULONG)nY);
  612.  
  613.     *X = nX;  *Y = nY;
  614. }
  615.  
  616. void Recherche (DepartX, DepartY, ArriveeX, ArriveeY)
  617. USHORT DepartX, DepartY, ArriveeX, ArriveeY;
  618. {
  619.     register UBYTE Dir; /* 0..3 */
  620.     USHORT X1, Y1;
  621.     register USHORT X2, Y2;
  622.  
  623.     X1 = (USHORT) Cvt (DepartX);  (USHORT) Y1 = Cvt (DepartY);
  624.     X2 = (USHORT) Cvt (ArriveeX); (USHORT) Y2 = Cvt (ArriveeY);
  625.     Dir = 1;
  626.  
  627.     Move (RP, (ULONG)X1, (ULONG)Y1);
  628.  
  629.     while ( (X1 != X2) || (Y1 != Y2) )
  630.     {
  631.        if ( PasDeMur(X1,Y1,(Dir+1)%4) ) /* Pas de mur a droite ? */
  632.        {
  633.           Avance (&X1,&Y1,Dir=(Dir+1)%4);
  634.           continue;
  635.        }
  636.  
  637.        if ( PasDeMur(X1,Y1,Dir) ) /* Pas de mur devant ? */
  638.        {
  639.           Avance (&X1,&Y1,Dir);
  640.           continue;
  641.        }
  642.  
  643.        Dir = (Dir+2)%4; /* Cul-de-sac : volte-face */
  644.     }
  645. }
  646.  
  647.  
  648. /*************************************************************************
  649. **************************************************************************
  650. **************************************************************************
  651. ***
  652. ***  main ()
  653. ***
  654. **/
  655.  
  656. main ()
  657. {
  658.     struct IntuiMessage *Msg;     /* Pointeur pour prendre les messages */
  659.     ULONG Classe;                 /* Pour noter la classe du message */
  660.     USHORT Code;                  /* Pour noter le code du message */
  661.     struct Gadget *Adresse;       /* Pour noter l'addresse du gadget */
  662.  
  663.     BOOL LabyExiste = FALSE;      /* TRUE quand il y a un labyrinthe a fouiller */
  664.     BOOL TitleOn = FALSE;
  665.     BOOL LabyCree = FALSE;
  666.  
  667.    /*=== Init ===*/
  668.  
  669.     Initialise ();
  670.  
  671.    /*=== On attends un evenement: IntuiMessage ou CTRL_C. ===*/
  672.  
  673.     FOREVER
  674.     {
  675.        if (Wait (SIGBREAKF_CTRL_C|1L<<FenetreWB->UserPort->mp_SigBit)==SIGBREAKF_CTRL_C)
  676.           Sortie ();
  677.  
  678.        while (Msg=GetMsg(FenetreWB->UserPort))
  679.        {
  680.           Classe  = Msg->Class;
  681.           Code    = Msg->Code;
  682.           Adresse = (struct Gadget *) Msg->IAddress;
  683.           ReplyMsg (Msg);
  684.  
  685.           if (Classe == CLOSEWINDOW) Sortie ();
  686.  
  687.           if (Classe==MOUSEBUTTONS)
  688.           {
  689.              if (!LabyExiste || Code!=MENUDOWN) continue;
  690.  
  691.              if (TitleOn)
  692.              {
  693.                 ShowTitle (Ecran, (LONG)(TitleOn=FALSE));
  694.              }
  695.              else
  696.              {
  697.                 ShowTitle (Ecran, (LONG)(TitleOn=TRUE));
  698.                 ScreenToFront (Ecran);
  699.              }
  700.  
  701.              continue; /* le while */
  702.           }
  703.  
  704.           switch (Adresse->GadgetID)
  705.           {
  706.              case GadgRechercheID : if (LabyCree)
  707.                                     {
  708.                                        ShowTitle (Ecran, (LONG)(TitleOn=FALSE));
  709.                                        ScreenToFront (Ecran);
  710.                                        Recherche (CarMinX, CarMinY, CarMaxX, CarMaxY);
  711.                                        LabyCree = FALSE;
  712.                                     }
  713.                                     else DisplayBeep (0L);
  714.                                     break;
  715.  
  716.              case GadgCreationID  : if (LabyExiste)
  717.                                     {
  718.                                        AfficheCarrefours ();
  719.                                        ShowTitle (Ecran, (LONG)(TitleOn=FALSE));
  720.                                        ScreenToFront (Ecran);
  721.                                        Laby (CarMinX, CarMaxX, CarMinY, CarMaxY);
  722.                                        LabyCree = TRUE;
  723.                                     }
  724.                                     else DisplayBeep (0L);
  725.                                     break;
  726.  
  727.              case Gadg_640_400_ID : OuvreFenetre (Mode_640_400);
  728.                                     TitleOn = FALSE;
  729.                                     LabyExiste = TRUE;
  730.                                     LabyCree = FALSE;
  731.                                     break;
  732.              case Gadg_640_200_ID : OuvreFenetre (Mode_640_200);
  733.                                     TitleOn = FALSE;
  734.                                     LabyExiste = TRUE;
  735.                                     LabyCree = FALSE;
  736.                                     break;
  737.              case Gadg_320_400_ID : OuvreFenetre (Mode_320_400);
  738.                                     TitleOn = FALSE;
  739.                                     LabyExiste = TRUE;
  740.                                     LabyCree = FALSE;
  741.                                     break;
  742.              case Gadg_320_200_ID : OuvreFenetre (Mode_320_200);
  743.                                     TitleOn = FALSE;
  744.                                     LabyExiste = TRUE;
  745.                                     LabyCree = FALSE;
  746.                                     break;
  747.           }
  748.        }
  749.     }
  750. }
  751.