#include <stdio.h>
#include "swis.h"
#include <kernel.h>
#include <time.h>
#include "Sound.h"

// SWI Registers
_kernel_swi_regs inreg;
_kernel_swi_regs outreg;

// Sprite buffer
unsigned char *buffer;

extern struct CompositionElement composition[128];
extern int current_element;

#define DISPLAY_MODE 28
#define DISPLAY_X 1280
#define DISPLAY_Y 960

#define PLAYER_Y_START 100
#define PLAYER_X_SPEED 10
#define MAX_NPCS 5
#define MAX_STARS 49
#define MAX_PROJECTILES 10

enum sprite_e{player_ship, durno_ship, durno_ship2, player_shipl,player_shipr,explode_shp1,explode_shp2,explode_shp3,explode_shp4,photon1,photon2};
char *sprites[] = {"player_ship","durno_ship","durno_ship2","player_shipl","player_shipr","explode_shp1","explode_shp2","explode_shp3","explode_shp4","photon1","photon2"};

char hudbuffer[63];

enum font_e{sys_12_8,font_max};

enum colour_e{lcars_black,lcars_violet1,debugpink,stargrey1,stargrey2,stargrey3,debuggreen};
int colours[] = {0x11111100,0xc4727200,0xcc00ff00,0x66666600,0x22222200,0x44444400,0x00ff0000};
int font[font_max];

enum debugs_e{dbbase,dbhitbox,dbweapons,dbinput,dbperformance,dbnpcs,dbmax};
int debugs[dbmax];

struct EntityLocation_s {
  short signed int X,Y;
};

enum npctype_e{bigdurno, littledurno,maxnpctype};

struct NPC_s {
  struct EntityLocation_s location;
  enum sprite_e sprite;
  enum npctype_e npctype;
  struct EntityLocation_s velocity;
  int health;
  struct EntityLocation_s hitbox_bl;
  struct EntityLocation_s hitbox_tr;
  int collideforce;
  int collidable;
  int explodenextframe;
};

struct Star_s {
  struct EntityLocation_s location;
  int colour;
  unsigned char length;
};

struct Player_s {
  struct EntityLocation_s location;
  enum sprite_e idlesprite;
  enum sprite_e sprite;
  unsigned char velocity;
  int nextidlesprite;
  int shields;
  int integrity;
  int remainingdistance;
  struct EntityLocation_s hitbox_bl;
  struct EntityLocation_s hitbox_tr;
  struct EntityLocation_s phaser1;
  struct EntityLocation_s phaser2;
};

struct Projectile_s {
  struct EntityLocation_s location;
  enum sprite_e sprite;
  struct EntityLocation_s velocity;
  int collidable;
  int nextframe;
  short int active;
  int damage;
};

struct Star_s Stars[MAX_STARS];
struct NPC_s NPCS[MAX_NPCS];
struct Projectile_s Projectiles[MAX_PROJECTILES];
struct Player_s Player;


