-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstub_mm.cpp
375 lines (305 loc) · 9.34 KB
/
stub_mm.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
/**
* vim: set ts=4 sw=4 tw=99 noet :
* ======================================================
* Metamod:Source Stub Plugin
* Written by AlliedModders LLC.
* ======================================================
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* This stub plugin is public domain.
*/
#define META_IS_SOURCE2 1
#define SOURCE_ENGINE 28
#include <stdio.h>
#include "stub_mm.h"
StubPlugin g_StubPlugin;
ISource2GameClients *gameclients = NULL;
ISource2Server *gamedll = NULL;
ISource2ServerConfig *serverconfig = NULL;
IVEngineServer2 *engine = NULL;
CGlobalVars *gpGlobals = NULL;
CGameEntitySystem *g_entitySystem = NULL;
PlayerData g_playerData[MAXPLAYERS + 1];
#include "hooks.cpp"
bool enableDebug = false;
f32 tickrate = 128.0f;
f32 maxspeed = 250.0f;
#include "util.cpp"
#include "kztimer.cpp"
PLUGIN_EXPOSE(StubPlugin, g_StubPlugin);
bool StubPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
PLUGIN_SAVEVARS();
MathLib_Init(2.2f, 2.2f, 0.0f, 2, false, false, false, false);
GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer2, INTERFACEVERSION_VENGINESERVER);
GET_V_IFACE_CURRENT(GetServerFactory, gamedll, ISource2Server, INTERFACEVERSION_SERVERGAMEDLL);
GET_V_IFACE_CURRENT(GetServerFactory, serverconfig, ISource2ServerConfig, INTERFACEVERSION_SERVERCONFIG);
GET_V_IFACE_CURRENT(GetServerFactory, gameclients, ISource2GameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
return Hooks_HookFunctions(error, maxlen);
}
bool StubPlugin::Unload(char *error, size_t maxlen)
{
Hooks_UnhookFunctions();
return true;
}
internal CCSPLAYERPAWN_POSTTHINK(Hook_CCSPP_PostThink)
{
subhook_remove(CCSPP_PostThink_hook);
CCSPP_PostThink(this_);
if (enableDebug && gpGlobals->tickcount % 64 == 0) META_CONPRINTF("%i PostThink %x\n", gpGlobals->tickcount, this_);
CPlayerSlot slot = GetPlayerIndex(this_);
if (IsValidPlayerSlot(slot))
{
PlayerData *pd = &g_playerData[slot.Get()];
pd->oldAngles = this_->m_angEyeAngles;
}
subhook_install(CCSPP_PostThink_hook);
}
internal CCSP_MS__CHECKJUMPBUTTON(Hook_CCSP_MS__CheckJumpButton)
{
subhook_remove(CCSP_MS__CheckJumpButton_hook);
CCSP_MS__CheckJumpButton(this_, mv);
//if (enableDebug) META_CONPRINTF("(POST) [%i (%.6f)] m_flJumpUntil %f | m_flJumpPressedTime %f, | delta %f\n", gpGlobals->tickcount, gpGlobals->curtime, this_->m_flJumpUntil, this_->m_flJumpPressedTime, gpGlobals->curtime - this_->m_flJumpPressedTime);
subhook_install(CCSP_MS__CheckJumpButton_hook);
}
internal CCSP_MS__WALKMOVE(Hook_CCSP_MS__WalkMove)
{
subhook_remove(CCSP_MS__WalkMove_hook);
CCSP_MS__WalkMove(this_, mv);
s32 slot = GetPlayerIndex(this_->pawn);
if (IsValidPlayerSlot(slot))
{
PlayerData *pd = &g_playerData[slot];
pd->realPreVelMod = CalcPrestrafeVelMod(pd, this_, mv);
}
subhook_install(CCSP_MS__WalkMove_hook);
}
internal CCSP_MS__ONJUMP(Hook_CCSP_MS__OnJump)
{
subhook_remove(CCSP_MS__OnJump_hook);
f32 lastJumpUntil = this_->m_flJumpUntil;
CCSP_MS__OnJump(this_, mv);
f32 speed = mv->m_vecVelocity.Length2D();
if (speed > 380.0f && this_->m_flJumpUntil > lastJumpUntil)
{
f32 factor = 380.0f / speed;
mv->m_vecVelocity.x *= factor;
mv->m_vecVelocity.y *= factor;
}
if (this_->m_flJumpUntil > lastJumpUntil)
{
CPlayerSlot slot = GetPlayerIndex(this_);
if (IsValidPlayerSlot(slot))
{
PlayerData *pd = &g_playerData[slot.Get()];
pd->preSpeed = mv->m_vecVelocity.Length2D();
}
if (enableDebug) META_CONPRINTF("Hook_CCSP_MS__OnJump %x | moveservice %x | pawn %x | controller %x\n", CGameEntitySystem__EntityByIndex(g_entitySystem, mv->m_nPlayerHandle.m_Index & 0x3fff), this_, this_->pawn, this_->pawn->m_hController);
}
subhook_install(CCSP_MS__OnJump_hook);
}
internal void Hook_ClientCommand(CPlayerSlot slot, const CCommand& args)
{
META_CONPRINTF("ClientCommand: %s\n", args.GetCommandString());
if (strcmp(args[0], "toggledebug") == 0)
{
enableDebug = !enableDebug;
DoPrintCenter("enableDebug %s\n", enableDebug ? "on" : "off");
RETURN_META(MRES_SUPERCEDE);
}
else if (strcmp(args[0], "gettickrate") == 0)
{
META_CONPRINTF("Current tickrate: %f", tickrate);
RETURN_META(MRES_SUPERCEDE);
}
else if (strcmp(args[0], "settickrate") == 0 && args.ArgC() == 2)
{
// Engine defined min/max tickrate
tickrate = clamp(atof(args[1]), 10, 1000);
META_CONPRINTF("New tickrate: %f. Please change the map to apply the new tickrate.\n", tickrate);
RETURN_META(MRES_SUPERCEDE);
}
RETURN_META(MRES_IGNORED);
}
internal CCSGC__GETTICKINTERVAL(Hook_CCSGC__GetTickInterval)
{
return clamp(1 / tickrate, 0.001, 0.1);
}
internal CCSP_MS__PROCESSMOVEMENT(Hook_CCSP_MS__ProcessMovement)
{
subhook_remove(CCSP_MS__ProcessMovement_hook);
CPlayerSlot slot = GetPlayerIndex(this_);
if (IsValidPlayerSlot(slot))
{
PlayerData *pd = &g_playerData[slot.Get()];
pd->turning = GetTurning(pd, mv);
char buffer[1024] = "";
strcat(buffer, GetSpeedText(pd, this_, mv));
strcat(buffer, "\n");
strcat(buffer, GetKeyText(this_, mv));
DoPrintCenter(buffer);
}
this_->pawn->m_bTakesDamage = false;
CCSP_MS__ProcessMovement(this_, mv);
subhook_install(CCSP_MS__ProcessMovement_hook);
}
internal CCSPP_GETMAXSPEED(Hook_CCSPP_GetMaxSpeed)
{
CPlayerSlot slot = GetPlayerIndex(this_);
if (IsValidPlayerSlot(slot))
{
PlayerData *pd = &g_playerData[slot.Get()];
return 250.0f * pd->realPreVelMod;
}
return 250.0f;
}
internal CCSP_MS__FRICTION(Hook_CCSP_MS__Friction)
{
subhook_remove(CCSP_MS__Friction_hook);
CCSP_MS__Friction(this_, mv);
subhook_install(CCSP_MS__Friction_hook);
}
internal CCSP_MS__AIRACCELERATE(Hook_CCSP_MS__AirAccelerate)
{
subhook_remove(CCSP_MS__AirAccelerate_hook);
gpGlobals = engine->GetServerGlobals();
f32 oldMaxSpeed = CCSPP_GetMaxSpeed((CCSPlayerPawn*)this_->pawn);
mv->m_flMaxSpeed = MIN(250.0f, mv->m_flMaxSpeed);
// call airaccel twice to simulate 128 tick airstrafing
s32 slot = GetPlayerIndex(this_->pawn);
if (gpGlobals->interval_per_tick == 1.0f / 64.0f
&& IsValidPlayerSlot(slot))
{
PlayerData *pd = &g_playerData[slot];
QAngle angles = mv->m_vecViewAngles;
// normalise angles beforehand
if (angles[0] - pd->oldAngles[0] > 180)
{
angles[0] -= 360.0f;
}
else if (angles[0] - pd->oldAngles[0] < 180)
{
angles[0] += 360.0f;
}
angles += pd->oldAngles;
angles *= 0.5f;
Vector forward, right, up;
AngleVectors(angles, &forward, &right, &up);
f32 fmove = -mv->m_flForwardMove;
f32 smove = mv->m_flSideMove;
forward[2] = 0;
right[2] = 0;
VectorNormalize(forward);
VectorNormalize(right);
Vector newWishdir = Vector(0, 0, 0);
for (s32 i = 0; i < 2; i++)
{
newWishdir[i] = forward[i] * fmove + right[i] * smove;
}
VectorNormalize(newWishdir);
gpGlobals->interval_per_tick = 1.0f / 128.0f;
CCSP_MS__AirAccelerate(this_, mv, newWishdir, maxspeed, accel);
CCSP_MS__AirAccelerate(this_, mv, wishdir, maxspeed, accel);
gpGlobals->interval_per_tick = 1.0f / 64.0f;
}
else
{
CCSP_MS__AirAccelerate(this_, mv, wishdir, maxspeed, accel);
}
mv->m_flMaxSpeed = oldMaxSpeed;
subhook_install(CCSP_MS__AirAccelerate_hook);
}
internal CREATEENTITY(Hook_CreateEntity)
{
subhook_remove(CreateEntity_hook);
CBaseEntity *result = CreateEntity(this_, a2, class_, memory, zero, a6, a7, a8);
if (result && strcmp(result->m_pEntity->m_designerName.String(), "func_button") == 0)
{
CBaseButton *button = (CBaseButton *)result;
button = button;
}
subhook_install(CreateEntity_hook);
return result;
}
internal FINDUSEENTITY(Hook_FindUseEntity)
{
subhook_remove(FindUseEntity_hook);
CBaseEntity *ent = FindUseEntity(pawn);
if (enableDebug) META_CONPRINTF("%x FindUseEnt: %x\n", pawn, ent);
subhook_install(FindUseEntity_hook);
return ent;
}
internal INITIALISEGAMEENTITYSYSTEM(Hook_InitialiseGameEntitySystem)
{
subhook_remove(InitialiseGameEntitySystem_hook);
g_entitySystem = InitialiseGameEntitySystem(memory);
subhook_install(InitialiseGameEntitySystem_hook);
return g_entitySystem;
}
internal void Hook_GameFrame(bool simulating, bool bFirstTick, bool bLastTick)
{
gpGlobals = engine->GetServerGlobals();
}
internal void ResetPlayerData(PlayerData *pd)
{
*pd = PlayerData{};
}
internal void Hook_ClientFullyConnect(CPlayerSlot slot)
{
SetupKZTimerConvars();
engine->ServerCommand("sv_lan true");
ResetPlayerData(&g_playerData[slot.Get()]);
}
internal float Hook_ProcessUsercmds(CPlayerSlot slot, bf_read *buf, int numcmds, bool ignore, bool paused)
{
RETURN_META_VALUE(MRES_IGNORED, 0.0f);
}
void StubPlugin::AllPluginsLoaded()
{
/* This is where we'd do stuff that relies on the mod or other plugins
* being initialized (for example, cvars added and events registered).
*/
}
bool StubPlugin::Pause(char *error, size_t maxlen)
{
return true;
}
bool StubPlugin::Unpause(char *error, size_t maxlen)
{
return true;
}
const char *StubPlugin::GetLicense()
{
return "Public Domain";
}
const char *StubPlugin::GetVersion()
{
return "0.0.0";
}
const char *StubPlugin::GetDate()
{
return __DATE__;
}
const char *StubPlugin::GetLogTag()
{
return "GC";
}
const char *StubPlugin::GetAuthor()
{
return "GameChaos";
}
const char *StubPlugin::GetDescription()
{
return "Sample empty plugin";
}
const char *StubPlugin::GetName()
{
return "Stub Plugin";
}
const char *StubPlugin::GetURL()
{
return "http://www.sourcemm.net/";
}