Home » Recent acquisitions » Acorn ADFS disks » adfs_ArchimedesWorld_15_03.adf » !AcornAns_AcornAns » Flocking/C/c/Old/PureC

Flocking/C/c/Old/PureC

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Recent acquisitions » Acorn ADFS disks » adfs_ArchimedesWorld_15_03.adf » !AcornAns_AcornAns
Filename: Flocking/C/c/Old/PureC
Read OK:
File size: 2909 bytes
Load address: 0000
Exec address: 0000
File contents
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <stdarg.h>

#include "werr.h"
#include "os.h"
#include "kernel.h"
#include "akbd.h"
#include "bbc.h"
#include "pointer.h"
#include "visdelay.h"

#include "swis.h"

#define lone 16
#define one (1<<lone)
#define abs(a) ((a)<0 ? -(a) : (a))
typedef enum {multi, nonmulti} monitor;

#define werrterm 0
				/*principal quantities unbracketed, derived quantities bracketed:*/
typedef struct {
  int x, y, r, r2;		/*position, radius (& radius squared)*/
} pillar_data;

typedef struct {
  int x, y, xn, yn;		/*position (& new position)*/
  int vx, vy, mv;		/*velocity (& speed)*/
  int dm, im;			/*distance squared to nearest boid & index of that boid*/
  int ax, ay, avx, avy, ac;	/*accumulated relative-positions/velocities of boids in locale & number of such*/
  int dummy15, dummy16;		/*pad size to a power of 2*/
} boid_data;

extern int rbbcinc(int r, int k);
extern int div_frac16(int number, int divisor);
extern int mul_frac16(int x, int a);
extern int mul_frac16c(int x, int a);
extern int sqrt_frac16(unsigned int x);
extern int gauss16(void);
extern void sgauss16(int seed);
extern int rand16(void);
extern int randu16(void);
extern void srand16(int seed);
extern int cos16(int a);
extern int sin16(int a);
extern int exp16(int a);
extern int ln16(int a);
extern int pow16(int a, int b);
extern int acs16(int a);
extern int asn16(int a);
extern int sig16(int a);

/*multiplies two 16 bit fixed point numbers, returning a 10 bit fixed point number, hence can represent
  numbers upto 1448*1448 eg result of mul1610(1448*one,1448*one);
  useful for arithmetic with squares of positions or velocities*/
#define mul1610(a,b) (mul_frac16((a)/8,(b)/8))
/*returns 16 bfp square root of 10 bfp argument*/
#define sqrt1016(a) (8*sqrt_frac16((a)))

BOOL vga;
int mode;
int bank;
BOOL single_step;
int tn2, ttotal;

int n, g;
int rc, rm, rl, rc2, rl2;
int pn, mc;
pillar_data *pillar=0;
boid_data *boid=0;

monitor read_monitor_type(void)
{
  int r2;
  os_swi3r(6, 161,133,0, 0,0,&r2);
  if ((r2/4 & 3) == 1) return multi;
  else return nonmulti;
}

void tidy(void)
{
  os_swi2(OS_Byte, 112, 1);
  os_swi2(OS_Byte, 113, 1);
}

void swap_banks(void)
{
  os_swi2(OS_Byte, 113, bank);
  os_swi2(OS_Byte, 112, bank=3-bank);
}

void prepare_screen(void)
{
  bank=1;
  os_swi2(OS_Byte, 112, bank);
  os_swi2(OS_Byte, 113, 3-bank);
}

void press(int i)
{
  for (;bbc_inkey(i)!=-1;);
  return;
}

void release(int i)
{
  for (;bbc_inkey(i)!=0;);
  return;
}

BOOL kbhit(void)
{
  return bbc_inkey(-99)==-1;
}

void step(void)
{
  if (bbc_inkey(-82)) single_step=TRUE;
  if (single_step) for (; single_step;) {
    if (bbc_inkey(-82)) {
      release(-82);
      break;
    }
    if (bbc_inkey(-83)) single_step=FALSE;
  }
}

void seed_rand16(void)
{
  int v=clock() & 0xff;
  v|=v<<8;
  srand16(v | v<<16);
}

/*returns 16bfp magnitude of vector with 16bfp components x & y*/
int magnitude(int x, int y)
{
  int f = sqrt1016(mul1610(x,x)+mul1610(y,y));
  if (f<abs(x)) f=abs(x);	/*compensate for some nasty underflow problems*/
  if (f<abs(y)) f=abs(y);	/*with above fixed point arithmetic*/
  return f<one/256 ? one/256 : f;
}

int nearests_sqrt(int i)
{
  int f = sqrt1016(boid[i].dm);
  if (f<abs(boid[boid[i].im].x-boid[i].x)) f=abs(boid[boid[i].im].x-boid[i].x);
  if (f<abs(boid[boid[i].im].y-boid[i].y)) f=abs(boid[boid[i].im].y-boid[i].y);
  return f<one/256 ? one/256 : f;
}

/*'bounce' velocity via approach vector, nb x,y,vx,vy 16bfp, while m=x*x+y*y 10bfp*/
void bounce(int x, int y, int *vx, int *vy, int m)
{
  int  s = mul1610(x, *vx) + mul1610(y, *vy);
  if (s>0) {
       s = 2*div_frac16(s, m==0 ? 1 : m);
    *vx -= mul_frac16(x, s);
    *vy -= mul_frac16(y, s);
  }
}

BOOL init(void)
{
  int i;
  atexit(tidy);
  vga = read_monitor_type()==multi ? TRUE : FALSE;
  mode = vga ? 18 : 0;
  tidy();
  bbc_mode(mode+128);
  bbc_mode(mode);
  bbc_cursor(0);
  os_cli("pointer");
  bbc_origin(640,512);
  pointer_reset_shape();
  release(-99);
  os_swi2(OS_Byte, 15, 1);
  do {
    printf("Please enter number of boids (min 2, suggest 24+) ");
    scanf("%i", &n);
  } while (n<2);
  do {
    printf("Please enter number of pillars (1 to 100, suggest 4) ");
    scanf("%i", &pn);
  } while (pn<1 || pn>100);
  do {
    printf("Please enter boid size (1 to 8, suggest 4) ");
    scanf("%i", &g);
  } while (g<1 || g>8);
  do {
    printf("Please enter seed for random initialisation (any non-zero integer,\nor 0 for a 'random' seed; try 101) ");
    scanf("%i", &i);
  } while (FALSE);

  if (i) srand16(~(i^(i<<16)));
  else seed_rand16();
  rc=5*g*one;		/*radius for collision detection*/
  rm=8*g*one;		/*radius for velocity matching*/
  rl=40*g*one;		/*radius of locale*/
  rc2=mul1610(rc,rc);
  rl2=mul1610(rl,rl);
  if (pillar) {
    free(pillar);
    pillar=0;
  }
  if (boid) {
    free(boid);
    boid=0;
  }
  if (pillar = calloc(pn, sizeof(pillar_data)), pillar==0) {
    werr(werrterm, "Insufficient memory");
    return FALSE;
  }
  if (boid   = calloc( n, sizeof(  boid_data)),   boid==0) {
    werr(werrterm, "Insufficient memory");
    return FALSE;
  }
  mc=0;
  for (i=0; i<pn; i++) {
    pillar[i].x  = rand16()*640;
    pillar[i].y  = rand16()*512;
    pillar[i].r  = randu16()*40;
    pillar[i].r2 = mul1610(pillar[i].r+rc/2, pillar[i].r+rc/2);
  }
  for (i=0; i<n; i++) {
    boid[i].x  = 64*g*rand16();
    boid[i].y  = 64*g*rand16();
    boid[i].vx =  4*g*randu16();
    boid[i].vy =  2*g*rand16();
    boid[i].mv = magnitude(boid[i].vx,boid[i].vy);
  }
  prepare_screen();
  return TRUE;
}

void check_mouse(void)
{
  int x,y,z, i,d;
  os_swi3r(OS_Mouse, 0,0,0, &x,&y,&z);
  x*=one;
  y*=one;
  switch(z) {
    case 1:
      if (pillar[mc].r>one) {
        pillar[mc].r-=one;
        pillar[mc].r2=mul1610(pillar[mc].r+rc/2, pillar[mc].r+rc/2);
      }
      break;
    case 2:
      if (pillar[mc].r<one*200) {
        pillar[mc].r+=one;
        pillar[mc].r2=mul1610(pillar[mc].r+rc/2, pillar[mc].r+rc/2);
      }
      break;
    case 4:
      for (i=mc; i<pn; i++) {
        if (abs(pillar[i].x-x)<pillar[i].r && abs(pillar[i].y-y)<pillar[i].r) {
          d = mul1610(pillar[i].x-x,pillar[i].x-x) + mul1610(pillar[i].y-y,pillar[i].y-y);
          if (d < pillar[i].r2) {
            i+=0x10000;
            break;
          }
        }
        if (i==mc)   i=-1;
        if (i==mc-1) i=mc;
      }
      if (i&0x10000 && i!=0x10000+mc) {
        mc = i-0x10000;
        bbc_vdu(7);
      }
      else {
        pillar[mc].x = x;
        pillar[mc].y = y;
      }
      break;
  }
}

void move_boids(void)
{
  int i, j, d;
  int x, y, vx, vy;
  int a, b;
  int rx, ry;
  boid_data *bp, *bpe;
  for (bp=boid,bpe=bp+n; bp<bpe; bp++) {
    bp->dm=0x7fffffff;
    bp->ax=bp->ay=bp->avx=bp->avy=bp->ac=0;
  }
  tn2=clock();
  for (i=0; i<n-1; i++)
    for (j=i+1; j<n; j++) {
      a = (rx=boid[i].x-boid[j].x)/one;
      a = a*a;
      if (a<boid[i].dm/1024 || a<boid[j].dm/1024 || a<rl2/1024) {
        b = (ry=boid[i].y-boid[j].y)/one;
        a += b*b;
        if (a<boid[i].dm/1024 || a<boid[j].dm/1024 || a<rl2/1024) {
          d = mul1610(rx,rx) + mul1610(ry,ry);
          if (d<boid[i].dm) {
            boid[i].dm = d;
            boid[i].im = j;
          }
          if (d<boid[j].dm) {
            boid[j].dm = d;
            boid[j].im = i;
          }
          if (d<rl2) {
            boid[i].ax  += boid[j].x-boid[i].x;
            boid[i].ay  += boid[j].y-boid[i].y;
            boid[i].avx += boid[j].vx;
            boid[i].avy += boid[j].vy;
            boid[i].ac  += 1;
            boid[j].ax  += boid[i].x-boid[j].x;
            boid[j].ay  += boid[i].y-boid[j].y;
            boid[j].avx += boid[i].vx;
            boid[j].avy += boid[i].vy;
            boid[j].ac  += 1;
          }
        }
      }
    }
  tn2=clock()-tn2;
  for (i=0; i<n; i++) {
    x  = boid[i].x;
    y  = boid[i].y;
    vx = boid[i].vx;
    vy = boid[i].vy;
    if (boid[i].ac<1) {
      j = boid[i].im;
      boid[i].ax  += boid[j].x-x;
      boid[i].ay  += boid[j].y-y;
      boid[i].avx += boid[j].vx;
      boid[i].avy += boid[j].vy;
      boid[i].ac  += 1;
    }
    if (x<rc-640*one || x>640*one-rc || y<rc-512*one || y>512*one-rc) {
      if (x<rc-640*one&&vx<0 || x>640*one-rc&&vx>0) vx=-vx;
      if (y<rc-512*one&&vy<0 || y>512*one-rc&&vy>0) vy=-vy;
    }
    else {
      for (j=0; j<pn; j++) {
        if (abs(pillar[j].x-x)<pillar[j].r && abs(pillar[j].y-y)<pillar[j].r) {
          d = mul1610(pillar[j].x-x,pillar[j].x-x) + mul1610(pillar[j].y-y,pillar[j].y-y);
          if (d < pillar[j].r2) {
            j+=0x10000;
            break;
          }
        }
      }
      if (j & 0x10000) {
        j-=0x10000;
        bounce(pillar[j].x-x, pillar[j].y-y, &vx, &vy, d);
      }
      else if (boid[i].dm<rc2) bounce(boid[boid[i].im].x-x, boid[boid[i].im].y-y, &vx, &vy, boid[i].dm);
      else {
        a = boid[i].ax/boid[i].ac;
        b = boid[i].ay/boid[i].ac;
        d = magnitude(a,b);
        a = div_frac16(a,d);
        b = div_frac16(b,d);
        d = nearests_sqrt(i);
        if (d<rm) {
          vx = div_frac16( mul_frac16(boid[i].avx/boid[i].ac, rm-d)+mul_frac16(d-rc, a*g) , rm-rc );
          vy = div_frac16( mul_frac16(boid[i].avy/boid[i].ac, rm-d)+mul_frac16(d-rc, b*g) , rm-rc );
        }
        else {
          d  = div_frac16(d, rm);
          if (d>16*one) d=16*one;	/*prevent ridiculously high catch up speeds*/
          vx = mul_frac16(d, a*g);
          vy = mul_frac16(d, b*g);
        }
      }
    }
    d = magnitude(vx,vy);
    if (d<g*one) {
      vx = div_frac16(vx*g,d);
      vy = div_frac16(vy*g,d);
      d  = g*one;
    }
    boid[i].xn = boid[i].x+vx;
    boid[i].yn = boid[i].y+vy;
    boid[i].vx = vx;
    boid[i].vy = vy;
    boid[i].mv = d;
  }
  for (i=0; i<n; i++) {
    boid[i].x = boid[i].xn;
    boid[i].y = boid[i].yn;
  }
}

