/****************************************                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          /*********************************
**        GayBlade   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,gCPUSpeed;
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];	
/************************ UserAction ****************************/

UserAction(mouseLoc)
Point			mouseLoc;

{
int				itemType, itemHit,  dialogDone = FALSE,secs;
Handle			itemHandle;
Rect			itemRect,rect,buttonRect,sourceRect,destRect,worldBounds;
Str255			text,name;
short			status;
Rect			fightRect, shieldRect, spellRect, useRect;
CIconHandle		buttonIcon;
CGrafPtr		currentPort,currentPort2;		/* Port used before switching to the gworld's. */
GDHandle		currentDevice,currentDevice2;	/* GDevice used before switching to the gworld's. */
GWorldPtr		TempGWorld,TempGWorld2;	
PixMapHandle	TempPixMap,TempPixMap2;			//  Needed PixMapHandle to lock down.		
PicHandle		buttonPict;

	SetRect(&rect,0,0,182,39);					/* button pad rect */		
	NewGWorld( &TempGWorld, 0, &rect, nil , nil, 0 );
	TempPixMap = GetGWorldPixMap( TempGWorld );			//  Get the GWorlds Pixmaphandle
	LockPixels( TempPixMap );							//  Lock it down
	
	GetGWorld( &currentPort, &currentDevice );		//  Save screen environment

	SetGWorld( TempGWorld, nil );					//	Make screen GWorld current
		RGBForeColor( &gBlack );					//  make sure Fore ground color black
		SetRect(&buttonRect,102,324,282,363);		/*  COPY BUTTON PAD */
	CopyBits ( &(*gGameWindow).portBits, 
		&(((GrafPtr) TempGWorld)->portBits), &buttonRect, &rect, 0, nil );		
		
SetGWorld( currentPort, currentDevice );			/*  now set back to screen */

SetRect(&fightRect,102,324,141,363);
SetRect(&shieldRect,145,324,184,363);
SetRect(&spellRect,191,324,230,363);
SetRect(&useRect,234,324,273,363);

if (gDungParty[0]==99)		/* no char, then go away! */
{
	return;
}

if (PtInRect(mouseLoc, &fightRect))	
	{
		buttonIcon=GetCIcon(201);	
		PlotCIcon(&fightRect,buttonIcon);
			Fight();	
	}
else if (PtInRect(mouseLoc, &shieldRect))
	{
		buttonIcon=GetCIcon(202);
			PlotCIcon(&shieldRect,buttonIcon);
			Shield();
	}
else if (PtInRect(mouseLoc, &spellRect))
	{
		buttonIcon=GetCIcon(203);
		PlotCIcon(&spellRect,buttonIcon);
		Spell();		
	}
else if (PtInRect(mouseLoc, &useRect))
	{
		buttonIcon=GetCIcon(204);	
		PlotCIcon(&useRect,buttonIcon);
		UseItem();
	}	 

									/* BELOW copies  gworld info to screen */		
		RGBForeColor(white);
		RGBBackColor(black);
									// draw saved stuff in GWorld back to main screen
	CopyBits (&(((GrafPtr) TempGWorld)->portBits), 
		&(*gGameWindow).portBits, &rect, &buttonRect, 0, nil );
	DisposeGWorld(TempGWorld);		// this is temp GWorld so dump it	
}

/************************ Fight ****************************/

