-
Notifications
You must be signed in to change notification settings - Fork 4
/
gta2-helper.cpp
206 lines (171 loc) · 4.95 KB
/
gta2-helper.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
#include "pch.h"
#include "gta2-helper.h"
void fnShowCustomTextMessage(WCHAR* message) {
auto locales = ByPtr(LocaleSettings, ptrToS2LocalesSettings);
auto locale = locales->ptrToLocales;
auto original = locale->text;
locale->text = message;
fnShowMessageToPlayer(3, "1001");
locale->text = original;
}
Roof* getCarRoofWithSpriteIfExists(Roof* startroof, short spritetype)
{
if (!startroof)
return 0;
if (startroof->sprite->sprite == spritetype)
return startroof;
if (startroof->next)
return getCarRoofWithSpriteIfExists(startroof->next, spritetype);
return 0;
}
Roof* getCarLastRoof(Roof* startroof)
{
if (!startroof)
return 0;
if (startroof->next)
return getCarLastRoof(startroof->next);
return startroof;
}
Car* fnGetCarByID(int id) {
auto prefab = ByPtr(CarsPrefab, ptrToCarsPrefabs);
auto tcar = prefab->lastCar;
Car* car = 0;
while (tcar)
{
if (tcar->id == id) {
car = tcar;
break;
}
tcar = tcar->lastCar;
}
if (car) return car;
else return 0;
}
SCR_f FloatEncode(double x) {
return x * 16384;
}
double FloatDecode(SCR_f x) {
return (double)x / 16384.0;
}
Ped* FindTheNearestPed(Ped* basePed)
{
Ped* nearestPed = nullptr;
int nearestPedDistance = 16384 * 512;
PedManager_S25* manager = ByPtr(PedManager_S25, ptrToPedManager);
Ped* lastPed = manager->lastPedInArray;
if(!lastPed) return nullptr;
for (int i = 0; i <= lastPed->id; i++)
{
Ped* ped = fnGetPedByID(i);
if(!ped) continue;
if(ped == basePed) continue;
SCR_f x;
SCR_f y;
if (ped->currentCar) {
if(!ped->currentCar->sprite) continue;
x = ped->currentCar->sprite->x;
y = ped->currentCar->sprite->y;
}
else {
x = ped->x;
y = ped->y;
}
int pedDistance = sqrt(pow(x - basePed->x, 2) + pow(y - basePed->y, 2));
if (pedDistance <= nearestPedDistance)
{
nearestPedDistance = pedDistance;
nearestPed = ped;
}
}
return nearestPed;
}
Car* FindTheNearestCar(SCR_f x, SCR_f y, SCR_f z)
{
Car* nearestCar = nullptr;
int nearestCarDistance = 16384 * 512;
auto prefab = ByPtr(CarsPrefab, ptrToCarsPrefabs);
auto lastCar = prefab->lastCar;
if(!lastCar) return nullptr;
for (int i = 0; i <= lastCar->id; i++)
{
Car* car = fnGetCarByID(i);
if (!car || !car->sprite || !car->sprite->x) continue;
int carDistance = sqrt(pow(car->sprite->x - x, 2) + pow(car->sprite->y - y, 2));
if (carDistance <= nearestCarDistance)
{
nearestCarDistance = carDistance;
nearestCar = car;
}
}
return nearestCar;
}
Car* FindTheNearestCar(Ped* basePed)
{
if (!basePed) return nullptr;
return FindTheNearestCar(basePed->x, basePed->y, basePed->z);
}
SCR_f* GetPointInADistance(SCR_f baseX, SCR_f baseY, short angle, SCR_f distance) {
SCR_f targetXY[2];
double nAngle = angle / 4.0 + 270.0;
targetXY[0] = baseX + (cos(nAngle * 0.017453f) * distance);
targetXY[1] = baseY - (sin(nAngle * 0.017453f) * distance);
return targetXY;
}
POINT ConvertGameWorldCoordinateToScreen(SCR_f gameX, SCR_f gameY) {
POINT p;
int width = *(int*)0x00673578;
int height = *(int*)0x006732e8;
auto pp = ByPtr(CameraOrPhysics, ptrToPlayerPhysics);
double right = FloatDecode(pp->cameraBoundaries.right);
double left = FloatDecode(pp->cameraBoundaries.left);
double widthX = right - left;
double gameUnitsToScreenProportionX = (double)width / widthX;
double targetX = FloatDecode(gameX) - left; // offset from left
double top = FloatDecode(pp->cameraBoundaries.top);
double bottom = FloatDecode(pp->cameraBoundaries.bottom);
double heightY = bottom - top;
double gameUnitsToScreenProportionY = (double)height / heightY;
double targetY = FloatDecode(gameY) - top; // offset from top
double centerX = (right - left) / 2.0 + left;
double centerY = (bottom - top) / 2.0 + top;
const double magic = 50;
const int magic2 = 800;
double zoom = (double)widthX / 9.0;
double cameraCorrectionX = (FloatDecode(gameX) - centerX) / magic;
double cameraCorrectionY = (FloatDecode(gameY) - centerY) / magic;
p.x = (long)(targetX * gameUnitsToScreenProportionX + cameraCorrectionX * magic2 / zoom);
p.y = (long)(targetY * gameUnitsToScreenProportionY + cameraCorrectionY * magic2 / zoom);
return p;
}
bool IsPointSafe(SCR_f x, SCR_f y, SCR_f z)
{
if(x < 1 * 16384) return false;
if(x > 254 * 16384) return false;
if(y < 1 * 16384) return false;
if(y > 254 * 16384) return false;
if(z < 0 * 16384) return false;
if(z > 7 * 16384) return false;
return true;
}
void ClampPointToSafe(SCR_f &x, SCR_f &y)
{
if(x < 1 * 16384) x = 1 * 16384;
if(x > 254 * 16384) x = 254 * 16384;
if(y < 1 * 16384) y = 1 * 16384;
if(y > 254 * 16384) y = 254 * 16384;
}
void ClampPointToSafe(SCR_f& x, SCR_f& y, SCR_f& z)
{
ClampPointToSafe(x, y);
if(z < 0 * 16384) z = 0 * 16384;
if(z > 7 * 16384) z = 7 * 16384;
}
void ReplaceCode(DWORD* address, BYTE* newCode, int length)
{
DWORD oldProtection = { 0 };
VirtualProtectEx(GetCurrentProcess(), (LPVOID)address, length, PAGE_EXECUTE_READWRITE, &oldProtection);
for (int i = 0; i < length; i++)
{
*(BYTE*)((LPBYTE)address + i) = newCode[i];
}
}