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

/************************ TheTavern ****************************/

TheTavern()
{

int				itemType, tavern_id, itemHit, x,z,count,temp[4],
				dialogDone = FALSE,allowedIn=NO; 
Handle			itemHandle;
Rect			itemRect,middleRect,worldBounds;
Str255			text;
short			status;

gSndNum=19402;
	PlaySound();

for (x=0;x<=7;x++)		/* no char, then go away! */
{
	if (GameChars[x].status!=99)
		{
		allowedIn=YES;
		break;
		}
}

if (allowedIn==NO)
	{
 		ClearMessage();
		DrawString("\pThere is no one in the Tavern.");
		return;
	}

gDialogNum=411;
	DoDialog();

count = 0;				/* loop counter */
gCurChar=0;		
gWhichChar=0 ;			/* current char stats =0    */
	
GetDItem(gCastleDialog, 1, &itemType, &itemHandle, &itemRect);
	HiliteControl(itemHandle, 0);

for (z=0;z<=7;z++)									/* list all 8 chars */						
 	{
	 GetDItem(gCastleDialog, z+4, &itemType, &itemHandle, &itemRect);	 
	  if (GameChars[z].status!=99)					/* if char there */
		{ 
		  SetCTitle(itemHandle,GameChars[z].name);
  			 ShowControl(itemHandle);	
  			 RGBForeColor(&gNormName);
		 		   GetDItem(gCastleDialog, z+12, &itemType, &itemHandle, &itemRect);
				 	if (GameChars[z].type==0)
				 	 	SetIText  ( itemHandle, "\pQueer");			
					if (GameChars[z].type==1)
				 	 	SetIText  ( itemHandle, "\pDrag Queen");
				 	if (GameChars[z].type==2)
						SetIText  ( itemHandle, "\pMuse");
					if (GameChars[z].type==3)
 						SetIText ( itemHandle, "\pGuppie");
					if (GameChars[z].type==4)
					 	SetIText ( itemHandle, "\pLesbian");
					if (GameChars[z].type==5)
					 	SetIText ( itemHandle, "\pMaster");
		RGBForeColor(&gSpecName);
				 	 if (GameChars[z].status==DEAD)
				 	 	SetIText(itemHandle,"\pDEAD");
				 	 else if (GameChars[z].status==PERMDEAD)
				 	 	SetIText(itemHandle,"\pPERM DEAD");
				 	 else if (GameChars[z].status==POISONED)
				 	 	SetIText(itemHandle,"\pPOISONED");
		RGBForeColor(&gBlack);
				GetDItem(gCastleDialog, z+20, &itemType, &itemHandle, &itemRect);
					NumToString(GameChars[z].level,text);
						SetIText(itemHandle,text);   
						GetDItem(gCastleDialog, z+28, &itemType, &itemHandle, &itemRect);
					NumToString(GameChars[z].hits,text);
				SetIText(itemHandle,text);   
		}
	   else
	   	{
			GetDItem(gCastleDialog, z+4, &itemType, &itemHandle, &itemRect);	 
		  		SetCTitle(itemHandle,"\popen");
	  		HiliteControl(itemHandle,255);	  /* grey it  */
	  	}
	}

for (z=0;z<=3;z++)							 /* check chars in party */					
 	{	
	  if (gDungParty[z] != 99)
	  	{
	  	GetDItem(gCastleDialog, 4+gDungParty[z], &itemType, &itemHandle, &itemRect);
			SetCtlValue (itemHandle,ON);
	  	 		GetDItem(gCastleDialog, 4+gDungParty[z], &itemType, &itemHandle, &itemRect);
			HiliteControl(itemHandle,255);	  /* grey it  */
	     	count++;						  /*  set counter  */ 
	     }
	}

	
for (z=3;z>=0;z--)
{
  if (gDungParty[z] != 99)				/*	 check last char */
  	{
  	 	GetDItem(gCastleDialog, 4+gDungParty[z], &itemType, &itemHandle, &itemRect);
		HiliteControl(itemHandle,0);	/* 	grey it */
		break;
	}
}	

while (dialogDone == FALSE)
	{
		ModalDialog(NIL_POINTER, &itemHit);
			switch (itemHit)
			{
				case 1:					/* LEAVE */
					dialogDone = TRUE;
					gPartyNum=count-1;	/* count = Number in party */
					break;
 	  	 		case 4:
  	  	 		case 5:
  	  	 		case 6:
  	  	 		case 7:
 	  	 		case 8:
  	  	 		case 9:
  	  	 		case 10:
  	  	 		case 11:
  	  	 				GetDItem(gCastleDialog, itemHit, &itemType, &itemHandle, &itemRect);
						status=GetCtlValue(itemHandle);
						 
						if (status == OFF && count <= 3)		/* button off , room for more */
							{
							  Click();
							  SetCtlValue (itemHandle,ON);
					 	    	  gDungParty[count]=itemHit-4;
					 	    		gCurChar=gDungParty[count];
					 	    	    gWhichChar=count; 
									ClearMessage();
									DrawString(GameChars[gCurChar].name); 
									DrawString("\p has joined your party.");
									SetPort(gCastleDialog);
							if (gDisplay==0)
								ListItems();
							else	 	    	  
					 	      UpdateStats();  
					 	      
					 	    if (count > 0)			/* if not first char */
					 	     {
		  	  	 				GetDItem(gCastleDialog,4+gDungParty[count-1], &itemType, 
									&itemHandle, &itemRect);
								HiliteControl(itemHandle,255);
							   }
					 	      count++;
							}	 
						 
						else if (status == ON)	/* button on */
							{
							  Click();
						 	  count--;
						 	     SetCtlValue (itemHandle,OFF);
					 	      	  gCurChar=gDungParty[count];
						 	      gWhichChar=count;
						 	      ClearBStats();ClearStats();
									ClearMessage();	
									DrawString(GameChars[gCurChar].name); DrawString("\p has left your party.");
									SetPort(gCastleDialog);
									RGBBackColor(&gStatsBack);						 	      
					 	      	gDungParty[count]=99;
					 	      if (count>=1)
					 	      	  {	
						 	          GetDItem(gCastleDialog, 4+gDungParty[count-1], &itemType, 
								    	&itemHandle, &itemRect);
									 	  HiliteControl(itemHandle,0);
									 	 gWhichChar=count-1;
										  gCurChar=gDungParty[count-1];
										if (gDisplay==0)
											ListItems();
										else
									  		UpdateStats();
								  }
							 }
	
						break;
				}
	}

	DeadChar();			/** sorts party **/
		UpdateAllNames();
	 		DisposeDialog(gCastleDialog);
	 	SetPort(gGameWindow);
	RGBForeColor(&gNormName);		
}