/
hats.sp
381 lines (317 loc) · 10.6 KB
/
hats.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#if defined STANDALONE_BUILD
#include <sourcemod>
#include <sdktools>
#include <zephstocks>
#include <sdkhooks>
#include <thirdperson>
#undef REQUIRE_EXTENSIONS
#include <store>
new bool:GAME_TF2 = false;
#endif
enum Hat
{
String:szModel[PLATFORM_MAX_PATH],
String:szAttachment[64],
Float:fPosition[3],
Float:fAngles[3],
bool:bBonemerge,
iTeam,
iSlot
}
new Handle:g_hLookupAttachment = INVALID_HANDLE;
new g_eHats[STORE_MAX_ITEMS][Hat];
new g_iClientHats[MAXPLAYERS+1][STORE_MAX_SLOTS];
new g_iHats = 0;
new g_cvarDefaultT = -1;
new g_cvarDefaultCT = -1;
new g_cvarOverrideEnabled = -1;
new bool:g_bTOverride = false;
new bool:g_bCTOverride = false;
#if defined STANDALONE_BUILD
public OnPluginStart()
#else
public Hats_OnPluginStart()
#endif
{
#if !defined STANDALONE_BUILD
// This is not a standalone build, we don't want hats to kill the whole plugin for us
if(GetExtensionFileStatus("sdkhooks.ext")!=1)
{
LogError("SDKHooks isn't installed or failed to load. Hats will be disabled. Please install SDKHooks. (https://forums.alliedmods.net/showthread.php?t=106748)");
return;
}
#else
// TF2 is unsupported
new String:m_szGameDir[32];
GetGameFolderName(m_szGameDir, sizeof(m_szGameDir));
if(strcmp(m_szGameDir, "tf")==0)
GAME_TF2 = true;
#endif
if(GAME_TF2)
return;
new Handle:m_hGameConf = LoadGameConfigFile("store.gamedata");
StartPrepSDKCall(SDKCall_Entity);
PrepSDKCall_SetFromConf(m_hGameConf, SDKConf_Signature, "LookupAttachment");
PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_AddParameter(SDKType_String, SDKPass_Pointer);
g_hLookupAttachment = EndPrepSDKCall();
CloseHandle(m_hGameConf);
if(!g_hLookupAttachment && !GAME_CSGO)
{
LogError("LookupAttachment signature is out of date or not supported in this game. Hats will be disabled. Please contact the author.");
return;
}
Store_RegisterHandler("hat", "model", Hats_OnMapStart, Hats_Reset, Hats_Config, Hats_Equip, Hats_Remove, true);
g_cvarDefaultT = RegisterConVar("sm_store_hats_default_t", "models/player/t_leet.mdl", "Terrorist model that supports hats", TYPE_STRING);
g_cvarDefaultCT = RegisterConVar("sm_store_hats_default_ct", "models/player/ct_urban.mdl", "Counter-Terrorist model that supports hats", TYPE_STRING);
g_cvarOverrideEnabled = RegisterConVar("sm_store_hats_skin_override", "0", "Allow the store to override player model if it doesn't support hats", TYPE_INT);
HookEvent("player_spawn", Hats_PlayerSpawn);
HookEvent("player_death", Hats_PlayerRemoveEvent);
HookEvent("player_team", Hats_PlayerRemoveEvent);
}
public Hats_OnMapStart()
{
for(new a=0;a<=MaxClients;++a)
for(new b=0;b<STORE_MAX_SLOTS;++b)
g_iClientHats[a][b]=0;
for(new i=0;i<g_iHats;++i)
{
PrecacheModel2(g_eHats[i][szModel], true);
Downloader_AddFileToDownloadsTable(g_eHats[i][szModel]);
}
// Just in case...
if(FileExists(g_eCvars[g_cvarDefaultT][sCache], true))
{
g_bTOverride = true;
PrecacheModel2(g_eCvars[g_cvarDefaultT][sCache], true);
Downloader_AddFileToDownloadsTable(g_eCvars[g_cvarDefaultT][sCache]);
}
else
g_bTOverride = false;
if(FileExists(g_eCvars[g_cvarDefaultCT][sCache], true))
{
g_bCTOverride = true;
PrecacheModel2(g_eCvars[g_cvarDefaultCT][sCache], true);
Downloader_AddFileToDownloadsTable(g_eCvars[g_cvarDefaultCT][sCache]);
}
else
g_bCTOverride = false;
}
public Hats_Reset()
{
g_iHats = 0;
}
public Hats_Config(&Handle:kv, itemid)
{
Store_SetDataIndex(itemid, g_iHats);
decl Float:m_fTemp[3];
KvGetString(kv, "model", g_eHats[g_iHats][szModel], PLATFORM_MAX_PATH);
KvGetVector(kv, "position", m_fTemp);
g_eHats[g_iHats][fPosition] = m_fTemp;
KvGetVector(kv, "angles", m_fTemp);
g_eHats[g_iHats][fAngles] = m_fTemp;
g_eHats[g_iHats][bBonemerge] = (KvGetNum(kv, "bonemerge", 0)?true:false);
g_eHats[g_iHats][iTeam] = KvGetNum(kv, "team", 0);
g_eHats[g_iHats][iSlot] = KvGetNum(kv, "slot");
KvGetString(kv, "attachment", g_eHats[g_iHats][szAttachment], 64, "");
if(strcmp(g_eHats[g_iHats][szAttachment], "")==0)
{
if(GAME_CSGO)
{
strcopy(g_eHats[g_iHats][szAttachment], 64, "facemask");
g_eHats[g_iHats][fPosition][2] -= 4.0;
}
else
strcopy(g_eHats[g_iHats][szAttachment], 64, "forward");
}
if(!(FileExists(g_eHats[g_iHats][szModel], true)))
return false;
++g_iHats;
return true;
}
public Hats_Equip(client, id)
{
if(!IsClientInGame(client) || !IsPlayerAlive(client) || !(2<=GetClientTeam(client)<=3))
return -1;
new m_iData = Store_GetDataIndex(id);
RemoveHat(client, g_eHats[m_iData][iSlot]);
CreateHat(client, id);
return g_eHats[m_iData][iSlot];
}
public Hats_Remove(client, id)
{
new m_iData = Store_GetDataIndex(id);
RemoveHat(client, g_eHats[m_iData][iSlot]);
return g_eHats[m_iData][iSlot];
}
public Action:Hats_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
if(!IsClientInGame(client) || !IsPlayerAlive(client) || !(2<=GetClientTeam(client)<=3))
return Plugin_Continue;
// Support for plugins that set client model
CreateTimer(0.1, Hats_PlayerSpawn_Post, GetClientUserId(client));
return Plugin_Continue;
}
public Action:Hats_PlayerSpawn_Post(Handle:timer, any:userid)
{
new client = GetClientOfUserId(userid);
if(!client || !IsClientInGame(client) || !IsPlayerAlive(client) || !(2<=GetClientTeam(client)<=3))
return Plugin_Stop;
for(new i=0;i<STORE_MAX_SLOTS;++i)
{
RemoveHat(client, i);
CreateHat(client, -1, i);
}
return Plugin_Stop;
}
public Action:Hats_PlayerRemoveEvent(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
if(!client || !IsClientInGame(client))
return Plugin_Continue;
for(new i=0;i<STORE_MAX_SLOTS;++i)
RemoveHat(client, i);
return Plugin_Continue;
}
CreateHat(client, itemid=-1, slot=0)
{
new m_iEquipped = (itemid==-1?Store_GetEquippedItem(client, "hat", slot):itemid);
if(m_iEquipped >= 0)
{
new m_iData = Store_GetDataIndex(m_iEquipped);
new m_iTeam = GetClientTeam(client);
if(g_eHats[m_iData][iTeam] != 0 && m_iTeam!=g_eHats[m_iData][iTeam])
return;
// If the model doesn't support hats, set the model to one that does
if(!LookupAttachment(client, g_eHats[m_iData][szAttachment]))
{
if(g_eCvars[g_cvarOverrideEnabled][aCache])
{
if(m_iTeam==2 && g_bTOverride)
SetEntityModel(client, g_eCvars[g_cvarDefaultT][sCache]);
else if(m_iTeam==3 && g_bCTOverride)
SetEntityModel(client, g_eCvars[g_cvarDefaultCT][sCache]);
else
return;
}
else
return;
}
// Calculate the final position and angles for the hat
decl Float:m_fHatOrigin[3];
decl Float:m_fHatAngles[3];
decl Float:m_fForward[3];
decl Float:m_fRight[3];
decl Float:m_fUp[3];
GetClientAbsOrigin(client,m_fHatOrigin);
GetClientAbsAngles(client,m_fHatAngles);
m_fHatAngles[0] += g_eHats[m_iData][fAngles][0];
m_fHatAngles[1] += g_eHats[m_iData][fAngles][1];
m_fHatAngles[2] += g_eHats[m_iData][fAngles][2];
new Float:m_fOffset[3];
m_fOffset[0] = g_eHats[m_iData][fPosition][0];
m_fOffset[1] = g_eHats[m_iData][fPosition][1];
m_fOffset[2] = g_eHats[m_iData][fPosition][2];
GetAngleVectors(m_fHatAngles, m_fForward, m_fRight, m_fUp);
m_fHatOrigin[0] += m_fRight[0]*m_fOffset[0]+m_fForward[0]*m_fOffset[1]+m_fUp[0]*m_fOffset[2];
m_fHatOrigin[1] += m_fRight[1]*m_fOffset[0]+m_fForward[1]*m_fOffset[1]+m_fUp[1]*m_fOffset[2];
m_fHatOrigin[2] += m_fRight[2]*m_fOffset[0]+m_fForward[2]*m_fOffset[1]+m_fUp[2]*m_fOffset[2];
// Create the hat entity
new m_iEnt = CreateEntityByName("prop_dynamic_override");
DispatchKeyValue(m_iEnt, "model", g_eHats[m_iData][szModel]);
DispatchKeyValue(m_iEnt, "spawnflags", "256");
DispatchKeyValue(m_iEnt, "solid", "0");
SetEntPropEnt(m_iEnt, Prop_Send, "m_hOwnerEntity", client);
if(g_eHats[m_iData][bBonemerge])
Bonemerge(m_iEnt);
DispatchSpawn(m_iEnt);
AcceptEntityInput(m_iEnt, "TurnOn", m_iEnt, m_iEnt, 0);
// Save the entity index
g_iClientHats[client][g_eHats[m_iData][iSlot]]=m_iEnt;
// We don't want the client to see his own hat
SDKHook(m_iEnt, SDKHook_SetTransmit, Hook_SetTransmit);
// Teleport the hat to the right position and attach it
TeleportEntity(m_iEnt, m_fHatOrigin, m_fHatAngles, NULL_VECTOR);
SetVariantString("!activator");
AcceptEntityInput(m_iEnt, "SetParent", client, m_iEnt, 0);
SetVariantString(g_eHats[m_iData][szAttachment]);
AcceptEntityInput(m_iEnt, "SetParentAttachmentMaintainOffset", m_iEnt, m_iEnt, 0);
}
}
public RemoveHat(client, slot)
{
if(g_iClientHats[client][slot] != 0 && IsValidEdict(g_iClientHats[client][slot]))
{
SDKUnhook(g_iClientHats[client][slot], SDKHook_SetTransmit, Hook_SetTransmit);
new String:m_szClassname[64];
GetEdictClassname(g_iClientHats[client][slot], STRING(m_szClassname));
if(strcmp("prop_dynamic", m_szClassname)==0)
AcceptEntityInput(g_iClientHats[client][slot], "Kill");
}
g_iClientHats[client][slot]=0;
}
public Action:Hook_SetTransmit(ent, client)
{
if(GetFeatureStatus(FeatureType_Native, "IsPlayerInTP")==FeatureStatus_Available)
if(IsPlayerInTP(client))
return Plugin_Continue;
for(new i=0;i<STORE_MAX_SLOTS;++i)
if(ent == g_iClientHats[client][i])
return Plugin_Handled;
if(client && IsClientInGame(client))
{
new m_iObserverMode = GetEntProp(client, Prop_Send, "m_iObserverMode");
new m_hObserverTarget = GetEntPropEnt(client, Prop_Send, "m_hObserverTarget");
if(m_iObserverMode == 4 && m_hObserverTarget>=0)
{
for(new i=0;i<STORE_MAX_SLOTS;++i)
if(ent == g_iClientHats[m_hObserverTarget][i])
return Plugin_Handled;
}
}
return Plugin_Continue;
}
public LookupAttachment(client, String:point[])
{
if(GAME_CSGO)
return true;
if(g_hLookupAttachment==INVALID_HANDLE)
return false;
if(!client || !IsClientInGame(client))
return false;
return SDKCall(g_hLookupAttachment, client, point);
}
public Bonemerge(ent)
{
new m_iEntEffects = GetEntProp(ent, Prop_Send, "m_fEffects");
m_iEntEffects &= ~32;
m_iEntEffects |= 1;
m_iEntEffects |= 128;
SetEntProp(ent, Prop_Send, "m_fEffects", m_iEntEffects);
}
public Store_OnClientModelChanged(client, String:model[])
{
if(!IsClientInGame(client) || !IsPlayerAlive(client))
return;
if(strcmp(model, g_eCvars[g_cvarDefaultT][sCache])==0 || strcmp(model, g_eCvars[g_cvarDefaultCT][sCache])==0)
return;
if(!LookupAttachment(client, "forward"))
{
new bool:m_bHasHats = false;
for(new i=0;i<STORE_MAX_SLOTS;++i)
{
if(Store_GetEquippedItem(client, "hat", i)!=-1)
{
m_bHasHats = true;
RemoveHat(client, i);
CreateHat(client, -1, i);
}
}
if(m_bHasHats)
if(g_eCvars[g_cvarOverrideEnabled][aCache])
Chat(client, "%t", "Override Enabled");
else
Chat(client, "%t", "Override Disabled");
}
}