Fight()
{		
int			z,y,x;
long		hitPoints;
unsigned	min,max;
Str255		theText;

z=DeadCharMsg();					/* if char is dead, leave */
	if (z==-1)
	 	return;
	 	
if (gInCastle==YES)		/* in castle, don't allow */
	{
		ClearMessage();
			DrawString("\pNo fighting is allowed in the Castle!");
			Pause();Pause();
		return;
	}
	z=DeadCharMsg();				/* if char is dead, leave */
if (z==-1)
	 	return;
	 	
  if (gInEncounter==NO)				/** NOT IN ENCOUNTER **/
	{
		gSndNum=30907;
			PlaySound();
			ClearMessage();DrawString(GameChars[gCurChar].name);
			DrawString("\p swings at nothing!");
		return;
	}
  else								/** YES ENCOUNTER!! **/
    {
     ClearMessage();				/** lower your dex, more chance of missing */
     y=GetRandomNum(min=0,max=GameChars[gCurChar].dex);
     	if (y<=1)
     		{
     			gSndNum=30907;
					PlaySound();
     			DrawString("\pMissed!");
     			Pause();
     			return;
     		}
         else
         	{
         	if (GameChars[gCurChar].str/3 <=0)
         		min=1;
         	else 
         		min=GameChars[gCurChar].str/2;
         	hitPoints=GetRandomNum(min,max=GameChars[gCurChar].str);	/* hits rnd. of str */
         	for (x=0;x<=6;x++)						/** NOTE: return with full damage, if no shielding! **/
				{ 
			if (GameItems[GameChars[gCurChar].itemsInUse[x]].type==SWORD ||
				GameItems[GameChars[gCurChar].itemsInUse[x]].type==DAGGER ||
				GameItems[GameChars[gCurChar].itemsInUse[x]].type==GAUNTLETS ||
				GameItems[GameChars[gCurChar].itemsInUse[x]].type==FLAIL)
				{
					hitPoints=hitPoints+GameItems[GameChars[gCurChar].itemsInUse[x]].dmg;
					break;		/* total damage char hits for */
				}
			}
	if (hitPoints<=0)
		hitPoints=1;  /* chars hit for at least one hit point */
	z=FALSE;
	while (z !=TRUE)		/* find living char (there's at least one)  */
		{
		y=GetRandomNum(min=0,max=gCreatureCounter);
		if (gCreatHits[y] != -1)
			break;
		}
		
	if (gCreatHits[y]-hitPoints <= 0)		/* damage to creature */
		{
		ClearMessage();
			gSndNum=8549;
			PlaySound();	
				DrawString("\pYou destroyed a ");
				DrawString(GameCreatures[0].name);
				DrawString("\p!");
				GameChars[gCurChar].exp=GameChars[gCurChar].exp +
					GameCreatures[0].level * 13;
			Pause();Pause();
		gCreatureCounter--;					/* subtract dead creature */
		}
		
	else
		{
		   if (GetRandomNum(min=0,max=100)<=50)
     			gSndNum=128;
     		else
				gSndNum=32302;
			PlaySound();
		gCreatHits[y]=gCreatHits[y]-hitPoints;
			ClearMessage();
				DrawString("\pYou hit for ");
				NumToString(hitPoints,theText);
				DrawString(theText);				
			if (hitPoints==1)							/** if only one creature **/
		 		DrawString("\p hit!");
		 	else
				DrawString("\p hits!");
			GameChars[gCurChar].exp=GameChars[gCurChar].exp+1;
		Pause();Pause();
		}
	}
}
}
/************************ Shield ****************************/

Shield()
{		
int	z,x,count=0;

z=DeadCharMsg();					/* if char is dead, leave */

if (z==-1)
	 	return;
	 	
if (gInCastle==YES)		/* in castle, don't allow */
{
		ClearMessage();
			DrawString("\pNo parrying is allowed in the Castle!");
			Pause();Pause();			
		return;
}

if (gInEncounter==NO)
	{
		ClearMessage();DrawString(GameChars[gCurChar].name);
			DrawString("\p parrys, though nothing is here.");
		return;
	}
  else			/** in ENCOUNTER!! **/
    {	
    ClearMessage();
    DrawString(GameChars[gCurChar].name);
    DrawString("\p protects by parrying.");
	    for (x=0;x<=3;x++)					/* find open slot in shield array */
	    {
	    	if (gCharShields[x]==99)		/* if open slot for shielder*/
	    		{
	   			 gCharShields[x]=gCurChar;	/* set slot to char */
	   			 break;						/* and exit loop */
	   			 }
	   	}
	    }
	 Pause();
}
/************************ Spell ****************************/

