//===== eAthena Script =======================================
//= War of Emperium Guild Manager Function
//===== By: ==================================================
//= jAthena - kalen (1.0)
//= 1.1 by Akaru, ho|yAnge|X, and Valaris
//===== Current Version: =====================================
//= 1.41
//===== Compatible With: =====================================
//= eAthena 0.1+; RO Episode 4+
//===== Description: =========================================
//= 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, gaurdian scripts etc.
//===== Additional Comments: =================================
//= v1.31: Added support for Emsolute Develop [celest]
//= v1.2: All Guild manager scripts use this function. Optimized
//= Comerce and Defense investment. [kobra_k88]
//= v1.2a Function now returns to script that called it.
//= Added disablenpc line to surrender castle option to remove kafra
//= upon surrender.[kobra_k88]
//= v1.2b U can't surrender the base during WOE [Lupus]
//= v1.2c Fixed issue of guardians hp not increasing upon defense
//= investment.[kobra_k88]
//= v1.3 Now you can't install Guardians during WOE [Lupus]
//= v1.4 Remove surrender abbility (Was 100% custom as far as I can tell) [Kayla]
//= v1.41 Fixed possible Economy investment overflow with Emsolute Develop learnt [Lupus]
//============================================================



//==============================================
function	script	F_GldManager	{

	set @GID, GetCastleData(getarg(1)+".gat",1);
	if (strcharinfo(0) == getguildmaster(@GID)) goto L_Start;
	if (@GID == 0) goto L_NotOwn;
	if (getcharid(2) == @GID) goto L_Mem;

L_NotMem:
	mes "[ "+getarg(0)+" ]";
	mes "I am here to follow ^5533FF" + getguildmaster(@GID) + "^000000's command! Hey! Your not even a part of the guild!!";
	mes "Where are the guardians? Destroy these intruders!";
	return 0;

L_NotOwn:
	mes "[ "+getarg(0)+" ]";
	mes "I am waiting for my master.  Brave adventurer, follow your destiny!";
	return 0;

L_Mem:
	mes "[ "+getarg(0)+" ]";
	mes "You're not ^5533FF" + getguildmaster(@GID) + "^000000! I am here to follow ^5533FF" + getguildmaster(@GID) + "^000000's command only";
	return 0;

L_Start:
	mes "[ "+getarg(0)+" ]";
	mes "Welcome Master ^5533FF" + getguildmaster(@GID) + "^000000 ! I will assist you in any way I can!";
	next;
	menu 	"Guild Base Briefing",M_Base, "Commerce Investment",M_Comrc, "Defence Investment",M_Def, "Guardian Installation",M_Gaurd, 
		"Kafra Service Employment / Dismissal",M_Kaf, "Enter Treasure Room",M_Treas, "Cancel",M_End;

	//========================
	M_Base:
	//=========
		mes "[ "+getarg(0)+" ]";
		mes "Guild Base Investment Information.";
		mes " ";
		mes "Current Commerce Investment is : ^FF3322" + GetCastleData(getarg(1)+".gat",2) + "^000000 points.";
		mes "^0000ff - You have invested " + GetCastleData(getarg(1)+".gat",4) + " times today.^000000";
		next;
		mes "[ "+getarg(0)+" ]";
		mes "Current Defence Investment is : ^FF3322" + GetCastleData(getarg(1)+".gat",3)  + "^000000 points.";
		mes "^0000ff- You have invested " +  GetCastleData(getarg(1)+".gat",5)  + " times today.^000000";
		mes " ";
		mes "That is about it.";
		return 0;

	//========================
	M_Comrc:
	//=========
		set @TriggerE,GetCastleData(getarg(1)+".gat",4);
		set @Economy,GetCastleData(getarg(1)+".gat",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)+" ]";
		if(@TriggerE == 2) goto L_MaxTimesC;
		if(@Economy >= 100) goto L_MaxInvestC;
		mes "If you improve your Commerce Investment, the guild's productive power increases to produce more goods.";
		mes "So an investment will be required if you're considering future growth.";
		next;
		mes "[ "+getarg(0)+" ]";
		if(@TriggerE == 0) mes "You can invest up to two times a day, but the second time costs more";
		if(@TriggerE == 0) mes "The needed investment amount is ^5533FF" + @eco_invest + "^000000 zeny.";
		if(@TriggerE == 1) set @eco_invest,@eco_invest*4;
		if(@TriggerE == 1) mes "You've already invested once today, but you can invest again at ^5533FF" + @eco_invest + "^000000 Zeny.";
		next;
		mes "[ "+getarg(0)+" ]";
		mes "Would you like to invest?";
		next;
		menu "Invest Commerce.",-,"Cancel.",M_End;

			if(Zeny < @eco_invest) goto sL_NoZenyC;
			set Zeny,Zeny-@eco_invest;
			SetCastleData getarg(1)+".gat",4,@TriggerE+1;
			// if we learnt Emsolute Develop there's 50% chance to get +1 investment again
			SetCastleData getarg(1)+".gat",2,@Economy + 1 + (getgdskilllv(@GID,10014)>0 && rand(100)>50 && @Economy<99);
			mes "[ "+getarg(0)+" ]";
			mes "You have invested successfully.";
			return 0;

			sL_NoZenyC:
				mes "[ "+getarg(0)+" ]";
				mes "Master, you do not have enough money to invest.  Investment has been cancelled.";
				return 0;
		L_MaxTimesC:
			mes "^ff0000You have already invested twice today, and that's the limit.^000000 I'm expecting to see our riches grow at a high level.";
 			return 0;
		L_MaxInvestC:
			mes "[ "+getarg(0)+" ]";
			mes " ";
			mes "^ff0000This Castle's commerce investment is already maxed at 100 points. You don't have to invest any further.^000000";
	 		return 0;

	//=========================
	M_Def:
	//========
		set @TriggerD,GetCastleData(getarg(1)+".gat",5);
		set @Defence,GetCastleData(getarg(1)+".gat",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)+" ]";
		if(@TriggerD == 2) goto L_MaxTimesD;
		if(@Defence >= 100) goto L_MaxInvestD;
		mes "If you improve investment of defence, the durability of our Guardians and the Emperium will increase.";
		mes "So if you consider our future battles, an investment will be required.";
		next;
		mes "[ "+getarg(0)+" ]";
		if(@TriggerD == 0) mes "You can invest up to two times a day, but the second time costs more";
		if(@TriggerD == 0) mes "The needed investment amount is ^5533FF" + @def_invest + "^000000 zeny.";	
		if(@TriggerD == 1) set @def_invest,@def_invest*4;
		if(@TriggerD == 1) mes "You've already invested once today, but you can invest again at ^5533FF" + @def_invest + "^000000 Zeny.";
		next;
		mes "[ "+getarg(0)+" ]";
		mes "Would you like to invest?";
		next;
		menu "Invest Defence.",-, "Cancel",M_End;

			if(Zeny < @def_invest) goto sL_NoZenyD;
			set Zeny,Zeny-@def_invest;
			SetCastleData getarg(1)+".gat",5,@TriggerD+1;
			SetCastleData getarg(1)+".gat",3,@Defence+1;
			// set new hp values for guardians
			set @Defence, @Defence + 1;
			set @AGuardian, 28634 + (@Defence*2000);
			set @KGuardian, 30214 + (@Defence*2000);
			set @SGuardian, 15670 + (@Defence*2000);
			//set @AGuardian,strmobinfo(4,1285) + (@Defence*2000);
			//set @KGuardian,strmobinfo(4,1286) + (@Defence*2000);
			//set @SGuardian,strmobinfo(4,1287) + (@Defence*2000);
			if (GetCastleData(getarg(1)+".gat",10) == 1) SetCastleData getarg(1)+".gat",18,@SGuardian;
			if (GetCastleData(getarg(1)+".gat",11) == 1) SetCastleData getarg(1)+".gat",19,@SGuardian;
			if (GetCastleData(getarg(1)+".gat",12) == 1) SetCastleData getarg(1)+".gat",20,@SGuardian;
			if (GetCastleData(getarg(1)+".gat",13) == 1) SetCastleData getarg(1)+".gat",21,@AGuardian;
			if (GetCastleData(getarg(1)+".gat",14) == 1) SetCastleData getarg(1)+".gat",22,@AGuardian;
			if (GetCastleData(getarg(1)+".gat",15) == 1) SetCastleData getarg(1)+".gat",23,@KGuardian;
			if (GetCastleData(getarg(1)+".gat",16) == 1) SetCastleData getarg(1)+".gat",24,@KGuardian;
			if (GetCastleData(getarg(1)+".gat",17) == 1) SetCastleData getarg(1)+".gat",25,@KGuardian;

			mes "[ "+getarg(0)+" ]";
			mes "You have invested successfully.";
			return 0;

			sL_NoZenyD:
				mes "[ "+getarg(0)+" ]";
				mes "Master, you do not have enough money to invest in Defence.  Defence investment has been cancelled.";
				return 0;
		L_MaxTimesD:
			mes "^ff0000You have already invested twice today, and that's the limit.^000000 I'm expecting to see our riches grow at a high level.";
 			return 0;
		L_MaxInvestD:
			mes "^ff0000This Castle's Defence Investment is already maxed at 100 points. You don't have to invest any further.^000000";
	 		return 0;

	//=========================
	M_Gaurd:
	//=========
		if (getgdskilllv(@GID,10002) == 0) goto L_NoSkGuard;
		set @Defence,GetCastleData(getarg(1)+".gat",3);
		set @Guardian0,guardianinfo(0);
		set @Guardian1,guardianinfo(1);
		set @Guardian2,guardianinfo(2);
		set @Guardian3,guardianinfo(3);
		set @Guardian4,guardianinfo(4);
		set @Guardian5,guardianinfo(5);
		set @Guardian6,guardianinfo(6);
		set @Guardian7,guardianinfo(7);
		set @AGuardian, 28634 + (@Defence*2000);
		set @KGuardian, 30214 + (@Defence*2000);
		set @SGuardian, 15670 + (@Defence*2000);
		//set @AGuardian,strmobinfo(4,1285) + (@Defence*2000);
		//set @KGuardian,strmobinfo(4,1286) + (@Defence*2000);
		//set @SGuardian,strmobinfo(4,1287) + (@Defence*2000);

		mes "[ "+getarg(0)+" ]";
		if(agitcheck(0) != 0) goto L_CantGuard;
		mes "Would you like to install a guardian? Guardians will protect the guild base from enemies.";
		mes "Please choose a guardian.";
		next;
	
		menu "Soldier Guardian (" + @Guardian0 + "/" + @SGuardian + ")",L4_1,
			"Soldier Guardian (" + @Guardian1 + "/" + @SGuardian + ")",L4_2,
			"Soldier Guardian (" + @Guardian2 + "/" + @SGuardian + ")",L4_3,
			"Archer Guardian (" + @Guardian3 + "/" + @AGuardian + ")",L4_4,
			"Archer Guardian (" + @Guardian4 + "/" + @AGuardian + ")",L4_5,
			"Knight Guardian (" + @Guardian5 + "/" + @KGuardian + ")",L4_6,
			"Knight Guardian (" + @Guardian6 + "/" + @KGuardian + ")",L4_7,
			"Knight Guardian (" + @Guardian7 + "/" + @KGuardian + ")",L4_8;
	
		L4_1:
			if (GetCastleData(getarg(1)+".gat",10) == 1) goto L_GotGuard;
			set @GDnum,10;
			set @GDnum2,18;
			set @GuardianHP,@SGuardian;
			goto L4_9;
		L4_2:
			if (GetCastleData(getarg(1)+".gat",11) == 1) goto L_GotGuard;
			set @GDnum,11;
			set @GDnum2,19;
			set @GuardianHP,@SGuardian;
			goto L4_9;
		L4_3:
			if (GetCastleData(getarg(1)+".gat",12) == 1) goto L_GotGuard;
			set @GDnum,12;
			set @GDnum2,20;
			set @GuardianHP,@SGuardian;
			goto L4_9;
		L4_4:
			if (GetCastleData(getarg(1)+".gat",13) == 1) goto L_GotGuard;
			set @GDnum,13;
			set @GDnum2,21;
			set @GuardianHP,@AGuardian;
			goto L4_9;
		L4_5:
			if (GetCastleData(getarg(1)+".gat",14) == 1) goto L_GotGuard;
			set @GDnum,14;
			set @GDnum2,22;
			set @GuardianHP,@AGuardian;
			goto L4_9;
		L4_6:
			if (GetCastleData(getarg(1)+".gat",15) == 1) goto L_GotGuard;
			set @GDnum,15;
			set @GDnum2,23;
			set @GuardianHP,@KGuardian;
			goto L4_9;
		L4_7:
			if (GetCastleData(getarg(1)+".gat",16) == 1) goto L_GotGuard;
			set @GDnum,16;
			set @GDnum2,24;
			set @GuardianHP,@KGuardian;
			goto L4_9;
		L4_8:
			if (GetCastleData(getarg(1)+".gat",17) == 1) goto L_GotGuard;
			set @GDnum,17;
			set @GDnum2,25;
			set @GuardianHP,@KGuardian;
		L4_9:
			mes "[ "+getarg(0)+" ]";
			mes "Would you really like to install a guardian? You need ^5533FF10,000 zeny^000000 to install one....";
			next;
			menu "Install",-, "Cancel",M_End;

				if (Zeny < 10000) goto sL_NoZenyG;
				set Zeny,Zeny-10000;
				SetCastleData getarg(1)+".gat",@GDnum,1;
				SetCastleData getarg(1)+".gat",@GDnum2,@GuardianHP;
				return 1;

				sL_NoZenyG:
					mes "[ "+getarg(0)+" ]";
					mes "I'm sorry Master, but you do not have enough zeny for a Guardian.";
					return 0;
		L_NoSkGuard:
			mes "[ "+getarg(0)+" ]";
			mes "I'm sorry Master but you cannot install any guardians right now.  Your guild must learn the Guild skill ^5533FFGuardian Research^000000 first.";
			mes "Guardian Installation has been cancelled.";
			return 0;
		L_GotGuard:
			mes "[ "+getarg(0)+" ]";
			mes "Excuse me Master, but that guardian has already been installed.....";
			emotion 4;
			return 0;
		L_CantGuard:
			mes "Master.... don't you know that we can't install guardians during the War Of Emperium?!!";
			emotion 4;
			return 0;

	//===========================
	M_Kaf:
	//======
		mes "[ "+getarg(0)+" ]";
		if (GetCastleData(getarg(1)+".gat",9) == 1) goto L_Dismiss;
		if (getgdskilllv(@GID,10001) == 0) goto L_NoSkKaf;

		L_Hire:
			mes "Would you like to employ the services of a Kafra? You will need ^5533FF10,000 Zeny^000000 to do so... ";
			next;
			menu "Employ Kafra.",-,"Cancel",sM_KafEnd;

				mes "[ "+getarg(0)+" ]";
				if (Zeny < 10000) goto sL_NoZenyK;
				set Zeny,Zeny-10000;
				enablenpc "Kafra Service#"+getarg(4);
				SetCastleData getarg(1)+".gat",9,1;
				mes "You have created a contract with the Kafra Service Company.";
				next;
				cutin "kafra_01",2;
				mes "[ Kafra Service ]";
				mes "How do you do? I'm here to provide you with helpful service! I'll do the best I can to serve you.";
				next;
				cutin "kafra_01",255;
				mes "[ "+getarg(0)+" ]";
				//mes "Your employment contract lasts ^5533FF1 month^000000.  After this term is over you will have to create a new contract.";
				mes "I think the Kafra Service will benefit our guild members.";
				return 0;

				sL_NoZenyK:
					mes "Master, you do not have enough money to employ a Kafra. Employment has been cancelled.";
					return 0;
			sM_KafEnd:
				mes "[ "+getarg(0)+" ]";
				mes "As you wish Master.  But I suggest we get a Kafra as soon as possible!";
				return 0;

		L_Dismiss:
			mes "Would you like to dismiss the current Kafra?";
			next;
			menu "Dismissal",-,"Cancel",sM_KafEnd2;

				cutin "kafra_01",2;
				mes "[ Kafra Service ]";
				mes "Have I done anything wrong? If I did, will you please forgive me?";
				next;
				menu "Dismiss",-,"Cancel",ssM_KafEnd2;

					mes "[ Kafra Service ]";
					mes "It's unfortunate that I won't be able to serve your guild anymore....";
					next;
					disablenpc "Kafra Service#"+getarg(4);
					SetCastleData getarg(1)+".gat",9,0;
					cutin "kafra_01",255;
					mes "[ "+getarg(0)+" ]";
					mes "The Kafra has been dismissed.  But... we should really get a Kafra as soon as possible!";
					return 0;
				ssM_KafEnd2:
					mes "[ Kafra Service ]";
					mes "Thank you master, I'll do my best! ^^.";
					cutin "kafra_01",255;
					return 0;
			sM_KafEnd2:
				mes "[ "+getarg(0)+" ]";
				mes "Master, I think you should keep the current Kafra Service because she is already trying her best to serve us";
				return 0;

		L_NoSkKaf:
			mes "Master, you don't have a contract with the Kafra Service Company.";
			mes "In order to hire a Kafra, you must first learn the Guild skill ^5533FFContract With Kafra^000000.";
			return 0;

	//=========================
	M_Treas:
	//========
		mes "[ "+getarg(0)+" ]";
		mes "Would you to go to our Treasure Room? Only you, the Guild Master, are allowed to enter this room.";
		next;
		menu "Enter Treasure room.",-,"Cancel",sM_TresEnd;

			mes "[ "+getarg(0)+" ]";
			mes "Please follow me through the secret passage way.";
			mes "You must pull down on the secret switch in order to get out.";
			next;
			warp getarg(1)+".gat",getarg(2),getarg(3);
			return 0;
		sM_TresEnd:
			mes "[ "+getarg(0)+" ]";
			mes "The goods are produced everyday.";
			mes "You should get them whenever you can because they might dissapear if you take them at the wrong time.";
			return 0;


	//==========================
	M_End:
	//=======
		mes "[ "+getarg(0)+" ]";
		mes "As you wish, master.";
		return 0;
}
