3 * Adds preset spawning to CS:S DM.
4 * This file is part of CS:S DM, Copyright (C) 2005-2007 AlliedModders LLC
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #define CSSDM_GUNMENU_NO 0
29 #define CSSDM_GUNMENU_YES 1
30 #define CSSDM_GUNMENU_RANDOM 2
33 new Handle:cssdm_enable_equipment; /** cssdm_enable_equipment cvar */
34 new g_ArmorOffset = -1; /* m_ArmorValue offset */
35 new g_NVOffset = -1; /* m_bHasNightVision offset */
36 new g_HealthOffset = -1; /* m_iHealth offset */
37 new Handle:g_SpawnTimers[MAXPLAYERS+1]; /* Post-spawn timers */
38 new Handle:g_hPrimaryMenu = INVALID_HANDLE; /* Priamry menu Handle */
39 new Handle:g_hSecondaryMenu = INVALID_HANDLE; /* Secondary menu Handle */
40 new Handle:g_hEquipMenu = INVALID_HANDLE; /* Main equipment menu */
41 new bool:g_IsEnabled = false; /* Whether the plugin should work */
42 new g_PrimaryChoices[MAXPLAYERS+1]; /* Primary weapon selections */
43 new g_SecondaryChoices[MAXPLAYERS+1]; /* Secondary weapon selections */
44 new bool:g_GunMenuEnabled[MAXPLAYERS+1]; /* Whether the gun menu is enabled */
45 new bool:g_GunMenuAvailable[MAXPLAYERS+1]; /* Whether the gun menu is available */
48 new g_PrimaryList[CSSDM_MAX_WEAPONS];
49 new g_SecondaryList[CSSDM_MAX_WEAPONS];
50 new g_PrimaryCount = 0;
51 new g_SecondaryCount = 0;
52 new g_PrimaryMenu = CSSDM_GUNMENU_YES;
53 new g_SecondaryMenu = CSSDM_GUNMENU_YES;
54 new bool:g_AllowBuy = false;
55 new g_ArmorAmount = 100;
56 new bool:g_Helmets = true;
58 new bool:g_Smokes = false;
59 new bool:g_HEs = false;
60 new bool:g_NightVision = false;
61 new bool:g_DefuseKits = true;
62 new bool:g_AllowGunCommand = true;
63 new g_HealthAmount = 100;
66 new g_BotPrimaryList[CSSDM_MAX_WEAPONS];
67 new g_BotSecondaryList[CSSDM_MAX_WEAPONS];
68 new g_BotPrimaryCount = 0;
69 new g_BotSecondaryCount = 0;
71 new bool:g_BotHelmets = false;
73 new bool:g_BotSmokes = false;
74 new bool:g_BotHEs = false;
75 new bool:g_BotDefuseKits = true;
76 new g_BotHealthAmount = 100;
79 public Plugin:myinfo =
81 name = "CS:S DM Equipment",
82 author = "AlliedModders LLC",
83 description = "Adds gun menu/equipment to CS:S DM",
84 version = CSSDM_VERSION,
85 url = "http://www.bailopan.net/cssdm/"
92 public OnPluginStart()
94 RegConsoleCmd("say", Command_Say);
95 RegConsoleCmd("say_team", Command_Say);
97 cssdm_enable_equipment = CreateConVar("cssdm_enable_equipment", "1", "Sets whether the equipment plugin is enabled");
98 g_ArmorOffset = FindSendPropOffs("CCSPlayer", "m_ArmorValue");
99 g_NVOffset = FindSendPropOffs("CCSPlayer", "m_bHasNightVision");
100 g_HealthOffset = FindSendPropOffs("CCSPlayer", "m_iHealth");
102 g_hEquipMenu = CreateMenu(Menu_EquipHandler, MenuAction_DrawItem);
103 SetMenuTitle(g_hEquipMenu, "Weapon Options:");
104 SetMenuExitButton(g_hEquipMenu, false);
105 AddMenuItem(g_hEquipMenu, "", "New weapons");
106 AddMenuItem(g_hEquipMenu, "", "Same weapons");
107 AddMenuItem(g_hEquipMenu, "", "Same weapons every time");
108 AddMenuItem(g_hEquipMenu, "", "Random weapons");
109 AddMenuItem(g_hEquipMenu, "", "Random weapons every time");
112 public OnConfigsExecuted()
116 if (!GetConVarInt(cssdm_enable_equipment))
122 if ((g_IsEnabled = LoadConfigFile("cfg/cssdm/cssdm.equip.txt")) == false)
124 LogError("[CSSDM] Could not find equipment file \"%s\"", "cfg/cssdm/cssdm.equip.txt");
128 /** See if there is a map version */
130 decl String:path[255];
131 GetCurrentMap(map, sizeof(map));
132 Format(path, sizeof(path), "cfg/cssdm/%s/cssdm.equip.txt", map);
134 if (FileExists(path))
136 LoadConfigFile(path);
140 public Action:DM_OnClientDeath(client)
142 if (g_SpawnTimers[client] != INVALID_HANDLE)
144 KillTimer(g_SpawnTimers[client]);
145 g_SpawnTimers[client] = INVALID_HANDLE;
147 g_GunMenuAvailable[client] = true;
150 public OnClientPutInServer(client)
152 g_GunMenuEnabled[client] = true;
153 g_GunMenuAvailable[client] = true;
154 g_PrimaryChoices[client] = -1;
155 g_SecondaryChoices[client] = -1;
158 public OnClientDisconnect(client)
160 if (g_SpawnTimers[client] != INVALID_HANDLE)
162 KillTimer(g_SpawnTimers[client]);
163 g_SpawnTimers[client] = INVALID_HANDLE;
167 public DM_OnClientSpawned(client)
169 if (!IsFakeClient(client))
171 if (g_ArmorAmount > 0)
173 GivePlayerItem(client, "item_assaultsuit");
174 GivePlayerItem(client, "item_assaultsuit");
175 SetClientArmor(client, g_ArmorAmount);
177 if (g_HealthAmount > 0)
179 SetClientHealth(client, g_HealthAmount);
183 GivePlayerItem(client, "item_kevlar");
184 GivePlayerItem(client, "item_kevlar");
186 GiveGrenades(client, g_Flashes, g_HEs, g_Smokes);
189 GiveNightVision(client);
192 /* See if we'll need to strip this bot */
193 if (g_BotSecondaryCount || g_BotPrimaryCount)
195 DM_StripBotItems(client);
199 GivePlayerItem(client, "item_assaultsuit");
200 GivePlayerItem(client, "item_assaultsuit");
201 SetClientArmor(client, g_BotArmor);
203 /* Make sure bot didn't buy armor */
204 SetClientArmor(client, 0);
206 if (g_BotHealthAmount > 0)
208 SetClientHealth(client, g_BotHealthAmount);
212 GivePlayerItem(client, "item_kevlar");
213 GivePlayerItem(client, "item_kevlar");
215 GiveGrenades(client, g_BotFlashes, g_BotHEs, g_BotSmokes);
217 if (g_SpawnTimers[client] == INVALID_HANDLE)
219 /* Small delay - we want to avoid ResetHUD */
220 g_SpawnTimers[client] = CreateTimer(0.1, PlayerPostSpawn, client);
224 public Action:PlayerPostSpawn(Handle:timer, any:client)
226 g_SpawnTimers[client] = INVALID_HANDLE;
233 if (IsFakeClient(client))
235 if (g_BotSecondaryCount > 0)
238 if (g_BotSecondaryCount == 1)
240 index = g_BotSecondaryList[0];
242 index = GetRandomInt(0, g_BotSecondaryCount-1);
243 index = g_BotSecondaryList[index];
245 decl String:classname[64];
246 DM_GetWeaponClassname(index, classname, sizeof(classname));
247 GivePlayerItem(client, classname);
249 if (g_BotPrimaryCount > 0)
252 if (g_BotPrimaryCount == 1)
254 index = g_BotPrimaryList[0];
256 index = GetRandomInt(0, g_BotPrimaryCount-1);
257 index = g_BotPrimaryList[index];
259 decl String:classname[64];
260 DM_GetWeaponClassname(index, classname, sizeof(classname));
261 GivePlayerItem(client, classname);
263 if (g_BotDefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
265 GivePlayerItem(client, "item_defuser");
268 if (g_DefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
270 GivePlayerItem(client, "item_defuser");
274 /* First, check if we should only be giving one of something automatically */
275 if ((g_SecondaryMenu == CSSDM_GUNMENU_RANDOM || g_SecondaryMenu == CSSDM_GUNMENU_YES)
276 && g_SecondaryCount == 1)
278 GiveSecondary(client, 0);
280 } else if (g_PrimaryMenu == CSSDM_GUNMENU_RANDOM && g_PrimaryCount > 1) {
281 GivePrimary(client, g_PrimaryCount);
285 if ((g_PrimaryMenu == CSSDM_GUNMENU_RANDOM || g_PrimaryMenu == CSSDM_GUNMENU_YES)
286 && g_PrimaryCount == 1)
288 GivePrimary(client, 0);
290 } else if (g_SecondaryMenu == CSSDM_GUNMENU_RANDOM && g_SecondaryCount > 1) {
291 GiveSecondary(client, g_SecondaryCount);
295 /* If we've already given two weapons, there is no need for a menu. */
301 if (g_GunMenuEnabled[client])
303 DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
305 GiveBothFromChoices(client);
312 public Action:Command_Say(client, args)
316 return Plugin_Continue;
319 new String:text[192];
320 GetCmdArg(1, text, sizeof(text));
322 if (strcmp(text, "guns") == 0)
324 if (!g_AllowGunCommand)
326 PrintToChat(client, "[CSSDM] %t", "GunsMenuDisabled");
327 return Plugin_Handled;
330 if (!ChooseFromSecondary() && !ChooseFromPrimary())
332 PrintToChat(client, "[CSSDM] %t", "GunsMenuNotAvailable");
333 return Plugin_Handled;
336 if (g_GunMenuEnabled[client])
338 PrintToChat(client, "[CSSDM] %t", "GunsMenuAlreadyEnabled");
339 return Plugin_Handled;
342 g_GunMenuEnabled[client] = true;
343 if (!g_GunMenuAvailable[client])
345 PrintToChat(client, "[CSSDM] %t", "GunsMenuReactivated");
347 DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
350 return Plugin_Handled;
353 return Plugin_Continue;
356 public Action:OnClientCommand(client, args)
358 if (!ShouldRun() || g_AllowBuy)
360 return Plugin_Continue;
364 GetCmdArg(0, cmd, sizeof(cmd));
366 if (StrEqual(cmd, "buy")
367 || StrEqual(cmd, "autobuy")
368 || StrEqual(cmd, "rebuy")
369 || StrEqual(cmd, "buyequip")
370 || StrEqual(cmd, "buymenu"))
372 return Plugin_Handled;
375 return Plugin_Continue;
378 public Menu_EquipHandler(Handle:menu, MenuAction:action, param1, param2)
385 if (action == MenuAction_DrawItem)
387 if (param2 == 1 || param2 == 2)
389 if (g_PrimaryChoices[param1] == -1 && g_PrimaryChoices[param1] == -1)
391 return ITEMDRAW_DISABLED;
394 return ITEMDRAW_DEFAULT;
396 else if (action == MenuAction_Select)
400 if (ChooseFromSecondary())
402 DisplayMenu(g_hSecondaryMenu, param1, MENU_TIME_FOREVER);
403 } else if (ChooseFromPrimary()) {
404 DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
406 } else if (param2 == 1) {
407 GiveBothFromChoices(param1);
408 } else if (param2 == 2) {
409 GiveBothFromChoices(param1);
410 g_GunMenuEnabled[param1] = false;
411 PrintToChat(param1, "[CSSDM] %t", "SayGunsNotify");
412 } else if (param2 == 3) {
413 GivePrimary(param1, g_PrimaryCount);
414 GiveSecondary(param1, g_SecondaryCount);
415 } else if (param2 == 4) {
416 GivePrimary(param1, g_PrimaryCount);
417 GiveSecondary(param1, g_SecondaryCount);
418 GiveBothFromChoices(param1);
419 g_GunMenuEnabled[param1] = false;
420 PrintToChat(param1, "[CSSDM] %t", "SayGunsNotify");
422 g_GunMenuAvailable[param1] = false;
428 public Menu_PrimaryHandler(Handle:menu, MenuAction:action, param1, param2)
435 if (action == MenuAction_DrawItem)
437 return ITEMDRAW_DEFAULT;
439 else if (action == MenuAction_Select)
441 GivePrimary(param1, param2);
447 public Menu_SecondaryHandler(Handle:menu, MenuAction:action, param1, param2)
454 if (action == MenuAction_DrawItem)
456 return ITEMDRAW_DEFAULT;
458 else if (action == MenuAction_Select)
460 GiveSecondary(param1, param2);
461 if (ChooseFromPrimary())
463 DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
470 bool:ChooseFromPrimary()
472 return (g_PrimaryMenu == CSSDM_GUNMENU_YES
473 && g_PrimaryCount > 1);
476 bool:ChooseFromSecondary()
478 return (g_SecondaryMenu == CSSDM_GUNMENU_YES
479 && g_SecondaryCount > 1);
482 GiveWeapon(client, index)
484 new DmWeaponType:type = DM_GetWeaponType(index);
486 new entity = DM_GetClientWeapon(client, type);
489 DM_DropWeapon(client, entity);
493 DM_GetWeaponClassname(index, cls, sizeof(cls));
495 return GivePlayerItem(client, cls);
498 GiveBothFromChoices(client)
500 if (ChooseFromPrimary())
502 GivePrimary(client, g_PrimaryChoices[client]);
505 if (ChooseFromSecondary())
507 GiveSecondary(client, g_SecondaryChoices[client]);
512 * Gives a primary weapon to a client.
513 * If the list_index is > the count, we choose one randomly.
514 * This is a hack so we can append "Random" to the choice list.
516 GivePrimary(client, list_index)
524 if (list_index >= g_PrimaryCount)
526 if (g_PrimaryCount > 1)
528 weapon_index = GetRandomInt(0, g_PrimaryCount-1);
529 weapon_index = g_PrimaryList[weapon_index];
531 weapon_index = g_PrimaryList[0];
534 weapon_index = g_PrimaryList[list_index];
537 g_PrimaryChoices[client] = list_index;
539 GiveWeapon(client, weapon_index);
543 * Gives a secondary weapon to a client.
544 * If the list_index is > the count, we choose one randomly.
545 * This is a hack so we can append "Random" to the choice list.
547 GiveSecondary(client, list_index)
555 if (list_index >= g_SecondaryCount)
557 if (g_SecondaryCount > 1)
559 weapon_index = GetRandomInt(0, g_SecondaryCount-1);
560 weapon_index = g_SecondaryList[weapon_index];
562 weapon_index = g_SecondaryList[0];
565 weapon_index = g_SecondaryList[list_index];
568 g_SecondaryChoices[client] = list_index;
570 GiveWeapon(client, weapon_index);
575 return (DM_IsRunning() && g_IsEnabled);
578 bool:KvGetYesOrNo(Handle:kv, const String:key[], bool:curdefault)
580 decl String:value[12];
581 KvGetString(kv, key, value, sizeof(value), curdefault ? "yes" : "no");
582 return (strcmp(value, "yes") == 0);
585 KvGetGunMenu(Handle:kv, const String:key[], def)
587 decl String:sdef[12];
588 if (def == CSSDM_GUNMENU_YES)
590 strcopy(sdef, sizeof(sdef), "yes");
591 } else if (def == CSSDM_GUNMENU_RANDOM) {
592 strcopy(sdef, sizeof(sdef), "random");
594 strcopy(sdef, sizeof(sdef), "no");
597 decl String:value[12];
598 KvGetString(kv, key, value, sizeof(value), sdef);
600 if (strcmp(value, "yes") == 0)
602 return CSSDM_GUNMENU_YES;
603 } else if (strcmp(value, "random") == 0) {
604 return CSSDM_GUNMENU_RANDOM;
607 return CSSDM_GUNMENU_NO;
612 g_PrimaryMenu = CSSDM_GUNMENU_YES;
613 g_SecondaryMenu = CSSDM_GUNMENU_YES;
620 g_NightVision = false;
622 g_HealthAmount = 100;
623 g_AllowGunCommand = true;
626 bool:LoadConfigFile(const String:path[])
628 new Handle:kv = CreateKeyValues("Equipment");
629 if (!FileToKeyValues(kv, path))
634 decl String:value[255];
637 if (KvJumpToKey(kv, "Settings"))
639 g_AllowGunCommand = KvGetYesOrNo(kv, "guns_command", g_AllowGunCommand);
643 /* Load menu options */
644 if (KvJumpToKey(kv, "Menus"))
646 g_PrimaryMenu = KvGetGunMenu(kv, "primary", g_PrimaryMenu);
647 g_SecondaryMenu = KvGetGunMenu(kv, "secondary", g_SecondaryMenu);
648 g_AllowBuy = KvGetYesOrNo(kv, "buy", g_AllowBuy);
652 /* Load automatic stuff */
653 if (KvJumpToKey(kv, "AutoItems"))
655 g_ArmorAmount = KvGetNum(kv, "armor", g_ArmorAmount);
656 g_Flashes = KvGetNum(kv, "flashbangs", g_Flashes);
657 g_Helmets = KvGetYesOrNo(kv, "helmet", g_Helmets);
658 g_Smokes = KvGetYesOrNo(kv, "smokegrenade", g_Smokes);
659 g_HEs = KvGetYesOrNo(kv, "hegrenade", g_HEs);
660 g_NightVision = KvGetYesOrNo(kv, "nightvision", g_NightVision);
661 g_DefuseKits = KvGetYesOrNo(kv, "defusekits", g_DefuseKits);
662 g_HealthAmount = KvGetNum(kv, "health", g_HealthAmount);
667 if (KvJumpToKey(kv, "BotItems"))
669 /* Clear out old lists first */
670 g_BotPrimaryCount = 0;
671 g_BotSecondaryCount = 0;
673 if (KvGotoFirstSubKey(kv, false))
677 KvGetSectionName(kv, value, sizeof(value));
678 if (strcmp(value, "weapon") == 0)
680 KvGetString(kv, NULL_STRING, value, sizeof(value));
681 new index = DM_GetWeaponID(value);
684 new DmWeaponType:type = DM_GetWeaponType(index);
685 if (type == DmWeapon_Primary && g_BotPrimaryCount < CSSDM_MAX_WEAPONS)
687 g_BotPrimaryList[g_BotPrimaryCount++] = index;
688 } else if (type == DmWeapon_Secondary && g_BotSecondaryCount < CSSDM_MAX_WEAPONS) {
689 g_BotSecondaryList[g_BotSecondaryCount++] = index;
692 } else if (strcmp(value, "armor") == 0) {
693 g_BotArmor = KvGetNum(kv, NULL_STRING, 100);
694 } else if (strcmp(value, "helmet") == 0) {
695 g_BotHelmets = KvGetYesOrNo(kv, NULL_STRING, true);
696 } else if (strcmp(value, "flashbangs") == 0) {
697 g_BotFlashes = KvGetNum(kv, NULL_STRING);
698 } else if (strcmp(value, "smokegrenade") == 0) {
699 g_BotSmokes = KvGetYesOrNo(kv, NULL_STRING, false);
700 } else if (strcmp(value, "hegrenade") == 0) {
701 g_BotHEs = KvGetYesOrNo(kv, NULL_STRING, false);
702 } else if (strcmp(value, "defusekits") == 0) {
703 g_DefuseKits = KvGetYesOrNo(kv, NULL_STRING, true);
704 } else if (strcmp(value, "health") == 0) {
705 g_BotHealthAmount = KvGetNum(kv, NULL_STRING, 100);
707 } while (KvGotoNextKey(kv, false));
713 /* Load secondary weapons */
714 if (KvJumpToKey(kv, "SecondaryMenu"))
716 /* Clear out old list first */
717 g_SecondaryCount = 0;
719 if (KvGotoFirstSubKey(kv, false))
723 KvGetSectionName(kv, value, sizeof(value));
724 if (strcmp(value, "weapon") == 0)
726 KvGetString(kv, NULL_STRING, value, sizeof(value));
727 new index = DM_GetWeaponID(value);
728 if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Secondary)
730 g_SecondaryList[g_SecondaryCount++] = index;
733 } while (KvGotoNextKey(kv, false) && g_SecondaryCount < CSSDM_MAX_WEAPONS);
739 /* Load primary weapons */
740 if (KvJumpToKey(kv, "PrimaryMenu"))
742 /* Clear out old list first */
745 if (KvGotoFirstSubKey(kv, false))
749 KvGetSectionName(kv, value, sizeof(value));
750 if (strcmp(value, "weapon") == 0)
752 KvGetString(kv, NULL_STRING, value, sizeof(value));
753 new index = DM_GetWeaponID(value);
754 if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Primary)
756 g_PrimaryList[g_PrimaryCount++] = index;
759 } while (KvGotoNextKey(kv, false) && g_PrimaryCount < CSSDM_MAX_WEAPONS);
767 /* Build the primary menu */
768 if (g_hPrimaryMenu != INVALID_HANDLE)
770 CloseHandle(g_hPrimaryMenu);
772 g_hPrimaryMenu = CreateMenu(Menu_PrimaryHandler, MenuAction_DrawItem);
773 SetMenuTitle(g_hPrimaryMenu, "Primary weapon:");
774 for (new i=0; i<g_PrimaryCount; i++)
776 new index = g_PrimaryList[i];
777 DM_GetWeaponName(index, value, sizeof(value));
778 AddMenuItem(g_hPrimaryMenu, "", value);
780 AddMenuItem(g_hPrimaryMenu, "", "Random");
782 /* Build the secondary menu */
783 if (g_hSecondaryMenu != INVALID_HANDLE)
785 CloseHandle(g_hSecondaryMenu);
787 g_hSecondaryMenu = CreateMenu(Menu_SecondaryHandler, MenuAction_DrawItem);
788 SetMenuTitle(g_hSecondaryMenu, "Secondary weapon:");
789 for (new i=0; i<g_SecondaryCount; i++)
791 new index = g_SecondaryList[i];
792 DM_GetWeaponName(index, value, sizeof(value));
793 AddMenuItem(g_hSecondaryMenu, "", value);
795 AddMenuItem(g_hSecondaryMenu, "", "Random");
800 SetClientArmor(client, armor)
802 if (g_ArmorOffset == -1)
807 SetEntData(client, g_ArmorOffset, armor, 4, true);
810 SetClientHealth(client, health)
812 if (g_HealthOffset == -1)
817 SetEntData(client, g_HealthOffset, health, 4, true);
820 GiveGrenades(client, flashnum, bool:he, bool:smoke)
824 GivePlayerItem(client, "weapon_hegrenade");
826 for (new i=0; i<flashnum; i++)
828 GivePlayerItem(client, "weapon_flashbang");
832 GivePlayerItem(client, "weapon_smokegrenade");
836 GiveNightVision(client)
838 if (g_NVOffset == -1)
843 SetEntData(client, g_NVOffset, true, 1, true);