cssdm_ctrl.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jun 25 23:30:11 2007 +0000 (2007-06-25)
changeset 8 fdcccf33a7cf
parent 0 67dcd379a2d5
child 12 f3fedcdb9bd4
permissions -rw-r--r--
fixed translation file not being loaded
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * By David "BAILOPAN" Anderson
     6  * All rights reserved.
     7  * ===============================================================
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or (at
    12  * your option) any later version.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but
    15  * WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    17  * General Public License for more details.
    18  * 
    19  * You should have received a copy of the GNU General Public License
    20  * along with this program; see the file COPYING; if not, write to the
    21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
    22  * MA 02110-1301 USA
    23  * 
    24  * Version: $Id$
    25  */
    26 
    27 #include "cssdm_headers.h"
    28 #include "cssdm_config.h"
    29 #include "cssdm_ctrl.h"
    30 #include "cssdm_ffa.h"
    31 #include "cssdm_callbacks.h"
    32 
    33 extern ConVar cssdm_enabled;
    34 extern ConVar cssdm_ffa_enabled;
    35 extern bool g_IsLoadedOkay;
    36 
    37 bool g_IsRunning = false;
    38 bool g_IsMapStarted = false;
    39 bool g_HasConfigRan = false;
    40 
    41 void DM_StartEverything()
    42 {
    43 	if (!g_IsMapStarted || g_HasConfigRan)
    44 	{
    45 		return;
    46 	}
    47 
    48 	g_HasConfigRan = true;
    49 
    50 	/* If we're not running but we should be, start up now.
    51 	 * g_IsRunning should never be true here.
    52 	 */
    53 	if (!g_IsRunning && cssdm_enabled.GetInt() == 1)
    54 	{
    55 		/* We should be enabled! */
    56 		DM_Enable();
    57 	}
    58 }
    59 
    60 void OnLevelInitialized()
    61 {
    62 	if (!g_IsLoadedOkay)
    63 	{
    64 		return;
    65 	}
    66 
    67 	g_IsMapStarted = true;
    68 	engine->ServerCommand("exec cssdm/cssdm.cfg\n");
    69 
    70 	char map_cmd[128];
    71 	snprintf(map_cmd, sizeof(map_cmd), "exec cssdm/%s.cfg\n", STRING(gpGlobals->mapname));
    72 	engine->ServerCommand(map_cmd);
    73 	
    74 	engine->ServerCommand("cssdm internal 1\n");
    75 }
    76 
    77 void OnLevelEnd()
    78 {
    79 	/* We fully disable CS:S DM when the map ends.
    80 	 * This is so our DM_Enable call is properly paired, but
    81 	 * also so CGameRules hooks get flushed.
    82 	 */
    83 	if (g_IsRunning)
    84 	{
    85 		DM_Disable();
    86 	}
    87 
    88 	g_IsMapStarted = false;
    89 	g_HasConfigRan = false;
    90 }
    91 
    92 bool DM_Disable()
    93 {
    94 	if (!g_IsLoadedOkay || !g_IsRunning)
    95 	{
    96 		return false;
    97 	}
    98 
    99 	g_IsRunning = false;
   100 
   101 	/* If FFA is enabled, we must unpatch it. */
   102 	if (DM_FFA_IsPatched())
   103 	{
   104 		DM_Unpatch_FFA();
   105 	}
   106 
   107 	DM_OnShutdown();
   108 
   109 	return true;
   110 }
   111 
   112 bool DM_Enable()
   113 {
   114 	if (!g_IsLoadedOkay || g_IsRunning || !g_IsMapStarted || !g_HasConfigRan)
   115 	{
   116 		return false;
   117 	}
   118 
   119 	g_IsRunning = true;
   120 
   121 	DM_OnStartup();
   122 
   123 	/* If FFA should be enabled, patch it. */
   124 	if (cssdm_ffa_enabled.GetInt() != 0
   125 		&& DM_FFA_IsPrepared()
   126 		&& !DM_FFA_IsPatched())
   127 	{
   128 		DM_Patch_FFA();
   129 	}
   130 
   131 	return true;
   132 }
   133 
   134 CON_COMMAND(cssdm, "CS:S DM console menu")
   135 {
   136 	int args = engine->Cmd_Argc();
   137 	if (args < 2)
   138 	{
   139 		/* :TODO: display menu */
   140 		return;
   141 	}
   142 
   143 	const char *arg = engine->Cmd_Argv(1);
   144 	if (strcmp(arg, "internal") == 0)
   145 	{
   146 		if (args < 3)
   147 		{
   148 			return;
   149 		}
   150 		int num = atoi(engine->Cmd_Argv(2));
   151 		if (num == 1)
   152 		{
   153 			DM_StartEverything();
   154 		}
   155 	}
   156 }