/******************************************************************************************
	CRTEST PS   (c) 1999-2001 ISL

	inventory.c:			Inventory stuff
******************************************************************************************/

//===================================================================
// 
// Create an inventory for the character (eg. inventoryType gingerInv)
// then set currentInv = &<name of inv>, so that that inventory is used whenever
// inventory functions are called.
//
// To swap items between characters:- 
//
// This example removes the Boots from Ginger's inventory and puts it into Rocky's:-
//
// inventorySwapItem(BOOTS, &gamePlayData->inventories[INVNO_GINGER], &gamePlayData->inventories[INVNO_ROCKY]);
//                   (item)  (source)   (destination)
// The inventorySwapItem() function is the only one that doesn't require 
// currentInv to be defined
//===================================================================

#include "global.h"
#include "main.h"
#include "menu.h"
#include "gametext.h"
#include "map_draw.h"
#include "overlays.h"
#include "maths.h"
#include "card.h"
#include "inventory.h"
#include "scenics.h"
#include "sptstream.h"
#include "demo.h"
#include "enemies.h"
#include "charactr.h"
#include "map_play.h"
#include "puzzles.h"
#include "fma.h"
#include "charactr.h"
#include "sprouts.h"
#include "loadsnd.h"
#include "sound.h"

#undef NULL
#define NULL 0

VOID_SUBROUTINE old_draw_routine = NULL;
VOID_SUBROUTINE old_cycle_routine = NULL;

#ifdef _WINDOWS
extern int scrOffsetX, scrOffsetY;
#endif

char inventoryPlansAvailable[5];
char *inventoryPlanNames[5];			// And the description that's printed on the paper

enum {		// states for the inventory screen
		SLOT_CYCLE,		// cycling through inventory objects
		PLAN_VIEW,		// looking at one of the plans full-screen
		MAP_VIEW,		// Looking at the map full-screen
		INV_EXIT		// quitting
};

//SfxSampleType *inv_bleep;
//SfxSampleType *inv_click;

// Things that need to be kept in the same order...
// inventory_itemsize[]
// inventory_tmap_names[]
// The item name-texts in gametext.xls
int inv_messagetimer = 0;			// A timer for printing 'Too heavy / Not enough room' etc messages while trying to pick things up 
char *inv_currentmessage = NULL;

unsigned char inv_startwithmap;
unsigned char inv_exitstraightaway;

void inventoryPlanViewCycle(void);

// How many slots each item takes up
// NB keep in same order as other things
unsigned char inventory_itemsize[]={		//0-24
	0,					// Empty slot
	1,					// Butter knife
	1,					// Rusty shears
	1,					// Wire cutters

	2,					// a1 Poles		
	1,					// Gloves
	2,					// Boots
	1,					// Nightdress

	1,					// Spoon
	2,					// Cooking weights
	1,					// Matches
	2,					// Fireworks
	1,					// Tennis Racket
	1,					// Pram Chassis
	1,					// Strong cord
	4,					// Broken ladder
	1,					// Bone

	1,					// Egg
	1,					// Gears
	1,					// Tools
	1,					// Wood
	1,					// Oil can
	4,					// Engine
	1,					// Chain
	2,					// a4 poles
	1,					// Yarn
	1,					// Binoculars
	1,					// Laundry
	2,					// Gnome

	7};					// Invalid - size 7 so you'll never be able to pick it up

// Texture-map names (and obe names) of the inventory objects...
// NB keep in same order as other things
char *inventory_tmap_names[] =
{
	// Act 1
	"EMPTY",	// so we could have an "empty" texture
	"KNIFE",
	"SHEARS",
	"CUTTERS",

	// Act 1 Pt 2
	"A1POLES",
	"GLOVES",
	"BOOTS",
	"DRESS",

	// Act 2
	"SPOON",
	"WEIGHTS",
	"MATCHES",
	"FIREWORKS",
	"RACKET",
	"PRAM",
	"CORD",
	"LADDER",
	"BONE",

	// Act 4
	"EGG",
	"GEARS",
	"TOOLS",
	"WOOD",
	"OILCAN",
	"ENGINE",
	"CHAIN",
	"A4POLES",
	"YARN",
	"BINOCULARS",
	"LAUNDRY",
	"GNOME",

	"MAP1",
	"MAP2",
	"MAP3",
	"MAP4",
	"MAP5",
	"MAP6",
	"MAP7",
	"MAP8",
	"MAP9",
	"MAP10",
	"MAP11",
	"MAP12",
	"MAP13",
	"MAP14",

	"SPROUTS",// Multiple instances, which are dealt with by the scenic handler

	"PIC1",
	"PIC2",
	"PIC3",
	"PIC4",
	"PIC5",
	"PIC6",
	"PIC7",
	"PIC8",
	"PIC9",
	"PIC10",
	"PIC11"
};

#define MAX_INV_SLOTS 6;

// Sprite Streaming Technology...
// We have enough Vram for EITHER the INVENTORY, one PLAN, or the MAP. Not two.

#define MAX_STREAM_SPRITES 100

// These need to match the "STREAMBANK_xxxx" enum in inventory.h
char *stream_filenames[] =
{
	NULL,
	"INVENTORY\\INVENT1.SPT",				// Act 1 objects
	"INVENTORY\\INVENT2.SPT",				// Act 2 objects
	"INVENTORY\\INVENT4.SPT",				// Act 4 objects
	"INVENTORY\\MAP.SPT",
	"INVENTORY\\A1_PLAN1.SPT",
	"INVENTORY\\A1_PLAN2.SPT",
	"INVENTORY\\A1_PLAN3.SPT",
	"INVENTORY\\A2_PLAN1.SPT",
	"INVENTORY\\A2_PLAN2.SPT",
	"INVENTORY\\A2_PLAN3.SPT",
	"INVENTORY\\A2_PLAN4.SPT",
	"INVENTORY\\A4_PLAN1.SPT",
	"INVENTORY\\A4_PLAN2.SPT",
	"INVENTORY\\A4_PLAN3.SPT",
	"INVENTORY\\A4_PLAN4.SPT"
};

int	streamed_bankno = STREAMBANK_NONE;
char last_item_collected = EMPTY;
TextureBankType *streamed_bank = NULL;

#ifdef _WINDOWS
TextureBankType *invBank[14];
int invBanksLoaded=NULL;
#endif

TextureType *inv_plan_texture = NULL;

TextureType *objectpics[TOTAL_ITEMS];				
TextureType *planpic = NULL;					
TextureType *cloth = NULL;
TextureType *arrow = NULL;

// Pointers for all the backdrop textures, which are big pictures

// RFW TextureBankType *bg_textures = NULL;
TextureType *bg_plan = NULL;
TextureType *bg_mapicon = NULL;
TextureType *bg_nail = NULL;
TextureType *bg_wood = NULL;
TextureType *bg_wood2 = NULL;
TextureType *bg_wood3 = NULL;
TextureType *tick = NULL;
TextureType *halftick = NULL;

TextureType *map_textures[14];
VECTOR map_positions[14];

struct {
	unsigned short state;
	short selected_plan;
	unsigned short numplans;
} inventory_globals;


//------------------------------------------------------------------------

// The inventories are now an array of four inventories, in the "gameSaveData" structure
// (see card.h)

//inventoryType gingerInv;
//inventoryType rockyInv;
//inventoryType nickInv;
//inventoryType fetcherInv;


// Changes when you switch between characters
inventoryType *currentInv = NULL;	//&gamePlayData->inventories[INVNO_GINGER]; // RFW - I'm initialising it at loadtime, no worries






//----------------------------------------------------------------------------------
//	Function 	: int inventoryEnsureStreamBank(int n)
//	Purpose 	: Load SPT directly into VRAM, bypassing main RAM
//	Parameters 	: None
//	Returns 	: None
//  Info		: Load a sprite file into Vram without needing an intermediate RAM buffer.
// 				  Necessary, because we're calling here with the level file loaded
//----------------------------------------------------------------------------------

int inventoryEnsureStreamBank(int n)
{
 	streamed_bankno = n;
  	streamed_bank = textureLoadBank(stream_filenames[n]);

	return 1;
}






//----------------------------------------------------------------------------------
//	Function 	: void inventoryLoadPlan(void)
//	Purpose 	: Stream the plan from CD
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryLoadPlan(int planNum)
{
	inventoryEnsureStreamBank(planNum);						// which plan (or map) gets loaded depends on what level you're on and which one you've got selected
	inv_plan_texture = textureFindInAllBanks("BIGPLAN");	// I think that all plans are called big plan
}




//----------------------------------------------------------------------------------
//	Function 	: void inventoryLoadInventory(void)
//	Purpose 	: Load the relevant object images for this level
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryLoadInventory(void)
{
	int i;

	// BP: I've had to split the inventory textures up into 3 banks, 
	// one for each act, so we dont have to have em all loaded at once

	if(levinfo->level_number<=LEVNUMBER_A1_DOGCHASE)
		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT1);
	else if(levinfo->level_number<=LEVNUMBER_A2_COGCHASE)
		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT2);
	else if(levinfo->level_number<=LEVNUMBER_A4_FLYMACHINE)
		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT4);

	// Find all the inventory textures for this level
	if(levinfo->level_number<=LEVNUMBER_A1_DOGCHASE)
	{
		for(i = 0; i < NIGHTDRESS+1; i++)
		{
			utilPrintf("looking for  %s\n",inventory_tmap_names[i]);
			objectpics[i]=textureFindInBank(streamed_bank,inventory_tmap_names[i]);
			if(!objectpics[i])
				utilPrintf("inventory texture %s not found\n",inventory_tmap_names[i]);
		}
	}

	else if(levinfo->level_number<=LEVNUMBER_A2_COGCHASE)
	{
		for(i = SPOON; i < BONE+1; i++)
		{
			objectpics[i]=textureFindInBank(streamed_bank,inventory_tmap_names[i]);
			ASSERT(objectpics[i]);
		}
	}

	else if(levinfo->level_number<=LEVNUMBER_A4_FLYMACHINE)
	{
		for(i = EGG; i < GNOME+1; i++)
		{
			objectpics[i]=textureFindInBank(streamed_bank,inventory_tmap_names[i]);
			ASSERT(objectpics[i]);
		}
	}
}





