/* Multi-Ant by Nava Whiteford   */
/* For Forbnicate issue 4        */
/* Now you can have lots of ants */

#include <stdio.h>
#include "roslib.h"
#define s 10
#define halfs 5
#define scrx 160
#define scry 120
#define maxant 1024
void input();
void moveant(int n);

int colour, gen, end, done, key, numant, pos;
struct ant {int x; int y; int head; int dead;};
struct ant ant[maxant];


void main()
{

  os_mode(29);
  input();
  os_mode(29);

  for(;end==0;)
  {
    for(pos=0;pos<numant;pos++) { if(ant[pos].dead==0) moveant(pos); }
    key=os_inkey(0);
    if(key==113) end=1;
  }
}


void input()
{
  int antx, anty, head, num;
  printf("When finished press q small q to quit \n");

  printf("enter total number of ants? ");
  scanf("%d", &numant);

  for(num=0;num<numant;num++)
  {
    printf("enter ant %d x coordinates? ", num);
    scanf("%d", &antx);
    printf("enter ant %d y coordinates? ", num);
    scanf("%d", &anty);
    printf("enter ant %d initial heading? ", num);
    scanf("%d", &head);
    if(head>3) head=3;
    if(head<0) head=0;

    ant[num].x=antx;
    ant[num].y=anty;
    ant[num].head=head;
  }
}


void moveant(int n)
{
  colour=os_point(ant[n].x*s+halfs,ant[n].y*s+halfs);

  if(ant[n].x>scrx) ant[n].dead=1;
  if(ant[n].y>scry) ant[n].dead=1;
  if(ant[n].x< 0  ) ant[n].dead=1;
  if(ant[n].y< 0  ) ant[n].dead=1;

  if(colour==0){os_gcol(0,1);ant[n].head++;if(ant[n].head== 4)ant[n].head=0;}
  if(colour==1){os_gcol(0,0);ant[n].head--;if(ant[n].head==-1)ant[n].head=3;}
  os_rectanglefill(ant[n].x*s, ant[n].y*s, s, s);

  if(ant[n].head==0) ant[n].y++;
  if(ant[n].head==1) ant[n].x++;
  if(ant[n].head==2) ant[n].y--;
  if(ant[n].head==3) ant[n].x--;

  gen++; /* moves on generation number */

}

