// TESTED: OKAY (i think so, but ugly -> redo it!)
// guildmastercalc(guildnumber) counts all fealty settings of all members of 'guildnumber' and sets
// a new guildmaster if there is a draw then there will be no master, til the next check ;)
void cGuildStone::CalcMaster()
{

	std::map<unsigned int, unsigned int> votes; // Key is member serial and data #votes
	
	unsigned int i;
	for ( i = 0; i < member.size(); ++i)
	{
		P_CHAR pc = FindCharBySerial( member[i] );
		votes[pc->guildfealty()]++;
	}

/*	struct maxVotes : public binary_function< pair<unsigned int, unsigned int>, pair<unsigned int, unsigned int>, bool>
	{
		operator(pair<unsigned int, unsigned int> a, pair<unsigned int, unsigned int> b) 
		{ 
			return (a.second < b.second);
		}
	};*/

	std::map<unsigned int, unsigned int>::iterator it = max_element(votes.begin(), votes.end(), votes.value_comp());

	unsigned int currenthighest = it->first;
	unsigned int currenthighestvotes = it->second;
	votes.erase( it );
	// check for draw;
	it = max_element(votes.begin(), votes.end(), votes.value_comp());
	bool draw =  ( it->second == currenthighestvotes );

	if (!draw)
		this->master = currenthighest;
}
// guildstonemenu() : Opens the guild menu for a player
// Recognizes Guildmaster with the owner fields of the stone.
// Ofcourse checks for membership before opening any gump ;)
void cGuildStone::Menu(UOXSOCKET s, int page)
{
	//int total,i, counter,guild,recruit,war,member;
	int counter = 1;
	int lentext;
	int gumpnum = 0;
	char guildfealty[60],guildt[16],toggle[6];
	static char mygump[MAXMEMRECWAR][257];

	P_CHAR pc = currchar[s];

	if (!isMember( pc ) && !pc->isGM())
	{
		sysmessage(s, "You are not a member of this guild. Ask an existing guildmember to invite you into this guild.");
		return;
	}

	strcpy(guildfealty, "yourself");
	if ((pc->guildfealty() != pc->serial)&&(pc->guildfealty() != INVALID_SERIAL))
	{
		vector<SERIAL>::iterator it = find(member.begin(), member.end(), pc->guildfealty());
		if ( it != member.end())
		{
			strcpy(guildfealty, FindCharBySerial(*it)->name.c_str());
		}
	}
	else 
		pc->setGuildfealty( pc->serial );
	if (this->ownserial == INVALID_SERIAL) 
		CalcMaster();
	
	P_CHAR guildmaster = FindCharBySerial(this->ownserial);

	switch (this->guildType) 
	{
		case standard:		strcpy(guildt, " Standard");	break;
		case order:			strcpy(guildt, "n Order");		break;
		case chaos:			strcpy(guildt, " Chaos");		break;
	}
	
	if (pc->guildtoggle()) 
		strcpy(toggle, "On");
	else
		strcpy(toggle, "Off");

	// our prefix is 0xFE (darkstorm)
	gmprefix[7] = 0xFE;

	switch(page)
	{
	case 1:
		gumpnum=9;
		gmprefix[8] = 1;

		if (guildmaster<0) return;

		lentext = sprintf(mygump[0], "%s (%s %s)", this->guildName.c_str(), guildmaster->guildtitle().latin1(), guildmaster->name.c_str());
		strcpy(mygump[1],"Recruit someone into the guild.");
		strcpy(mygump[2],"View the current roster.");
		strcpy(mygump[3],"View the guild's charter.");
		sprintf(mygump[4],"Declare your fealty. You are currently loyal to %s.",guildfealty);
		sprintf(mygump[5],"Toggle showing the guild's abbreviation in your name to unguilded people. Currently %s.",toggle);
		strcpy(mygump[6],"Resign from the guild.");
		strcpy(mygump[7],"View list of candidates who have been sponsored to the guild.");
	    if ((pc->serial == this->ownserial)|| (pc->isGM()))							// Guildmaster Access?
		{															
			gumpnum=10;
			gmprefix[8] = 0;
			sprintf(mygump[8],"Access %s functions.", guildmaster->guildtitle().latin1());
			sprintf(mygump[9],"View list of guild that %s has declared war on.", this->guildName.c_str());
			sprintf(mygump[10],"View list of guilds that have declared war on %s.",this->guildName.c_str());
		} else {													// Normal Members access!
			sprintf(mygump[8],"View list of guilds that %s have declared war on.",this->guildName.c_str());
			sprintf(mygump[9],"View list of guilds that have declared war on %s.",this->guildName.c_str());
		}
		break;
	case 2:														// guildmaster menu
		gumpnum=14;
		lentext = sprintf(mygump[0], "%s, %s functions", this->guildName.c_str(), guildmaster->guildtitle().latin1());
		strcpy(mygump[1], "Set the guild name.");
		strcpy(mygump[2], "Set the guild's abbreviation.");
		sprintf(mygump[3], "Change the type of the guild. (Currently a%s guild.)",guildt);
		strcpy(mygump[4], "Set the guild's charter.");
		strcpy(mygump[5], "Dismiss a member.");
		strcpy(mygump[6], "Declare war from menu.");
		strcpy(mygump[7], "Declare war from targeting enemy.");
		strcpy(mygump[8], "Declare peace.");
		strcpy(mygump[9], "Accept a candidate seeking membership.");
		strcpy(mygump[10],"Refuse a candidate seeking membership.");
		strcpy(mygump[11],"Set the guildmaster's title.");
		strcpy(mygump[12],"Grant a title to another member.");
		strcpy(mygump[13],"Move this guildstone.");
		strcpy(mygump[14],"Return to the main menu.");

		gmprefix[8] = 2;
		break;
	case 3:														// guild type
		gumpnum=4;
		lentext=sprintf(mygump[0], "Please select the type you want your guild to be related to.");
		strcpy(mygump[1], "Select this to return to main menu.");
		strcpy(mygump[2], "Set to Standard.");
		strcpy(mygump[3], "Set to Order.");
		strcpy(mygump[4], "Set to Chaos.");
		gmprefix[8] = 3;
		break;
	case 4:														// edit charter
		gumpnum=3;
		lentext=sprintf(mygump[0], "Set %s charter.", this->guildName.c_str());
		strcpy(mygump[1], "Select this to return to main menu.");
		strcpy(mygump[2], "Set the charter.");
		strcpy(mygump[3], "Set the webpage.");
		gmprefix[8] = 4;
		break;
	case 5:														// view charter
		gumpnum=2;
		lentext=sprintf(mygump[0], "%s charter.", this->guildName.c_str());
		sprintf(mygump[1], "%s. Select this to return to the main menu.", this->charter.c_str());
		sprintf(mygump[2], "Visit the guild website at %s", this->webpage.c_str());
		gmprefix[8] = 5;
		break;
	case 6:
	{														// Candidates list
		gumpnum = this->recruit.size();
		lentext=sprintf(mygump[0], "%s list of candidates.", this->guildName.c_str());
		sprintf(mygump[1], "Select this to return to the menu.");
		unsigned int i;
		for (i = 0; i < recruit.size(); ++i)
		{
			strcpy(mygump[counter++], FindCharBySerial(this->recruit[i])->name.c_str());
		}
		gmprefix[8] = 6;
		break;
	}
	case 7:
	{														// roster
		gumpnum = this->member.size();
		lentext=sprintf(mygump[0], "%s members roster.", this->guildName.c_str());
		strcpy(mygump[1], "Select this to return to the menu.");
		counter=1;
		unsigned int i;
		for (i = 0; i < member.size(); ++i)
		{
			counter++;
			strcpy(mygump[counter], FindCharBySerial(this->member[i])->name.c_str());
		}
		gmprefix[8] = 7;
		break;
	}
	case 8:
	{													// member dismiss
		gumpnum = this->member.size();
		lentext=sprintf(mygump[0], "Dismiss what member?");
		strcpy(mygump[1], "Select this to return to the menu.");
		counter=1;
		unsigned int i;
		for ( i = 0; i < member.size(); ++i)
		{
			counter++;
			strcpy(mygump[counter], FindCharBySerial(this->member[i])->name.c_str());
		}
		gmprefix[8] = 8;
		break;
	}
	case 9:	
	{													// Refuse Candidates
		gumpnum = this->recruit.size();
		lentext=sprintf(mygump[0], "Refuse what candidate? %s", this->guildName.c_str());
		strcpy(mygump[1], "Select this to return to the menu.");
		counter=1;
		unsigned int i;
		for ( i = 0; i < recruit.size(); ++i)
		{
			counter++;
			strcpy(mygump[counter], FindCharBySerial(this->recruit[i])->name.c_str());
		}
		gmprefix[8] = 9;
		break;
	}
	case 10:														// Accept Candidates
	{
		gumpnum = this->recruit.size();
		lentext=sprintf(mygump[0], "Accept what candidate?.%s", this->guildName.c_str());
		strcpy(mygump[1], "Select this to return to the menu.");
		counter=1;
		unsigned int i;
		for ( i = 0; i < recruit.size(); ++i)
		{
			counter++;
			strcpy(mygump[counter], FindCharBySerial(this->recruit[i])->name.c_str());
		}
		gmprefix[8] = 10;
		break;
	}
	case 11:														// War list
	{
		gumpnum = this->war.size();
		lentext=sprintf(mygump[0], "Guild that %s has declared war on.", this->guildName.c_str());
		strcpy(mygump[1], "Select this to return to the menu.");
		counter=1;
		unsigned int i;
		for ( i = 0; i < war.size(); ++i )
		{
			counter++;
			cGuildStone* pStone = dynamic_cast<cGuildStone*>(FindItemBySerial(this->war[i]));
			strcpy(mygump[counter], pStone->guildName.c_str());
		}
		gmprefix[8] = 11;
		break;
	}
	case 12:
	{														// grant title
		gumpnum = this->member.size();
		lentext=sprintf(mygump[0], "Grant a title to whom?");
		strcpy(mygump[1], "Select this to return to the menu.");
		counter = 1;
		unsigned int i;
		for (i = 0; i < this->member.size(); ++i)
		{
			counter++;
			strcpy(mygump[counter], FindCharBySerial(this->member[i])->name.c_str());
		}
		gmprefix[8] = 12;
		break;
	}
	case 13:	
	{													// fealty
		gumpnum = this->member.size();
		lentext = sprintf(mygump[0], "Whom do you wish to be loyal to?");
		strcpy(mygump[1], "Select this to return to the menu.");
		counter=1;
		unsigned int i;
		for (i = 0; i < this->member.size(); ++i)
		{
			counter++;
			strcpy(mygump[counter], FindCharBySerial(this->member[i])->name.c_str());
		}
		
		gmprefix[8] = 13;
		break;
	}
	case 14:														// declare War list
	{
		gumpnum=1;
		lentext=sprintf(mygump[0], "What guilds do you with to declare war?");
		strcpy(mygump[1], "Select this to return to the menu.");
		counter=1;
		list<SERIAL>::iterator it;
		for (it = guilds.begin(); it != guilds.end(); ++it)
		{
			if ( !this->free && this->serial != *it )
			{
				unsigned int i;
				for (i = 0; i < this->war.size(); ++i)
				{
					if (this->war[i] == *it) 
					{
						++gumpnum;
						P_ITEM pStone = FindItemBySerial(*it);
						strcpy(mygump[++counter], pStone->name().ascii());
					}
				}
			}
		}

		gmprefix[8] = 14;
		break;
	}
	case 15:														// declare peace list
	{
		gumpnum = this->war.size();
		lentext = sprintf(mygump[0], "What guilds do you with to declare peace?");
		strcpy(mygump[1], "Select this to return to the menu.");
		counter = 1;
		unsigned int i;
		for (i = 0; i < this->war.size(); ++i)
		{
			if ( this->war[i] != INVALID_SERIAL)
			{
				P_ITEM pStone = FindItemBySerial( this->war[i] );
				strcpy(mygump[++counter], pStone->name().ascii());
			}
		}
		gmprefix[8] = 15;
		break;
	}
	case 16:														// War list 2
		{
		gumpnum=1;
		lentext=sprintf(mygump[0], "Guilds that have decalred war on %s.", this->name().ascii());
		strcpy(mygump[1], "Select this to return to the menu.");
		counter=1;
		list<SERIAL>::iterator it;
		for (it = guilds.begin(); it != guilds.end(); ++it)
		{
			if ((*it) != INVALID_SERIAL)
			{
				unsigned int i;
				for ( i = 0; i < this->war.size(); ++i)
				{
					if (this->war[i] == *it)
					{
						++gumpnum;
						P_ITEM pStone = FindItemBySerial(*it);
						strcpy(mygump[++counter], pStone->name().ascii() );
					}
				}
			}
		}
		gmprefix[8] = 16;
		break;
		}
	}
	
	int total=9+1+lentext+1;
	int i;
	for (i = 1; i <= gumpnum; i++)
	{
		total+=4+1+strlen(mygump[i]);
	}
	gmprefix[1] = total>>8;
	gmprefix[2] = total%256;
	LongToCharPtr(pc->serial, &gmprefix[3]);
	Xsend(s, gmprefix, 9);
	Xsend(s, &lentext, 1);
	Xsend(s, mygump[0], lentext);
	lentext = gumpnum;
	Xsend(s, &lentext, 1);
	for (i = 1; i <= gumpnum; i++)
	{
		gmmiddle[0]=0;
		gmmiddle[1]=0;
		Xsend(s,gmmiddle,4);
		lentext=strlen(mygump[i]);
		Xsend(s,&lentext,1);
		Xsend(s,mygump[i],lentext);
	}
	return;
}