//----------------------------------------------------------------------------------
//	Function 	: void inventoryDrawBG(void)
//	Purpose 	: Draw the inventory background
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryDrawBG(void)
{
	int i, j;

	overlaySprite(planpic, 101, -7, 999, 0x808080);

	fontPrintCentred(font,147,85,TEXTSTR(TEXTSTR_INVENTORY_PLANS),80,80,128);

	overlaySprite(bg_wood, -256, -128, 1000, 0x505050);
	overlaySprite(bg_wood, -128, -128, 1000, 0x505050);
	overlaySprite(bg_wood2, 0, -128, 1000, 0x505050);
	overlaySprite(bg_wood, 128, -128, 1000, 0x505050);

	overlaySprite(bg_wood, -256, -64, 1000, 0x505050);
	overlaySprite(bg_wood, -128, -64, 1000, 0x505050);
	overlaySprite(bg_wood2, 0, -64, 1000, 0x505050);
	overlaySprite(bg_wood, 128, -64, 1000, 0x505050);

	overlaySprite(bg_wood, -256, 0, 1000, 0x505050);
	overlaySprite(bg_wood, -128, 0, 1000, 0x505050);
	overlaySprite(bg_wood2, 0, 0, 1000, 0x505050);
	overlaySprite(bg_wood, 128, 0, 1000, 0x505050);

	overlaySprite(bg_wood3,-256, 64, 1000, 0x505050);
	overlaySprite(bg_wood3, -128, 64, 1000, 0x505050);
	overlaySprite(bg_wood2, 0, 64, 1000, 0x505050);
	overlaySprite(bg_wood, 128, 64, 1000, 0x505050);



	/*for(i=-256;i<768;i+=128)
	{
		overlaySprite(bg_wood, i, -128, 1000, 0x505050);
		overlaySprite(bg_wood, i, -64, 1000, 0x505050);
		overlaySprite(bg_wood, i , 0, 1000, 0x505050);
		overlaySprite(bg_wood, i , 64, 1000, 0x505050);
	}*/

	fontPrintCentred(font, 153, -34, TEXTSTR(TEXTSTR_INVENTORY_VIEW_MAP), 80,80,128);

	overlaySprite(cloth, -254, 3, 999, 0x808080);

	fontPrintCentred(font, -131, 88, TEXTSTR(TEXTSTR_INVENTORY_TRITOQUIT), 80,80,128);
	
	overlaySprite(bg_nail, 143, -114, 998, 0x606060);

	overlaySprite(bg_mapicon, 80, -110, 999, 0x808080);
}




//----------------------------------------------------------------------------------
//	Function 	: void inventoryDrawBG(void)
//	Purpose 	: Draw the inventory background
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryDrawBGOnly(void)
{
	int i;
	
	for(i=-256;i<768;i+=128)
	{
		overlaySprite(bg_wood, i, -128, 1023, 0x505050);
		overlaySprite(bg_wood, i, -64, 1023, 0x505050);
		overlaySprite(bg_wood, i , 0, 1023, 0x505050);
		overlaySprite(bg_wood, i , 64, 1023, 0x505050);
	}
}




//----------------------------------------------------------------------------------
//	Function 	: void inventoryDrawItemSelectName(void)
//	Purpose 	: Print a description of the currently selected object / plan, and a description
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryDrawItemSelectName(void)
{
	int item;
	item = currentInv->slot_contents[currentInv->selected_slot];
	fontPrintCentred(font, -128, 12, TEXTSTR(item + TEXTSTR_INVENTORY_ITEM_NOTHING), 80,80,128);
	
	if(currentInv->slot_contents[currentInv->selected_slot]!=EMPTY)
	{
		if(currentInv->selected_item!= item)
			fontPrintCentred(font, -128, 42, TEXTSTR(TEXTSTR_INVENTORY_XTOEQUIP), 80,80,128);
		else
			fontPrintCentred(font, -128, 42, TEXTSTR(TEXTSTR_INVENTORY_XTOUNEQUIP), 80,80,128);
	}
}






//----------------------------------------------------------------------------------
//	Function 	: void inventoryDrawPlan(void)
//	Purpose 	: Draw the currently selected plan
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryDrawPlan(void)
{	
	inventoryDrawTicksOnPlans();
	if(inventoryCheckForPlanCompletion(inventoryPlansAvailable[inventory_globals.selected_plan]))
	{
		overlaySprite(inv_plan_texture,-256,-112, 0, 0x404040);
	}
	else
		overlaySprite(inv_plan_texture,-256,-112, 0, 0x808080);


#if(PALMODE==YES)
	fontPrintCentred(font, 0 , 100, TEXTSTR(TEXTSTR_INVENTORY_TRITOQUIT), 80,80,128);
	fontPrint(font,-225,100,TEXTSTR(TEXTSTR_INVENTORY_PREVIOUS),80,80,128);
	fontPrint(font,163,100,TEXTSTR(TEXTSTR_INVENTORY_NEXT),80,80,128);

	overlaySprite(arrow,227,100, 0, 0x808080);
	overlaySpriteFlipped(arrow,-250,100, 0, 0x808080);
#else
	fontPrintCentred(font, 0 , 96, TEXTSTR(TEXTSTR_INVENTORY_TRITOQUIT), 80,80,128);
	fontPrint(font,-225,96,TEXTSTR(TEXTSTR_INVENTORY_PREVIOUS),80,80,128);
	fontPrint(font,163,96,TEXTSTR(TEXTSTR_INVENTORY_NEXT),80,80,128);
	overlaySprite(arrow,227,95, 0, 0x808080);
	overlaySpriteFlipped(arrow,-250,95, 0, 0x808080);

#endif

}

//----------------------------------------------------------------------------------
//	Function 	: void fmaDrawPlan(void)
//	Purpose 	: very similar to the old routine but a bit different Draw the currently selected plan
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------
void fmaDrawPlan(void)
{	
#if PALMODE==1
	overlaySprite(inv_plan_texture,-256,-128, 0, 0x808080);
#else
	//overlaySpriteScaled(inv_plan_texture,-256,-120, 0, 0x808080, 256, 230);
	overlaySpriteScaled(inv_plan_texture,-256,-135, 0, 0x808080, 256, 230);
#endif

}




