/* 
	PScript by Peter Pon
	
	scene14
*/
#include "..\PScriptLib\Def.h"

#include "..\PScriptLib\box.pst"
#include "..\PScriptLib\desk.pst"
#include "..\PScriptLib\chair.pst"
#include "..\PScriptLib\jug.pst"
int g_iw01OpenTime;
int g_iDoorWaitTime = 3000;
int DeathMatch = 1; 
int TeamDeathMatch = 5; 
int GameMod; 
int EventEnter = 200; 
int EventExit = 201;
int EventDeath = 202; 
int g_CharacterArena[99];
int g_ArenaCharacters[99];
int g_TeamAArenaCharacters[16];
int g_TeamBArenaCharacters[16];

Scene_OnInit()
{
	SetSceneItem("D_sn06w01", "pose", 0, 0);
	SetSceneItem("D_Item01", "name", "machine");
	SetSceneItem("D_Item01", "attribute", "collision", 1);
	SetSceneItem("D_dm01", "name", "machine");
	SetSceneItem("D_dm01", "attribute", "collision", 1);
	SetSceneItem("D_dm02", "name", "machine");
	SetSceneItem("D_dm02", "attribute", "collision", 1);
	InitBoxes(g_iNumBoxes);
	InitBBoxes(g_iNumBBoxes);
	InitChairs(g_iNumChairs);
	InitDeskes(g_iNumDeskes);
	InitJugs(g_iNumJugs);
	GameMod = GameCallBack("mod", 0);
	int i;
	string itemname;
	for (i=0; i<16; i=i+1)
	{
		g_CharacterArena[i] = -1;
		g_ArenaCharacters[i] = 0;
		g_TeamAArenaCharacters[i] = 0;
		g_TeamBArenaCharacters[i] = 0;
		
		MakeString(itemname, "D_tpEC", i+1);
		SetSceneItem(itemname, "name", "machine");
		SetSceneItem(itemname, "attribute", "collision", 0);
		SetSceneItem(itemname, "attribute", "interactive", 1);

		MakeString(itemname, "D_tpEC", i+17);
		SetSceneItem(itemname, "name", "machine");
		SetSceneItem(itemname, "attribute", "collision", 0);
		SetSceneItem(itemname, "attribute", "interactive", 1);
	}

        for (i=0; i<8; i=i+1)
	{
		MakeString(itemname, "D_tpAC", i+1);
		SetSceneItem(itemname, "name", "machine");
		SetSceneItem(itemname, "attribute", "collision", 0);

		MakeString(itemname, "D_tpAC", i+9);
		SetSceneItem(itemname, "name", "machine");
		SetSceneItem(itemname, "attribute", "collision", 0);
		
		MakeString(itemname, "D_tpBC", i+1);
		SetSceneItem(itemname, "name", "machine");
		SetSceneItem(itemname, "attribute", "collision", 0);

		MakeString(itemname, "D_tpBC", i+9);
		SetSceneItem(itemname, "name", "machine");
		SetSceneItem(itemname, "attribute", "collision", 0);
	}
}
Scene_OnCharacterEvent(int id, int event)
{
	int i;
	int arena;
	int team;
	Output("Character:", id, "event:", event);
	
	if ( event==EventExit || event==EventDeath )
	{
		if ( GameMod==DeathMatch )
		{
			arena = g_CharacterArena[id];
			if ( arena!=-1 )
			{
				g_ArenaCharacters[arena] = g_ArenaCharacters[arena];
			}
			g_CharacterArena[id] = -1;
		}
		if ( GameMod==TeamDeathMatch )
		{
			arena = g_CharacterArena[id];
			team = GetTeam(id);
			if ( arena!=-1 )
			{
				if ( team==1 )
				{
					g_TeamAArenaCharacters[arena] = g_TeamAArenaCharacters[arena] - 1;
				}
				if ( team==2 )
				{
					g_TeamBArenaCharacters[arena] = g_TeamBArenaCharacters[arena] - 1;
				}
			}
			g_CharacterArena[id] = -1;
		}
	}		
}
TransferToArena(int characterid, int tpid)
{
	string arenaname;
	int arena = tpid/2;
	Output("arena characters", g_ArenaCharacters[arena]);
	if ( g_ArenaCharacters[arena]<99)
	{
		g_ArenaCharacters[arena]=g_ArenaCharacters[arena];
		g_CharacterArena[characterid] = arena;
		MakeString(arenaname, "D_tpED", tpid+1);
		Misc("transfer", characterid, arenaname);
		Output("transfer", characterid, "to", tpid);
	}
}
TransferFromArena(int characterid, int tpid)
{
	string arenaname;
	int arena = tpid/2;

	if ( g_ArenaCharacters[arena]<99)
	{
		g_ArenaCharacters[arena]=g_ArenaCharacters[arena];
		g_CharacterArena[characterid] = -1;
		MakeString(arenaname, "D_tpED", tpid+17);
		Misc("transfer", characterid, arenaname);
		Output("transfer", characterid, "from", tpid);
	}
}
D_tpEC01_OnTouch(int id, int characterid) 
{ 
    Output("EC01 Touched"); 
    TransferToArena(characterid, 0); 
} 
D_tpEC02_OnTouch(int id, int characterid) 
{ 
    Output("EC02 Touched"); 
    TransferToArena(characterid, 1); 
} 
Scene_OnLoad()
{
	int i;
	string name;
	
	// stone step
	for ( i=1; i<=99; i++ )
	{
		MakeString(name, "D_sn06dm", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_sn06cq", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_sn06cmq", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_sn06q", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_sn06w", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_sn06db", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=28; i++ )
	{
		MakeString(name, "D_sn06wl", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=15; i++ )
	{
		MakeString(name, "D_sn06wll", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_sn06cf", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_sn06shu", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_sn06shuye", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 0);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_sn06tz", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_dx", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_ggp", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_guang", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 0);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_sn06huo", i);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 0);
		SetSceneItem(name, "attribute", "damage", 1);
		SetSceneItem(name, "attribute", "damagevalue", 50);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_kgk", i);
		SetSceneItem(name, "pose", 0, 0);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
	}
	for ( i=1; i<=999; i++ )
	{
		MakeString(name, "D_kgg", i);
		SetSceneItem(name, "pose", 0, 0);
		SetSceneItem(name, "name", "machine");
		SetSceneItem(name, "attribute", "collision", 1);
		SetSceneItem(name, "attribute", "active", 0);
	}
}
D_kgk01_OnAttack(int id, int character, int damage)
{ 
	NetEvent(1);
	SetSceneItem("D_dm01", "pose", 1, 0);
	SetSceneItem(id, "pose", 1, 0);
	SetSceneItem("D_kgg01", "attribute", "active", 1);
	SetSceneItem("D_kgk01", "attribute", "active", 0);
	NetEvent(0);
}
D_kgg01_OnAttack(int id, int character, int damage)
{ 
	NetEvent(1);
	SetSceneItem("D_dm01", "pose", 2, 0);
	SetSceneItem(id, "pose", 1, 0);
	SetSceneItem("D_kgg01", "attribute", "active", 0);
	SetSceneItem("D_kgk01", "attribute", "active", 1);
	NetEvent(0);
}
D_kgk02_OnAttack(int id, int character, int damage)
{ 
	int pose = GetSceneItem("D_sn06w01", "pose");
	if ( pose!=0 )
	{
		return 0;
	}
	g_iw01OpenTime = Misc("gettime");
	NetEvent(1);
	SetSceneItem("D_sn06w01", "pose", 1, 0);
	SetSceneItem(id, "pose", 1, 0);
	NetEvent(0);
}
D_kgk03_OnAttack(int id, int character, int damage)
{ 
	int pose = GetSceneItem("D_sn06w01", "pose");
	if ( pose!=0 )
	{
		return 0;
	}
	g_iw01OpenTime = Misc("gettime");
	NetEvent(1);
	SetSceneItem("D_sn06w01", "pose", 1, 0);
	SetSceneItem(id, "pose", 1, 0);
	NetEvent(0);
}
D_sn06w01_OnIdle(int id)
{
	int pose = GetSceneItem(id, "pose");
	if ( pose==0 )
	{
		return 0;
	}
	int state = GetSceneItem(id, "state");
	if ( pose==1 && state==3 )
	{
		int diff = Misc("gettime") - g_iw01OpenTime;
		if ( diff > g_iDoorWaitTime )
		{
			Output("Close Door");
			NetEvent(1);
			SetSceneItem(id, "pose", 2, 0);
			NetEvent(0);
		}
		return 1;
	}
	if ( pose==2 && state==3 )
	{
		NetEvent(1);
		SetSceneItem(id, "pose", 0, 0);
		NetEvent(0);
		return 1;
	}
}
D_dm01_OnAttack(int id, int character, int damage)
{   
	NetEvent(1);
	SetSceneItem("D_dm01", "pose", 1, 0);
	NetEvent(0);
} 