forked from Prof-Butts/Hook_XWACockpitLook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelemetry.cpp
299 lines (274 loc) · 14.5 KB
/
Telemetry.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
#include <windows.h>
#include <Stdio.h>
#include <stdarg.h>
#include <string>
#include "XWAObject.h"
#include "Telemetry.h"
#include "UDP.h"
extern const int *localPlayerIndex;
extern const unsigned int *g_playerInHangar;
extern const __int8 *provingGrounds;
extern ObjectEntry **objects;
extern PlayerDataEntry *PlayerDataTable;
extern CraftDefinitionEntry *CraftDefinitionTable;
PlayerTelemetry g_PrevPlayerTelemetry;
TargetTelemetry g_TargetTelemetry;
LocationTelemetry g_LocationTelemetry;
void log_debug(const char *format, ...);
#ifdef TELEMETRY_JSON
void SendXWADataOverUDP()
{
std::string msg = "";
//char buf[512];
//sprintf_s(buf, 512, "speed:%d", (int)(PlayerDataTable[*localPlayerIndex].currentSpeed / 2.25f));
msg += "{\n";
// PLAYER SECTION
msg += "\t\"player\":\n\t{\n";
{
int16_t objectIndex = (int16_t)PlayerDataTable[*localPlayerIndex].objectIndex;
if (objectIndex <= 0) goto target_section;
int speed = (int)(PlayerDataTable[*localPlayerIndex].currentSpeed / 2.25f);
ObjectEntry *object = &((*objects)[objectIndex]);
if (object == NULL) goto target_section;
MobileObjectEntry *mobileObject = object->MobileObjectPtr;
if (mobileObject == NULL) goto target_section;
CraftInstance *craftInstance = mobileObject->craftInstancePtr;
if (craftInstance == NULL) goto target_section;
CraftDefinitionEntry *craftDefinition = &(CraftDefinitionTable[craftInstance->CraftType]);
if (craftDefinition == NULL) goto target_section;
char *name = (char *)craftDefinition->pCraftName;
char *short_name = (char *)craftDefinition->pCraftShortName;
int hull = (int)(100.0f * (1.0f - (float)craftInstance->HullDamageReceived / (float)craftInstance->HullStrength));
hull = max(0, hull);
float total_shield_points = 2.0f * (float)craftDefinition->ShieldHitPoints;
int shields_front = (int)(100.0 * (float)craftInstance->ShieldPointsFront / total_shield_points);
int shields_back = (int)(100.0 * (float)craftInstance->ShieldPointsBack / total_shield_points);
shields_front = max(0, shields_front);
shields_back = max(0, shields_back);
msg += "\t\t\"name\" : \"" + std::string(name) + "\",\n";
msg += "\t\t\"short name\" : \"" + std::string(short_name) + "\",\n";
msg += "\t\t\"speed\" : " + std::to_string(speed) + ",\n";
//msg += "\t\t\"current speed\" : " + std::to_string(mobileObject->currentSpeed) + "\n"; // Redundant
msg += "\t\t\"craft type\" : " + std::to_string(craftInstance->CraftType) + ",\n";
msg += "\t\t\"ELS lasers\" : " + std::to_string(craftInstance->ElsLasers) + ",\n";
msg += "\t\t\"ELS shields\" : " + std::to_string(craftInstance->ElsShields) + ",\n";
msg += "\t\t\"ELS beam\" : " + std::to_string(craftInstance->ElsBeam) + ",\n";
msg += "\t\t\"s-foils\" : " + std::to_string(craftInstance->SfoilsState) + ",\n";
msg += "\t\t\"shield direction\" : " + std::to_string(craftInstance->ShieldDirection) + ",\n";
msg += "\t\t\"front shields\" : " + std::to_string(shields_front) + ",\n";
msg += "\t\t\"back shields\" : " + std::to_string(shields_back) + ",\n";
msg += "\t\t\"hull\" : " + std::to_string(hull) + ",\n";
msg += "\t\t\"system\" : " + std::to_string(craftInstance->SystemStrength) + ",\n";
msg += "\t\t\"beam active\" : " + std::to_string(craftInstance->BeamActive) + ",\n";
msg += "\t\t\"beam energy\" : " + std::to_string(craftInstance->BeamEnergy) + "\n";
//log_debug("[UDP] Throttle: %d", CraftDefinitionTable[objectIndex].EngineThrottle);
//log_debug("[UDP] Throttle: %s", CraftDefinitionTable[objectIndex].CockpitFileName);
//log_debug("[UDP] localPlayerIndex: %d, Index: %d", *localPlayerIndex, PlayerDataTable[*localPlayerIndex].objectIndex);
//Prints 0, 0
//log_debug("[UDP] CameraIdx: %d", PlayerDataTable[*localPlayerConnectedAs].cameraFG);
// Prints -1
}
target_section:
msg += "\t},\n";
msg += "\t\"target\":\n\t{\n";
{
//log_debug("[UDP] Player IFF: %d, team: %d", PlayerDataTable[*localPlayerIndex].IFF, PlayerDataTable[*localPlayerIndex].team);
short currentTargetIndex = PlayerDataTable[*localPlayerIndex].currentTargetIndex;
// currentTargetIndex can apparently be 0. I remember the game crashing, but it doesn't anymore!
if (currentTargetIndex < 0) goto status_section;
ObjectEntry *object = &((*objects)[currentTargetIndex]);
if (object == NULL) goto status_section;
MobileObjectEntry *mobileObject = object->MobileObjectPtr;
if (mobileObject == NULL) goto status_section;
CraftInstance *craftInstance = mobileObject->craftInstancePtr;
if (craftInstance == NULL) goto status_section;
CraftDefinitionEntry *craftDefinition = &(CraftDefinitionTable[craftInstance->CraftType]);
if (craftDefinition == NULL) goto status_section;
char *name = (char *)craftDefinition->pCraftName;
char *short_name = (char *)craftDefinition->pCraftShortName;
int IFF = object->MobileObjectPtr->IFF;
int hull = (int)(100.0f * (1.0f - (float)craftInstance->HullDamageReceived / (float)craftInstance->HullStrength));
hull = max(0, hull);
float total_shield_points = 2.0f * (float)craftDefinition->ShieldHitPoints;
int shields = (int)(100.0f * (craftInstance->ShieldPointsFront + craftInstance->ShieldPointsBack) / total_shield_points);
shields = max(0, shields);
msg += "\t\t\"name\" : \"" + std::string(name) + "\",\n";
msg += "\t\t\"short name\" : \"" + std::string(short_name) + "\",\n";
msg += "\t\t\"IFF\" : " + std::to_string(IFF) + ",\n";
msg += "\t\t\"cargo\" : \"" + std::string(craftInstance->Cargo) + "\",\n";
msg += "\t\t\"craft type\" : " + std::to_string(craftInstance->CraftType) + ",\n";
//msg += "\t\t\"shield direction\" : " + std::to_string(craftInstance->ShieldDirection) + "\n";
msg += "\t\t\"shields\" : " + std::to_string(shields) + ",\n";
msg += "\t\t\"hull\" : " + std::to_string(hull) + ",\n";
// state is 0 when the craft is static
// state is 3 when the craft is destroyed
msg += "\t\t\"state\" : " + std::to_string(craftInstance->CraftState) + ",\n";
//msg += "\t\t\"system\" : " + std::to_string(craftInstance->SystemStrength) + "\n";
// CycleTime is always 236, CycleTimer counts down from CycleTime to -1 and starts over
//msg += "\t\t\"cycle time\" : " + std::to_string(craftInstance->CycleTime) + "\n";
//msg += "\t\t\"cycle timer\" : " + std::to_string(craftInstance->CycleTimer) + "\n";
//log_debug("[UDP] %s", object->MobileObjectPtr->pChar); // Displays (null)
//log_debug("[UDP] Target IFF: %d, Team: %d, MarkingColor: %d", object->MobileObjectPtr->IFF, object->MobileObjectPtr->Team, object->MobileObjectPtr->markingColor);
}
status_section:
msg += "\t},\n";
msg += "\t{\n";
msg += "\t\"status\":\n\t{\n";
{
msg += "\t\t\"location\" : ";
if ((*g_playerInHangar))
msg += "\"hangar\"\n";
else
msg += "\"space\"\n";
}
msg += "\t}\n";
// Final bracket
msg += "}";
SendUDPMessage((char *)msg.c_str());
}
#endif
#ifdef TELEMETRY_SIMPLIFIED
void SendXWADataOverUDP()
{
std::string msg = "";
// PLAYER SECTION
{
int16_t objectIndex = (int16_t)PlayerDataTable[*localPlayerIndex].objectIndex;
if (objectIndex <= 0) goto target_section;
int speed = (int)(PlayerDataTable[*localPlayerIndex].currentSpeed / 2.25f);
//log_debug("[DBG] objectIndex: %d, *localPlayerIndex: %d, localPlayerIndex: 0x%x", objectIndex, *localPlayerIndex, localPlayerIndex);
ObjectEntry *object = &((*objects)[objectIndex]);
if (object == NULL) goto target_section;
MobileObjectEntry *mobileObject = object->MobileObjectPtr;
if (mobileObject == NULL) goto target_section;
CraftInstance *craftInstance = mobileObject->craftInstancePtr;
if (craftInstance == NULL) goto target_section;
CraftDefinitionEntry *craftDefinition = &(CraftDefinitionTable[craftInstance->CraftType]);
if (craftDefinition == NULL) goto target_section;
char *name = (char *)craftDefinition->pCraftName;
char *short_name = (char *)craftDefinition->pCraftShortName;
int hull = (int)(100.0f * (1.0f - (float)craftInstance->HullDamageReceived / (float)craftInstance->HullStrength));
hull = max(0, hull);
float total_shield_points = 2.0f * (float)craftDefinition->ShieldHitPoints;
int shields_front = (int)(100.0 * (float)craftInstance->ShieldPointsFront / total_shield_points);
int shields_back = (int)(100.0 * (float)craftInstance->ShieldPointsBack / total_shield_points);
shields_front = max(0, shields_front);
shields_back = max(0, shields_back);
if (name != g_PrevPlayerTelemetry.name)
msg += "player|crafttypename:" + std::string(name) + "\n";
if (short_name != g_PrevPlayerTelemetry.short_name)
msg += "player|shortcrafttypename:" + std::string(short_name) + "\n";
if (speed != g_PrevPlayerTelemetry.speed)
msg += "player|speed:" + std::to_string(speed) + "\n";
//msg += "\t\t\"current speed\" : " + std::to_string(mobileObject->currentSpeed) + "\n"; // Redundant
if (craftInstance->ElsLasers != g_PrevPlayerTelemetry.ElsLasers)
msg += "player|elslasers:" + std::to_string(craftInstance->ElsLasers) + "\n";
if (craftInstance->ElsShields != g_PrevPlayerTelemetry.ElsShields)
msg += "player|elsshields:" + std::to_string(craftInstance->ElsShields) + "\n";
if (craftInstance->ElsBeam != g_PrevPlayerTelemetry.ElsBeam)
msg += "player|elsbeam:" + std::to_string(craftInstance->ElsBeam) + "\n";
if (craftInstance->SfoilsState != g_PrevPlayerTelemetry.SfoilsState)
msg += "player|sfoils:" + std::to_string(craftInstance->SfoilsState) + "\n";
if (craftInstance->ShieldDirection != g_PrevPlayerTelemetry.ShieldDirection)
msg += "player|shielddirection:" + std::to_string(craftInstance->ShieldDirection) + "\n";
if (shields_front != g_PrevPlayerTelemetry.shields_front)
msg += "player|shieldfront:" + std::to_string(shields_front) + "\n";
if (shields_back != g_PrevPlayerTelemetry.shields_back)
msg += "player|shieldback:" + std::to_string(shields_back) + "\n";
if (hull != g_PrevPlayerTelemetry.hull)
msg += "player|hull:" + std::to_string(hull) + "\n";
//msg = "player|system:" + std::to_string(craftInstance->SystemStrength);
if (craftInstance->BeamActive != g_PrevPlayerTelemetry.BeamActive)
msg += "player|beamactive:" + std::to_string(craftInstance->BeamActive) + "\n";
//craftInstance->BeamEnergy
//log_debug("[UDP] Throttle: %d", CraftDefinitionTable[objectIndex].EngineThrottle);
//log_debug("[UDP] Throttle: %s", CraftDefinitionTable[objectIndex].CockpitFileName);
//log_debug("[UDP] localPlayerIndex: %d, Index: %d", *localPlayerIndex, PlayerDataTable[*localPlayerIndex].objectIndex);
//Prints 0, 0
//log_debug("[UDP] CameraIdx: %d", PlayerDataTable[*localPlayerConnectedAs].cameraFG);
// Prints -1
// Store the data for the next frame
g_PrevPlayerTelemetry.name = name;
g_PrevPlayerTelemetry.short_name = short_name;
g_PrevPlayerTelemetry.speed = speed;
g_PrevPlayerTelemetry.ElsLasers = craftInstance->ElsLasers;
g_PrevPlayerTelemetry.ElsShields = craftInstance->ElsShields;
g_PrevPlayerTelemetry.ElsBeam = craftInstance->ElsBeam;
g_PrevPlayerTelemetry.SfoilsState = craftInstance->SfoilsState;
g_PrevPlayerTelemetry.ShieldDirection = craftInstance->ShieldDirection;
g_PrevPlayerTelemetry.shields_front = shields_front;
g_PrevPlayerTelemetry.shields_back = shields_back;
g_PrevPlayerTelemetry.hull = hull;
g_PrevPlayerTelemetry.BeamActive = craftInstance->BeamActive;
}
// TARGET SECTION
target_section:
{
short currentTargetIndex = PlayerDataTable[*localPlayerIndex].currentTargetIndex;
// currentTargetIndex can apparently be 0. I remember the game crashing, but it doesn't anymore!
if (currentTargetIndex < 0) goto status_section;
ObjectEntry *object = &((*objects)[currentTargetIndex]);
if (object == NULL) goto status_section;
MobileObjectEntry *mobileObject = object->MobileObjectPtr;
if (mobileObject == NULL) goto status_section;
CraftInstance *craftInstance = mobileObject->craftInstancePtr;
if (craftInstance == NULL) goto status_section;
CraftDefinitionEntry *craftDefinition = &(CraftDefinitionTable[craftInstance->CraftType]);
if (craftDefinition == NULL) goto status_section;
char *name = (char *)craftDefinition->pCraftName;
char *short_name = (char *)craftDefinition->pCraftShortName;
int IFF = object->MobileObjectPtr->IFF;
int hull = (int)(100.0f * (1.0f - (float)craftInstance->HullDamageReceived / (float)craftInstance->HullStrength));
hull = max(0, hull);
float total_shield_points = 2.0f * (float)craftDefinition->ShieldHitPoints;
int shields = (int)(100.0f * (craftInstance->ShieldPointsFront + craftInstance->ShieldPointsBack) / total_shield_points);
shields = max(0, shields);
if (name != g_TargetTelemetry.name)
msg += "target|crafttypename:" + std::string(name) + "\n";
if (short_name != g_TargetTelemetry.short_name)
msg += "target|shortcrafttypename:" + std::string(short_name) + "\n";
if (IFF != g_TargetTelemetry.IFF)
msg += "target|IFF:" + std::to_string(IFF) + "\n";
if (strcmp(craftInstance->Cargo, g_TargetTelemetry.Cargo) != 0)
msg += "target|cargo:" + std::string(craftInstance->Cargo) + "\n";
if (shields != g_TargetTelemetry.shields)
msg += "target|shields:" + std::to_string(shields) + "\n";
if (hull != g_TargetTelemetry.hull)
msg += "target|hull:" + std::to_string(hull) + "\n";
// state is 0 when the craft is static
// state is 3 when the craft is destroyed
if (craftInstance->CraftState != g_TargetTelemetry.CraftState)
msg += "target|state:" + std::to_string(craftInstance->CraftState) + "\n";
// CycleTime is always 236, CycleTimer counts down from CycleTime to -1 and starts over
//msg += "\t\t\"cycle time\" : " + std::to_string(craftInstance->CycleTime) + "\n";
//msg += "\t\t\"cycle timer\" : " + std::to_string(craftInstance->CycleTimer) + "\n";
// Store the data for the next frame
g_TargetTelemetry.name = name;
g_TargetTelemetry.short_name = short_name;
g_TargetTelemetry.IFF = IFF;
memcpy(g_TargetTelemetry.Cargo, craftInstance->Cargo, 16);
g_TargetTelemetry.shields = shields;
g_TargetTelemetry.hull = hull;
g_TargetTelemetry.CraftState = craftInstance->CraftState;
}
// STATUS SECTION
status_section:
{
if ((bool)(*g_playerInHangar) != g_LocationTelemetry.playerInHangar) {
msg += "status|location:";
if ((*g_playerInHangar))
msg += "hangar";
else
msg += "space";
msg += "\n";
}
g_LocationTelemetry.playerInHangar = (bool)(*g_playerInHangar);
}
int len = msg.length();
if (len > 0) {
msg.erase(msg.find_last_not_of(" \t\n") + 1);
if (msg.length() > 0)
SendUDPMessage((char *)msg.c_str());
}
}
#endif