/****************************************                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          /*********************************
**        DragonBlade   Ver 1.0.0      **
**      © Copyright by Ryan Best 1992  **
**          All Rights Reserved.       **
*****************************************/
#include "GB.h"

extern	WindowPtr	gPictureWindow, gGameWindow, gCastleWindow;
extern	Boolean		gDone, gWNEImplemented;
extern	EventRecord	gTheEvent;
extern	MenuHandle	gAppleMenu,gFileMenu,gOptionsMenu,gSpecialMenu;
extern	Rect		gIntroRect, gTavernRect, gBankRect, gRollerRect, gStoreRect, gCharRect0,
					gCharRect1, gCharRect2, gCharRect3,gItemsRect, gMapRect, gSpellsRect,gSrcRect,
					gGuildRect,gHealerRect,gdungeonRect, gStatsRect, gPartyInfoRect,
					gUseRect,gPanicRect, gMoveRect, gDungDispRect, gActionRect,gConsoleRect, 
					gMScreenRect,leftRect,centerRect,rightRect,cLeftRect,cCenterRect,cRightRect,
					doorRect,clwallRect,crwallRect,cRect, gChestRect,gItemRect,gListStatsRect,
					gListItemsRect, gCompassRect,gTreasureRect;
extern	PicHandle	gThePicture, gConsolePicture, gDungeon1, gLeftWall, gCenterWall, gRightWall,
					gLeftDoor,gRightDoor,gFlatWall,gFlatDoor,gOpenSpace,gFarWall,gFarDoor,gCLWall,gCRWall,
					gCOpenSpace,gCLDoor,gCRDoor,gCFWall,gCFDoor, gMScreenPicture;
extern	CIconHandle	gVialIcon,gShieldIcon, gRingIcon, gSphereIcon,gFlailIcon,gCloakIcon,gArrowIcon,gDaggerIcon,
					gSwordIcon,gHelmetIcon,gTorchIcon,gBookIcon,gGlovesIcon,gBurnedIcon,gArmorIcon,
					gChestIcon, gCompassIcon,gCNorthIcon,gCSouthIcon,gCEastIcon,
					gCWestIcon;
extern int			gSndOn,gPanicOn, gInCastle, gPartyNum,gNewChar, gSndNum, gLastChar, gLight,
					gLightSpell,gItemUseScreen, gCurChar, gInPrison,gDialogNum,gDisplay,gDungLevel,gXLoc,gYLoc,
					gView,gResistFire,gResistIce,gPartyInvis,gInEncounter,gCureWho,gFoundItems[3],
					gCreatType, gCreatureCounter, gCreatHits[10],gLevelCount,dmgType,gTreasure,
					gDisarmSpellCast,gCompass,ASave,gMagicItems[4],gSpeed,gCharShields[4],gEvade;
extern	long		gStats[6], gDungParty[4],gWhichChar,glastWhen,gPanic;
extern	Point		glastWhere;
extern	DialogPtr	gCastleDialog;
extern	RGBColor	gNameBack, gStatsBack, gSpecName,gNormName,gMessBack,gBlack,gStatsFore;
extern	CCrsrHandle	gSpellCursor;
extern	SFReply		fileName;

extern	struct	CharInfo 
{
	Str255	name;
	int		type;
	int		race;
	int		level;
	int		exp;
	int		status;
	int		hits;
	int		dmg;
	int		age;
	int		str;
	int		wis;
	int		itl;
	int		con;
	int		dex;
	int		items[7];	
	int		itemsInUse[7];			/* what items are being used */
	int		gold;
	int	 	bank;					/* released wizard? */
	int		mspells[4],cspells[4];
	int		dngLevel, xLoc, yLoc,Pos;	
};
extern	struct	CharInfo	GameChars[8];						/*  Can store up to 8 chars per game  */

extern	struct	ItemInfo 
{
	Str255			name;
	unsigned char	type;
	unsigned char	status;		/* whether item is in use or not */
	unsigned char	instore;
	unsigned char	level;
	Byte			attrib;
	unsigned char	dmg;
	unsigned char	def;
	unsigned char	con;
};
extern	struct	ItemInfo	GameItems[101];

extern	struct	DungInfo 				
{
	unsigned int	XY[10][10];		/*  x, y  rooms location */
	unsigned int	N[100];			/* 100 rooms w/north walls */
	unsigned int	S[100];			/* 100 rooms w/south walls */
	unsigned int	E[100];			/* 100 rooms w/east walls */
	unsigned int	W[100];			/* 100 rooms w/west walls */
	unsigned int	SA[100];
};
extern	struct	DungInfo	Dungeon[1];	

extern struct	CreatureInfo 				/* access database each encounter */
{
	Str255			name;
	unsigned char	type;
	unsigned char	status;		/* For use in editor only */
	unsigned char	instore;	/* Unused variable */
	unsigned char	level;
	Byte			attrib;
	Byte			dmg;
	Byte			def;
	Byte			hits;
};

extern struct	CreatureInfo	GameCreatures[1];