int tick = 0;
int lasttick = 0;
extern int screen;
void intro()
{
  int currentstart = 0;

  sound_voices(4);

  sound_set_voice(1,"WaveSynth-Beep");
  sound_set_voice(2,"WaveSynth-Beep");
  sound_set_voice(3,"WaveSynth-Beep");
  sound_set_voice(4,"WaveSynth-Beep");
  sound_composition_init();
  
  // DUn, dun, daan duhhn
  sound_composition_element_add(currentstart,1,sound_note("A4#"),200);
  sound_composition_element_add(currentstart,2,sound_note("A5#"),200);
  currentstart += 200;
  sound_composition_element_add(currentstart,1,sound_note("F3"),200);
  sound_composition_element_add(currentstart,2,sound_note("F4"),200);
  currentstart += 200;
  sound_composition_element_add(currentstart,1,sound_note("G3#"),200);
  sound_composition_element_add(currentstart,2,sound_note("G4#"),200);
  currentstart += 200;
  sound_composition_element_add(currentstart,1,sound_note("C3"),200);
  sound_composition_element_add(currentstart,2,sound_note("C4"),200);
  currentstart += 200;

  // DUn, dun, daan duhhn
  sound_composition_element_add(currentstart,1,sound_note("A3#"),200);
  sound_composition_element_add(currentstart,2,sound_note("A4#"),200);
  currentstart += 200;
  sound_composition_element_add(currentstart,1,sound_note("F2"),200);
  currentstart += 200;
  sound_composition_element_add(currentstart,2,sound_note("G2#"),200);
  currentstart += 200;
  sound_composition_element_add(currentstart,1,sound_note("C2"),200);
  currentstart += 250;

  // Duh
  sound_composition_element_add(currentstart,3,sound_note("A3#"),300);
  sound_composition_element_add(currentstart,4,sound_note("A2#"),300);
  currentstart += 200;

  //REM Dun De Da
  sound_composition_element_add(currentstart,1,sound_note("F1"),20);
  currentstart += 50;
  sound_composition_element_add(currentstart,2,sound_note("A2#"),5);
  currentstart += 20;
  sound_composition_element_add(currentstart,1,sound_note("D2#"),200);
  currentstart += 180;


  sound_composition_element_add(currentstart,2,sound_note("D2"),50);
  currentstart += 50;
  sound_composition_element_add(currentstart,1,sound_note("A2#"),30);
  currentstart += 30;
  sound_composition_element_add(currentstart,2,sound_note("G1"),50);
  currentstart += 50;
  sound_composition_element_add(currentstart,1,sound_note("C2"),50);
  currentstart += 50;
  sound_composition_element_add(currentstart,3,sound_note("A2#"),100);
  sound_composition_element_add(currentstart,4,sound_note("F2"),100);
  currentstart += 100;
  
  draw_sprite("tng",320,400);
  sound_composition_start(clock());
  
  while(sound_composition_incomplete())
  {
    sound_composition_tick(clock());

    if(input_readkey(98))
      sound_composition_stop();
  }
}

void game_draw_player()
{
  draw_sprite(sprites[Player.sprite], Player.location.X, Player.location.Y);

  if(debugs[dbhitbox])
  {
    graphics_colour(colours[debugpink]);

    // Bounding box debug
    draw_rectangle(
      Player.location.X + Player.hitbox_bl.X,
      Player.location.Y + Player.hitbox_bl.Y,
      Player.location.X + Player.hitbox_tr.X,
      Player.location.Y + Player.hitbox_tr.Y
    );
  }

  if(debugs[dbweapons])
  {
    graphics_colour(colours[debuggreen]);

    // Draw phaser banks
    draw_line(
      Player.location.X + Player.phaser1.X,
      Player.location.Y + Player.phaser1.Y -5,
      Player.location.X + Player.phaser1.X,
      Player.location.Y + Player.phaser1.Y + 5
    );
    draw_line(
      Player.location.X + Player.phaser1.X -5,
      Player.location.Y + Player.phaser1.Y,
      Player.location.X + Player.phaser1.X +5,
      Player.location.Y + Player.phaser1.Y 
    );
    draw_line(
      Player.location.X + Player.phaser2.X,
      Player.location.Y + Player.phaser2.Y -5,
      Player.location.X + Player.phaser2.X,
      Player.location.Y + Player.phaser2.Y + 5
    );
    draw_line(
      Player.location.X + Player.phaser2.X -5,
      Player.location.Y + Player.phaser2.Y,
      Player.location.X + Player.phaser2.X +5,
      Player.location.Y + Player.phaser2.Y 
    );
  }
}

void game_draw_npcs()
{
  int i;
  for(i = 0; i < MAX_NPCS; i++)
  {
    draw_sprite(sprites[NPCS[i].sprite], NPCS[i].location.X, NPCS[i].location.Y);
    if(tick > NPCS[i].explodenextframe)
    {
      if((NPCS[i].sprite >= explode_shp1) && (NPCS[i].sprite <= explode_shp4))
      {
        NPCS[i].sprite++;
        if(NPCS[i].sprite > explode_shp4)
          NPCS[i].sprite = explode_shp1;
      }
      NPCS[i].explodenextframe = tick + 4;
    }
    
    if(debugs[dbhitbox])
    {
      graphics_colour(colours[debugpink]);

      // Bounding box debug
      draw_rectangle(
        NPCS[i].location.X + NPCS[i].hitbox_bl.X,
        NPCS[i].location.Y + NPCS[i].hitbox_bl.Y,
        NPCS[i].location.X + NPCS[i].hitbox_tr.X,
        NPCS[i].location.Y + NPCS[i].hitbox_tr.Y
      );
    }
  }
}

