-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDID.as
373 lines (321 loc) · 13.3 KB
/
DID.as
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
void Main() {
DID::Main();
}
void Render() {
if (UI::IsGameUIVisible() || !diegeticDisabledWithUI) {
if (!DID::diegeticDisplayOverridden) {
DID::Render();
}
}
}
bool keyHeldDown = false;
void OnKeyPress(bool down, VirtualKey key)
{
if (!keyHeldDown) {
if(key == diegeticEnabledShortcut)
{
diegeticEnabled = !diegeticEnabled;
}
}
keyHeldDown = down;
}
void RenderMenu() {
if(UI::MenuItem("\\$8f0" + Icons::InfoCircle + "\\$z Diegetic Information Display", "", diegeticEnabled)) {
diegeticEnabled = !diegeticEnabled;
}
}
void OnSettingsChanged() {
DID::OnSettingsChanged();
}
namespace DID {
CameraSpecificSettings CSP;
void Render() {
if (!diegeticEnabled) return;
auto vp = VehicleState::GetViewingPlayer();
if (vp is null) return;
auto vis = VehicleState::GetVis(GetApp().GameScene, vp);
if (vis is null) return;
if (GetApp().Network.PlaygroundClientScriptAPI.IsInGameMenuDisplayed) return;
DID::ResetDrawState(vis);
Camera::ActiveCam curCam;
if (useCameraDetection == CameraDetectionMode::On || (Camera::CameraNodSafe && useCameraDetection == CameraDetectionMode::Auto)) {
curCam = Camera::GetCurrentGameCamera();
} else {
vec3 cam1 = DID::projectHatSpace(vec3(0,0,-2));
vec3 cam3 = DID::projectHatSpace(vec3(0,0,-1));
if (Camera::IsBehind(cam3)) {
curCam = Camera::ActiveCam::Cam3Alt;
} else if (Camera::IsBehind(cam1)) {
curCam = Camera::ActiveCam::Cam3;
} else {
curCam = Camera::ActiveCam::Cam1;
}
}
switch (curCam) {
case Camera::ActiveCam::Cam3:
CSP.diegeticCustomOffset = diegeticCustomOffsetCam3;
CSP.diegeticHorizontalDistance = diegeticHorizontalDistanceCam3;
CSP.diegeticLetterSpacing = diegeticLetterSpacingCam3;
CSP.diegeticLineSpacing = diegeticLineSpacingCam3;
CSP.diegeticScale = diegeticScaleCam3;
break;
case Camera::ActiveCam::Cam3Alt:
CSP.diegeticCustomOffset = diegeticCustomOffsetCam3a;
CSP.diegeticHorizontalDistance = diegeticHorizontalDistanceCam3a;
CSP.diegeticLetterSpacing = diegeticLetterSpacingCam3a;
CSP.diegeticLineSpacing = diegeticLineSpacingCam3a;
CSP.diegeticScale = diegeticScaleCam3a;
break;
default:
CSP.diegeticCustomOffset = diegeticCustomOffset;
CSP.diegeticHorizontalDistance = diegeticHorizontalDistance;
CSP.diegeticLetterSpacing = diegeticLetterSpacing;
CSP.diegeticLineSpacing = diegeticLineSpacing;
CSP.diegeticScale = diegeticScale;
break;
}
float camDot = Math::Dot(Camera::GetCurrentLookingDirection(), vis.AsyncState.Dir);
CSP.backwards = camDot < 0;
nvg::StrokeWidth(diegeticStrokeWidth*CSP.diegeticScale);
nvg::LineCap(nvg::LineCapType::Butt);
nvg::LineJoin(nvg::LineCapType::Butt);
nvg::MiterLimit(0);
// get max length of left items to add padding
int maxLen = 0;
string[] leftPadded;
leftPadded.Resize(4);
for (uint i = 0; i < 8; i++) {
maxLen = Math::Max(lanes[i].content.Length, maxLen);
}
for (uint i = 0; i < 4; i++) {
leftPadded[i] = LOOOOOOOONG.SubStr(0, maxLen - lanes[i].content.Length) + lanes[i].content;
}
int leftOffset, rightOffset;
if (CSP.backwards) {
leftOffset = CSP.diegeticHorizontalDistance;
rightOffset = (CSP.diegeticHorizontalDistance + maxLen*uint(CSP.diegeticLetterSpacing))*-1;
} else {
leftOffset = (CSP.diegeticHorizontalDistance + maxLen*uint(CSP.diegeticLetterSpacing)) * -1;
rightOffset = CSP.diegeticHorizontalDistance;
}
for (uint i = 0; i < 4; i++) {
if (diegeticOutline.w > 0.0) {
nvg::StrokeColor(diegeticOutline);
nvg::LineCap(nvg::LineCapType::Round);
nvg::LineJoin(nvg::LineCapType::Round);
nvg::StrokeWidth(diegeticStrokeWidth*3.0*CSP.diegeticScale);
DID::drawString(leftPadded[i], vec2(leftOffset, 0)+CSP.diegeticCustomOffset.xy, CSP.diegeticCustomOffset.z + i*CSP.diegeticLineSpacing*-1);
DID::drawString(lanes[i+4].content, vec2(rightOffset, 0)+CSP.diegeticCustomOffset.xy, CSP.diegeticCustomOffset.z + i*CSP.diegeticLineSpacing*-1);
nvg::StrokeWidth(diegeticStrokeWidth*CSP.diegeticScale);
nvg::LineCap(nvg::LineCapType::Butt);
nvg::LineJoin(nvg::LineCapType::Butt);
}
if (diegeticFillmode) {
nvg::FillColor(lanes[i].color);
} else {
nvg::StrokeColor(lanes[i].color);
}
DID::drawString(leftPadded[i], vec2(leftOffset, 0)+CSP.diegeticCustomOffset.xy, CSP.diegeticCustomOffset.z + i*CSP.diegeticLineSpacing*-1);
if (diegeticFillmode) {
nvg::FillColor(lanes[i+4].color);
} else {
nvg::StrokeColor(lanes[i+4].color);
}
DID::drawString(lanes[i+4].content, vec2(rightOffset, 0)+CSP.diegeticCustomOffset.xy, CSP.diegeticCustomOffset.z + i*CSP.diegeticLineSpacing*-1);
}
}
DID::LaneConfig@[] lanes;
void Main() {
init();
while (true) {
auto app = cast<CTrackMania>(GetApp());
auto map = app.RootMap;
if(map is null || map.MapInfo.MapUid == "" || VehicleState::GetViewingPlayer() is null) {
yield();
} else {
step();
}
yield();
}
}
void OnSettingsChanged() {
init();
}
void init() {
lanes.Resize(8);
font = dictionary();
loadFont();
registerLaneProviderAddon(NullProvider());
registerLaneProviderAddon(FrontSpeedProvider());
registerLaneProviderAddon(SideSpeedProvider());
registerLaneProviderAddon(SteeringProvider());
registerLaneProviderAddon(WetTiresProvider());
registerLaneProviderAddon(GearProvider());
registerLaneProviderAddon(RPMProvider());
registerLaneProviderAddon(IceTireProviderAvg());
registerLaneProviderAddon(IceTireProviderFL());
registerLaneProviderAddon(IceTireProviderFR());
registerLaneProviderAddon(IceTireProviderRL());
registerLaneProviderAddon(IceTireProviderRR());
#if DEPENDENCY_MLFEEDRACEDATA && DEPENDENCY_MLHOOK
registerLaneProviderAddon(RaceTimeHandler());
registerLaneProviderAddon(RaceTimeSplitHandler());
registerLaneProviderAddon(SplitDeltaHandler());
registerLaneProviderAddon(CurrentLapHandler());
registerLaneProviderAddon(CurrentLapAlwaysHandler());
registerLaneProviderAddon(CurrentCheckpointHandler());
registerLaneProviderAddon(NoRespawnTimeHandler());
registerLaneProviderAddon(NoRespawnSplitsHandler());
#endif
#if DEPENDENCY_SPLITSPEEDS
DID::registerLaneProviderAddon(SplitSpeedsDiffProvider());
DID::registerLaneProviderAddon(SplitSpeedsSpeedProvider());
#endif
#if DEPENDENCY_AK_HINTS || DEPENDENCY_AK_HINTS_DEV
DID::registerLaneProviderAddon(AkHintsHandler());
#endif
// fill all our slots with empty info to prevent NPE on first frame running
for (uint i = 0; i < 8; i++) {
@lanes[i] = getInfoText("DID/NullProvider", i);
}
SetLaneProvidersFromSettings();
startnew(initYield);
}
void initYield() {
Camera::CheckIfSafe();
}
// Currently, this takes about 0.3ms for me which is approx 1/3 of total execution time with a basic DID layout.
// This is most likely due to bad memory reuse.
// Suggestion: add a `.Update` method to `LaneConfig` that can copy values from an update source.
// Recreating the objects each frame is SLOOOOOOOW
void SetLaneProvidersFromSettings() {
@lanes[0] = getInfoText(LineL1, 0);
@lanes[1] = getInfoText(LineL2, 1);
@lanes[2] = getInfoText(LineL3, 2);
@lanes[3] = getInfoText(LineL4, 3);
@lanes[4] = getInfoText(LineR1, 4);
@lanes[5] = getInfoText(LineR2, 5);
@lanes[6] = getInfoText(LineR3, 6);
@lanes[7] = getInfoText(LineR4, 7);
}
void step() {
if (!diegeticEnabled) return;
// this is kinda dumb but whatever
SetLaneProvidersFromSettings();
return;
}
LaneConfig defaults();
LaneConfig@ getInfoText(const string &in type, uint slot) {
defaults.color = getDefaultLaneColor(slot);
if (renderDemo) {
DemoProvider dp;
LaneConfig lc = dp.getLaneConfig(defaults);
lc.content = Text::Format("%d", slot) + " " + lc.content;
return lc;
}
try {
if (hasProvider(type)) {
LaneProvider@ lp = getProvider(type);
return lp.getLaneConfig(defaults);
}
} catch {
warn("error requesting string for slot " + Text::Format("%d", slot));
}
NullProvider np;
return np.getLaneConfig(defaults);
}
vec4 getDefaultLaneColor(uint slot) {
vec4 customSlotColor;
if (slot == 0) customSlotColor = CustomColorLineL1;
if (slot == 1) customSlotColor = CustomColorLineL2;
if (slot == 2) customSlotColor = CustomColorLineL3;
if (slot == 3) customSlotColor = CustomColorLineL4;
if (slot == 4) customSlotColor = CustomColorLineR1;
if (slot == 5) customSlotColor = CustomColorLineR2;
if (slot == 6) customSlotColor = CustomColorLineR3;
if (slot == 7) customSlotColor = CustomColorLineR4;
if (customSlotColor.w == 0.0) return diegeticColor;
return customSlotColor;
}
// from checkpoint counter
uint getTotLaps() {
CSmArenaClient@ playground = cast<CSmArenaClient>(GetApp().CurrentPlayground);
return playground.Map.TMObjective_IsLapRace ? playground.Map.TMObjective_NbLaps : 1;
}
// from checkpoint counter
uint getTotCPs() {
CSmArenaClient@ playground = cast<CSmArenaClient>(GetApp().CurrentPlayground);
MwFastBuffer<CGameScriptMapLandmark@> landmarks = playground.Arena.MapLandmarks;
uint _maxCP = 0;
array<int> links = {};
for(uint i = 0; i < landmarks.Length; i++) {
if(landmarks[i].Waypoint !is null && !landmarks[i].Waypoint.IsFinish && !landmarks[i].Waypoint.IsMultiLap) {
// we have a CP, but we don't know if it is Linked or not
if(landmarks[i].Tag == "Checkpoint") {
_maxCP += 1;
} else if(landmarks[i].Tag == "LinkedCheckpoint") {
if(links.Find(landmarks[i].Order) < 0) {
_maxCP += 1;
links.InsertLast(landmarks[i].Order);
}
} else {
warn("The current map, " + string(playground.Map.MapName) + " (" + playground.Map.IdName + "), is not compliant with checkpoint naming rules."
+ " If the CP count for this map is inaccurate, please report this map on the GitHub issues page:"
+ " https://github.com/Phlarx/tm-checkpoint-counter/issues");
_maxCP += 1;
}
}
}
return _maxCP;
}
dictionary font;
vec3[][] fontGlyphs = array<array<vec3>>(256);
const vec3[]@ GetFontGlyph(uint8 char) {
auto ret = fontGlyphs[char];
if (char == 0x20 || ret.Length > 0) return ret;
throw('No glyph for char: ' + char);
return {};
}
void loadFont() {
Json::Value json;
switch (diegeticFont) {
case Fonts::Custom:
json = Json::FromFile(IO::FromStorageFolder("font.custom.json"));
break;
case Fonts::SevenSegment:
json = Json::FromFile("font.7seg.json");
break;
case Fonts::SixteenSegment:
json = Json::FromFile("font.16seg.json");
break;
case Fonts::NixieReduced:
json = Json::FromFile("font.shittyNixie.json");
break;
case Fonts::Nixie:
default:
json = Json::FromFile("font.nixie.json");
break;
}
for (uint i = 0; i < json.GetKeys().Length; i++) {
string glyph = json.GetKeys()[i];
vec3[] pts;
for (uint v = 0; v < json[glyph].Length; v++) {
pts.InsertLast(vec3(json[glyph][v][0],json[glyph][v][1],json[glyph][v][2]));
}
auto char = glyph[0];
auto gIx = int(char); // - fontCharOffset;
fontGlyphs[gIx].Resize(pts.Length);
for (uint v = 0; v < pts.Length; v++) {
fontGlyphs[gIx][v] = pts[v];
}
font.Set(glyph, pts);
}
}
enum Fonts {
Nixie,
NixieReduced,
SevenSegment,
SixteenSegment,
Custom
}
}