home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume4 / xrobots / part01 / game.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-04  |  6.2 KB  |  299 lines

  1. /*
  2.  * game.c  --  xrobots v1.0
  3.  *
  4.  * Most of the game logic is here.  
  5.  */
  6.  
  7. #include "game.h"
  8. #include "score.h"
  9.  
  10. /* some of these are global */
  11. int human_x, human_y, last_human_x, last_human_y;
  12.  
  13. int robot_array[MAXX][MAXY],
  14.     robot_array_bak[MAXX][MAXY];    
  15.     /* These arrays are filled with the robots and the heaps 
  16.      * They do not include the player(human).
  17.      */
  18. int score;
  19. int num_robots;
  20. int level;
  21. int game_active;
  22. int sonic_used;
  23.  
  24. /*----------------------------------------------------------------------*/
  25. void
  26. new_game()
  27. {
  28.   score = 0;
  29.   level = 0;
  30.   sonic_used = 1;
  31.   game_active = 1;
  32.   new_level();
  33.   update_score(score);
  34. }
  35.  
  36. /*----------------------------------------------------------------------*/
  37. void
  38. add_score(n)
  39.   int n;
  40. {
  41.   score += 10 * n; /* ten points for each wasted robot. */
  42.   num_robots -= n;
  43.   if(n) update_score(score);
  44. }
  45.  
  46. /*----------------------------------------------------------------------*/
  47. void
  48. new_level()
  49. {
  50.   int x,y,tmp;
  51.  
  52.   reset_sonic_button();
  53.   if(!sonic_used) {
  54.     score += 50;     /* bonus for not using sonic screwdriver on last level*/
  55.     update_score(score);
  56.   }
  57.   sonic_used = 0;
  58.   level++;
  59.   num_robots = tmp = level*5;
  60.  
  61.   for_each                            /* clean out both the robot arrays */
  62.   {
  63.     robot_array[x][y] = EMPTY;
  64.     robot_array_bak[x][y] = EMPTY;
  65.   }
  66.   
  67.   human_x = (int)random()%MAXX;                  /* assign human to new space */
  68.   human_y = (int)random()%MAXY;
  69.   robot_array[human_x][human_y] = ROBOT;
  70.  
  71.   while(tmp--)
  72.   {
  73.     x = (int)random()%MAXX;
  74.     y = (int)random()%MAXY;
  75.     if(robot_array[x][y] == ROBOT) tmp++;        /* space already occupied */
  76.     robot_array[x][y] = ROBOT;
  77.   }
  78.   robot_array[human_x][human_y] = EMPTY;
  79.   display_level();
  80.   display_possible_moves();
  81. }
  82.  
  83. /*----------------------------------------------------------------------*/
  84. int
  85. chase()
  86. {
  87.   /* chase() returns the number of robots that were wasted in each call. */
  88.   /* after each call, be sure to check if all the robots are dead */
  89.   int x,y;
  90.   int newx,newy;
  91.   int num_wasted = 0;
  92.  
  93.   for_each
  94.   {
  95.     robot_array_bak[x][y] = robot_array[x][y];
  96.     if(robot_array[x][y] != HEAP)
  97.       robot_array[x][y] = 0;
  98.   }
  99.  
  100.   for_each
  101.   {
  102.     if(robot_array_bak[x][y] != ROBOT)
  103.       continue;
  104.  
  105.     if(x>human_x)                           /* move toward the human */
  106.       newx = x-1;
  107.     else  
  108.       if(x<human_x)
  109.         newx = x+1;
  110.       else
  111.         newx = x;
  112.  
  113.     if(y>human_y) 
  114.       newy = y-1;
  115.     else  
  116.       if(y<human_y)
  117.         newy = y+1;
  118.       else
  119.         newy = y;
  120.  
  121. #   ifdef DEBUG
  122.     printf("moving (%d,%d) to (%d,%d)\n",x,y,newx,newy);
  123. #   endif
  124.  
  125.     /* check to see if a robot or heap was already in that spot */
  126.     if(robot_array[newx][newy] == ROBOT)
  127.     {
  128.       robot_array[newx][newy] = HEAP;
  129.       num_wasted += 2;
  130.       continue;
  131.     }
  132.     if(robot_array[newx][newy] == HEAP)
  133.     {
  134.       num_wasted++;
  135.       continue;
  136.     }
  137.     /* move robot to new location */
  138.     robot_array[newx][newy] = ROBOT;
  139.   }
  140.   return(num_wasted);
  141. }
  142.  
  143. /*----------------------------------------------------------------------*/
  144. void
  145. undo_chase()
  146. {
  147.   int x,y;
  148.   for_each
  149.     robot_array[x][y] = robot_array_bak[x][y];
  150. }
  151.  
  152. /*----------------------------------------------------------------------*/
  153. void
  154. teleport()
  155. {
  156.   int num_wasted;
  157.  
  158.   if(!game_active) return;
  159.   do
  160.   {
  161.     human_x = (int)random()%MAXX;        /* pick a spot not already occupied */
  162.     human_y = (int)random()%MAXY;
  163.   }
  164.   while(robot_array[human_x][human_y] != EMPTY);
  165.  
  166.   show_teleport();
  167.  
  168.   num_wasted = chase();
  169.  
  170.   if(robot_array[human_x][human_y] != EMPTY)
  171.   {
  172.     /* death... */
  173.     undo_chase();    
  174.     /* it is a matter of preference to clear the screen when you die... */
  175.     display_level();
  176.     do_death();
  177.     check_score(score);
  178.     game_active = 0;
  179.     return;
  180.   }
  181.  
  182.   display_level();
  183.  
  184.   score += num_robots;       /* bonus for teleporting */
  185.   score += num_wasted * 10;  /* score for any that collided */
  186.   num_robots -= num_wasted;
  187.   update_score(score);
  188.  
  189.   if(!num_robots) 
  190.     new_level();
  191.   else 
  192.     display_possible_moves();
  193.  
  194. }
  195.  
  196. /*----------------------------------------------------------------------*/
  197. void
  198. sonic_screwdriver()
  199. {
  200.   /* remove the neighboring robots by calling chase(), then clear out */
  201.   /* the human's position of robots */
  202.   int num_wasted;
  203.  
  204.   if(!game_active) return;
  205.   if(sonic_used) return;
  206.   sonic_used = 1;
  207.   show_sonic();
  208.   num_wasted = chase();
  209.   if(robot_array[human_x][human_y] == ROBOT)
  210.     add_score(1);
  211.   robot_array[human_x][human_y] = EMPTY;
  212.  
  213.   last_human_x = human_x;
  214.   last_human_y = human_y;
  215.   show_movement();
  216.  
  217.   add_score(num_wasted);
  218.   if(!num_robots) 
  219.     new_level();
  220.   else 
  221.     display_possible_moves();
  222. }
  223.  
  224. /*----------------------------------------------------------------------*/
  225. void
  226. wait_for_em()
  227. {
  228. /* call chase until any robot is breathing right down the human's neck  */
  229.   int num_wasted;
  230.  
  231.   if(!game_active) return;
  232.  
  233.   for(;;)
  234.   {
  235.     num_wasted = chase();
  236.     if(!num_robots)
  237.     {
  238.       add_score(num_wasted);
  239.       new_level();
  240.       break;
  241.     }
  242.     if(robot_array[human_x][human_y] != EMPTY)
  243.     {
  244.       /* backout of latest chase() and break loop */
  245.       undo_chase();
  246.       break;
  247.     }
  248.     add_score(num_wasted);
  249.     show_movement();
  250.   }
  251.   display_possible_moves();
  252. }
  253.  
  254. /*----------------------------------------------------------------------*/
  255.  
  256.  
  257. int
  258. can_go(x,y)
  259.   int x,y;
  260. {
  261. /* check if (x,y) is a legit move. */
  262.   if( INYRANGE(y-1) ) {
  263.     if( INXRANGE(x-1) )
  264.       if(robot_array[x-1][y-1] == ROBOT)  return 0;
  265.     if( INXRANGE(x) )
  266.       if(robot_array[x][y-1] == ROBOT)  return 0;
  267.     if( INXRANGE(x+1) )
  268.       if(robot_array[x+1][y-1] == ROBOT)  return 0;
  269.   }
  270.  
  271.   if( INYRANGE(y) ) {
  272.     if( INXRANGE(x-1) )
  273.       if(robot_array[x-1][y] == ROBOT)  return 0;
  274.     if( INXRANGE(x) ) {
  275.       if(robot_array[x][y] == ROBOT)  return 0;
  276.       if(robot_array[x][y] == HEAP)   return 0;
  277.       }
  278.     if( INXRANGE(x+1) )
  279.       if(robot_array[x+1][y] == ROBOT)  return 0;
  280.   }
  281.  
  282.   if( INYRANGE(y+1) ) {
  283.     if( INXRANGE(x-1) )
  284.       if(robot_array[x-1][y+1] == ROBOT)  return 0;
  285.     if( INXRANGE(x) )
  286.       if(robot_array[x][y+1] == ROBOT)  return 0;
  287.     if( INXRANGE(x+1) )
  288.       if(robot_array[x+1][y+1] == ROBOT)  return 0;
  289.   }
  290.  
  291.   if( !INXRANGE(x) )  return 0;
  292.   if( !INYRANGE(y) )  return 0;
  293.  
  294.   return 1;
  295. }
  296.  
  297. /*----------------------------------------------------------------------*/
  298.  
  299.