void game_draw_projectiles()
{
  int i;
  for(i = 0; i < MAX_PROJECTILES; i++)
  {
    if(Projectiles[i].active == 0)
      continue;

    draw_sprite(sprites[Projectiles[i].sprite], Projectiles[i].location.X,Projectiles[i].location.Y);
  }
}

void game_draw_stars()
{
  unsigned char i;
  
  for(i = 0; i < MAX_STARS; i++)
  {
    graphics_colour(Stars[i].colour);
    draw_line(Stars[i].location.X,Stars[i].location.Y,Stars[i].location.X,Stars[i].location.Y+Stars[i].length);
  }
}

void game_setup_player()
{
  Player.location.X = DISPLAY_X/2;
  Player.location.Y = PLAYER_Y_START;
  Player.sprite = player_ship;
  Player.idlesprite = player_ship;
  Player.velocity = 100;
  Player.shields = 100;
  Player.integrity = 100;
  Player.remainingdistance = 1500000;
  Player.hitbox_bl.X = 0;
  Player.hitbox_bl.Y = 0;
  Player.hitbox_tr.X = 60;
  Player.hitbox_tr.Y = 81;
  Player.phaser1.X = 20;
  Player.phaser1.Y = 75;
  Player.phaser2.X = 41;
  Player.phaser2.Y = 75;

}

void game_tick_stars()
{
  int i;
  for(i = 0; i < MAX_STARS; i++)
  {
    Stars[i].location.Y -= (tick - lasttick) * 4;
    if((Stars[i].location.Y + Stars[i].length) <= 0)
    {
      Stars[i].colour = colours[stargrey1 + (rand() % 2)];
      Stars[i].length = 20 + (rand() % 10);
      Stars[i].location.X = rand() % DISPLAY_X;
      Stars[i].location.Y = DISPLAY_Y + Stars[i].length;
    }
  }
}

void game_setup_stars()
{
  int i;
  for(i = 0; i < MAX_STARS; i++)
  {
    Stars[i].colour = colours[stargrey1 + (rand() % 2)];
    Stars[i].length = 20 + (rand() % 10);
    Stars[i].location.X = rand() % DISPLAY_X;
    Stars[i].location.Y = rand() % DISPLAY_Y;
  }
}

void game_setup_input()
{
  /*
    inreg.r[0] = 4;
  inreg.r[1] = 1;
  _kernel_swi(OS_Byte,&inreg,&outreg);
  */
}

void game_respawn_npc(int id)
{
  NPCS[id].location.X = rand() % DISPLAY_X;
  NPCS[id].location.Y = DISPLAY_Y + (rand() % (DISPLAY_Y/2));
  NPCS[id].npctype = rand() % (maxnpctype);
  NPCS[id].collidable = 1;
    switch(NPCS[id].npctype)
    {
      case bigdurno:
        NPCS[id].sprite = durno_ship;
        NPCS[id].velocity.X = 0;
        NPCS[id].velocity.Y = (rand() % 3) + 1;
        NPCS[id].health = 1000;
        NPCS[id].hitbox_bl.X = 0;
        NPCS[id].hitbox_bl.Y = 0;
        NPCS[id].hitbox_tr.X = 48;
        NPCS[id].hitbox_tr.Y = 74;
        NPCS[id].collideforce = 1000;
        break;
      case littledurno:
        NPCS[id].sprite = durno_ship2;
        NPCS[id].velocity.X = (rand() % 3) - 1;
        NPCS[id].velocity.Y = (rand() % 2) + 6;
        NPCS[id].health = 30;
        NPCS[id].hitbox_bl.X = 0;
        NPCS[id].hitbox_bl.Y = 0;
        NPCS[id].hitbox_tr.X = 38;
        NPCS[id].hitbox_tr.Y = 56;
        NPCS[id].collideforce = 30;
        break;
    };
}

void game_setup_npcs()
{
  int i;
  for(i = 0; i < MAX_NPCS; i++)
  {
    game_respawn_npc(i);
  }
}

