home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Games / dynAMIte / Developer / C / BotFrame.c next >
Encoding:
C/C++ Source or Header  |  2001-07-22  |  2.6 KB  |  142 lines

  1. #include    <stdlib.h>
  2. #include     <stdio.h>
  3. #include    <time.h>
  4.  
  5. #include     <exec/semaphores.h>
  6. #include     <dos/dos.h>
  7. #include     <proto/exec.h>
  8. #include     <proto/dos.h>
  9.  
  10. #include     "dynamite.h"
  11.  
  12.  
  13. BOOL        checkDynamite (struct dynamitesemaphore ** );
  14. void        mainLoop (struct dynamitesemaphore *);
  15. void        doAI (struct dynamitesemaphore *);
  16.  
  17. LONG thisbotnum
  18.  
  19. int
  20. main (void)
  21. {
  22.     struct dynamitesemaphore    *dynasema;
  23.  
  24.  
  25.     srand (time(NULL));
  26.  
  27.     if ( FALSE == checkDynamite (&dynasema) )
  28.     {
  29.         puts ("*** DynAMIte not running!");
  30.         return EXIT_FAILURE;
  31.     }
  32.  
  33.     /* some information */
  34.     printf ("Number of clients: %Ld\n",dynasema->opencnt);
  35.  
  36.     /* enter main loop */
  37.     mainLoop (dynasema);
  38.  
  39.     /* decrease opencount to tell dynamite that you no
  40.     longer need the semaphore. dynamite will remove the
  41.     semaphore only if opencnt is 0 at the end */
  42.     ObtainSemaphore(&dynasema->sema);
  43.  
  44.   /* reset direction */
  45.   dynasema->walk=DIR_NONE;
  46.  
  47.   /* set botinfo entry back to 0 */
  48.   dynasema->botinfo[thisbotnum]=NULL;
  49.  
  50.     dynasema->opencnt--;
  51.     ReleaseSemaphore(&dynasema->sema);
  52.  
  53.     return EXIT_SUCCESS;
  54. }
  55.  
  56. BOOL
  57. checkDynamite (struct dynamitesemaphore ** dynasema )
  58. {
  59.     Forbid();    /* try to find the semaphore */
  60.     if (*dynasema = (struct dynamitesemaphore *)FindSemaphore ("dynAMIte.0")
  61.         {
  62.         /* increase opencount to tell dynamite that you are using the
  63.         semaphore. dynamite will remove semaphore only if opencnt is
  64.         0 at its end */
  65.  
  66.         ++ dynasema->opencnt;
  67.  
  68.     thisbotnum = dynasema->opencnt;
  69.  
  70.     /* set botinfo string */
  71.     dynasema->botinfo[dynasema->opencnt]=(char*)"dynamite sample bot v1.0 - doing nothing useful"
  72.  
  73.         }
  74.     Permit();
  75.  
  76.     if ( NULL == *dynasema )
  77.         return FALSE;
  78.  
  79.     return TRUE;
  80. }
  81.  
  82. void
  83. mainLoop (struct dynamitesemaphore * dynasema)
  84. {
  85.     struct player                 *ourplayer;
  86.     BOOL                        done = FALSE,
  87.                                 delay;
  88.  
  89.     while (FALSE == done)
  90.     {
  91.         if (CheckSignal (SIGBREAKF_CTRL_C))
  92.             done = TRUE;
  93.         else
  94.         {
  95.             delay = TRUE;
  96.             ObtainSemaphore (&dynasema->sema);
  97.  
  98.             if (dynasema->quit)
  99.             {
  100.                 /* dynamite wants to quit, so we do dynamite a favour */
  101.                 printf ("dynAMIte is about to quit...\n");
  102.                 done = TRUE;
  103.             }
  104.             else
  105.             {
  106.                 /* if a game is running */
  107.                 if (dynasema->gamerunning>=GAME_GAME)
  108.                 {
  109.                     /* and player is no observer */
  110.                     if (dynasema->thisplayer < 8)
  111.                     {
  112.                         delay = FALSE;
  113.  
  114.                         ourplayer = dynasema->players[dynasema->thisplayer];
  115.  
  116.                         /* and our player is alive */
  117.                         if (ourplayer->dead)
  118.                             doAI (dynasema);
  119.                     }
  120.                 }
  121.             }
  122.  
  123.             ReleaseSemaphore (&dynasema->sema);
  124.  
  125.             if (delay)
  126.             {
  127.                 /* no game is running */
  128.                 /* do a small delay to let the cpu do other things :( */
  129.                 Delay (10);
  130.             }
  131.  
  132.         }
  133.     }
  134. }
  135.  
  136. void
  137. doAI (struct dynamitesemaphore * dynasema)
  138. {
  139.     dynasema->walk = rand() % DIR_UP+1;
  140. }
  141.  
  142.