/
l4d2_weaponrules.sp
150 lines (129 loc) · 3.57 KB
/
l4d2_weaponrules.sp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
SourcePawn is Copyright (C) 2006-2008 AlliedModders LLC. All rights reserved.
SourceMod is Copyright (C) 2006-2008 AlliedModders LLC. All rights reserved.
Pawn and SMALL are Copyright (C) 1997-2008 ITB CompuPhase.
Source is Copyright (C) Valve Corporation.
All trademarks are property of their respective owners.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma semicolon 1
#include <sourcemod>
#include <weapons>
#define DEBUG 0
new g_GlobalWeaponRules[WeaponId]={-1, ...};
// state tracking for roundstart looping
new g_bRoundStartHit;
new g_bConfigsExecuted;
public OnPluginStart()
{
RegServerCmd("l4d2_addweaponrule", AddWeaponRuleCb);
RegServerCmd("l4d2_resetweaponrules", ResetWeaponRulesCb);
HookEvent("round_start", RoundStartCb, EventHookMode_PostNoCopy);
ResetWeaponRules();
}
public Action:ResetWeaponRulesCb(args)
{
ResetWeaponRules();
return Plugin_Handled;
}
ResetWeaponRules()
{
for(new i=0; i < _:WeaponId; i++) g_GlobalWeaponRules[i]=-1;
}
public RoundStartCb(Handle:event, const String:name[], bool:dontBroadcast)
{
CreateTimer(0.3, RoundStartDelay, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE);
}
public OnMapStart()
{
g_bRoundStartHit=false;
g_bConfigsExecuted=false;
}
public OnConfigsExecuted()
{
g_bConfigsExecuted=true;
if(g_bRoundStartHit)
{
WeaponSearchLoop();
}
}
public Action:RoundStartDelay(Handle:timer)
{
g_bRoundStartHit=true;
if(g_bConfigsExecuted)
{
WeaponSearchLoop();
}
}
public Action:AddWeaponRuleCb(args)
{
if(args < 2)
{
PrintToServer("Usage: l4d2_addweaponrule <match> <replace>");
return Plugin_Handled;
}
decl String:weaponbuf[64];
GetCmdArg(1, weaponbuf, sizeof(weaponbuf));
new WeaponId:match = WeaponNameToId2(weaponbuf);
GetCmdArg(2, weaponbuf, sizeof(weaponbuf));
new WeaponId:to = WeaponNameToId2(weaponbuf);
AddWeaponRule(match, _:to);
return Plugin_Handled;
}
AddWeaponRule(WeaponId:match, to)
{
if(IsValidWeaponId(match) && (to == -1 || IsValidWeaponId(WeaponId:to)))
{
g_GlobalWeaponRules[match] = _:to;
#if DEBUG
PrintToServer("Added weapon rule: %d to %d", match, to);
#endif
}
}
WeaponSearchLoop()
{
new entcnt = GetEntityCount();
for(new ent=1; ent < entcnt; ent++)
{
new WeaponId:source=IdentifyWeapon(ent);
if(source > WEPID_NONE && g_GlobalWeaponRules[source] != -1)
{
if(g_GlobalWeaponRules[source] == _:WEPID_NONE)
{
AcceptEntityInput(ent, "kill");
#if DEBUG
PrintToServer("Found Weapon %d, killing", source);
#endif
}
else
{
#if DEBUG
PrintToServer("Found Weapon %d, converting to %d", source, g_GlobalWeaponRules[source]);
#endif
ConvertWeaponSpawn(ent, WeaponId:g_GlobalWeaponRules[source]);
}
}
}
}
// Tries the given weapon name directly, and upon failure,
// tries prepending "weapon_" to the given name
stock WeaponId:WeaponNameToId2(const String:name[])
{
static String:namebuf[64]="weapon_";
new WeaponId:wepid = WeaponNameToId(name);
if(wepid == WEPID_NONE)
{
strcopy(namebuf[7], sizeof(namebuf)-7, name);
wepid=WeaponNameToId(namebuf);
}
return wepid;
}