void game_tick_npcs()
{
  int i;
  for(i = 0; i < MAX_NPCS; i++)
  {
    NPCS[i].location.Y -= (tick - lasttick) * NPCS[i].velocity.Y;
    NPCS[i].location.X -= (tick - lasttick) * NPCS[i].velocity.X;

    if(NPCS[i].location.Y + NPCS[i].hitbox_tr.Y <= 0)
      game_respawn_npc(i);
    
    if((NPCS[i].location.X + NPCS[i].hitbox_tr.X) > DISPLAY_X)
      game_respawn_npc(i);
    
    if((NPCS[i].location.X + NPCS[i].hitbox_bl.X) < 0)
      game_respawn_npc(i);
  }
}

void game_draw_debugmenu()
{
  int i;
  if(debugs[dbbase])
  {
    font_colour(colours[debuggreen],colours[lcars_black],font[sys_12_8]);
    draw_text("Debug List",DISPLAY_X-200,DISPLAY_Y-40,font[sys_12_8]);
    font_colour(colours[debugs[dbhitbox]?debugpink:stargrey1],colours[lcars_black],font[sys_12_8]);
    draw_text("1: hitbox",DISPLAY_X-200,DISPLAY_Y-60,font[sys_12_8]);
    font_colour(colours[debugs[dbweapons]?debugpink:stargrey1],colours[lcars_black],font[sys_12_8]);
    draw_text("2: weapons",DISPLAY_X-200,DISPLAY_Y-80,font[sys_12_8]);
    font_colour(colours[debugs[dbinput]?debugpink:stargrey1],colours[lcars_black],font[sys_12_8]);
    draw_text("3: input",DISPLAY_X-200,DISPLAY_Y-100,font[sys_12_8]);
    font_colour(colours[debugs[dbperformance]?debugpink:stargrey1],colours[lcars_black],font[sys_12_8]);
    draw_text("4: performance",DISPLAY_X-200,DISPLAY_Y-120,font[sys_12_8]);
    font_colour(colours[debugs[dbnpcs]?debugpink:stargrey1],colours[lcars_black],font[sys_12_8]);
    draw_text("5: NPCs",DISPLAY_X-200,DISPLAY_Y-140,font[sys_12_8]);
  }

  if(debugs[dbperformance])
  {
    font_colour(colours[debuggreen],colours[lcars_black],font[sys_12_8]);
    sprintf(hudbuffer,"Cents per frame: %i",(tick-lasttick));
    draw_text(hudbuffer,DISPLAY_X-500,DISPLAY_Y-60,font[sys_12_8]);
  }

  if(debugs[dbnpcs])
  {
    font_colour(colours[debuggreen],colours[lcars_black],font[sys_12_8]);
    for(i = 0; i < MAX_NPCS; i++)
    {
      sprintf(hudbuffer,"NPCS[%i] %i,%i %i,%i %i",i,NPCS[i].velocity.X,NPCS[i].velocity.Y,NPCS[i].location.X,NPCS[i].location.Y);
      draw_text(hudbuffer,DISPLAY_X-800,DISPLAY_Y-60-(i * 20),font[sys_12_8]);
    }
  }

  if(0)
  {
    font_colour(colours[debuggreen],colours[lcars_black],font[sys_12_8]);
    for(i = 0; i < MAX_PROJECTILES; i++)
    {
      sprintf(hudbuffer,"Projectiles[%i] %i,%i %i,%i %i",i,Projectiles[i].velocity.X,Projectiles[i].velocity.Y,Projectiles[i].location.X,Projectiles[i].location.Y,Projectiles[i].active);
      draw_text(hudbuffer,DISPLAY_X-800,DISPLAY_Y-60-(i * 20),font[sys_12_8]);
    }
  }
}