/************************ TheStoreDialog ****************************/

TheStore()
{

int			itemType, store_id, slot, z, x,count,theitem, itemHit,  
			y,dialogDone = FALSE,itemCount[9]; 
Handle		itemHandle;
Rect		itemRect;
Str255		text;
long		price;
unsigned	min,max;

gSndNum=19402;
PlaySound();

if (gDungParty[0]==99)		/* no char, then go away! */
{
	ClearMessage();
		DrawString("\pSorry, Shylock's is closed right now.");
	return;
}
z=DeadCharMsg();
	if (z == -1)			/* dead char */
		return;

	gDisplay=0;		/* items */
	ListItems();
	gDialogNum=407;
	DoDialog();
	
 	ParamText (GameChars[gCurChar].name,"\p","\p ","\p ");	 
		ClearMessage();	
	DrawString(GameChars[gCurChar].name);DrawString("\p has entered the store.");
	RGBBackColor(&gStatsBack);
	
	GetDItem(gCastleDialog, OKAY_ITEM, &itemType, &itemHandle, &itemRect);
	HiliteControl(itemHandle, 0);

z=4;	 
count=0;

for (x=0;x<=100 || z<=18;x++)		/** List 8 items in store **/
	{
	if (GameItems[x].instore==1)		/** item is in store **/
		{
		GetDItem(gCastleDialog, z, &itemType, &itemHandle, &itemRect);
			SetCTitle(itemHandle,GameItems[x].name);
			itemCount[count]=x;		/** counter for items in store ***/
			z=z+2;
		count++;
		}
	}

	NumToString(GameChars[gCurChar].gold,text);		/** account balance **/
	GetDItem(gCastleDialog, 22, &itemType, &itemHandle, &itemRect);
RGBForeColor(&gSpecName);
	SetIText(itemHandle,text);	
RGBForeColor(&gBlack); 		
	
	while (dialogDone == FALSE)
	{
		ModalDialog(NIL_POINTER, &itemHit);
			switch (itemHit)
			{
				case 1:								/* LEAVE */
					DisposeDialog(gCastleDialog);
					dialogDone = TRUE;
					SetPort(gGameWindow);
					break;
				case 4:	
					theitem=itemCount[0];			 
					BuyItem(theitem,itemHit,price);
					break;
				case 6: 
					theitem=itemCount[1];			 
					BuyItem(theitem,itemHit,price);
					break;
				case 8: 
					theitem=itemCount[2];			 
					BuyItem(theitem,itemHit,price);
					break;
				case 10:
					theitem=itemCount[3];			 
					BuyItem(theitem,itemHit,price);
					break;
				case 12:
					theitem=itemCount[4];			 
					BuyItem(theitem,itemHit,price);
					break;
				case 14:
					theitem=itemCount[5];			 
					BuyItem(theitem,itemHit,price);
					break;
				case 16:
					theitem=itemCount[6];			 
					BuyItem(theitem,itemHit,price);
					break;
				case 18:
					theitem=itemCount[7];			 
					BuyItem(theitem,itemHit,price);
					break;
		}
	}
	
}

/************************ BuyItem ****************************/
BuyItem(theitem,itemHit,price)

int			theitem,itemHit;
long		price;

{

int			itemType, store_id, slot, z,  dialogDone = FALSE; 
Handle		itemHandle;
Rect		itemRect;
Str255		text;

	 GetDItem(gCastleDialog, itemHit+1, &itemType, &itemHandle, &itemRect);
	 GetIText(itemHandle, text);							
	   StringToNum(text,&price);
	if (GameChars[gCurChar].gold<=0 || 
		GameChars[gCurChar].gold<price)
		{
		ClearMessage();
		DrawString("\pYou don't have enough gold to buy that!"); 
		return;
		}
	else		/* all okay, now check for open slots */
		{
		for (z=0;z<=6;z++)
		{
			if (GameChars[gCurChar].items[z]==101)
				{
				  slot=z;
				  z=99;			/* exit */
				 }
		}
			if (z == 7)			/* no slots open */
			{
				ClearMessage();
				DrawString("\pYou can't carry any more items.");
				return;
			}

			
		/* add item to characters equipment! */
							
		GameChars[gCurChar].gold=GameChars[gCurChar].gold-price;
		gSndNum=10305;		/* ping */
			PlaySound();
		ClearMessage();
			GetDItem(gCastleDialog, itemHit, &itemType, &itemHandle, &itemRect);			  
			GetCTitle (itemHandle, text );	
			DrawString(GameChars[gCurChar].name); DrawString("\p just purchased a ");
			DrawString(GameItems[theitem].name);
		RGBBackColor(&gStatsBack);					
			GameChars[gCurChar].items[slot]=theitem; 
		ListItems();
		NumToString(GameChars[gCurChar].gold,text);
			GetDItem(gCastleDialog, 22, &itemType, &itemHandle, &itemRect);
			RGBForeColor(&gSpecName);
				SetIText(itemHandle,text);	
			RGBForeColor(&gBlack);
	
		}
}