int main(void)
{
  int i;
  if (!init()) return 0;
  for (single_step=FALSE;;) {
    ttotal=clock();
    bbc_cls();
    move_boids();
    check_mouse();
    for (i=0; i<n; i++) {
      bbc_move(boid[i].x/one, boid[i].y/one);
      bbc_drawby((boid[i].vx*2*g)/boid[i].mv, (boid[i].vy*2*g)/boid[i].mv);
    }
    for (i=0; i<pn; i++) bbc_circle(pillar[i].x/one, pillar[i].y/one, pillar[i].r/one);
    ttotal=clock()-ttotal;
    printf("%i\n%i\n",tn2,ttotal);
    os_swi1(OS_Byte, 19);
    swap_banks();
    step();
    if (kbhit())
      if (!init()) break;
  }
  return 0;
}
00000000  0a 23 69 6e 63 6c 75 64  65 20 3c 73 74 64 69 6f  |.#include <stdio|
00000010  2e 68 3e 0a 23 69 6e 63  6c 75 64 65 20 3c 73 74  |.h>.#include <st|
00000020  64 6c 69 62 2e 68 3e 0a  23 69 6e 63 6c 75 64 65  |dlib.h>.#include|
00000030  20 3c 73 74 72 69 6e 67  2e 68 3e 0a 23 69 6e 63  | <string.h>.#inc|
00000040  6c 75 64 65 20 3c 74 69  6d 65 2e 68 3e 0a 23 69  |lude <time.h>.#i|
00000050  6e 63 6c 75 64 65 20 3c  6d 61 74 68 2e 68 3e 0a  |nclude <math.h>.|
00000060  23 69 6e 63 6c 75 64 65  20 3c 73 74 64 61 72 67  |#include <stdarg|
00000070  2e 68 3e 0a 0a 23 69 6e  63 6c 75 64 65 20 22 77  |.h>..#include "w|
00000080  65 72 72 2e 68 22 0a 23  69 6e 63 6c 75 64 65 20  |err.h".#include |
00000090  22 6f 73 2e 68 22 0a 23  69 6e 63 6c 75 64 65 20  |"os.h".#include |
000000a0  22 6b 65 72 6e 65 6c 2e  68 22 0a 23 69 6e 63 6c  |"kernel.h".#incl|
000000b0  75 64 65 20 22 61 6b 62  64 2e 68 22 0a 23 69 6e  |ude "akbd.h".#in|
000000c0  63 6c 75 64 65 20 22 62  62 63 2e 68 22 0a 23 69  |clude "bbc.h".#i|
000000d0  6e 63 6c 75 64 65 20 22  70 6f 69 6e 74 65 72 2e  |nclude "pointer.|
000000e0  68 22 0a 23 69 6e 63 6c  75 64 65 20 22 76 69 73  |h".#include "vis|
000000f0  64 65 6c 61 79 2e 68 22  0a 0a 23 69 6e 63 6c 75  |delay.h"..#inclu|
00000100  64 65 20 22 73 77 69 73  2e 68 22 0a 0a 23 64 65  |de "swis.h"..#de|
00000110  66 69 6e 65 20 6c 6f 6e  65 20 31 36 0a 23 64 65  |fine lone 16.#de|
00000120  66 69 6e 65 20 6f 6e 65  20 28 31 3c 3c 6c 6f 6e  |fine one (1<<lon|
00000130  65 29 0a 23 64 65 66 69  6e 65 20 61 62 73 28 61  |e).#define abs(a|
00000140  29 20 28 28 61 29 3c 30  20 3f 20 2d 28 61 29 20  |) ((a)<0 ? -(a) |
00000150  3a 20 28 61 29 29 0a 74  79 70 65 64 65 66 20 65  |: (a)).typedef e|
00000160  6e 75 6d 20 7b 6d 75 6c  74 69 2c 20 6e 6f 6e 6d  |num {multi, nonm|
00000170  75 6c 74 69 7d 20 6d 6f  6e 69 74 6f 72 3b 0a 0a  |ulti} monitor;..|
00000180  23 64 65 66 69 6e 65 20  77 65 72 72 74 65 72 6d  |#define werrterm|
00000190  20 30 0a 09 09 09 09 2f  2a 70 72 69 6e 63 69 70  | 0...../*princip|
000001a0  61 6c 20 71 75 61 6e 74  69 74 69 65 73 20 75 6e  |al quantities un|
000001b0  62 72 61 63 6b 65 74 65  64 2c 20 64 65 72 69 76  |bracketed, deriv|
000001c0  65 64 20 71 75 61 6e 74  69 74 69 65 73 20 62 72  |ed quantities br|
000001d0  61 63 6b 65 74 65 64 3a  2a 2f 0a 74 79 70 65 64  |acketed:*/.typed|
000001e0  65 66 20 73 74 72 75 63  74 20 7b 0a 20 20 69 6e  |ef struct {.  in|
000001f0  74 20 78 2c 20 79 2c 20  72 2c 20 72 32 3b 09 09  |t x, y, r, r2;..|
00000200  2f 2a 70 6f 73 69 74 69  6f 6e 2c 20 72 61 64 69  |/*position, radi|
00000210  75 73 20 28 26 20 72 61  64 69 75 73 20 73 71 75  |us (& radius squ|
00000220  61 72 65 64 29 2a 2f 0a  7d 20 70 69 6c 6c 61 72  |ared)*/.} pillar|
00000230  5f 64 61 74 61 3b 0a 0a  74 79 70 65 64 65 66 20  |_data;..typedef |
00000240  73 74 72 75 63 74 20 7b  0a 20 20 69 6e 74 20 78  |struct {.  int x|
00000250  2c 20 79 2c 20 78 6e 2c  20 79 6e 3b 09 09 2f 2a  |, y, xn, yn;../*|
00000260  70 6f 73 69 74 69 6f 6e  20 28 26 20 6e 65 77 20  |position (& new |
00000270  70 6f 73 69 74 69 6f 6e  29 2a 2f 0a 20 20 69 6e  |position)*/.  in|
00000280  74 20 76 78 2c 20 76 79  2c 20 6d 76 3b 09 09 2f  |t vx, vy, mv;../|
00000290  2a 76 65 6c 6f 63 69 74  79 20 28 26 20 73 70 65  |*velocity (& spe|
000002a0  65 64 29 2a 2f 0a 20 20  69 6e 74 20 64 6d 2c 20  |ed)*/.  int dm, |
000002b0  69 6d 3b 09 09 09 2f 2a  64 69 73 74 61 6e 63 65  |im;.../*distance|
000002c0  20 73 71 75 61 72 65 64  20 74 6f 20 6e 65 61 72  | squared to near|
000002d0  65 73 74 20 62 6f 69 64  20 26 20 69 6e 64 65 78  |est boid & index|
000002e0  20 6f 66 20 74 68 61 74  20 62 6f 69 64 2a 2f 0a  | of that boid*/.|
000002f0  20 20 69 6e 74 20 61 78  2c 20 61 79 2c 20 61 76  |  int ax, ay, av|
00000300  78 2c 20 61 76 79 2c 20  61 63 3b 09 2f 2a 61 63  |x, avy, ac;./*ac|
00000310  63 75 6d 75 6c 61 74 65  64 20 72 65 6c 61 74 69  |cumulated relati|
00000320  76 65 2d 70 6f 73 69 74  69 6f 6e 73 2f 76 65 6c  |ve-positions/vel|
00000330  6f 63 69 74 69 65 73 20  6f 66 20 62 6f 69 64 73  |ocities of boids|
00000340  20 69 6e 20 6c 6f 63 61  6c 65 20 26 20 6e 75 6d  | in locale & num|
00000350  62 65 72 20 6f 66 20 73  75 63 68 2a 2f 0a 20 20  |ber of such*/.  |
00000360  69 6e 74 20 64 75 6d 6d  79 31 35 2c 20 64 75 6d  |int dummy15, dum|
00000370  6d 79 31 36 3b 09 09 2f  2a 70 61 64 20 73 69 7a  |my16;../*pad siz|
00000380  65 20 74 6f 20 61 20 70  6f 77 65 72 20 6f 66 20  |e to a power of |
00000390  32 2a 2f 0a 7d 20 62 6f  69 64 5f 64 61 74 61 3b  |2*/.} boid_data;|
000003a0  0a 0a 65 78 74 65 72 6e  20 69 6e 74 20 72 62 62  |..extern int rbb|
000003b0  63 69 6e 63 28 69 6e 74  20 72 2c 20 69 6e 74 20  |cinc(int r, int |
000003c0  6b 29 3b 0a 65 78 74 65  72 6e 20 69 6e 74 20 64  |k);.extern int d|
000003d0  69 76 5f 66 72 61 63 31  36 28 69 6e 74 20 6e 75  |iv_frac16(int nu|
000003e0  6d 62 65 72 2c 20 69 6e  74 20 64 69 76 69 73 6f  |mber, int diviso|
000003f0  72 29 3b 0a 65 78 74 65  72 6e 20 69 6e 74 20 6d  |r);.extern int m|
00000400  75 6c 5f 66 72 61 63 31  36 28 69 6e 74 20 78 2c  |ul_frac16(int x,|
00000410  20 69 6e 74 20 61 29 3b  0a 65 78 74 65 72 6e 20  | int a);.extern |
00000420  69 6e 74 20 6d 75 6c 5f  66 72 61 63 31 36 63 28  |int mul_frac16c(|
00000430  69 6e 74 20 78 2c 20 69  6e 74 20 61 29 3b 0a 65  |int x, int a);.e|
00000440  78 74 65 72 6e 20 69 6e  74 20 73 71 72 74 5f 66  |xtern int sqrt_f|
00000450  72 61 63 31 36 28 75 6e  73 69 67 6e 65 64 20 69  |rac16(unsigned i|
00000460  6e 74 20 78 29 3b 0a 65  78 74 65 72 6e 20 69 6e  |nt x);.extern in|
00000470  74 20 67 61 75 73 73 31  36 28 76 6f 69 64 29 3b  |t gauss16(void);|
00000480  0a 65 78 74 65 72 6e 20  76 6f 69 64 20 73 67 61  |.extern void sga|
00000490  75 73 73 31 36 28 69 6e  74 20 73 65 65 64 29 3b  |uss16(int seed);|
000004a0  0a 65 78 74 65 72 6e 20  69 6e 74 20 72 61 6e 64  |.extern int rand|
000004b0  31 36 28 76 6f 69 64 29  3b 0a 65 78 74 65 72 6e  |16(void);.extern|
000004c0  20 69 6e 74 20 72 61 6e  64 75 31 36 28 76 6f 69  | int randu16(voi|
000004d0  64 29 3b 0a 65 78 74 65  72 6e 20 76 6f 69 64 20  |d);.extern void |
000004e0  73 72 61 6e 64 31 36 28  69 6e 74 20 73 65 65 64  |srand16(int seed|
000004f0  29 3b 0a 65 78 74 65 72  6e 20 69 6e 74 20 63 6f  |);.extern int co|
00000500  73 31 36 28 69 6e 74 20  61 29 3b 0a 65 78 74 65  |s16(int a);.exte|
00000510  72 6e 20 69 6e 74 20 73  69 6e 31 36 28 69 6e 74  |rn int sin16(int|
00000520  20 61 29 3b 0a 65 78 74  65 72 6e 20 69 6e 74 20  | a);.extern int |
00000530  65 78 70 31 36 28 69 6e  74 20 61 29 3b 0a 65 78  |exp16(int a);.ex|
00000540  74 65 72 6e 20 69 6e 74  20 6c 6e 31 36 28 69 6e  |tern int ln16(in|
00000550  74 20 61 29 3b 0a 65 78  74 65 72 6e 20 69 6e 74  |t a);.extern int|
00000560  20 70 6f 77 31 36 28 69  6e 74 20 61 2c 20 69 6e  | pow16(int a, in|
00000570  74 20 62 29 3b 0a 65 78  74 65 72 6e 20 69 6e 74  |t b);.extern int|
00000580  20 61 63 73 31 36 28 69  6e 74 20 61 29 3b 0a 65  | acs16(int a);.e|
00000590  78 74 65 72 6e 20 69 6e  74 20 61 73 6e 31 36 28  |xtern int asn16(|
000005a0  69 6e 74 20 61 29 3b 0a  65 78 74 65 72 6e 20 69  |int a);.extern i|
000005b0  6e 74 20 73 69 67 31 36  28 69 6e 74 20 61 29 3b  |nt sig16(int a);|
000005c0  0a 0a 2f 2a 6d 75 6c 74  69 70 6c 69 65 73 20 74  |../*multiplies t|
000005d0  77 6f 20 31 36 20 62 69  74 20 66 69 78 65 64 20  |wo 16 bit fixed |
000005e0  70 6f 69 6e 74 20 6e 75  6d 62 65 72 73 2c 20 72  |point numbers, r|
000005f0  65 74 75 72 6e 69 6e 67  20 61 20 31 30 20 62 69  |eturning a 10 bi|
00000600  74 20 66 69 78 65 64 20  70 6f 69 6e 74 20 6e 75  |t fixed point nu|
00000610  6d 62 65 72 2c 20 68 65  6e 63 65 20 63 61 6e 20  |mber, hence can |
00000620  72 65 70 72 65 73 65 6e  74 0a 20 20 6e 75 6d 62  |represent.  numb|
00000630  65 72 73 20 75 70 74 6f  20 31 34 34 38 2a 31 34  |ers upto 1448*14|
00000640  34 38 20 65 67 20 72 65  73 75 6c 74 20 6f 66 20  |48 eg result of |
00000650  6d 75 6c 31 36 31 30 28  31 34 34 38 2a 6f 6e 65  |mul1610(1448*one|
00000660  2c 31 34 34 38 2a 6f 6e  65 29 3b 0a 20 20 75 73  |,1448*one);.  us|
00000670  65 66 75 6c 20 66 6f 72  20 61 72 69 74 68 6d 65  |eful for arithme|
00000680  74 69 63 20 77 69 74 68  20 73 71 75 61 72 65 73  |tic with squares|
00000690  20 6f 66 20 70 6f 73 69  74 69 6f 6e 73 20 6f 72  | of positions or|
000006a0  20 76 65 6c 6f 63 69 74  69 65 73 2a 2f 0a 23 64  | velocities*/.#d|
000006b0  65 66 69 6e 65 20 6d 75  6c 31 36 31 30 28 61 2c  |efine mul1610(a,|
000006c0  62 29 20 28 6d 75 6c 5f  66 72 61 63 31 36 28 28  |b) (mul_frac16((|
000006d0  61 29 2f 38 2c 28 62 29  2f 38 29 29 0a 2f 2a 72  |a)/8,(b)/8))./*r|
000006e0  65 74 75 72 6e 73 20 31  36 20 62 66 70 20 73 71  |eturns 16 bfp sq|
000006f0  75 61 72 65 20 72 6f 6f  74 20 6f 66 20 31 30 20  |uare root of 10 |
00000700  62 66 70 20 61 72 67 75  6d 65 6e 74 2a 2f 0a 23  |bfp argument*/.#|
00000710  64 65 66 69 6e 65 20 73  71 72 74 31 30 31 36 28  |define sqrt1016(|
00000720  61 29 20 28 38 2a 73 71  72 74 5f 66 72 61 63 31  |a) (8*sqrt_frac1|
00000730  36 28 28 61 29 29 29 0a  0a 42 4f 4f 4c 20 76 67  |6((a)))..BOOL vg|
00000740  61 3b 0a 69 6e 74 20 6d  6f 64 65 3b 0a 69 6e 74  |a;.int mode;.int|
00000750  20 62 61 6e 6b 3b 0a 42  4f 4f 4c 20 73 69 6e 67  | bank;.BOOL sing|
00000760  6c 65 5f 73 74 65 70 3b  0a 69 6e 74 20 74 6e 32  |le_step;.int tn2|
00000770  2c 20 74 74 6f 74 61 6c  3b 0a 0a 69 6e 74 20 6e  |, ttotal;..int n|
00000780  2c 20 67 3b 0a 69 6e 74  20 72 63 2c 20 72 6d 2c  |, g;.int rc, rm,|
00000790  20 72 6c 2c 20 72 63 32  2c 20 72 6c 32 3b 0a 69  | rl, rc2, rl2;.i|
000007a0  6e 74 20 70 6e 2c 20 6d  63 3b 0a 70 69 6c 6c 61  |nt pn, mc;.pilla|
000007b0  72 5f 64 61 74 61 20 2a  70 69 6c 6c 61 72 3d 30  |r_data *pillar=0|
000007c0  3b 0a 62 6f 69 64 5f 64  61 74 61 20 2a 62 6f 69  |;.boid_data *boi|
000007d0  64 3d 30 3b 0a 0a 6d 6f  6e 69 74 6f 72 20 72 65  |d=0;..monitor re|
000007e0  61 64 5f 6d 6f 6e 69 74  6f 72 5f 74 79 70 65 28  |ad_monitor_type(|
000007f0  76 6f 69 64 29 0a 7b 0a  20 20 69 6e 74 20 72 32  |void).{.  int r2|
00000800  3b 0a 20 20 6f 73 5f 73  77 69 33 72 28 36 2c 20  |;.  os_swi3r(6, |
00000810  31 36 31 2c 31 33 33 2c  30 2c 20 30 2c 30 2c 26  |161,133,0, 0,0,&|
00000820  72 32 29 3b 0a 20 20 69  66 20 28 28 72 32 2f 34  |r2);.  if ((r2/4|
00000830  20 26 20 33 29 20 3d 3d  20 31 29 20 72 65 74 75  | & 3) == 1) retu|
00000840  72 6e 20 6d 75 6c 74 69  3b 0a 20 20 65 6c 73 65  |rn multi;.  else|
00000850  20 72 65 74 75 72 6e 20  6e 6f 6e 6d 75 6c 74 69  | return nonmulti|
00000860  3b 0a 7d 0a 0a 76 6f 69  64 20 74 69 64 79 28 76  |;.}..void tidy(v|
00000870  6f 69 64 29 0a 7b 0a 20  20 6f 73 5f 73 77 69 32  |oid).{.  os_swi2|
00000880  28 4f 53 5f 42 79 74 65  2c 20 31 31 32 2c 20 31  |(OS_Byte, 112, 1|
00000890  29 3b 0a 20 20 6f 73 5f  73 77 69 32 28 4f 53 5f  |);.  os_swi2(OS_|
000008a0  42 79 74 65 2c 20 31 31  33 2c 20 31 29 3b 0a 7d  |Byte, 113, 1);.}|
000008b0  0a 0a 76 6f 69 64 20 73  77 61 70 5f 62 61 6e 6b  |..void swap_bank|
000008c0  73 28 76 6f 69 64 29 0a  7b 0a 20 20 6f 73 5f 73  |s(void).{.  os_s|
000008d0  77 69 32 28 4f 53 5f 42  79 74 65 2c 20 31 31 33  |wi2(OS_Byte, 113|
000008e0  2c 20 62 61 6e 6b 29 3b  0a 20 20 6f 73 5f 73 77  |, bank);.  os_sw|
000008f0  69 32 28 4f 53 5f 42 79  74 65 2c 20 31 31 32 2c  |i2(OS_Byte, 112,|
00000900  20 62 61 6e 6b 3d 33 2d  62 61 6e 6b 29 3b 0a 7d  | bank=3-bank);.}|
00000910  0a 0a 76 6f 69 64 20 70  72 65 70 61 72 65 5f 73  |..void prepare_s|
00000920  63 72 65 65 6e 28 76 6f  69 64 29 0a 7b 0a 20 20  |creen(void).{.  |
00000930  62 61 6e 6b 3d 31 3b 0a  20 20 6f 73 5f 73 77 69  |bank=1;.  os_swi|
00000940  32 28 4f 53 5f 42 79 74  65 2c 20 31 31 32 2c 20  |2(OS_Byte, 112, |
00000950  62 61 6e 6b 29 3b 0a 20  20 6f 73 5f 73 77 69 32  |bank);.  os_swi2|
00000960  28 4f 53 5f 42 79 74 65  2c 20 31 31 33 2c 20 33  |(OS_Byte, 113, 3|
00000970  2d 62 61 6e 6b 29 3b 0a  7d 0a 0a 76 6f 69 64 20  |-bank);.}..void |
00000980  70 72 65 73 73 28 69 6e  74 20 69 29 0a 7b 0a 20  |press(int i).{. |
00000990  20 66 6f 72 20 28 3b 62  62 63 5f 69 6e 6b 65 79  | for (;bbc_inkey|
000009a0  28 69 29 21 3d 2d 31 3b  29 3b 0a 20 20 72 65 74  |(i)!=-1;);.  ret|
000009b0  75 72 6e 3b 0a 7d 0a 0a  76 6f 69 64 20 72 65 6c  |urn;.}..void rel|
000009c0  65 61 73 65 28 69 6e 74  20 69 29 0a 7b 0a 20 20  |ease(int i).{.  |
000009d0  66 6f 72 20 28 3b 62 62  63 5f 69 6e 6b 65 79 28  |for (;bbc_inkey(|
000009e0  69 29 21 3d 30 3b 29 3b  0a 20 20 72 65 74 75 72  |i)!=0;);.  retur|
000009f0  6e 3b 0a 7d 0a 0a 42 4f  4f 4c 20 6b 62 68 69 74  |n;.}..BOOL kbhit|
00000a00  28 76 6f 69 64 29 0a 7b  0a 20 20 72 65 74 75 72  |(void).{.  retur|
00000a10  6e 20 62 62 63 5f 69 6e  6b 65 79 28 2d 39 39 29  |n bbc_inkey(-99)|
00000a20  3d 3d 2d 31 3b 0a 7d 0a  0a 76 6f 69 64 20 73 74  |==-1;.}..void st|
00000a30  65 70 28 76 6f 69 64 29  0a 7b 0a 20 20 69 66 20  |ep(void).{.  if |
00000a40  28 62 62 63 5f 69 6e 6b  65 79 28 2d 38 32 29 29  |(bbc_inkey(-82))|
00000a50  20 73 69 6e 67 6c 65 5f  73 74 65 70 3d 54 52 55  | single_step=TRU|
00000a60  45 3b 0a 20 20 69 66 20  28 73 69 6e 67 6c 65 5f  |E;.  if (single_|
00000a70  73 74 65 70 29 20 66 6f  72 20 28 3b 20 73 69 6e  |step) for (; sin|
00000a80  67 6c 65 5f 73 74 65 70  3b 29 20 7b 0a 20 20 20  |gle_step;) {.   |
00000a90  20 69 66 20 28 62 62 63  5f 69 6e 6b 65 79 28 2d  | if (bbc_inkey(-|
00000aa0  38 32 29 29 20 7b 0a 20  20 20 20 20 20 72 65 6c  |82)) {.      rel|
00000ab0  65 61 73 65 28 2d 38 32  29 3b 0a 20 20 20 20 20  |ease(-82);.     |
00000ac0  20 62 72 65 61 6b 3b 0a  20 20 20 20 7d 0a 20 20  | break;.    }.  |
00000ad0  20 20 69 66 20 28 62 62  63 5f 69 6e 6b 65 79 28  |  if (bbc_inkey(|
00000ae0  2d 38 33 29 29 20 73 69  6e 67 6c 65 5f 73 74 65  |-83)) single_ste|
00000af0  70 3d 46 41 4c 53 45 3b  0a 20 20 7d 0a 7d 0a 0a  |p=FALSE;.  }.}..|
00000b00  76 6f 69 64 20 73 65 65  64 5f 72 61 6e 64 31 36  |void seed_rand16|
00000b10  28 76 6f 69 64 29 0a 7b  0a 20 20 69 6e 74 20 76  |(void).{.  int v|
00000b20  3d 63 6c 6f 63 6b 28 29  20 26 20 30 78 66 66 3b  |=clock() & 0xff;|
00000b30  0a 20 20 76 7c 3d 76 3c  3c 38 3b 0a 20 20 73 72  |.  v|=v<<8;.  sr|
00000b40  61 6e 64 31 36 28 76 20  7c 20 76 3c 3c 31 36 29  |and16(v | v<<16)|
00000b50  3b 0a 7d 0a 0a 2f 2a 72  65 74 75 72 6e 73 20 31  |;.}../*returns 1|
00000b60  36 62 66 70 20 6d 61 67  6e 69 74 75 64 65 20 6f  |6bfp magnitude o|
00000b70  66 20 76 65 63 74 6f 72  20 77 69 74 68 20 31 36  |f vector with 16|
00000b80  62 66 70 20 63 6f 6d 70  6f 6e 65 6e 74 73 20 78  |bfp components x|
00000b90  20 26 20 79 2a 2f 0a 69  6e 74 20 6d 61 67 6e 69  | & y*/.int magni|
00000ba0  74 75 64 65 28 69 6e 74  20 78 2c 20 69 6e 74 20  |tude(int x, int |
00000bb0  79 29 0a 7b 0a 20 20 69  6e 74 20 66 20 3d 20 73  |y).{.  int f = s|
00000bc0  71 72 74 31 30 31 36 28  6d 75 6c 31 36 31 30 28  |qrt1016(mul1610(|
00000bd0  78 2c 78 29 2b 6d 75 6c  31 36 31 30 28 79 2c 79  |x,x)+mul1610(y,y|
00000be0  29 29 3b 0a 20 20 69 66  20 28 66 3c 61 62 73 28  |));.  if (f<abs(|
00000bf0  78 29 29 20 66 3d 61 62  73 28 78 29 3b 09 2f 2a  |x)) f=abs(x);./*|
00000c00  63 6f 6d 70 65 6e 73 61  74 65 20 66 6f 72 20 73  |compensate for s|
00000c10  6f 6d 65 20 6e 61 73 74  79 20 75 6e 64 65 72 66  |ome nasty underf|
00000c20  6c 6f 77 20 70 72 6f 62  6c 65 6d 73 2a 2f 0a 20  |low problems*/. |
00000c30  20 69 66 20 28 66 3c 61  62 73 28 79 29 29 20 66  | if (f<abs(y)) f|
00000c40  3d 61 62 73 28 79 29 3b  09 2f 2a 77 69 74 68 20  |=abs(y);./*with |
00000c50  61 62 6f 76 65 20 66 69  78 65 64 20 70 6f 69 6e  |above fixed poin|
00000c60  74 20 61 72 69 74 68 6d  65 74 69 63 2a 2f 0a 20  |t arithmetic*/. |
00000c70  20 72 65 74 75 72 6e 20  66 3c 6f 6e 65 2f 32 35  | return f<one/25|
00000c80  36 20 3f 20 6f 6e 65 2f  32 35 36 20 3a 20 66 3b  |6 ? one/256 : f;|
00000c90  0a 7d 0a 0a 69 6e 74 20  6e 65 61 72 65 73 74 73  |.}..int nearests|
00000ca0  5f 73 71 72 74 28 69 6e  74 20 69 29 0a 7b 0a 20  |_sqrt(int i).{. |
00000cb0  20 69 6e 74 20 66 20 3d  20 73 71 72 74 31 30 31  | int f = sqrt101|
00000cc0  36 28 62 6f 69 64 5b 69  5d 2e 64 6d 29 3b 0a 20  |6(boid[i].dm);. |
00000cd0  20 69 66 20 28 66 3c 61  62 73 28 62 6f 69 64 5b  | if (f<abs(boid[|
00000ce0  62 6f 69 64 5b 69 5d 2e  69 6d 5d 2e 78 2d 62 6f  |boid[i].im].x-bo|
00000cf0  69 64 5b 69 5d 2e 78 29  29 20 66 3d 61 62 73 28  |id[i].x)) f=abs(|
00000d00  62 6f 69 64 5b 62 6f 69  64 5b 69 5d 2e 69 6d 5d  |boid[boid[i].im]|
00000d10  2e 78 2d 62 6f 69 64 5b  69 5d 2e 78 29 3b 0a 20  |.x-boid[i].x);. |
00000d20  20 69 66 20 28 66 3c 61  62 73 28 62 6f 69 64 5b  | if (f<abs(boid[|
00000d30  62 6f 69 64 5b 69 5d 2e  69 6d 5d 2e 79 2d 62 6f  |boid[i].im].y-bo|
00000d40  69 64 5b 69 5d 2e 79 29  29 20 66 3d 61 62 73 28  |id[i].y)) f=abs(|
00000d50  62 6f 69 64 5b 62 6f 69  64 5b 69 5d 2e 69 6d 5d  |boid[boid[i].im]|
00000d60  2e 79 2d 62 6f 69 64 5b  69 5d 2e 79 29 3b 0a 20  |.y-boid[i].y);. |
00000d70  20 72 65 74 75 72 6e 20  66 3c 6f 6e 65 2f 32 35  | return f<one/25|
00000d80  36 20 3f 20 6f 6e 65 2f  32 35 36 20 3a 20 66 3b  |6 ? one/256 : f;|
00000d90  0a 7d 0a 0a 2f 2a 27 62  6f 75 6e 63 65 27 20 76  |.}../*'bounce' v|
00000da0  65 6c 6f 63 69 74 79 20  76 69 61 20 61 70 70 72  |elocity via appr|
00000db0  6f 61 63 68 20 76 65 63  74 6f 72 2c 20 6e 62 20  |oach vector, nb |
00000dc0  78 2c 79 2c 76 78 2c 76  79 20 31 36 62 66 70 2c  |x,y,vx,vy 16bfp,|
00000dd0  20 77 68 69 6c 65 20 6d  3d 78 2a 78 2b 79 2a 79  | while m=x*x+y*y|
00000de0  20 31 30 62 66 70 2a 2f  0a 76 6f 69 64 20 62 6f  | 10bfp*/.void bo|
00000df0  75 6e 63 65 28 69 6e 74  20 78 2c 20 69 6e 74 20  |unce(int x, int |
00000e00  79 2c 20 69 6e 74 20 2a  76 78 2c 20 69 6e 74 20  |y, int *vx, int |
00000e10  2a 76 79 2c 20 69 6e 74  20 6d 29 0a 7b 0a 20 20  |*vy, int m).{.  |
00000e20  69 6e 74 20 20 73 20 3d  20 6d 75 6c 31 36 31 30  |int  s = mul1610|
00000e30  28 78 2c 20 2a 76 78 29  20 2b 20 6d 75 6c 31 36  |(x, *vx) + mul16|
00000e40  31 30 28 79 2c 20 2a 76  79 29 3b 0a 20 20 69 66  |10(y, *vy);.  if|
00000e50  20 28 73 3e 30 29 20 7b  0a 20 20 20 20 20 20 20  | (s>0) {.       |
00000e60  73 20 3d 20 32 2a 64 69  76 5f 66 72 61 63 31 36  |s = 2*div_frac16|
00000e70  28 73 2c 20 6d 3d 3d 30  20 3f 20 31 20 3a 20 6d  |(s, m==0 ? 1 : m|
00000e80  29 3b 0a 20 20 20 20 2a  76 78 20 2d 3d 20 6d 75  |);.    *vx -= mu|
00000e90  6c 5f 66 72 61 63 31 36  28 78 2c 20 73 29 3b 0a  |l_frac16(x, s);.|
00000ea0  20 20 20 20 2a 76 79 20  2d 3d 20 6d 75 6c 5f 66  |    *vy -= mul_f|
00000eb0  72 61 63 31 36 28 79 2c  20 73 29 3b 0a 20 20 7d  |rac16(y, s);.  }|
00000ec0  0a 7d 0a 0a 42 4f 4f 4c  20 69 6e 69 74 28 76 6f  |.}..BOOL init(vo|
00000ed0  69 64 29 0a 7b 0a 20 20  69 6e 74 20 69 3b 0a 20  |id).{.  int i;. |
00000ee0  20 61 74 65 78 69 74 28  74 69 64 79 29 3b 0a 20  | atexit(tidy);. |
00000ef0  20 76 67 61 20 3d 20 72  65 61 64 5f 6d 6f 6e 69  | vga = read_moni|
00000f00  74 6f 72 5f 74 79 70 65  28 29 3d 3d 6d 75 6c 74  |tor_type()==mult|
00000f10  69 20 3f 20 54 52 55 45  20 3a 20 46 41 4c 53 45  |i ? TRUE : FALSE|
00000f20  3b 0a 20 20 6d 6f 64 65  20 3d 20 76 67 61 20 3f  |;.  mode = vga ?|
00000f30  20 31 38 20 3a 20 30 3b  0a 20 20 74 69 64 79 28  | 18 : 0;.  tidy(|
00000f40  29 3b 0a 20 20 62 62 63  5f 6d 6f 64 65 28 6d 6f  |);.  bbc_mode(mo|
00000f50  64 65 2b 31 32 38 29 3b  0a 20 20 62 62 63 5f 6d  |de+128);.  bbc_m|
00000f60  6f 64 65 28 6d 6f 64 65  29 3b 0a 20 20 62 62 63  |ode(mode);.  bbc|
00000f70  5f 63 75 72 73 6f 72 28  30 29 3b 0a 20 20 6f 73  |_cursor(0);.  os|
00000f80  5f 63 6c 69 28 22 70 6f  69 6e 74 65 72 22 29 3b  |_cli("pointer");|
00000f90  0a 20 20 62 62 63 5f 6f  72 69 67 69 6e 28 36 34  |.  bbc_origin(64|
00000fa0  30 2c 35 31 32 29 3b 0a  20 20 70 6f 69 6e 74 65  |0,512);.  pointe|
00000fb0  72 5f 72 65 73 65 74 5f  73 68 61 70 65 28 29 3b  |r_reset_shape();|
00000fc0  0a 20 20 72 65 6c 65 61  73 65 28 2d 39 39 29 3b  |.  release(-99);|
00000fd0  0a 20 20 6f 73 5f 73 77  69 32 28 4f 53 5f 42 79  |.  os_swi2(OS_By|
00000fe0  74 65 2c 20 31 35 2c 20  31 29 3b 0a 20 20 64 6f  |te, 15, 1);.  do|
00000ff0  20 7b 0a 20 20 20 20 70  72 69 6e 74 66 28 22 50  | {.    printf("P|
00001000  6c 65 61 73 65 20 65 6e  74 65 72 20 6e 75 6d 62  |lease enter numb|
00001010  65 72 20 6f 66 20 62 6f  69 64 73 20 28 6d 69 6e  |er of boids (min|
00001020  20 32 2c 20 73 75 67 67  65 73 74 20 32 34 2b 29  | 2, suggest 24+)|
00001030  20 22 29 3b 0a 20 20 20  20 73 63 61 6e 66 28 22  | ");.    scanf("|
00001040  25 69 22 2c 20 26 6e 29  3b 0a 20 20 7d 20 77 68  |%i", &n);.  } wh|
00001050  69 6c 65 20 28 6e 3c 32  29 3b 0a 20 20 64 6f 20  |ile (n<2);.  do |
00001060  7b 0a 20 20 20 20 70 72  69 6e 74 66 28 22 50 6c  |{.    printf("Pl|
00001070  65 61 73 65 20 65 6e 74  65 72 20 6e 75 6d 62 65  |ease enter numbe|
00001080  72 20 6f 66 20 70 69 6c  6c 61 72 73 20 28 31 20  |r of pillars (1 |
00001090  74 6f 20 31 30 30 2c 20  73 75 67 67 65 73 74 20  |to 100, suggest |
000010a0  34 29 20 22 29 3b 0a 20  20 20 20 73 63 61 6e 66  |4) ");.    scanf|
000010b0  28 22 25 69 22 2c 20 26  70 6e 29 3b 0a 20 20 7d  |("%i", &pn);.  }|
000010c0  20 77 68 69 6c 65 20 28  70 6e 3c 31 20 7c 7c 20  | while (pn<1 || |
000010d0  70 6e 3e 31 30 30 29 3b  0a 20 20 64 6f 20 7b 0a  |pn>100);.  do {.|
000010e0  20 20 20 20 70 72 69 6e  74 66 28 22 50 6c 65 61  |    printf("Plea|
000010f0  73 65 20 65 6e 74 65 72  20 62 6f 69 64 20 73 69  |se enter boid si|
00001100  7a 65 20 28 31 20 74 6f  20 38 2c 20 73 75 67 67  |ze (1 to 8, sugg|
00001110  65 73 74 20 34 29 20 22  29 3b 0a 20 20 20 20 73  |est 4) ");.    s|
00001120  63 61 6e 66 28 22 25 69  22 2c 20 26 67 29 3b 0a  |canf("%i", &g);.|
00001130  20 20 7d 20 77 68 69 6c  65 20 28 67 3c 31 20 7c  |  } while (g<1 ||
00001140  7c 20 67 3e 38 29 3b 0a  20 20 64 6f 20 7b 0a 20  || g>8);.  do {. |
00001150  20 20 20 70 72 69 6e 74  66 28 22 50 6c 65 61 73  |   printf("Pleas|
00001160  65 20 65 6e 74 65 72 20  73 65 65 64 20 66 6f 72  |e enter seed for|
00001170  20 72 61 6e 64 6f 6d 20  69 6e 69 74 69 61 6c 69  | random initiali|
00001180  73 61 74 69 6f 6e 20 28  61 6e 79 20 6e 6f 6e 2d  |sation (any non-|
00001190  7a 65 72 6f 20 69 6e 74  65 67 65 72 2c 5c 6e 6f  |zero integer,\no|
000011a0  72 20 30 20 66 6f 72 20  61 20 27 72 61 6e 64 6f  |r 0 for a 'rando|
000011b0  6d 27 20 73 65 65 64 3b  20 74 72 79 20 31 30 31  |m' seed; try 101|
000011c0  29 20 22 29 3b 0a 20 20  20 20 73 63 61 6e 66 28  |) ");.    scanf(|
000011d0  22 25 69 22 2c 20 26 69  29 3b 0a 20 20 7d 20 77  |"%i", &i);.  } w|
000011e0  68 69 6c 65 20 28 46 41  4c 53 45 29 3b 0a 0a 20  |hile (FALSE);.. |
000011f0  20 69 66 20 28 69 29 20  73 72 61 6e 64 31 36 28  | if (i) srand16(|
00001200  7e 28 69 5e 28 69 3c 3c  31 36 29 29 29 3b 0a 20  |~(i^(i<<16)));. |
00001210  20 65 6c 73 65 20 73 65  65 64 5f 72 61 6e 64 31  | else seed_rand1|
00001220  36 28 29 3b 0a 20 20 72  63 3d 35 2a 67 2a 6f 6e  |6();.  rc=5*g*on|
00001230  65 3b 09 09 2f 2a 72 61  64 69 75 73 20 66 6f 72  |e;../*radius for|
00001240  20 63 6f 6c 6c 69 73 69  6f 6e 20 64 65 74 65 63  | collision detec|
00001250  74 69 6f 6e 2a 2f 0a 20  20 72 6d 3d 38 2a 67 2a  |tion*/.  rm=8*g*|
00001260  6f 6e 65 3b 09 09 2f 2a  72 61 64 69 75 73 20 66  |one;../*radius f|
00001270  6f 72 20 76 65 6c 6f 63  69 74 79 20 6d 61 74 63  |or velocity matc|
00001280  68 69 6e 67 2a 2f 0a 20  20 72 6c 3d 34 30 2a 67  |hing*/.  rl=40*g|
00001290  2a 6f 6e 65 3b 09 09 2f  2a 72 61 64 69 75 73 20  |*one;../*radius |
000012a0  6f 66 20 6c 6f 63 61 6c  65 2a 2f 0a 20 20 72 63  |of locale*/.  rc|
000012b0  32 3d 6d 75 6c 31 36 31  30 28 72 63 2c 72 63 29  |2=mul1610(rc,rc)|
000012c0  3b 0a 20 20 72 6c 32 3d  6d 75 6c 31 36 31 30 28  |;.  rl2=mul1610(|
000012d0  72 6c 2c 72 6c 29 3b 0a  20 20 69 66 20 28 70 69  |rl,rl);.  if (pi|
000012e0  6c 6c 61 72 29 20 7b 0a  20 20 20 20 66 72 65 65  |llar) {.    free|
000012f0  28 70 69 6c 6c 61 72 29  3b 0a 20 20 20 20 70 69  |(pillar);.    pi|
00001300  6c 6c 61 72 3d 30 3b 0a  20 20 7d 0a 20 20 69 66  |llar=0;.  }.  if|
00001310  20 28 62 6f 69 64 29 20  7b 0a 20 20 20 20 66 72  | (boid) {.    fr|
00001320  65 65 28 62 6f 69 64 29  3b 0a 20 20 20 20 62 6f  |ee(boid);.    bo|
00001330  69 64 3d 30 3b 0a 20 20  7d 0a 20 20 69 66 20 28  |id=0;.  }.  if (|
00001340  70 69 6c 6c 61 72 20 3d  20 63 61 6c 6c 6f 63 28  |pillar = calloc(|
00001350  70 6e 2c 20 73 69 7a 65  6f 66 28 70 69 6c 6c 61  |pn, sizeof(pilla|
00001360  72 5f 64 61 74 61 29 29  2c 20 70 69 6c 6c 61 72  |r_data)), pillar|
00001370  3d 3d 30 29 20 7b 0a 20  20 20 20 77 65 72 72 28  |==0) {.    werr(|
00001380  77 65 72 72 74 65 72 6d  2c 20 22 49 6e 73 75 66  |werrterm, "Insuf|
00001390  66 69 63 69 65 6e 74 20  6d 65 6d 6f 72 79 22 29  |ficient memory")|
000013a0  3b 0a 20 20 20 20 72 65  74 75 72 6e 20 46 41 4c  |;.    return FAL|
000013b0  53 45 3b 0a 20 20 7d 0a  20 20 69 66 20 28 62 6f  |SE;.  }.  if (bo|
000013c0  69 64 20 20 20 3d 20 63  61 6c 6c 6f 63 28 20 6e  |id   = calloc( n|
000013d0  2c 20 73 69 7a 65 6f 66  28 20 20 62 6f 69 64 5f  |, sizeof(  boid_|
000013e0  64 61 74 61 29 29 2c 20  20 20 62 6f 69 64 3d 3d  |data)),   boid==|
000013f0  30 29 20 7b 0a 20 20 20  20 77 65 72 72 28 77 65  |0) {.    werr(we|
00001400  72 72 74 65 72 6d 2c 20  22 49 6e 73 75 66 66 69  |rrterm, "Insuffi|
00001410  63 69 65 6e 74 20 6d 65  6d 6f 72 79 22 29 3b 0a  |cient memory");.|
00001420  20 20 20 20 72 65 74 75  72 6e 20 46 41 4c 53 45  |    return FALSE|
00001430  3b 0a 20 20 7d 0a 20 20  6d 63 3d 30 3b 0a 20 20  |;.  }.  mc=0;.  |
00001440  66 6f 72 20 28 69 3d 30  3b 20 69 3c 70 6e 3b 20  |for (i=0; i<pn; |
00001450  69 2b 2b 29 20 7b 0a 20  20 20 20 70 69 6c 6c 61  |i++) {.    pilla|
00001460  72 5b 69 5d 2e 78 20 20  3d 20 72 61 6e 64 31 36  |r[i].x  = rand16|
00001470  28 29 2a 36 34 30 3b 0a  20 20 20 20 70 69 6c 6c  |()*640;.    pill|
00001480  61 72 5b 69 5d 2e 79 20  20 3d 20 72 61 6e 64 31  |ar[i].y  = rand1|
00001490  36 28 29 2a 35 31 32 3b  0a 20 20 20 20 70 69 6c  |6()*512;.    pil|
000014a0  6c 61 72 5b 69 5d 2e 72  20 20 3d 20 72 61 6e 64  |lar[i].r  = rand|
000014b0  75 31 36 28 29 2a 34 30  3b 0a 20 20 20 20 70 69  |u16()*40;.    pi|
000014c0  6c 6c 61 72 5b 69 5d 2e  72 32 20 3d 20 6d 75 6c  |llar[i].r2 = mul|
000014d0  31 36 31 30 28 70 69 6c  6c 61 72 5b 69 5d 2e 72  |1610(pillar[i].r|
000014e0  2b 72 63 2f 32 2c 20 70  69 6c 6c 61 72 5b 69 5d  |+rc/2, pillar[i]|
000014f0  2e 72 2b 72 63 2f 32 29  3b 0a 20 20 7d 0a 20 20  |.r+rc/2);.  }.  |
00001500  66 6f 72 20 28 69 3d 30  3b 20 69 3c 6e 3b 20 69  |for (i=0; i<n; i|
00001510  2b 2b 29 20 7b 0a 20 20  20 20 62 6f 69 64 5b 69  |++) {.    boid[i|
00001520  5d 2e 78 20 20 3d 20 36  34 2a 67 2a 72 61 6e 64  |].x  = 64*g*rand|
00001530  31 36 28 29 3b 0a 20 20  20 20 62 6f 69 64 5b 69  |16();.    boid[i|
00001540  5d 2e 79 20 20 3d 20 36  34 2a 67 2a 72 61 6e 64  |].y  = 64*g*rand|
00001550  31 36 28 29 3b 0a 20 20  20 20 62 6f 69 64 5b 69  |16();.    boid[i|
00001560  5d 2e 76 78 20 3d 20 20  34 2a 67 2a 72 61 6e 64  |].vx =  4*g*rand|
00001570  75 31 36 28 29 3b 0a 20  20 20 20 62 6f 69 64 5b  |u16();.    boid[|
00001580  69 5d 2e 76 79 20 3d 20  20 32 2a 67 2a 72 61 6e  |i].vy =  2*g*ran|
00001590  64 31 36 28 29 3b 0a 20  20 20 20 62 6f 69 64 5b  |d16();.    boid[|
000015a0  69 5d 2e 6d 76 20 3d 20  6d 61 67 6e 69 74 75 64  |i].mv = magnitud|
000015b0  65 28 62 6f 69 64 5b 69  5d 2e 76 78 2c 62 6f 69  |e(boid[i].vx,boi|
000015c0  64 5b 69 5d 2e 76 79 29  3b 0a 20 20 7d 0a 20 20  |d[i].vy);.  }.  |
000015d0  70 72 65 70 61 72 65 5f  73 63 72 65 65 6e 28 29  |prepare_screen()|
000015e0  3b 0a 20 20 72 65 74 75  72 6e 20 54 52 55 45 3b  |;.  return TRUE;|
000015f0  0a 7d 0a 0a 76 6f 69 64  20 63 68 65 63 6b 5f 6d  |.}..void check_m|
00001600  6f 75 73 65 28 76 6f 69  64 29 0a 7b 0a 20 20 69  |ouse(void).{.  i|
00001610  6e 74 20 78 2c 79 2c 7a  2c 20 69 2c 64 3b 0a 20  |nt x,y,z, i,d;. |
00001620  20 6f 73 5f 73 77 69 33  72 28 4f 53 5f 4d 6f 75  | os_swi3r(OS_Mou|
00001630  73 65 2c 20 30 2c 30 2c  30 2c 20 26 78 2c 26 79  |se, 0,0,0, &x,&y|
00001640  2c 26 7a 29 3b 0a 20 20  78 2a 3d 6f 6e 65 3b 0a  |,&z);.  x*=one;.|
00001650  20 20 79 2a 3d 6f 6e 65  3b 0a 20 20 73 77 69 74  |  y*=one;.  swit|
00001660  63 68 28 7a 29 20 7b 0a  20 20 20 20 63 61 73 65  |ch(z) {.    case|
00001670  20 31 3a 0a 20 20 20 20  20 20 69 66 20 28 70 69  | 1:.      if (pi|
00001680  6c 6c 61 72 5b 6d 63 5d  2e 72 3e 6f 6e 65 29 20  |llar[mc].r>one) |
00001690  7b 0a 20 20 20 20 20 20  20 20 70 69 6c 6c 61 72  |{.        pillar|
000016a0  5b 6d 63 5d 2e 72 2d 3d  6f 6e 65 3b 0a 20 20 20  |[mc].r-=one;.   |
000016b0  20 20 20 20 20 70 69 6c  6c 61 72 5b 6d 63 5d 2e  |     pillar[mc].|
000016c0  72 32 3d 6d 75 6c 31 36  31 30 28 70 69 6c 6c 61  |r2=mul1610(pilla|
000016d0  72 5b 6d 63 5d 2e 72 2b  72 63 2f 32 2c 20 70 69  |r[mc].r+rc/2, pi|
000016e0  6c 6c 61 72 5b 6d 63 5d  2e 72 2b 72 63 2f 32 29  |llar[mc].r+rc/2)|
000016f0  3b 0a 20 20 20 20 20 20  7d 0a 20 20 20 20 20 20  |;.      }.      |
00001700  62 72 65 61 6b 3b 0a 20  20 20 20 63 61 73 65 20  |break;.    case |
00001710  32 3a 0a 20 20 20 20 20  20 69 66 20 28 70 69 6c  |2:.      if (pil|
00001720  6c 61 72 5b 6d 63 5d 2e  72 3c 6f 6e 65 2a 32 30  |lar[mc].r<one*20|
00001730  30 29 20 7b 0a 20 20 20  20 20 20 20 20 70 69 6c  |0) {.        pil|
00001740  6c 61 72 5b 6d 63 5d 2e  72 2b 3d 6f 6e 65 3b 0a  |lar[mc].r+=one;.|
00001750  20 20 20 20 20 20 20 20  70 69 6c 6c 61 72 5b 6d  |        pillar[m|
00001760  63 5d 2e 72 32 3d 6d 75  6c 31 36 31 30 28 70 69  |c].r2=mul1610(pi|
00001770  6c 6c 61 72 5b 6d 63 5d  2e 72 2b 72 63 2f 32 2c  |llar[mc].r+rc/2,|
00001780  20 70 69 6c 6c 61 72 5b  6d 63 5d 2e 72 2b 72 63  | pillar[mc].r+rc|
00001790  2f 32 29 3b 0a 20 20 20  20 20 20 7d 0a 20 20 20  |/2);.      }.   |
000017a0  20 20 20 62 72 65 61 6b  3b 0a 20 20 20 20 63 61  |   break;.    ca|
000017b0  73 65 20 34 3a 0a 20 20  20 20 20 20 66 6f 72 20  |se 4:.      for |
000017c0  28 69 3d 6d 63 3b 20 69  3c 70 6e 3b 20 69 2b 2b  |(i=mc; i<pn; i++|
000017d0  29 20 7b 0a 20 20 20 20  20 20 20 20 69 66 20 28  |) {.        if (|
000017e0  61 62 73 28 70 69 6c 6c  61 72 5b 69 5d 2e 78 2d  |abs(pillar[i].x-|
000017f0  78 29 3c 70 69 6c 6c 61  72 5b 69 5d 2e 72 20 26  |x)<pillar[i].r &|
00001800  26 20 61 62 73 28 70 69  6c 6c 61 72 5b 69 5d 2e  |& abs(pillar[i].|
00001810  79 2d 79 29 3c 70 69 6c  6c 61 72 5b 69 5d 2e 72  |y-y)<pillar[i].r|
00001820  29 20 7b 0a 20 20 20 20  20 20 20 20 20 20 64 20  |) {.          d |
00001830  3d 20 6d 75 6c 31 36 31  30 28 70 69 6c 6c 61 72  |= mul1610(pillar|
00001840  5b 69 5d 2e 78 2d 78 2c  70 69 6c 6c 61 72 5b 69  |[i].x-x,pillar[i|
00001850  5d 2e 78 2d 78 29 20 2b  20 6d 75 6c 31 36 31 30  |].x-x) + mul1610|
00001860  28 70 69 6c 6c 61 72 5b  69 5d 2e 79 2d 79 2c 70  |(pillar[i].y-y,p|
00001870  69 6c 6c 61 72 5b 69 5d  2e 79 2d 79 29 3b 0a 20  |illar[i].y-y);. |
00001880  20 20 20 20 20 20 20 20  20 69 66 20 28 64 20 3c  |         if (d <|
00001890  20 70 69 6c 6c 61 72 5b  69 5d 2e 72 32 29 20 7b  | pillar[i].r2) {|
000018a0  0a 20 20 20 20 20 20 20  20 20 20 20 20 69 2b 3d  |.            i+=|
000018b0  30 78 31 30 30 30 30 3b  0a 20 20 20 20 20 20 20  |0x10000;.       |
000018c0  20 20 20 20 20 62 72 65  61 6b 3b 0a 20 20 20 20  |     break;.    |
000018d0  20 20 20 20 20 20 7d 0a  20 20 20 20 20 20 20 20  |      }.        |
000018e0  7d 0a 20 20 20 20 20 20  20 20 69 66 20 28 69 3d  |}.        if (i=|
000018f0  3d 6d 63 29 20 20 20 69  3d 2d 31 3b 0a 20 20 20  |=mc)   i=-1;.   |
00001900  20 20 20 20 20 69 66 20  28 69 3d 3d 6d 63 2d 31  |     if (i==mc-1|
00001910  29 20 69 3d 6d 63 3b 0a  20 20 20 20 20 20 7d 0a  |) i=mc;.      }.|
00001920  20 20 20 20 20 20 69 66  20 28 69 26 30 78 31 30  |      if (i&0x10|
00001930  30 30 30 20 26 26 20 69  21 3d 30 78 31 30 30 30  |000 && i!=0x1000|
00001940  30 2b 6d 63 29 20 7b 0a  20 20 20 20 20 20 20 20  |0+mc) {.        |
00001950  6d 63 20 3d 20 69 2d 30  78 31 30 30 30 30 3b 0a  |mc = i-0x10000;.|
00001960  20 20 20 20 20 20 20 20  62 62 63 5f 76 64 75 28  |        bbc_vdu(|
00001970  37 29 3b 0a 20 20 20 20  20 20 7d 0a 20 20 20 20  |7);.      }.    |
00001980  20 20 65 6c 73 65 20 7b  0a 20 20 20 20 20 20 20  |  else {.       |
00001990  20 70 69 6c 6c 61 72 5b  6d 63 5d 2e 78 20 3d 20  | pillar[mc].x = |
000019a0  78 3b 0a 20 20 20 20 20  20 20 20 70 69 6c 6c 61  |x;.        pilla|
000019b0  72 5b 6d 63 5d 2e 79 20  3d 20 79 3b 0a 20 20 20  |r[mc].y = y;.   |
000019c0  20 20 20 7d 0a 20 20 20  20 20 20 62 72 65 61 6b  |   }.      break|
000019d0  3b 0a 20 20 7d 0a 7d 0a  0a 76 6f 69 64 20 6d 6f  |;.  }.}..void mo|
000019e0  76 65 5f 62 6f 69 64 73  28 76 6f 69 64 29 0a 7b  |ve_boids(void).{|
000019f0  0a 20 20 69 6e 74 20 69  2c 20 6a 2c 20 64 3b 0a  |.  int i, j, d;.|
00001a00  20 20 69 6e 74 20 78 2c  20 79 2c 20 76 78 2c 20  |  int x, y, vx, |
00001a10  76 79 3b 0a 20 20 69 6e  74 20 61 2c 20 62 3b 0a  |vy;.  int a, b;.|
00001a20  20 20 69 6e 74 20 72 78  2c 20 72 79 3b 0a 20 20  |  int rx, ry;.  |
00001a30  62 6f 69 64 5f 64 61 74  61 20 2a 62 70 2c 20 2a  |boid_data *bp, *|
00001a40  62 70 65 3b 0a 20 20 66  6f 72 20 28 62 70 3d 62  |bpe;.  for (bp=b|
00001a50  6f 69 64 2c 62 70 65 3d  62 70 2b 6e 3b 20 62 70  |oid,bpe=bp+n; bp|
00001a60  3c 62 70 65 3b 20 62 70  2b 2b 29 20 7b 0a 20 20  |<bpe; bp++) {.  |
00001a70  20 20 62 70 2d 3e 64 6d  3d 30 78 37 66 66 66 66  |  bp->dm=0x7ffff|
00001a80  66 66 66 3b 0a 20 20 20  20 62 70 2d 3e 61 78 3d  |fff;.    bp->ax=|
00001a90  62 70 2d 3e 61 79 3d 62  70 2d 3e 61 76 78 3d 62  |bp->ay=bp->avx=b|
00001aa0  70 2d 3e 61 76 79 3d 62  70 2d 3e 61 63 3d 30 3b  |p->avy=bp->ac=0;|
00001ab0  0a 20 20 7d 0a 20 20 74  6e 32 3d 63 6c 6f 63 6b  |.  }.  tn2=clock|
00001ac0  28 29 3b 0a 20 20 66 6f  72 20 28 69 3d 30 3b 20  |();.  for (i=0; |
00001ad0  69 3c 6e 2d 31 3b 20 69  2b 2b 29 0a 20 20 20 20  |i<n-1; i++).    |
00001ae0  66 6f 72 20 28 6a 3d 69  2b 31 3b 20 6a 3c 6e 3b  |for (j=i+1; j<n;|
00001af0  20 6a 2b 2b 29 20 7b 0a  20 20 20 20 20 20 61 20  | j++) {.      a |
00001b00  3d 20 28 72 78 3d 62 6f  69 64 5b 69 5d 2e 78 2d  |= (rx=boid[i].x-|
00001b10  62 6f 69 64 5b 6a 5d 2e  78 29 2f 6f 6e 65 3b 0a  |boid[j].x)/one;.|
00001b20  20 20 20 20 20 20 61 20  3d 20 61 2a 61 3b 0a 20  |      a = a*a;. |
00001b30  20 20 20 20 20 69 66 20  28 61 3c 62 6f 69 64 5b  |     if (a<boid[|
00001b40  69 5d 2e 64 6d 2f 31 30  32 34 20 7c 7c 20 61 3c  |i].dm/1024 || a<|
00001b50  62 6f 69 64 5b 6a 5d 2e  64 6d 2f 31 30 32 34 20  |boid[j].dm/1024 |
00001b60  7c 7c 20 61 3c 72 6c 32  2f 31 30 32 34 29 20 7b  ||| a<rl2/1024) {|
00001b70  0a 20 20 20 20 20 20 20  20 62 20 3d 20 28 72 79  |.        b = (ry|
00001b80  3d 62 6f 69 64 5b 69 5d  2e 79 2d 62 6f 69 64 5b  |=boid[i].y-boid[|
00001b90  6a 5d 2e 79 29 2f 6f 6e  65 3b 0a 20 20 20 20 20  |j].y)/one;.     |
00001ba0  20 20 20 61 20 2b 3d 20  62 2a 62 3b 0a 20 20 20  |   a += b*b;.   |
00001bb0  20 20 20 20 20 69 66 20  28 61 3c 62 6f 69 64 5b  |     if (a<boid[|
00001bc0  69 5d 2e 64 6d 2f 31 30  32 34 20 7c 7c 20 61 3c  |i].dm/1024 || a<|
00001bd0  62 6f 69 64 5b 6a 5d 2e  64 6d 2f 31 30 32 34 20  |boid[j].dm/1024 |
00001be0  7c 7c 20 61 3c 72 6c 32  2f 31 30 32 34 29 20 7b  ||| a<rl2/1024) {|
00001bf0  0a 20 20 20 20 20 20 20  20 20 20 64 20 3d 20 6d  |.          d = m|
00001c00  75 6c 31 36 31 30 28 72  78 2c 72 78 29 20 2b 20  |ul1610(rx,rx) + |
00001c10  6d 75 6c 31 36 31 30 28  72 79 2c 72 79 29 3b 0a  |mul1610(ry,ry);.|
00001c20  20 20 20 20 20 20 20 20  20 20 69 66 20 28 64 3c  |          if (d<|
00001c30  62 6f 69 64 5b 69 5d 2e  64 6d 29 20 7b 0a 20 20  |boid[i].dm) {.  |
00001c40  20 20 20 20 20 20 20 20  20 20 62 6f 69 64 5b 69  |          boid[i|
00001c50  5d 2e 64 6d 20 3d 20 64  3b 0a 20 20 20 20 20 20  |].dm = d;.      |
00001c60  20 20 20 20 20 20 62 6f  69 64 5b 69 5d 2e 69 6d  |      boid[i].im|
00001c70  20 3d 20 6a 3b 0a 20 20  20 20 20 20 20 20 20 20  | = j;.          |
00001c80  7d 0a 20 20 20 20 20 20  20 20 20 20 69 66 20 28  |}.          if (|
00001c90  64 3c 62 6f 69 64 5b 6a  5d 2e 64 6d 29 20 7b 0a  |d<boid[j].dm) {.|
00001ca0  20 20 20 20 20 20 20 20  20 20 20 20 62 6f 69 64  |            boid|
00001cb0  5b 6a 5d 2e 64 6d 20 3d  20 64 3b 0a 20 20 20 20  |[j].dm = d;.    |
00001cc0  20 20 20 20 20 20 20 20  62 6f 69 64 5b 6a 5d 2e  |        boid[j].|
00001cd0  69 6d 20 3d 20 69 3b 0a  20 20 20 20 20 20 20 20  |im = i;.        |
00001ce0  20 20 7d 0a 20 20 20 20  20 20 20 20 20 20 69 66  |  }.          if|
00001cf0  20 28 64 3c 72 6c 32 29  20 7b 0a 20 20 20 20 20  | (d<rl2) {.     |
00001d00  20 20 20 20 20 20 20 62  6f 69 64 5b 69 5d 2e 61  |       boid[i].a|
00001d10  78 20 20 2b 3d 20 62 6f  69 64 5b 6a 5d 2e 78 2d  |x  += boid[j].x-|
00001d20  62 6f 69 64 5b 69 5d 2e  78 3b 0a 20 20 20 20 20  |boid[i].x;.     |
00001d30  20 20 20 20 20 20 20 62  6f 69 64 5b 69 5d 2e 61  |       boid[i].a|
00001d40  79 20 20 2b 3d 20 62 6f  69 64 5b 6a 5d 2e 79 2d  |y  += boid[j].y-|
00001d50  62 6f 69 64 5b 69 5d 2e  79 3b 0a 20 20 20 20 20  |boid[i].y;.     |
00001d60  20 20 20 20 20 20 20 62  6f 69 64 5b 69 5d 2e 61  |       boid[i].a|
00001d70  76 78 20 2b 3d 20 62 6f  69 64 5b 6a 5d 2e 76 78  |vx += boid[j].vx|
00001d80  3b 0a 20 20 20 20 20 20  20 20 20 20 20 20 62 6f  |;.            bo|
00001d90  69 64 5b 69 5d 2e 61 76  79 20 2b 3d 20 62 6f 69  |id[i].avy += boi|
00001da0  64 5b 6a 5d 2e 76 79 3b  0a 20 20 20 20 20 20 20  |d[j].vy;.       |
00001db0  20 20 20 20 20 62 6f 69  64 5b 69 5d 2e 61 63 20  |     boid[i].ac |
00001dc0  20 2b 3d 20 31 3b 0a 20  20 20 20 20 20 20 20 20  | += 1;.         |
00001dd0  20 20 20 62 6f 69 64 5b  6a 5d 2e 61 78 20 20 2b  |   boid[j].ax  +|
00001de0  3d 20 62 6f 69 64 5b 69  5d 2e 78 2d 62 6f 69 64  |= boid[i].x-boid|
00001df0  5b 6a 5d 2e 78 3b 0a 20  20 20 20 20 20 20 20 20  |[j].x;.         |
00001e00  20 20 20 62 6f 69 64 5b  6a 5d 2e 61 79 20 20 2b  |   boid[j].ay  +|
00001e10  3d 20 62 6f 69 64 5b 69  5d 2e 79 2d 62 6f 69 64  |= boid[i].y-boid|
00001e20  5b 6a 5d 2e 79 3b 0a 20  20 20 20 20 20 20 20 20  |[j].y;.         |
00001e30  20 20 20 62 6f 69 64 5b  6a 5d 2e 61 76 78 20 2b  |   boid[j].avx +|
00001e40  3d 20 62 6f 69 64 5b 69  5d 2e 76 78 3b 0a 20 20  |= boid[i].vx;.  |
00001e50  20 20 20 20 20 20 20 20  20 20 62 6f 69 64 5b 6a  |          boid[j|
00001e60  5d 2e 61 76 79 20 2b 3d  20 62 6f 69 64 5b 69 5d  |].avy += boid[i]|
00001e70  2e 76 79 3b 0a 20 20 20  20 20 20 20 20 20 20 20  |.vy;.           |
00001e80  20 62 6f 69 64 5b 6a 5d  2e 61 63 20 20 2b 3d 20  | boid[j].ac  += |
00001e90  31 3b 0a 20 20 20 20 20  20 20 20 20 20 7d 0a 20  |1;.          }. |
00001ea0  20 20 20 20 20 20 20 7d  0a 20 20 20 20 20 20 7d  |       }.      }|
00001eb0  0a 20 20 20 20 7d 0a 20  20 74 6e 32 3d 63 6c 6f  |.    }.  tn2=clo|
00001ec0  63 6b 28 29 2d 74 6e 32  3b 0a 20 20 66 6f 72 20  |ck()-tn2;.  for |
00001ed0  28 69 3d 30 3b 20 69 3c  6e 3b 20 69 2b 2b 29 20  |(i=0; i<n; i++) |
00001ee0  7b 0a 20 20 20 20 78 20  20 3d 20 62 6f 69 64 5b  |{.    x  = boid[|
00001ef0  69 5d 2e 78 3b 0a 20 20  20 20 79 20 20 3d 20 62  |i].x;.    y  = b|
00001f00  6f 69 64 5b 69 5d 2e 79  3b 0a 20 20 20 20 76 78  |oid[i].y;.    vx|
00001f10  20 3d 20 62 6f 69 64 5b  69 5d 2e 76 78 3b 0a 20  | = boid[i].vx;. |
00001f20  20 20 20 76 79 20 3d 20  62 6f 69 64 5b 69 5d 2e  |   vy = boid[i].|
00001f30  76 79 3b 0a 20 20 20 20  69 66 20 28 62 6f 69 64  |vy;.    if (boid|
00001f40  5b 69 5d 2e 61 63 3c 31  29 20 7b 0a 20 20 20 20  |[i].ac<1) {.    |
00001f50  20 20 6a 20 3d 20 62 6f  69 64 5b 69 5d 2e 69 6d  |  j = boid[i].im|
00001f60  3b 0a 20 20 20 20 20 20  62 6f 69 64 5b 69 5d 2e  |;.      boid[i].|
00001f70  61 78 20 20 2b 3d 20 62  6f 69 64 5b 6a 5d 2e 78  |ax  += boid[j].x|
00001f80  2d 78 3b 0a 20 20 20 20  20 20 62 6f 69 64 5b 69  |-x;.      boid[i|
00001f90  5d 2e 61 79 20 20 2b 3d  20 62 6f 69 64 5b 6a 5d  |].ay  += boid[j]|
00001fa0  2e 79 2d 79 3b 0a 20 20  20 20 20 20 62 6f 69 64  |.y-y;.      boid|
00001fb0  5b 69 5d 2e 61 76 78 20  2b 3d 20 62 6f 69 64 5b  |[i].avx += boid[|
00001fc0  6a 5d 2e 76 78 3b 0a 20  20 20 20 20 20 62 6f 69  |j].vx;.      boi|
00001fd0  64 5b 69 5d 2e 61 76 79  20 2b 3d 20 62 6f 69 64  |d[i].avy += boid|
00001fe0  5b 6a 5d 2e 76 79 3b 0a  20 20 20 20 20 20 62 6f  |[j].vy;.      bo|
00001ff0  69 64 5b 69 5d 2e 61 63  20 20 2b 3d 20 31 3b 0a  |id[i].ac  += 1;.|
00002000  20 20 20 20 7d 0a 20 20  20 20 69 66 20 28 78 3c  |    }.    if (x<|
00002010  72 63 2d 36 34 30 2a 6f  6e 65 20 7c 7c 20 78 3e  |rc-640*one || x>|
00002020  36 34 30 2a 6f 6e 65 2d  72 63 20 7c 7c 20 79 3c  |640*one-rc || y<|
00002030  72 63 2d 35 31 32 2a 6f  6e 65 20 7c 7c 20 79 3e  |rc-512*one || y>|
00002040  35 31 32 2a 6f 6e 65 2d  72 63 29 20 7b 0a 20 20  |512*one-rc) {.  |
00002050  20 20 20 20 69 66 20 28  78 3c 72 63 2d 36 34 30  |    if (x<rc-640|
00002060  2a 6f 6e 65 26 26 76 78  3c 30 20 7c 7c 20 78 3e  |*one&&vx<0 || x>|
00002070  36 34 30 2a 6f 6e 65 2d  72 63 26 26 76 78 3e 30  |640*one-rc&&vx>0|
00002080  29 20 76 78 3d 2d 76 78  3b 0a 20 20 20 20 20 20  |) vx=-vx;.      |
00002090  69 66 20 28 79 3c 72 63  2d 35 31 32 2a 6f 6e 65  |if (y<rc-512*one|
000020a0  26 26 76 79 3c 30 20 7c  7c 20 79 3e 35 31 32 2a  |&&vy<0 || y>512*|
000020b0  6f 6e 65 2d 72 63 26 26  76 79 3e 30 29 20 76 79  |one-rc&&vy>0) vy|
000020c0  3d 2d 76 79 3b 0a 20 20  20 20 7d 0a 20 20 20 20  |=-vy;.    }.    |
000020d0  65 6c 73 65 20 7b 0a 20  20 20 20 20 20 66 6f 72  |else {.      for|
000020e0  20 28 6a 3d 30 3b 20 6a  3c 70 6e 3b 20 6a 2b 2b  | (j=0; j<pn; j++|
000020f0  29 20 7b 0a 20 20 20 20  20 20 20 20 69 66 20 28  |) {.        if (|
00002100  61 62 73 28 70 69 6c 6c  61 72 5b 6a 5d 2e 78 2d  |abs(pillar[j].x-|
00002110  78 29 3c 70 69 6c 6c 61  72 5b 6a 5d 2e 72 20 26  |x)<pillar[j].r &|
00002120  26 20 61 62 73 28 70 69  6c 6c 61 72 5b 6a 5d 2e  |& abs(pillar[j].|
00002130  79 2d 79 29 3c 70 69 6c  6c 61 72 5b 6a 5d 2e 72  |y-y)<pillar[j].r|
00002140  29 20 7b 0a 20 20 20 20  20 20 20 20 20 20 64 20  |) {.          d |
00002150  3d 20 6d 75 6c 31 36 31  30 28 70 69 6c 6c 61 72  |= mul1610(pillar|
00002160  5b 6a 5d 2e 78 2d 78 2c  70 69 6c 6c 61 72 5b 6a  |[j].x-x,pillar[j|
00002170  5d 2e 78 2d 78 29 20 2b  20 6d 75 6c 31 36 31 30  |].x-x) + mul1610|
00002180  28 70 69 6c 6c 61 72 5b  6a 5d 2e 79 2d 79 2c 70  |(pillar[j].y-y,p|
00002190  69 6c 6c 61 72 5b 6a 5d  2e 79 2d 79 29 3b 0a 20  |illar[j].y-y);. |
000021a0  20 20 20 20 20 20 20 20  20 69 66 20 28 64 20 3c  |         if (d <|
000021b0  20 70 69 6c 6c 61 72 5b  6a 5d 2e 72 32 29 20 7b  | pillar[j].r2) {|
000021c0  0a 20 20 20 20 20 20 20  20 20 20 20 20 6a 2b 3d  |.            j+=|
000021d0  30 78 31 30 30 30 30 3b  0a 20 20 20 20 20 20 20  |0x10000;.       |
000021e0  20 20 20 20 20 62 72 65  61 6b 3b 0a 20 20 20 20  |     break;.    |
000021f0  20 20 20 20 20 20 7d 0a  20 20 20 20 20 20 20 20  |      }.        |
00002200  7d 0a 20 20 20 20 20 20  7d 0a 20 20 20 20 20 20  |}.      }.      |
00002210  69 66 20 28 6a 20 26 20  30 78 31 30 30 30 30 29  |if (j & 0x10000)|
00002220  20 7b 0a 20 20 20 20 20  20 20 20 6a 2d 3d 30 78  | {.        j-=0x|
00002230  31 30 30 30 30 3b 0a 20  20 20 20 20 20 20 20 62  |10000;.        b|
00002240  6f 75 6e 63 65 28 70 69  6c 6c 61 72 5b 6a 5d 2e  |ounce(pillar[j].|
00002250  78 2d 78 2c 20 70 69 6c  6c 61 72 5b 6a 5d 2e 79  |x-x, pillar[j].y|
00002260  2d 79 2c 20 26 76 78 2c  20 26 76 79 2c 20 64 29  |-y, &vx, &vy, d)|
00002270  3b 0a 20 20 20 20 20 20  7d 0a 20 20 20 20 20 20  |;.      }.      |
00002280  65 6c 73 65 20 69 66 20  28 62 6f 69 64 5b 69 5d  |else if (boid[i]|
00002290  2e 64 6d 3c 72 63 32 29  20 62 6f 75 6e 63 65 28  |.dm<rc2) bounce(|
000022a0  62 6f 69 64 5b 62 6f 69  64 5b 69 5d 2e 69 6d 5d  |boid[boid[i].im]|
000022b0  2e 78 2d 78 2c 20 62 6f  69 64 5b 62 6f 69 64 5b  |.x-x, boid[boid[|
000022c0  69 5d 2e 69 6d 5d 2e 79  2d 79 2c 20 26 76 78 2c  |i].im].y-y, &vx,|
000022d0  20 26 76 79 2c 20 62 6f  69 64 5b 69 5d 2e 64 6d  | &vy, boid[i].dm|
000022e0  29 3b 0a 20 20 20 20 20  20 65 6c 73 65 20 7b 0a  |);.      else {.|
000022f0  20 20 20 20 20 20 20 20  61 20 3d 20 62 6f 69 64  |        a = boid|
00002300  5b 69 5d 2e 61 78 2f 62  6f 69 64 5b 69 5d 2e 61  |[i].ax/boid[i].a|
00002310  63 3b 0a 20 20 20 20 20  20 20 20 62 20 3d 20 62  |c;.        b = b|
00002320  6f 69 64 5b 69 5d 2e 61  79 2f 62 6f 69 64 5b 69  |oid[i].ay/boid[i|
00002330  5d 2e 61 63 3b 0a 20 20  20 20 20 20 20 20 64 20  |].ac;.        d |
00002340  3d 20 6d 61 67 6e 69 74  75 64 65 28 61 2c 62 29  |= magnitude(a,b)|
00002350  3b 0a 20 20 20 20 20 20  20 20 61 20 3d 20 64 69  |;.        a = di|
00002360  76 5f 66 72 61 63 31 36  28 61 2c 64 29 3b 0a 20  |v_frac16(a,d);. |
00002370  20 20 20 20 20 20 20 62  20 3d 20 64 69 76 5f 66  |       b = div_f|
00002380  72 61 63 31 36 28 62 2c  64 29 3b 0a 20 20 20 20  |rac16(b,d);.    |
00002390  20 20 20 20 64 20 3d 20  6e 65 61 72 65 73 74 73  |    d = nearests|
000023a0  5f 73 71 72 74 28 69 29  3b 0a 20 20 20 20 20 20  |_sqrt(i);.      |
000023b0  20 20 69 66 20 28 64 3c  72 6d 29 20 7b 0a 20 20  |  if (d<rm) {.  |
000023c0  20 20 20 20 20 20 20 20  76 78 20 3d 20 64 69 76  |        vx = div|
000023d0  5f 66 72 61 63 31 36 28  20 6d 75 6c 5f 66 72 61  |_frac16( mul_fra|
000023e0  63 31 36 28 62 6f 69 64  5b 69 5d 2e 61 76 78 2f  |c16(boid[i].avx/|
000023f0  62 6f 69 64 5b 69 5d 2e  61 63 2c 20 72 6d 2d 64  |boid[i].ac, rm-d|
00002400  29 2b 6d 75 6c 5f 66 72  61 63 31 36 28 64 2d 72  |)+mul_frac16(d-r|
00002410  63 2c 20 61 2a 67 29 20  2c 20 72 6d 2d 72 63 20  |c, a*g) , rm-rc |
00002420  29 3b 0a 20 20 20 20 20  20 20 20 20 20 76 79 20  |);.          vy |
00002430  3d 20 64 69 76 5f 66 72  61 63 31 36 28 20 6d 75  |= div_frac16( mu|
00002440  6c 5f 66 72 61 63 31 36  28 62 6f 69 64 5b 69 5d  |l_frac16(boid[i]|
00002450  2e 61 76 79 2f 62 6f 69  64 5b 69 5d 2e 61 63 2c  |.avy/boid[i].ac,|
00002460  20 72 6d 2d 64 29 2b 6d  75 6c 5f 66 72 61 63 31  | rm-d)+mul_frac1|
00002470  36 28 64 2d 72 63 2c 20  62 2a 67 29 20 2c 20 72  |6(d-rc, b*g) , r|
00002480  6d 2d 72 63 20 29 3b 0a  20 20 20 20 20 20 20 20  |m-rc );.        |
00002490  7d 0a 20 20 20 20 20 20  20 20 65 6c 73 65 20 7b  |}.        else {|
000024a0  0a 20 20 20 20 20 20 20  20 20 20 64 20 20 3d 20  |.          d  = |
000024b0  64 69 76 5f 66 72 61 63  31 36 28 64 2c 20 72 6d  |div_frac16(d, rm|
000024c0  29 3b 0a 20 20 20 20 20  20 20 20 20 20 69 66 20  |);.          if |
000024d0  28 64 3e 31 36 2a 6f 6e  65 29 20 64 3d 31 36 2a  |(d>16*one) d=16*|
000024e0  6f 6e 65 3b 09 2f 2a 70  72 65 76 65 6e 74 20 72  |one;./*prevent r|
000024f0  69 64 69 63 75 6c 6f 75  73 6c 79 20 68 69 67 68  |idiculously high|
00002500  20 63 61 74 63 68 20 75  70 20 73 70 65 65 64 73  | catch up speeds|
00002510  2a 2f 0a 20 20 20 20 20  20 20 20 20 20 76 78 20  |*/.          vx |
00002520  3d 20 6d 75 6c 5f 66 72  61 63 31 36 28 64 2c 20  |= mul_frac16(d, |
00002530  61 2a 67 29 3b 0a 20 20  20 20 20 20 20 20 20 20  |a*g);.          |
00002540  76 79 20 3d 20 6d 75 6c  5f 66 72 61 63 31 36 28  |vy = mul_frac16(|
00002550  64 2c 20 62 2a 67 29 3b  0a 20 20 20 20 20 20 20  |d, b*g);.       |
00002560  20 7d 0a 20 20 20 20 20  20 7d 0a 20 20 20 20 7d  | }.      }.    }|
00002570  0a 20 20 20 20 64 20 3d  20 6d 61 67 6e 69 74 75  |.    d = magnitu|
00002580  64 65 28 76 78 2c 76 79  29 3b 0a 20 20 20 20 69  |de(vx,vy);.    i|
00002590  66 20 28 64 3c 67 2a 6f  6e 65 29 20 7b 0a 20 20  |f (d<g*one) {.  |
000025a0  20 20 20 20 76 78 20 3d  20 64 69 76 5f 66 72 61  |    vx = div_fra|
000025b0  63 31 36 28 76 78 2a 67  2c 64 29 3b 0a 20 20 20  |c16(vx*g,d);.   |
000025c0  20 20 20 76 79 20 3d 20  64 69 76 5f 66 72 61 63  |   vy = div_frac|
000025d0  31 36 28 76 79 2a 67 2c  64 29 3b 0a 20 20 20 20  |16(vy*g,d);.    |
000025e0  20 20 64 20 20 3d 20 67  2a 6f 6e 65 3b 0a 20 20  |  d  = g*one;.  |
000025f0  20 20 7d 0a 20 20 20 20  62 6f 69 64 5b 69 5d 2e  |  }.    boid[i].|
00002600  78 6e 20 3d 20 62 6f 69  64 5b 69 5d 2e 78 2b 76  |xn = boid[i].x+v|
00002610  78 3b 0a 20 20 20 20 62  6f 69 64 5b 69 5d 2e 79  |x;.    boid[i].y|
00002620  6e 20 3d 20 62 6f 69 64  5b 69 5d 2e 79 2b 76 79  |n = boid[i].y+vy|
00002630  3b 0a 20 20 20 20 62 6f  69 64 5b 69 5d 2e 76 78  |;.    boid[i].vx|
00002640  20 3d 20 76 78 3b 0a 20  20 20 20 62 6f 69 64 5b  | = vx;.    boid[|
00002650  69 5d 2e 76 79 20 3d 20  76 79 3b 0a 20 20 20 20  |i].vy = vy;.    |
00002660  62 6f 69 64 5b 69 5d 2e  6d 76 20 3d 20 64 3b 0a  |boid[i].mv = d;.|
00002670  20 20 7d 0a 20 20 66 6f  72 20 28 69 3d 30 3b 20  |  }.  for (i=0; |
00002680  69 3c 6e 3b 20 69 2b 2b  29 20 7b 0a 20 20 20 20  |i<n; i++) {.    |
00002690  62 6f 69 64 5b 69 5d 2e  78 20 3d 20 62 6f 69 64  |boid[i].x = boid|
000026a0  5b 69 5d 2e 78 6e 3b 0a  20 20 20 20 62 6f 69 64  |[i].xn;.    boid|
000026b0  5b 69 5d 2e 79 20 3d 20  62 6f 69 64 5b 69 5d 2e  |[i].y = boid[i].|
000026c0  79 6e 3b 0a 20 20 7d 0a  7d 0a 0a 69 6e 74 20 6d  |yn;.  }.}..int m|
000026d0  61 69 6e 28 76 6f 69 64  29 0a 7b 0a 20 20 69 6e  |ain(void).{.  in|
000026e0  74 20 69 3b 0a 20 20 69  66 20 28 21 69 6e 69 74  |t i;.  if (!init|
000026f0  28 29 29 20 72 65 74 75  72 6e 20 30 3b 0a 20 20  |()) return 0;.  |
00002700  66 6f 72 20 28 73 69 6e  67 6c 65 5f 73 74 65 70  |for (single_step|
00002710  3d 46 41 4c 53 45 3b 3b  29 20 7b 0a 20 20 20 20  |=FALSE;;) {.    |
00002720  74 74 6f 74 61 6c 3d 63  6c 6f 63 6b 28 29 3b 0a  |ttotal=clock();.|
00002730  20 20 20 20 62 62 63 5f  63 6c 73 28 29 3b 0a 20  |    bbc_cls();. |
00002740  20 20 20 6d 6f 76 65 5f  62 6f 69 64 73 28 29 3b  |   move_boids();|
00002750  0a 20 20 20 20 63 68 65  63 6b 5f 6d 6f 75 73 65  |.    check_mouse|
00002760  28 29 3b 0a 20 20 20 20  66 6f 72 20 28 69 3d 30  |();.    for (i=0|
00002770  3b 20 69 3c 6e 3b 20 69  2b 2b 29 20 7b 0a 20 20  |; i<n; i++) {.  |
00002780  20 20 20 20 62 62 63 5f  6d 6f 76 65 28 62 6f 69  |    bbc_move(boi|
00002790  64 5b 69 5d 2e 78 2f 6f  6e 65 2c 20 62 6f 69 64  |d[i].x/one, boid|
000027a0  5b 69 5d 2e 79 2f 6f 6e  65 29 3b 0a 20 20 20 20  |[i].y/one);.    |
000027b0  20 20 62 62 63 5f 64 72  61 77 62 79 28 28 62 6f  |  bbc_drawby((bo|
000027c0  69 64 5b 69 5d 2e 76 78  2a 32 2a 67 29 2f 62 6f  |id[i].vx*2*g)/bo|
000027d0  69 64 5b 69 5d 2e 6d 76  2c 20 28 62 6f 69 64 5b  |id[i].mv, (boid[|
000027e0  69 5d 2e 76 79 2a 32 2a  67 29 2f 62 6f 69 64 5b  |i].vy*2*g)/boid[|
000027f0  69 5d 2e 6d 76 29 3b 0a  20 20 20 20 7d 0a 20 20  |i].mv);.    }.  |
00002800  20 20 66 6f 72 20 28 69  3d 30 3b 20 69 3c 70 6e  |  for (i=0; i<pn|
00002810  3b 20 69 2b 2b 29 20 62  62 63 5f 63 69 72 63 6c  |; i++) bbc_circl|
00002820  65 28 70 69 6c 6c 61 72  5b 69 5d 2e 78 2f 6f 6e  |e(pillar[i].x/on|
00002830  65 2c 20 70 69 6c 6c 61  72 5b 69 5d 2e 79 2f 6f  |e, pillar[i].y/o|
00002840  6e 65 2c 20 70 69 6c 6c  61 72 5b 69 5d 2e 72 2f  |ne, pillar[i].r/|
00002850  6f 6e 65 29 3b 0a 20 20  20 20 74 74 6f 74 61 6c  |one);.    ttotal|
00002860  3d 63 6c 6f 63 6b 28 29  2d 74 74 6f 74 61 6c 3b  |=clock()-ttotal;|
00002870  0a 20 20 20 20 70 72 69  6e 74 66 28 22 25 69 5c  |.    printf("%i\|
00002880  6e 25 69 5c 6e 22 2c 74  6e 32 2c 74 74 6f 74 61  |n%i\n",tn2,ttota|
00002890  6c 29 3b 0a 20 20 20 20  6f 73 5f 73 77 69 31 28  |l);.    os_swi1(|
000028a0  4f 53 5f 42 79 74 65 2c  20 31 39 29 3b 0a 20 20  |OS_Byte, 19);.  |
000028b0  20 20 73 77 61 70 5f 62  61 6e 6b 73 28 29 3b 0a  |  swap_banks();.|
000028c0  20 20 20 20 73 74 65 70  28 29 3b 0a 20 20 20 20  |    step();.    |
000028d0  69 66 20 28 6b 62 68 69  74 28 29 29 0a 20 20 20  |if (kbhit()).   |
000028e0  20 20 20 69 66 20 28 21  69 6e 69 74 28 29 29 20  |   if (!init()) |
000028f0  62 72 65 61 6b 3b 0a 20  20 7d 0a 20 20 72 65 74  |break;.  }.  ret|
00002900  75 72 6e 20 30 3b 0a 7d  0a                       |urn 0;.}.|
00002909