forked from tntgamer685347/TNTsTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.cpp
More file actions
211 lines (171 loc) · 7.22 KB
/
Example.cpp
File metadata and controls
211 lines (171 loc) · 7.22 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
#include "Example.hpp"
#include "Offsets.hpp" // using pointers is the most straightforward, safest and easist
#include "Modules/Mods/Drawing.hpp"
#include "ImGui/imgui.h"
#include <algorithm>
#include <cmath>
#ifndef IM_PI
#define IM_PI 3.14159265358979323846f
#endif
bool ExampleModule::IsInGame = false;
AGameEvent_Soccar_TA* ExampleModule::CurrentGameEvent = nullptr;
std::vector<CarBoostData> ExampleModule::carBoostData;
std::vector<FVector> ExampleModule::ballScreenPositions;
APRI_TA* ExampleModule::localPlayerPRI = nullptr;
void ExampleModule::Hook() {
Events.HookEventPre("Function TAGame.GameEvent_Soccar_TA.PostBeginPlay", OnGameEventStart);
Events.HookEventPre("Function TAGame.GameEvent_Soccar_TA.Destroyed", OnGameEventDestroyed);
Events.HookEventPre("Function TAGame.GameEvent_Soccar_TA.Active.BeginState", OnGameEventStart);
Events.HookEventPre("Function TAGame.GameEvent_Soccar_TA.Countdown.BeginState", OnGameEventStart);
Events.HookEventPost("Function TAGame.PlayerController_TA.PlayerTick", PlayerTickCalled);
}
void ExampleModule::OnGameEventDestroyed(PreEvent& event)
{
try
{
CurrentGameEvent = nullptr;
IsInGame = false;
carBoostData.clear();
ballScreenPositions.clear();
}
catch (...) { Console.Error("GameEventHook: Exception in OnGameEventDestroyed"); }
}
void ExampleModule::OnGameEventStart(PreEvent& event)
{
try
{
Console.Write("GameEventHook: Game event started: " + std::string(event.Function()->GetName()));
if (event.Caller() && event.Caller()->IsA(AGameEvent_Soccar_TA::StaticClass()))
{
CurrentGameEvent = static_cast<AGameEvent_Soccar_TA*>(event.Caller());
Console.Write("GameEventHook: Stored GameEvent instance");
}
IsInGame = true;
}
catch (...) { Console.Error("GameEventHook: Exception in OnGameEventStart"); }
}
void ExampleModule::PlayerTickCalled(const PostEvent& event) {
if (!IsInGame || !CurrentGameEvent || !event.Caller() || !event.Caller()->IsA(APlayerController_TA::StaticClass())) {
return;
}
carBoostData.clear();
ballScreenPositions.clear();
TArray<APlayerController_TA*> localPlayers = CurrentGameEvent->LocalPlayers;
if (localPlayers.size() == 0 || !localPlayers[0]) {
return;
}
APlayerController_TA* localPlayerController = localPlayers[0];
localPlayerPRI = localPlayerController->PRI;
TArray<ACar_TA*> cars = CurrentGameEvent->Cars;
TArray<ABall_TA*> balls = CurrentGameEvent->GameBalls;
for (APlayerController_TA* localPlayer : localPlayers) {
// example for getting Car/PRI
ACar_TA* car = localPlayer->Car;
APRI_TA* PRI = localPlayer->PRI;
//get input
//FVehicleInputs currentInputs = localPlayer->VehicleInput; //might not work
}
// get all cars with boost data
for (ACar_TA* car : cars) {
if (!car) continue;
FVector carLocation = car->Location;
FVector boostCircleLocation = carLocation;
boostCircleLocation.Z += 100.0f;
FVector screenPos = Drawing::CalculateScreenCoordinate(boostCircleLocation, localPlayerController);
ACarComponent_Boost_TA* boostComponent = car->BoostComponent;
float boostAmount = 0.0f;
try {
if (boostComponent) {
boostAmount = boostComponent->CurrentBoostAmount * 100;
Console.Write("Boost amount read: " + std::to_string(boostAmount));
}
else {
Console.Write("BoostComponent was nullptr");
}
}
catch (...) {
boostAmount = 0.0f;
Console.Write("BoostComponent was nullptr or sum");
}
CarBoostData data;
data.screenPosition = screenPos;
data.boostAmount = boostAmount;
Console.Write("Stored boost amount: " + std::to_string(data.boostAmount));
carBoostData.push_back(data);
}
// get all balls and save in list
for (ABall_TA* ball : balls) {
if (!ball) continue;
FVector ballLocation = ball->Location;
FVector screenPos = Drawing::CalculateScreenCoordinate(ballLocation, localPlayerController);
ballScreenPositions.push_back(screenPos);
}
}
void ExampleModule::OnRender() {
if (!IsInGame) {
return;
}
ImDrawList* drawList = ImGui::GetBackgroundDrawList();
// Draw Boost Circles
for (const CarBoostData& data : carBoostData) {
if (data.screenPosition.Z == 0) {
float boostPercentage = data.boostAmount / 100.0f;
if (boostPercentage < 0.0f) boostPercentage = 0.0f;
if (boostPercentage > 1.0f) boostPercentage = 1.0f;
ImVec2 center(data.screenPosition.X, data.screenPosition.Y);
float radius = 25.0f;
drawList->AddCircleFilled(center, radius, IM_COL32(50, 50, 50, 180));
if (boostPercentage > 0.0f) {
float startAngle = -IM_PI * 0.5f;
float endAngle = startAngle + (2.0f * IM_PI * boostPercentage);
ImU32 boostColor;
if (boostPercentage < 0.33f) {
boostColor = IM_COL32(255, 80, 80, 220);
}
else if (boostPercentage < 0.66f) {
boostColor = IM_COL32(255, 200, 0, 220);
}
else {
boostColor = IM_COL32(80, 255, 80, 220);
}
drawList->PathClear();
drawList->PathLineTo(center);
const int numSegments = 32;
for (int i = 0; i <= numSegments; i++) {
float angle = startAngle + (endAngle - startAngle) * ((float)i / numSegments);
float x = center.x + cosf(angle) * radius;
float y = center.y + sinf(angle) * radius;
drawList->PathLineTo(ImVec2(x, y));
}
drawList->PathLineTo(center);
drawList->PathFillConvex(boostColor);
}
drawList->AddCircle(center, radius, IM_COL32(255, 255, 255, 150), 32, 2.0f);
std::string boostText = std::to_string((int)data.boostAmount);
ImVec2 textSize = ImGui::CalcTextSize(boostText.c_str());
ImVec2 textPos(center.x - textSize.x * 0.5f, center.y - textSize.y * 0.5f);
drawList->AddText(textPos, IM_COL32(255, 255, 255, 255), boostText.c_str());
}
}
// ball circles
for (const FVector& screenPos : ballScreenPositions) {
if (screenPos.Z == 0) {
drawList->AddCircleFilled(ImVec2(screenPos.X, screenPos.Y), 8.f, IM_COL32(0, 255, 0, 255));
}
}
}
ExampleModule::ExampleModule() : Module("GameEventHook", "Hooks into game events", States::STATES_All) {
OnCreate();
}
ExampleModule::~ExampleModule() { OnDestroy(); }
void ExampleModule::OnCreate() {
// do non
}
void ExampleModule::OnDestroy() {
// do non
}
void ExampleModule::Initialize() {
Hook();
Console.Write("ExampleModule Initalized.");
}
ExampleModule Example;