// guildrecruit() Let the guild members recruit some player into the guild.
// Checks the guild database if "to be recruited" player already in any other guild.
// puts a tag with players serial number into the guilds recruit database.
void cGuildStone::Recruit(UOXSOCKET s)
{

	if ( currchar[s]->guildstone() == INVALID_SERIAL ) 
	{
		sysmessage(s,"you are in no guild");
		return;
	}

	if(buffer[s][11]==0xFF && buffer[s][12]==0xFF && buffer[s][13]==0xFF && buffer[s][14]==0xFF) return; // check if user canceled operation - Morrolan
	int serial = calcserial(buffer[s][7],buffer[s][8],buffer[s][9],buffer[s][10]);
	P_CHAR pc = FindCharBySerial( serial );
	if(pc != NULL)
	{
			if (pc->guildstone() != INVALID_SERIAL) 
				sysmessage(s,"This person is already in a guild.");
			else 
			{
				if (pc->isPlayer())
				{
					this->recruit.push_back(pc->serial);
				} 
				else sysmessage(s,"This is not a player.");
			}
			//break;
		//} for
	}
	this->Menu(s,1);
	return;
}
// OKAY (but take another look)
// guildresign() : Player gets removed from the guilddatabase, and gets a message.
// Offcourse guilddatabase gets checked for members left, if everyone is gone, then vanish
// the guildstone. After Guildmaster resigns, the fealty of each remaining member calculates
// a new guildmaster, if there is a draw then we'll have no master until they change their minds ;)
void GuildResign(int s)
{

	P_CHAR pc = currchar[s];

	cGuildStone* pStone = dynamic_cast<cGuildStone*>(FindItemBySerial(pc->guildstone()));

	if (pStone == NULL)
	{
		sysmessage(s, "You are in no guild");
		return;
	}

	pStone->removeMember( currchar[s] );
	sysmessage(s,"You are no longer in that guild.");
	if ((pStone->ownserial == pc->serial) && (!pStone->member.empty()))
	{
		pStone->SetOwnSerial(INVALID_SERIAL);
		pStone->CalcMaster();
	}
	if (pStone->member.empty())
	{
		Items->DeleItem( pStone );
		sysmessage(s,"You have been the last member of that guild so the stone vanishes.");
	}
	return;
}
// TESTED: OKAY (fine fine.. now proove that it really works.. )
// guildtitle(viewing character socket, clicked character) displays players title string, over the name
// of clicked character, name color gets calculated from the guild relationship of both players
// Called by: textflags()
void GuildTitle(int s, P_CHAR pc_player2)
{
	char title[150];
	char abbreviation[5];
	char guildtype[10];
	int tl;

	if ( pc_player2 == NULL )
		return;

	if ( pc_player2->guildstone() != INVALID_SERIAL && pc_player2->guildtoggle() )
	{
		cGuildStone* pStone = dynamic_cast<cGuildStone*>(FindItemBySerial( pc_player2->guildstone() ));
		strcpy(abbreviation, pStone->abbreviation.c_str());
		
		if (!(strcmp(abbreviation,"")))
			strcpy(abbreviation,"none");
		switch (pStone->guildType)
		{
		case cGuildStone::order:	strcpy(guildtype, "Order");		break;
		case cGuildStone::chaos:	strcpy(guildtype, "Chaos");		break;
		default:													break;		
		}

		if (!pc_player2->guildtitle().isEmpty()) 
			sprintf(title,"[%s, %s] [%s]",pc_player2->guildtitle().latin1(),abbreviation,guildtype);
		else 
			sprintf(title,"[%s] [%s]",abbreviation, guildtype);

		tl=44+strlen(title)+1;
		talk[1]=tl>>8;
		talk[2]=tl%256;
		LongToCharPtr(pc_player2->serial, &talk[3]);
		talk[7]=1;
		talk[8]=1;
		talk[9]=0;
		ShortToCharPtr(pc_player2->emotecolor, &talk[10]);
		talk[12]=0;
		talk[13]=3;
		Xsend(s, talk, 14);
		Xsend(s, sysname, 30);
		Xsend(s, title, strlen(title)+1);
	}
int GuildCompare(P_CHAR player1, P_CHAR player2)
{
	if (player1 == NULL || player2 == NULL) return 0;

	// one of both not in a guild -> no guildwarfare
	if (player1->guildstone() == INVALID_SERIAL || player2->guildstone() == INVALID_SERIAL ) return 0;

	if (player1->guildstone() == player2->guildstone()) { return 1; }

	cGuildStone* pStone1 = dynamic_cast<cGuildStone*>(FindItemBySerial(player1->guildstone()));
	cGuildStone* pStone2 = dynamic_cast<cGuildStone*>(FindItemBySerial(player2->guildstone()));

	if ( pStone1->guildType != pStone2->guildType && pStone1->guildType != cGuildStone::standard && pStone2->guildType != cGuildStone::standard)
	{
		return 2;
	}

	unsigned int i;
	for (i = 0; i < pStone1->war.size(); ++i)
	{
		if ( find(pStone1->war.begin(), pStone1->war.end(), pStone2->serial) != pStone1->war.end() )
		{
			if ( find( pStone2->war.begin(), pStone2->war.end(), pStone1->serial ) != pStone2->war.end() )
			{
				return 2;
			}
		}
	}
	return 0;
}
// placeguildstone() : spawns a renamed gravestone at players location and removes deed
// Placer gets guildmaster, whatever he does ;)
// Force placer to give that damn guild a damn name :)
void StonePlacement(UOXSOCKET s)
{
	P_CHAR pc = currchar[s];
	P_ITEM pDeed = FindItemBySerial(pc->fx1);
	cGuildStone* pStone = NULL;

	if (CheckValidPlace(s)!=1)
	{
		sysmessage(s, "You cannot place guildstones at any other location than your house");
		return;
	}
	
	if (pDeed->id() == 0x14F0)
	{
		if (pc->guildstone() != INVALID_SERIAL)
		{
			itemmessage(s,"You are already in a guild.",pDeed->serial);
			return;
		}
		pStone = new cGuildStone;		
		if (!pStone)
		{//AntiChrist - to prevent crashes
			sysmessage(s, "Cannot create guildstone");
			return;
		}
		pStone->Init();
		pStone->setId(0x0ED5);
		pStone->setName( "Guildstone for an unnamed guild" );
		Items->GetScriptItemSetting(pStone);
		pc->setGuildstone( pStone->serial );
		if (pc->id() == 0x0191)	
			pc->setGuildtitle("Guildmistress");
		else
			pc->setGuildtitle("Guildmaster");

		pStone->webpage = DEFAULTWEBPAGE;
		pStone->charter = DEFAULTCHARTER;
		pStone->addMember( pc );
		pStone->guildType = cGuildStone::standard;
		pStone->moveTo(pc->pos);
		pStone->setType( 202 );
		pStone->priv = 0;
		pStone->setLockedDown();
		pStone->setOwnSerialOnly(pc->serial);

		RefreshItem(pStone);//AntiChrist
		Items->DeleItem(pDeed);
		entrygump(s, pc->serial,100,1,40,"Enter a name for the guild.");
	}
	else
	{
/*		guildnumber = SearchByStone(s);
		if (guildnumber==-1)
		{//AntiChrist
			sysmessage(s,"There are already enough guildstones placed.");
			return;
		}
		if (( pDeed->serial==guilds[guildnumber].stone &&
			pc->serial == guilds[guildnumber].master) ||
			pc->isGM() )
		{
			sprintf(stonename, "Guildstone for %s", guilds[guildnumber].name);
			pStone = Items->SpawnItem(currchar[s], 1, stonename, 0, 0x0ED5, 0, 0);
			if (!pStone)
			{
				sysmessage(s,"Cannot create guildstone");
				return;
			}
			pStone->MoveTo(pc->pos.x,pc->pos.y,pc->pos.z);
			pStone->type = 202;
			pStone->priv = 0;		
			RefreshItem(pStone);//AntiChrist
			Items->DeleItem(pDeed);
			pc->fx1 = INVALID_SERIAL;
			guilds[guildnumber].stone = pStone->serial;
		}
		else
			itemmessage(s,"You are not the guildmaster of this guild. Only the guildmaster may use this guildstone teleporter.",pDeed->serial);
*/	}
}