/**
 * @file pause.c
 *
 * @brief Handles pausing and resuming the game.
 *
 * Main trick to pausing/unpausing is to allow things based on time
 * to behave properly when the toolkit opens a window.
 *
 * @todo Should probably be eliminated by making everything use the dt system.
 */

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

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

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

static void pilot_pause(void);
static void pilot_unpause(void);
static void pilot_delay(unsigned int delay);

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

  pilot_pause();

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

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

  pilot_unpause();

  paused = 0;
}

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

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

    /* Pause timers. */
    pilot_stack[i]->tcontrol -= t;
    for(j = 0; j < MAX_AI_TIMERS; j++)
      pilot_stack[i]->timer[j] -= t;

    /* Pause outfits. */
    for(j = 0; j < pilot_stack[i]->noutfits; j++) {
      if(pilot_stack[i]->outfits[j].timer > 0)
        pilot_stack[i]->outfits[j].timer -= t;
    }
  }
}

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

    /* Rerun timers. */
    pilot_stack[i]->tcontrol += t;
    for(j = 0; j < MAX_AI_TIMERS; j++)
      pilot_stack[i]->timer[j] += t;

    /* Pause outfits. */
    for(j = 0; j < pilot_stack[i]->noutfits; j++) {
      if(pilot_stack[i]->outfits[j].timer > 0)
        pilot_stack[i]->outfits[j].timer += t;
    }
  }
}

static void pilot_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;

    for(j = 0; j < pilot_stack[i]->noutfits; j++) {
      if(pilot_stack[i]->outfits[j].timer > 0)
        pilot_stack[i]->outfits[j].timer += delay;
    }
  }
}