Spell()
{		
int				z,x,y, dialogDone = FALSE, screen=0;
Rect			spellPadRect,destRect,worldBounds,buttonRect;
Str255			theText;
PicHandle		spellPadPict;
GWorldPtr		world;
GWorldPtr		MakeGWorld();
CCrsrHandle		gItemCursor;
Point			mouseLoc;

ClearMessage();
RGBForeColor(white);
RGBBackColor(black);

	z=DeadCharMsg();							/* if char is dead, leave */
if (z==-1)
	 	return;
	 	
if (gInCastle==YES)								/* in castle, don't allow */
{
	ClearMessage();
		DrawString("\pNo spell casting allowed in the Castle!");
			Pause();Pause();		
	return;
}

if (GameChars[gCurChar].type==0)				/* don't allow not spell caster */
{
	ClearMessage();
		DrawString("\pQueers can't cast spells!");
		Pause();Pause();
	return;
}

		SetRect(&destRect,0,0,517,369);				/* copy entire fckng screen */
		SetRect(&worldBounds,0,0,517,369);

SetRect(&spellPadRect,357,153,507,362);				/* Spell pad rectangle */
	SetRect(&buttonRect,408,333,454,351);	
	world=MakeGWorld(&worldBounds);
CopyIntroBits(world,&gConsoleRect);					/* copy current screen */ 
 
  		
	ClearMessage();	
	DrawString("\pCast which Spell?");
		gItemCursor=GetCCursor(129);	/* now get color cursor */		
	SetCCursor(gItemCursor); 
	
 	if (GameChars[gCurChar].type==2)	/* PLOT approprite spellpad for type */
 		{
		PlotMagicPad(spellPadRect);		/* plot magic screen */
		screen=0;						/* on MAGIC screen */
		}
 	if (GameChars[gCurChar].type==4 || GameChars[gCurChar].type==1)
 		{
		PlotClericPad(spellPadRect);	/* plot cleric screen */
		screen=1;						/* on CLERIC screen */
		}
	else
		{
		PlotMagicPad(spellPadRect);		/* plot magic screen */
		screen=0;						/* on MAGIC screen */
		}
		
FlushEvents (everyEvent, REMOVE_ALL_EVENTS);	/* clear events before going to loop */

z=FALSE;
	while (z!=TRUE)
	  {
		if (gWNEImplemented)
			WaitNextEvent(everyEvent,&gTheEvent,MIN_SLEEP,NIL_MOUSE_REGION);
		else
			{
				SystemTask();
				SndTask();
				GetNextEvent(everyEvent, &gTheEvent);
			}	
		
		switch (gTheEvent.what)
		{
			case mouseDown:								/* mousedown event */
				GetMouse(&mouseLoc);					/* get LOCAL coords */
				if (PtInRect(mouseLoc, &buttonRect))
				{
					if (screen==0)						/* on MAGIC screen */
					{
						PlotClericPad(spellPadRect);	/* then plot cleric spells */
						screen=1;
					}
					else if (screen==1)					/* on CLERIC screen */
					{
						PlotMagicPad(spellPadRect);	/* then plot magic spells */
						screen=0;
					}						
				}				
				else if (PtInRect(mouseLoc, &spellPadRect))
				{
					ClearMessage();
						if (screen==0)
							{
								RGBForeColor(white);
								RGBBackColor(black);
								CopyWorldBits(world,&gConsoleRect);	/* copy off screen */							
								CastMagicSpell(mouseLoc);
								z=TRUE;
								break;
							}
						else if (screen==1)
							{
								RGBForeColor(white);
								RGBBackColor(black);
								CopyWorldBits(world,&gConsoleRect);	/* copy off screen */							
								CastClericSpell(mouseLoc);
								z=TRUE;
								break;
							}				
				}
				else				/* mouse down elsewhere */
				{
					ClearMessage();
					DrawString("\pSpell was miscast!");
					Pause();Pause();	
					z=TRUE;
						RGBForeColor(white);
						RGBBackColor(black);
						CopyWorldBits(world,&gConsoleRect);	/* copy off screen */							
				
				break;			
				}
			}
		}
	
			DisposeCCursor(gItemCursor); 		/* cursor back to normal */	
			  InitCursor();	
	DisposeGWorld ( world );
	
}

/************************ CastMagicSpell ****************************/
CastMagicSpell(mouseLoc)

Point			mouseLoc;

{
Rect		whichSpellRect;
int			spellStatus=-1,spellLevel, spellType;

SetPort(gGameWindow);								/* redundant call, but ... */

SetRect(&whichSpellRect,367,211,425,222);			/* Light Wound */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=0;									/* first level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);	/* okay to cast? */
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();
		return;
	}
	else											/* YES! */
		{
		Wound();
		return;
		}
	}
	