//----------------------------------------------------------------------------------
//	Function 	: void inventoryDrawMap(void)
//	Purpose 	: Draw the map
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryDrawMap(void)
{	
	int i;

	// Make sure we've got map piece 8, as it doesnt always seem to work if we just stick it in startup.c, although the addition of a printf
	// seems to make it work, for whatever strange reason?
	gamePlayData->map_pieces_got[8] = 1;

	// Draw the highlighted bits of plan if you happen to be in that bit of the game
	if(levinfo->level_number==LEVNUMBER_A2_COGCHASE)
		if(gamePlayData->map_pieces_got[3]==1)
			overlaySprite(map_textures[3],map_positions[3].vx,map_positions[3].vy, 0, 0x404080);

	if((levinfo->level_number==LEVNUMBER_A4_COOP4_N) || (levinfo->level_number==LEVNUMBER_A4_WORKHUT4) || (levinfo->level_number==LEVNUMBER_A4_CONSTRUCT) || (levinfo->level_number==LEVNUMBER_A4_SCENIC5) || (levinfo->level_number==LEVNUMBER_A4_SCENIC6))
		if(gamePlayData->map_pieces_got[4]==1)
			overlaySprite(map_textures[4],map_positions[4].vx,map_positions[4].vy, 0, 0x404080);

	if((levinfo->level_number==LEVNUMBER_A4_COOP3_N) || (levinfo->level_number==LEVNUMBER_A4_WORKHUT3) || (levinfo->level_number==LEVNUMBER_A4_WINGSHUT) || (levinfo->level_number==LEVNUMBER_A4_SCENIC3) || (levinfo->level_number==LEVNUMBER_A4_SCENIC4) || (levinfo->level_number==LEVNUMBER_A2_COOP3_D) || (levinfo->level_number==LEVNUMBER_A2_WORKHUT3) || (levinfo->level_number==LEVNUMBER_A2_WINGSHUT) || (levinfo->level_number==LEVNUMBER_A2_SCENIC3) || (levinfo->level_number==LEVNUMBER_A2_SCENIC4))
		if(gamePlayData->map_pieces_got[5]==1)
			overlaySprite(map_textures[5],map_positions[5].vx,map_positions[5].vy, 0, 0x404080);

	if((levinfo->level_number==LEVNUMBER_A2_BARNEXT_D) || (levinfo->level_number==LEVNUMBER_A2_OLDBARN_D) || (levinfo->level_number==LEVNUMBER_A4_BARNEXT_N) || (levinfo->level_number==LEVNUMBER_A4_OLDBARN_N))
		if(gamePlayData->map_pieces_got[6]==1)
			overlaySprite(map_textures[6],map_positions[6].vx,map_positions[6].vy, 0, 0x404080);

	if((levinfo->level_number==LEVNUMBER_A4_EGGROOM) || (levinfo->level_number==LEVNUMBER_A4_PIEBARN) ||(levinfo->level_number==LEVNUMBER_A2_EGGROOM))
		if(gamePlayData->map_pieces_got[7]==1)
			overlaySprite(map_textures[7],map_positions[7].vx,map_positions[7].vy, 0, 0x404080);

	// Winner of the prize for Longest embedded 'if' statement in the world, ever, weighing in at 720 characters, 15 arguments, goes to....
	if((levinfo->level_number==LEVNUMBER_A4_HUT17) || (levinfo->level_number==LEVNUMBER_A4_FOWLER) || (levinfo->level_number==LEVNUMBER_A4_WORKHUT1) || (levinfo->level_number==LEVNUMBER_A4_EGGHUT) || (levinfo->level_number==LEVNUMBER_A4_COOP1_N) || (levinfo->level_number==LEVNUMBER_A1_HUT17) || (levinfo->level_number==LEVNUMBER_A1_FOWLER) || (levinfo->level_number==LEVNUMBER_A1_WORKHUT1) || (levinfo->level_number==LEVNUMBER_A1_EGGHUT) || (levinfo->level_number==LEVNUMBER_A1_COOP1_N) || (levinfo->level_number==LEVNUMBER_A2_HUT17) || (levinfo->level_number==LEVNUMBER_A2_FOWLER) || (levinfo->level_number==LEVNUMBER_A2_WORKHUT1) || (levinfo->level_number==LEVNUMBER_A2_EGGHUT) ||(levinfo->level_number==LEVNUMBER_A2_COOP1_D))
		if(gamePlayData->map_pieces_got[8]==1)
			overlaySprite(map_textures[8],map_positions[8].vx,map_positions[8].vy, 0, 0x404080);

	if((levinfo->level_number==LEVNUMBER_A4_COOP2_N) || (levinfo->level_number==LEVNUMBER_A4_WORKHUT2) || (levinfo->level_number==LEVNUMBER_A4_ENGINEHUT) || (levinfo->level_number==LEVNUMBER_A4_SCENIC1) || (levinfo->level_number==LEVNUMBER_A4_SCENIC2) || (levinfo->level_number==LEVNUMBER_A2_WORKHUT2) || (levinfo->level_number==LEVNUMBER_A2_ENGINEHUT) || (levinfo->level_number==LEVNUMBER_A2_SCENIC1) || (levinfo->level_number==LEVNUMBER_A2_COOP2_D) || (levinfo->level_number==LEVNUMBER_A2_SCENIC2))
		if(gamePlayData->map_pieces_got[9]==1)
			overlaySprite(map_textures[9],map_positions[9].vx,map_positions[9].vy, 0, 0x404080);

	if((levinfo->level_number==LEVNUMBER_A2_HOUSEXT_D) || (levinfo->level_number==LEVNUMBER_A1_HOUSEXT_N) || (levinfo->level_number==LEVNUMBER_A4_HOUSEXT_N))
		if(gamePlayData->map_pieces_got[10]==1)
			overlaySprite(map_textures[10],map_positions[10].vx,map_positions[10].vy, 0, 0x404080);

	if((levinfo->level_number==LEVNUMBER_A4_TUNNEL) || (levinfo->level_number==LEVNUMBER_A4_TOOLEXT_N) || (levinfo->level_number==LEVNUMBER_A2_TOOLEXT_D))
		if(gamePlayData->map_pieces_got[11]==1)
			overlaySprite(map_textures[11],map_positions[11].vx,map_positions[11].vy, 0, 0x404080);

	if((levinfo->level_number==LEVNUMBER_A4_GARDEN_N) || (levinfo->level_number==LEVNUMBER_A1_GARDEN_N) || (levinfo->level_number==LEVNUMBER_A4_KITCHEN) || (levinfo->level_number==LEVNUMBER_A4_LOUNGE) || (levinfo->level_number==LEVNUMBER_A2_GARDEN_D))
		if(gamePlayData->map_pieces_got[12]==1)
			overlaySprite(map_textures[12],map_positions[12].vx,map_positions[12].vy, 0, 0x404080);
	
	if((levinfo->level_number==LEVNUMBER_A4_TOOLSHED_N) || (levinfo->level_number==LEVNUMBER_A4_TOOLSHED_A) || (levinfo->level_number==LEVNUMBER_A2_TOOLSHED_D))
		if(gamePlayData->map_pieces_got[13]==1)
			overlaySprite(map_textures[13],map_positions[13].vx,map_positions[13].vy, 0, 0x404080);

	for(i=0;i<14;i++)
	{
		if(gamePlayData->map_pieces_got[i]==1)
		{
			overlaySprite(map_textures[i],map_positions[i].vx,map_positions[i].vy, 0, 0x808080);
		}
	}
#if(PALMODE==YES)
	fontPrintCentred(font, 0 , 100, TEXTSTR(TEXTSTR_INVENTORY_TRITOQUIT), 80,80,128);
#else
	fontPrintCentred(font, 0 , 95, TEXTSTR(TEXTSTR_INVENTORY_TRITOQUIT), 80,80,128);
#endif

	overlaySprite(map_textures[5],map_positions[i].vx,map_positions[i].vy, 0, 0x808080);
}





//----------------------------------------------------------------------------------
//	Function 	: void inventoryDrawInventory(void)
//	Purpose 	: Draw the inventory screen
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryDrawInventory()
{
	int current_item;

	if(gamePlayData->eggs_carried==6)
	{	
		overlaySprite(bg_nail, -222, -107, 800, 0x808080);
		overlaySprite(bg_nail, -222, -51, 800, 0x808080);
		overlaySprite(bg_nail, -140, -107, 800, 0x808080);
		overlaySprite(bg_nail, -140, -51, 800, 0x808080);
		overlaySprite(bg_nail, -58, -107, 800, 0x808080);
		overlaySprite(bg_nail, -58, -51, 800, 0x808080);

		overlaySprite(objectpics[EGG], -242, -104, 800, 0x808080);
		overlaySprite(objectpics[EGG], -242, -48, 800, 0x808080);
		overlaySprite(objectpics[EGG], -160, -104, 800, 0x808080);
		overlaySprite(objectpics[EGG], -160, -48, 800, 0x808080);
		overlaySprite(objectpics[EGG], -78, -104, 800, 0x808080);
		overlaySprite(objectpics[EGG], -78, -48, 800, 0x808080);
		fontPrintCentred(font, -128, 12, TEXTSTR(TEXTSTR_INVENTORY_ITEM_NOTHING + EGG), 80,80,128);
	}
	else
	{
		if(inventory_globals.state==SLOT_CYCLE)		
		// draw the sprite for each block
			inventoryDrawItemSelectName();

		if(currentInv->slot_contents[0]!=EMPTY)
		{
			if((currentInv->selected_slot==0) && (inventory_globals.state==SLOT_CYCLE))
			{
				if(inventory_itemsize[currentInv->slot_contents[0]]==4)
					overlaySprite(bg_nail, -187, -107, 800, 0x808080);
				else
					overlaySprite(bg_nail, -222, -109, 800, 0x808080);	

				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[0]], -242, -104, 800, inventory_itemsize[currentInv->slot_contents[0]] , (currentInv->selected_item==currentInv->slot_contents[0]),0x808080);
			}
			else 
			{
				if(inventory_itemsize[currentInv->slot_contents[0]]==4)
					overlaySprite(bg_nail, -187, -107, 800, 0x404040);
				else
					overlaySprite(bg_nail, -222, -109, 800, 0x404040);		

				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[0]], -242, -104, 800, inventory_itemsize[currentInv->slot_contents[0]] , (currentInv->selected_item==currentInv->slot_contents[0]),0x404040);
			}
		}
		else
			overlaySprite(bg_nail, -222, -109, 800, 0x404040);	


		// only draw block 1 if its a size 1 object
		if((currentInv->slot_contents[1]!=EMPTY)&&(inventory_itemsize[currentInv->slot_contents[1]]==1))
		{
			if((currentInv->selected_slot==1) && (inventory_globals.state==SLOT_CYCLE))
			{
				overlaySprite(bg_nail, -222, -53, 800, 0x808080);
				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[1]], -242, -48, 800, inventory_itemsize[currentInv->slot_contents[1]] , (currentInv->selected_item==currentInv->slot_contents[1]), 0x808080);
			}
			else 
			{
				overlaySprite(bg_nail, -222 , -53, 800, 0x404040);
				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[1]], -242, -48, 800, inventory_itemsize[currentInv->slot_contents[1]] , (currentInv->selected_item==currentInv->slot_contents[1]), 0x404040);
			}	
		}
		else if(inventory_itemsize[currentInv->slot_contents[0]]<2)
			overlaySprite(bg_nail, -222, -53, 800, 0x404040);		

		// only draw block 2 if its a size 1 or 2 object
		if((currentInv->slot_contents[2]!=EMPTY)&&(inventory_itemsize[currentInv->slot_contents[2]]!=4))
		{
			if((currentInv->selected_slot==2) && (inventory_globals.state==SLOT_CYCLE))
			{
				overlaySprite(bg_nail, -140, -109, 800, 0x808080);
				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[2]], -160, -104, 800, inventory_itemsize[currentInv->slot_contents[2]] , (currentInv->selected_item==currentInv->slot_contents[2]), 0x808080);
			}
			else
			{
				overlaySprite(bg_nail, -140, -109, 800, 0x404040);
				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[2]], -160, -104, 800, inventory_itemsize[currentInv->slot_contents[2]] , (currentInv->selected_item==currentInv->slot_contents[2]), 0x404040);
			}
		}
		else if(inventory_itemsize[currentInv->slot_contents[0]]!=4)
			overlaySprite(bg_nail, -140, -109, 800, 0x404040);	

		// only draw block 3 if its a size 1 object
		if((currentInv->slot_contents[3]!=EMPTY)&&(inventory_itemsize[currentInv->slot_contents[3]]==1))
		{
			if((currentInv->selected_slot==3) && (inventory_globals.state==SLOT_CYCLE))
			{
				overlaySprite(bg_nail, -140, -53, 800, 0x808080);
				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[3]], -160, -48, 800, inventory_itemsize[currentInv->slot_contents[3]] , (currentInv->selected_item==currentInv->slot_contents[3]), 0x808080);
			}
			else
			{
				overlaySprite(bg_nail, -140, -53, 800, 0x404040);
				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[3]], -160, -48, 800, inventory_itemsize[currentInv->slot_contents[3]] , (currentInv->selected_item==currentInv->slot_contents[3]), 0x404040);
			}
		}
		else if(inventory_itemsize[currentInv->slot_contents[0]]!=4)
			overlaySprite(bg_nail, -140, -53, 800, 0x404040);		

		if(currentInv->slot_contents[4]!=EMPTY)
		{
			if((currentInv->selected_slot==4) && (inventory_globals.state==SLOT_CYCLE))
			{
				overlaySprite(bg_nail, -58, -109, 800,0x808080);
				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[4]], -78, -104, 800, inventory_itemsize[currentInv->slot_contents[4]] , (currentInv->selected_item==currentInv->slot_contents[4]), 0x808080);
			}
			else
			{
				overlaySprite(bg_nail, -58, -109, 800,0x404040);
				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[4]], -78, -104, 800, inventory_itemsize[currentInv->slot_contents[4]] , (currentInv->selected_item==currentInv->slot_contents[4]), 0x404040);
			}
		}
		else
			overlaySprite(bg_nail, -58 , -109, 800,0x404040);		

		// only draw block 5 if its a size 1 object
		if((currentInv->slot_contents[5]!=EMPTY)&&(inventory_itemsize[currentInv->slot_contents[5]]==1))
		{	
			if((currentInv->selected_slot==5) && (inventory_globals.state==SLOT_CYCLE))
			{	
				overlaySprite(bg_nail, -58 , -53, 800,0x808080);
				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[5]], -78, -48, 800, inventory_itemsize[currentInv->slot_contents[5]] , (currentInv->selected_item==currentInv->slot_contents[5]), 0x808080);
			}
			else
			{
				overlaySprite(bg_nail, -58 , -53, 800,0x404040);
				inventoryShowStretchedImage(objectpics[currentInv->slot_contents[5]], -78, -48, 800, inventory_itemsize[currentInv->slot_contents[5]] , (currentInv->selected_item==currentInv->slot_contents[5]), 0x404040);
			}
		}
		else
			overlaySprite(bg_nail, -58 , -53, 800,0x404040);
	}	
}





