/************************************************************************************
// Chicken Run Memory Card Stuff...
************************************************************************************/

#ifndef __CARD_H__
#define __CARD_H__

#ifndef _INVENTORY_H_
#include "inventory.h"
#endif
#ifndef __OPTIONS_H__
#include "options.h"
#endif
#ifndef _PUZZLES_H_
#include "puzzles.h"
#endif
#ifndef _FMA_H_
#include "fma.h"
#endif


// We always save out 4096 bytes (the isl-card lib internally stores two copies)
// This is made out of bff-ey segments

typedef struct CARD_HEADERSTR
{
	char	magic[2];					// Always 'SC'
	char	type;						// Icon type, always 0x11
	char	blockEntry;					// Number of slots (1)
	char 	title[64];					// Shift-JIS title
	char 	reserved[28];				// Reserved
	char	clut[32];					// Icon palette
	char	icons[3][128];				// Icon data (only first one)
}CARD_HEADERSTR;





#define CARD_SAVE_VERSION 0

#define INV_STATUS_NOTCOLLECTED	(0)
#define INV_STATUS_IN_INVENTORY	(1)
#define INV_STATUS_REMOVED		((char)(-1))



// Essentially 16 global variables
// * Tasks complete. Movable thing statuses. Actually, these should be global variables, accessible via egg.
//		= Snipping the wire fence in Act 1
//		= Taking the wood in Act 4's Old Barn (wood's an inventory item, but there's the drain-cover underneath it)
//		= cutting the clothes line (& hence existance of the nightdress collectable)
//		= FINISHED the Act4 toolshed tractor puzzle (coz it governs what room you go to)

// Tasks that needn't be marked as finished
//		Intermediate states of the Act 4 toolshed tractor (resets)
//		State of Act2 toolshed tractor (resets)
//		Positions of the pushable trolleys, haybales (they reset on entering the room)

// Added a2_breifing TD and a4
enum
{
	TASK_CUT_COOP1_FENCE,
	TASK_DUG_COOP2_FENCE,
	TASK_CUT_CLOTHES_LINE,
	TASK_REVEALED_SEWER,
	TASK_FINISHED_A4_TRACTOR,
	TASK_A1_BRIEF_FMA,
	TASK_A2_BRIEF_FMA,
	TASK_A4_BRIEF_FMA,

	TASK_FIREWORKS_AVAIL,
	TASK_CATAPULT_AVAIL,
	TASK_SEESAW_AVAIL,

	TASK_ENGINE_AVAIL,
	TASK_WINGMAKE_AVAIL,
	TASK_CONSTRUCT_AVAIL,
	
	MAX_TASKS = 16
};




// Which levels have saved stats... This is a bit rough & ready until we've got real data to work from
enum
{
	RESCUELEVEL_CHASE1,
	RESCUELEVEL_CATAPULT,
	RESCUELEVEL_FIREWORKS,
	RESCUELEVEL_SEESAW,
	N_RESCUE_LEVELS
};

enum
{
	TIME_ACT1,
	TIME_ACT2,
	TIME_ACT4,
	TIME_DOGCHASE,					
	TIME_COGCHASE,
	N_TIMED_LEVELS
};

enum
{
	MEDAL_WON_SEESAW,
	MEDAL_WON_FIREWORKS,
	MEDAL_WON_CATAPULT,
	MEDAL_WON_ENGINE,
	MEDAL_WON_CONSTRUCTION,
	MEDAL_WON_WINGS,
	MEDAL_WON_EGGLAYING,
	N_MEDAL_LEVELS
};


// What happens to the gameflags if you opt to REPLAY act1...? Ohboy. This is why Actionman have two gamesaves, methinks

// Act1 is available for re*start* as soon as you've begun it.
// It's only available for re*play* once you've got to the dogchase

// Act1 is classed as "beaten" as soon as you've started the dogchase.
// However, Act2 isn't classed as "notbeaten" until you HAVE finished the dogchase.

#define REPLAY_NOTAVAILABLE			0
#define REPLAY_NOTBEATEN			1
#define REPLAY_BEATEN				2

enum
{
	// Act 1
	REPLAY_AVAILABLE_ACT1,
	REPLAY_AVAILABLE_ACT1BOSS,

	// Act 2
	REPLAY_AVAILABLE_ACT2,
	REPLAY_AVAILABLE_SEESAW,
	REPLAY_AVAILABLE_FIREWORKS,
	REPLAY_AVAILABLE_CATAPULT,
	REPLAY_AVAILABLE_ACT2BOSS,

	// Act 3
	REPLAY_AVAILABLE_ACT3,
	REPLAY_AVAILABLE_EGGLAYING,
	REPLAY_AVAILABLE_ENGINE,
	REPLAY_AVAILABLE_BUILDING,
	REPLAY_AVAILABLE_WINGMAKING,
	REPLAY_AVAILABLE_ACT3BOSS,

	// Unused
//	REPLAY_AVAILABLE_ACT4,
//	REPLAY_AVAILABLE_ACT5BOSS,
//	REPLAY_AVAILABLE_CLAW,
//	REPLAY_AVAILABLE_GRAVYGUN,
//	REPLAY_AVAILABLE_PIEMAKER,


	N_REPLAY_LEVELS
};


typedef struct PLATFORM_SAVESTR
{
	int filledin;
	int	posx,posy,posz;
	int pushx,pushy,pushz;
	int enabled;
}PLATFORM_SAVESTR;

// Default best times

#if(PALMODE==YES)
	#define INIT_DOGCHASE_BEST	7500
	#define INIT_COGCHASE_BEST	7500
	#define INIT_ACT1_BEST (30*60*25)
	#define INIT_ACT2_BEST (90*60*25)
	#define INIT_ACT3_BEST (120*60*25)