SetRect(&whichSpellRect,434,211,495,222);			/* Evade */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=0;									/* first level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		Evade();
		return;
		}		
	}
	
SetRect(&whichSpellRect,366,232,425,243);			/* Heavy Wound */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=1;									/* second level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		HeavyWound();
		return;
		}
	}
		
SetRect(&whichSpellRect,435,232,445,243);			/* Light */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=1;									/* SECOND level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		if (gInEncounter==YES)						/* no lights in encounter */
			{
			ClearMessage();
			DrawString("\pYou can't cast that right now.");
			GameChars[gCurChar].mspells[1]++;		/* add spell unit back */
			Pause();Pause();
			return;
			}
		CastLight();
		return;
		}	
	}
	
SetRect(&whichSpellRect,366,256,425,267);			/* INVIS */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=2;									/* THIRD level spell */									/* second level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		Invis();
		return;
		}
	}
	
SetRect(&whichSpellRect,435,273,445,284);			/* INVIS OFF */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=2;									/* THIRD level spell */									/* second level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		InvisOff();
		return;
		}
	}		
SetRect(&whichSpellRect,366,273,425,284);			/* LOCATE */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=2;									/* THIRD level spell */									/* second level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		Locate();
		return;
		}
	}
SetRect(&whichSpellRect,435,256,445,267);			/* LIGHTNING */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=2;									/* THIRD level spell */									/* second level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		LightningBolts();
		return;
		}
	}
SetRect(&whichSpellRect,336,295,425,306);			/* FIREBALLS */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=3;									/* FOURTH level spell */									/* second level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		FireBalls();
		return;
		}
	}
SetRect(&whichSpellRect,435,312,495,323);			/* HOME */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=3;									/* FOURTH level spell */									/* second level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		if (gInEncounter==YES)						/* no homing in encounter */
			{
			ClearMessage();
			DrawString("\pYou can't cast that right now.");
			GameChars[gCurChar].mspells[3]++;		/* add spell unit back */
			Pause();Pause();
			return;
			}
		Home();
		return;
		}
	}					
SetRect(&whichSpellRect,435,295,495,306);			/* ICESTORM */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=3;									/* FOURTH level spell */									/* second level spell */
	spellType=0;									/* magic spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		IceStorm();
		return;
		}
	}						
}

/************************ CastClericSpell ****************************/
CastClericSpell(mouseLoc)

Point			mouseLoc;
{
Rect		whichSpellRect;
int			spellStatus=-1,spellLevel, spellType,validChar;

SetRect(&whichSpellRect,434,211,495,222);			/* Light Heal */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=0;									/* first level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		return;
	}
	else
	{
		ClearMessage();
		DrawString("\pLightly Heal Who?");
		Pause();
			validChar=GetChar();			
		if (validChar==-1)				/* no valid char was selected */
			return;
		else if (validChar!=-1)			/* good char */
			{
			LightHeal();
			return;
			}
	}
}
	
SetRect(&whichSpellRect,367,211,425,222);		/* disarm */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=0;									/* first level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
			Disarm();
			return;
		}
	}
	
SetRect(&whichSpellRect,366,232,425,243);			/* compass */		
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=1;									/* Second level spell */									/* second level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		Compass();
		return;
		}
	}
		
SetRect(&whichSpellRect,435,232,445,243);			/* Cure Poison */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=1;									/* second level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
	{
		ClearMessage();
		DrawString("\pCure Who?");
		Pause();
			validChar=GetChar();			
		if (validChar==-1)				/* no valid char was selected */
			return;
		else
			{
			CurePoison();
			return;
			}
	}
}
	
SetRect(&whichSpellRect,366,256,425,267);			/* Fire Shield */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=2;									/* THIRD level spell */									/* second level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		FireShield();
		return;
		}
	}
SetRect(&whichSpellRect,435,256,445,267);			/* HCure */
if (PtInRect(mouseLoc, &whichSpellRect))	/* Heavy Cure */
	{
	spellLevel=2;									/* third level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
	{
		ClearMessage();
		DrawString("\pHeavily Heal Who?");
		Pause();
			validChar=GetChar();			
		if (validChar==-1)				/* no valid char was selected */
			return;
		else
			{
			HeavyHeal();
			return;
			}
	}
	}
