forked from SirPlease/L4D2-Competitive-Rework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl4d2_static_shotgun_spread.sp
More file actions
197 lines (158 loc) · 5.52 KB
/
Copy pathl4d2_static_shotgun_spread.sp
File metadata and controls
197 lines (158 loc) · 5.52 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
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sourcescramble>
#define BULLET_MAX_SIZE 3
#define GAMEDATA_FILE "code_patcher"
#define KEY_SGSPREAD "sgspread"
// Original code & Notes: https://github.com/Jahze/l4d2_plugins/tree/master/spread_patch
// Static Shotgun Spread leverages code_patcher (code_patcher.txt gamedata)
// to replace RNG in pellet spread with static factors.
// This plugin allows you to adjust the spread characteristics, by live patching operands in the custom ASM.
// You can use the visualise_impacts.smx plugin to test the resulting spread.
// It will render small purple boxes where the server-side pellets land.
enum
{
eWindows = 0,
eLinux,
/* eMac, joke:) */
ePlatform_Size
};
int
g_ePlatform = eLinux;
static const int
g_iBulletOffsets[ePlatform_Size][BULLET_MAX_SIZE] = {
{ 0xf, 0x21, 0x30 }, // Windows
{ 0x11, 0x22, 0x2f } // Linux
},
g_iDegreeOffsets[ePlatform_Size] = {
0x3f, // Windows
0x43 // Linux
},
g_iFactorOffset[ePlatform_Size] = {
0x36, // Windows
0x34 // Linux
},
g_iCenterPelletOffset[ePlatform_Size] = {
-0x36, // Windows
-0x1c // Linux
};
Address
g_pBullets[BULLET_MAX_SIZE] = {Address_Null, ...},
g_pDegree = Address_Null,
g_pFactor = Address_Null,
g_pCenterPellet = Address_Null;
MemoryPatch
g_hPatchSgSpread = null;
ConVar
g_hCvarRing1Bullets = null,
g_hCvarRing1Factor = null,
g_hCvarCenterPellet = null;
public Plugin myinfo =
{
name = "L4D2 Static Shotgun Spread",
author = "Jahze, Visor, A1m`, Rena",
version = "1.6.5",
description = "Changes the values in the sgspread patch",
url = "https://github.com/SirPlease/L4D2-Competitive-Rework"
};
public void OnPluginStart()
{
InitGameData();
g_hCvarRing1Bullets = CreateConVar("sgspread_ring1_bullets", "3", "Number of bullets for the first ring, the remaining bullets will be in the second ring.");
g_hCvarRing1Factor = CreateConVar("sgspread_ring1_factor", "2", "Determines how far or closer the bullets will be from the center for the first ring.");
g_hCvarCenterPellet = CreateConVar("sgspread_center_pellet", "1", "Center pellet: 0 - off, 1 - on.", _, true, 0.0, true, 1.0);
InitPlugin();
}
void InitGameData()
{
Handle hConf = LoadGameConfigFile(GAMEDATA_FILE);
if (hConf == null) {
SetFailState("Missing gamedata \"" ... GAMEDATA_FILE ... "\"");
}
g_ePlatform = GameConfGetOffset(hConf, "OS");
if (g_ePlatform == -1) {
SetFailState("Failed to retrieve offset \"OS\"");
}
g_hPatchSgSpread = MemoryPatch.CreateFromConf(hConf, KEY_SGSPREAD);
if (g_hPatchSgSpread == null || !g_hPatchSgSpread.Validate()) {
SetFailState("Failed to validate MemoryPatch \"" ... KEY_SGSPREAD ... "\"");
}
Address pFinalAddr = g_hPatchSgSpread.Address + view_as<Address>(g_iCenterPelletOffset[g_ePlatform]);
int iCurrentValue = LoadFromAddress(pFinalAddr, NumberType_Int8);
if (iCurrentValue != 1) {
SetFailState("Center pellet offset is uncorrect! CheckByte: %x", iCurrentValue);
}
if (!g_hPatchSgSpread.Enable()) {
SetFailState("Failed to enable MemoryPatch \"" ... KEY_SGSPREAD ... "\"");
}
delete hConf;
}
void InitPlugin()
{
for (int i = 0; i < BULLET_MAX_SIZE; i++) {
g_pBullets[i] = g_hPatchSgSpread.Address + view_as<Address>(g_iBulletOffsets[g_ePlatform][i]);
}
g_pDegree = g_hPatchSgSpread.Address + view_as<Address>(g_iDegreeOffsets[g_ePlatform]);
g_pFactor = g_hPatchSgSpread.Address + view_as<Address>(g_iFactorOffset[g_ePlatform]);
g_pCenterPellet = g_hPatchSgSpread.Address + view_as<Address>(g_iCenterPelletOffset[g_ePlatform]);
HotPatchBullets(g_hCvarRing1Bullets.IntValue);
HotPatchFactor(g_hCvarRing1Factor.IntValue);
HotPatchCenterPellet(g_hCvarCenterPellet.BoolValue);
g_hCvarRing1Bullets.AddChangeHook(OnRing1BulletsChange);
g_hCvarRing1Factor.AddChangeHook(OnRing1FactorChange);
g_hCvarCenterPellet.AddChangeHook(OnCenterPelletChange);
}
public void OnPluginEnd()
{
if (g_pCenterPellet != Address_Null) {
int iCurrentValue = LoadFromAddress(g_pCenterPellet, NumberType_Int8);
if (iCurrentValue != 1) {
StoreToAddress(g_pCenterPellet, 1, NumberType_Int8);
}
}
}
void OnRing1BulletsChange(ConVar hConVar, const char[] sOldValue, const char[] sNewValue)
{
HotPatchBullets(hConVar.IntValue);
}
void OnRing1FactorChange(ConVar hConVar, const char[] sOldValue, const char[] sNewValue)
{
HotPatchFactor(hConVar.IntValue);
}
void OnCenterPelletChange(ConVar hConVar, const char[] sOldValue, const char[] sNewValue)
{
HotPatchCenterPellet(hConVar.BoolValue);
}
void HotPatchBullets(int iBullets)
{
PatchBullets(iBullets, g_hCvarCenterPellet.BoolValue);
float fBullets = float(iBullets);
if (g_ePlatform == eLinux) {
fBullets *= 2.0;
}
StoreToAddress(g_pDegree, view_as<int>(360.0 / fBullets), NumberType_Int32);
}
void HotPatchFactor(int iFactor)
{
// Asm patch on windows need the float type !
int iSetFactor = (g_ePlatform == eWindows) ? view_as<int>(float(iFactor)) : iFactor;
StoreToAddress(g_pFactor, iSetFactor, NumberType_Int32);
}
void HotPatchCenterPellet(bool bNewValue)
{
bool iCurrentValue = LoadFromAddress(g_pCenterPellet, NumberType_Int8);
if (iCurrentValue == bNewValue) {
return;
}
StoreToAddress(g_pCenterPellet, view_as<int>(bNewValue), NumberType_Int8);
PatchBullets(g_hCvarRing1Bullets.IntValue, bNewValue);
}
void PatchBullets(int iBullets, bool bCenterPellet)
{
int iOffset1 = iBullets + (bCenterPellet ? 1 : 0);
int iOffset2 = iBullets + (bCenterPellet ? 2 : 1);
StoreToAddress(g_pBullets[0], iOffset1, NumberType_Int8);
StoreToAddress(g_pBullets[1], iOffset1, NumberType_Int8);
StoreToAddress(g_pBullets[2], iOffset2, NumberType_Int8);
}