forked from SirPlease/L4D2-Competitive-Rework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl4d_consistent_escaperoute.sp
More file actions
325 lines (276 loc) · 9 KB
/
Copy pathl4d_consistent_escaperoute.sp
File metadata and controls
325 lines (276 loc) · 9 KB
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
#pragma semicolon 1
#pragma newdecls required
#define DEBUG 1
#define PLUGIN_VERSION "2.1"
#define PLUGIN_TAG "l4d_consistent_escaperoute"
#include <sourcemod>
#include <dhooks>
#include <sourcescramble>
#include <sdktools>
#include <left4dhooks>
#if DEBUG
#include <sdktools_functions>
#endif
public Plugin myinfo =
{
name = "[L4D & 2] Consistent Escape Route",
author = "Forgetest",
description = "True L4D.",
version = PLUGIN_VERSION,
url = "https://github.com/Target5150/MoYu_Server_Stupid_Plugins"
};
enum struct SDKCallParamsWrapper {
SDKType type;
SDKPassMethod pass;
int decflags;
int encflags;
}
methodmap GameDataWrapper < GameData {
public GameDataWrapper(const char[] file) {
GameData gd = new GameData(file);
if (!gd) SetFailState("Missing gamedata \"%s\"", file);
return view_as<GameDataWrapper>(gd);
}
property GameData Super {
public get() { return view_as<GameData>(this); }
}
public int GetOffset(const char[] key) {
int offset = this.Super.GetOffset(key);
if (offset == -1) SetFailState("Missing offset \"%s\"", key);
return offset;
}
public Address GetAddress(const char[] key) {
Address ptr = this.Super.GetAddress(key);
if (ptr == Address_Null) SetFailState("Missing address \"%s\"", key);
return ptr;
}
public MemoryPatch CreatePatchOrFail(const char[] name, bool enable = false) {
MemoryPatch hPatch = MemoryPatch.CreateFromConf(this, name);
if (!(enable ? hPatch.Enable() : hPatch.Validate()))
SetFailState("Failed to patch \"%s\"", name);
return hPatch;
}
public DynamicDetour CreateDetourOrFail(
const char[] name,
DHookCallback preHook = INVALID_FUNCTION,
DHookCallback postHook = INVALID_FUNCTION) {
DynamicDetour hSetup = DynamicDetour.FromConf(this, name);
if (!hSetup)
SetFailState("Missing detour setup \"%s\"", name);
if (preHook != INVALID_FUNCTION && !hSetup.Enable(Hook_Pre, preHook))
SetFailState("Failed to pre-detour \"%s\"", name);
if (postHook != INVALID_FUNCTION && !hSetup.Enable(Hook_Post, postHook))
SetFailState("Failed to post-detour \"%s\"", name);
return hSetup;
}
public Handle CreateSDKCallOrFail(
SDKCallType type,
SDKFuncConfSource src,
const char[] name,
const SDKCallParamsWrapper[] params = {},
int numParams = 0,
bool hasReturnValue = false,
const SDKCallParamsWrapper ret = {}) {
static const char k_sSDKFuncConfSource[SDKFuncConfSource][] = { "offset", "signature", "address" };
Handle result;
StartPrepSDKCall(type);
if (!PrepSDKCall_SetFromConf(this, src, name))
SetFailState("Missing %s \"%s\"", k_sSDKFuncConfSource[src], name);
for (int i = 0; i < numParams; ++i)
PrepSDKCall_AddParameter(params[i].type, params[i].pass, params[i].decflags, params[i].encflags);
if (hasReturnValue)
PrepSDKCall_SetReturnInfo(ret.type, ret.pass, ret.decflags, ret.encflags);
if (!(result = EndPrepSDKCall()))
SetFailState("Failed to prep sdkcall \"%s\"", name);
return result;
}
}
methodmap Address {}
#if DEBUG
int g_iOffs_m_nMainPathAreaCount;
methodmap CEscapeRoute < Address
{
public CEscapeRoute(int entity) {
return view_as<CEscapeRoute>(GetEntityAddress(entity));
}
public TerrorNavArea GetMainPathArea(int index) {
return LoadFromAddress(this
+ view_as<Address>(g_iOffs_m_nMainPathAreaCount + 8)
+ view_as<Address>(index * 4),
NumberType_Int32);
}
property int m_nMainPathAreaCount {
public get() { return LoadFromAddress(this
+ view_as<Address>(g_iOffs_m_nMainPathAreaCount),
NumberType_Int32); }
}
}
bool g_bEscapeRouteChecked = false;
ArrayList g_SavedEscapeRouteAreas;
int g_offs_m_id;
methodmap TerrorNavArea < Address
{
public int GetID() {
return LoadFromAddress(this + view_as<Address>(g_offs_m_id), NumberType_Int32);
}
}
#endif
MemoryPatch g_patch_SkipSpawnPosIdx;
Handle g_call_Checkpoint_GetLargestArea;
public void OnPluginStart()
{
GameDataWrapper gd = new GameDataWrapper("l4d_consistent_escaperoute");
#if DEBUG
g_iOffs_m_nMainPathAreaCount = gd.GetOffset("CEscapeRoute::m_nMainPathAreaCount");
g_offs_m_id = gd.GetOffset("CNavArea::m_id");
#endif
g_patch_SkipSpawnPosIdx = gd.CreatePatchOrFail("skip_spawn_position_idx", false);
SDKCallParamsWrapper params[] = {
{ SDKType_PlainOldData, SDKPass_Plain },
};
g_call_Checkpoint_GetLargestArea = gd.CreateSDKCallOrFail(SDKCall_Raw, SDKConf_Signature, "Checkpoint::GetLargestArea", _, 0, true, params[0]);
delete gd.CreateDetourOrFail("l4d_consistent_escaperoute::Checkpoint::GetSpawnPosition", _, DTR_Checkpoint_GetSpawnPosition_Post);
delete gd.CreateDetourOrFail("l4d_consistent_escaperoute::GetPlayerSpawnPosition", DTR_GetPlayerSpawnPosition, DTR_GetPlayerSpawnPosition_Post);
delete gd.CreateDetourOrFail("l4d_consistent_escaperoute::TerrorNavMesh::ComputeFlowDistances", DTR_ComputeFlowDistances, DTR_ComputeFlowDistances_Post);
delete gd;
#if DEBUG
g_SavedEscapeRouteAreas = new ArrayList();
HookEvent("round_start", Event_round_start);
#endif
}
public void OnMapEnd()
{
#if DEBUG
g_SavedEscapeRouteAreas.Clear();
#endif
}
static bool g_bInComputeFlowDistances = false;
MRESReturn DTR_ComputeFlowDistances(DHookReturn hReturn)
{
g_bInComputeFlowDistances = true;
return MRES_Ignored;
}
// GetPlayerSpawnPosition(SurvivorCharacterType,Vector *,QAngle *,TerrorNavArea **)
MRESReturn DTR_GetPlayerSpawnPosition(DHookReturn hReturn, DHookParam hParams)
{
if (!g_bInComputeFlowDistances)
return MRES_Ignored;
// if ( iSurvPosCount )
// {
// static int idx = -1;
// if ( ++idx >= iSurvPosCount )
// idx = 0;
//
// iPosIndexToUse = idx;
// pSurvPosEnt = vecSurvPositions.At(iPosIndexToUse);
// }
//
// NOTE: The patch here transforms the original code to make "iPosIndexToUse" always zero
// which forces the first survivor postion entity to be where the start area is.
g_patch_SkipSpawnPosIdx.Enable();
return MRES_Ignored;
}
// Checkpoint::GetSpawnPosition(Vector *, QAngle *, TerrorNavArea **)const
MRESReturn DTR_Checkpoint_GetSpawnPosition_Post(Address pThis, DHookReturn hReturn, DHookParam hParams)
{
if (!g_bInComputeFlowDistances)
return MRES_Ignored;
#if DEBUG
PrintToServer("[%s] Overriding Checkpoint::GetSpawnPosition", PLUGIN_TAG);
#endif
// NOTE:
// GetSpawnPosition always computes a random position within start safe area.
// The following makes it always return the largest area instead.
TerrorNavArea area = SDKCall(g_call_Checkpoint_GetLargestArea, pThis);
if (area == Address_Null)
return MRES_Ignored;
#if DEBUG
PrintToServer("[%s] Fixed player start area = %d", PLUGIN_TAG, area.GetID());
#endif
if (!hParams.IsNull(1))
{
float pos[3];
L4D_GetNavAreaCenter(area, pos);
hParams.SetVector(1, pos);
}
if (!hParams.IsNull(2))
{
hParams.SetVector(2, {0.0, 0.0, 0.0});
}
if (!hParams.IsNull(3))
{
hParams.SetObjectVar(3, 0, ObjectValueType_Int, area);
}
hReturn.Value = true;
return MRES_ChangedOverride;
}
// GetPlayerSpawnPosition(SurvivorCharacterType,Vector *,QAngle *,TerrorNavArea **)
MRESReturn DTR_GetPlayerSpawnPosition_Post(DHookReturn hReturn, DHookParam hParams)
{
if (!g_bInComputeFlowDistances)
return MRES_Ignored;
g_patch_SkipSpawnPosIdx.Disable();
return MRES_Ignored;
}
MRESReturn DTR_ComputeFlowDistances_Post(DHookReturn hReturn)
{
g_bInComputeFlowDistances = false;
#if DEBUG
RequestFrame(NextFrame_ComputeFlowDistance); // escape route is rebuilt a few calls after
#endif
return MRES_Ignored;
}
#if DEBUG
void Event_round_start(Event event, const char[] name, bool dontBroadcast)
{
g_bEscapeRouteChecked = false;
}
void NextFrame_ComputeFlowDistance()
{
if (g_bEscapeRouteChecked)
return;
int entity = FindEntityByClassname(INVALID_ENT_REFERENCE, "escape_route");
if (entity == INVALID_ENT_REFERENCE)
return;
// Only check once eveny beginning of round
g_bEscapeRouteChecked = true;
CEscapeRoute TheEscapeRoute = CEscapeRoute(entity);
if (g_SavedEscapeRouteAreas.Length)
{
bool fullyMatched = true;
PrintToServer("[%s] Checking escape route", PLUGIN_TAG);
if (g_SavedEscapeRouteAreas.Length != TheEscapeRoute.m_nMainPathAreaCount)
{
PrintToServer(" Count mismatches (was %d, now %d)", g_SavedEscapeRouteAreas.Length, TheEscapeRoute.m_nMainPathAreaCount);
fullyMatched = false;
}
else
{
for (int i = 0, size = TheEscapeRoute.m_nMainPathAreaCount; i < size; ++i)
{
TerrorNavArea area = TheEscapeRoute.GetMainPathArea(i);
if (area.GetID() != g_SavedEscapeRouteAreas.Get(i))
{
PrintToServer(" Area @ %d mismatches (was #%d, now #%d)", i, g_SavedEscapeRouteAreas.Get(i), area.GetID());
fullyMatched = false;
break;
}
}
}
PrintToServer("[%s] Finish checking escape route (success = %s)", PLUGIN_TAG, fullyMatched ? "yes" : "no");
if (!fullyMatched)
{
LogError("Unexpected mismatching escape route. Please report to author.");
}
}
else
{
PrintToServer("[%s] Saving escape route (count = %d)", PLUGIN_TAG, TheEscapeRoute.m_nMainPathAreaCount);
for (int i = 0, size = TheEscapeRoute.m_nMainPathAreaCount; i < size; ++i)
{
TerrorNavArea area = TheEscapeRoute.GetMainPathArea(i);
g_SavedEscapeRouteAreas.Push(area.GetID());
}
}
}
#endif