#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;
Handle g_hTimer_check_stage = null;
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)
{
    GetCurrentMap(g_cMapname, sizeof(g_cMapname));
    return view_as<bool>(StrEqual(g_cMapname, "ze_FFVII_Mako_Reactor_v5_3", false));
}
//----------------------------------------------------------------------------------------------------
// Purpose: http://git.unloze.com/UNLOZE/sm-plugins/src/master/MakoVote/scripting/MakoVote.sp
//----------------------------------------------------------------------------------------------------
public int GetCurrentStage()
{
	int iLevelCounterEnt = FindEntityByTargetname(INVALID_ENT_REFERENCE, "LevelCounter", "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()
{
    if (g_hSpecialRoundCheck != null)
        delete g_hSpecialRoundCheck;
    if (g_hTimer_check_stage != null)
        delete g_hTimer_check_stage;
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
    g_hTimer_check_stage = CreateTimer(10.0, Timer_CheckStage, _, TIMER_REPEAT);

    //hooks
    HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public Action Timer_CheckStage(Handle timer, any userid) 
{
    GetCurrentMap(g_cMapname, sizeof(g_cMapname));
    if (StrEqual(g_cMapname, "ze_FFVII_Mako_Reactor_v5_3", false) && g_bCheckedRound)
    {
        g_bCheckedRound = false;
        // == 11 means its racemode on mako
        if (GetCurrentStage() == 11)
        {
            int l_iZoneCount = unloze_zoneCount();
            char l_cIndexName[g_dIndex][g_dLength];
            int i;
            //ZONE_PREFIX_MAKO_START_1
            //ZONE_PREFIX_MAKO_RACE_2
            Call_StartForward(g_hSpecialRoundCheck);
            while (i < l_iZoneCount)
            {
                ZoneNameBasedOnIndex(i, l_cIndexName[i]);
                Call_PushString(l_cIndexName[i]);
                i++;
            }
            Call_Finish();
        }
    }
}
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------
public void Event_RoundStart(Handle event, const char[] name, bool dontBroadcast)
{
	g_bCheckedRound = true;
}