//----------------------------------------------------------------------------------
//	Function 	: void inventoryDraw(void)
//	Purpose 	: Draw the inventory / plan, whichever's selected
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryDraw(void)
{
	//debugPrintf("%d  ",inventory_globals.state);
	if(inventory_globals.state==MAP_VIEW)
	{
		inventoryDrawBGOnly();
		inventoryDrawMap();
	}
	
	else if(inventory_globals.state==PLAN_VIEW) 
	{	
		inventoryDrawBGOnly();
		inventoryDrawPlan();
	}

	else
	{	
		inventoryDrawInventory();
		inventoryDrawBG();
	}
}





//----------------------------------------------------------------------------------
//	Function 	: void inventoryEnd(void)
//	Purpose 	: Sort the inventory so that larger objects are always placed first
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryEnd(void)
{
// The inventory sprites stay loaded all the time, except
// when we're looking at plans/maps

// RFW 	textureUnloadBank(bg_textures);

	if(old_draw_routine == menuDraw)
	{
		menuStart();	// this is only really here for test purposes
	}
	else
	{
		draw_routine = old_draw_routine;
		cycle_routine = old_cycle_routine;
	}

	dcCleanTexStream();//will need to make sure this cleans up properly
//	inventoryLoadInventory();			// just make sure all our pointers are valid to stop those dodgy things from drawing
}



#ifdef DEBUG
void inventoryCheat(int dirn)
{
	int i;
	int item;
	int start = EMPTY;

	for(i=0;i<6;i++)
	{
		item = currentInv->slot_contents[i];

		if(item != EMPTY)
		{
			if(start == EMPTY)
				start = item;

			inventoryRemoveItem(item);

			i = -1;
		}
	}
	start+=dirn;
	if(start >= GNOME+1)		// Gnome is the final item in the inventory
		start = EMPTY+1;
	if(start <= EMPTY)
		start = GNOME;
	inventoryPickUpItem(start);
}
#endif





//----------------------------------------------------------------------------------
//	Function 	: void inventorySlotCycle(void)
//	Purpose 	: Cycle through the inventory items with the digital pad
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventorySlotCycle()
{
	int oldslot;

	if((padData.debounce[0] & PAD_RIGHT)&&(currentInv->selected_slot<4))
	{
		oldslot = currentInv->selected_slot;

		if(inventory_itemsize[currentInv->slot_contents[currentInv->selected_slot]]>2)
			currentInv->selected_slot+=inventory_itemsize[currentInv->slot_contents[currentInv->selected_slot]];
		else
			currentInv->selected_slot+=2;

		if(!(currentInv->slot_contents[currentInv->selected_slot]))
			currentInv->selected_slot = oldslot;
	}

	if(((padData.debounce[0] & PAD_LEFT) ||(pReadPad->analogXS[0]<-2048))&&(currentInv->selected_slot>1))
	{
		oldslot = currentInv->selected_slot;

		currentInv->selected_slot-=2;
		
		if(!(currentInv->slot_contents[currentInv->selected_slot]))
			currentInv->selected_slot = oldslot;
	}

	if((padData.debounce[0] & PAD_DOWN)||(pReadPad->analogYS[0]>2048))
	{
		oldslot = currentInv->selected_slot;

		if((inventory_itemsize[currentInv->slot_contents[currentInv->selected_slot]]==1) && !(currentInv->selected_slot%2))
			currentInv->selected_slot++;

		if(!(currentInv->slot_contents[currentInv->selected_slot]))
		{
			currentInv->selected_slot = oldslot;
		}
	}

	if((padData.debounce[0] & PAD_UP)||(pReadPad->analogYS[0]<-2048))
	{
		oldslot = currentInv->selected_slot;

		if(inventory_itemsize[currentInv->slot_contents[currentInv->selected_slot]]==1) 
		{
			if((currentInv->selected_slot == 1) || (currentInv->selected_slot == 3) || (currentInv->selected_slot == 5))
				currentInv->selected_slot--;
		}

		if(!(currentInv->slot_contents[currentInv->selected_slot]))
			currentInv->selected_slot = oldslot;
	}
	// make sure we haven't gone off the edge, as that would be bad
	if(currentInv->selected_slot>5) currentInv->selected_slot=5;

	// make sure we've always got the first block of a multi-block item
	switch(currentInv->selected_slot)
	{
		case 1:		// if slot 1 is selected and its a 2 or 4-block item, change 
					// the current slot to 0;
			if((inventory_itemsize[currentInv->slot_contents[currentInv->selected_slot]]==2)||(inventory_itemsize[currentInv->slot_contents[currentInv->selected_slot]]==4))
				currentInv->selected_slot=0;
			break;
		case 2:		// if slot 2 is selected and its a 4-block item, change it to slot 0;
			if(inventory_itemsize[currentInv->slot_contents[currentInv->selected_slot]]==4)	
				currentInv->selected_slot=0;
			break;
		case 3:		// if slot 3 is selected and its a 4-block item, change it to slot 0
					// or if its a 2-block item, change it to slot 2
			if(inventory_itemsize[currentInv->slot_contents[currentInv->selected_slot]]==4)	
				currentInv->selected_slot=0;
			else if(inventory_itemsize[currentInv->slot_contents[currentInv->selected_slot]]==2)	
				currentInv->selected_slot=2;
			break;
		case 5:		// if slot 5 is selected and its a 2-block item, change it to slot 4
			if(inventory_itemsize[currentInv->slot_contents[currentInv->selected_slot]]==2)	
				currentInv->selected_slot=4;
			break;
	}
}





//----------------------------------------------------------------------------------
//	Function 	: void inventoryCycle(void)
//	Purpose 	: The main inventory cycle routine
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryCycle(void)
{
	unsigned long subtractive;
	unsigned long additive;
	
#ifdef DEBUG
	if(padData.debounce[4] & PAD_LEFT)
		inventoryCheat(-1);
	if(padData.debounce[4] & PAD_RIGHT)
		inventoryCheat(1);
	if(pReadPad->debounce[4] & PAD_L2)
		inventoryDoTestData();
#endif

	if(inventory_globals.state==SLOT_CYCLE)
	{
		if(pReadPad->debounce[0] & PAD_CIRCLE)
		{
			inventory_globals.state = MAP_VIEW;
			inventoryLoadMap();
		}
	
		else if(padData.debounce[0] & PAD_SQUARE)
		{	
			inventoryLoadPlan(inventoryPlansAvailable[0]);
			inventory_globals.state=PLAN_VIEW;
		}
	}

	switch(inventory_globals.state)
	{
		case SLOT_CYCLE:
			
			inventorySlotCycle();
			
			if(padData.debounce[0] & PAD_CROSS)			// Equip with selected item and quit
			{	
				if(currentInv->selected_item==currentInv->slot_contents[currentInv->selected_slot])
					currentInv->selected_item = EMPTY;
				else
					currentInv->selected_item=currentInv->slot_contents[currentInv->selected_slot];
			}
			
			else if(padData.debounce[0] & (PAD_START | PAD_TRIANGLE))	// Quit back to game without equipping an item
			{
				inventoryEnd();
			}

			break;
		case PLAN_VIEW:
			inventoryPlanViewCycle();
			if(padData.debounce[0] & (PAD_CROSS | PAD_START | PAD_TRIANGLE))
			{
				inventoryLoadInventory();		
				inventory_globals.state = SLOT_CYCLE;
			}
			break;
		case MAP_VIEW:
			if(pReadPad->debounce[0] & PAD_TRIANGLE)
			{	
				if(inv_startwithmap!=0)
				{	
					inv_startwithmap = 0;
					inventoryEnd();
				}
			
				inventoryLoadInventory();
				inventory_globals.state = SLOT_CYCLE;
				currentInv->selected_slot = 0;
			}
	}
}



