home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / programs / programming / stuff / Ants / c / ants
Encoding:
Text File  |  1996-08-22  |  5.0 KB  |  234 lines

  1. /* This is a program to show the behaviour of some bizarre ants */
  2.  
  3. /**************************************
  4. * Mostly by Gav but bits (minor) by   *
  5. * Kieran, notably the structures      *
  6. **************************************/
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12. #include <kernel.h>
  13. #include <swis.h>
  14. #include "antdata.h"
  15.  
  16.  
  17.  
  18. void  change_to_mode(int modenumber);
  19. void  switch_off_cursors(void);
  20. void  switch_on_mouse(void);
  21. void  put_point(int x,int y);
  22. int   read_point(int x,int y);
  23. void  put_circle(int x,int y,int size);
  24. void  change_gcol(int colour);
  25. int   random(int maxnumber);
  26. int   getmaxx(void);
  27. int   getmaxy(void);
  28.  
  29. void  setup_ants(void);
  30. void  do_stuff_with_ants(void);
  31. void  do_interference_ray(void);
  32.  
  33.  
  34.  
  35. int main(int argc,char *argv[])                      /*  This is our lovely main function */
  36. {
  37.   if (argc != 1)                                     /*  Check whether the user has set a number of ants... */
  38.       {sscanf(argv[1],"%d",&numants);}               /*  ...if so use it */
  39.   else
  40.       {numants = DEFAULT_NUMBER_OF_ANTS;}            /*  ...if not, set it to the default value */
  41.  
  42.   printf("Number of ants = %d\n",numants);
  43.  
  44.   change_to_mode(screen_mode);                       /*   Change screen mode */
  45.   switch_off_cursors();
  46.   switch_on_mouse();
  47.  
  48.   setup_ants();
  49.  
  50.   do
  51.   {
  52.    do_stuff_with_ants();
  53.    do_interference_ray();
  54.   } while (1);
  55. }
  56.  
  57.  
  58.  
  59. /* ----------------------------------------------Program  functions----------------------------------------------*/
  60.  
  61. void setup_ants(void)
  62. {
  63.   int c;
  64.  
  65.   srand(time(NULL));                              /* Seed the random number generator */
  66.  
  67.   for ( c=0 ; c<numants ; c++)
  68.   {
  69.     antx[c] = random(getmaxx());
  70.     anty[c] = random(getmaxy());
  71.     antd[c] = (enum directiontype)random(4);
  72.     put_point(antx[c],anty[c]);
  73.   }
  74. }
  75.  
  76.  
  77. void do_stuff_with_ants(void)
  78. {
  79.   register int c;
  80.   for ( c=0 ; c<numants ; c++)
  81.   {
  82.     switch (antd[c]) {                  /* Find which way the ant is pointing and move accordingly */
  83.       case up:
  84.               anty[c] +=1;
  85.               if (anty[c] == getmaxy()) anty[c]=0;
  86.               break;
  87.       case down:
  88.               anty[c] -=1;
  89.               if (anty[c] == -1) anty[c]=getmaxy()-1;
  90.               break;
  91.       case right:
  92.               antx[c] +=1;
  93.               if (antx[c] == getmaxx()) antx[c]=0;
  94.               break;
  95.       case left:
  96.               antx[c] -=1;
  97.               if (antx[c] == -1) antx[c]=getmaxx()-1;
  98.               break;
  99.     }
  100.  
  101.     switch (read_point(antx[c],anty[c])) {     /*  See what colour the pixel is and turn the ant acordingly */
  102.       case WHITE:
  103.               antd[c]=(enum directiontype)((int)antd[c]+1);
  104.               if (antd[c] == 4) antd[c] = up;
  105.               put_point(antx[c],anty[c]);
  106.               break;
  107.       default:
  108.               antd[c]=(enum directiontype)((int)antd[c]-1);
  109.               if (antd[c] == -1) antd[c] = left;
  110.               put_point(antx[c],anty[c]);
  111.               break;
  112.     }
  113.   }
  114. }
  115.  
  116. void  do_interference_ray(void)
  117. {
  118.   int x,y,b;
  119.   _kernel_swi(OS_Mouse,&in,&out);
  120.   x = out.r[0];
  121.   y = out.r[1];
  122.   b = out.r[2];
  123.   switch (b) {
  124.     case 4 :
  125.             put_circle( x/x_factor , y/y_factor , CIRCLESIZE);
  126.             break;
  127.     case 1 :
  128.             change_gcol(BLACK);
  129.             put_circle( x/x_factor , y/y_factor , CIRCLESIZE);
  130.             change_gcol(WHITE);
  131.             break;
  132.   }
  133. }
  134.  
  135.  
  136. /* -----------------------------------------------Screen functions-----------------------------------------------*/
  137.  
  138.  
  139.  
  140. void change_to_mode(int modenumber)       /*  Changes screen mode  */
  141. {
  142.  
  143.   _kernel_swi(CHANGE_SCREEN_MODE,&in,&out);
  144.  
  145.   in.r[0] = modenumber;
  146.   _kernel_swi(OS_WriteC,&in,&out);
  147. }
  148.  
  149. void switch_off_cursors(void)
  150. {
  151.   _kernel_swi(279,&in,&out);
  152.   _kernel_swi(257,&in,&out);
  153.   _kernel_swi(256,&in,&out);
  154.   _kernel_swi(256,&in,&out);
  155.   _kernel_swi(256,&in,&out);
  156.   _kernel_swi(256,&in,&out);
  157.   _kernel_swi(256,&in,&out);
  158.   _kernel_swi(256,&in,&out);
  159.   _kernel_swi(256,&in,&out);
  160.   _kernel_swi(256,&in,&out);
  161. }
  162.  
  163. void switch_on_mouse(void)
  164. {
  165.   in.r[0]=106;
  166.   in.r[1]=1;
  167.   _kernel_swi(OS_Byte,&in,&out);
  168. }
  169.  
  170. int getmaxx(void)
  171. {return screen_width;}
  172.  
  173. int getmaxy(void)
  174. {return screen_height;}
  175.  
  176.  
  177.  
  178.  
  179. int  read_point(int x,int y)
  180. {
  181.  
  182.   in.r[0] = x * x_factor;
  183.   in.r[1] = y * y_factor;
  184.   _kernel_swi(OS_ReadPoint,&in,&out);
  185.   return out.r[2];
  186. }
  187.  
  188.  
  189. void  put_point(int x,int y)
  190. {
  191.   in.r[0] = 70;
  192.   in.r[1] = (x*x_factor);                                /* Put a dot at the pixel coordinates */
  193.   in.r[2] = (y*y_factor);
  194.   _kernel_swi(OS_Plot,&in,&out);
  195. }
  196.  
  197. void  put_circle(int x,int y,int size)
  198. {
  199.   in.r[0] = 4;
  200.   in.r[1] = (x*x_factor);
  201.   in.r[2] = (y*y_factor);
  202.   _kernel_swi(OS_Plot,&in,&out);
  203.  
  204.   in.r[0] = 153;
  205.   in.r[1] = size * x_factor;
  206.   in.r[2] = 0;
  207.   _kernel_swi(OS_Plot,&in,&out);
  208.  
  209. }
  210.  
  211. void change_gcol(int colour)
  212. {
  213.   _kernel_swi(CHANGE_COLOUR,&in,&out);
  214.   _kernel_swi(256,&in,&out);
  215.   in.r[0] = colour;
  216.   _kernel_swi(OS_WriteC,&in,&out);
  217. }
  218.  
  219.  
  220. /*   Other functions  */
  221.  
  222. int   random(int maxnumber)
  223. {
  224.  int random_num;
  225.  float temp;
  226.  
  227.  random_num = rand();
  228.  temp = (float)random_num*(float)maxnumber;
  229.  temp /= RAND_MAX;
  230.  random_num = (int)temp;
  231.  
  232.  return random_num;
  233. }
  234.