-
-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathsmn_halflife.cpp
648 lines (527 loc) · 16.9 KB
/
smn_halflife.cpp
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/**
* vim: set ts=4 sw=4 tw=99 noet :
* =============================================================================
* SourceMod
* Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* 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/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "sm_globals.h"
#include "sourcemod.h"
#include "sourcemm_api.h"
#include "PlayerManager.h"
#include "HalfLife2.h"
#include <vstdlib/random.h>
static cell_t SetRandomSeed(IPluginContext *pContext, const cell_t *params)
{
::RandomSeed(params[1]);
return 1;
}
static cell_t GetRandomFloat(IPluginContext *pContext, const cell_t *params)
{
float fMin = sp_ctof(params[1]);
float fMax = sp_ctof(params[2]);
float fRandom = ::RandomFloat(fMin, fMax);
return sp_ftoc(fRandom);
}
static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params)
{
return ::RandomInt(params[1], params[2]);
}
static cell_t IsMapValid(IPluginContext *pContext, const cell_t *params)
{
char *map;
pContext->LocalToString(params[1], &map);
return g_HL2.IsMapValid(map);
}
static cell_t FindMap(IPluginContext *pContext, const cell_t *params)
{
char *pMapname;
pContext->LocalToString(params[1], &pMapname);
if (params[0] == 2)
{
return static_cast<cell_t>(g_HL2.FindMap(pMapname, params[2]));
}
char *pDestMap;
pContext->LocalToString(params[2], &pDestMap);
return static_cast<cell_t>(g_HL2.FindMap(pMapname, pDestMap, params[3]));
}
static cell_t GetMapDisplayName(IPluginContext *pContext, const cell_t *params)
{
char *pMapname, *pDisplayname;
pContext->LocalToString(params[1], &pMapname);
pContext->LocalToString(params[2], &pDisplayname);
return static_cast<cell_t>(g_HL2.GetMapDisplayName(pMapname, pDisplayname, params[3]));
}
static cell_t IsDedicatedServer(IPluginContext *pContext, const cell_t *params)
{
return engine->IsDedicatedServer();
}
static cell_t GetEngineTime(IPluginContext *pContext, const cell_t *params)
{
#if SOURCE_ENGINE >= SE_NUCLEARDAWN
float fTime = Plat_FloatTime();
#else
float fTime = engine->Time();
#endif
return sp_ftoc(fTime);
}
static cell_t GetGameTime(IPluginContext *pContext, const cell_t *params)
{
return sp_ftoc(gpGlobals->curtime);
}
static cell_t GetGameTickCount(IPluginContext *pContext, const cell_t *params)
{
return gpGlobals->tickcount;
}
static cell_t GetGameFrameTime(IPluginContext *pContext, const cell_t *params)
{
return sp_ftoc(gpGlobals->frametime);
}
static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
{
if (!g_SourceMod.IsMapRunning())
{
return pContext->ThrowNativeError("Cannot create fakeclient when no map is active");
}
char *netname;
pContext->LocalToString(params[1], &netname);
edict_t *pEdict = engine->CreateFakeClient(netname);
/* :TODO: does the engine fire forwards for us and whatnot? no idea... */
if (!pEdict)
{
return 0;
}
return IndexOfEdict(pEdict);
}
static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
{
CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
if (!pPlayer)
{
return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
}
if (!pPlayer->IsConnected())
{
return pContext->ThrowNativeError("Client %d is not connected", params[1]);
}
if (!pPlayer->IsFakeClient())
{
return pContext->ThrowNativeError("Client %d is not a fake client", params[1]);
}
char *cvar, *value;
pContext->LocalToString(params[2], &cvar);
pContext->LocalToString(params[3], &value);
engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
return 1;
}
static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
{
const char *description;
if (params[3])
{
description = gamedll->GetGameDescription();
} else {
description = SERVER_CALL(GetGameDescription)();
}
size_t numBytes;
pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
return numBytes;
}
static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
{
const char *name = g_SourceMod.GetGameFolderName();
size_t numBytes;
pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
return numBytes;
}
/* Useless comment to bump the build */
static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
{
size_t bytes;
pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
return bytes;
}
static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
{
char *model;
pContext->LocalToString(params[1], &model);
return engine->PrecacheModel(model, params[2] ? true : false);
}
static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
{
char *sentencefile;
pContext->LocalToString(params[1], &sentencefile);
return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
}
static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
{
char *decal;
pContext->LocalToString(params[1], &decal);
return engine->PrecacheDecal(decal, params[2] ? true : false);
}
static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
{
char *generic;
pContext->LocalToString(params[1], &generic);
return engine->PrecacheGeneric(generic, params[2] ? true : false);
}
static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
{
char *model;
pContext->LocalToString(params[1], &model);
return engine->IsModelPrecached(model) ? 1 : 0;
}
static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
{
char *decal;
pContext->LocalToString(params[1], &decal);
return engine->IsDecalPrecached(decal) ? 1 : 0;
}
static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
{
char *generic;
pContext->LocalToString(params[1], &generic);
return engine->IsGenericPrecached(generic) ? 1 : 0;
}
static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
{
char *sample;
pContext->LocalToString(params[1], &sample);
return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
}
static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
{
char *sample;
pContext->LocalToString(params[1], &sample);
return enginesound->IsSoundPrecached(sample) ? 1 : 0;
}
static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
{
KeyValues *pKV;
HandleError herr;
Handle_t hndl = static_cast<Handle_t>(params[2]);
CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
if (!pPlayer)
{
return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
}
if (!pPlayer->IsInGame())
{
return pContext->ThrowNativeError("Client %d is not in game", params[1]);
}
pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
if (herr != HandleError_None)
{
return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
}
serverpluginhelpers->CreateMessage(pPlayer->GetEdict(),
static_cast<DIALOG_TYPE>(params[3]),
pKV,
vsp_interface);
return 1;
}
static cell_t PrintToChat(IPluginContext *pContext, const cell_t *params)
{
int client = params[1];
CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
if (!pPlayer)
{
return pContext->ThrowNativeError("Client index %d is invalid", client);
}
if (!pPlayer->IsInGame())
{
return pContext->ThrowNativeError("Client %d is not in game", client);
}
g_SourceMod.SetGlobalTarget(client);
char buffer[254];
{
DetectExceptions eh(pContext);
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
if (eh.HasException())
return 0;
}
if (!g_HL2.TextMsg(client, HUD_PRINTTALK, buffer))
{
return pContext->ThrowNativeError("Could not send a usermessage");
}
return 1;
}
static cell_t PrintCenterText(IPluginContext *pContext, const cell_t *params)
{
int client = params[1];
CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
if (!pPlayer)
{
return pContext->ThrowNativeError("Client index %d is invalid", client);
}
if (!pPlayer->IsInGame())
{
return pContext->ThrowNativeError("Client %d is not in game", client);
}
g_SourceMod.SetGlobalTarget(client);
char buffer[254];
{
DetectExceptions eh(pContext);
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
if (eh.HasException())
return 0;
}
if (!g_HL2.TextMsg(client, HUD_PRINTCENTER, buffer))
{
return pContext->ThrowNativeError("Could not send a usermessage");
}
return 1;
}
static cell_t PrintHintText(IPluginContext *pContext, const cell_t *params)
{
int client = params[1];
CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
if (!pPlayer)
{
return pContext->ThrowNativeError("Client index %d is invalid", client);
}
if (!pPlayer->IsInGame())
{
return pContext->ThrowNativeError("Client %d is not in game", client);
}
g_SourceMod.SetGlobalTarget(client);
char buffer[254];
{
DetectExceptions eh(pContext);
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
if (eh.HasException())
return 0;
}
if (!g_HL2.HintTextMsg(client, buffer))
{
return pContext->ThrowNativeError("Could not send a usermessage");
}
return 1;
}
static cell_t ShowVGUIPanel(IPluginContext *pContext, const cell_t *params)
{
HandleError herr;
char *name;
KeyValues *pKV = NULL;
int client = params[1];
Handle_t hndl = static_cast<Handle_t>(params[3]);
CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
if (!pPlayer)
{
return pContext->ThrowNativeError("Client index %d is invalid", client);
}
if (!pPlayer->IsInGame())
{
return pContext->ThrowNativeError("Client %d is not in game", client);
}
if (hndl != BAD_HANDLE)
{
pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
if (herr != HandleError_None)
{
return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
}
}
pContext->LocalToString(params[2], &name);
if (!g_HL2.ShowVGUIMenu(client, name, pKV, (params[4]) ? true : false))
{
return pContext->ThrowNativeError("Could not send a usermessage");
}
return 1;
}
static cell_t smn_IsPlayerAlive(IPluginContext *pContext, const cell_t *params)
{
CPlayer *player = g_Players.GetPlayerByIndex(params[1]);
if (player == NULL)
{
return pContext->ThrowNativeError("Invalid client index %d", params[1]);
}
else if (!player->IsInGame())
{
return pContext->ThrowNativeError("Client %d is not in game", params[1]);
}
unsigned int state = player->GetLifeState();
if (state == PLAYER_LIFE_UNKNOWN)
{
return pContext->ThrowNativeError("\"IsPlayerAlive\" not supported by this mod");
}
else if (state == PLAYER_LIFE_ALIVE)
{
return 1;
}
else
{
return 0;
}
}
static cell_t GuessSDKVersion(IPluginContext *pContext, const cell_t *params)
{
int version = g_SMAPI->GetSourceEngineBuild();
switch (version)
{
case SOURCE_ENGINE_ORIGINAL:
return 10;
case SOURCE_ENGINE_EPISODEONE:
return 20;
case SOURCE_ENGINE_DARKMESSIAH:
return 15;
case SOURCE_ENGINE_ORANGEBOX:
return 30;
case SOURCE_ENGINE_BLOODYGOODTIME:
return 32;
case SOURCE_ENGINE_EYE:
return 33;
case SOURCE_ENGINE_CSS:
return 34;
case SOURCE_ENGINE_ORANGEBOXVALVE_DEPRECATED:
case SOURCE_ENGINE_HL2DM:
case SOURCE_ENGINE_DODS:
case SOURCE_ENGINE_TF2:
case SOURCE_ENGINE_BMS:
case SOURCE_ENGINE_SDK2013:
case SOURCE_ENGINE_PVKII:
return 35;
case SOURCE_ENGINE_LEFT4DEAD:
return 40;
case SOURCE_ENGINE_NUCLEARDAWN:
case SOURCE_ENGINE_CONTAGION:
case SOURCE_ENGINE_LEFT4DEAD2:
return 50;
case SOURCE_ENGINE_ALIENSWARM:
return 60;
case SOURCE_ENGINE_PORTAL2:
return 70;
case SOURCE_ENGINE_CSGO:
return 80;
// Deprecated, and not very useful. Don't bother adding new games
}
return 0;
}
static cell_t GetEngineVersion(IPluginContext *pContext, const cell_t *params)
{
int engineVer = g_SMAPI->GetSourceEngineBuild();
if (engineVer == SOURCE_ENGINE_ORANGEBOXVALVE_DEPRECATED)
{
const char *gamedir = g_SourceMod.GetGameFolderName();
if (strcmp(gamedir, "tf") == 0)
return SOURCE_ENGINE_TF2;
else if (strcmp(gamedir, "cstrike") == 0)
return SOURCE_ENGINE_CSS;
else if (strcmp(gamedir, "dod") == 0)
return SOURCE_ENGINE_DODS;
else if (strcmp(gamedir, "hl2mp") == 0)
return SOURCE_ENGINE_HL2DM;
}
return engineVer;
}
static cell_t IndexToReference(IPluginContext *pContext, const cell_t *params)
{
if (params[1] >= NUM_ENT_ENTRIES || params[1] < 0)
{
return pContext->ThrowNativeError("Invalid entity index %i", params[1]);
}
return g_HL2.IndexToReference(params[1]);
}
static cell_t ReferenceToIndex(IPluginContext *pContext, const cell_t *params)
{
return g_HL2.ReferenceToIndex(params[1]);
}
static cell_t ReferenceToBCompatRef(IPluginContext *pContext, const cell_t *params)
{
return g_HL2.ReferenceToBCompatRef(params[1]);
}
// Must match ClientRangeType enum in halflife.inc
enum class ClientRangeType : cell_t
{
Visibility = 0,
Audibility,
};
static cell_t GetClientsInRange(IPluginContext *pContext, const cell_t *params)
{
cell_t *origin;
pContext->LocalToPhysAddr(params[1], &origin);
Vector vOrigin(sp_ctof(origin[0]), sp_ctof(origin[1]), sp_ctof(origin[2]));
ClientRangeType rangeType = (ClientRangeType) params[2];
CBitVec<ABSOLUTE_PLAYER_LIMIT> players;
engine->Message_DetermineMulticastRecipients(rangeType == ClientRangeType::Audibility, vOrigin, players);
cell_t *outPlayers;
pContext->LocalToPhysAddr(params[3], &outPlayers);
int maxPlayers = params[4];
int curPlayers = 0;
int index = players.FindNextSetBit(0);
while (index > -1 && curPlayers < maxPlayers)
{
int entidx = index + 1;
CPlayer *pPlayer = g_Players.GetPlayerByIndex(entidx);
if (pPlayer && pPlayer->IsInGame())
{
outPlayers[curPlayers++] = entidx;
}
index = players.FindNextSetBit(index + 1);
}
return curPlayers;
}
REGISTER_NATIVES(halflifeNatives)
{
{"CreateFakeClient", CreateFakeClient},
{"GetCurrentMap", GetCurrentMap},
{"GetEngineTime", GetEngineTime},
{"GetGameDescription", GetGameDescription},
{"GetGameFolderName", GetGameFolderName},
{"GetGameTime", GetGameTime},
{"GetGameTickCount", GetGameTickCount},
{"GetGameFrameTime", GetGameFrameTime},
{"GetRandomFloat", GetRandomFloat},
{"GetRandomInt", GetRandomInt},
{"IsDedicatedServer", IsDedicatedServer},
{"IsMapValid", IsMapValid},
{"FindMap", FindMap},
{"GetMapDisplayName", GetMapDisplayName},
{"SetFakeClientConVar", SetFakeClientConVar},
{"SetRandomSeed", SetRandomSeed},
{"PrecacheModel", PrecacheModel},
{"PrecacheSentenceFile", PrecacheSentenceFile},
{"PrecacheDecal", PrecacheDecal},
{"PrecacheGeneric", PrecacheGeneric},
{"IsModelPrecached", IsModelPrecached},
{"IsDecalPrecached", IsDecalPrecached},
{"IsGenericPrecached", IsGenericPrecached},
{"PrecacheSound", PrecacheSound},
{"IsSoundPrecached", IsSoundPrecached},
{"CreateDialog", smn_CreateDialog},
{"PrintToChat", PrintToChat},
{"PrintCenterText", PrintCenterText},
{"PrintHintText", PrintHintText},
{"ShowVGUIPanel", ShowVGUIPanel},
{"IsPlayerAlive", smn_IsPlayerAlive},
{"GuessSDKVersion", GuessSDKVersion},
{"GetEngineVersion", GetEngineVersion},
{"EntIndexToEntRef", IndexToReference},
{"EntRefToEntIndex", ReferenceToIndex},
{"MakeCompatEntRef", ReferenceToBCompatRef},
{"GetClientsInRange", GetClientsInRange},
{NULL, NULL},
};