//----------------------------------------------------------------------------------
//	Function 	: void inventoryStart(void)
//	Purpose 	: Start the inventory
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryStart(void)
{
	int i;

#ifdef _WINDOWS
	// JS : We can load ALL of the textures that the inventory uses, since we have oodles and
	// oodles of RAM.
	if (!invBanksLoaded)
	{
		for(i=1; i < 14; i++)
		{
			invBank[i] = textureLoadBank(stream_filenames[i]);
			textureDownloadBank(invBank[i]);
		}
		invBanksLoaded=1;
	}
#endif

	inventoryPrintContentsToConsole();
	debugPrintf("Selected item: %d\n",currentInv->selected_item);
	
	old_draw_routine = draw_routine;
	old_cycle_routine = cycle_routine;

	map_positions[0].vx = 0;
	map_positions[0].vy = 0;

	map_positions[1].vx = 0;
	map_positions[1].vy = 103;

	map_positions[2].vx = 1;
	map_positions[2].vy = 145;

	map_positions[3].vx = 112;
	map_positions[3].vy = 0;

	map_positions[4].vx = 108;
	map_positions[4].vy = 57;

	map_positions[5].vx = 106;
	map_positions[5].vy = 106;

	map_positions[6].vx = 107;
	map_positions[6].vy = 145;

	map_positions[7].vx = 180;
	map_positions[7].vy = 0;

	map_positions[8].vx = 197;
	map_positions[8].vy = 57;

	map_positions[9].vx = 194;
	map_positions[9].vy = 106;

	map_positions[10].vx = 260;
	map_positions[10].vy = 0;

	map_positions[11].vx = 247;
	map_positions[11].vy = 111;

	map_positions[12].vx = 333;
	map_positions[12].vy = 0;

	map_positions[13].vx = 335;
	map_positions[13].vy = 73;

	for(i=0;i<14;i++)
	{
		map_positions[i].vx-=256;
		map_positions[i].vy-=112;
	}
	//-------------------------------------------------
	// Background textures
	//-------------------------------------------------

/*
// We don't want a load here - I've merged "back" into the A1/A2/A4 inventory collection
// (Also frees up some vram, coz back.spt was still loaded when the map/plan was being shown
#ifdef _WINDOWS
	bg_textures = textureLoadBank("INVENTORY\\BACK.SPT");
	textureDownloadBank(bg_textures);						
	textureDestroyBank(bg_textures);						
	ASSERT(bg_textures);
#else
	bg_textures = streamLoadSpt("INVENTORY\\BACK.SPT", MAX_STREAM_SPRITES);
#endif
*/


	inventoryLoadInventory();					// Load all the images etc in
		
	draw_routine = inventoryDraw;
	cycle_routine = inventoryCycle;
	inventory_globals.state=SLOT_CYCLE;

	// See how many plans we've got, and which ones
	inventory_globals.selected_plan = 0;

	if(levinfo->level_number<=LEVNUMBER_A1_DOGCHASE)		// Act 1 plans
	{
		if(gamePlayData->inventory_item_status[BUTTER_KNIFE]==INV_STATUS_REMOVED	&& gamePlayData->inventory_item_status[RUSTY_SHEARS]==INV_STATUS_REMOVED)
		{
			inventory_globals.numplans = 3;
	
			inventoryPlansAvailable[0] = STREAMBANK_A1_PLAN1;
			inventoryPlanNames[0] = TEXTSTR(TEXTSTR_INVENTORY_A1PLAN1);
		
			inventoryPlansAvailable[1] = STREAMBANK_A1_PLAN2;
			inventoryPlanNames[1] = TEXTSTR(TEXTSTR_INVENTORY_A1PLAN2);
		
			inventoryPlansAvailable[2] = STREAMBANK_A1_PLAN3;
			inventoryPlanNames[2] = TEXTSTR(TEXTSTR_INVENTORY_A1PLAN3);

			inventoryPlansAvailable[3] = NULL;	
			inventoryPlanNames[3] = NULL;
		}
		else
		{
			inventory_globals.numplans = 2;
		
			inventoryPlansAvailable[0] = STREAMBANK_A1_PLAN1;
			inventoryPlanNames[0] = TEXTSTR(TEXTSTR_INVENTORY_A1PLAN1);
		
			inventoryPlansAvailable[1] = STREAMBANK_A1_PLAN2;
			inventoryPlanNames[1] = TEXTSTR(TEXTSTR_INVENTORY_A1PLAN2);
		
			inventoryPlansAvailable[2] = NULL;
			inventoryPlanNames[2] = NULL;

			inventoryPlansAvailable[3] = NULL;	
			inventoryPlanNames[3] = NULL;
		}
	}
	
	else if(levinfo->level_number<=LEVNUMBER_A2_COGCHASE)	// Act 2 plans
	{
		inventory_globals.numplans = 4;
		
		inventoryPlansAvailable[0] = STREAMBANK_A2_PLAN1;
		inventoryPlanNames[0] = TEXTSTR(TEXTSTR_INVENTORY_A2PLAN1);
		
		inventoryPlansAvailable[1] = STREAMBANK_A2_PLAN2;
		inventoryPlanNames[1] = TEXTSTR(TEXTSTR_INVENTORY_A2PLAN2);
		
		inventoryPlansAvailable[2] = STREAMBANK_A2_PLAN3;
		inventoryPlanNames[2] = TEXTSTR(TEXTSTR_INVENTORY_A2PLAN3);

		inventoryPlansAvailable[3] = STREAMBANK_A2_PLAN4;
		inventoryPlanNames[3] = NULL;
	}

	else if(levinfo->level_number<=LEVNUMBER_A4_FLYMACHINE)	// Act 3 (4?) plans
	{
		inventory_globals.numplans = 4;

		inventoryPlansAvailable[0] = STREAMBANK_A4_PLAN1;
		inventoryPlanNames[0] = TEXTSTR(TEXTSTR_INVENTORY_A4PLAN1);

		inventoryPlansAvailable[1] = STREAMBANK_A4_PLAN2;
		inventoryPlanNames[1] = TEXTSTR(TEXTSTR_INVENTORY_A4PLAN2);

		inventoryPlansAvailable[2] = STREAMBANK_A4_PLAN3;
		inventoryPlanNames[2] = TEXTSTR(TEXTSTR_INVENTORY_A4PLAN3);

		inventoryPlansAvailable[3] = STREAMBANK_A4_PLAN4;
		inventoryPlanNames[3] = TEXTSTR(TEXTSTR_INVENTORY_A4PLAN4);
	}

	// initialise the positions of the plans

	if(last_item_collected)
	{
		currentInv->selected_slot = inventoryCheckForItem(last_item_collected);
		last_item_collected = 0;
	}

	else
		currentInv->selected_slot=0;


	//-------------------------------------------------
	if(inv_startwithmap == 1)
	{
		debugPrintf("Starting on map\n");
		inventoryLoadMap();
		inventory_globals.state=MAP_VIEW;
	}
}





//----------------------------------------------------------------------------------
//	Function 	: void inventorySort(void)
//	Purpose 	: Sort the inventory so that larger objects are always placed first
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventorySort(void)
{
	int i;
	int temp[6];
	int position;
	
	for(i=0;i<6;i++)
		temp[i]=EMPTY;

	position=0;

	for(i=0;i<6;i++)
	{
		if(inventory_itemsize[currentInv->slot_contents[i]]==4)
		{
			temp[position]=currentInv->slot_contents[i];
			currentInv->slot_contents[i]=0;
			position++;
		}
	}

	for(i=0;i<6;i++)
	{
		if(inventory_itemsize[currentInv->slot_contents[i]]==2)
		{
			temp[position]=currentInv->slot_contents[i];
			currentInv->slot_contents[i]=0;
			position++;
		}
	}

	for(i=0;i<6;i++)
	{
		if(inventory_itemsize[currentInv->slot_contents[i]]==1)
		{
			temp[position]=currentInv->slot_contents[i];
			currentInv->slot_contents[i]=0;
			position++;
		}
	}

	for(i=0;i<6;i++)
		currentInv->slot_contents[i]=temp[i];
}






//----------------------------------------------------------------------------------
//	Function 	: void inventoryPrintContentsToConsole(void)
//	Purpose 	: Dump the contents of the inventory to the PC screen for debugging purposes
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryPrintContentsToConsole(void)
{
	int i;
	int item;
	utilPrintf("Inventory Contents: ");
	for(i=0;i<6;i++)
	{
		item = currentInv->slot_contents[i];
		utilPrintf("  %d : %s , ",i,TEXTSTR(item + TEXTSTR_INVENTORY_ITEM_NOTHING));
	}
}




//----------------------------------------------------------------------------------
//	Function 	: void inventoryReset(unsigned short item)
//	Purpose 	: check for a specified item in the inventory, and return the first slot number in which it is held
//	Parameters 	: unsigned short item = the item to look for
//	Returns 	: The slot number, or INV_INVALIDOBJECT if not held
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryReset(void)
{
	int i;

	currentInv->empty_slots = 6;
	currentInv->selected_slot = 0;
	currentInv->selected_item = EMPTY;
	
	for(i=0;i<6;i++)
		currentInv->slot_contents[i] = EMPTY;
}




//----------------------------------------------------------------------------------
//	Function 	: void inventoryCheckForItem(unsigned short item)
//	Purpose 	: check for a specified item in the inventory, and return the first slot number in which it is held
//	Parameters 	: unsigned short item = the item to look for
//	Returns 	: The slot number, or INV_INVALIDOBJECT if not held
//	Info 		: 
//----------------------------------------------------------------------------------

