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:g_hEquipCfg = INVALID_HANDLE; /* cssdm_equip_cfg cvar */
34 new g_ArmorOffset = -1; /* m_ArmorValue offset */
35 new g_NVOffset = -1; /* m_bHasNightVision offset */
36 new Handle:g_SpawnTimers[MAXPLAYERS+1]; /* Post-spawn timers */
37 new Handle:g_hPrimaryMenu = INVALID_HANDLE; /* Priamry menu Handle */
38 new Handle:g_hSecondaryMenu = INVALID_HANDLE; /* Secondary menu Handle */
39 new Handle:g_hEquipMenu = INVALID_HANDLE; /* Main equipment menu */
40 new bool:g_IsEnabled = false; /* Whether the plugin should work */
41 new g_PrimaryChoices[MAXPLAYERS+1]; /* Primary weapon selections */
42 new g_SecondaryChoices[MAXPLAYERS+1]; /* Secondary weapon selections */
43 new bool:g_GunMenuEnabled[MAXPLAYERS+1]; /* Whether the gun menu is enabled */
44 new bool:g_GunMenuAvailable[MAXPLAYERS+1]; /* Whether the gun menu is available */
45 new bool:g_AllowGunCommand = true; /* Whether the gun menu command is enabled */
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;
64 new g_BotPrimaryList[CSSDM_MAX_WEAPONS];
65 new g_BotSecondaryList[CSSDM_MAX_WEAPONS];
66 new g_BotPrimaryCount = 0;
67 new g_BotSecondaryCount = 0;
69 new bool:g_BotHelmets = false;
71 new bool:g_BotSmokes = false;
72 new bool:g_BotHEs = false;
73 new bool:g_BotDefuseKits = true;
76 public Plugin:myinfo =
78 name = "CS:S DM Equipment",
79 author = "AlliedModders LLC",
80 description = "Adds gun menu/equipment to CS:S DM",
81 version = CSSDM_VERSION,
82 url = "http://www.bailopan.net/cssdm/"
89 public OnPluginStart()
91 RegConsoleCmd("say", Command_Say);
92 RegConsoleCmd("say_team", Command_Say);
94 g_hEquipCfg = CreateConVar("cssdm_equip_cfg", "cssdm.equip.txt", "CS:S DM Equipment Config File");
95 g_ArmorOffset = FindSendPropOffs("CCSPlayer", "m_ArmorValue");
96 g_NVOffset = FindSendPropOffs("CCSPlayer", "m_bHasNightVision");
98 g_hEquipMenu = CreateMenu(Menu_EquipHandler, MenuAction_DrawItem);
99 SetMenuTitle(g_hEquipMenu, "Weapon Options:");
100 SetMenuExitButton(g_hEquipMenu, false);
101 AddMenuItem(g_hEquipMenu, "", "New weapons");
102 AddMenuItem(g_hEquipMenu, "", "Same weapons");
103 AddMenuItem(g_hEquipMenu, "", "Same weapons every time");
104 AddMenuItem(g_hEquipMenu, "", "Random weapons");
105 AddMenuItem(g_hEquipMenu, "", "Random weapons every time");
108 public OnConfigsExecuted()
110 decl String:cvar[64];
111 GetConVarString(g_hEquipCfg, cvar, sizeof(cvar));
118 decl String:path[255];
119 Format(path, sizeof(path), "cfg/cssdm/%s", cvar);
121 if ((g_IsEnabled = LoadConfigFile(path)) == false)
123 LogError("[CSSDM] Could not find equipment file \"%s\"", path);
127 if (strcmp(cvar, "cssdm.equip.txt") == 0)
129 /** See if there is a map version */
131 GetCurrentMap(map, sizeof(map));
132 Format(path, sizeof(path), "cfg/cssdm/%s/cssdm.equip.txt", map);
134 if (FileExists(path))
136 LoadConfigFile(path);
141 public Action:DM_OnClientDeath(client)
143 if (g_SpawnTimers[client] != INVALID_HANDLE)
145 KillTimer(g_SpawnTimers[client]);
146 g_SpawnTimers[client] = INVALID_HANDLE;
148 g_GunMenuAvailable[client] = true;
151 public OnClientPutInServer(client)
153 g_GunMenuEnabled[client] = true;
154 g_GunMenuAvailable[client] = true;
155 g_PrimaryChoices[client] = -1;
156 g_SecondaryChoices[client] = -1;
159 public OnClientDisconnect(client)
161 if (g_SpawnTimers[client] != INVALID_HANDLE)
163 KillTimer(g_SpawnTimers[client]);
164 g_SpawnTimers[client] = INVALID_HANDLE;
168 public DM_OnClientSpawned(client)
170 if (!IsFakeClient(client))
172 if (g_ArmorAmount > 0)
174 GivePlayerItem(client, "item_assaultsuit");
175 GivePlayerItem(client, "item_assaultsuit");
176 SetClientArmor(client, g_ArmorAmount);
180 GivePlayerItem(client, "item_kevlar");
181 GivePlayerItem(client, "item_kevlar");
183 GiveGrenades(client, g_Flashes, g_HEs, g_Smokes);
186 GiveNightVision(client);
189 /* See if we'll need to strip this bot */
190 if (g_BotSecondaryCount || g_BotPrimaryCount)
192 DM_StripBotItems(client);
196 GivePlayerItem(client, "item_assaultsuit");
197 GivePlayerItem(client, "item_assaultsuit");
198 SetClientArmor(client, g_BotArmor);
200 /* Make sure bot didn't buy armor */
201 SetClientArmor(client, 0);
205 GivePlayerItem(client, "item_kevlar");
206 GivePlayerItem(client, "item_kevlar");
208 GiveGrenades(client, g_BotFlashes, g_BotHEs, g_BotSmokes);
210 if (g_SpawnTimers[client] == INVALID_HANDLE)
212 /* Small delay - we want to avoid ResetHUD */
213 g_SpawnTimers[client] = CreateTimer(0.1, PlayerPostSpawn, client);
217 public Action:PlayerPostSpawn(Handle:timer, any:client)
219 g_SpawnTimers[client] = INVALID_HANDLE;
226 if (IsFakeClient(client))
228 if (g_BotSecondaryCount > 0)
231 if (g_BotSecondaryCount == 1)
233 index = g_BotSecondaryList[0];
235 index = GetRandomInt(0, g_BotSecondaryCount-1);
236 index = g_BotSecondaryList[index];
238 decl String:classname[64];
239 DM_GetWeaponClassname(index, classname, sizeof(classname));
240 GivePlayerItem(client, classname);
242 if (g_BotPrimaryCount > 0)
245 if (g_BotPrimaryCount == 1)
247 index = g_BotPrimaryList[0];
249 index = GetRandomInt(0, g_BotPrimaryCount-1);
250 index = g_BotPrimaryList[index];
252 decl String:classname[64];
253 DM_GetWeaponClassname(index, classname, sizeof(classname));
254 GivePlayerItem(client, classname);
256 if (g_BotDefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
258 GivePlayerItem(client, "item_defuser");
261 if (g_DefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
263 GivePlayerItem(client, "item_defuser");
264 PrintToChat(client, "Oh my god, you have a defuse kit");
268 /* First, check if we should only be giving one of something automatically */
269 if ((g_SecondaryMenu == CSSDM_GUNMENU_RANDOM || g_SecondaryMenu == CSSDM_GUNMENU_YES)
270 && g_SecondaryCount == 1)
272 GiveSecondary(client, 0);
274 } else if (g_PrimaryMenu == CSSDM_GUNMENU_RANDOM && g_PrimaryCount > 1) {
275 GivePrimary(client, g_PrimaryCount);
279 if ((g_PrimaryMenu == CSSDM_GUNMENU_RANDOM || g_PrimaryMenu == CSSDM_GUNMENU_YES)
280 && g_PrimaryCount == 1)
282 GivePrimary(client, 0);
284 } else if (g_SecondaryMenu == CSSDM_GUNMENU_RANDOM && g_SecondaryCount > 1) {
285 GiveSecondary(client, g_SecondaryCount);
289 /* If we've already given two weapons, there is no need for a menu. */
295 if (g_GunMenuEnabled[client])
297 DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
299 GiveBothFromChoices(client);
306 public Action:Command_Say(client, args)
310 return Plugin_Continue;
313 new String:text[192];
314 GetCmdArg(1, text, sizeof(text));
316 if (strcmp(text, "guns") == 0)
318 if (!g_AllowGunCommand)
320 PrintToChat(client, "[CSSDM] The guns menu command is disabled.");
321 return Plugin_Handled;
324 if (!ChooseFromSecondary() && !ChooseFromPrimary())
326 PrintToChat(client, "[CSSDM] The guns menu is not available.");
327 return Plugin_Handled;
330 if (g_GunMenuEnabled[client])
332 PrintToChat(client, "[CSSDM] Your guns menu is already enabled.");
333 return Plugin_Handled;
336 g_GunMenuEnabled[client] = true;
337 if (!g_GunMenuAvailable[client])
339 PrintToChat(client, "[CSSDM] Your guns menu has been enabled for your next respawn.");
341 DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
344 return Plugin_Handled;
347 return Plugin_Continue;
350 public Action:OnClientCommand(client, args)
352 if (!ShouldRun() || g_AllowBuy)
354 return Plugin_Continue;
358 GetCmdArg(0, cmd, sizeof(cmd));
360 if (StrEqual(cmd, "buy")
361 || StrEqual(cmd, "autobuy")
362 || StrEqual(cmd, "rebuy")
363 || StrEqual(cmd, "buyequip")
364 || StrEqual(cmd, "buymenu"))
366 return Plugin_Handled;
369 return Plugin_Continue;
372 public Menu_EquipHandler(Handle:menu, MenuAction:action, param1, param2)
379 if (action == MenuAction_DrawItem)
381 if (param2 == 1 || param2 == 2)
383 if (g_PrimaryChoices[param1] == -1 && g_PrimaryChoices[param1] == -1)
385 return ITEMDRAW_DISABLED;
388 return ITEMDRAW_DEFAULT;
390 else if (action == MenuAction_Select)
394 if (ChooseFromSecondary())
396 DisplayMenu(g_hSecondaryMenu, param1, MENU_TIME_FOREVER);
397 } else if (ChooseFromPrimary()) {
398 DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
400 } else if (param2 == 1) {
401 GiveBothFromChoices(param1);
402 } else if (param2 == 2) {
403 GiveBothFromChoices(param1);
404 g_GunMenuEnabled[param1] = false;
405 PrintToChat(param1, "[CSSDM] Say \"guns\" in chat to re-enable your menu.");
406 } else if (param2 == 3) {
407 GivePrimary(param1, g_PrimaryCount);
408 GiveSecondary(param1, g_SecondaryCount);
409 } else if (param2 == 4) {
410 GivePrimary(param1, g_PrimaryCount);
411 GiveSecondary(param1, g_SecondaryCount);
412 GiveBothFromChoices(param1);
413 g_GunMenuEnabled[param1] = false;
414 PrintToChat(param1, "[CSSDM] Say \"guns\" in chat to re-enable your menu.");
416 g_GunMenuAvailable[param1] = false;
422 public Menu_PrimaryHandler(Handle:menu, MenuAction:action, param1, param2)
429 if (action == MenuAction_DrawItem)
431 return ITEMDRAW_DEFAULT;
433 else if (action == MenuAction_Select)
435 GivePrimary(param1, param2);
441 public Menu_SecondaryHandler(Handle:menu, MenuAction:action, param1, param2)
448 if (action == MenuAction_DrawItem)
450 return ITEMDRAW_DEFAULT;
452 else if (action == MenuAction_Select)
454 GiveSecondary(param1, param2);
455 if (ChooseFromPrimary())
457 DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
464 bool:ChooseFromPrimary()
466 return (g_PrimaryMenu == CSSDM_GUNMENU_YES
467 && g_PrimaryCount > 1);
470 bool:ChooseFromSecondary()
472 return (g_SecondaryMenu == CSSDM_GUNMENU_YES
473 && g_SecondaryCount > 1);
476 GiveWeapon(client, index)
478 new DmWeaponType:type = DM_GetWeaponType(index);
480 new entity = DM_GetClientWeapon(client, type);
483 DM_DropWeapon(client, entity);
487 DM_GetWeaponClassname(index, cls, sizeof(cls));
489 return GivePlayerItem(client, cls);
492 GiveBothFromChoices(client)
494 if (ChooseFromPrimary())
496 GivePrimary(client, g_PrimaryChoices[client]);
499 if (ChooseFromSecondary())
501 GiveSecondary(client, g_SecondaryChoices[client]);
506 * Gives a primary weapon to a client.
507 * If the list_index is > the count, we choose one randomly.
508 * This is a hack so we can append "Random" to the choice list.
510 GivePrimary(client, list_index)
518 if (list_index >= g_PrimaryCount)
520 if (g_PrimaryCount > 1)
522 weapon_index = GetRandomInt(0, g_PrimaryCount-1);
523 weapon_index = g_PrimaryList[weapon_index];
525 weapon_index = g_PrimaryList[0];
528 weapon_index = g_PrimaryList[list_index];
531 g_PrimaryChoices[client] = list_index;
533 GiveWeapon(client, weapon_index);
537 * Gives a secondary weapon to a client.
538 * If the list_index is > the count, we choose one randomly.
539 * This is a hack so we can append "Random" to the choice list.
541 GiveSecondary(client, list_index)
549 if (list_index >= g_SecondaryCount)
551 if (g_SecondaryCount > 1)
553 weapon_index = GetRandomInt(0, g_SecondaryCount-1);
554 weapon_index = g_SecondaryList[weapon_index];
556 weapon_index = g_SecondaryList[0];
559 weapon_index = g_SecondaryList[list_index];
562 g_SecondaryChoices[client] = list_index;
564 GiveWeapon(client, weapon_index);
569 return (DM_IsRunning() && g_IsEnabled);
572 bool:KvGetYesOrNo(Handle:kv, const String:key[], const String:def[])
574 decl String:value[12];
575 KvGetString(kv, key, value, sizeof(value), def);
576 return (strcmp(value, "yes") == 0);
579 KvGetGunMenu(Handle:kv, const String:key[], const String:def[])
581 decl String:value[12];
582 KvGetString(kv, key, value, sizeof(value), def);
584 if (strcmp(value, "yes") == 0)
586 return CSSDM_GUNMENU_YES;
587 } else if (strcmp(value, "random") == 0) {
588 return CSSDM_GUNMENU_RANDOM;
591 return CSSDM_GUNMENU_NO;
594 bool:LoadConfigFile(const String:path[])
596 new Handle:kv = CreateKeyValues("Equipment");
597 if (!FileToKeyValues(kv, path))
602 decl String:value[255];
605 if (KvJumpToKey(kv, "Settings"))
607 g_AllowGunCommand = KvGetYesOrNo(kv, "guns_command", "yes");
611 /* Load menu options */
612 if (KvJumpToKey(kv, "Menus"))
614 g_PrimaryMenu = KvGetGunMenu(kv, "primary", "yes");
615 g_SecondaryMenu = KvGetGunMenu(kv, "secondary", "yes");
616 g_AllowBuy = KvGetYesOrNo(kv, "buy", "no");
620 /* Load automatic stuff */
621 if (KvJumpToKey(kv, "AutoItems"))
623 g_ArmorAmount = KvGetNum(kv, "armor", 100);
624 g_Flashes = KvGetNum(kv, "flashbangs");
625 g_Helmets = KvGetYesOrNo(kv, "helmet", "yes");
626 g_Smokes = KvGetYesOrNo(kv, "smokegrenade", "no");
627 g_HEs = KvGetYesOrNo(kv, "hegrenade", "no");
628 g_NightVision = KvGetYesOrNo(kv, "nightvision", "no");
629 g_DefuseKits = KvGetYesOrNo(kv, "defusekits", "yes");
634 if (KvJumpToKey(kv, "BotItems"))
636 /* Clear out old lists first */
637 g_BotPrimaryCount = 0;
638 g_BotSecondaryCount = 0;
640 if (KvGotoFirstSubKey(kv, false))
644 KvGetSectionName(kv, value, sizeof(value));
645 if (strcmp(value, "weapon") == 0)
647 KvGetString(kv, NULL_STRING, value, sizeof(value));
648 new index = DM_GetWeaponID(value);
651 new DmWeaponType:type = DM_GetWeaponType(index);
652 if (type == DmWeapon_Primary && g_BotPrimaryCount < CSSDM_MAX_WEAPONS)
654 g_BotPrimaryList[g_BotPrimaryCount++] = index;
655 } else if (type == DmWeapon_Secondary && g_BotSecondaryCount < CSSDM_MAX_WEAPONS) {
656 g_BotSecondaryList[g_BotSecondaryCount++] = index;
659 } else if (strcmp(value, "armor") == 0) {
660 g_BotArmor = KvGetNum(kv, NULL_STRING, 100);
661 } else if (strcmp(value, "helmet") == 0) {
662 g_BotHelmets = KvGetYesOrNo(kv, NULL_STRING, "yes");
663 } else if (strcmp(value, "flashbangs") == 0) {
664 g_BotFlashes = KvGetNum(kv, NULL_STRING);
665 } else if (strcmp(value, "smokegrenade") == 0) {
666 g_BotSmokes = KvGetYesOrNo(kv, NULL_STRING, "no");
667 } else if (strcmp(value, "hegrenade") == 0) {
668 g_BotHEs = KvGetYesOrNo(kv, NULL_STRING, "no");
669 } else if (strcmp(value, "defusekits") == 0) {
670 g_DefuseKits = KvGetYesOrNo(kv, NULL_STRING, "yes");
672 } while (KvGotoNextKey(kv, false));
678 /* Load secondary weapons */
679 if (KvJumpToKey(kv, "SecondaryMenu"))
681 /* Clear out old list first */
682 g_SecondaryCount = 0;
684 if (KvGotoFirstSubKey(kv, false))
688 KvGetSectionName(kv, value, sizeof(value));
689 if (strcmp(value, "weapon") == 0)
691 KvGetString(kv, NULL_STRING, value, sizeof(value));
692 new index = DM_GetWeaponID(value);
693 if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Secondary)
695 g_SecondaryList[g_SecondaryCount++] = index;
698 } while (KvGotoNextKey(kv, false) && g_SecondaryCount < CSSDM_MAX_WEAPONS);
704 /* Load primary weapons */
705 if (KvJumpToKey(kv, "PrimaryMenu"))
707 /* Clear out old list first */
710 if (KvGotoFirstSubKey(kv, false))
714 KvGetSectionName(kv, value, sizeof(value));
715 if (strcmp(value, "weapon") == 0)
717 KvGetString(kv, NULL_STRING, value, sizeof(value));
718 new index = DM_GetWeaponID(value);
719 if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Primary)
721 g_PrimaryList[g_PrimaryCount++] = index;
724 } while (KvGotoNextKey(kv, false) && g_PrimaryCount < CSSDM_MAX_WEAPONS);
732 /* Build the primary menu */
733 if (g_hPrimaryMenu != INVALID_HANDLE)
735 CloseHandle(g_hPrimaryMenu);
737 g_hPrimaryMenu = CreateMenu(Menu_PrimaryHandler, MenuAction_DrawItem);
738 SetMenuTitle(g_hPrimaryMenu, "Primary weapon:");
739 for (new i=0; i<g_PrimaryCount; i++)
741 new index = g_PrimaryList[i];
742 DM_GetWeaponName(index, value, sizeof(value));
743 AddMenuItem(g_hPrimaryMenu, "", value);
745 AddMenuItem(g_hPrimaryMenu, "", "Random");
747 /* Build the secondary menu */
748 if (g_hSecondaryMenu != INVALID_HANDLE)
750 CloseHandle(g_hSecondaryMenu);
752 g_hSecondaryMenu = CreateMenu(Menu_SecondaryHandler, MenuAction_DrawItem);
753 SetMenuTitle(g_hSecondaryMenu, "Secondary weapon:");
754 for (new i=0; i<g_SecondaryCount; i++)
756 new index = g_SecondaryList[i];
757 DM_GetWeaponName(index, value, sizeof(value));
758 AddMenuItem(g_hSecondaryMenu, "", value);
760 AddMenuItem(g_hSecondaryMenu, "", "Random");
765 SetClientArmor(client, armor)
767 if (g_ArmorOffset == -1)
772 SetEntData(client, g_ArmorOffset, armor, 4, true);
775 GiveGrenades(client, flashnum, bool:he, bool:smoke)
779 GivePlayerItem(client, "weapon_hegrenade");
781 for (new i=0; i<flashnum; i++)
783 GivePlayerItem(client, "weapon_flashbang");
787 GivePlayerItem(client, "weapon_smokegrenade");
791 GiveNightVision(client)
793 if (g_NVOffset == -1)
798 SetEntData(client, g_NVOffset, true, 1, true);