#pragma semicolon 1
#define DEBUG
#define PLUGIN_AUTHOR "jenz"
#define PLUGIN_VERSION "1.00"
#define g_dLength 256
#define g_dIndex 64
#include <sourcemod>
#include <sdktools>
#include <unloze_zones>
#pragma newdecls required
static Handle g_hSpecialRoundCheck;
bool g_bSpecialMap;
bool g_bCheckedRound;
char g_cMapname[g_dIndex];
public Plugin myinfo = 
{
	name = "unloze racetimer specialmaps",
	author = PLUGIN_AUTHOR,
	description = "intended for adding racemaps based on levels and whatnot",
	version = PLUGIN_VERSION,
	url = "www.unloze.com"
};
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public APLRes AskPluginLoad2(Handle myself, bool late, char [] error, int err_max)
{
	g_hSpecialRoundCheck = CreateGlobalForward("CheckIfSpecialRoundZones", ET_Ignore, Param_String, Param_String);
	CreateNative("unloze_gBSpecialMapDisplay", Native_Isspecialmap);
	return APLRes_Success;
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public int Native_Isspecialmap(Handle handler, int numParams)
{
	CreateTimer(10.0, Timer_CheckStage, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
	GetCurrentMap(g_cMapname, sizeof(g_cMapname));
	if (StrEqual(g_cMapname, "ze_FFVII_Mako_Reactor_v5_3", false))
	{
		g_bSpecialMap = true;
		return view_as<bool>(1);
	}
	else
		g_bSpecialMap = false;
	return view_as<bool>(0);
}
//----------------------------------------------------------------------------------------------------
// Purpose: http://git.unloze.com/UNLOZE/sm-plugins/src/master/MakoVote/scripting/MakoVote.sp
//----------------------------------------------------------------------------------------------------
public int GetCurrentStage()
{
	int iLevelCounterEnt = FindEntityByTargetname(INVALID_ENT_REFERENCE, "Level_Counter", "math_counter");
	if (iLevelCounterEnt == -1)
		return 0;
	int offset = FindDataMapInfo(iLevelCounterEnt, "m_OutValue");
	int iCounterVal = RoundFloat(GetEntDataFloat(iLevelCounterEnt, offset));
	return iCounterVal;
}
//----------------------------------------------------------------------------------------------------
// Purpose: http://git.unloze.com/UNLOZE/sm-plugins/src/master/MakoVote/scripting/MakoVote.sp
//----------------------------------------------------------------------------------------------------
public int FindEntityByTargetname(int entity, const char[] sTargetname, const char[] sClassname)
{
	if (sTargetname[0] == '#') // HammerID
	{
		int HammerID = StringToInt(sTargetname[1]);

		while((entity = FindEntityByClassname(entity, sClassname)) != INVALID_ENT_REFERENCE)
		{
			if(GetEntProp(entity, Prop_Data, "m_iHammerID") == HammerID)
				return entity;
		}
	}
	else // Targetname
	{
		int Wildcard = FindCharInString(sTargetname, '*');
		char sTargetnameBuf[g_dIndex];

		while((entity = FindEntityByClassname(entity, sClassname)) != INVALID_ENT_REFERENCE)
		{
			if(GetEntPropString(entity, Prop_Data, "m_iName", sTargetnameBuf, sizeof(sTargetnameBuf)) <= 0)
				continue;

			if(strncmp(sTargetnameBuf, sTargetname, Wildcard) == 0)
				return entity;
		}
	}
	return INVALID_ENT_REFERENCE;
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void OnPluginEnd()
{
	CloseHandle(g_hSpecialRoundCheck);
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	//hooks
	HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public Action Timer_CheckStage(Handle timer, any userid) 
{
	if (g_bCheckedRound)
	{
		g_bCheckedRound = false;
		if (!g_bSpecialMap || GetCurrentStage() != 11)
		{
			Call_StartForward(g_hSpecialRoundCheck);
			Call_PushString("");
			Call_PushString("");
			Call_Finish();
			return;
		}
		int l_iZoneCount = unloze_zoneCount();
		char l_cIndexName[g_dIndex][g_dLength];
		int i;
		Call_StartForward(g_hSpecialRoundCheck);
		while (i < l_iZoneCount)
		{
			ZoneNameBasedOnIndex(i, l_cIndexName[i]);
			if (StrContains(l_cIndexName[i], "ZZ") > -1)
			{
				Call_PushString(l_cIndexName[i +1]);
			}
			else
				Call_PushString(l_cIndexName[i]);
			i++;
		}
		Call_Finish();
	}
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void Event_RoundStart(Handle event, const char[] name, bool dontBroadcast)
{
	g_bCheckedRound = true;
}