unsigned short inventoryCheckForItem(unsigned short item)
{
	int i;
	for(i=0;i<6;i++)
	{
		if(currentInv->slot_contents[i]==item)
			return i;
	}
	return INV_INVALIDOBJECT;
}





//----------------------------------------------------------------------------------
//	Function 	: unsigned short inventorySlotStatus(unsigned short slot_number)
//	Purpose 	: Return the status of a specific inventory slot
//	Parameters 	: slot_number = the slot you want to query
//	Returns 	: The contents of that slot, or INV_INVALIDOBJECT if not a valid slot number
//	Info 		:
//----------------------------------------------------------------------------------

unsigned short inventorySlotStatus(unsigned short slot_number)
{
	if(slot_number>5)
		return INV_INVALIDOBJECT;
	else
		return(currentInv->slot_contents[slot_number]);
}






//----------------------------------------------------------------------------------
//	Function 	: unsigned short inventorySwapItem(unsigned short item, inventoryType *sourceInv, inventoryType *destInv)
//	Purpose 	: Remove an item from one inventory and add it to another
//	Parameters 	: item = object to swap, sourceInv = source inventory, destInv = destination inventory
//	Returns 	: INV_OK if successful, otherwise an errorcode as defined in inventory.h
//	Info 		:
//----------------------------------------------------------------------------------

unsigned short inventorySwapItem(unsigned short item, inventoryType *sourceInv, inventoryType *destInv)
{
	inventoryType *oldInv;
	unsigned short result;

	oldInv = currentInv;

	currentInv = sourceInv;
	result = inventoryCheckForItem(item);
	if(result!=INV_INVALIDOBJECT)
	{	
		inventoryRemoveItem(item);
		currentInv = destInv;
		inventoryPickUpItem(item);
		currentInv = sourceInv;
		inventorySort();
	}

	currentInv = oldInv;
	return INV_OK;
}




//----------------------------------------------------------------------------------
//	Function 	: unsigned short inventoryRemoveItem(unsigned short item)
//	Purpose 	: Remove an item from the inventory
//	Parameters 	: item = the item you want to remove
//	Returns 	: INV_OK if successful, otherwise an errorcode as defined in inventory.h
//	Info 		:
//----------------------------------------------------------------------------------

unsigned short inventoryRemoveItem(unsigned short item)
{
	unsigned char result;
	int i;
	int size;

	result = inventoryCheckForItem(item);	// get the first block number
	if(result!=INV_INVALIDOBJECT)
	{	// empty all blocks
		for(i=0;i<inventory_itemsize[item];i++)
		{
			currentInv->slot_contents[result+i] = EMPTY;
			currentInv->empty_slots++;	// :)
		}
		scenicInventoryNotification(item,SCENIC_INVENTORY_REMOVING);
		inventorySort();

		if(currentInv->selected_item == item)	// Quite likely, the thing we'll be removing *will* be the equipped item.
		{
			currentInv->selected_item = EMPTY;
		}

		utilPrintf("inv-remove: item %d removed!\n",item);
		inventoryPrintContentsToConsole();
		return INV_OK;
	}
	return INV_INVALIDOBJECT;
}





//----------------------------------------------------------------------------------
//	Function 	: unsigned short inventoryPickUpItem(unsigned short item)
//	Purpose 	: Add an item to the inventory and remove it from the game world
//	Parameters 	: item = the item being collected
//	Returns 	: INV_OK if successful, other wise an errorcode defined in inventory.h
//	Info 		:
//----------------------------------------------------------------------------------

unsigned short inventoryPickUpItem(unsigned short item)
{
	int i;
	int j;

// JS : Fix me !!
#ifndef _WINDOWS
	if(item == SPROUTS)
	{
		scenicInventoryNotification(item,SCENIC_INVENTORY_PICKINGUP);
		sproutsTimer = 0;

//(scenicInventoryNotification, internally, does this:- gamePlayData->sprouts_carried += 10; )

		return INV_INVALIDOBJECT;	// return INV_OK if we want sprout-collection to take us into the inventory screen
	}
#endif


	debugPrintf("Item: %d\n",item);
	if((item<=EMPTY)||(item>=TOTAL_ITEMS))
	{
		utilPrintf("inv-pickup: invalid object\n");
		return INV_INVALIDOBJECT;
	}


	if(item<=GNOME)		// Its an item
	{
		if(gamePlayData->eggs_carried==6)
		{
			return INV_NOSPACE;
		}

		if(inventory_itemsize[item]>currentInv->empty_slots) 
		{
			utilPrintf("inv-pickup: no space\n");
			return INV_NOSPACE;
		}

		// loop thru the inventory to make sure we aren't already carrying this item
		for(i=0;i<6;i++)
		{
			if(currentInv->slot_contents[i]==item)
			{
				DB("inv-pickup: already holding\n");
				return INV_ALREADYHOLDING;
			}
		}
			
		for(i=0;currentInv->slot_contents[i]!=EMPTY;i++){}
			
		if(i>5)
		{
			DB("inv-pickup: no space\n");
			return INV_NOSPACE;
		}
	
		// i now contains the first empty slot
		for(j=i;j<inventory_itemsize[item]+i;j++)
		{
			currentInv->slot_contents[j]=item;
			currentInv->empty_slots--;
		}	

		inventorySort();

		scenicInventoryNotification(item,SCENIC_INVENTORY_PICKINGUP);	

		last_item_collected = item;
		utilPrintf("inv-pickup: item %d picked up!\n",item);
		return INV_OK;
	}
	else if(item<=MAP14)			// Its a map piece
	{
		scenicInventoryNotification(item,SCENIC_INVENTORY_PICKINGUP);	
		debugPrintf("Picking up map piece %d",item-GNOME-1);
		gamePlayData->map_pieces_got[item-GNOME-1] = 1;
		return INV_OK;
	}
	else if(item<=PIC11)			// Its a bonus picture
	{
		scenicInventoryNotification(item,SCENIC_INVENTORY_PICKINGUP);	
		debugPrintf("Picking up picture %d",item-REEL5-1);
		gameSaveData.stills_available[item-REEL5-1] = 1;
#if(PALMODE==YES)
		inv_messagetimer = 75;
#else
		inv_messagetimer = 90;
#endif
		inv_currentmessage = TEXTSTR(TEXTSTR_HELP_GOTPICTURE);

		return INV_INVALIDOBJECT;
	}
	return INV_INVALIDOBJECT;
}






//----------------------------------------------------------------------------------
//	Function 	: unsigned short inventoryDoTestData(void)
//	Purpose 	: Pick up a few objects for testing purposes
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryDoTestData(void)
{
	int i;
//	currentInv = &gamePlayData->inventories[INVNO_GINGER];
//	inventoryReset();					// Empty it
	
//	inventoryPickUpItem(STRONG_CORD);
//	inventoryPickUpItem(PRAM_CHASSIS);
//	inventoryPickUpItem(BROKEN_LADDER);
//	inventoryPickUpItem(TENNIS_RACKET);

	gamePlayData->eggs_carried = 6;
	for(i=0;i<14;i++)
	{
		if(gamePlayData->map_pieces_got[i] != 1)
			inventoryPickUpItem(MAP1+i);
	}
}






//=====================================

// Run this check before calling:-
//	if(currentInv->selected_item != EMPTY
//		&& streamed_bankno == STREAMBANK_INVENTORY
//		&& streamed_bank
//		)

//#define EQUIPPED_OVERLAY_X (-200)	// (invent)(sprouts)
#define EQUIPPED_OVERLAY_X (-140)	// (sprouts)(invent)
#define EQUIPPED_OVERLAY_Y (80)
void inventoryEquippedItemOverlayDraw()
{
	TextureType *tex;
	
	ASSERT(currentInv->selected_item != EMPTY);
	
	if(levinfo->level_number<=LEVNUMBER_A1_DOGCHASE)
	{
		ASSERT(streamed_bankno == STREAMBANK_INVENTORYACT1);
	}

	else if (levinfo->level_number<=LEVNUMBER_A2_COGCHASE)
	{
		ASSERT(streamed_bankno == STREAMBANK_INVENTORYACT2);
	}

	else if(levinfo->level_number<=LEVNUMBER_A4_FLYMACHINE)
	{
		ASSERT(streamed_bankno == STREAMBANK_INVENTORYACT4);
	}

	ASSERT(streamed_bank);

	tex = objectpics[currentInv->selected_item];

	overlaySprite(tex, EQUIPPED_OVERLAY_X - tex->w/2, EQUIPPED_OVERLAY_Y - tex->h/2, PANEL_OVERLAYS_SORTDEPTH,0xff808080);
}



//----------------------------------------------------------------------------------
//	Function 	: void inventoryLoadMap(void)
//	Purpose 	: Load up all the sprites for the map pieces 
//	Parameters 	: None
//	Returns 	: None
//	Info 		: 
//----------------------------------------------------------------------------------

