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 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 g_hEquipCfg = CreateConVar("cssdm_equip_cfg", "cssdm.equip.txt", "CS:S DM Equipment Config File");
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 decl String:cvar[64];
117 GetConVarString(g_hEquipCfg, cvar, sizeof(cvar));
124 decl String:path[255];
125 Format(path, sizeof(path), "cfg/cssdm/%s", cvar);
127 if ((g_IsEnabled = LoadConfigFile(path)) == false)
129 LogError("[CSSDM] Could not find equipment file \"%s\"", path);
133 if (strcmp(cvar, "cssdm.equip.txt") == 0)
135 /** See if there is a map version */
137 GetCurrentMap(map, sizeof(map));
138 Format(path, sizeof(path), "cfg/cssdm/%s/cssdm.equip.txt", map);
140 if (FileExists(path))
142 LoadConfigFile(path);
147 public Action:DM_OnClientDeath(client)
149 if (g_SpawnTimers[client] != INVALID_HANDLE)
151 KillTimer(g_SpawnTimers[client]);
152 g_SpawnTimers[client] = INVALID_HANDLE;
154 g_GunMenuAvailable[client] = true;
157 public OnClientPutInServer(client)
159 g_GunMenuEnabled[client] = true;
160 g_GunMenuAvailable[client] = true;
161 g_PrimaryChoices[client] = -1;
162 g_SecondaryChoices[client] = -1;
165 public OnClientDisconnect(client)
167 if (g_SpawnTimers[client] != INVALID_HANDLE)
169 KillTimer(g_SpawnTimers[client]);
170 g_SpawnTimers[client] = INVALID_HANDLE;
174 public DM_OnClientSpawned(client)
176 if (!IsFakeClient(client))
178 if (g_ArmorAmount > 0)
180 GivePlayerItem(client, "item_assaultsuit");
181 GivePlayerItem(client, "item_assaultsuit");
182 SetClientArmor(client, g_ArmorAmount);
184 if (g_HealthAmount > 0)
186 SetClientHealth(client, g_HealthAmount);
190 GivePlayerItem(client, "item_kevlar");
191 GivePlayerItem(client, "item_kevlar");
193 GiveGrenades(client, g_Flashes, g_HEs, g_Smokes);
196 GiveNightVision(client);
199 /* See if we'll need to strip this bot */
200 if (g_BotSecondaryCount || g_BotPrimaryCount)
202 DM_StripBotItems(client);
206 GivePlayerItem(client, "item_assaultsuit");
207 GivePlayerItem(client, "item_assaultsuit");
208 SetClientArmor(client, g_BotArmor);
210 /* Make sure bot didn't buy armor */
211 SetClientArmor(client, 0);
213 if (g_BotHealthAmount > 0)
215 SetClientHealth(client, g_BotHealthAmount);
219 GivePlayerItem(client, "item_kevlar");
220 GivePlayerItem(client, "item_kevlar");
222 GiveGrenades(client, g_BotFlashes, g_BotHEs, g_BotSmokes);
224 if (g_SpawnTimers[client] == INVALID_HANDLE)
226 /* Small delay - we want to avoid ResetHUD */
227 g_SpawnTimers[client] = CreateTimer(0.1, PlayerPostSpawn, client);
231 public Action:PlayerPostSpawn(Handle:timer, any:client)
233 g_SpawnTimers[client] = INVALID_HANDLE;
240 if (IsFakeClient(client))
242 if (g_BotSecondaryCount > 0)
245 if (g_BotSecondaryCount == 1)
247 index = g_BotSecondaryList[0];
249 index = GetRandomInt(0, g_BotSecondaryCount-1);
250 index = g_BotSecondaryList[index];
252 decl String:classname[64];
253 DM_GetWeaponClassname(index, classname, sizeof(classname));
254 GivePlayerItem(client, classname);
256 if (g_BotPrimaryCount > 0)
259 if (g_BotPrimaryCount == 1)
261 index = g_BotPrimaryList[0];
263 index = GetRandomInt(0, g_BotPrimaryCount-1);
264 index = g_BotPrimaryList[index];
266 decl String:classname[64];
267 DM_GetWeaponClassname(index, classname, sizeof(classname));
268 GivePlayerItem(client, classname);
270 if (g_BotDefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
272 GivePlayerItem(client, "item_defuser");
275 if (g_DefuseKits && GetClientTeam(client) == CSSDM_TEAM_CT)
277 GivePlayerItem(client, "item_defuser");
281 /* First, check if we should only be giving one of something automatically */
282 if ((g_SecondaryMenu == CSSDM_GUNMENU_RANDOM || g_SecondaryMenu == CSSDM_GUNMENU_YES)
283 && g_SecondaryCount == 1)
285 GiveSecondary(client, 0);
287 } else if (g_PrimaryMenu == CSSDM_GUNMENU_RANDOM && g_PrimaryCount > 1) {
288 GivePrimary(client, g_PrimaryCount);
292 if ((g_PrimaryMenu == CSSDM_GUNMENU_RANDOM || g_PrimaryMenu == CSSDM_GUNMENU_YES)
293 && g_PrimaryCount == 1)
295 GivePrimary(client, 0);
297 } else if (g_SecondaryMenu == CSSDM_GUNMENU_RANDOM && g_SecondaryCount > 1) {
298 GiveSecondary(client, g_SecondaryCount);
302 /* If we've already given two weapons, there is no need for a menu. */
308 if (g_GunMenuEnabled[client])
310 DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
312 GiveBothFromChoices(client);
319 public Action:Command_Say(client, args)
323 return Plugin_Continue;
326 new String:text[192];
327 GetCmdArg(1, text, sizeof(text));
329 if (strcmp(text, "guns") == 0)
331 if (!g_AllowGunCommand)
333 PrintToChat(client, "[CSSDM] %t", "GunsMenuDisabled");
334 return Plugin_Handled;
337 if (!ChooseFromSecondary() && !ChooseFromPrimary())
339 PrintToChat(client, "[CSSDM] %t", "GunsMenuNotAvailable");
340 return Plugin_Handled;
343 if (g_GunMenuEnabled[client])
345 PrintToChat(client, "[CSSDM] %t", "GunsMenuAlreadyEnabled");
346 return Plugin_Handled;
349 g_GunMenuEnabled[client] = true;
350 if (!g_GunMenuAvailable[client])
352 PrintToChat(client, "[CSSDM] %t", "GunsMenuReactivated");
354 DisplayMenu(g_hEquipMenu, client, MENU_TIME_FOREVER);
357 return Plugin_Handled;
360 return Plugin_Continue;
363 public Action:OnClientCommand(client, args)
365 if (!ShouldRun() || g_AllowBuy)
367 return Plugin_Continue;
371 GetCmdArg(0, cmd, sizeof(cmd));
373 if (StrEqual(cmd, "buy")
374 || StrEqual(cmd, "autobuy")
375 || StrEqual(cmd, "rebuy")
376 || StrEqual(cmd, "buyequip")
377 || StrEqual(cmd, "buymenu"))
379 return Plugin_Handled;
382 return Plugin_Continue;
385 public Menu_EquipHandler(Handle:menu, MenuAction:action, param1, param2)
392 if (action == MenuAction_DrawItem)
394 if (param2 == 1 || param2 == 2)
396 if (g_PrimaryChoices[param1] == -1 && g_PrimaryChoices[param1] == -1)
398 return ITEMDRAW_DISABLED;
401 return ITEMDRAW_DEFAULT;
403 else if (action == MenuAction_Select)
407 if (ChooseFromSecondary())
409 DisplayMenu(g_hSecondaryMenu, param1, MENU_TIME_FOREVER);
410 } else if (ChooseFromPrimary()) {
411 DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
413 } else if (param2 == 1) {
414 GiveBothFromChoices(param1);
415 } else if (param2 == 2) {
416 GiveBothFromChoices(param1);
417 g_GunMenuEnabled[param1] = false;
418 PrintToChat(param1, "[CSSDM] %t", "SayGunsNotify");
419 } else if (param2 == 3) {
420 GivePrimary(param1, g_PrimaryCount);
421 GiveSecondary(param1, g_SecondaryCount);
422 } else if (param2 == 4) {
423 GivePrimary(param1, g_PrimaryCount);
424 GiveSecondary(param1, g_SecondaryCount);
425 GiveBothFromChoices(param1);
426 g_GunMenuEnabled[param1] = false;
427 PrintToChat(param1, "[CSSDM] %t", "SayGunsNotify");
429 g_GunMenuAvailable[param1] = false;
435 public Menu_PrimaryHandler(Handle:menu, MenuAction:action, param1, param2)
442 if (action == MenuAction_DrawItem)
444 return ITEMDRAW_DEFAULT;
446 else if (action == MenuAction_Select)
448 GivePrimary(param1, param2);
454 public Menu_SecondaryHandler(Handle:menu, MenuAction:action, param1, param2)
461 if (action == MenuAction_DrawItem)
463 return ITEMDRAW_DEFAULT;
465 else if (action == MenuAction_Select)
467 GiveSecondary(param1, param2);
468 if (ChooseFromPrimary())
470 DisplayMenu(g_hPrimaryMenu, param1, MENU_TIME_FOREVER);
477 bool:ChooseFromPrimary()
479 return (g_PrimaryMenu == CSSDM_GUNMENU_YES
480 && g_PrimaryCount > 1);
483 bool:ChooseFromSecondary()
485 return (g_SecondaryMenu == CSSDM_GUNMENU_YES
486 && g_SecondaryCount > 1);
489 GiveWeapon(client, index)
491 new DmWeaponType:type = DM_GetWeaponType(index);
493 new entity = DM_GetClientWeapon(client, type);
496 DM_DropWeapon(client, entity);
500 DM_GetWeaponClassname(index, cls, sizeof(cls));
502 return GivePlayerItem(client, cls);
505 GiveBothFromChoices(client)
507 if (ChooseFromPrimary())
509 GivePrimary(client, g_PrimaryChoices[client]);
512 if (ChooseFromSecondary())
514 GiveSecondary(client, g_SecondaryChoices[client]);
519 * Gives a primary weapon to a client.
520 * If the list_index is > the count, we choose one randomly.
521 * This is a hack so we can append "Random" to the choice list.
523 GivePrimary(client, list_index)
531 if (list_index >= g_PrimaryCount)
533 if (g_PrimaryCount > 1)
535 weapon_index = GetRandomInt(0, g_PrimaryCount-1);
536 weapon_index = g_PrimaryList[weapon_index];
538 weapon_index = g_PrimaryList[0];
541 weapon_index = g_PrimaryList[list_index];
544 g_PrimaryChoices[client] = list_index;
546 GiveWeapon(client, weapon_index);
550 * Gives a secondary weapon to a client.
551 * If the list_index is > the count, we choose one randomly.
552 * This is a hack so we can append "Random" to the choice list.
554 GiveSecondary(client, list_index)
562 if (list_index >= g_SecondaryCount)
564 if (g_SecondaryCount > 1)
566 weapon_index = GetRandomInt(0, g_SecondaryCount-1);
567 weapon_index = g_SecondaryList[weapon_index];
569 weapon_index = g_SecondaryList[0];
572 weapon_index = g_SecondaryList[list_index];
575 g_SecondaryChoices[client] = list_index;
577 GiveWeapon(client, weapon_index);
582 return (DM_IsRunning() && g_IsEnabled);
585 bool:KvGetYesOrNo(Handle:kv, const String:key[], bool:curdefault)
587 decl String:value[12];
588 KvGetString(kv, key, value, sizeof(value), curdefault ? "yes" : "no");
589 return (strcmp(value, "yes") == 0);
592 KvGetGunMenu(Handle:kv, const String:key[], def)
594 decl String:sdef[12];
595 if (def == CSSDM_GUNMENU_YES)
597 strcopy(sdef, sizeof(sdef), "yes");
598 } else if (def == CSSDM_GUNMENU_RANDOM) {
599 strcopy(sdef, sizeof(sdef), "random");
601 strcopy(sdef, sizeof(sdef), "no");
604 decl String:value[12];
605 KvGetString(kv, key, value, sizeof(value), sdef);
607 if (strcmp(value, "yes") == 0)
609 return CSSDM_GUNMENU_YES;
610 } else if (strcmp(value, "random") == 0) {
611 return CSSDM_GUNMENU_RANDOM;
614 return CSSDM_GUNMENU_NO;
619 g_PrimaryMenu = CSSDM_GUNMENU_YES;
620 g_SecondaryMenu = CSSDM_GUNMENU_YES;
627 g_NightVision = false;
629 g_HealthAmount = 100;
630 g_AllowGunCommand = true;
633 bool:LoadConfigFile(const String:path[])
635 new Handle:kv = CreateKeyValues("Equipment");
636 if (!FileToKeyValues(kv, path))
641 decl String:value[255];
644 if (KvJumpToKey(kv, "Settings"))
646 g_AllowGunCommand = KvGetYesOrNo(kv, "guns_command", g_AllowGunCommand);
650 /* Load menu options */
651 if (KvJumpToKey(kv, "Menus"))
653 g_PrimaryMenu = KvGetGunMenu(kv, "primary", g_PrimaryMenu);
654 g_SecondaryMenu = KvGetGunMenu(kv, "secondary", g_SecondaryMenu);
655 g_AllowBuy = KvGetYesOrNo(kv, "buy", g_AllowBuy);
659 /* Load automatic stuff */
660 if (KvJumpToKey(kv, "AutoItems"))
662 g_ArmorAmount = KvGetNum(kv, "armor", g_ArmorAmount);
663 g_Flashes = KvGetNum(kv, "flashbangs", g_Flashes);
664 g_Helmets = KvGetYesOrNo(kv, "helmet", g_Helmets);
665 g_Smokes = KvGetYesOrNo(kv, "smokegrenade", g_Smokes);
666 g_HEs = KvGetYesOrNo(kv, "hegrenade", g_HEs);
667 g_NightVision = KvGetYesOrNo(kv, "nightvision", g_NightVision);
668 g_DefuseKits = KvGetYesOrNo(kv, "defusekits", g_DefuseKits);
669 g_HealthAmount = KvGetNum(kv, "health", g_HealthAmount);
674 if (KvJumpToKey(kv, "BotItems"))
676 /* Clear out old lists first */
677 g_BotPrimaryCount = 0;
678 g_BotSecondaryCount = 0;
680 if (KvGotoFirstSubKey(kv, false))
684 KvGetSectionName(kv, value, sizeof(value));
685 if (strcmp(value, "weapon") == 0)
687 KvGetString(kv, NULL_STRING, value, sizeof(value));
688 new index = DM_GetWeaponID(value);
691 new DmWeaponType:type = DM_GetWeaponType(index);
692 if (type == DmWeapon_Primary && g_BotPrimaryCount < CSSDM_MAX_WEAPONS)
694 g_BotPrimaryList[g_BotPrimaryCount++] = index;
695 } else if (type == DmWeapon_Secondary && g_BotSecondaryCount < CSSDM_MAX_WEAPONS) {
696 g_BotSecondaryList[g_BotSecondaryCount++] = index;
699 } else if (strcmp(value, "armor") == 0) {
700 g_BotArmor = KvGetNum(kv, NULL_STRING, 100);
701 } else if (strcmp(value, "helmet") == 0) {
702 g_BotHelmets = KvGetYesOrNo(kv, NULL_STRING, true);
703 } else if (strcmp(value, "flashbangs") == 0) {
704 g_BotFlashes = KvGetNum(kv, NULL_STRING);
705 } else if (strcmp(value, "smokegrenade") == 0) {
706 g_BotSmokes = KvGetYesOrNo(kv, NULL_STRING, false);
707 } else if (strcmp(value, "hegrenade") == 0) {
708 g_BotHEs = KvGetYesOrNo(kv, NULL_STRING, false);
709 } else if (strcmp(value, "defusekits") == 0) {
710 g_DefuseKits = KvGetYesOrNo(kv, NULL_STRING, true);
711 } else if (strcmp(value, "health") == 0) {
712 g_BotHealthAmount = KvGetNum(kv, NULL_STRING, 100);
714 } while (KvGotoNextKey(kv, false));
720 /* Load secondary weapons */
721 if (KvJumpToKey(kv, "SecondaryMenu"))
723 /* Clear out old list first */
724 g_SecondaryCount = 0;
726 if (KvGotoFirstSubKey(kv, false))
730 KvGetSectionName(kv, value, sizeof(value));
731 if (strcmp(value, "weapon") == 0)
733 KvGetString(kv, NULL_STRING, value, sizeof(value));
734 new index = DM_GetWeaponID(value);
735 if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Secondary)
737 g_SecondaryList[g_SecondaryCount++] = index;
740 } while (KvGotoNextKey(kv, false) && g_SecondaryCount < CSSDM_MAX_WEAPONS);
746 /* Load primary weapons */
747 if (KvJumpToKey(kv, "PrimaryMenu"))
749 /* Clear out old list first */
752 if (KvGotoFirstSubKey(kv, false))
756 KvGetSectionName(kv, value, sizeof(value));
757 if (strcmp(value, "weapon") == 0)
759 KvGetString(kv, NULL_STRING, value, sizeof(value));
760 new index = DM_GetWeaponID(value);
761 if (index != -1 && DM_GetWeaponType(index) == DmWeapon_Primary)
763 g_PrimaryList[g_PrimaryCount++] = index;
766 } while (KvGotoNextKey(kv, false) && g_PrimaryCount < CSSDM_MAX_WEAPONS);
774 /* Build the primary menu */
775 if (g_hPrimaryMenu != INVALID_HANDLE)
777 CloseHandle(g_hPrimaryMenu);
779 g_hPrimaryMenu = CreateMenu(Menu_PrimaryHandler, MenuAction_DrawItem);
780 SetMenuTitle(g_hPrimaryMenu, "Primary weapon:");
781 for (new i=0; i<g_PrimaryCount; i++)
783 new index = g_PrimaryList[i];
784 DM_GetWeaponName(index, value, sizeof(value));
785 AddMenuItem(g_hPrimaryMenu, "", value);
787 AddMenuItem(g_hPrimaryMenu, "", "Random");
789 /* Build the secondary menu */
790 if (g_hSecondaryMenu != INVALID_HANDLE)
792 CloseHandle(g_hSecondaryMenu);
794 g_hSecondaryMenu = CreateMenu(Menu_SecondaryHandler, MenuAction_DrawItem);
795 SetMenuTitle(g_hSecondaryMenu, "Secondary weapon:");
796 for (new i=0; i<g_SecondaryCount; i++)
798 new index = g_SecondaryList[i];
799 DM_GetWeaponName(index, value, sizeof(value));
800 AddMenuItem(g_hSecondaryMenu, "", value);
802 AddMenuItem(g_hSecondaryMenu, "", "Random");
807 SetClientArmor(client, armor)
809 if (g_ArmorOffset == -1)
814 SetEntData(client, g_ArmorOffset, armor, 4, true);
817 SetClientHealth(client, health)
819 if (g_HealthOffset == -1)
824 SetEntData(client, g_HealthOffset, health, 4, true);
827 GiveGrenades(client, flashnum, bool:he, bool:smoke)
831 GivePlayerItem(client, "weapon_hegrenade");
833 for (new i=0; i<flashnum; i++)
835 GivePlayerItem(client, "weapon_flashbang");
839 GivePlayerItem(client, "weapon_smokegrenade");
843 GiveNightVision(client)
845 if (g_NVOffset == -1)
850 SetEntData(client, g_NVOffset, true, 1, true);