void game_collider_tick()
{
  int i;
  for(i = 0; i < MAX_NPCS; i++)
  {
    if(!NPCS[i].collidable)
      continue;
    // Excuse this formatting
    if(
      game_hitbox_collide(
        (Player.location.X + Player.hitbox_bl.X),(Player.location.Y + Player.hitbox_bl.Y),
        (Player.hitbox_tr.X - Player.hitbox_bl.X),(Player.hitbox_tr.Y - Player.hitbox_bl.Y),
        (NPCS[i].location.X + NPCS[i].hitbox_bl.X),(NPCS[i].location.Y + NPCS[i].hitbox_bl.Y),
        (NPCS[i].hitbox_tr.X - NPCS[i].hitbox_bl.X),(NPCS[i].hitbox_tr.Y - NPCS[i].hitbox_bl.Y)
      )
    )
    {

      if(debugs[dbhitbox])
      {
        font_colour(colours[debuggreen],colours[lcars_black],font[sys_12_8]);
        sprintf(hudbuffer,"NPCS[%i] hits player",i,NPCS[i].location.X,NPCS[i].location.Y);
        draw_text(hudbuffer,DISPLAY_X-900,DISPLAY_Y-260-(i * 20),font[sys_12_8]);
      }

      NPCS[i].health -= 300;
      NPCS[i].collidable = 0;
      NPCS[i].velocity.Y = NPCS[i].velocity.Y / 2;
      NPCS[i].velocity.X = NPCS[i].velocity.X * 4;

      Player.shields -= NPCS[i].collideforce;
      if(Player.shields < 0)
      {
        Player.integrity += Player.shields;
        Player.shields = 0;
      }
 
      if(NPCS[i].health <= 0)
      {
        NPCS[i].sprite = explode_shp1;
        NPCS[i].explodenextframe = tick + 4;
        sound_play(1,-5,0,100);
        sound_play(3,-15,0,1000);
        sound_play(2,-10,1,100);
      }
    }
  }
}

