#pragma once
#include "misn_lua.h"

/* Availability by location. */
#define MIS_AVAIL_NONE        0
#define MIS_AVAIL_COMPUTER    1
#define MIS_AVAIL_BAR         2
#define MIS_AVAIL_OUTFIT      3
#define MIS_AVAIL_SHIPYARD    4
#define MIS_AVAIL_LAND        5

/* Flags. */
#define mis_isFlag(m,f)   ((m)->flags  & (f))
#define mis_setFlag(m,f)  ((m)->flags |= (f))
#define mis_rmFlag(m,f)   ((m)->flags ^= (f))

#define MISSION_UNIQUE        1 /* Unique missions can't be repeated. */

/* Static mission data. */
typedef struct MissionData_ {
  char* name; /* the name of the mission. */

  /* Availability. */
  struct {
    int loc;      /* Location. */
    int chance;   /* Chance of it appearing. */

    /* For specific cases. */
    char* planet;
    char* system;

    /* For generic cases. */
    int* factions;
    int nfactions;

    char* cond;   /* Conditional that must be met. */
    char* done;   /* Previous mission that must have been done. */
  } avail;

  unsigned int flags; /* Flags to store binary properties. */

  char* lua;
} MissionData;

/* Active mission. */
typedef struct Mission_ {
  MissionData* data;
  /* Unique mission identifier, used for keeping track of hooks. */
  unsigned int id;

  char* title;  /* Not to be confused with name.. */
  char* desc;   /* Description of the mission. */
  char* reward; /* Rewards - in text. */

  /* Mission cargo give to the player - Need to cleanup. */
  unsigned int* cargo;
  int ncargo;

  char* sys_marker; /* Marked system. */

  lua_State* L; /* The state of the running lua code. */
} Mission;

#define MISSION_MAX 6 /* No sense in the player having unlimited missions.. */
extern Mission player_missions[MISSION_MAX];

/* For mission computer. */
Mission* missions_computer(int* n, int faction, char* planet, char* sysname);
/* Player accepted mission - mission computer. */
int mission_accept(Mission* mission);
void missions_bar(int faction, char* planet, char* sysname);

/* Misc. */
int mission_getID(char* name);
MissionData* mission_get(int id);
void mission_sysMark(void);

/* Cargo stuff. */
void mission_linkCargo(Mission* misn, unsigned int cargo_id);
void mission_unlinkCargo(Mission* misn, unsigned int cargo_id);

/* Load/Quit. */
int missions_load(void);
void mission_cleanup(Mission* misn);
void missions_free(void);
void missions_cleanup(void);

