#pragma semicolon 1
#define PLUGIN_AUTHOR "jenz"
#define g_dLength 400
#define PLUGIN_VERSION "1.0"
#pragma newdecls required

#include <sourcemod>
#include <sdktools>
#include <cstrike>
#include <sdkhooks>

bool g_b_is_icecap_act2 = false;

public Plugin myinfo = 
{
	name = "icecap_crash_fix_from_roundend",
	author = PLUGIN_AUTHOR,
	description = "solves parenting and game_Ui deactivating on roundend",
	version = PLUGIN_VERSION,
	url = "www.unloze.com"
};
//----------------------------------------------------------------------------------------------------
// Purpose: 
//----------------------------------------------------------------------------------------------------

public void OnMapStart()
{
    char map[PLATFORM_MAX_PATH];
    GetCurrentMap(map, PLATFORM_MAX_PATH);
    g_b_is_icecap_act2 = StrEqual(map, "ze_icecap_escape_act2_b3s_fix", false);
}

public void OnPluginStart()
{
    OnMapStart();
    //this makes absolutely no sense, there is no point whatso ever in fucking doing this. 
    //but without this i get the complaint [SM] Plugin maps/test_icecap.smx failed to load: Unable to load plugin (invalid method code end).
    //is really makes absolutely zero sense why this would be needed
    for (int i = 0; i < MaxClients; i++)
    {
        if (IsValidClient(i))
        {
            OnClientPostAdminCheck(i);
        }
    }
    AddCommandListener(Command_ztele, "ztele");
    HookEventEx("round_end", OnRoundEnd, EventHookMode_Pre);
    HookEntityOutput("trigger_teleport", "OnTrigger", trigger_teleport);
    HookEntityOutput("trigger_teleport", "OnStartTouch", trigger_teleport);
}

public Action Command_ztele(int client, const char[] Command, int Args)
{
    if (g_b_is_icecap_act2)
    {
        if (IsValidClient(client))
        {
            int userid = GetClientUserId(client);
            ServerCommand("sm_forceinputplayer #%i Clearparent", userid);
        }
    }
}

public void OnClientPostAdminCheck(int client)
{
}

stock bool IsValidClient(int client)
{
    if (client > 0 && client <= MaxClients && IsClientConnected(client) && IsClientInGame(client) && !IsFakeClient(client))
        return true;
    return false;
}

//right before round end deactivate gameUI and unparent players from moving vehicles.
public void OnRoundEnd(Handle event, const char[] name, bool dontBroadcast)
{
    if (g_b_is_icecap_act2)
    {
        //its the correct map if it contains icecap_escape_act2
        ServerCommand("sm_forceinput Game Deactivate; sm_forceinput Game2 Deactivate;sm_forceinput Game3 Deactivate;sm_forceinput Game4 Deactivate; sm_forceinput Gameleft Deactivate; sm_forceinput Gameright Deactivate; sm_forceinput Gameseat Deactivate; sm_forceinput Gameseat2 Deactivate; sm_forceinput Gameseat3 Deactivate; sm_forceinput Gameseat4 Deactivate; sm_forceinput Gameseat5 Deactivate; sm_forceinput Gameseat6 Deactivate;");
        ServerCommand("sm_forceinput player Clearparent;");
        ServerCommand("sm_forceinput heli kill; sm_forceinput moblie kill; sm_forceinput car kill; sm_forceinput battery kill;");
    }
}

public void trigger_teleport(const char[] output, int entity_index, int client, float delay)
{
    if (g_b_is_icecap_act2)
    {
        if (IsValidClient(client))
        {
            int userid = GetClientUserId(client);
            ServerCommand("sm_forceinputplayer #%i Clearparent", userid);
        }
    }
}
