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 LoadTranslations("cssdm.phrases");
96 RegConsoleCmd("say", Command_Say);
97 RegConsoleCmd("say_team", Command_Say);
99 cssdm_enable_equipment = CreateConVar("cssdm_enable_equipment", "1", "Sets whether the equipment plugin is enabled");
100 g_ArmorOffset = FindSendPropOffs("CCSPlayer", "m_ArmorValue");
101 g_NVOffset = FindSendPropOffs("CCSPlayer", "m_bHasNightVision");
102 g_HealthOffset = FindSendPropOffs("CCSPlayer", "m_iHealth");
104 g_hEquipMenu = CreateMenu(Menu_EquipHandler, MenuAction_DrawItem);
105 SetMenuTitle(g_hEquipMenu, "Weapon Options:");
106 SetMenuExitButton(g_hEquipMenu, false);
107 AddMenuItem(g_hEquipMenu, "", "New weapons");
108 AddMenuItem(g_hEquipMenu, "", "Same weapons");
109 AddMenuItem(g_hEquipMenu, "", "Same weapons every time");
110 AddMenuItem(g_hEquipMenu, "", "Random weapons");
111 AddMenuItem(g_hEquipMenu, "", "Random weapons every time");
114 public OnConfigsExecuted()
118 if (!GetConVarInt(cssdm_enable_equipment))
124 if ((g_IsEnabled = LoadConfigFile("cfg/cssdm/cssdm.equip.txt")) == false)
126 LogError("[CSSDM] Could not find equipment file \"%s\"", "cfg/cssdm/cssdm.equip.txt");
130 /** See if there is a map version */
132 decl String:path[255];
133 GetCurrentMap(map, sizeof(map));
134 Format(path, sizeof(path), "cfg/cssdm/maps/%s.equip.txt", map);
136 if (FileExists(path))
138 LoadConfigFile(path);
142 public Action:DM_OnClientDeath(client)
144 if (g_SpawnTimers[client] != INVALID_HANDLE)
146 KillTimer(g_SpawnTimers[client]);
147 g_SpawnTimers[client] = INVALID_HANDLE;
149 g_GunMenuAvailable[client] = true;
152 public OnClientPutInServer(client)
154 g_GunMenuEnabled[client] = true;
155 g_GunMenuAvailable[client] = true;
156 g_PrimaryChoices[client] = -1;
157 g_SecondaryChoices[client] = -1;
160 public OnClientDisconnect(client)
162 if (g_SpawnTimers[client] != INVALID_HANDLE)
164 KillTimer(g_SpawnTimers[client]);
165 g_SpawnTimers[client] = INVALID_HANDLE;
169 public DM_OnClientSpawned(client)
171 if (!IsFakeClient(client))
173 if (g_ArmorAmount > 0)
175 GivePlayerItem(client, "item_assaultsuit");
176 GivePlayerItem(client, "item_assaultsuit");
177 SetClientArmor(client, g_ArmorAmount);
179 if (g_HealthAmount > 0)
181 SetClientHealth(client, g_HealthAmount);
185 GivePlayerItem(client, "item_kevlar");
186 GivePlayerItem(client, "item_kevlar");
188 GiveGrenades(client, g_Flashes, g_HEs, g_Smokes);
191 GiveNightVision(client);
194 /* See if we'll need to strip this bot */
195 if (g_BotSecondaryCount || g_BotPrimaryCount)
197 DM_StripBotItems(client);
201 GivePlayerItem(client, "item_assaultsuit");
202 GivePlayerItem(client, "item_assaultsuit");
203 SetClientArmor(client, g_BotArmor);
205 /* Make sure bot didn't buy armor */
206 SetClientArmor(client, 0);
208 if (g_BotHealthAmount > 0)
210 SetClientHealth(client, g_BotHealthAmount);
214 GivePlayerItem(client, "item_kevlar");
215 GivePlayerItem(client, "item_kevlar");
217 GiveGrenades(client, g_BotFlashes, g_BotHEs, g_BotSmokes);
219 if (g_SpawnTimers[client] == INVALID_HANDLE)
221 /* Small delay - we want to avoid ResetHUD */
222 g_SpawnTimers[client] = CreateTimer(0.1, PlayerPostSpawn, client);
226 public Action:PlayerPostSpawn(Handle:timer, any:client)
228 g_SpawnTimers[client] = INVALID_HANDLE;
235 if (IsFakeClient(client))
237 if (g_BotSecondaryCount > 0)
240 if (g_BotSecondaryCount == 1)
242 index = g_BotSecondaryList[0];
244 index = GetRandomInt(0, g_BotSecondaryCount-1);
245 index = g_BotSecondaryList[index];
247 decl String:classname[64];
248 DM_GetWeaponClassname(index, classname, sizeof(classname));
249 GivePlayerItem(client, classname);
251 if (g_BotPrimaryCount > 0)
254 if (g_BotPrimaryCount == 1)
256 index = g_BotPrimaryList[0];
258 index = GetRandomInt(0, g_BotPrimaryCount-1);
259 index = g_BotPrimaryList[index];
261 decl String:classname[64];
262 DM_GetWeaponClassname(index, classname, sizeof(classname));
263 GivePlayerItem(client, classname);
265 if (g_BotDefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
267 GivePlayerItem(client, "item_defuser");
270 if (g_DefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
272 GivePlayerItem(client, "item_defuser");
276 /* First, check if we should only be giving one of something automatically */
277 if ((g_SecondaryMenu == CSSDM_GUNMENU_RANDOM || g_SecondaryMenu == CSSDM_GUNMENU_YES)
278 && g_SecondaryCount == 1)
280 GiveSecondary(client, 0);
282 } else if (g_PrimaryMenu == CSSDM_GUNMENU_RANDOM && g_PrimaryCount > 1) {
283 GivePrimary(client, g_PrimaryCount);
287 if ((g_PrimaryMenu == CSSDM_GUNMENU_RANDOM || g_PrimaryMenu == CSSDM_GUNMENU_YES)
288 && g_PrimaryCount == 1)
290 GivePrimary(client, 0);
292 } else if (g_SecondaryMenu == CSSDM_GUNMENU_RANDOM && g_SecondaryCount > 1) {
293 GiveSecondary(client, g_SecondaryCount);
297 /* If we've already given two weapons, there is no need for a menu. */
303 if (g_GunMenuEnabled[client])
305 DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
307 GiveBothFromChoices(client);
314 public Action:Command_Say(client, args)
318 return Plugin_Continue;
321 new String:text[192];
322 GetCmdArg(1, text, sizeof(text));
324 if (strcmp(text, "guns") == 0)
326 if (!g_AllowGunCommand)
328 PrintToChat(client, "[CSSDM] %t", "GunsMenuDisabled");
329 return Plugin_Handled;
332 if (!ChooseFromSecondary() && !ChooseFromPrimary())
334 PrintToChat(client, "[CSSDM] %t", "GunsMenuNotAvailable");
335 return Plugin_Handled;
338 if (g_GunMenuEnabled[client])
340 PrintToChat(client, "[CSSDM] %t", "GunsMenuAlreadyEnabled");
341 return Plugin_Handled;
344 g_GunMenuEnabled[client] = true;
345 if (!g_GunMenuAvailable[client])
347 PrintToChat(client, "[CSSDM] %t", "GunsMenuReactivated");
349 DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
352 return Plugin_Handled;
355 return Plugin_Continue;
358 public Action:OnClientCommand(client, args)
360 if (!ShouldRun() || g_AllowBuy)
362 return Plugin_Continue;
366 GetCmdArg(0, cmd, sizeof(cmd));
368 if (StrEqual(cmd, "buy")
369 || StrEqual(cmd, "autobuy")
370 || StrEqual(cmd, "rebuy")
371 || StrEqual(cmd, "buyequip")
372 || StrEqual(cmd, "buymenu"))
374 return Plugin_Handled;
377 return Plugin_Continue;
380 public Menu_EquipHandler(Handle:menu, MenuAction:action, param1, param2)
387 if (action == MenuAction_DrawItem)
389 if (param2 == 1 || param2 == 2)
391 if (g_PrimaryChoices[param1] == -1 && g_PrimaryChoices[param1] == -1)
393 return ITEMDRAW_DISABLED;
396 return ITEMDRAW_DEFAULT;
398 else if (action == MenuAction_Select)
402 if (ChooseFromSecondary())
404 DisplayMenu(g_hSecondaryMenu, param1, MENU_TIME_FOREVER);
405 } else if (ChooseFromPrimary()) {
406 DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
408 } else if (param2 == 1) {
409 GiveBothFromChoices(param1);
410 } else if (param2 == 2) {
411 GiveBothFromChoices(param1);
412 g_GunMenuEnabled[param1] = false;
413 PrintToChat(param1, "[CSSDM] %t", "SayGunsNotify");
414 } else if (param2 == 3) {
415 GivePrimary(param1, g_PrimaryCount);
416 GiveSecondary(param1, g_SecondaryCount);
417 } else if (param2 == 4) {
418 GivePrimary(param1, g_PrimaryCount);
419 GiveSecondary(param1, g_SecondaryCount);
420 GiveBothFromChoices(param1);
421 g_GunMenuEnabled[param1] = false;
422 PrintToChat(param1, "[CSSDM] %t", "SayGunsNotify");
424 g_GunMenuAvailable[param1] = false;
430 public Menu_PrimaryHandler(Handle:menu, MenuAction:action, param1, param2)
437 if (action == MenuAction_DrawItem)
439 return ITEMDRAW_DEFAULT;
441 else if (action == MenuAction_Select)
443 GivePrimary(param1, param2);
449 public Menu_SecondaryHandler(Handle:menu, MenuAction:action, param1, param2)
456 if (action == MenuAction_DrawItem)
458 return ITEMDRAW_DEFAULT;
460 else if (action == MenuAction_Select)
462 GiveSecondary(param1, param2);
463 if (ChooseFromPrimary())
465 DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
472 bool:ChooseFromPrimary()
474 return (g_PrimaryMenu == CSSDM_GUNMENU_YES
475 && g_PrimaryCount > 1);
478 bool:ChooseFromSecondary()
480 return (g_SecondaryMenu == CSSDM_GUNMENU_YES
481 && g_SecondaryCount > 1);
484 GiveWeapon(client, index)
486 new DmWeaponType:type = DM_GetWeaponType(index);
488 new entity = DM_GetClientWeapon(client, type);
491 DM_DropWeapon(client, entity);
495 DM_GetWeaponClassname(index, cls, sizeof(cls));
497 return GivePlayerItem(client, cls);
500 GiveBothFromChoices(client)
502 if (ChooseFromPrimary())
504 GivePrimary(client, g_PrimaryChoices[client]);
507 if (ChooseFromSecondary())
509 GiveSecondary(client, g_SecondaryChoices[client]);
514 * Gives a primary weapon to a client.
515 * If the list_index is > the count, we choose one randomly.
516 * This is a hack so we can append "Random" to the choice list.
518 GivePrimary(client, list_index)
526 if (list_index >= g_PrimaryCount)
528 if (g_PrimaryCount > 1)
530 weapon_index = GetRandomInt(0, g_PrimaryCount-1);
531 weapon_index = g_PrimaryList[weapon_index];
533 weapon_index = g_PrimaryList[0];
536 weapon_index = g_PrimaryList[list_index];
539 g_PrimaryChoices[client] = list_index;
541 GiveWeapon(client, weapon_index);
545 * Gives a secondary weapon to a client.
546 * If the list_index is > the count, we choose one randomly.
547 * This is a hack so we can append "Random" to the choice list.
549 GiveSecondary(client, list_index)
557 if (list_index >= g_SecondaryCount)
559 if (g_SecondaryCount > 1)
561 weapon_index = GetRandomInt(0, g_SecondaryCount-1);
562 weapon_index = g_SecondaryList[weapon_index];
564 weapon_index = g_SecondaryList[0];
567 weapon_index = g_SecondaryList[list_index];
570 g_SecondaryChoices[client] = list_index;
572 GiveWeapon(client, weapon_index);
577 return (DM_IsRunning() && g_IsEnabled);
580 bool:KvGetYesOrNo(Handle:kv, const String:key[], bool:curdefault)
582 decl String:value[12];
583 KvGetString(kv, key, value, sizeof(value), curdefault ? "yes" : "no");
584 return (strcmp(value, "yes") == 0);
587 KvGetGunMenu(Handle:kv, const String:key[], def)
589 decl String:sdef[12];
590 if (def == CSSDM_GUNMENU_YES)
592 strcopy(sdef, sizeof(sdef), "yes");
593 } else if (def == CSSDM_GUNMENU_RANDOM) {
594 strcopy(sdef, sizeof(sdef), "random");
596 strcopy(sdef, sizeof(sdef), "no");
599 decl String:value[12];
600 KvGetString(kv, key, value, sizeof(value), sdef);
602 if (strcmp(value, "yes") == 0)
604 return CSSDM_GUNMENU_YES;
605 } else if (strcmp(value, "random") == 0) {
606 return CSSDM_GUNMENU_RANDOM;
609 return CSSDM_GUNMENU_NO;
614 g_PrimaryMenu = CSSDM_GUNMENU_YES;
615 g_SecondaryMenu = CSSDM_GUNMENU_YES;
622 g_NightVision = false;
624 g_HealthAmount = 100;
625 g_AllowGunCommand = true;
628 bool:LoadConfigFile(const String:path[])
630 new Handle:kv = CreateKeyValues("Equipment");
631 if (!FileToKeyValues(kv, path))
636 decl String:value[255];
639 if (KvJumpToKey(kv, "Settings"))
641 g_AllowGunCommand = KvGetYesOrNo(kv, "guns_command", g_AllowGunCommand);
645 /* Load menu options */
646 if (KvJumpToKey(kv, "Menus"))
648 g_PrimaryMenu = KvGetGunMenu(kv, "primary", g_PrimaryMenu);
649 g_SecondaryMenu = KvGetGunMenu(kv, "secondary", g_SecondaryMenu);
650 g_AllowBuy = KvGetYesOrNo(kv, "buy", g_AllowBuy);
654 /* Load automatic stuff */
655 if (KvJumpToKey(kv, "AutoItems"))
657 g_ArmorAmount = KvGetNum(kv, "armor", g_ArmorAmount);
658 g_Flashes = KvGetNum(kv, "flashbangs", g_Flashes);
659 g_Helmets = KvGetYesOrNo(kv, "helmet", g_Helmets);
660 g_Smokes = KvGetYesOrNo(kv, "smokegrenade", g_Smokes);
661 g_HEs = KvGetYesOrNo(kv, "hegrenade", g_HEs);
662 g_NightVision = KvGetYesOrNo(kv, "nightvision", g_NightVision);
663 g_DefuseKits = KvGetYesOrNo(kv, "defusekits", g_DefuseKits);
664 g_HealthAmount = KvGetNum(kv, "health", g_HealthAmount);
669 if (KvJumpToKey(kv, "BotItems"))
671 /* Clear out old lists first */
672 g_BotPrimaryCount = 0;
673 g_BotSecondaryCount = 0;
675 if (KvGotoFirstSubKey(kv, false))
679 KvGetSectionName(kv, value, sizeof(value));
680 if (strcmp(value, "weapon") == 0)
682 KvGetString(kv, NULL_STRING, value, sizeof(value));
683 new index = DM_GetWeaponID(value);
686 new DmWeaponType:type = DM_GetWeaponType(index);
687 if (type == DmWeapon_Primary && g_BotPrimaryCount < CSSDM_MAX_WEAPONS)
689 g_BotPrimaryList[g_BotPrimaryCount++] = index;
690 } else if (type == DmWeapon_Secondary && g_BotSecondaryCount < CSSDM_MAX_WEAPONS) {
691 g_BotSecondaryList[g_BotSecondaryCount++] = index;
694 } else if (strcmp(value, "armor") == 0) {
695 g_BotArmor = KvGetNum(kv, NULL_STRING, 100);
696 } else if (strcmp(value, "helmet") == 0) {
697 g_BotHelmets = KvGetYesOrNo(kv, NULL_STRING, true);
698 } else if (strcmp(value, "flashbangs") == 0) {
699 g_BotFlashes = KvGetNum(kv, NULL_STRING);
700 } else if (strcmp(value, "smokegrenade") == 0) {
701 g_BotSmokes = KvGetYesOrNo(kv, NULL_STRING, false);
702 } else if (strcmp(value, "hegrenade") == 0) {
703 g_BotHEs = KvGetYesOrNo(kv, NULL_STRING, false);
704 } else if (strcmp(value, "defusekits") == 0) {
705 g_DefuseKits = KvGetYesOrNo(kv, NULL_STRING, true);
706 } else if (strcmp(value, "health") == 0) {
707 g_BotHealthAmount = KvGetNum(kv, NULL_STRING, 100);
709 } while (KvGotoNextKey(kv, false));
715 /* Load secondary weapons */
716 if (KvJumpToKey(kv, "SecondaryMenu"))
718 /* Clear out old list first */
719 g_SecondaryCount = 0;
721 if (KvGotoFirstSubKey(kv, false))
725 KvGetSectionName(kv, value, sizeof(value));
726 if (strcmp(value, "weapon") == 0)
728 KvGetString(kv, NULL_STRING, value, sizeof(value));
729 new index = DM_GetWeaponID(value);
730 if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Secondary)
732 g_SecondaryList[g_SecondaryCount++] = index;
735 } while (KvGotoNextKey(kv, false) && g_SecondaryCount < CSSDM_MAX_WEAPONS);
741 /* Load primary weapons */
742 if (KvJumpToKey(kv, "PrimaryMenu"))
744 /* Clear out old list first */
747 if (KvGotoFirstSubKey(kv, false))
751 KvGetSectionName(kv, value, sizeof(value));
752 if (strcmp(value, "weapon") == 0)
754 KvGetString(kv, NULL_STRING, value, sizeof(value));
755 new index = DM_GetWeaponID(value);
756 if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Primary)
758 g_PrimaryList[g_PrimaryCount++] = index;
761 } while (KvGotoNextKey(kv, false) && g_PrimaryCount < CSSDM_MAX_WEAPONS);
769 /* Build the primary menu */
770 if (g_hPrimaryMenu != INVALID_HANDLE)
772 CloseHandle(g_hPrimaryMenu);
774 g_hPrimaryMenu = CreateMenu(Menu_PrimaryHandler, MenuAction_DrawItem);
775 SetMenuTitle(g_hPrimaryMenu, "Primary weapon:");
776 for (new i=0; i<g_PrimaryCount; i++)
778 new index = g_PrimaryList[i];
779 DM_GetWeaponName(index, value, sizeof(value));
780 AddMenuItem(g_hPrimaryMenu, "", value);
782 AddMenuItem(g_hPrimaryMenu, "", "Random");
784 /* Build the secondary menu */
785 if (g_hSecondaryMenu != INVALID_HANDLE)
787 CloseHandle(g_hSecondaryMenu);
789 g_hSecondaryMenu = CreateMenu(Menu_SecondaryHandler, MenuAction_DrawItem);
790 SetMenuTitle(g_hSecondaryMenu, "Secondary weapon:");
791 for (new i=0; i<g_SecondaryCount; i++)
793 new index = g_SecondaryList[i];
794 DM_GetWeaponName(index, value, sizeof(value));
795 AddMenuItem(g_hSecondaryMenu, "", value);
797 AddMenuItem(g_hSecondaryMenu, "", "Random");
802 SetClientArmor(client, armor)
804 if (g_ArmorOffset == -1)
809 SetEntData(client, g_ArmorOffset, armor, 4, true);
812 SetClientHealth(client, health)
814 if (g_HealthOffset == -1)
819 SetEntData(client, g_HealthOffset, health, 4, true);
822 GiveGrenades(client, flashnum, bool:he, bool:smoke)
826 GivePlayerItem(client, "weapon_hegrenade");
828 for (new i=0; i<flashnum; i++)
830 GivePlayerItem(client, "weapon_flashbang");
834 GivePlayerItem(client, "weapon_smokegrenade");
838 GiveNightVision(client)
840 if (g_NVOffset == -1)
845 SetEntData(client, g_NVOffset, true, 1, true);