int game_hitbox_collide(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
{
    if((x1 + w1) >= x2)
      if(x1 <= (x2 + w2))
        if((y1 + h1) >= y2)
          if(y1 <= (y2 + h2))
            return 1;

  return 0;
}

void game_projectiles_tick()
{
  int i;
  for(i = 0; i < MAX_PROJECTILES; i++)
  {
    if(!Projectiles[i].active)
      continue;

    if(tick > Projectiles[i].nextframe)
    {
      if((Projectiles[i].sprite >= photon1) && (Projectiles[i].sprite <= photon2))
      {
        Projectiles[i].sprite++;
        if(Projectiles[i].sprite > photon2)
          Projectiles[i].sprite = photon1;
      }
      Projectiles[i].nextframe = tick + 10;
    }


    Projectiles[i].location.Y -= (tick - lasttick) * Projectiles[i].velocity.Y;
    Projectiles[i].location.X -= (tick - lasttick) * Projectiles[i].velocity.X;

    if((Projectiles[i].location.Y) <= 0)
    {
      Projectiles[i].active = 0;
    }
  }
}

void game_spawn_projectile(int id,int Px,int Py, int Vx,int Vy,enum sprite_e sprite, int damage)
{
  int i;
  
  if(id < 0)
  {
    for(i = 0; i < MAX_PROJECTILES; i++)
    {
        if(Projectiles[i].active == 0)
          id = i;
    }
  }

  // If no velocity X specified we're targetting the player
  if(Vx == 0)
  {
    int Xdistance = abs(Player.location.X - Px);
    int Ydistance = abs(Player.location.Y - Py);
    int distance = sqrt((Xdistance^2) + (Ydistance^2));
    Vx = Xdistance / (distance / 10);
    Vy = Ydistance / (distance / 10);
    if(Player.location.X > Px)
      Vx = 0 - Vx;
  }

  //If no free IDs then we go without
  if(id >= 0)
  {
    Projectiles[id].location.X = Px;
    Projectiles[id].location.Y = Py;
    Projectiles[id].velocity.X = Vx;
    Projectiles[id].velocity.Y = Vy;
    Projectiles[id].active = 1;
    Projectiles[id].sprite = sprite;
    Projectiles[id].nextframe = tick + 10;
    Projectiles[id].damage = damage;
    Projectiles[id].collidable = 1;
  }

}

void game_input_tick()
{
  if(debugs[dbbase])
  {
    // 1
    if(input_readkey(17))
      debugs[dbinput] = 1;
    // 2
    if(input_readkey(48))
      debugs[dbhitbox] = 1;
    // 3
    if(input_readkey(49))
      debugs[dbweapons] = 1;
    // 4
    if(input_readkey(18))
      debugs[dbperformance] = 1;
    // 5
    if(input_readkey(19))
      debugs[dbnpcs] = 1;
    // W
    if(input_readkey(33))
      game_spawn_projectile(-1,DISPLAY_X/2,DISPLAY_Y - 200,0,2,photon1,50);
  }
  
  if(debugs[dbinput])
  {
    font_colour(colours[debuggreen],colours[lcars_black],font[sys_12_8]);
    sprintf(hudbuffer,"Keycode: %i",input_readanykey());
    draw_text(hudbuffer,DISPLAY_X-500,DISPLAY_Y-40,font[sys_12_8]);
  }

  // Q
  if(input_readkey(16))
    debugs[dbbase] = 1;

  if(tick > Player.nextidlesprite)
    Player.sprite = Player.idlesprite;

  // Right arrow
  if(input_readkey(121))
  {
    Player.location.X += PLAYER_X_SPEED * (tick - lasttick);
    Player.sprite = player_shipr;
    Player.nextidlesprite = tick + 15;
    if((Player.location.X + Player.hitbox_tr.X) > DISPLAY_X)
    {
      Player.location.X = DISPLAY_X - Player.hitbox_tr.X;
    }
  }

  // Left arrow
  if(input_readkey(25))
  {
    Player.location.X -= PLAYER_X_SPEED * (tick - lasttick);
    Player.sprite = player_shipl;
    Player.nextidlesprite = tick + 15;
    if((Player.location.X + Player.hitbox_bl.X) < 0)
    {
      Player.location.X = 0 - Player.hitbox_bl.X;
    }
  }
}

void game_setup_audio()
{
  sound_voices(4);

  sound_set_voice(1,"WaveSynth-Beep");
  sound_set_voice(2,"Percussion-Noise");
  sound_set_voice(3,"Percussion-Soft");
  sound_set_voice(4,"Percussion-Noise");
}

void game_draw_hud()
{
  draw_sprite("lcars",4,DISPLAY_Y-180);

  font_colour(colours[lcars_black],colours[lcars_violet1],font[sys_12_8]);

  draw_text("Shields",75,DISPLAY_Y-62,font[sys_12_8]);
  draw_text("Integrity",75,DISPLAY_Y-92,font[sys_12_8]);
  draw_text("Velocity",75,DISPLAY_Y-122,font[sys_12_8]);
  draw_text("Distance",75,DISPLAY_Y-152,font[sys_12_8]);
  
  font_colour(colours[lcars_violet1],colours[lcars_black],font[sys_12_8]);

  sprintf(hudbuffer,"%i",Player.shields);
  draw_text(hudbuffer,230,DISPLAY_Y-62,font[sys_12_8]);
  
  sprintf(hudbuffer,"%i",Player.integrity);
  draw_text(hudbuffer,230,DISPLAY_Y-92,font[sys_12_8]);

  sprintf(hudbuffer,"%i",Player.velocity);
  draw_text(hudbuffer,230,DISPLAY_Y-122,font[sys_12_8]);

  sprintf(hudbuffer,"%i",Player.remainingdistance/1000);
  draw_text(hudbuffer,230,DISPLAY_Y-152,font[sys_12_8]);
}

void game_setup()
{
  game_setup_input();
  game_setup_audio();
  game_setup_stars();
  game_setup_player();
  game_setup_npcs();
  tick = clock();
}

void game_tick_player()
{
  Player.remainingdistance -= Player.velocity * (tick - lasttick);
}

void game_tick()
{
  lasttick = tick;
  tick = clock();

  screen_flipbuffer();
  screen_clear();

  game_tick_stars();
  game_input_tick();
  game_tick_player();
  game_tick_npcs();
  game_collider_tick();
  game_projectiles_tick();
  
  game_draw_stars();
  game_draw_player();
  game_draw_npcs();
  game_draw_projectiles();

  game_draw_hud();
  game_draw_debugmenu();
}

int main(int argc, char *argv[])
{
  sound_on();

  display_mode(DISPLAY_MODE);
  load_sprites("Spr");

  //intro();

  display_mode(DISPLAY_MODE);

  font[sys_12_8] = font_find("System.Medium",12,8);

  game_setup();
  
  while(Player.integrity > 0)
    game_tick();

  printf("you ded\n");

  free(buffer);

  return 0;
}
