Mario Kart 64
Loading...
Searching...
No Matches
Track.h
Go to the documentation of this file.
1#ifndef ENGINE_TRACK_H
2#define ENGINE_TRACK_H
3
4#include <libultraship/libultraship.h>
5#include "CoreMath.h"
6
7#ifdef __cplusplus
9#include <optional>
10#include <nlohmann/json.hpp>
13extern "C" {
14#endif
15
16#include "defines.h"
17#include "camera.h"
18#include "data/some_data.h"
19#include "bomb_kart.h"
20#include "path_spawn_metadata.h"
21#include "waypoints.h"
22#include "sounds.h"
23#include "common_structs.h"
24#include "code_800029B0.h"
25
26#ifdef __cplusplus
27}
28#endif
29
40
41// Extends infinitely in the Y direction
42// If a player is overtop of a water volume then it should use its height
43// Recommend using the new water surface type. This is here to support the stock tracks.
44// Albeit, there's no reason you cannot use this so long as you input a square.
45// How to use: WaterVolumes.push_back({0, -100, 100, -100, 100});
47 float Height; // Y coordinate of the Water level
48 float MinX;
49 float MaxX;
50 float MinZ;
51 float MaxZ;
52};
53
54typedef struct MinimapProps {
55 const char* Texture;
56 int16_t Width;
57 int16_t Height;
58 IVector2D Pos[2]; // Minimap position for players 1 and 2. 3/4 player mode is hard-coded to the center.
59 int32_t PlayerX; // The offset to place the player markers
60 int32_t PlayerY;
61 float PlayerScaleFactor; // Scale factor of the player markers
62 float FinishlineX; // The offset to place the finishline texture on the minimap
64 RGB8 Colour; // Colour of the visible pixels (the track path)
66
67void ResizeMinimap(MinimapProps* minimap);
68void ReverseGfx(Gfx* gfx);
69
70void InvertTriangleWinding(Gfx* gfx);
71void InvertTriangleWindingByName(const char* name);
74
81typedef struct {
82 uint64_t crc;
83 u8 surfaceType; // Determines what kind of surface the player drives on (ex. dirt, asphalt, etc.)
85 u16 flags;
87
88typedef struct Properties {
89 char Name[128];
90 char DebugName[128];
91 char TrackLength[128];
94 const char* AIBehaviour;
97 float NearPersp;
98 float FarPersp;
99 int16_t* AIDistance;
106 TrackPathPoint* PathTable[4]; // Only used for podium ceremony
107 TrackPathPoint* PathTable2[5]; // The fifth entry is for vehicles
108 uint8_t* CloudTexture;
113 float WaterLevel; // Used for effects, and Lakitu pick up height. Not necessarily the visual water model height.
114
115#ifdef __cplusplus
116 nlohmann::json to_json() const {
117 nlohmann::json j;
118 // j["Id"] = Id ? Id : "";
119 j["Name"] = Name ? Name : "";
120 j["DebugName"] = DebugName ? DebugName : "";
121 j["TrackLength"] = TrackLength ? TrackLength : "";
122 //j["AIBehaviour"] = AIBehaviour ? AIBehaviour : "";
123 j["LakituTowType"] = LakituTowType;
124 j["AIMaximumSeparation"] = AIMaximumSeparation;
125 j["AIMinimumSeparation"] = AIMinimumSeparation;
126 j["NearPersp"] = NearPersp;
127 j["FarPersp"] = FarPersp;
128
129 // AIDistance as a JSON array
130 j["AIDistance"] = std::vector<int16_t>(AIDistance, AIDistance + 32); // gAIDistances array size of 32
131
132 j["AISteeringSensitivity"] = AISteeringSensitivity;
133
134 // PathSizes - Assuming TrackPathSizes can be serialized similarly
135 // j["PathSizes"] = PathSizes; // Implement your serialization logic here
136
137 j["CurveTargetSpeed"] = { CurveTargetSpeed[0], CurveTargetSpeed[1], CurveTargetSpeed[2], CurveTargetSpeed[3] };
138 j["NormalTargetSpeed"] = { NormalTargetSpeed[0], NormalTargetSpeed[1], NormalTargetSpeed[2], NormalTargetSpeed[3] };
139 j["D_0D0096B8"] = { D_0D0096B8[0], D_0D0096B8[1], D_0D0096B8[2], D_0D0096B8[3] };
140 j["OffTrackTargetSpeed"] = { OffTrackTargetSpeed[0], OffTrackTargetSpeed[1], OffTrackTargetSpeed[2], OffTrackTargetSpeed[3] };
141
142 // Serialize arrays PathTable and PathTable2 (convert pointers into a JSON array if possible)
143 //j["PathTable"] = {{}};
144 //j["PathTable2"] = {{}};
145 // Populate PathTable and PathTable2
146
147 //j["Clouds"] = Clouds ? nlohmann::json{{"x", Clouds->x, "y", Clouds->y, "z", Clouds->z}} : nullptr;
148 //j["CloudList"] = CloudList ? nlohmann::json{{"x", CloudList->x, "y", CloudList->y, "z", CloudList->z}} : nullptr;
149
150 j["MinimapPosition"] = {Minimap.Pos[0].X, Minimap.Pos[0].Y};
151 j["MinimapPosition2P"] = {Minimap.Pos[1].X, Minimap.Pos[1].Y};
152 j["MinimapPlayerX"] = Minimap.PlayerX;
153 j["MinimapPlayerY"] = Minimap.PlayerY;
154 j["MinimapPlayerScaleFactor"] = Minimap.PlayerScaleFactor;
155 j["MinimapFinishlineX"] = Minimap.FinishlineX;
156 j["MinimapFinishlineY"] = Minimap.FinishlineY;
157 j["MinimapColour"] = {static_cast<int>(Minimap.Colour.r), static_cast<int>(Minimap.Colour.g), static_cast<int>(Minimap.Colour.b)};
158 // SkyboxColors - assuming SkyboxColors can be serialized similarly
159
160 #define TO_INT(value) static_cast<int>(value)
161 j["Skybox"] = {
162 TO_INT(Skybox.TopRight.r), TO_INT(Skybox.TopRight.g), TO_INT(Skybox.TopRight.b),
163 TO_INT(Skybox.BottomRight.r), TO_INT(Skybox.BottomRight.g), TO_INT(Skybox.BottomRight.b),
164 TO_INT(Skybox.BottomLeft.r), TO_INT(Skybox.BottomLeft.g), TO_INT(Skybox.BottomLeft.b),
165 TO_INT(Skybox.TopLeft.r), TO_INT(Skybox.TopLeft.g), TO_INT(Skybox.TopLeft.b),
166 TO_INT(Skybox.FloorTopRight.r), TO_INT(Skybox.FloorTopRight.g), TO_INT(Skybox.FloorTopRight.b),
167 TO_INT(Skybox.FloorBottomRight.r), TO_INT(Skybox.FloorBottomRight.g), TO_INT(Skybox.FloorBottomRight.b),
168 TO_INT(Skybox.FloorBottomLeft.r), TO_INT(Skybox.FloorBottomLeft.g), TO_INT(Skybox.FloorBottomLeft.b),
169 TO_INT(Skybox.FloorTopLeft.r), TO_INT(Skybox.FloorTopLeft.g), TO_INT(Skybox.FloorTopLeft.b)
170 };
171 j["Sequence"] = static_cast<int>(Sequence);
172
173 j["WaterLevel"] = static_cast<float>(WaterLevel);
174 #undef CAST_TO_INT
175
176 return j;
177 }
178
179 // Function to load struct from JSON
180 void from_json(const nlohmann::json& j) {
181 //Id = j.at("Id").get<std::string>().c_str();
182// Name = j.at("Name").get<std::string>().c_str();
183 strncpy(Name, j.at("Name").get<std::string>().c_str(), sizeof(Name) - 1);
184 Name[sizeof(Name) - 1] = '\0'; // Ensure null termination
185
186// DebugName = j.at("DebugName").get<std::string>().c_str();
187 strncpy(DebugName, j.at("DebugName").get<std::string>().c_str(), sizeof(DebugName) - 1);
188 DebugName[sizeof(DebugName) - 1] = '\0'; // Ensure null termination
189
190 // TrackLength = j.at("TrackLength").get<std::string>().c_str();
191 strncpy(TrackLength, j.at("TrackLength").get<std::string>().c_str(), sizeof(TrackLength) - 1);
192 TrackLength[sizeof(TrackLength) - 1] = '\0'; // Ensure null termination
193
194 //AIBehaviour = j.at("AIBehaviour").get<std::string>().c_str();
195 LakituTowType = j.at("LakituTowType").get<int>();
196
197 AIMaximumSeparation = j.at("AIMaximumSeparation").get<float>();
198 AIMinimumSeparation = j.at("AIMinimumSeparation").get<float>();
199 NearPersp = j.at("NearPersp").get<float>();
200 FarPersp = j.at("FarPersp").get<float>();
201
202 const auto temp = j.at("AIDistance").get<std::vector<int16_t>>();
203
204 // Ensure the vector has 32 entries
205 if (temp.size() == 32) {
206 // Copy the data into the existing AIDistances array
207 std::copy(temp.begin(), temp.end(), AIDistance);
208 } else {
209 printf("[Track.h] [from_json()] AIDistance array not size of 32\n");
210 }
211
212 AISteeringSensitivity = j.at("AISteeringSensitivity").get<uint32_t>();
213
214 // Deserialize PathSizes and other custom structs if needed
215
216 CurveTargetSpeed[0] = j.at("CurveTargetSpeed")[0].get<float>();
217 CurveTargetSpeed[1] = j.at("CurveTargetSpeed")[1].get<float>();
218 CurveTargetSpeed[2] = j.at("CurveTargetSpeed")[2].get<float>();
219 CurveTargetSpeed[3] = j.at("CurveTargetSpeed")[3].get<float>();
220
221 NormalTargetSpeed[0] = j.at("NormalTargetSpeed")[0].get<float>();
222 NormalTargetSpeed[1] = j.at("NormalTargetSpeed")[1].get<float>();
223 NormalTargetSpeed[2] = j.at("NormalTargetSpeed")[2].get<float>();
224 NormalTargetSpeed[3] = j.at("NormalTargetSpeed")[3].get<float>();
225
226 D_0D0096B8[0] = j.at("D_0D0096B8")[0].get<float>();
227 D_0D0096B8[1] = j.at("D_0D0096B8")[1].get<float>();
228 D_0D0096B8[2] = j.at("D_0D0096B8")[2].get<float>();
229 D_0D0096B8[3] = j.at("D_0D0096B8")[3].get<float>();
230
231 OffTrackTargetSpeed[0] = j.at("OffTrackTargetSpeed")[0].get<float>();
232 OffTrackTargetSpeed[1] = j.at("OffTrackTargetSpeed")[1].get<float>();
233 OffTrackTargetSpeed[2] = j.at("OffTrackTargetSpeed")[2].get<float>();
234 OffTrackTargetSpeed[3] = j.at("OffTrackTargetSpeed")[3].get<float>();
235
236 // Deserialize arrays PathTable and PathTable2 similarly
237
238 //Clouds = nullptr; // Deserialize if data is present
239 //CloudList = nullptr; // Deserialize if data is present
240 Minimap.Pos[0].X = j.at("MinimapPosition")[0].get<int32_t>();
241 Minimap.Pos[0].Y = j.at("MinimapPosition")[1].get<int32_t>();
242 Minimap.Pos[1].X = j.at("MinimapPosition2P")[0].get<int32_t>();
243 Minimap.Pos[1].Y = j.at("MinimapPosition2P")[1].get<int32_t>();
244 Minimap.PlayerX = j.at("MinimapPlayerX").get<int32_t>();
245 Minimap.PlayerY = j.at("MinimapPlayerY").get<int32_t>();
246 Minimap.PlayerScaleFactor = j.at("MinimapPlayerScaleFactor").get<float>();
247 Minimap.FinishlineX = j.at("MinimapFinishlineX").get<float>();
248 Minimap.FinishlineY = j.at("MinimapFinishlineY").get<float>();
249 Minimap.Colour.r = j.at("MinimapColour")[0].get<uint8_t>();
250 Minimap.Colour.g = j.at("MinimapColour")[1].get<uint8_t>();
251 Minimap.Colour.b = j.at("MinimapColour")[2].get<uint8_t>();
252 //textures = nullptr; // Deserialize textures if present
253
254 Skybox.TopRight.r = j.at("Skybox")[0].get<uint8_t>();
255 Skybox.TopRight.g = j.at("Skybox")[1].get<uint8_t>();
256 Skybox.TopRight.b = j.at("Skybox")[2].get<uint8_t>();
257
258 Skybox.BottomRight.r = j.at("Skybox")[3].get<uint8_t>();
259 Skybox.BottomRight.g = j.at("Skybox")[4].get<uint8_t>();
260 Skybox.BottomRight.b = j.at("Skybox")[5].get<uint8_t>();
261
262 Skybox.BottomLeft.r = j.at("Skybox")[6].get<uint8_t>();
263 Skybox.BottomLeft.g = j.at("Skybox")[7].get<uint8_t>();
264 Skybox.BottomLeft.b = j.at("Skybox")[8].get<uint8_t>();
265
266 Skybox.TopLeft.r = j.at("Skybox")[9].get<uint8_t>();
267 Skybox.TopLeft.g = j.at("Skybox")[10].get<uint8_t>();
268 Skybox.TopLeft.b = j.at("Skybox")[11].get<uint8_t>();
269
270 Skybox.FloorTopRight.r = j.at("Skybox")[12].get<uint8_t>();
271 Skybox.FloorTopRight.g = j.at("Skybox")[13].get<uint8_t>();
272 Skybox.FloorTopRight.b = j.at("Skybox")[14].get<uint8_t>();
273
274 Skybox.FloorBottomRight.r = j.at("Skybox")[15].get<uint8_t>();
275 Skybox.FloorBottomRight.g = j.at("Skybox")[16].get<uint8_t>();
276 Skybox.FloorBottomRight.b = j.at("Skybox")[17].get<uint8_t>();
277
278 Skybox.FloorBottomLeft.r = j.at("Skybox")[18].get<uint8_t>();
279 Skybox.FloorBottomLeft.g = j.at("Skybox")[19].get<uint8_t>();
280 Skybox.FloorBottomLeft.b = j.at("Skybox")[20].get<uint8_t>();
281
282 Skybox.FloorTopLeft.r = j.at("Skybox")[21].get<uint8_t>();
283 Skybox.FloorTopLeft.g = j.at("Skybox")[22].get<uint8_t>();
284 Skybox.FloorTopLeft.b = j.at("Skybox")[23].get<uint8_t>();
285
286 Sequence = static_cast<MusicSeq>(j.at("Sequence").get<int>());
287 WaterLevel = j.at("WaterLevel").get<float>();
288 }
289 void SetText(char* name, const char* title, size_t bufferSize) {
290 // Copy the title into the name buffer, ensuring it's null-terminated and within bounds
291 std::strncpy(name, title, bufferSize - 1);
292 name[bufferSize - 1] = '\0'; // Ensure the string is null-terminated
293 }
294
295 const char* GetName() {
296 return Name;
297 }
298
299 void New() {
300 SetText(Name, "", sizeof(Name));
301 SetText(DebugName, "", sizeof(DebugName));
302 SetText(TrackLength, "", sizeof(TrackLength));
303 }
304#endif
305
307
308#ifdef __cplusplus
309
310
311class World; // <-- Forward declare
312
313class Track {
314
315public:
316 std::string Id;
317 Properties Props;
318
319 // This allows multiple water levels in a map.
320 // Ex. DK Jungle where there's a waterfall and you can drive above and below it.
321 std::vector<WaterVolume> WaterVolumes;
322
323 bool bSpawnFinishline = true;
324 std::optional<FVector> FinishlineSpawnPoint;
325
326 // O2R Loading
327 std::shared_ptr<Ship::Archive> RootArchive;
328 std::string SceneFilePtr;
329 std::string TrackSectionsPtr;
330
331 bool bIsMod = false;
332 std::vector<SpawnParams> SpawnList;
333
334 bool bTourEnabled = false;
335 std::vector<TourCamera::CameraShot> TourShots;
336
337
338 virtual ~Track() = default;
339
340 explicit Track();
341
342 virtual void LoadO2R(std::string trackPath); // Load custom track from o2r
343 virtual void Load(); // Decompress and load stock tracks or from o2r but TrackSectionsPtr must be set.
344 virtual void Load(Vtx* vtx, Gfx *gfx); // Load custom track from code. Load must be overridden and then call to this base class method impl.
345 virtual void UnLoad();
346 virtual void ParseTrackSections(TrackSections* sections, size_t size);
347
352 virtual void BeginPlay();
353 void SpawnActors();
354 virtual void TestPath();
355 virtual void InitClouds();
356 virtual void TickClouds(s32, Camera*);
357 virtual void SomeCollisionThing(Player *player, Vec3f arg1, Vec3f arg2, Vec3f arg3, f32* arg4, f32* arg5, f32* arg6, f32* arg7);
358 virtual void InitTrackObjects();
359 virtual void TickTrackObjects();
360 virtual void DrawTrackObjects(s32 cameraId);
361 virtual void SomeSounds();
362 virtual void CreditsSpawnActors();
363 virtual void WhatDoesThisDo(Player*, int8_t);
364 virtual void WhatDoesThisDoAI(Player*, int8_t);
365 virtual void SetStaffGhost();
366 virtual void Draw(ScreenContext*);
367 virtual void DrawCredits();
368 virtual void Waypoints(Player* player, int8_t playerId);
369 virtual f32 GetWaterLevel(FVector pos, Collision* collision);
370 virtual void ScrollingTextures();
371 // Draw transparent models (water, signs, arrows, etc.)
372 virtual void DrawWater(ScreenContext* screen, uint16_t pathCounter, uint16_t cameraRot,
373 uint16_t playerDirection);
374 virtual void Destroy();
375 virtual bool IsMod();
376
377 private:
378 void Init();
379};
380
381#endif
382
383#endif // ENGINE_TRACK_H
void InvertTriangleWindingByName(const char *name)
Definition Track.cpp:182
void ReverseGfx(Gfx *gfx)
void RestoreTriangleWinding()
Definition Track.cpp:191
struct Properties Properties
void ResizeMinimap(MinimapProps *minimap)
Definition Track.cpp:37
void InvertTriangleWinding(Gfx *gfx)
Definition Track.cpp:178
bool IsTriangleWindingInverted()
Definition Track.cpp:240
Definition World.h:39
f32 Vec3f[3]
Definition common_structs.h:10
f32 Vec4f[4]
Definition common_structs.h:11
#define j
void SpawnActors(std::vector< std::pair< std::string, SpawnParams > > spawnList)
void Destroy()
Definition ImguiUI.cpp:90
@ f32
Definition GenericArray.h:59
struct StarData CloudData
MusicSeq
Definition sounds.h:118
Definition CoreMath.h:115
Definition Track.h:54
IVector2D Pos[2]
Definition Track.h:58
int16_t Width
Definition Track.h:56
RGB8 Colour
Definition Track.h:64
float FinishlineY
Definition Track.h:63
int32_t PlayerY
Definition Track.h:60
const char * Texture
Definition Track.h:55
float PlayerScaleFactor
Definition Track.h:61
float FinishlineX
Definition Track.h:62
int32_t PlayerX
Definition Track.h:59
int16_t Height
Definition Track.h:57
Definition Track.h:88
char Name[128]
Definition Track.h:89
uint8_t * CloudTexture
Definition Track.h:108
TrackPathPoint * PathTable2[5]
Definition Track.h:107
CloudData * Clouds
Definition Track.h:109
Vec4f D_0D0096B8
Definition Track.h:104
Vec4f CurveTargetSpeed
Definition Track.h:102
float AIMaximumSeparation
Definition Track.h:95
float WaterLevel
Definition Track.h:113
int32_t LakituTowType
Definition Track.h:92
Vec4f OffTrackTargetSpeed
Definition Track.h:105
uint32_t AISteeringSensitivity
Definition Track.h:100
TrackPathPoint * PathTable[4]
Definition Track.h:106
const char * AIBehaviour
Definition Track.h:94
TrackPathSizes PathSizes
Definition Track.h:101
float NearPersp
Definition Track.h:97
SkyboxColours Skybox
Definition Track.h:111
float AIMinimumSeparation
Definition Track.h:96
char TrackLength[128]
Definition Track.h:91
enum MusicSeq Sequence
Definition Track.h:112
int16_t * AIDistance
Definition Track.h:99
char DebugName[128]
Definition Track.h:90
CloudData * CloudList
Definition Track.h:110
float FarPersp
Definition Track.h:98
MinimapProps Minimap
Definition Track.h:93
Vec4f NormalTargetSpeed
Definition Track.h:103
Definition common_structs.h:427
Definition Track.h:30
RGB8 BottomLeft
Definition Track.h:33
RGB8 FloorBottomRight
Definition Track.h:36
RGB8 BottomRight
Definition Track.h:32
RGB8 FloorBottomLeft
Definition Track.h:37
RGB8 TopRight
Definition Track.h:31
RGB8 TopLeft
Definition Track.h:34
RGB8 FloorTopRight
Definition Track.h:35
RGB8 FloorTopLeft
Definition Track.h:38
Definition waypoints.h:14
Definition path_spawn_metadata.h:9
Definition Track.h:81
uint64_t crc
Definition Track.h:82
u8 sectionId
Definition Track.h:84
u8 surfaceType
Definition Track.h:83
u16 flags
Definition Track.h:85
Definition Track.h:46
float Height
Definition Track.h:47
float MaxX
Definition Track.h:49
float MinZ
Definition Track.h:50
float MinX
Definition Track.h:48
float MaxZ
Definition Track.h:51