/* Player stuff for gradient wars

   By James Stanley

   No license - Use as you wish */

#include "player.h"

fixed px, py, pxs, pys, pangle, pthrust;
int pscore, pcurlevel = 0, pcuruplevel = 0, plives = -1, phealth = 40, bestscore, bestrocketscore, pweapon, pweptime;
int levels[MAXLEVEL] = { 80, 300, 800, 1500, 2000, 2750, 3750 };
int uplevels[MAXUPLEVEL] = { 100, 600, 1200, 2200, 3000 };

void player_control(void) {
  static int cooldown = 0;
  fixed angle;
  fixed xs, ys;
  
  if(key[KEY_LEFT] || joy[0].stick[0].axis[0].d1) pangle -= itofix(5);
  if(key[KEY_RIGHT] || joy[0].stick[0].axis[0].d2) pangle += itofix(5);
  
  if(key[KEY_UP] || joy[0].stick[0].axis[1].d1 || joy[0].button[1].b) {
    pthrust += 0x51f;//ftofix(0.02);
    new_thrust(px, py, pangle-itofix(128));
  } else if(key[KEY_DOWN] || joy[0].stick[0].axis[1].d2) {
    pthrust -= 0x28f;//ftofix(0.01);
    new_thrust(px, py, pangle);
  } else pthrust = fixmul(pthrust, 0x6666/*ftofix(0.4)*/);
  
  if(cooldown) cooldown--;
  
  if((key[KEY_LSHIFT] || joy[0].button[0].b) && (cooldown <= 0)) {
    play_sample(shoot, 255, 127, 1000, 0);
    switch(pweapon) {
      case STANDARD:
        cooldown = 12;
        xs = pxs + (fixcos(pangle) * 8);
        ys = pys + (fixsin(pangle) * 8);
        new_bullet(px, py, xs, ys, pangle);
        xs = pxs + (fixcos(pangle+itofix(8)) * 6);
        ys = pys + (fixsin(pangle+itofix(8)) * 6);
        new_bullet(px, py, xs, ys, pangle);
        xs = pxs + (fixcos(pangle-itofix(8)) * 6);
        ys = pys + (fixsin(pangle-itofix(8)) * 6);
        new_bullet(px, py, xs, ys, pangle);
        break;
      case MG:
        cooldown = 3;
        xs = pxs + (fixcos(pangle) * 10);
        ys = pys + (fixsin(pangle) * 10);
        new_bullet(px, py, xs, ys, pangle);
        xs = pxs + (fixcos(pangle+itofix(8)) * 8);
        ys = pys + (fixsin(pangle+itofix(8)) * 8);
        new_bullet(px, py, xs, ys, pangle);
        xs = pxs + (fixcos(pangle-itofix(8)) * 8);
        ys = pys + (fixsin(pangle-itofix(8)) * 8);
        new_bullet(px, py, xs, ys, pangle);
        break;
      case FIVE_MG:
        cooldown = 6;
        xs = pxs + (fixcos(pangle) * 8);
        ys = pys + (fixsin(pangle) * 8);
        new_bullet(px, py, xs, ys, pangle);
        xs = pxs + (fixcos(pangle+itofix(8)) * 6);
        ys = pys + (fixsin(pangle+itofix(8)) * 6);
        new_bullet(px, py, xs, ys, pangle);
        xs = pxs + (fixcos(pangle+itofix(16)) * 4);
        ys = pys + (fixsin(pangle+itofix(16)) * 4);
        new_bullet(px, py, xs, ys, pangle);
        xs = pxs + (fixcos(pangle-itofix(8)) * 6);
        ys = pys + (fixsin(pangle-itofix(8)) * 6);
        new_bullet(px, py, xs, ys, pangle);
        xs = pxs + (fixcos(pangle-itofix(16)) * 4);
        ys = pys + (fixsin(pangle-itofix(16)) * 4);
        new_bullet(px, py, xs, ys, pangle);
        break;
      case NUKE:
        cooldown = 20;
        kill_enemies(1);
        break;
      case SPIRAL:
        angle = pangle + itofix((pweptime % 25) * 21);
        xs = pxs + (fixcos(angle-itofix(8)) * 6);
        ys = pys + (fixsin(angle-itofix(8)) * 6);
        new_bullet(px, py, xs, ys, angle);
          if((pweptime%8) == 0) {//Occasionally shoot the standard weapon as well
          xs = pxs + (fixcos(pangle) * 8);
          ys = pys + (fixsin(pangle) * 8);
          new_bullet(px, py, xs, ys, pangle);
          xs = pxs + (fixcos(pangle+itofix(8)) * 6);
          ys = pys + (fixsin(pangle+itofix(8)) * 6);
          new_bullet(px, py, xs, ys, pangle);
          xs = pxs + (fixcos(pangle-itofix(8)) * 6);
          ys = pys + (fixsin(pangle-itofix(8)) * 6);
          new_bullet(px, py, xs, ys, pangle);
        }
        break;
    }
    
    //Now down-charge the weapon
    if(pweptime) {
      pweptime--;
      if(pweptime <= 0) {
        pweapon = STANDARD;
        display_text("STANDARD WEAPON", fixtoi(px), fixtoi(py));
      }
    }
  }
}