SetRect(&whichSpellRect,366,273,425,284);			/* Ice Shield */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=2;									/* THIRD level spell */									/* second level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		IceShield();
		return;
		}
	}
	
SetRect(&whichSpellRect,435,273,445,284);			/* Raise */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=2;									/* THIRD level spell */									/* second level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
	{
		ClearMessage();
		DrawString("\pRaise Who?");
		Pause();
			validChar=GetChar();			
		if (validChar==-1)				/* no valid char was selected */
			return;
		else
			{
			RaiseDead();
			return;
			}
	}
  }

SetRect(&whichSpellRect,336,295,425,306);			/* EyeDeath */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=3;									/* FOURTH level spell */									/* second level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		EyeOfDeath();
		return;
		}
	}
	
SetRect(&whichSpellRect,435,312,495,323);			/*  Cure All */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=3;									/* FOURTH level spell */									/* second level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
		{
		CureAll();
		return;
		}
	}
						
SetRect(&whichSpellRect,435,295,495,306);			/* Restore */
if (PtInRect(mouseLoc, &whichSpellRect))
	{
	spellLevel=3;									/* FOURTH level spell */									/* second level spell */
	spellType=1;									/* cleric spell */
	spellStatus=CheckSpells(spellType,spellLevel);
	if (spellStatus==-1)							/* No! */
	{
		ClearMessage();
		DrawString("\pNothing happensÉ");
		Pause();Pause();		
		return;
	}
	else
	{
		RestoreSpells();
		return;
	}
  }
}
/************************ CheckSpellStatus ****************************/
CheckSpells(spellType,spellLevel)
int			spellType,spellLevel;
{

if (spellType==0 && spellLevel==0)	/*** MSPELL - LEVEL 1 ***/
{
	if (GameChars[gCurChar].mspells[spellLevel]==0)		/* not valid char or spells */
		return(-1);
	else
		{
		GameChars[gCurChar].mspells[0]--;				/* subtract spell */
		return(0);
		}
}
else if (spellType==0 && spellLevel==1)	/*** MSPELL - LEVEL 2 ***/
{
	if (GameChars[gCurChar].mspells[spellLevel]==0)		/* not valid char or spells */
		return(-1);
	else
		{
		GameChars[gCurChar].mspells[1]--;
		return(0);
		}
}
if (spellType==0 && spellLevel==2)	/*** MSPELL - LEVEL 3 ***/
{
	if (GameChars[gCurChar].mspells[spellLevel]==0)		/* not valid char or spells */
		return(-1);
	else
		{
		GameChars[gCurChar].mspells[2]--;
		return(0);
		}
}
else if (spellType==0 && spellLevel==3)	/*** MSPELL - LEVEL 4 ***/
{
	if (GameChars[gCurChar].mspells[spellLevel]==0)		/* not valid char or spells */
		return(-1);
	else
		{
		GameChars[gCurChar].mspells[3]--;
		return(0);
		}
}
if (spellType==1 && spellLevel==0)	/*** CSPELL - LEVEL 1 ***/
{
	if (GameChars[gCurChar].cspells[spellLevel]==0)		/* not valid char or spells */
		return(-1);
	else
		{
		GameChars[gCurChar].cspells[0]--;
		return(0);
		}
}
if (spellType==1 && spellLevel==1)	/*** CSPELL - LEVEL 2 ***/
{
	if (GameChars[gCurChar].cspells[spellLevel]==0)		/* not valid char or spells */
		return(-1);
	else
		{
		GameChars[gCurChar].cspells[1]--;
		return(0);
		}
}
if (spellType==1 && spellLevel==2)	/*** CSPELL - LEVEL 3 ***/
{
	if (GameChars[gCurChar].cspells[spellLevel]==0)		/* not valid char or spells */
		return(-1);
	else
		{
		GameChars[gCurChar].cspells[2]--;
		return(0);
		}
}
if (spellType==1 && spellLevel==3)	/*** CSPELL - LEVEL 4 ***/
{
	if (GameChars[gCurChar].cspells[spellLevel]==0)		/* not valid char or spells */
		return(-1);
	else
		{
		GameChars[gCurChar].cspells[3]--;
		return(0);
		}
}
}			
/************************ PlotMagicPad ****************************/

