scripting/dm_equipment.sp
author Ruben Gonzalez <drifter01620@gmail.com>
Sun Oct 20 14:55:58 2013 -0400 (2013-10-20)
changeset 158 f388630a66df
parent 92 52cc55d67b2f
child 172 f7f4cb013eaf
permissions -rw-r--r--
Fix mac build
     1 /**
     2  * dm_equipment.sp
     3  * Adds preset spawning to CS:S DM.
     4  * This file is part of CS:S DM, Copyright (C) 2005-2007 AlliedModders LLC
     5  * by David "BAILOPAN" Anderson, http://www.bailopan.net/cssdm/
     6  *
     7  * This program is free software; you can redistribute it and/or
     8  * modify it under the terms of the GNU General Public License
     9  * as published by the Free Software Foundation; either version 2
    10  * of the License, or (at your option) any later version.
    11  *
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  * 
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program; if not, write to the Free Software
    19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    20  *
    21  * Version: $Id$
    22  */
    23 
    24 #pragma semicolon 1
    25 #include <sourcemod>
    26 #include <sdktools>
    27 #include <cssdm>
    28 
    29 #define CSSDM_GUNMENU_NO		0
    30 #define CSSDM_GUNMENU_YES		1
    31 #define CSSDM_GUNMENU_RANDOM	2
    32 
    33 /** Plugin Stuff */
    34 new Handle:cssdm_enable_equipment;					/** cssdm_enable_equipment cvar */
    35 new g_ArmorOffset = -1;								/* m_ArmorValue offset */
    36 new g_NVOffset = -1;								/* m_bHasNightVision offset */
    37 new g_HealthOffset = -1;							/* m_iHealth offset */
    38 new Handle:g_SpawnTimers[MAXPLAYERS+1];				/* Post-spawn timers */
    39 new Handle:g_hPrimaryMenu = INVALID_HANDLE;			/* Priamry menu Handle */
    40 new Handle:g_hSecondaryMenu = INVALID_HANDLE;		/* Secondary menu Handle */
    41 new Handle:g_hEquipMenu = INVALID_HANDLE;			/* Main equipment menu */
    42 new bool:g_IsEnabled = false;						/* Whether the plugin should work */
    43 new g_PrimaryChoices[MAXPLAYERS+1];					/* Primary weapon selections */
    44 new g_SecondaryChoices[MAXPLAYERS+1];				/* Secondary weapon selections */
    45 new bool:g_GunMenuEnabled[MAXPLAYERS+1];			/* Whether the gun menu is enabled */
    46 new bool:g_GunMenuAvailable[MAXPLAYERS+1];			/* Whether the gun menu is available */
    47 new bool:g_bIsGo = false;							/* Are we running on CS:GO? */
    48 
    49 /** HUMAN CONFIGS */
    50 new g_PrimaryList[CSSDM_MAX_WEAPONS];
    51 new g_SecondaryList[CSSDM_MAX_WEAPONS];
    52 new g_PrimaryCount = 0;
    53 new g_SecondaryCount = 0;
    54 new g_PrimaryMenu = CSSDM_GUNMENU_YES;
    55 new g_SecondaryMenu = CSSDM_GUNMENU_YES;
    56 new bool:g_AllowBuy = false;
    57 new g_ArmorAmount = 100;
    58 new bool:g_Helmets = true;
    59 new g_Flashes = 0;
    60 new bool:g_Smokes = false;
    61 new bool:g_HEs = false;
    62 new bool:g_NightVision = false;
    63 new bool:g_DefuseKits = true;
    64 new bool:g_AllowGunCommand = true;
    65 new g_HealthAmount = 100;
    66 //CSGO
    67 new bool:g_Decoy = false;
    68 new bool:g_Taser = true;
    69 
    70 /** BOT CONFIGS */
    71 new g_BotPrimaryList[CSSDM_MAX_WEAPONS];
    72 new g_BotSecondaryList[CSSDM_MAX_WEAPONS];
    73 new g_BotPrimaryCount = 0;
    74 new g_BotSecondaryCount = 0;
    75 new g_BotArmor = 100;
    76 new bool:g_BotHelmets = false;
    77 new g_BotFlashes = 0;
    78 new bool:g_BotSmokes = false;
    79 new bool:g_BotHEs = false;
    80 new bool:g_BotDefuseKits = true;
    81 new g_BotHealthAmount = 100;
    82 //CSGO
    83 new bool:g_BotDecoy = false;
    84 new bool:g_BotTaser = true;
    85 
    86 /** PUBLIC INFO */
    87 public Plugin:myinfo = 
    88 {
    89 	name = "CS:S DM Equipment",
    90 	author = "AlliedModders LLC",
    91 	description = "Adds gun menu/equipment to CS:S DM",
    92 	version = CSSDM_VERSION,
    93 	url = "http://www.bailopan.net/cssdm/"
    94 };
    95 
    96 /******************
    97  * IMPLEMENTATION *
    98  ******************/
    99  
   100 public OnPluginStart()
   101 {
   102 	new String:game[64];
   103 	GetGameFolderName(game, sizeof(game));
   104 	if (StrEqual(game, "csgo", false))
   105 	{
   106 		g_bIsGo = true;
   107 	}
   108 	
   109 	LoadTranslations("cssdm.phrases");
   110 	
   111 	RegConsoleCmd("say", Command_Say);
   112 	RegConsoleCmd("say_team", Command_Say);
   113 	
   114 	cssdm_enable_equipment = CreateConVar("cssdm_enable_equipment", "1", "Sets whether the equipment plugin is enabled");
   115 	HookConVarChange(cssdm_enable_equipment, OnEquipmentEnableChange);
   116 	
   117 	g_ArmorOffset = FindSendPropOffs("CCSPlayer", "m_ArmorValue");
   118 	g_NVOffset = FindSendPropOffs("CCSPlayer", "m_bHasNightVision");
   119 	g_HealthOffset = FindSendPropOffs("CCSPlayer", "m_iHealth");
   120 	
   121 	g_hEquipMenu = CreateMenu(Menu_EquipHandler, MenuAction_DrawItem|MenuAction_DisplayItem);
   122 	SetMenuTitle(g_hEquipMenu, "Weapon Options:");
   123 	SetMenuExitButton(g_hEquipMenu, false);
   124 	AddMenuItem(g_hEquipMenu, "", "New weapons");
   125 	AddMenuItem(g_hEquipMenu, "", "Same weapons");
   126 	AddMenuItem(g_hEquipMenu, "", "Same weapons every time");
   127 	AddMenuItem(g_hEquipMenu, "", "Random weapons");
   128 	AddMenuItem(g_hEquipMenu, "", "Random weapons every time");
   129 }
   130 
   131 public OnEquipmentEnableChange(Handle:convar, const String:oldValue[], const String:newValue[])
   132 {
   133 	g_IsEnabled = StringToInt(newValue) ? true : false;
   134 }
   135 
   136 public OnConfigsExecuted()
   137 {
   138 	LoadDefaults();
   139 	
   140 	if ((g_IsEnabled = LoadConfigFile("cfg/cssdm/cssdm.equip.txt")) == false)
   141 	{
   142 		LogError("[CSSDM] Could not find equipment file \"%s\"", "cfg/cssdm/cssdm.equip.txt");
   143 		return;
   144 	}
   145 	
   146 	if (!GetConVarInt(cssdm_enable_equipment))
   147 	{
   148 		g_IsEnabled = false;
   149 		return;
   150 	}
   151 	
   152 	/** See if there is a map version */
   153 	decl String:map[64];
   154 	decl String:path[255];
   155 	GetCurrentMap(map, sizeof(map));
   156 	Format(path, sizeof(path), "cfg/cssdm/maps/%s.equip.txt", map);
   157 	
   158 	if (FileExists(path))
   159 	{
   160 		LoadConfigFile(path);
   161 	}
   162 }
   163 
   164 public Action:DM_OnClientDeath(client)
   165 {
   166 	if (g_SpawnTimers[client] != INVALID_HANDLE)
   167 	{
   168 		KillTimer(g_SpawnTimers[client]);
   169 		g_SpawnTimers[client] = INVALID_HANDLE;
   170 	}
   171 	g_GunMenuAvailable[client] = true;
   172 }
   173 
   174 public OnClientPutInServer(client)
   175 {
   176 	g_GunMenuEnabled[client] = true;
   177 	g_GunMenuAvailable[client] = true;
   178 	g_PrimaryChoices[client] = -1;
   179 	g_SecondaryChoices[client] = -1;
   180 }
   181 
   182 public OnClientDisconnect(client)
   183 {
   184 	if (g_SpawnTimers[client] != INVALID_HANDLE)
   185 	{
   186 		KillTimer(g_SpawnTimers[client]);
   187 		g_SpawnTimers[client] = INVALID_HANDLE;
   188 	}
   189 }
   190 
   191 public DM_OnClientSpawned(client)
   192 {
   193 	if (!ShouldRun())
   194 	{
   195 		return;
   196 	}
   197 	
   198 	if (!IsFakeClient(client))
   199 	{
   200 		if (g_ArmorAmount > 0)
   201 		{
   202 			GivePlayerItem(client, "item_assaultsuit");
   203 			GivePlayerItem(client, "item_assaultsuit");
   204 			SetClientArmor(client, g_ArmorAmount);
   205 		}
   206 		if (g_HealthAmount > 0)
   207 		{
   208 			SetClientHealth(client, g_HealthAmount);
   209 		}
   210 		if (g_Helmets)
   211 		{
   212 			GivePlayerItem(client, "item_kevlar");
   213 			GivePlayerItem(client, "item_kevlar");
   214 		}
   215 		GiveGrenades(client, g_Flashes, g_HEs, g_Smokes, g_Decoy);
   216 		if (g_NightVision)
   217 		{
   218 			GiveNightVision(client);
   219 		}
   220 		if (g_bIsGo && g_Taser)
   221 		{
   222 			GivePlayerItem(client, "weapon_taser");
   223 		}
   224 	}
   225 	else
   226 	{
   227 		/* See if we'll need to strip this bot */
   228 		if (g_BotSecondaryCount || g_BotPrimaryCount)
   229 		{
   230 			DM_StripBotItems(client);
   231 		}
   232 		if (g_BotArmor > 0)
   233 		{
   234 			GivePlayerItem(client, "item_assaultsuit");
   235 			GivePlayerItem(client, "item_assaultsuit");
   236 			SetClientArmor(client, g_BotArmor);
   237 		} else {
   238 			/* Make sure bot didn't buy armor */
   239 			SetClientArmor(client, 0);
   240 		}
   241 		if (g_BotHealthAmount > 0)
   242 		{
   243 			SetClientHealth(client, g_BotHealthAmount);
   244 		}
   245 		if (g_BotHelmets)
   246 		{
   247 			GivePlayerItem(client, "item_kevlar");
   248 			GivePlayerItem(client, "item_kevlar");
   249 		}
   250 		if (g_bIsGo && g_BotTaser)
   251 		{
   252 			GivePlayerItem(client, "weapon_taser");
   253 		}
   254 		GiveGrenades(client, g_BotFlashes, g_BotHEs, g_BotSmokes, g_BotDecoy);
   255 	}
   256 	if (!g_AllowBuy && g_SpawnTimers[client] == INVALID_HANDLE)
   257 	{
   258 		/* Small delay - we want to avoid ResetHUD */
   259 		g_SpawnTimers[client] = CreateTimer(0.1, PlayerPostSpawn, client);
   260 	}
   261 }
   262 
   263 public Action:PlayerPostSpawn(Handle:timer, any:client)
   264 {
   265 	g_SpawnTimers[client] = INVALID_HANDLE;
   266 	
   267 	if (!ShouldRun())
   268 	{
   269 		return Plugin_Stop;
   270 	}
   271 	
   272 	if (IsFakeClient(client))
   273 	{
   274 		if (g_BotSecondaryCount > 0)
   275 		{
   276 			new index;
   277 			if (g_BotSecondaryCount == 1)
   278 			{
   279 				index = g_BotSecondaryList[0];
   280 			} else {
   281 				index = GetRandomInt(0, g_BotSecondaryCount-1);
   282 				index = g_BotSecondaryList[index];
   283 			}
   284 			decl String:classname[64];
   285 			DM_GetWeaponClassname(index, classname, sizeof(classname));
   286 			GivePlayerItem(client, classname);
   287 		}
   288 		if (g_BotPrimaryCount > 0)
   289 		{
   290 			new index;
   291 			if (g_BotPrimaryCount == 1)
   292 			{
   293 				index = g_BotPrimaryList[0];
   294 			} else {
   295 				index = GetRandomInt(0, g_BotPrimaryCount-1);
   296 				index = g_BotPrimaryList[index];
   297 			}
   298 			decl String:classname[64];
   299 			DM_GetWeaponClassname(index, classname, sizeof(classname));
   300 			GivePlayerItem(client, classname);
   301 		}
   302 		if (g_BotDefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
   303 		{
   304 			GivePlayerItem(client, "item_defuser");
   305 		}
   306 	} else {
   307 		if (g_DefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
   308 		{
   309 			GivePlayerItem(client, "item_defuser");
   310 		}
   311 		
   312 		new numGiven = 0;
   313 		/* First, check if we should only be giving one of something automatically */
   314 		if ((g_SecondaryMenu == CSSDM_GUNMENU_RANDOM || g_SecondaryMenu == CSSDM_GUNMENU_YES)
   315 			&& g_SecondaryCount == 1)
   316 		{
   317 			GiveSecondary(client, 0);
   318 			numGiven++;
   319 		} else if (g_PrimaryMenu == CSSDM_GUNMENU_RANDOM && g_PrimaryCount > 1) {
   320 			GivePrimary(client, g_PrimaryCount);
   321 			numGiven++;
   322 		}
   323 		
   324 		if ((g_PrimaryMenu == CSSDM_GUNMENU_RANDOM || g_PrimaryMenu == CSSDM_GUNMENU_YES)
   325 			&& g_PrimaryCount == 1)
   326 		{
   327 			GivePrimary(client, 0);
   328 			numGiven++;
   329 		} else if (g_SecondaryMenu == CSSDM_GUNMENU_RANDOM && g_SecondaryCount > 1) {
   330 			GiveSecondary(client, g_SecondaryCount);
   331 			numGiven++;
   332 		}
   333 		
   334 		/* If we've already given two weapons, there is no need for a menu. */
   335 		if (numGiven == 2)
   336 		{
   337 			return Plugin_Stop;
   338 		}
   339 		
   340 		if (g_GunMenuEnabled[client])
   341 		{
   342 			DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
   343 		} else {
   344 			GiveBothFromChoices(client);
   345 		}
   346 	}
   347 	
   348 	return Plugin_Stop;
   349 }
   350 
   351 public Action:Command_Say(client, args)
   352 {
   353 	if (!ShouldRun())
   354 	{
   355 		return Plugin_Continue;
   356 	}
   357 	
   358 	new String:text[192];
   359 	GetCmdArg(1, text, sizeof(text));
   360 	
   361 	if (strcmp(text, "guns") == 0)
   362 	{
   363 		if (!g_AllowGunCommand)
   364 		{
   365 			PrintToChat(client, "[CSSDM] %t", "GunsMenuDisabled");
   366 			return Plugin_Handled;
   367 		}
   368 		
   369 		if (!ChooseFromSecondary() && !ChooseFromPrimary())
   370 		{
   371 			PrintToChat(client, "[CSSDM] %t", "GunsMenuNotAvailable");
   372 			return Plugin_Handled;
   373 		}
   374 		
   375 		if (g_GunMenuEnabled[client])
   376 		{
   377 			PrintToChat(client, "[CSSDM] %t", "GunsMenuAlreadyEnabled");
   378 			return Plugin_Handled;
   379 		}
   380 		
   381 		g_GunMenuEnabled[client] = true;
   382 		if (!g_GunMenuAvailable[client])
   383 		{
   384 			PrintToChat(client, "[CSSDM] %t", "GunsMenuReactivated");
   385 		} else {
   386 			DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
   387 		}
   388 		
   389 		return Plugin_Handled;
   390 	}
   391 	
   392 	return Plugin_Continue;
   393 }
   394 
   395 public Action:OnClientCommand(client, args)
   396 {
   397 	if (!ShouldRun() || g_AllowBuy)
   398 	{
   399 		return Plugin_Continue;
   400 	}
   401 	
   402 	decl String:cmd[32];
   403 	GetCmdArg(0, cmd, sizeof(cmd));
   404 	
   405 	if (StrEqual(cmd, "buy")
   406 		|| StrEqual(cmd, "autobuy")
   407 		|| StrEqual(cmd, "rebuy")
   408 		|| StrEqual(cmd, "buyequip")
   409 		|| StrEqual(cmd, "buymenu"))
   410 	{
   411 		return Plugin_Handled;
   412 	}
   413 	
   414 	return Plugin_Continue;
   415 }
   416 
   417 public Menu_EquipHandler(Handle:menu, MenuAction:action, param1, param2)
   418 {
   419 	if (!ShouldRun())
   420 	{
   421 		return 0;
   422 	}
   423 	
   424 	if (action == MenuAction_DrawItem)
   425 	{
   426 		if (param2 == 1 || param2 == 2)
   427 		{
   428 			if (g_PrimaryChoices[param1] == -1 && g_SecondaryChoices[param1] == -1)
   429 			{
   430 				return ITEMDRAW_DISABLED;
   431 			}
   432 		}
   433 		return ITEMDRAW_DEFAULT;
   434 	}
   435 	else if (action == MenuAction_Select)
   436 	{
   437 		if (param2 == 0)
   438 		{
   439 			if (ChooseFromSecondary())
   440 			{
   441 				DisplayMenu(g_hSecondaryMenu, param1, MENU_TIME_FOREVER);
   442 			} else if (ChooseFromPrimary()) {
   443 				DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
   444 			}
   445 		} else if (param2 == 1) {
   446 			GiveBothFromChoices(param1);
   447 		} else if (param2 == 2) {
   448 			GiveBothFromChoices(param1);
   449 			g_GunMenuEnabled[param1] = false;
   450 			PrintToChat(param1, "[CSSDM] %t", "SayGunsNotify");
   451 		} else if (param2 == 3) {
   452 			GivePrimary(param1, g_PrimaryCount);
   453 			GiveSecondary(param1, g_SecondaryCount);
   454 		} else if (param2 == 4) {
   455 			GivePrimary(param1, g_PrimaryCount);
   456 			GiveSecondary(param1, g_SecondaryCount);
   457 			GiveBothFromChoices(param1);
   458 			g_GunMenuEnabled[param1] = false;
   459 			PrintToChat(param1, "[CSSDM] %t", "SayGunsNotify");
   460 		}
   461 		g_GunMenuAvailable[param1] = false;
   462 	}
   463 	else if (action == MenuAction_DisplayItem)
   464 	{
   465 		decl style;
   466 		decl String:info[12], String:lang_phrase[32];
   467 		
   468 		if (!GetMenuItem(menu, param2, info, sizeof(info), style, lang_phrase, sizeof(lang_phrase)))
   469 		{
   470 			return 0;
   471 		}
   472 		
   473 		decl String:t_phrase[64];
   474 		Format(t_phrase, sizeof(t_phrase), "%T", lang_phrase, param1);
   475 		
   476 		return RedrawMenuItem(t_phrase);
   477 	}
   478 	
   479 	return 0;
   480 }
   481 
   482 public Menu_PrimaryHandler(Handle:menu, MenuAction:action, param1, param2)
   483 {
   484 	if (!ShouldRun())
   485 	{
   486 		return 0;
   487 	}
   488 	
   489 	if (action == MenuAction_DrawItem)
   490 	{
   491 		return ITEMDRAW_DEFAULT;
   492 	}
   493 	else if (action == MenuAction_Select)
   494 	{
   495 		GivePrimary(param1, param2);
   496 	}
   497 	else if (action == MenuAction_Display)
   498 	{
   499 		new Handle:hPanel = Handle:param2;
   500 		decl String:title[128];
   501 		
   502 		Format(title, sizeof(title), "%T:", "Primary weapon", param1);
   503 		
   504 		SetPanelTitle(hPanel, title);
   505 	}
   506 	
   507 	return 0;
   508 }
   509 
   510 public Menu_SecondaryHandler(Handle:menu, MenuAction:action, param1, param2)
   511 {
   512 	if (!ShouldRun())
   513 	{
   514 		return 0;
   515 	}
   516 	
   517 	if (action == MenuAction_DrawItem)
   518 	{
   519 		return ITEMDRAW_DEFAULT;
   520 	}
   521 	else if (action == MenuAction_Select)
   522 	{
   523 		GiveSecondary(param1, param2);
   524 		if (ChooseFromPrimary())
   525 		{
   526 			DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
   527 		}
   528 	}
   529 	else if (action == MenuAction_Cancel
   530 			 && param2 == MenuCancel_Exit)
   531 	{
   532 		if (ChooseFromPrimary())
   533 		{
   534 			DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
   535 		}
   536 	}
   537 	else if (action == MenuAction_Display)
   538 	{
   539 		new Handle:hPanel = Handle:param2;
   540 		decl String:title[128];
   541 		
   542 		Format(title, sizeof(title), "%T:", "Secondary weapon", param1);
   543 		
   544 		SetPanelTitle(hPanel, title);
   545 	}
   546 	
   547 	return 0;
   548 }
   549 
   550 bool:ChooseFromPrimary()
   551 {
   552 	return (g_PrimaryMenu == CSSDM_GUNMENU_YES
   553 			&& g_PrimaryCount > 1);
   554 }
   555 
   556 bool:ChooseFromSecondary()
   557 {
   558 	return (g_SecondaryMenu == CSSDM_GUNMENU_YES
   559 			&& g_SecondaryCount > 1);
   560 }
   561 
   562 GiveWeapon(client, index)
   563 {
   564 	if (!IsPlayerAlive(client))
   565 	{
   566 		return;
   567 	}
   568 
   569 	new DmWeaponType:type = DM_GetWeaponType(index);
   570 	
   571 	new entity = DM_GetClientWeapon(client, type);
   572 	if (entity != -1)
   573 	{
   574 		DM_DropWeapon(client, entity);
   575 	}
   576 	
   577 	new String:cls[64];
   578 	DM_GetWeaponClassname(index, cls, sizeof(cls));
   579 	
   580 	GivePlayerItem(client, cls);
   581 }
   582 
   583 GiveBothFromChoices(client)
   584 {
   585 	if (ChooseFromPrimary())
   586 	{
   587 		GivePrimary(client, g_PrimaryChoices[client]);
   588 	}
   589 	
   590 	if (ChooseFromSecondary())
   591 	{
   592 		GiveSecondary(client, g_SecondaryChoices[client]);
   593 	}
   594 }
   595 
   596 /**
   597  * Gives a primary weapon to a client.
   598  * If the list_index is > the count, we choose one randomly.
   599  * This is a hack so we can append "Random" to the choice list.
   600  */
   601 GivePrimary(client, list_index)
   602 {
   603 	if (list_index < 0)
   604 	{
   605 		return;
   606 	}
   607 	
   608 	new weapon_index;
   609 	if (list_index >= g_PrimaryCount)
   610 	{
   611 		if (g_PrimaryCount > 1)
   612 		{
   613 			weapon_index = GetRandomInt(0, g_PrimaryCount-1);
   614 			weapon_index = g_PrimaryList[weapon_index];
   615 		} else if (g_PrimaryCount == 1) {
   616 			weapon_index = g_PrimaryList[0];
   617 		} else {
   618 			return;
   619 		}
   620 	} else {
   621 		weapon_index = g_PrimaryList[list_index];
   622 	}
   623 	
   624 	g_PrimaryChoices[client] = list_index;
   625 	
   626 	GiveWeapon(client, weapon_index);
   627 }
   628 
   629 /**
   630  * Gives a secondary weapon to a client.
   631  * If the list_index is > the count, we choose one randomly.
   632  * This is a hack so we can append "Random" to the choice list.
   633  */
   634 GiveSecondary(client, list_index)
   635 {
   636 	if (list_index < 0)
   637 	{
   638 		return;
   639 	}
   640 	
   641 	new weapon_index;
   642 	if (list_index >= g_SecondaryCount)
   643 	{
   644 		if (g_SecondaryCount > 1)
   645 		{
   646 			weapon_index = GetRandomInt(0, g_SecondaryCount-1);
   647 			weapon_index = g_SecondaryList[weapon_index];
   648 		} else if (g_SecondaryCount == 1) {
   649 			weapon_index = g_SecondaryList[0];
   650 		} else {
   651 			return;
   652 		}
   653 	} else {
   654 		weapon_index = g_SecondaryList[list_index];
   655 	}
   656 	
   657 	g_SecondaryChoices[client] = list_index;
   658 	
   659 	GiveWeapon(client, weapon_index);
   660 }
   661 
   662 bool:ShouldRun()
   663 {
   664 	return (DM_IsRunning() && g_IsEnabled);
   665 }
   666 
   667 bool:KvGetYesOrNo(Handle:kv, const String:key[], bool:curdefault)
   668 {
   669 	decl String:value[12];
   670 	KvGetString(kv, key, value, sizeof(value), curdefault ? "yes" : "no");
   671 	return (strcmp(value, "yes") == 0);
   672 }
   673 
   674 KvGetGunMenu(Handle:kv, const String:key[], def)
   675 {
   676 	decl String:sdef[12];
   677 	if (def == CSSDM_GUNMENU_YES)
   678 	{
   679 		strcopy(sdef, sizeof(sdef), "yes");
   680 	} else if (def == CSSDM_GUNMENU_RANDOM) {
   681 		strcopy(sdef, sizeof(sdef), "random");
   682 	} else {
   683 		strcopy(sdef, sizeof(sdef), "no");
   684 	}
   685 	
   686 	decl String:value[12];
   687 	KvGetString(kv, key, value, sizeof(value), sdef);
   688 	
   689 	if (strcmp(value, "yes") == 0)
   690 	{
   691 		return CSSDM_GUNMENU_YES;
   692 	} else if (strcmp(value, "random") == 0) {
   693 		return CSSDM_GUNMENU_RANDOM;
   694 	}
   695 	
   696 	return CSSDM_GUNMENU_NO;
   697 }
   698 
   699 LoadDefaults()
   700 {
   701 	g_PrimaryMenu = CSSDM_GUNMENU_YES;
   702 	g_SecondaryMenu = CSSDM_GUNMENU_YES;
   703 	g_AllowBuy = false;
   704 	g_ArmorAmount = 100;
   705 	g_Helmets = true;
   706 	g_Flashes = 0;
   707 	g_Smokes = false;
   708 	g_HEs = false;
   709 	g_NightVision = false;
   710 	g_DefuseKits = true;
   711 	g_HealthAmount = 100;
   712 	g_AllowGunCommand = true;
   713 }
   714 
   715 bool:LoadConfigFile(const String:path[])
   716 {
   717 	new Handle:kv = CreateKeyValues("Equipment");
   718 	if (!FileToKeyValues(kv, path))
   719 	{
   720 		return false;
   721 	}
   722 	
   723 	decl String:value[255];
   724 	
   725 	/* Load settings */
   726 	if (KvJumpToKey(kv, "Settings"))
   727 	{
   728 		g_AllowGunCommand = KvGetYesOrNo(kv, "guns_command", g_AllowGunCommand);
   729 		KvGoBack(kv);
   730 	}
   731 	
   732 	/* Load menu options */
   733 	if (KvJumpToKey(kv, "Menus"))
   734 	{
   735 		g_PrimaryMenu = KvGetGunMenu(kv, "primary", g_PrimaryMenu);
   736 		g_SecondaryMenu = KvGetGunMenu(kv, "secondary", g_SecondaryMenu);
   737 		g_AllowBuy = KvGetYesOrNo(kv, "buy", g_AllowBuy);
   738 		KvGoBack(kv);
   739 	}
   740 	
   741 	/* Load automatic stuff */
   742 	if (KvJumpToKey(kv, "AutoItems"))
   743 	{
   744 		g_ArmorAmount = KvGetNum(kv, "armor", g_ArmorAmount);
   745 		g_Flashes = KvGetNum(kv, "flashbangs", g_Flashes);
   746 		g_Helmets = KvGetYesOrNo(kv, "helmet", g_Helmets);
   747 		g_Smokes = KvGetYesOrNo(kv, "smokegrenade", g_Smokes);
   748 		g_HEs = KvGetYesOrNo(kv, "hegrenade", g_HEs);
   749 		g_NightVision = KvGetYesOrNo(kv, "nightvision", g_NightVision);
   750 		g_DefuseKits = KvGetYesOrNo(kv, "defusekits", g_DefuseKits);
   751 		g_HealthAmount = KvGetNum(kv, "health", g_HealthAmount);
   752 		g_Decoy = KvGetYesOrNo(kv, "decoy", g_Decoy);
   753 		g_Taser = KvGetYesOrNo(kv, "taser", g_Taser);
   754 		KvGoBack(kv);
   755 	}
   756 	
   757 	/* Load bot items */
   758 	if (KvJumpToKey(kv, "BotItems"))
   759 	{
   760 		/* Clear out old lists first */
   761 		g_BotPrimaryCount = 0;
   762 		g_BotSecondaryCount = 0;
   763 		
   764 		if (KvGotoFirstSubKey(kv, false))
   765 		{
   766 			do
   767 			{
   768 				KvGetSectionName(kv, value, sizeof(value));
   769 				if (strcmp(value, "weapon") == 0)
   770 				{
   771 					KvGetString(kv, NULL_STRING, value, sizeof(value));
   772 					new index = DM_GetWeaponID(value);
   773 					if (index != -1)
   774 					{
   775 						new DmWeaponType:type = DM_GetWeaponType(index);
   776 						if (type == DmWeapon_Primary && g_BotPrimaryCount < CSSDM_MAX_WEAPONS)
   777 						{
   778 							g_BotPrimaryList[g_BotPrimaryCount++] = index;
   779 						} else if (type == DmWeapon_Secondary && g_BotSecondaryCount < CSSDM_MAX_WEAPONS) {
   780 							g_BotSecondaryList[g_BotSecondaryCount++] = index;
   781 						}
   782 					}					
   783 				} else if (strcmp(value, "armor") == 0) {
   784 					g_BotArmor = KvGetNum(kv, NULL_STRING, 100);
   785 				} else if (strcmp(value, "helmet") == 0) {
   786 					g_BotHelmets = KvGetYesOrNo(kv, NULL_STRING, true);
   787 				} else if (strcmp(value, "flashbangs") == 0) {
   788 					g_BotFlashes = KvGetNum(kv, NULL_STRING);
   789 				} else if (strcmp(value, "smokegrenade") == 0) {
   790 					g_BotSmokes = KvGetYesOrNo(kv, NULL_STRING, false);
   791 				} else if (strcmp(value, "hegrenade") == 0) {
   792 					g_BotHEs = KvGetYesOrNo(kv, NULL_STRING, false);
   793 				} else if (strcmp(value, "defusekits") == 0) {
   794 					g_BotDefuseKits = KvGetYesOrNo(kv, NULL_STRING, true);
   795 				} else if (strcmp(value, "health") == 0) {
   796 					g_BotHealthAmount = KvGetNum(kv, NULL_STRING, 100);
   797 				} else if (strcmp(value, "decoy") == 0) {
   798 					g_BotDecoy = KvGetYesOrNo(kv, NULL_STRING, false);
   799 				} else if (strcmp(value, "taser") == 0) {
   800 					g_BotTaser = KvGetYesOrNo(kv, NULL_STRING, true);
   801 				}
   802 			} while (KvGotoNextKey(kv, false));
   803 			KvGoBack(kv);
   804 		}
   805 		KvGoBack(kv);
   806 	}
   807 	
   808 	/* Load secondary weapons */
   809 	if (KvJumpToKey(kv, "SecondaryMenu"))
   810 	{
   811 		/* Clear out old list first */
   812 		g_SecondaryCount = 0;
   813 		
   814 		if (KvGotoFirstSubKey(kv, false))
   815 		{
   816 			do
   817 			{
   818 				KvGetSectionName(kv, value, sizeof(value));
   819 				if (strcmp(value, "weapon") == 0)
   820 				{
   821 					KvGetString(kv, NULL_STRING, value, sizeof(value));
   822 					new index = DM_GetWeaponID(value);
   823 					if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Secondary)
   824 					{
   825 						g_SecondaryList[g_SecondaryCount++] = index;
   826 					}
   827 				}
   828 			} while (KvGotoNextKey(kv, false) && g_SecondaryCount < CSSDM_MAX_WEAPONS);
   829 			KvGoBack(kv);
   830 		}
   831 		KvGoBack(kv);
   832 	}
   833 	
   834 	/* Load primary weapons */
   835 	if (KvJumpToKey(kv, "PrimaryMenu"))
   836 	{
   837 		/* Clear out old list first */
   838 		g_PrimaryCount = 0;
   839 		
   840 		if (KvGotoFirstSubKey(kv, false))
   841 		{
   842 			do
   843 			{
   844 				KvGetSectionName(kv, value, sizeof(value));
   845 				if (strcmp(value, "weapon") == 0)
   846 				{
   847 					KvGetString(kv, NULL_STRING, value, sizeof(value));
   848 					new index = DM_GetWeaponID(value);
   849 					if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Primary)
   850 					{
   851 						g_PrimaryList[g_PrimaryCount++] = index;
   852 					}
   853 				}
   854 			} while (KvGotoNextKey(kv, false) && g_PrimaryCount < CSSDM_MAX_WEAPONS);
   855 			KvGoBack(kv);
   856 		}
   857 		KvGoBack(kv);
   858 	}
   859 	
   860 	CloseHandle(kv);
   861 	
   862 	/* Build the primary menu */
   863 	if (g_hPrimaryMenu != INVALID_HANDLE)
   864 	{
   865 		CloseHandle(g_hPrimaryMenu);
   866 	}
   867 	g_hPrimaryMenu = CreateMenu(Menu_PrimaryHandler, MenuAction_DrawItem|MenuAction_Display);
   868 	SetMenuTitle(g_hPrimaryMenu, "Primary weapon:");
   869 	for (new i=0; i<g_PrimaryCount; i++)
   870 	{
   871 		new index = g_PrimaryList[i];
   872 		DM_GetWeaponName(index, value, sizeof(value));
   873 		AddMenuItem(g_hPrimaryMenu, "", value);
   874 	}
   875 	AddMenuItem(g_hPrimaryMenu, "", "Random");
   876 	
   877 	/* Build the secondary menu */
   878 	if (g_hSecondaryMenu != INVALID_HANDLE)
   879 	{
   880 		CloseHandle(g_hSecondaryMenu);
   881 	}
   882 	g_hSecondaryMenu = CreateMenu(Menu_SecondaryHandler, MenuAction_DrawItem|MenuAction_Display);
   883 	SetMenuTitle(g_hSecondaryMenu, "Secondary weapon:");
   884 	for (new i=0; i<g_SecondaryCount; i++)
   885 	{
   886 		new index = g_SecondaryList[i];
   887 		DM_GetWeaponName(index, value, sizeof(value));
   888 		AddMenuItem(g_hSecondaryMenu, "", value);
   889 	}
   890 	AddMenuItem(g_hSecondaryMenu, "", "Random");
   891 	
   892 	return true;
   893 }
   894 
   895 SetClientArmor(client, armor)
   896 {
   897 	if (g_ArmorOffset == -1)
   898 	{
   899 		return;
   900 	}
   901 	
   902 	SetEntData(client, g_ArmorOffset, armor, 4, true);
   903 }
   904 
   905 SetClientHealth(client, health)
   906 {
   907 	if (g_HealthOffset == -1)
   908 	{
   909 		return;
   910 	}
   911 	
   912 	SetEntData(client, g_HealthOffset, health, 4, true);
   913 }
   914 
   915 GiveGrenades(client, flashnum, bool:he, bool:smoke, bool:decoy)
   916 {
   917 	if (!IsPlayerAlive(client))
   918 	{
   919 		return;
   920 	}
   921 
   922 	if (he)
   923 	{
   924 		GivePlayerItem(client, "weapon_hegrenade");
   925 	}
   926 	for (new i=0; i<flashnum; i++)
   927 	{
   928 		GivePlayerItem(client, "weapon_flashbang");
   929 	}
   930 	if (smoke)
   931 	{
   932 		GivePlayerItem(client, "weapon_smokegrenade");
   933 	}
   934 	if (g_bIsGo && decoy)
   935 	{
   936 		GivePlayerItem(client, "weapon_decoy");
   937 	}
   938 }
   939 
   940 GiveNightVision(client)
   941 {
   942 	if (g_NVOffset == -1)
   943 	{
   944 		return;
   945 	}
   946 	
   947 	SetEntData(client, g_NVOffset, true, 1, true);
   948 }
   949