home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 138.lha / Cycle / play.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  4KB  |  152 lines

  1. /*
  2.   This module contains the functions that players can use to control their
  3.   movement and find out about the playing arena. These are:
  4.  
  5.   Register()         used to register in the game (done by the manager)
  6.   Retire()           exit the game (also done by the manager)
  7.   Inquire()          find out a given cell in the arena is bstructed
  8.   GetScreenMenu()    return pointer to the arena bitmap
  9.   Direction()        specify new direction
  10.   Await()            wait until new game begins
  11.   GetInfo()          give players's current position and direction
  12.   Look()             look in given direction and return status of cell there
  13.  */
  14. #include "cycles.h"
  15. #ifdef AZTEC_C
  16. extern long Enable_Abort;
  17. #endif
  18. PORT *FindPort();
  19.  
  20. static ORDER  order;
  21. static PORT   *sPort;
  22. static PORT   *cPort;
  23. static BYTE   *screen;
  24. static long   x_size,y_size,x_bytes;
  25. static long   colour;
  26.  
  27. static xdir[] = {0,1,0,-1};  /* up, right, down, left */
  28. static ydir[] = {-1,0,1,0};  /* up, right, down, left */
  29.  
  30. /* send a message to the target and wait for a reply */
  31. MSG *Send(msg)
  32. MSG *msg;
  33. {
  34.   PutMsg(sPort,msg);
  35.   WaitPort(cPort);
  36.   return(GetMsg(cPort));
  37. }
  38. /* register this player with the cycle server returns    */
  39. /* non-zero if registration is unsuccessful also tells   */
  40. /* you which colour you will be playing (for output)     */
  41.  
  42. Register(my_colour)
  43. long *my_colour;
  44. {
  45.   cPort = CreatePort(0,0);
  46.   if (!cPort) {
  47.     printf("Can't create a reply port\n");
  48.     return(-1);
  49.   }
  50.   order.msg.mn_Node.ln_Type = NT_MESSAGE;
  51.   order.msg.mn_ReplyPort    = cPort;
  52.   order.order               = ORD_AWAIT;
  53.   order.cycle               = 0;
  54.  
  55. /* it is critical that the port not disappear between the time
  56.  * it is found and the first message is sent to it.
  57.  * This will ensure that proper shutdown will always happen
  58.  * if the server decides to terminate
  59.  */
  60.  
  61. Forbid();
  62. sPort = FindPort("Cycle Server");
  63. if (!sPort) {
  64.   Permit();
  65.   printf("Can't find Cycle Server Port\n");
  66.   DeletePort(cPort);
  67.   return(-1);
  68. }
  69. PutMsg(sPort,&order);
  70. Permit();
  71.  
  72. WaitPort(cPort);
  73. GetMsg(cPort);
  74.  
  75. if (order.status) {
  76.   printf("Server shutting down. No more clients accepted\n");
  77.   DeletePort(cPort);
  78.   return(-1);
  79. }
  80. colour    = order.cycle->colour;
  81. x_size    = order.table->x;
  82. y_size    = order.table->y;
  83. screen    = order.table->buf;
  84. x_bytes   = (x_size>>3) + !!(x_size & 7);
  85. *my_colour = colour;
  86.  
  87.   return(0);
  88. }
  89.  
  90. /* retire from the game...free resources */
  91. /* used to participate; no diagnostics   */
  92. void Retire()
  93. {
  94.   /* check to see if we're already gone. If so then no ORD_RETIRE */
  95.   if (order.status != STATUS_REMOVED) {
  96.     order.order = ORD_RETIRE;
  97.     Send(&order);
  98.   }
  99.   DeletePort(cPort);
  100. }
  101.  
  102. static long power[] = {1,2,4,8,16,32,64,128};
  103.  
  104. /* return the status of the pixel @ x,y */
  105. Inquire(x,y)
  106. {
  107.   if (x<0 || y<0 || x>=x_size || y>=y_size) return(1);
  108.   return ((screen[(x>>3) + y*x_bytes] & power[x&7]) != 0);
  109. }
  110.  
  111. /* return the location of the screen...in case the client wishes
  112.  * to do some custom testing
  113.  */
  114. IMAGE *GetScreenMem()
  115. {
  116.   return(order.table);
  117. }
  118.  
  119. /* returns status from server, non-zero indicates      */
  120. /* something exceptional e.g. LOSER or WINNER declared */
  121. Direction(dir)
  122. {
  123.   order.order  = ORD_DIRECTION;
  124.   order.dir    = dir;
  125.   Send(&order);
  126.   return(order.status);
  127. }
  128.  
  129. /* wait for new game to start */
  130. Await()
  131. {
  132.   order.order = ORD_AWAIT;
  133.   Send(&order);
  134.   return(order.status);
  135. }
  136.  
  137. /* get last known information about self from server */
  138. GetInfo(x,y,dir)
  139. long *x,*y,*dir;
  140. {
  141.   *x    = order.x;
  142.   *y    = order.y;
  143.   *dir  = order.dir;
  144. }
  145.  
  146. /* look in the given direction for a pixel */
  147. Look(dir)
  148. long dir;
  149. {
  150.   return(Inquire(order.x + xdir[dir],order.y + ydir[dir]));
  151. }
  152.