home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / p / pcrob13.zip / EXAMPLES / DEMO.C next >
C/C++ Source or Header  |  1992-10-29  |  6KB  |  278 lines

  1. /*     DEMO.C - a demonstration PCRobots Robot in Turbo C        */
  2. /*        Written by P.D.Smith - July 1992            */
  3.  
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. /* Include the PCROBOTS header file for the special function definitions*/
  8. #include "pcrobots.h"
  9.  
  10.  
  11. /* Function prototypes */
  12. void    read_local_map(int x,int y);
  13.  
  14. /* Type definitions */
  15. typedef unsigned char byte;
  16.  
  17. /* Define the robot's icon on the screen - this is a small smiley shape    */
  18. const char pattern[5]={27,4,4,17,14};
  19.  
  20. /*
  21.  * Define possible angles of motion
  22.  * x,y are x,y offsets on the map
  23.  * angle is the angle of motion to specify
  24.  */
  25. struct     {
  26.      int    x,
  27.         y,
  28.         angle;
  29.     }
  30.     angle[8]={{ 1, 0,0},
  31.           { 1, 1,45},
  32.           { 0, 1,90},
  33.           {-1, 1,135},
  34.           {-1, 0,180},
  35.           {-1,-1,225},
  36.           { 0,-1,270},
  37.           { 1,-1,315}};
  38.  
  39. /* Array to hold the map */
  40. byte    map[100][100];
  41.  
  42. /* Current X and Y coordinates of the robot */
  43. int    x_pos,
  44.     y_pos;
  45.  
  46. /* Structure to hold information about the refueling points that
  47.  * have been located.
  48.  * x,y are coordinates (in map units)
  49.  * last_visit is the time of the last visit (in game ticks)
  50.  *     to avoid revisiting refuelers too often
  51.  */
  52. struct  {
  53.      int     x,
  54.         y;
  55.      long    last_visit;
  56.     } refueler[10];
  57. int    num_refuelers=0;    /* number of refuelers that have
  58.                    been found */
  59.  
  60. int    go_dir;            /* current direction (index to angle[]) */
  61. int    last_battery;        /* last battery reading    */
  62. int    last_armour;        /* last armour status */
  63.  
  64. int    scan_angle=0;        /* Current scanning angle */
  65. int    range;            /* values returned */
  66. int    id;            /* from the scan routine */
  67.  
  68. void    main(void)
  69. {
  70.  int    i,
  71.     j,
  72.     try;
  73.  int    x_square,
  74.     y_square,
  75.     next_x,
  76.     next_y;
  77.  int    new_armour,
  78.     new_battery;
  79.  int    invis_count=0;
  80.  int    speed=0;
  81.  long    now,
  82.     last_shoot=-50;
  83.  int    shot_counter=0;        /* this is used to detect shells hitting walls */
  84.  
  85. /* Configure robot - all normal, except with invisibility facility
  86.  * This *MUST* be the first special function called by the robot   */
  87.  configure(2,2,2,2,2,1);
  88.  
  89.  randomize();
  90.  
  91.  go_dir=random(8);        /* Select initial direction */
  92.  
  93. /* Set the robot's icon */
  94.  set_pattern(pattern);
  95.  
  96. /* Clear the map to '255' so we know where we haven't been yet */
  97.  for (i=0;i<100;i++)
  98.  {
  99.   for (j=0;j<100;j++)
  100.    map[i][j]=255;
  101.  }
  102.  
  103.  last_armour=damage();      /* get initial armour and battery levels */
  104.  last_battery=battery();
  105.  
  106. /* Note - this is an *INFINITE* loop - the PCROBOTS executive program
  107.    kills the robot off */
  108.  while(1)
  109.  {
  110. /* Get the current position */
  111.   getxy(&x_pos,&y_pos);
  112.  
  113.   now=ticks();
  114.  
  115. /* Get the current map square */
  116.   x_square = x_pos/10;
  117.   y_square = y_pos/10;
  118. /* Get the next square to be moved into */
  119.   next_x   = x_square+angle[go_dir].x;
  120.   next_y   = y_square+angle[go_dir].y;
  121.  
  122. /* If the current map square, or the next map_square to be moved into,
  123.  * are unknown, read the local map */
  124.   if ((map[y_square][x_square] == 255) ||
  125.     (map[next_y][next_x]   == 255))
  126.   {
  127.    read_local_map (x_square,y_square);
  128.   }
  129.  
  130. /* Ok, now we know where we are, look to see if the target square contains
  131.  * a wall or trap - if so, choose a new direction which is clear */
  132.  
  133. /* Quick and easy way of dealing with the robot being near an outside wall */
  134.   if (next_x<2)
  135.    go_dir=(random(3)-1)%8;
  136.   if (next_x>=98)
  137.    go_dir=random(3)+3;
  138.   if (next_y<2)
  139.    go_dir=random(3)+1;
  140.   if (next_y>=98)
  141.    go_dir=random(3)+5;
  142.  
  143.   next_x   = x_square+angle[go_dir].x;
  144.   next_y   = y_square+angle[go_dir].y;
  145.  
  146.   try=0;
  147.   while ((map[next_y][next_x] == ARENA_WALL) ||
  148.      (map[next_y][next_x] == ARENA_DAMAGE) ||
  149.      ((next_x<0)||(next_x>=100)||(next_y<0)||(next_y>=100)))
  150.   {
  151.    debug_flag(0,1);
  152.    srand((unsigned)now);
  153.    go_dir=random(8);
  154.  
  155.    next_x   = x_square+angle[go_dir].x;
  156.    next_y   = y_square+angle[go_dir].y;
  157.  
  158. /* Failsafe check to avoid the program getting trapped here */
  159.    if (try++ > 50)
  160.    {
  161.     debug_flag(1,1);
  162.     break;
  163.    }
  164.   }
  165.   debug_flag(0,0);
  166.  
  167.  
  168.   movement(speed,angle[go_dir].angle);
  169.  
  170. /* Come out of invisibility if necessary */
  171.   if (invis_count>0)
  172.    invis_count--;
  173.   else
  174.    invisibility(0);
  175.  
  176. /* See if we've been hit! */
  177.   new_armour=damage();
  178.   if (new_armour<last_armour)
  179.   {
  180.    invisibility(1);
  181.    go_dir=random(8);
  182.    invis_count=30;
  183.   }
  184.   last_armour=new_armour;
  185.  
  186.   new_battery=battery();
  187.  
  188. /* See if badly damaged- may be able to buy more armour */
  189.   if ((last_armour<50)&&(new_battery>400))
  190.   {
  191.    buy_armour(2);
  192.   }
  193.  
  194. /* If battery is depleted rest for a while */
  195.   if ((new_battery<(600-(speed*8)))&&(invis_count==0))
  196.    speed=0;
  197.   else
  198.    speed=50;
  199.  
  200.  
  201. /* there's no point wasting time if my cannon hasn't reloaded yet */
  202.   if ((now-last_shoot)>=50)
  203.   {
  204.    id=scan(scan_angle,10,&range);
  205.  
  206. // Check if no target, or target out of range - go to next angle if so */
  207.    if ((id<0)||(range>700))
  208.    {
  209.     scan_angle=(scan_angle+10)%360;
  210.     shot_counter=0;
  211.    }
  212.    else
  213.    {
  214.     shot_counter++;
  215.     if (shot_counter==4)
  216.     {
  217.      if (get_shell_status()==SHELL_HIT_WALL)
  218.      {
  219.       scan_angle=(scan_angle+10)%360;
  220.       id=-1;
  221.      }
  222.      shot_counter=0;
  223.     }
  224.     if (id>=0)
  225.     {
  226.      shoot(scan_angle,range);
  227.      last_shoot=now;
  228.     }
  229.    }
  230.   }
  231.  
  232.  }
  233. }
  234.  
  235.  
  236. /* Copy the local map into the main map
  237.  * x,y are the current 'map' coordinates */
  238. void    read_local_map(int x,int y)
  239. {
  240.  int     i,
  241.     j,
  242.     val;
  243.  byte    *ptr;
  244.  
  245. /* Buffer to hold the local map before copying it to the main map    */
  246.  byte    local_map[81];
  247.  
  248.  
  249. /* Obtain the local map */
  250.  get_local_map(local_map);
  251.  
  252. /* Copy the local map into the main map */
  253.  ptr=local_map;
  254.  
  255. /* Get coordinates of top left of local map area */
  256.  x-=4;
  257.  y-=4;
  258.  
  259.  for (i=0;i<9;i++)
  260.  {
  261.   for (j=0;j<9;j++)
  262.   {
  263. /* only copy if within the actual arena */
  264.    if ((x<0)||(x>=100)||(y<0)||(y>=100))
  265.     ptr++;
  266.    else
  267.    {
  268.     val=*(ptr++);
  269.     map[y][x]=val;    /* store the map contents */
  270.  
  271.    }
  272.    x++;            /* next column */
  273.   }
  274.   y++;            /* next row */
  275.   x-=9;
  276.  }
  277. }
  278.