//===== eAthena Script =======================================
//= War of Emperium Guild Manager Function
//===== By: ==================================================
//= jAthena - kalen (1.0) & eAthena Team
//===== Current Version: =====================================
//= 1.9b
//===== Compatible With: =====================================
//= eAthena SVN; RO Episode 4+
//===== Description: =========================================
//= [ Aegis Conversion]
//= The Guild Manager allows the Guildmaster to invest in comerce
//= and defense, hire guardians and kafras, go to the treasure room,
//= and surrender the guild castle.
//==============================================
//= Break down of arguments used in the function:
//=   arg(0): name of Castle Manager
//=   arg(1): name of guild castle.
//=   arg(2): x1 coordinate for warp to treasure room
//=   arg(3): y1 coordinate for warp to treasure room
//=   arg(4): guild script suffix for kafra, Guardian scripts etc.
//===== Additional Comments: =================================
//= 1.31: Added support for Emsolute Develop [celest]
//= 1.2: All Guild manager scripts use this function. Optimized Comerce and Defense investment. [kobra_k88]
//= 1.2a Function now returns to script that called it. Added disablenpc line to surrender castle option to remove kafra upon surrender.[kobra_k88]
//= 1.2b U can't surrender the base during WOE [Lupus]
//= 1.2c Fixed issue of guardians hp not increasing upon defense investment.[kobra_k88]
//= 1.3 Now you can't install Guardians during WOE [Lupus]
//= 1.4 Remove surrender abbility (Was 100% custom as far as I can tell) [Kayla]
//= 1.41 Fixed possible Economy investment overflow with Emsolute Develop learnt [Lupus]
//= 1.5 Official Novice Castles Menu (u can't invest / hire guardians) [Lupus]
//= 1.6 According to recent info u can re-install Guardians during WoE [Lupus]
//= 1.6a Fix for guild manager recognizing [KarLaeda]
//= 1.6b Fixed the chance for double invest, now 50% instead of 49% [Lupus]
//= 1.7 Changed the names of the Kafra from "Service" to "Staff" [L0ne_W0lf]
//= 1.8 Rescripted to Aegis 10.3 standards. [L0ne_W0lf]
//=	This update changes quite a bit of the internal workings of the guild manager.
//=	No longer uses defined numerical values for guardian HP calculation
//=	Guardian summon display list is now DYNAMIC. Updated with guardian corrections.
//=	Added dialog for castle Abandoning. Commented out be default.
//= 1.9 Fixed guild members being able to access Guild Master services. [L0ne_W0lf]
//= 1.9a Fixed Kafra cutin not closing if you choose not to dismiss her. [L0ne_W0lf]
//= 1.9b Corrected impropper variable being used for defense investment. [L0ne_W0lf]
//============================================================

