// way too much division and 32bit maths - unusably slow.
// ... should run like shit off a shovel on the PiTrex though!

#include <vectrex.h>
#define pot0 (*((volatile bit8 *) 0xc81b))
#define pot1 (*((volatile bit8 *) 0xc81c))

typedef char bit8;
typedef long bit16;
typedef long long bit32;

#define mul16(x,y) ((bit32)(x) * (bit32)(y))
//#define div14(x) (bit16)((x) / 16384)
#define div14(x) (bit16)((x) >> 14)
#define abs(x) (((x) < 0) ? -(x) : (x))

#define VPX1 64
#define VPY1 88
#define VPX2 256
#define VPY2 216
#define MAX_BODY 8

const bit16 particles_data[] = {
        32,
        -96,39,95,59,77,-108,-2,-11,-100,115,52,9,121,-45,117,111,
        9,17,44,52,62,43,-11,-42,-87,61,11,-18,-113,69,4,17,
        62,
        42,-68,-9,-94,-3,-113,-34,19,125,-53,41,112,-30,100,76,
        114,-45,-22,-19,60,-71,-71,68,47,56,111,-60,5,-7,-92,-4,
        28,-82,-43,-65,18,80,-95,-125,-108,-84,55,7,111,29,15,56,
        -16,-101,-40,86,106,-12,-78,82,19,90,-98,123,21,30,50,90
        };

static bit16 divy(bit32 x);

static bit16 view_speed = 3;
static bit32 view_big[3] = {0l,0l,0l};
static bit16 view_pos[3] = {0,0,0};
static bit16 view_vec[9] = {16384,0,0, 0,16384,0, 0,0,16384};
static bit16 x_rot_val = 0;
static bit16 y_rot_val = 0;

static void norm(bit16 *a, bit16 *b);

static void xrotate(bit16 *v, bit16 x)
{
  bit16 t[3];
  t[0] = v[6];
  t[1] = v[7];
  t[2] = v[8];
  v[6] -= div14(mul16(v[3],x));
  v[7] -= div14(mul16(v[4],x));
  v[8] -= div14(mul16(v[5],x));
  v[3] += div14(mul16(t[0],x));
  v[4] += div14(mul16(t[1],x));
  v[5] += div14(mul16(t[2],x));
  norm(v+3,v+3);
  norm(v+6,v+6);
}

static void yrotate(bit16 *v, bit16 x)
{
  bit16 t[3];
  t[0] = v[6];
  t[1] = v[7];
  t[2] = v[8];
  v[6] -= div14(mul16(v[0],x));
  v[7] -= div14(mul16(v[1],x));
  v[8] -= div14(mul16(v[2],x));
  v[0] += div14(mul16(t[0],x));
  v[1] += div14(mul16(t[1],x));
  v[2] += div14(mul16(t[2],x));
  norm(v,v);
  norm(v+3,v+3);
}

static void norm(bit16 *a, bit16 *b)
{
  bit32 v = mul16(a[0],a[0])+mul16(a[1],a[1])+mul16(a[2],a[2]);
  bit16 g,m;
  if((v < 268304400l) || (v > 268566544l)) {
    g = 0;
    for(m=16384; m != 0; m /= 2) {
      g |= m;
      if(mul16(g,g) > v) {
	   g ^= m;
      }
    }
    b[0] = (bit16)(mul16(a[0],16384) / g);
    b[1] = (bit16)(mul16(a[1],16384) / g);
    b[2] = (bit16)(mul16(a[2],16384) / g);
  }
}

static void move(void)
{
  if(x_rot_val != 0) xrotate(view_vec,x_rot_val);
  if(y_rot_val != 0) yrotate(view_vec,y_rot_val);
  view_pos[0] = div14(view_big[0] += mul16(view_vec[3],view_speed));
  view_pos[1] = div14(view_big[1] += mul16(view_vec[4],view_speed));
  view_pos[2] = div14(view_big[2] += mul16(view_vec[5],view_speed));
}

static bit32 scprod(bit16 *v1, bit16 *v2)
{
  return mul16(v1[0],v2[0]) + mul16(v1[1],v2[1]) + mul16(v1[2],v2[2]);
}

static void draw_particles(void)
{
  bit16 *p = (bit16 *)particles_data;
  bit16 i,x,y,d;
  bit16 particles_pos[3];
  for(i=*p++; i != 0; i--,p+=3) {
    particles_pos[0] = (bit8)(p[0]-view_pos[0]);
    particles_pos[1] = (bit8)(p[1]-view_pos[1]);
    particles_pos[2] = (bit8)(p[2]-view_pos[2]);
    d = div14(scprod(particles_pos,view_vec+3));
    // This can be optimised but at this point not worth the effort:
    if(d > 5) {
      x = divy(scprod(particles_pos,view_vec) / d) + (VPX1+VPX2)/2;
      if((x > VPX1) && (x < VPX2)) {
        y = divy(scprod(particles_pos,view_vec+6) / d) + (VPY1+VPY2)/2;
        if((y > VPY1) && (y < VPY2)) {
          //fprintf(stderr, "(%d,%d)\n", x, y);
          // recenter for Vectrex:  subtract (VPY1+VPY2)/2 etc
          Dot_d((int)y - (int)((VPY1>>1) + (VPY2>>1)), (int)x - (int)((VPX1>>1) + (VPX2>>1)));
        }
      }
    }
  }
}

static inline bit16 divy(bit32 x)
{
  return (bit16)(x >> 8);
}

int main(void) {
  for (;;) {
    Wait_Recal();
    Print_Str_d(0, -70, "STARFIELD\x80");

    // get an up/level/down command from the joystick
    Joy_Analog();
    x_rot_val = pot0;
    y_rot_val = pot1;

    move();

    draw_particles();
  }
  return 0;
}
