/****************************************                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          /*********************************
**        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];
/************************ MagicStore ****************************/

MagicStore()
{

int			itemType, store_id, slot, z, x,count,theitem, item, itemHit,  
			y,dialogDone = FALSE; 
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("\pWe're busy casting spells. Come back later.");
	return;
}
z=DeadCharMsg();
	if (z == -1)			/* dead char */
		return;

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

	NumToString(GameChars[gCurChar].gold,text);		/** account balance **/
		GetDItem(gCastleDialog, 13, &itemType, &itemHandle, &itemRect);
		RGBForeColor(&gSpecName);
		SetIText(itemHandle,text);	
	RGBForeColor(&gBlack); 	
		
	ListMagic();		/* now plot items */
	
	while (dialogDone == FALSE)
	{
		ModalDialog(NIL_POINTER, &itemHit);
			switch (itemHit)
			{
				case 1:								/* done */
					DisposeDialog(gCastleDialog);
					dialogDone = TRUE;
					SetPort(gGameWindow);
					break;
				case 14:							/* more */
					ListMagic();
					break;
				case 3:	
					theitem=gMagicItems[0];			 
					BuyMagicItem(theitem,itemHit);
					break;
				case 5: 
					theitem=gMagicItems[1];			 
					BuyMagicItem(theitem,itemHit);
					break;
				case 7: 
					theitem=gMagicItems[2];			 
					BuyMagicItem(theitem,itemHit);
					break;
				case 9:
					theitem=gMagicItems[3];			 
					BuyMagicItem(theitem,itemHit);
					break;
		}		
	}
}
/************************ ListMagic ****************************/

ListMagic()
{
int			itemType, store_id, slot, z, x,count,theitem, item, itemHit,  
			y,dialogDone = FALSE,cost; 
Handle		itemHandle;
Rect		itemRect;
Str255		theText;
long		price;
unsigned	min,max;


z=3;	 
count=0;

y=TRUE;
while (y!=FALSE)
{	
for (x=0;x<=3;x++)			/** List 4 items in store approp. to chars level **/
	{
	item=GetRandomNum(min=0,max=74);
	if (GameItems[item].level <= GameChars[gCurChar].level &&
		GameItems[item].level>=1 && GameItems[item].level<=11 )
		{
		GetDItem(gCastleDialog, z, &itemType, &itemHandle, &itemRect);
			SetCTitle(itemHandle,GameItems[item].name);
			gMagicItems[count]=item;		/** counter for items in shoppe  ***/
			cost=GetRandomNum(min=1,max=5);			
		GetDItem(gCastleDialog, z+1, &itemType, &itemHandle, &itemRect);
			NumToString((GameItems[item].level*134)+cost,theText);
			SetIText(itemHandle,theText);						
			z=z+2;
			count++;			
		if (count == 4)	
			{
			y=FALSE;
			break;			/* all 4 items found, now leave */
			}
		}
	}
}
}

/************************ BuyMagicItem ****************************/
BuyMagicItem(theitem,itemHit)

int			theitem,itemHit;

{

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

	 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;
			}

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