#include <stdio.h>
#include <stdlib.h>
#include "intlong.h"

#define umod(x,y) ((unsigned)(x) % (unsigned)(y))
bit16 rnd();
FILE *fpi;
startype stars[1050],*star_ptr;
planettype planets[10],*planet_ptr;
planettype moons[10],*moon_ptr;
bit16 n_stars;
bit16 n_suns;
bit16 n_planets;
bit16 n_moons;
bit16 randseed;
bit16 n_near,n_far;
startype *far_root,*near_root;
extern bit16 icos[];

void make_planet(bit16 x, bit16 y, bit16 z, bit16 r, bit16 col, bit16 type)
{
  planettype *p;
  p = &planets[n_planets++];
  p->pos[0] = x;
  p->pos[1] = y;
  p->pos[2] = z;
  p->r = r;
  p->colour = col;
  p->type = type;
#ifdef DEBUG
  printf("planet type %d at %d,%d",type,x,y);
#endif
}

void enter_galaxy(void)
{
#ifdef NEVER
  register int i;
  int x,y,z;
  fpi = fopen("stars.dat","r");
  if(fpi == NULL) exit(0);
  fscanf(fpi,"%d",&n_stars);
  for(i=0; i<n_stars; i++) {
    stars[i].next = &stars[i+1];
    fscanf(fpi,"%d,%d,%d",&stars[i].pos[0]
                         ,&stars[i].pos[1]
                         ,&stars[i].pos[2]);
    x = stars[i].pos[0] / 64;
    y = stars[i].pos[1] / 64;
    z = stars[i].pos[2] / 64;
  }
  stars[i-1].next = stars;
#else
  n_stars = 2;
  stars[0].pos[0] = 3;
  stars[0].pos[1] = 4;
  stars[0].pos[2] = 5;
  stars[1].pos[0] = 103;
  stars[1].pos[1] = 104;
  stars[1].pos[2] = 105;
  stars[0].next = &stars[1];
  stars[0].next = &stars[0];
#endif
  far_root = stars;
  near_root = stars;
  n_far = n_stars;
  n_near = 0;
}

void enter_solar(void)
{
  bit16 n_xp;
  bit16 i;
  bit16 r_ss,r;
  bit16 r_inc;
  randseed = (star_ptr - stars);
  r = 500 + (rnd() & 255);
  n_planets = 0;
  switch(rnd() & 3) {
    case 3:     make_planet(-r*2,-r*2,0,r,7,0);
    case 2:     make_planet(r*2,-r*2,0,r/2,6,0);
    case 0:
    case 1:     make_planet(0,0,0,r,5,0);
  }
  n_suns = n_planets;
  n_xp = umod(rnd(),5) + 1;
  r *= 3;
  r_ss = 5000 + umod(rnd(),9000);
  r_inc = (r_ss - r) / n_xp;
  for(; r<r_ss-100; r += r_inc) {
    i = rnd();
    make_planet(div14(mul16(icos[i & 31],r)),div14(mul16(icos[(i+8) & 31],r))
                ,0,10,7,1);
  }
}

void enter_planet(void)
{
}

bit16 rnd(void)
{
  return (randseed = randseed * 3421 + 1);
}

