View Single Post
Dionys
Senior Member
Join Date: Sep 2008
Location: Terra, Russia
Old 06-04-2009 , 06:45   Re: [L4D] Game Mode Config Loader
Reply With Quote #14

good addition to the plugin - to add hook event convar change mp_gamemode and z_difficulty
for example:
Code:
public OnPluginStart()
{
    decl String:ModName[50];
    GetGameFolderName(ModName, sizeof(ModName));
    if (!StrEqual(ModName, "left4dead", false))
    {
        SetFailState("Use this Left 4 Dead only.");
    }

    g_hGameMode = FindConVar("mp_gamemode");        //coop, versus, survival
    g_hDifficulty = FindConVar("z_difficulty");        //Easy, Normal, Hard, Impossible
    
    g_hConVar_ConfigCoop_Easy = CreateConVar("l4d_autoexec_coop_easy", "coop_easy.cfg", "CFG file to execute when game mode is coop and the difficulty is Easy.", CVAR_FLAGS);
    g_hConVar_ConfigCoop_Normal = CreateConVar("l4d_autoexeccoop_normal", "coop_normal.cfg", "CFG file to execute when game mode is coop and the difficulty is Normal.", CVAR_FLAGS);
    g_hConVar_ConfigCoop_Hard = CreateConVar("l4d_autoexec_coop_hard", "coop_hard.cfg", "CFG file to execute when game mode is coop and the difficulty is Advanced.", CVAR_FLAGS);
    g_hConVar_ConfigCoop_Impossible = CreateConVar("l4d_autoexec_coop_impossible", "coop_impossible.cfg", "CFG file to execute when game mode is coop and the difficulty is Expert.", CVAR_FLAGS);
    g_hConVar_ConfigVersus = CreateConVar("l4d_autoexec_versus", "versus.cfg", "CFG file to execute when game mode is versus.", CVAR_FLAGS);
    g_hConVar_ConfigSurvival = CreateConVar("l4d_autoexec_survival", "survival.cfg", "CFG file to execute when game mode is survival.", CVAR_FLAGS);
    
    HookConVarChange(g_hGameMode, ConVarChange_GameMode);
    HookConVarChange(g_hDifficulty, ConVarChange_Difficulty);
}

public OnMapStart()
{
    AutoexecCFG();
}

public ConVarChange_GameMode(Handle:convar, const String:oldValue[], const String:newValue[])
{
    if (strcmp(oldValue, newValue) != 0)
    {
        AutoexecCFG();
    }
}

public ConVarChange_Difficulty(Handle:convar, const String:oldValue[], const String:newValue[])
{
    if (strcmp(oldValue, newValue) != 0)
    {
        AutoexecCFG();
    }
}

AutoexecCFG()
{
    decl String:sGameMode[32];
    decl String:sGameDifficulty[32];
    decl String:sConfigName[256];
    
    GetConVarString(g_hGameMode, sGameMode, sizeof(sGameMode));
    GetConVarString(g_hDifficulty, sGameDifficulty, sizeof(sGameDifficulty));

    if (StrEqual(sGameMode, "coop", false))
    {
        if (StrEqual(sGameDifficulty, "Easy", false))
        {
            GetConVarString(g_hConVar_ConfigCoop_Easy, sConfigName, sizeof(sConfigName));
            TrimString(sConfigName);
            ServerCommand("exec %s", sConfigName);
        }
        else if (StrEqual(sGameDifficulty, "Normal", false))
        {
            GetConVarString(g_hConVar_ConfigCoop_Normal, sConfigName, sizeof(sConfigName));
            TrimString(sConfigName);
            ServerCommand("exec %s", sConfigName);
        }
        else if (StrEqual(sGameDifficulty, "Hard", false))
        {
            GetConVarString(g_hConVar_ConfigCoop_Hard, sConfigName, sizeof(sConfigName));
            TrimString(sConfigName);
            ServerCommand("exec %s", sConfigName);
        }
        else if (StrEqual(sGameDifficulty, "Impossible", false))
        {
            GetConVarString(g_hConVar_ConfigCoop_Impossible, sConfigName, sizeof(sConfigName));
            TrimString(sConfigName);
            ServerCommand("exec %s", sConfigName);
        }
    }
    else if (StrEqual(sGameMode, "survival", false))
    {
        GetConVarString(g_hConVar_ConfigSurvival, sConfigName, sizeof(sConfigName));
        TrimString(sConfigName);
        ServerCommand("exec %s", sConfigName);
    }
    else if (StrEqual(sGameMode, "versus", false))
    {
        GetConVarString(g_hConVar_ConfigVersus, sConfigName, sizeof(sConfigName));
        TrimString(sConfigName);
        ServerCommand("exec %s", sConfigName);
    }
}
so need add check FileExists(ConfigFile):
for example:
Code:
...
new String:ConfigFile[PLATFORM_MAX_PATH];
...
FormatEx(ConfigFile, sizeof(ConfigFile), "cfg\\%s", sConfigName);
if(FileExists(ConfigFile))
{
      PrintToServer("[SM] Execute %s file", sConfigName);
      ServerCommand("exec %s", sConfigName);
}
...

Last edited by Dionys; 06-11-2009 at 05:55.
Dionys is offline