#include "pilot.h"
#include "pause.h"

/* Main thing with pausing is to allow things based on time to */
/* work properly when the toolkit opens a window. */

int paused = 0; /* Are we paused. */

/* From pilot.c */
extern Pilot** pilot_stack;
extern int pilot_nstack;
/* From space.c */
extern unsigned int spawn_timer;
/* From main.c */
extern unsigned int gtime;

static void pilot_nstack_pause(void);
static void pilot_nstack_unpause(void);
static void pilot_nstack_delay(unsigned int delay);

/* Pause the game. */
void pause_game(void) {
  if(paused) return; /* Well well.. We are paused already. */

  pilot_nstack_pause();
  spawn_timer -= SDL_GetTicks();

  paused = 1; /* We should unpause it. */
}

void unpause_game(void) {
  if(!paused) return; /* We are unpaused already. */

  pilot_nstack_unpause();
  spawn_timer += SDL_GetTicks();

  paused = 0;
}

/* Set the timers back. */
void pause_delay(unsigned int delay) {
  pilot_nstack_delay(delay);
}

/* Pilots pausing/unpausing. */
static void pilot_nstack_pause(void) {
  int i, j;
  unsigned int t = SDL_GetTicks();
  for(i = 0; i < pilot_nstack; i++) {
    pilot_stack[i]->ptimer -= t;

    pilot_stack[i]->tcontrol -= t;
    for(j = 0; j < MAX_AI_TIMERS; j++)
      pilot_stack[i]->timer[j] -= t;
  }
}

static void pilot_nstack_unpause(void) {
  int i, j;
  unsigned int t = SDL_GetTicks();
  for(i = 0; i < pilot_nstack; i++) {
    pilot_stack[i]->ptimer += t;

    pilot_stack[i]->tcontrol += t;
    for(j = 0; j < MAX_AI_TIMERS; j++)
      pilot_stack[i]->timer[j] += t;
  }
}

static void pilot_nstack_delay(unsigned int delay) {
  int i, j;
  for(i = 0; i < pilot_nstack; i++) {
    pilot_stack[i]->ptimer += delay;

    pilot_stack[i]->tcontrol += delay;
    for(j = 0; j < MAX_AI_TIMERS; j++)
      pilot_stack[i]->timer[j] += delay;
  }
}

