home *** CD-ROM | disk | FTP | other *** search
- /* This is a program to show the behaviour of some bizarre ants */
-
- /**************************************
- * Mostly by Gav but bits (minor) by *
- * Kieran, notably the structures *
- **************************************/
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <kernel.h>
- #include <swis.h>
- #include "antdata.h"
-
-
-
- void change_to_mode(int modenumber);
- void switch_off_cursors(void);
- void switch_on_mouse(void);
- void put_point(int x,int y);
- int read_point(int x,int y);
- void put_circle(int x,int y,int size);
- void change_gcol(int colour);
- int random(int maxnumber);
- int getmaxx(void);
- int getmaxy(void);
-
- void setup_ants(void);
- void do_stuff_with_ants(void);
- void do_interference_ray(void);
-
-
-
- int main(int argc,char *argv[]) /* This is our lovely main function */
- {
- if (argc != 1) /* Check whether the user has set a number of ants... */
- {sscanf(argv[1],"%d",&numants);} /* ...if so use it */
- else
- {numants = DEFAULT_NUMBER_OF_ANTS;} /* ...if not, set it to the default value */
-
- printf("Number of ants = %d\n",numants);
-
- change_to_mode(screen_mode); /* Change screen mode */
- switch_off_cursors();
- switch_on_mouse();
-
- setup_ants();
-
- do
- {
- do_stuff_with_ants();
- do_interference_ray();
- } while (1);
- }
-
-
-
- /* ----------------------------------------------Program functions----------------------------------------------*/
-
- void setup_ants(void)
- {
- int c;
-
- srand(time(NULL)); /* Seed the random number generator */
-
- for ( c=0 ; c<numants ; c++)
- {
- antx[c] = random(getmaxx());
- anty[c] = random(getmaxy());
- antd[c] = (enum directiontype)random(4);
- put_point(antx[c],anty[c]);
- }
- }
-
-
- void do_stuff_with_ants(void)
- {
- register int c;
- for ( c=0 ; c<numants ; c++)
- {
- switch (antd[c]) { /* Find which way the ant is pointing and move accordingly */
- case up:
- anty[c] +=1;
- if (anty[c] == getmaxy()) anty[c]=0;
- break;
- case down:
- anty[c] -=1;
- if (anty[c] == -1) anty[c]=getmaxy()-1;
- break;
- case right:
- antx[c] +=1;
- if (antx[c] == getmaxx()) antx[c]=0;
- break;
- case left:
- antx[c] -=1;
- if (antx[c] == -1) antx[c]=getmaxx()-1;
- break;
- }
-
- switch (read_point(antx[c],anty[c])) { /* See what colour the pixel is and turn the ant acordingly */
- case WHITE:
- antd[c]=(enum directiontype)((int)antd[c]+1);
- if (antd[c] == 4) antd[c] = up;
- put_point(antx[c],anty[c]);
- break;
- default:
- antd[c]=(enum directiontype)((int)antd[c]-1);
- if (antd[c] == -1) antd[c] = left;
- put_point(antx[c],anty[c]);
- break;
- }
- }
- }
-
- void do_interference_ray(void)
- {
- int x,y,b;
- _kernel_swi(OS_Mouse,&in,&out);
- x = out.r[0];
- y = out.r[1];
- b = out.r[2];
- switch (b) {
- case 4 :
- put_circle( x/x_factor , y/y_factor , CIRCLESIZE);
- break;
- case 1 :
- change_gcol(BLACK);
- put_circle( x/x_factor , y/y_factor , CIRCLESIZE);
- change_gcol(WHITE);
- break;
- }
- }
-
-
- /* -----------------------------------------------Screen functions-----------------------------------------------*/
-
-
-
- void change_to_mode(int modenumber) /* Changes screen mode */
- {
-
- _kernel_swi(CHANGE_SCREEN_MODE,&in,&out);
-
- in.r[0] = modenumber;
- _kernel_swi(OS_WriteC,&in,&out);
- }
-
- void switch_off_cursors(void)
- {
- _kernel_swi(279,&in,&out);
- _kernel_swi(257,&in,&out);
- _kernel_swi(256,&in,&out);
- _kernel_swi(256,&in,&out);
- _kernel_swi(256,&in,&out);
- _kernel_swi(256,&in,&out);
- _kernel_swi(256,&in,&out);
- _kernel_swi(256,&in,&out);
- _kernel_swi(256,&in,&out);
- _kernel_swi(256,&in,&out);
- }
-
- void switch_on_mouse(void)
- {
- in.r[0]=106;
- in.r[1]=1;
- _kernel_swi(OS_Byte,&in,&out);
- }
-
- int getmaxx(void)
- {return screen_width;}
-
- int getmaxy(void)
- {return screen_height;}
-
-
-
-
- int read_point(int x,int y)
- {
-
- in.r[0] = x * x_factor;
- in.r[1] = y * y_factor;
- _kernel_swi(OS_ReadPoint,&in,&out);
- return out.r[2];
- }
-
-
- void put_point(int x,int y)
- {
- in.r[0] = 70;
- in.r[1] = (x*x_factor); /* Put a dot at the pixel coordinates */
- in.r[2] = (y*y_factor);
- _kernel_swi(OS_Plot,&in,&out);
- }
-
- void put_circle(int x,int y,int size)
- {
- in.r[0] = 4;
- in.r[1] = (x*x_factor);
- in.r[2] = (y*y_factor);
- _kernel_swi(OS_Plot,&in,&out);
-
- in.r[0] = 153;
- in.r[1] = size * x_factor;
- in.r[2] = 0;
- _kernel_swi(OS_Plot,&in,&out);
-
- }
-
- void change_gcol(int colour)
- {
- _kernel_swi(CHANGE_COLOUR,&in,&out);
- _kernel_swi(256,&in,&out);
- in.r[0] = colour;
- _kernel_swi(OS_WriteC,&in,&out);
- }
-
-
- /* Other functions */
-
- int random(int maxnumber)
- {
- int random_num;
- float temp;
-
- random_num = rand();
- temp = (float)random_num*(float)maxnumber;
- temp /= RAND_MAX;
- random_num = (int)temp;
-
- return random_num;
- }
-