void player_update(void) {
  if(gamemode == NORMAL) {
    if(pcurlevel < MAXLEVEL) {
      if(pscore > levels[pcurlevel]) {
        pcurlevel++;
        play_sample(newenemy, 255, 127, 1000, 0);
        display_text("NEW ENEMY", fixtoi(px), fixtoi(py));
      }
    }
    if(pcuruplevel < MAXUPLEVEL) {
      if(pscore > uplevels[pcuruplevel]) {
        pcuruplevel++;
        play_sample(newenemy, 255, 127, 1000, 0);
        display_text("NEW POWERUP", fixtoi(px), fixtoi(py));
      }
    }
  }
  
  while(fixtoi(pangle) > 255) pangle -= itofix(255);
  while(fixtoi(pangle) < 0) pangle += itofix(255);
  
  if(pthrust > 0x199a/*ftofix(0.1)*/) pthrust = 0x199a;//ftofix(0.1);
  if(pthrust < -0xccd/*ftofix(-0.05)*/) pthrust = 0xfffff333;//ftofix(-0.05);
  
  pxs = fixmul(pxs, 0xfd71/*ftofix(0.99)*/);
  pys = fixmul(pys, 0xfd71/*ftofix(0.99)*/);
  
  pxs += fixmul(fixcos(pangle), pthrust);
  pys += fixmul(fixsin(pangle), pthrust);
  
  px += pxs;
  py += pys;
  
  if(fixtoi(px) > (WIDTH-48)) {
    pxs -= itofix(1);
  } else if(fixtoi(px) < 48) {
    pxs += itofix(1);
  }
  
  if(fixtoi(py) > (HEIGHT-48)) {
    pys -= itofix(1);
  } else if(fixtoi(py) < 48) {
    pys += itofix(1);
  }
}

void load_highscore(void) {
  FILE *file;
  char buff[5];
  int i;
  int thescore;
  
  //Normal score
  file = fopen("highscore", "r");
  if(file) {
    for(i = 0; i < 4; i++) buff[i] = fgetc(file);
    memcpy(&thescore, buff, 4);
    fclose(file);
  } else thescore = 0;
  bestscore = ntohl(thescore);
  
  //Rocket score
  file = fopen("rocketscore", "r");
  if(file) {
    for(i = 0; i < 4; i++) buff[i] = fgetc(file);
    memcpy(&thescore, buff, 4);
    fclose(file);
  } else thescore = 0;
  bestrocketscore = ntohl(thescore);
}

void save_highscore(void) {
#ifndef DEBUG_MODE
  FILE *file;
  int score;
  char buff[5];
  int i;

  if(gamemode == NORMAL) {
    file = fopen("highscore", "w");
  } else file = fopen("rocketscore", "w");
  
  if(!file) {
    //TODO: Work out how to say so
    return;
  }
  
  score = htonl((gamemode == NORMAL ? bestscore : bestrocketscore));
  
  memcpy(buff, &score, 4);
  buff[4] = '\0';
  
  for(i = 0; i < 4; i++) fputc(buff[i], file);
  
  fclose(file);
#ifdef GP2X
  sync();
#endif
#endif
}
