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/
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.
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.
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.
29 #define CSSDM_GUNMENU_NO 0
30 #define CSSDM_GUNMENU_YES 1
31 #define CSSDM_GUNMENU_RANDOM 2
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 */
49 new g_PrimaryList[CSSDM_MAX_WEAPONS];
50 new g_SecondaryList[CSSDM_MAX_WEAPONS];
51 new g_PrimaryCount = 0;
52 new g_SecondaryCount = 0;
53 new g_PrimaryMenu = CSSDM_GUNMENU_YES;
54 new g_SecondaryMenu = CSSDM_GUNMENU_YES;
55 new bool:g_AllowBuy = false;
56 new g_ArmorAmount = 100;
57 new bool:g_Helmets = true;
59 new bool:g_Smokes = false;
60 new bool:g_HEs = false;
61 new bool:g_NightVision = false;
62 new bool:g_DefuseKits = true;
63 new bool:g_AllowGunCommand = true;
64 new g_HealthAmount = 100;
67 new g_BotPrimaryList[CSSDM_MAX_WEAPONS];
68 new g_BotSecondaryList[CSSDM_MAX_WEAPONS];
69 new g_BotPrimaryCount = 0;
70 new g_BotSecondaryCount = 0;
72 new bool:g_BotHelmets = false;
74 new bool:g_BotSmokes = false;
75 new bool:g_BotHEs = false;
76 new bool:g_BotDefuseKits = true;
77 new g_BotHealthAmount = 100;
80 public Plugin:myinfo =
82 name = "CS:S DM Equipment",
83 author = "AlliedModders LLC",
84 description = "Adds gun menu/equipment to CS:S DM",
85 version = CSSDM_VERSION,
86 url = "http://www.bailopan.net/cssdm/"
93 public OnPluginStart()
95 LoadTranslations("cssdm.phrases");
97 RegConsoleCmd("say", Command_Say);
98 RegConsoleCmd("say_team", Command_Say);
100 cssdm_enable_equipment = CreateConVar("cssdm_enable_equipment", "1", "Sets whether the equipment plugin is enabled");
101 g_ArmorOffset = FindSendPropOffs("CCSPlayer", "m_ArmorValue");
102 g_NVOffset = FindSendPropOffs("CCSPlayer", "m_bHasNightVision");
103 g_HealthOffset = FindSendPropOffs("CCSPlayer", "m_iHealth");
105 g_hEquipMenu = CreateMenu(Menu_EquipHandler, MenuAction_DrawItem);
106 SetMenuTitle(g_hEquipMenu, "Weapon Options:");
107 SetMenuExitButton(g_hEquipMenu, false);
108 AddMenuItem(g_hEquipMenu, "", "New weapons");
109 AddMenuItem(g_hEquipMenu, "", "Same weapons");
110 AddMenuItem(g_hEquipMenu, "", "Same weapons every time");
111 AddMenuItem(g_hEquipMenu, "", "Random weapons");
112 AddMenuItem(g_hEquipMenu, "", "Random weapons every time");
115 public OnConfigsExecuted()
119 if (!GetConVarInt(cssdm_enable_equipment))
125 if ((g_IsEnabled = LoadConfigFile("cfg/cssdm/cssdm.equip.txt")) == false)
127 LogError("[CSSDM] Could not find equipment file \"%s\"", "cfg/cssdm/cssdm.equip.txt");
131 /** See if there is a map version */
133 decl String:path[255];
134 GetCurrentMap(map, sizeof(map));
135 Format(path, sizeof(path), "cfg/cssdm/maps/%s.equip.txt", map);
137 if (FileExists(path))
139 LoadConfigFile(path);
143 public Action:DM_OnClientDeath(client)
145 if (g_SpawnTimers[client] != INVALID_HANDLE)
147 KillTimer(g_SpawnTimers[client]);
148 g_SpawnTimers[client] = INVALID_HANDLE;
150 g_GunMenuAvailable[client] = true;
153 public OnClientPutInServer(client)
155 g_GunMenuEnabled[client] = true;
156 g_GunMenuAvailable[client] = true;
157 g_PrimaryChoices[client] = -1;
158 g_SecondaryChoices[client] = -1;
161 public OnClientDisconnect(client)
163 if (g_SpawnTimers[client] != INVALID_HANDLE)
165 KillTimer(g_SpawnTimers[client]);
166 g_SpawnTimers[client] = INVALID_HANDLE;
170 public DM_OnClientSpawned(client)
172 if (!IsFakeClient(client))
174 if (g_ArmorAmount > 0)
176 GivePlayerItem(client, "item_assaultsuit");
177 GivePlayerItem(client, "item_assaultsuit");
178 SetClientArmor(client, g_ArmorAmount);
180 if (g_HealthAmount > 0)
182 SetClientHealth(client, g_HealthAmount);
186 GivePlayerItem(client, "item_kevlar");
187 GivePlayerItem(client, "item_kevlar");
189 GiveGrenades(client, g_Flashes, g_HEs, g_Smokes);
192 GiveNightVision(client);
195 /* See if we'll need to strip this bot */
196 if (g_BotSecondaryCount || g_BotPrimaryCount)
198 DM_StripBotItems(client);
202 GivePlayerItem(client, "item_assaultsuit");
203 GivePlayerItem(client, "item_assaultsuit");
204 SetClientArmor(client, g_BotArmor);
206 /* Make sure bot didn't buy armor */
207 SetClientArmor(client, 0);
209 if (g_BotHealthAmount > 0)
211 SetClientHealth(client, g_BotHealthAmount);
215 GivePlayerItem(client, "item_kevlar");
216 GivePlayerItem(client, "item_kevlar");
218 GiveGrenades(client, g_BotFlashes, g_BotHEs, g_BotSmokes);
220 if (!g_AllowBuy && g_SpawnTimers[client] == INVALID_HANDLE)
222 /* Small delay - we want to avoid ResetHUD */
223 g_SpawnTimers[client] = CreateTimer(0.1, PlayerPostSpawn, client);
227 public Action:PlayerPostSpawn(Handle:timer, any:client)
229 g_SpawnTimers[client] = INVALID_HANDLE;
236 if (IsFakeClient(client))
238 if (g_BotSecondaryCount > 0)
241 if (g_BotSecondaryCount == 1)
243 index = g_BotSecondaryList[0];
245 index = GetRandomInt(0, g_BotSecondaryCount-1);
246 index = g_BotSecondaryList[index];
248 decl String:classname[64];
249 DM_GetWeaponClassname(index, classname, sizeof(classname));
250 GivePlayerItem(client, classname);
252 if (g_BotPrimaryCount > 0)
255 if (g_BotPrimaryCount == 1)
257 index = g_BotPrimaryList[0];
259 index = GetRandomInt(0, g_BotPrimaryCount-1);
260 index = g_BotPrimaryList[index];
262 decl String:classname[64];
263 DM_GetWeaponClassname(index, classname, sizeof(classname));
264 GivePlayerItem(client, classname);
266 if (g_BotDefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
268 GivePlayerItem(client, "item_defuser");
271 if (g_DefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
273 GivePlayerItem(client, "item_defuser");
277 /* First, check if we should only be giving one of something automatically */
278 if ((g_SecondaryMenu == CSSDM_GUNMENU_RANDOM || g_SecondaryMenu == CSSDM_GUNMENU_YES)
279 && g_SecondaryCount == 1)
281 GiveSecondary(client, 0);
283 } else if (g_PrimaryMenu == CSSDM_GUNMENU_RANDOM && g_PrimaryCount > 1) {
284 GivePrimary(client, g_PrimaryCount);
288 if ((g_PrimaryMenu == CSSDM_GUNMENU_RANDOM || g_PrimaryMenu == CSSDM_GUNMENU_YES)
289 && g_PrimaryCount == 1)
291 GivePrimary(client, 0);
293 } else if (g_SecondaryMenu == CSSDM_GUNMENU_RANDOM && g_SecondaryCount > 1) {
294 GiveSecondary(client, g_SecondaryCount);
298 /* If we've already given two weapons, there is no need for a menu. */
304 if (g_GunMenuEnabled[client])
306 DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
308 GiveBothFromChoices(client);
315 public Action:Command_Say(client, args)
319 return Plugin_Continue;
322 new String:text[192];
323 GetCmdArg(1, text, sizeof(text));
325 if (strcmp(text, "guns") == 0)
327 if (!g_AllowGunCommand)
329 PrintToChat(client, "[CSSDM] %t", "GunsMenuDisabled");
330 return Plugin_Handled;
333 if (!ChooseFromSecondary() && !ChooseFromPrimary())
335 PrintToChat(client, "[CSSDM] %t", "GunsMenuNotAvailable");
336 return Plugin_Handled;
339 if (g_GunMenuEnabled[client])
341 PrintToChat(client, "[CSSDM] %t", "GunsMenuAlreadyEnabled");
342 return Plugin_Handled;
345 g_GunMenuEnabled[client] = true;
346 if (!g_GunMenuAvailable[client])
348 PrintToChat(client, "[CSSDM] %t", "GunsMenuReactivated");
350 DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
353 return Plugin_Handled;
356 return Plugin_Continue;
359 public Action:OnClientCommand(client, args)
361 if (!ShouldRun() || g_AllowBuy)
363 return Plugin_Continue;
367 GetCmdArg(0, cmd, sizeof(cmd));
369 if (StrEqual(cmd, "buy")
370 || StrEqual(cmd, "autobuy")
371 || StrEqual(cmd, "rebuy")
372 || StrEqual(cmd, "buyequip")
373 || StrEqual(cmd, "buymenu"))
375 return Plugin_Handled;
378 return Plugin_Continue;
381 public Menu_EquipHandler(Handle:menu, MenuAction:action, param1, param2)
388 if (action == MenuAction_DrawItem)
390 if (param2 == 1 || param2 == 2)
392 if (g_PrimaryChoices[param1] == -1 && g_PrimaryChoices[param1] == -1)
394 return ITEMDRAW_DISABLED;
397 return ITEMDRAW_DEFAULT;
399 else if (action == MenuAction_Select)
403 if (ChooseFromSecondary())
405 DisplayMenu(g_hSecondaryMenu, param1, MENU_TIME_FOREVER);
406 } else if (ChooseFromPrimary()) {
407 DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
409 } else if (param2 == 1) {
410 GiveBothFromChoices(param1);
411 } else if (param2 == 2) {
412 GiveBothFromChoices(param1);
413 g_GunMenuEnabled[param1] = false;
414 PrintToChat(param1, "[CSSDM] %t", "SayGunsNotify");
415 } else if (param2 == 3) {
416 GivePrimary(param1, g_PrimaryCount);
417 GiveSecondary(param1, g_SecondaryCount);
418 } else if (param2 == 4) {
419 GivePrimary(param1, g_PrimaryCount);
420 GiveSecondary(param1, g_SecondaryCount);
421 GiveBothFromChoices(param1);
422 g_GunMenuEnabled[param1] = false;
423 PrintToChat(param1, "[CSSDM] %t", "SayGunsNotify");
425 g_GunMenuAvailable[param1] = false;
431 public Menu_PrimaryHandler(Handle:menu, MenuAction:action, param1, param2)
438 if (action == MenuAction_DrawItem)
440 return ITEMDRAW_DEFAULT;
442 else if (action == MenuAction_Select)
444 GivePrimary(param1, param2);
450 public Menu_SecondaryHandler(Handle:menu, MenuAction:action, param1, param2)
457 if (action == MenuAction_DrawItem)
459 return ITEMDRAW_DEFAULT;
461 else if (action == MenuAction_Select)
463 GiveSecondary(param1, param2);
464 if (ChooseFromPrimary())
466 DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
473 bool:ChooseFromPrimary()
475 return (g_PrimaryMenu == CSSDM_GUNMENU_YES
476 && g_PrimaryCount > 1);
479 bool:ChooseFromSecondary()
481 return (g_SecondaryMenu == CSSDM_GUNMENU_YES
482 && g_SecondaryCount > 1);
485 GiveWeapon(client, index)
487 new DmWeaponType:type = DM_GetWeaponType(index);
489 new entity = DM_GetClientWeapon(client, type);
492 DM_DropWeapon(client, entity);
496 DM_GetWeaponClassname(index, cls, sizeof(cls));
498 return GivePlayerItem(client, cls);
501 GiveBothFromChoices(client)
503 if (ChooseFromPrimary())
505 GivePrimary(client, g_PrimaryChoices[client]);
508 if (ChooseFromSecondary())
510 GiveSecondary(client, g_SecondaryChoices[client]);
515 * Gives a primary weapon to a client.
516 * If the list_index is > the count, we choose one randomly.
517 * This is a hack so we can append "Random" to the choice list.
519 GivePrimary(client, list_index)
527 if (list_index >= g_PrimaryCount)
529 if (g_PrimaryCount > 1)
531 weapon_index = GetRandomInt(0, g_PrimaryCount-1);
532 weapon_index = g_PrimaryList[weapon_index];
534 weapon_index = g_PrimaryList[0];
537 weapon_index = g_PrimaryList[list_index];
540 g_PrimaryChoices[client] = list_index;
542 GiveWeapon(client, weapon_index);
546 * Gives a secondary weapon to a client.
547 * If the list_index is > the count, we choose one randomly.
548 * This is a hack so we can append "Random" to the choice list.
550 GiveSecondary(client, list_index)
558 if (list_index >= g_SecondaryCount)
560 if (g_SecondaryCount > 1)
562 weapon_index = GetRandomInt(0, g_SecondaryCount-1);
563 weapon_index = g_SecondaryList[weapon_index];
565 weapon_index = g_SecondaryList[0];
568 weapon_index = g_SecondaryList[list_index];
571 g_SecondaryChoices[client] = list_index;
573 GiveWeapon(client, weapon_index);
578 return (DM_IsRunning() && g_IsEnabled);
581 bool:KvGetYesOrNo(Handle:kv, const String:key[], bool:curdefault)
583 decl String:value[12];
584 KvGetString(kv, key, value, sizeof(value), curdefault ? "yes" : "no");
585 return (strcmp(value, "yes") == 0);
588 KvGetGunMenu(Handle:kv, const String:key[], def)
590 decl String:sdef[12];
591 if (def == CSSDM_GUNMENU_YES)
593 strcopy(sdef, sizeof(sdef), "yes");
594 } else if (def == CSSDM_GUNMENU_RANDOM) {
595 strcopy(sdef, sizeof(sdef), "random");
597 strcopy(sdef, sizeof(sdef), "no");
600 decl String:value[12];
601 KvGetString(kv, key, value, sizeof(value), sdef);
603 if (strcmp(value, "yes") == 0)
605 return CSSDM_GUNMENU_YES;
606 } else if (strcmp(value, "random") == 0) {
607 return CSSDM_GUNMENU_RANDOM;
610 return CSSDM_GUNMENU_NO;
615 g_PrimaryMenu = CSSDM_GUNMENU_YES;
616 g_SecondaryMenu = CSSDM_GUNMENU_YES;
623 g_NightVision = false;
625 g_HealthAmount = 100;
626 g_AllowGunCommand = true;
629 bool:LoadConfigFile(const String:path[])
631 new Handle:kv = CreateKeyValues("Equipment");
632 if (!FileToKeyValues(kv, path))
637 decl String:value[255];
640 if (KvJumpToKey(kv, "Settings"))
642 g_AllowGunCommand = KvGetYesOrNo(kv, "guns_command", g_AllowGunCommand);
646 /* Load menu options */
647 if (KvJumpToKey(kv, "Menus"))
649 g_PrimaryMenu = KvGetGunMenu(kv, "primary", g_PrimaryMenu);
650 g_SecondaryMenu = KvGetGunMenu(kv, "secondary", g_SecondaryMenu);
651 g_AllowBuy = KvGetYesOrNo(kv, "buy", g_AllowBuy);
655 /* Load automatic stuff */
656 if (KvJumpToKey(kv, "AutoItems"))
658 g_ArmorAmount = KvGetNum(kv, "armor", g_ArmorAmount);
659 g_Flashes = KvGetNum(kv, "flashbangs", g_Flashes);
660 g_Helmets = KvGetYesOrNo(kv, "helmet", g_Helmets);
661 g_Smokes = KvGetYesOrNo(kv, "smokegrenade", g_Smokes);
662 g_HEs = KvGetYesOrNo(kv, "hegrenade", g_HEs);
663 g_NightVision = KvGetYesOrNo(kv, "nightvision", g_NightVision);
664 g_DefuseKits = KvGetYesOrNo(kv, "defusekits", g_DefuseKits);
665 g_HealthAmount = KvGetNum(kv, "health", g_HealthAmount);
670 if (KvJumpToKey(kv, "BotItems"))
672 /* Clear out old lists first */
673 g_BotPrimaryCount = 0;
674 g_BotSecondaryCount = 0;
676 if (KvGotoFirstSubKey(kv, false))
680 KvGetSectionName(kv, value, sizeof(value));
681 if (strcmp(value, "weapon") == 0)
683 KvGetString(kv, NULL_STRING, value, sizeof(value));
684 new index = DM_GetWeaponID(value);
687 new DmWeaponType:type = DM_GetWeaponType(index);
688 if (type == DmWeapon_Primary && g_BotPrimaryCount < CSSDM_MAX_WEAPONS)
690 g_BotPrimaryList[g_BotPrimaryCount++] = index;
691 } else if (type == DmWeapon_Secondary && g_BotSecondaryCount < CSSDM_MAX_WEAPONS) {
692 g_BotSecondaryList[g_BotSecondaryCount++] = index;
695 } else if (strcmp(value, "armor") == 0) {
696 g_BotArmor = KvGetNum(kv, NULL_STRING, 100);
697 } else if (strcmp(value, "helmet") == 0) {
698 g_BotHelmets = KvGetYesOrNo(kv, NULL_STRING, true);
699 } else if (strcmp(value, "flashbangs") == 0) {
700 g_BotFlashes = KvGetNum(kv, NULL_STRING);
701 } else if (strcmp(value, "smokegrenade") == 0) {
702 g_BotSmokes = KvGetYesOrNo(kv, NULL_STRING, false);
703 } else if (strcmp(value, "hegrenade") == 0) {
704 g_BotHEs = KvGetYesOrNo(kv, NULL_STRING, false);
705 } else if (strcmp(value, "defusekits") == 0) {
706 g_BotDefuseKits = KvGetYesOrNo(kv, NULL_STRING, true);
707 } else if (strcmp(value, "health") == 0) {
708 g_BotHealthAmount = KvGetNum(kv, NULL_STRING, 100);
710 } while (KvGotoNextKey(kv, false));
716 /* Load secondary weapons */
717 if (KvJumpToKey(kv, "SecondaryMenu"))
719 /* Clear out old list first */
720 g_SecondaryCount = 0;
722 if (KvGotoFirstSubKey(kv, false))
726 KvGetSectionName(kv, value, sizeof(value));
727 if (strcmp(value, "weapon") == 0)
729 KvGetString(kv, NULL_STRING, value, sizeof(value));
730 new index = DM_GetWeaponID(value);
731 if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Secondary)
733 g_SecondaryList[g_SecondaryCount++] = index;
736 } while (KvGotoNextKey(kv, false) && g_SecondaryCount < CSSDM_MAX_WEAPONS);
742 /* Load primary weapons */
743 if (KvJumpToKey(kv, "PrimaryMenu"))
745 /* Clear out old list first */
748 if (KvGotoFirstSubKey(kv, false))
752 KvGetSectionName(kv, value, sizeof(value));
753 if (strcmp(value, "weapon") == 0)
755 KvGetString(kv, NULL_STRING, value, sizeof(value));
756 new index = DM_GetWeaponID(value);
757 if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Primary)
759 g_PrimaryList[g_PrimaryCount++] = index;
762 } while (KvGotoNextKey(kv, false) && g_PrimaryCount < CSSDM_MAX_WEAPONS);
770 /* Build the primary menu */
771 if (g_hPrimaryMenu != INVALID_HANDLE)
773 CloseHandle(g_hPrimaryMenu);
775 g_hPrimaryMenu = CreateMenu(Menu_PrimaryHandler, MenuAction_DrawItem);
776 SetMenuTitle(g_hPrimaryMenu, "Primary weapon:");
777 for (new i=0; i<g_PrimaryCount; i++)
779 new index = g_PrimaryList[i];
780 DM_GetWeaponName(index, value, sizeof(value));
781 AddMenuItem(g_hPrimaryMenu, "", value);
783 AddMenuItem(g_hPrimaryMenu, "", "Random");
785 /* Build the secondary menu */
786 if (g_hSecondaryMenu != INVALID_HANDLE)
788 CloseHandle(g_hSecondaryMenu);
790 g_hSecondaryMenu = CreateMenu(Menu_SecondaryHandler, MenuAction_DrawItem);
791 SetMenuTitle(g_hSecondaryMenu, "Secondary weapon:");
792 for (new i=0; i<g_SecondaryCount; i++)
794 new index = g_SecondaryList[i];
795 DM_GetWeaponName(index, value, sizeof(value));
796 AddMenuItem(g_hSecondaryMenu, "", value);
798 AddMenuItem(g_hSecondaryMenu, "", "Random");
803 SetClientArmor(client, armor)
805 if (g_ArmorOffset == -1)
810 SetEntData(client, g_ArmorOffset, armor, 4, true);
813 SetClientHealth(client, health)
815 if (g_HealthOffset == -1)
820 SetEntData(client, g_HealthOffset, health, 4, true);
823 GiveGrenades(client, flashnum, bool:he, bool:smoke)
827 GivePlayerItem(client, "weapon_hegrenade");
829 for (new i=0; i<flashnum; i++)
831 GivePlayerItem(client, "weapon_flashbang");
835 GivePlayerItem(client, "weapon_smokegrenade");
839 GiveNightVision(client)
841 if (g_NVOffset == -1)
846 SetEntData(client, g_NVOffset, true, 1, true);