#include "main.h"

BUGGY     *player[50];

DATAFILE  *maindata;
DATAFILE  *gfxdata;

COLOR_MAP  flare_table;
COLOR_MAP  light_table;
COLOR_MAP  trans_table[3];
RGB_MAP    rgb_table;

BITMAP    *buffer;
BITMAP    *genmap;
BITMAP    *map;
BITMAP    *topbar;

short      sx = 640;
short      sy = 480;

LIST       elist;

int        numplayers = 0;


/*
Timer stuff.
*/
long frame_time = 0;
inline void frame_timer()
{
  frame_time++;
}
END_OF_FUNCTION(frame_timer);


/*
Scans the commandline parameters.
*/
const char* argfind(const char* arg, const int argc, const char* argv[])
{
  int i;
  int l = strlen(arg);
  for(i=0;i<argc;i++) {
    if(strnicmp(argv[i],arg,l)==0) {
      return &(argv[i][l]);
    }
  }
  return NULL;
}


/*
Main stuff.
*/
void main(const int argc, const char* argv[])
{
   bool showland = true;
   bool genland = false;
   int  _seed = 0;
   int  _numplayers = 2;
   int  _frameskip = 1;
   const char* s;

   /* Get command line arguments to change settings. */
   s = argfind("-fs",argc,argv);
   if (s) _frameskip = CLAMP(atoi(s), 1, 5);
   s = argfind("-gl",argc,argv);
   if (s) genland = true;
   s = argfind("-nl",argc,argv);
   if (s) showland = false;
   s = argfind("-p",argc,argv);
   if (s) _numplayers = CLAMP(atoi(s)-1, 1, 30);
   s = argfind("-sl",argc,argv);
   if (s) _seed = CLAMP(atoi(s), 0, 32000);

   allegro_init( );
   install_keyboard( );
   install_mouse( );
   install_timer( );
   LOCK_FUNCTION(frame_timer);
   LOCK_VARIABLE(frame_time);
   install_timer( );
   install_int_ex(frame_timer, MSEC_TO_TIMER(5));
   //install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL);
   set_gfx_mode( GFX_AUTODETECT, sx, sy, 0, 0 );
   buffer = create_bitmap( sx, sy );
   clear( buffer );
   text_mode( -1 );
   set_mouse_speed(0, 0);
   topbar = create_bitmap( MIN(sx,1024), 15 );
   clear( topbar );

   maindata = load_datafile("buggy.dat");
   gfxdata = load_datafile("gfxdata.dat");

   set_pallete(_mainpal);
   create_rgb_table(&rgb_table, _mainpal, NULL);
   rgb_map = &rgb_table;
   create_trans_table(&trans_table[0], _mainpal, 100, 100, 100, NULL);
   create_trans_table(&trans_table[1], _mainpal, 150, 150, 150, NULL);
   create_trans_table(&trans_table[2], _mainpal, 200, 200, 200, NULL);
   create_light_table(&light_table, _mainpal, 0, 0, 0, NULL);
   create_color_table(&flare_table, _mainpal, add_blender, NULL);
   color_map = &flare_table;

   int seed = _seed;
   if (!exists("moonmap.pcx") || genland) {
     printf("%c",7);
     textprintf(screen,_font(4),10,10,-1,"GENERATING LANDSCAPE...");
     genmap = load_map(seed);
     map = create_bitmap(sx, sy);
     draw_map(map, genmap);
     destroy_bitmap(genmap);
     save_bitmap("moonmap.pcx", map, _mainpal);
   } else {
     PALETTE pal;
     map = load_bitmap("moonmap.pcx", pal);
   }

   int frame_skip    = _frameskip;
   int frame_counter = 0;
   int frame_speed   = 0;

   numplayers = _numplayers;

   player[0] = new BUGGY(random()%sx, random()%sy, 0, AI_LOCAL, "Human");
   elist.add(player[0]);
   for (int i=1; i<=numplayers; i++) {
     player[i] = new BUGGY(random()%sx, random()%sy, i, AI_CPU, "CPU");
     elist.add(player[i]);
   }

   do {
      frame_counter++;
      if (!(frame_counter%frame_skip)) {
        frame_time = 0;
      }

      blit((BITMAP*)gfxdata[TOPBAR_PCX].dat, topbar, 0, 0, 0, 0, topbar->w, 15);

      elist.check_collisions();
      elist.move_all();
      if (!(frame_counter%frame_skip)) {
        if (showland)
          blit(map, buffer, 0, 0, 0, 0, sx, sy);
        else
          clear_to_color(buffer,5);
        elist.draw_all();
      }
      elist.remove_dead_nodes();

      //draw_sprite(buffer, _mainbitmap(3), 10, 10);
      textprintf(buffer,font,0,0,15,"fps %d", frame_speed);
      if (!(frame_counter%frame_skip)) {
        blit(topbar, buffer, 0, 0, 0, 0, topbar->w, 15);
        acquire_screen();
        blit(buffer, screen, 0, 0, 0, 0, sx, sy);
        release_screen();
      }

      frame_speed = 200 / frame_time;
      while (frame_time < 4 * frame_skip);
   } while (!key[KEY_ESC]);
}
END_OF_MAIN();

/* END */