void inventoryLoadMap(void)
{
	inventoryEnsureStreamBank(STREAMBANK_MAP);

	map_textures[0] = textureFindInAllBanks("M1");
	ASSERT(map_textures[0]);
	map_textures[1] = textureFindInAllBanks("M2");
	ASSERT(map_textures[1]);
	map_textures[2] = textureFindInAllBanks("M3");
	ASSERT(map_textures[2]);
	map_textures[3] = textureFindInAllBanks("M4");
	ASSERT(map_textures[3]);
	map_textures[4] = textureFindInAllBanks("M5");
	ASSERT(map_textures[4]);
	map_textures[5] = textureFindInAllBanks("M6");
	ASSERT(map_textures[5]);
	map_textures[6] = textureFindInAllBanks("M7");
	ASSERT(map_textures[6]);
	map_textures[7] = textureFindInAllBanks("M8");
	ASSERT(map_textures[7]);
	map_textures[8] = textureFindInAllBanks("M9");
	ASSERT(map_textures[8]);
	map_textures[9] = textureFindInAllBanks("M10");
	ASSERT(map_textures[9]);
	map_textures[10] = textureFindInAllBanks("M11");
	ASSERT(map_textures[10]);
	map_textures[11] = textureFindInAllBanks("M12");
	ASSERT(map_textures[11]);
	map_textures[12] = textureFindInAllBanks("M13");
	ASSERT(map_textures[12]);
	map_textures[13] = textureFindInAllBanks("M14");
	ASSERT(map_textures[13]);
}





//----------------------------------------------------------------------------------
//	Function 	: void inventoryDrawTicksOnPlans(void)
//	Purpose 	: Draw ticks on the plan images, according to which items have been collected / used
//	Parameters 	: None
//	Returns 	: None
//	Info 		:
//----------------------------------------------------------------------------------

void inventoryDrawTicksOnPlans(void)
{
	switch(inventoryPlansAvailable[inventory_globals.selected_plan])
	{
		case STREAMBANK_A1_PLAN2:				// Shears Plan
			
			if(gamePlayData->inventory_item_status[BUTTER_KNIFE] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,338-256,87-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[BUTTER_KNIFE] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,338-256,87-112,0,0x808080);
			}

			
			if(gamePlayData->inventory_item_status[RUSTY_SHEARS] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,338-256,168-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[RUSTY_SHEARS] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,338-256,168-112,0,0x808080);
			}

			break;
	
		case STREAMBANK_A1_PLAN3:				// Mannequin Plan
			
			if(gamePlayData->inventory_item_status[A1POLES] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,210-256,152-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[A1POLES] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,210-256,152-112,0,0x808080);
			}



			if(gamePlayData->inventory_item_status[GLOVES] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,314-256,158-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[GLOVES] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,314-256,158-112,0,0x808080);
			}



			if(gamePlayData->inventory_item_status[BOOTS] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,314-256,29-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[BOOTS] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,314-256,29-112,0,0x808080);
			}

			if(gamePlayData->inventory_item_status[NIGHTDRESS] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,316-256,109-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[NIGHTDRESS] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,316-256,109-112,0,0x808080);
			}

			break;
		
		case STREAMBANK_A2_PLAN1:
			
			if(gamePlayData->inventory_item_status[STRONG_CORD] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,192-256,17-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[STRONG_CORD] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,192-256,17-112,0,0x808080);
			}

				
			if(gamePlayData->inventory_item_status[PRAM_CHASSIS] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,317-256,30-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[PRAM_CHASSIS] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,317-256,30-112,0,0x808080);
			}


			if(gamePlayData->inventory_item_status[BROKEN_LADDER] ==INV_STATUS_REMOVED)
			{
				overlaySprite(tick,319-256,109-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[BROKEN_LADDER] ==INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,319-256,109-112,0,0x808080);
			}

				
			if(gamePlayData->inventory_item_status[TENNIS_RACKET] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,316-256,158-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[TENNIS_RACKET] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,316-256,158-112,0,0x808080);
			}

			break;
		

		case STREAMBANK_A2_PLAN2:

			if(gamePlayData->inventory_item_status[COOKING_WEIGHTS] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,370-256,73-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[COOKING_WEIGHTS] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,370-256,73-112,0,0x808080);
			}
			break;
		

		case STREAMBANK_A2_PLAN3:
		
			if(gamePlayData->inventory_item_status[FIREWORKS] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,318-256,79-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[FIREWORKS] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,318-256,79-112,0,0x808080);
			}

			if(gamePlayData->inventory_item_status[MATCHES] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,319-256,159-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[MATCHES] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,319-256,159-112,0,0x808080);
			}
			break;


		case STREAMBANK_A2_PLAN4:

			if(gameSaveData.medals_won[MEDAL_WON_CATAPULT]>0)
			{
				overlaySprite(tick,59-256,159-112,0,0x808080);
			}
			if(gameSaveData.medals_won[MEDAL_WON_SEESAW]>0)
			{
				overlaySprite(tick,430-256,161-112,0,0x808080);
			}
			if(gameSaveData.medals_won[MEDAL_WON_FIREWORKS]>0)
			{
				overlaySprite(tick,370-256,40-112,0,0x808080);
			}
			break;

			
		case STREAMBANK_A4_PLAN1:		
			// This is based on the Act 4 subgames being succesfully completed , rather than having collected items so...
			// If you've got at least a bronze medal in each one, class it as having been completed
			if(gameSaveData.medals_won[MEDAL_WON_ENGINE]>0)
				overlaySprite(tick,319-256,32-112,0,0x808080);

			if(gameSaveData.medals_won[MEDAL_WON_CONSTRUCTION]>0)
				overlaySprite(tick,319-256,112-112,0,0x808080);

			if(gameSaveData.medals_won[MEDAL_WON_WINGS]>0)
				overlaySprite(tick,317-256,160-112,0,0x808080);
		
			break;
		
		case STREAMBANK_A4_PLAN2:
		
			if(gamePlayData->inventory_item_status[OIL_CAN] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,322-256,50-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[OIL_CAN] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,322-256,50-112,0,0x808080);
			}



			if(gamePlayData->inventory_item_status[ENGINE] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,316-256,158-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[ENGINE] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,316-256,158-112,0,0x808080);
			}



			if(gamePlayData->inventory_item_status[CHAIN] ==INV_STATUS_REMOVED)
			{
				overlaySprite(tick,325-256,85-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[CHAIN] ==INV_STATUS_REMOVED)
			{
				overlaySprite(halftick,325-256,85-112,0,0x808080);
			}


			break;
		
		case STREAMBANK_A4_PLAN3:
	
			if(gamePlayData->inventory_item_status[LAUNDRY] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,309-256,52-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[LAUNDRY] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,309-256,52-112,0,0x808080);
			}

				
			if(gamePlayData->inventory_item_status[A4POLES] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,318-256,95-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[A4POLES] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,318-256,95-112,0,0x808080);
			}


			if(gamePlayData->inventory_item_status[YARN] ==INV_STATUS_REMOVED)
			{
				overlaySprite(tick,316-256,159-112,0,0x808080);
			}
			
			else if(gamePlayData->inventory_item_status[YARN] ==INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,316-256,159-112,0,0x808080);
			}

			break;
		
		case STREAMBANK_A4_PLAN4:

			if(gamePlayData->inventory_item_status[COGS] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,209-256,27-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[COGS] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,209-256,27-112,0,0x808080);
			}

			if(gamePlayData->inventory_item_status[TOOLS] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,336-256,108-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[TOOLS] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,336-256,108-112,0,0x808080);
			}

			if(gamePlayData->inventory_item_status[WOOD] ==INV_STATUS_REMOVED)
			{
				overlaySprite(tick,316-256,159-112,0,0x808080);
			}
			if(gamePlayData->inventory_item_status[WOOD] ==INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,316-256,159-112,0,0x808080);
			}


			if(gamePlayData->inventory_item_status[BINOCULARS] == INV_STATUS_REMOVED)
			{
				overlaySprite(tick,318-256,30-112,0,0x808080);
			}
			else if(gamePlayData->inventory_item_status[BINOCULARS] == INV_STATUS_IN_INVENTORY)
			{
				overlaySprite(halftick,318-256,30-112,0,0x808080);
			}

			break;
	}
}



	


//----------------------------------------------------------------------------------
//	Function 	: void inventoryShowStretchedImage(TextureType *spr, int x, int y, int depth, int size, unsigned long colour)
//	Purpose 	: Draw an image stretched in x and y by a factor of size
//	Parameters 	: Pointer to the texture, x, y (top left), ot depth, scale (1, 2 or 4), whether or not it is equipped, colour
//	Returns 	: None
//	Info 		: If equipped is 1, it'll draw a red border around it too
//----------------------------------------------------------------------------------

void inventoryShowStretchedImage(TextureType *spr, int x, int y, int depth, int size, int equipped, unsigned long colour)
{
	POLY_FT4 *si;

	BEGINPRIM(si, POLY_FT4);

	y*=1.875;

	x+=256;
	y+=240;
	
	x*=1.25;


	BEGINPRIM(si, POLY_FT4);

	si->x0 = si->x2 = x;
	
	if(size==4)
		si->x1 = si->x3 = x + (((spr->u1 - spr->u0)*2)*1.25);
	else
		si->x1 = si->x3 = x + ((spr->u1 - spr->u0)*1.25);
	
	si->y0 = si->y1 = y;
	
	if(size!=1)
		si->y2 = si->y3 = y + (((spr->v2 - spr->v0)*2)*1.875);

	else
		si->y2 = si->y3 = y + ((spr->v2 - spr->v0)*1.875);

	*(ULONG *)(&si->u0) = ((unsigned long)*(USHORT *)&spr->u0)
		                  | (((unsigned long)(spr->clut)) <<16);
	*(ULONG *)(&si->u2) = ((unsigned long)*(USHORT *)&spr->u2);
	*(ULONG *)(&si->u3) = ((unsigned long)*(USHORT *)&spr->u3);

	*(ULONG *)(&si->u1) = ((unsigned long)*(USHORT *)&spr->u1)
						  | (((unsigned long)(spr->tpage)) <<16);

	*(ULONG*)&si->r0 = (colour & 0xffffff) | ((GPU_COM_TF4)<<24);
	setlen(si, 9);

	ENDPRIM(si, depth, POLY_FT4);

	if(equipped)
	{
		POLY_F4 *si4;

		BEGINPRIM(si4, POLY_F4);
		
		si4->r0 = 0xff;
		si4->g0 = si4->b0 = 0;
		
		si4->x0 = si4->x2 = x-2;
		
		if(size!=4)
			si4->x1 = si4->x3 = x+66;
			
		else
			si4->x1 = si4->x3 = x+(66*2);

		si4->y0 = si4->y1 = y-1;

		if(size==1)
			si4->y2 = si4->y3 = y+43;

		else
			si4->y2 = si4->y3 = y+(43*2)+1;

		setPolyF4(si4);

		ENDPRIM(si4, depth+1, POLY_F4);
	}
}

 