PlotMagicPad(spellPadRect)
Rect		spellPadRect;

{		
int			z,x,y, dialogDone = FALSE;
Str255		theText;
PicHandle	spellPadPict;

spellPadPict = GetPicture(145);
DrawPicture (spellPadPict, &spellPadRect);

RGBBackColor(&gNameBack);
RGBForeColor(&gSpecName);	 	/* Update stuff in stats area */
 	x = 368;y = 177;	
  		MoveTo(x,y); DrawString(GameChars[gCurChar].name);
  		
  		x = 455;y = 194;				/** MAGIC spell units **/
  	for (z=0;z<=3;z++)
  		{
  			MoveTo(x+(z*11),y);
  			NumToString(GameChars[gCurChar].mspells[z],theText);
  			DrawString(theText);
  		}
}

/************************ PlotClericPad ****************************/

PlotClericPad(spellPadRect)
Rect		spellPadRect;
{		
int			z,x,y, dialogDone = FALSE;
Str255		theText;
PicHandle	spellPadPict;

	spellPadPict = GetPicture(146);
DrawPicture (spellPadPict, &spellPadRect);

RGBBackColor(&gNameBack);
RGBForeColor(&gSpecName);	 	/* Update stuff in stats area */
 	x = 368;y = 177;	
  		MoveTo(x,y); DrawString(GameChars[gCurChar].name);
  		
  		x = 455;y = 194;				/** CLERIC spell units **/
  	for (z=0;z<=3;z++)
  		{
  			MoveTo(x+(z*11),y);
  			NumToString(GameChars[gCurChar].cspells[z],theText);
  			DrawString(theText);
  		}
}

/************************ UseItem ****************************/

UseItem()		/* stay in routine until user voids */
{		
char			theChar;
GrafPtr			oldPort;
Rect			itemRect,destRect,worldBounds;
int				theTime,z=FALSE, x, y,count=-1,keepGoing;
Str255			theText;
CCrsrHandle		gItemCursor;
Point			mouseLoc;

z=DeadCharMsg();			/* if char is dead, leave */
	if (z==-1)
	 	return;

if (gInCastle==YES)			/* in castle, don't allow */
{
	ClearMessage();
		DrawString("\pYou can't use items in the Castle!");
			Pause();Pause();		
	return;
}

gItemUseScreen=YES;
x = 368;y = 169;			/*  everything checks out, replot screen and go */
  gDisplay=0;				/* items display */
  ListItems();

gItemCursor=GetCCursor(129);	/*  now get color cursors  */		
	SetCCursor(gItemCursor); 
	 
	ClearMessage();	
	DrawString("\pUse which item?");

	/*	PlaySound();	*/
	
FlushEvents (everyEvent, REMOVE_ALL_EVENTS);	/* clear events before going to loop */


z=FALSE;
	while (z!=TRUE)
	  {
		if (gWNEImplemented)
			WaitNextEvent(everyEvent,&gTheEvent,MIN_SLEEP,NIL_MOUSE_REGION);
		else
			{
				SystemTask();
				SndTask();
				GetNextEvent(everyEvent, &gTheEvent);
			}	
		
		switch (gTheEvent.what)
		{
			case mouseDown:			/* only look for mousedown event */
			GetMouse(&mouseLoc);	/* get LOCAL coords */

				if (PtInRect(mouseLoc, &gStatsRect))
				{
					if (Items()==NO)		/* go do it and check status */
					{
						ClearMessage();		/* not valid item but in valid rect. */
						DrawString("\pNothing happensÉ");
						Pause();Pause();
					}
						z=TRUE;				/* finish loop now */
					break;
				}
				else
				{
					ClearMessage();			/* not EVEN in valid rect! */
					DrawString("\pNothing happensÉ");
					Pause();Pause();
					gItemUseScreen=NO;		/* Reset it! */
					z=TRUE;
				}
			break;
		}		
	}
	DisposeCCursor(gItemCursor); 		/* cursor back to normal */	
	  InitCursor();		
}