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

#include "..\PScriptLib\box.pst"
#include "..\PScriptLib\desk.pst"
#include "..\PScriptLib\chair.pst"
#include "..\PScriptLib\jug.pst"

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()
{
	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] - 1;
			}
			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);
}