//----------------------------------------------------------------------------------
//	Function 	: void inventoryCheckForPlanCompletion(int plan)
//	Purpose 	: Return 1 if the plan in question has been completed
//	Parameters 	: The plan number
//	Returns 	: 1 or 0
//	Info 		:
//----------------------------------------------------------------------------------

int inventoryCheckForPlanCompletion(int plan_number)
{
	switch(plan_number)
	{
		case STREAMBANK_A1_PLAN1:		// Hut Plan - if you've cut the fence, this is done!
			if(gamePlayData->tasks_done[TASK_CUT_COOP1_FENCE])
				return 1;
			break;

		case STREAMBANK_A1_PLAN2:		// Wire Cutters Plan - if you've returned the Shears and the butter knife, this is done
			if(gamePlayData->inventory_item_status[BUTTER_KNIFE]==INV_STATUS_REMOVED	&& gamePlayData->inventory_item_status[RUSTY_SHEARS]==INV_STATUS_REMOVED)
				return 1;
			break;

		case STREAMBANK_A1_PLAN3:		// This is the dogchase  - if you're on act 2 or above, this is finished
			if(levinfo->level_number>LEVNUMBER_A1_DOGCHASE)
				return 1;
			break;

		case STREAMBANK_A2_PLAN1:		// Catapult game - if you've won a medal, its completed
			if(gameSaveData.medals_won[MEDAL_WON_CATAPULT]>0)
				return 1;
			break;

		case STREAMBANK_A2_PLAN2:		// Seesaw game - if you've won a medal, its completed
			if(gameSaveData.medals_won[MEDAL_WON_SEESAW]>0)
				return 1;
			break;

		case STREAMBANK_A2_PLAN3:		// Fireworx - if you've won a medal, its completed
			if(gameSaveData.medals_won[MEDAL_WON_FIREWORKS]>0)
				return 1;
			break;

		case STREAMBANK_A4_PLAN1:		// Crate Plan - if u finish this, u have finished the game!
			break;

		case STREAMBANK_A4_PLAN2:		// Engine game
			if(gameSaveData.medals_won[MEDAL_WON_ENGINE]>0)
				return 1;
			break;

		case STREAMBANK_A4_PLAN3:		// Wing plan
			if(gameSaveData.medals_won[MEDAL_WON_WINGS]>0)
				return 1;
			break;

		case STREAMBANK_A4_PLAN4:
			if(gameSaveData.medals_won[MEDAL_WON_CONSTRUCTION]>0)
				return 1;
			break;
	}

	return 0;
}



// Remove any map pieces that we've already got from the level

void inventoryRemoveDuplicateMapPieces(void)
{
	int i;
	SCENIC *pS;
	int inv_number;

	debugPrintf("Looking for duplicate map pieces...\n");

	for(i=0;i<14;i++)
	{
		if(gamePlayData->map_pieces_got[i]==1)	// See which map pieces we've got
		{
			inv_number = i+MAP1;			// The inventory_item number of this map piece
			
			// Now hunt throught the scenic list and remove it
			for(pS = scenicList; pS; pS=pS->next)
			{
				if((pS->flags & SCENICFLAG_IS_INVENTORY_ITEM) && (pS->inventory_item_number == inv_number))
				{
					debugPrintf("Found map piece %d in level.  You've already got this, so I'm removing it.\n",i+1);
					pS->flags &= ~(SCENICFLAG_ACTIVE);
				}
			}
		}
	}
}


// Note - no asserts, because not all the sprites are present in all the banks
// (This is called every time the streamed_bank is STREAMED or TIDIED)
void inventoryRefreshBackPointers(void)
{
	bg_nail = textureFindInAllBanks("NAIL");
	if(!bg_nail)
		debugPrintf("Unable to find NAIL texture!\n");

	bg_wood = textureFindInAllBanks("WOODSTRIP");
	if(!bg_wood)
		debugPrintf("Unable to find WOODSTRIP texture!\n");

	bg_wood2 = textureFindInAllBanks("WOODSTRIP2");
	if(!bg_wood)
		debugPrintf("Unable to find WOODSTRIP2 texture!\n");
	
	bg_wood3 = textureFindInAllBanks("WOODSTRIP3");
	if(!bg_wood)
		debugPrintf("Unable to find WOODSTRIP3 texture!\n");


	bg_mapicon = textureFindInAllBanks("MAPICON");
	if(!bg_mapicon)
		debugPrintf("Unable to find MAPICON texture!\n");

	planpic = textureFindInAllBanks("PLANFILE");
	if(!planpic)
		debugPrintf("Unable to locate PLANFILE texture!\n");

	cloth = textureFindInAllBanks("CLOTH");
	if(!cloth)
		debugPrintf("Unable to locate the CLOTH texture!\n");

	tick = textureFindInAllBanks("TICK");
	if(!tick)
		debugPrintf("Unable to find the TICK texture!\n");

	halftick = textureFindInAllBanks("HALFTICK");
	if(!halftick)
		debugPrintf("Unable to find the HALFTICK texture!\n");

	arrow = textureFindInAllBanks("RARROW");
	if(!arrow)
		debugPrintf("Unable to find ARROW texture!\n");
}


void inventoryEnsureCorrectActBank(void)
{
	if(levinfo->level_number<=LEVNUMBER_A1_DOGCHASE)
		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT1);
	else if(levinfo->level_number<=LEVNUMBER_A2_COGCHASE)
		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT2);
	else if(levinfo->level_number<=LEVNUMBER_A4_FLYMACHINE)
		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT4);
#ifndef _WINDOWS


	if(streamed_bank)
	{
//MIKEY		streamed_bank = textureTidyBank(streamed_bank);
	}
	inventoryRefreshBackPointers();
	inventoryRefreshPointers();

#endif
}


// Make sure the invent tmap pointers are set up correctly, coz the inventory does get thrown away quite often!
// (This is called ever time the streamed_bank is STREAMED or TIDIED)
void inventoryRefreshPointers(void)
{
	int i;


/*
// NB NB NB NB
// inventoryRefreshPointers is called after EVERY streamed-bank load. Including the plans.
// So the sprites won't necessarily be there, but all sprite pointers will contain either a CORRECT pointer or null.

	if(levinfo->level_number<=LEVNUMBER_A1_DOGCHASE)
		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT1);
	else if(levinfo->level_number<=LEVNUMBER_A2_COGCHASE)
		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT2);
	else if(levinfo->level_number<=LEVNUMBER_A4_FLYMACHINE)
		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT4);
*/
	// Find all the inventory textures for this level
	if(levinfo)
	{
		if(levinfo->level_number<=LEVNUMBER_A1_DOGCHASE)
		{
			for(i = 0; i < NIGHTDRESS+1; i++)
			{
				objectpics[i]=textureFindInBank(streamed_bank,inventory_tmap_names[i]);

			}
		}

		else if(levinfo->level_number<=LEVNUMBER_A2_COGCHASE)
		{
			for(i = SPOON; i < BONE+1; i++)
			{
				objectpics[i]=textureFindInBank(streamed_bank,inventory_tmap_names[i]);
			}
		}

		else if(levinfo->level_number<=LEVNUMBER_A4_FLYMACHINE)
		{
			for(i = EGG; i < GNOME+1; i++)
			{
				objectpics[i]=textureFindInBank(streamed_bank,inventory_tmap_names[i]);
			}
		}
	}
}




void inventoryBeginLevel(void)
{
	// BP: The map pieces are duplicated across acts now, so that if you don't pick them up they're still there next act
	// So, need to look through the list and remove any map pieces we've already got
	inventoryRemoveDuplicateMapPieces();

	inventoryEnsureCorrectActBank();

	// Make sure the inventory overlay pointers are valid so we dont get texture problems
	inventoryRefreshPointers();
}


void inventoryPlanViewCycle(void)
{
	if((padData.debounce[0] & PAD_LEFT)||(pReadPad->analogXS[0]<-2048))
	{
//		soundPlaySample(inv_click, 128, 1000);
		inventory_globals.selected_plan--;
		if(inventory_globals.selected_plan<0)
			inventory_globals.selected_plan = inventory_globals.numplans-1;

		inventoryLoadPlan(inventoryPlansAvailable[inventory_globals.selected_plan]);
	}
	
	if((padData.debounce[0] & PAD_RIGHT)||(pReadPad->analogXS[0]>2048))
	{
		inventory_globals.selected_plan++;
		if(inventory_globals.selected_plan>inventory_globals.numplans-1)
			inventory_globals.selected_plan = 0;

		inventoryLoadPlan(inventoryPlansAvailable[inventory_globals.selected_plan]);
	}
}

static void inventoryDrawNumber(int number, int x, int y, int r, int g, int b)
{
	int tens;
	int units;
	char text[1];

	if((number>99)||(number<0)) return;

	units = (int) number%10;
	number-=units;
	tens = ((int) number%100)/10;
	number-=tens;
	
	if(!tens)
	{
		*text = (unsigned char)units+48;
		fontPrint(font,x,y,text, r, g, b);
	}

	else 
	{
		*text = (unsigned char)tens+48;
		fontPrint(font,x,y,text, r, g, b);
		*text = (unsigned char)units+48;
		fontPrint(font,x+12,y,text, r, g, b);
	}
}