#else
	#define INIT_DOGCHASE_BEST	9000
	#define INIT_COGCHASE_BEST	9000
	#define INIT_ACT1_BEST (30*60*30)
	#define INIT_ACT2_BEST (90*60*30)
	#define INIT_ACT3_BEST (120*60*30)
#endif


// There are two gameplay's in use
// One is your "save position" - The furthest you've got in the game
// The other is your "replay" position (which ain't saved to card)

enum
{
	SUBGAME_FIREWORKS,
	SUBGAME_CATAPULT,
	SUBGAME_SEESAW,
	SUBGAME_ENGINE,
	SUBGAME_WINGS,
	SUBGAME_CONSTRUCT,
	N_SUBGAME_STATUSES
};
typedef struct GamePlayDataType
{
	int sprouts_carried;
	int eggs_carried;

	char SubGameStates[N_SUBGAME_STATUSES];
	char tasks_done[MAX_TASKS];
	char a2ts_state;
	char a4ts_state;
	char map_pieces_got[14];

// Note - (see inventory.doc) - We can actually get away with just the one inventory
// This includes the object lise & also the currently equipped item (inventory->selected_item)
	inventoryType inventories[NUM_CHARACTER_INVS];

	short pickups_collected[LEVNUMBER_NUM_LEVELS];	// 16 bits per level for use as (sprout) flags

	PLATFORM_SAVESTR a2er_plat[4];
	PLATFORM_SAVESTR a4er_plat[4];
	PLATFORM_SAVESTR a2ob_plat[4];
	PLATFORM_SAVESTR a4ki_plat[1];

// The objects in the game need to maintain a status
// a)	Not collected yet. Ie, they're at their initial location
//		(Which may include not existing). 0.
// b)	Sat in someone's inventory. 1,2,3,4 for ginger,rocky,nick,fetcher
// c)	Used, delivered, and so dead.	-1

	char inventory_item_status[TOTAL_ITEMS];

	FMAPLAYEDSTATUS	A1fmaStatus[MAX_ACT1_FMAS];
	FMAPLAYEDSTATUS	A2fmaStatus[MAX_ACT2_FMAS];
	FMAPLAYEDSTATUS	A4fmaStatus[MAX_ACT4_FMAS];

	int time_act_started;	// copied from gamesavedata

}GamePlayDataType;

typedef struct GameSaveDataType
{
//   Pickups collected, of however many flavours there are
//   (do we individually mark them at one-bit-each, or do we do counter-per-room, and reset 'em?)
//		Map pieces - multiple, spread across the game
//		Film reels. One per act.

	char movies_available[NUM_MOVIES];	// this includes (any) non-selectable intro movie, bizzarrely
	
	char stills_available[10];			// Stills that you can pick up as you wander about

// These are levels available for replay.
// Note - this does NOT reset when you ask for a new game
	char levels_available[N_REPLAY_LEVELS];


	/* What kind of medal you've won for each subgame - 0 = failed or not played
														1 = Bronze medal won
														2 = Silver medal won
														3 = Gold medal won*/
	char medals_won[N_MEDAL_LEVELS];

	int best_chickens_rescued[N_RESCUE_LEVELS];
	int best_times[N_TIMED_LEVELS];			
	int time_spent_playing;

	char beenRatsYet;
	char pad[3];

// Being removed, real soon now, & placed in the two "play" structures
/*
	int	sprouts_carried;
	int tasks_done[MAX_TASKS];
// Note - (see inventory.doc) - We can actually get away with just the one inventory
// This includes the object lise & also the currently equipped item (inventory->selected_item)
	inventoryType inventories[NUM_CHARACTER_INVS];

	short pickups_collected[LEVNUMBER_NUM_LEVELS];	// 16 bits per level for use as (sprout) flags

	PLATFORM_SAVESTR a2er_plat[4];
	PLATFORM_SAVESTR a4er_plat[4];
	PLATFORM_SAVESTR a2ob_plat[3];
	PLATFORM_SAVESTR a4ki_plat[1];
	int	a2ts_state;
	int a4ts_state;

	char inventory_item_status[TOTAL_ITEMS];
*/
}GameSaveDataType;


// The permanant options are kept in a single lump of data in "options.c/h", so that's okay.
extern GameSaveDataType gameSaveData;

// This can either point to the one inside "gamesavedatatype" or to an alternative "replaying act" one
extern GamePlayDataType *gamePlayData;


// Action man keep a second copy to revert to when you die (ummm, "are captured". Sorry)
//extern CARD_GAMESTATE_STR gameSaveLast;

typedef struct CARD_SAVE_STR
{
	CARD_HEADERSTR header;

	int		version;					// If the version or size doesn't match, then it's an "older version"
	int		size;						// of the save data, and is **discarded**

	OPTIONDATASTR options;		// permanant options
	GameSaveDataType state;		// permanant things you've unlocked
	GamePlayDataType play;		// current state of play

}CARD_SAVE_STR;


// Half of a block, less a bit (at least 4 bytes for crc)
// ie Don't let this EVER go over 4092,

// We're using this as the actual size of our save block, hence it's somewhat less
// but keep it lower for now, coz then we'll load/save quicker
//#define MAX_SAVE_SIZE	2000
#define MAX_SAVE_SIZE	4000


void cardPostLangStart();
void cardFowlerLoadStart();
void cardFowlerSaveStart();
void cardSplashSaveStart();

void cardInitialGameState();
void cardInitialPrefs(OPTIONDATASTR *data);



#endif
