//===== eAthena Script =======================================
//= War of Emperium Guild Treasure Room Functions
//===== By: ==================================================
//= holyAngelX (1.0) Akaru and ho|yAnge|X (1.1)
//===== Current Version: =====================================
//= 2.0
//===== Compatible With: =====================================
//= eAthena 1+; RO Episode 4+
//===== Description: =========================================
//= F_GldTreas spawns treasure chests used by the guild master.
//= F_GldTreasSw allows the player to get out of the treasure room.
//= F_TreasProtect removes non-guild master players from treasure room.
//==============================================
//= Break down of arguments used in the F_GldTreas:
//=   arg(0): name of guild castle
//=   arg(1): name of script that called the function
//=   arg(2): the box number amount
//=   arg(3): temp variable (count)
//=   arg(4): temp variable (box/monster id#)
//=   arg(5): box/monster id#
//=   arg(6): x1 coordinate for areamonster call
//=   arg(7): y1 coordinate for areamonster call
//=   arg(8): x2 coordinate for areamonster call
//=   arg(9): y1 coordinate for areamonster call
//=   
//= Break down of arguments used in the F_GldTreasSw:
//=   arg(0): name of guild castle.
//=   arg(1): x1 coordinate for warp back to guild castle
//=   arg(2): y1 coordinate for warp back to guild castle
//
//= Break down of arguments used in the F_TreasProtect:
//=   arg(0): name of guild castle.
//=   arg(1): name of warp-to city 
//=   arg(2): x1 coordinate for warp-to city
//=   arg(3): y1 coordinate for warp-to city
//===== Additional Comments: =================================
//= 1.2 Treasure room Spawn, and Treasure room Switch scripts now use these functions.[kobra_k88]
//= 1.2a Function now returns to script that called it.  Removed TreasureSpawn2.
//= Changed back to using specific global variables for number of boxes and the box id. [kobra_k88]
//= 1.2b Added a check to allow un broken treasure chests to respawn after map server restart.[kobra_k88]
//= 1.3 Fixed treasure boxes spawn. (Unrolled one loop a bit) [Lupus]
//= 1.4 New number of Treasure Boxes per castle: 25 at 100 Economic pts [Lupus]
//=	So you get your first chest only when your Economic Pts >= 4
//= 1.5 Fixed treasure number 'round exploit' [Lupus]
//= 1.6 to Aegis X.2 formula 4..24 Treasure Chests [Lupus]
//= 1.7 Box Count fix by Zoc. Now it spawns 1st/2nd Treasure Chest 50%/50% [Lupus]
//= 1.8 Official Treasure Spawn in Novice Castles. Had to add +1 in odd/even formula to leave correct ID of NG Treasure Box 8) [Lupus]
//= 1.9 Added F_TreasProtect to remove players who are not guild master from treasure room. [L0ne_W0lf]
//= 2.0 Updated F_GldTreas, no longer uses gotos, swapped out for a 'for' loop. [L0ne_W0lf]
//============================================================


//================================================
// Treasure Spawning Function
//================================================
function	script	F_GldTreas	{
	if(getarg(10) !=  1) {
		setcastledata getarg(0),4,0;
		setcastledata getarg(0),5,0;

		// Why on earth are we killing old treasure chest spawns?
		//killmonster getarg(0),"Treasure_"+getarg(1)+"::OnDied";

		// Don't spawn treasures if Castle is empty, or Eco is greater than 100
		if(GetCastleData(getarg(0),2) > 100 || GetCastleData(getarg(0),1) == 0) return;

		// Only spawn one treasure chest for notice castles.
		if (compare(getarg(0),"nguild"))
			set getarg(2),1;
		else
			set getarg(2),GetCastleData(getarg(0),2)/5+4;

		if (getarg(2) <= 0) return;

		//sets the counter variable = to the box number amount
		set getarg(3), getarg(2);
	}
	for (set .@i,1; .@i <= getarg(3) ; set .@i,.@i+1) {
		// set treasure box ID
		set getarg(4), getarg(5) + (.@i+1) % 2;
		areamonster getarg(0),getarg(6),getarg(7),getarg(8),getarg(9),"Treasure Chest",getarg(4),1,"Treasure_"+getarg(1)+"::OnDied";
	}
	return;
}

//==============================================================
//  Treasure Room Switch 
//===============================================================
function	script	F_GldTreasSw	{
	mes " ";
	mes "There's a small lever. Will you pull it? ";
	next;

	if (select("Pull.:Do not.") == 1) {
		warp getarg(0),getarg(1),getarg(2);
		return;
	}
	close;
}

//==============================================================
//  Treasure Room Protrection
//===============================================================
function	script	F_TreasProtect	{
	if (strcharinfo(0) != getguildmaster( GetCastleData(getarg(0),1) )) warp getarg(1),getarg(2),getarg(3);
}