function	script	F_GldManager	{
	set .@GID, GetCastleData(getarg(1),1);

	// Define the types of guardians on a per castle basis
	// 1 - Soldier Guardian; 2 - Archer Guardian; 3 - Knight Guardian
	// Aldebaran (Luina)
	if (getarg(1) == "aldeg_cas01") setarray .@guardiantype[0],1,2,2,2,2,3,3,3;
	if (getarg(1) == "aldeg_cas02") setarray .@guardiantype[0],3,3,3,1,1,2,2,2;
	if (getarg(1) == "aldeg_cas03") setarray .@guardiantype[0],3,3,1,1,1,2,2,2;
	if (getarg(1) == "aldeg_cas04") setarray .@guardiantype[0],2,2,2,1,1,1,3,3;
	if (getarg(1) == "aldeg_cas05") setarray .@guardiantype[0],2,2,1,1,3,3,3,3;
	// Geffen (Britoniah)
	if (getarg(1) == "gefg_cas01") setarray .@guardiantype[0],1,1,1,2,2,3,3,3;
	if (getarg(1) == "gefg_cas02") setarray .@guardiantype[0],1,1,1,2,2,3,3,3;
	if (getarg(1) == "gefg_cas03") setarray .@guardiantype[0],2,2,1,1,1,3,3,3;
	if (getarg(1) == "gefg_cas04") setarray .@guardiantype[0],2,2,1,1,1,3,3,3;
	if (getarg(1) == "gefg_cas05") setarray .@guardiantype[0],2,2,1,1,1,3,3,3;
	// Payon (Baulder)
	if (getarg(1) == "payg_cas01") setarray .@guardiantype[0],2,2,2,2,2,2,2,3;
	if (getarg(1) == "payg_cas02") setarray .@guardiantype[0],2,2,2,2,2,2,2,3;
	if (getarg(1) == "payg_cas03") setarray .@guardiantype[0],2,2,2,2,2,2,2,3;
	if (getarg(1) == "payg_cas04") setarray .@guardiantype[0],2,2,2,2,2,2,2,3;
	if (getarg(1) == "payg_cas05") setarray .@guardiantype[0],2,2,2,2,2,2,2,3;
	// Prontera (Valkyrie Realms)
	if (getarg(1) == "prtg_cas01") setarray .@guardiantype[0],1,1,1,2,2,3,3,3;
	if (getarg(1) == "prtg_cas02") setarray .@guardiantype[0],3,3,3,1,1,2,2,2;
	if (getarg(1) == "prtg_cas03") setarray .@guardiantype[0],3,3,3,1,1,2,2,2;
	if (getarg(1) == "prtg_cas04") setarray .@guardiantype[0],3,3,3,1,1,1,2,2;
	if (getarg(1) == "prtg_cas05") setarray .@guardiantype[0],3,3,3,1,1,1,2,2;

	if (.@GID == 0){
		mes "[ "+getarg(0)+" ]";
		mes "I have been waiting for a master to fulfill my destiny.";
		mes "Brave soul... fate will guide you towards your future...";
		return 0;
	}
	if (getcharid(2) != .@GID || strcharinfo(0) != getguildmaster(.@GID)){
		mes "[ "+getarg(0)+" ]";
		mes "No matter how much you pester me, I'll still follow my master ^ff0000" + getguildmaster(.@GID) + "^000000. Where are Guardians?! Send these ruffians away right now!";
		return 0;
	}
	mes "[ "+getarg(0)+" ]";
	mes "Welcome. My honorable master, ^ff0000" + getguildmaster(.@GID) + "^000000...";
	mes "Your humble servent, "+getarg(0)+", is here to serve you.";
	next;

	// To allow abandoning of castles, uncomment the following switch,
	// comment out the switch below it, and uncomment case 7.
	//switch(select("Castle briefing:Invest in commercial growth:Invest in Castle Defenses:Summon Guardian:Employ / discharge a storehouse staff member:Go into Master's room:Abandon Castle")) {
	switch(select("Castle briefing:Invest in commercial growth:Invest in Castle Defenses:Summon Guardian:Employ / discharge a storehouse staff member:Go into Master's room")) {
	case 1:
		mes "[ "+getarg(0)+" ]";
		mes "I will report the Castle briefing, Master.";
		mes " ";
		mes " ^0000ffNow, commercial growth level is " + GetCastleData(getarg(1),2) + ".";
		if (GetCastleData(getarg(1),4) > 0)
			mes "You invested " + GetCastleData(getarg(1),4) + "times in last 1 day.";
		mes "Now, the Castle Defense level is " + GetCastleData(getarg(1),3) + ".^000000";
		if (GetCastleData(getarg(1),5) > 0)
			mes " ^0000ff- You invested " + GetCastleData(getarg(1),5) + "times in last 1 day.^000000";
		mes " ";
		mes "That's all to report, Master.";
		return 0;

	case 2:
		set .@Economy,GetCastleData(getarg(1),2);
		if(.@Economy < 8) set .@eco_invest,10000;
		if(.@Economy >= 8) set .@eco_invest,20000;
		if(.@Economy >= 16) set .@eco_invest,40000;
		if(.@Economy >= 25) set .@eco_invest,80000;
		if(.@Economy >= 34) set .@eco_invest,160000;
		if(.@Economy >= 44) set .@eco_invest,320000;
		if(.@Economy >= 54) set .@eco_invest,640000;
		if(.@Economy >= 65) set .@eco_invest,1280000;
		if(.@Economy >= 76) set .@eco_invest,2560000;
		if(.@Economy >= 88) set .@eco_invest,5120000;
		mes "[ "+getarg(0)+" ]";
		mes "If you invest in commercial growth, the quantity of goods made by the guild will increase. Therfore, if you consider our future, investments will be a necessity.";
		mes " ";
		mes "Initially, you are able to invest just once but if you pay more money, you will be able to invest twice.";
		if (.@Economy == 100) {
			mes " ";
			mes "^ff0000The commercial growth level of our Castle is at it's highest, 100%. No more investments are needed. Just as I have expected from a great economist like you, Master.^000000";
			return 0;
		}
		mes " ";
		if (GetCastleData(getarg(1),4) == 2) {
			mes "^ff0000You have already invested twice today. You cannot invest any more.^000000 I expect riches of the guild to grow at a high rate.";
			return 0;
		}
		else if (GetCastleData(getarg(1),4) == 0)
			mes "The current investment amount required is ^ff0000" + .@eco_invest + "^000000 zeny. Will you invest?";
		else
			mes "You have already invested once. Do you wish to invest one more time? ^ff0000" + .@eco_invest + "^000000 more zeny will be needed.";
		next;
		if (select("Invest in commercial growth:Cancel") == 1) {
			mes "[ "+getarg(0)+" ]";
			if (Zeny < .@eco_invest) {
				mes "I'm sorry but there is not enough zeny to invest in the reserves of guild. You have failed to invest, Master.";
				return 0;
			}
			set zeny,zeny-.@eco_invest;
			SetCastleData getarg(1),4,GetCastleData(getarg(1),4)+1;
			SetCastleData getarg(1),2,.@Economy + 1 + (.@Economy<99 && rand(2) && getgdskilllv(.@GID,10014));
			mes "We have finished investing safely. I expect in our coming days, the growth level will increase.";
			return 0;
		}
		mes "[ "+getarg(0)+" ]";
		mes "I'll do as you have instructed my master... There is no hurry. There is nothing we can not accomplish.";
		return 0;

	case 3:
		set .@Defence,GetCastleData(getarg(1),3);
		if(.@Defence < 8) set .@def_invest,20000;
		if(.@Defence >= 8) set .@def_invest,40000;
		if(.@Defence >= 16) set .@def_invest,80000;
		if(.@Defence >= 25) set .@def_invest,160000;
		if(.@Defence >= 34) set .@def_invest,320000;
		if(.@Defence >= 44) set .@def_invest,640000;
		if(.@Defence >= 54) set .@def_invest,1280000;
		if(.@Defence >= 65) set .@def_invest,2560000;
		if(.@Defence >= 76) set .@def_invest,5120000;
		if(.@Defence >= 88) set .@def_invest,10240000;
		mes "[ "+getarg(0)+" ]";
		mes "If you raise Castle Defenses, the durability of Guardians and the Emperium will increase. Therefore, if you consider our coming battles, some investment in this area will be required.";
		mes " ";
		mes "Originally you can invest just once but if you pay more money, you can invest twice.";
		if (.@Defence == 100) {
			mes " ";
			mes "^ff0000But the Castle Defense level of our Castle is at it's highest, 100%. No more investments are needed. Just as I have expected from a great strategist like you, Master.^000000";
			return 0;
		}
		mes " ";
		if (GetCastleData(getarg(1),5) == 2) {
			mes "^ff0000You have already invested twice today. You cannot invest any more.^000000 I expect the Defenses of the guild to grow at a high rate.";
			return 0;
		}
		else if (GetCastleData(getarg(1),5) == 1)
			mes "The current investment amount required is ^ff0000" + .@def_invest + "^000000 zeny. Will you invest?";
		else
			mes "You have already invested once. Do you wish to invest one more time? ^ff0000" + .@def_invest + "^000000 more zeny will be needed.";
		next;

		if (select("Invest in Castle Defenses.:Cancel") == 1) {
			mes "[ "+getarg(0)+" ]";
			if (Zeny < .@def_invest) {
				mes "I'm sorry but there is not enough zeny to invest in the reserves of guild. You have failed to invest, Master.";
				return 0;
			}
			set Zeny,Zeny-.@def_invest;
			SetCastleData getarg(1),5,GetCastleData(getarg(1),5)+1;
			SetCastleData getarg(1),3,.@Defence+1;
			mes "We have finished investing safely. I expect in our coming days, the growth level will increase.";
			return 0;
		}
		mes "[ "+getarg(0)+" ]";
		mes "I'll do as you have instructed my master... There is no hurry. There is nothing we can not accomplish.";
		return 0;

	case 4:
		mes "[ "+getarg(0)+" ]";
		mes "Do you still want to summon a Guardian? It'll be a protector to defend us to the end.";
		mes "Choose a Guardian to summon.";
		next;

		for( set .@i, 0; .@i <= 7 ; set .@i, .@i+1 )
		{
			if      (.@guardiantype[.@i] == 1) set .@type$,"Guardian Soldier";
			else if (.@guardiantype[.@i] == 2) set .@type$,"Guardian Archer";
			else                               set .@type$,"Guardian Knight";

			if( guardianinfo(getarg(1),.@i,0) )
				setarray .@name$[.@i], .@type$ + " - Implemented (" + guardianinfo(getarg(1),.@i,2) + "/" + guardianinfo(getarg(1),.@i,1) + ")";
			else
				setarray .@name$[.@i], .@type$ + " - Not Implemented";
		}
	
		set .@menu$,.@name$[0]+":"+.@name$[1]+":"+.@name$[2]+":"+.@name$[3]+":"+.@name$[4]+":"+.@name$[5]+":"+.@name$[6]+":"+.@name$[7];

		// this thing will propagate outside of this function
		switch(select(.@menu$)) {
		case 1: set @GDnum,10; break;
		case 2: set @GDnum,11; break;
		case 3: set @GDnum,12; break;
		case 4: set @GDnum,13; break;
		case 5: set @GDnum,14; break;
		case 6: set @GDnum,15; break;
		case 7: set @GDnum,16; break;
		case 8: set @GDnum,17; break;
		}

		mes "[ "+getarg(0)+" ]";
		mes "Do you wish to summon the chosen guardian? 10,000 zeny is required to summon it.";
		next;

		if (select("Summon:Cancel") == 1) {
			mes "[ "+getarg(0)+" ]";
			if (getgdskilllv(.@GID,10002) == 0) {
				mes "Master, we do not have any research materials to summon the Guardian. If you want to accumulate knowledge for Guardians, you have to learn the Guild skill first, We have failed to summon the Guardian.";
				return 0;
			}
			if (GetCastleData(getarg(1),@GDnum) == 1) {
				mes "Master, you have already have that Guardian. You cannot summon any more.";
				return 0;
			}
			if (Zeny < 1000) {
				mes "Well... I'm sorry but we don't have enough funds to summon the Guardian. We have failed to summon the Guardian.";
				return 0;
			}
			set zeny,zeny-10000;
			SetCastleData getarg(1),@GDnum,1; // mark as 'installed'
			// guardian will be spawned outside of this function
			mes "We have successfully summoned the Guardian. We now have our strong force to defend us!";
			return 1;

		}
		mes "[ "+getarg(0)+" ]";
		mes "I have done as you have commanded, but please remember if you have money to spare, it will be better if we use a Guardian.";
		return 0;

	case 5:
		if (GetCastleData(getarg(1),9) == 1) {
			mes "[ "+getarg(0)+" ]";
			mes "We are now hiring Kafra employee... Will you fire any current Kafra employee?";
			next;
			switch(select("Discharge:Cancel")) {
				cutin "kafra_01",2;
				mes "[ Hired Kafra Employee ]";
				mes "I worked so hard... How can you do that, Master?... Please... Please reconsider... Check it again, Master... Please...";
				next;
				switch(select("Discharge:Cancel")) {
				case 1:
					mes "[ Hired Kafra Employee ]";
					mes "Oh, my goodness! This is nonsense!";
					next;
					cutin "kafra_01",255;
					break;
				case 2:
					mes "[ Hired Kafra Employee ]";
					mes "I'll work hard for you... Thank you!";
					close2;					
					cutin "kafra_01",255;
					return 0;
				}
				break;
			case 2:
				mes "[ "+getarg(0)+" ]";
				mes "She worked hard in my opinion. It was a good decision to keep her.";
				return 0;
			}	
			disablenpc "Kafra Staff#"+getarg(4);
			SetCastleData getarg(1),9,0;
			mes "[ "+getarg(0)+" ]";
			mes "....";
			mes "I have discharged the Kafra employee... But... are you dissatisfied with the decision?";
			return 0;
		}
		else {
			mes "[ "+getarg(0)+" ]";
			mes "Will you contact Kafra head office to hire Kafra staff in our Castle?";
			mes "^ff0000 10,000 zeny is required for their services. ";
			next;
			if (select("Hire.:Cancel") == 1) {
				mes "[ "+getarg(0)+" ]";
				if (getgdskilllv(.@GID,10001) == 0) {
					mes "Master, we cannot hire Kafra staff because we have not established a contract with Kafra head office. If you want to establish a contract with Kafra head office, you have to learn Guild skill first.";
					return 0;
				}
				if (Zeny < 10000) {
					mes "[ "+getarg(0)+" ]";
					mes "Well... I'm sorry but we don't have enough funds to hire Kafra staff.";
					return 0;
				}
				set Zeny,Zeny-10000;
				enablenpc "Kafra Staff#"+getarg(4);
				SetCastleData getarg(1),9,1;
				mes "We have completed our task in establishing a contract with Kafra head office and hired Kafra staff.";
				next;
				cutin "kafra_01",2;
				mes "[ Hired Kafra Employee ]";
				mes "How do you do? I was dispatched from head office.";
				mes "I'll do my best to not tarnish reputation of the Guild.";
				next;
				cutin "kafra_01",255;
				mes "[ "+getarg(0)+" ]";
				mes "The Contract terms of the hired Kafra staff is for 1 month and after this term, you will need to to pay an additional charge.";
				mes "This will be good for our workers.";
				return 0;
			}
			mes "[ "+getarg(0)+" ]";
			mes "I've done as you have commanded, but for the sake of our workers, it would be better to hire Kafra staff immediately.";
			return 0;
		}

	case 6:
		mes "[ "+getarg(0)+" ]";
		mes "Do you wish to go to the Master's Room.";
		mes "That place is for only for you... no one else can enter the room.";
		next;
		if (select("Go into Master's room.:Cancel") == 1) {
			mes "[ "+getarg(0)+" ]";
			mes "I'll show you the secret path. Follow me...please.";
			mes "When you want to return here, please press the secret switch.";
			warp getarg(1),getarg(2),getarg(3);
			end;
		}
		mes "[ "+getarg(0)+" ]";
		mes "Goods are produced once a day... if you don't remove them in time, they will not be produced anymore.";
		mes "Therefore, it will be better if you check up on them from time to time.";
		return 0;

	//case 7:
	//	mes "[ "+getarg(0)+" ]";
	//	mes "Master!!";
	//	mes "Will you give up this Castle after all that we have all gone through?!";
	//	mes "If.. If this is your final decision, what does all the blood that has been shed mean to you!?";
	//	mes "Please reconsider! Master!!";
	//	next;
	//	if (select("Leave Agit.:Cancel") == 1) {
	//		mes "[ "+getarg(0)+" ]";
	//		mes "Master!! Please think it over, Master!!";
	//		mes "The blood we shed so far...don't you care for all the sacrifices that were made?!";
	//		next;
	//		if (select("Cancel:Leave Castle.") == 1) {
	//			mes "[ "+getarg(0)+" ]";
	//			mes "I knew you were kidding, Master!!";
	//			mes "Please, Master.. Although you were kidding, don't do that again. You really scared me for a moment.";
	//			return 0;
	//		}
	//		mes "[ "+getarg(0)+" ]";
	//		mes "Master!!.....";
	//		mes "...... Ma..s...ter.....";
	//		callfunc "F_GuildBreak",getarg(0),getarg(4);
	//		return 0;
	//	}
	//	mes "[ "+getarg(0)+" ]";
	//	mes "I knew you were kidding, Master!!";
	//	mes "Please, Master.. Although you were kidding, don't do that again. You really scared me for a moment.";
	//	return 0;
	}
}
