summaryrefslogtreecommitdiff
path: root/src/engine/vehicles/TankerTruck.cpp
blob: 258a28109fc53c45a97a90ec098e2a5f0fd743c7 (plain)
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
374
375
376
#include <libultraship.h>
#include "TankerTruck.h"
#include <vector>
#include "engine/vehicles/Utils.h"
#include "port/Game.h"

extern "C" {
#include "macros.h"
#include "main.h"
#include "defines.h"
#include "code_80005FD0.h"
#include "racing/actors.h"
#include "racing/math_util.h"
#include "sounds.h"
#include "update_objects.h"
#include "render_player.h"
#include "audio/external.h"
extern s8 gPlayerCount;
}

size_t ATankerTruck::_count = 0;
std::map<uint32_t, std::vector<uint32_t>> ATankerTruck::TruckCounts;

ATankerTruck::ATankerTruck(const SpawnParams& params) : AActor(params) {
    Name = "Tanker Truck";
    ResourceName = "mk:tanker_truck";
    BoundingBoxSize = 2.0f;
    TrackPathPoint* temp_v0;
    u16 waypointOffset;
    s32 numWaypoints = gPathCountByPathIndex[0];

    Index = _count;
    PathIndex = params.PathIndex.value_or(0);
    PathPoint = 0;

    ATankerTruck::SpawnMode spawnMode = static_cast<ATankerTruck::SpawnMode>(params.Type.value_or(0));
    switch(spawnMode) {
        case SpawnMode::POINT: // Spawn truck at a specific path point
            PathPoint = params.PathPoint.value_or(0);
            TruckCounts[PathIndex].push_back(PathPoint);
            break;
        case SpawnMode::AUTO: // Automatically distribute trucks based on a specific path point
            printf("vehicle path size %d\n", gVehiclePathSize);
            PathPoint = GetVehiclePathPointDistributed(TruckCounts[PathIndex], gVehiclePathSize);
            TruckCounts[PathIndex].push_back(PathPoint);
            printf("train spawn path point: %d\n", PathPoint);
            break;
    }

    waypointOffset = PathPoint;
    temp_v0 = &gTrackPaths[PathIndex][PathPoint];
    Position[0] = (f32) temp_v0->x;
    Position[1] = (f32) temp_v0->y;
    Position[2] = (f32) temp_v0->z;
    ActorIndex = -1;
    WaypointIndex = waypointOffset;
    Velocity[0] = 0.0f;
    Velocity[1] = 0.0f;
    Velocity[2] = 0.0f;
    if (gModeSelection == TIME_TRIALS) {
        SomeType = ((Index - 1) % 3);
    } else {
        SomeType = random_int(3);
    }
    SomeMultiplierTheSequel = (f32) ((f64) (f32) (SomeType - 1) * 0.6);
    if (((gCCSelection > CC_50) || (gModeSelection == TIME_TRIALS)) && (SomeType == 2)) {
        Speed = params.Speed.value_or(0);
    } else {
        Speed = params.SpeedB.value_or(0);
    }
    Rotation[0] = 0;
    Rotation[2] = 0;
    if (gIsInExtra == 0) {
        Rotation[1] = func_8000D6D0(Position, (s16*) &WaypointIndex, Speed, SomeMultiplierTheSequel, 0, 3);
    } else {
        Rotation[1] = func_8000D940(Position, (s16*) &WaypointIndex, Speed, SomeMultiplierTheSequel, 0);
    }
    sVehicleSoundRenderCounter = 10;

    spawn_vehicle_on_road(Position, Rotation, Velocity, WaypointIndex, SomeMultiplierTheSequel, Speed);
    ActorIndex = add_actor_to_empty_slot(Position, Rotation, Velocity, ACTOR_TANKER_TRUCK);

    _count++;
}

void ATankerTruck::SetSpawnParams(SpawnParams& params) {
    params.Name = ResourceName;
    params.Type = static_cast<uint16_t>(SpawnType);
    params.PathIndex = PathIndex;
    params.PathPoint = PathPoint;
    params.Speed = Speed;
    params.SpeedB = SpeedB;
}

bool ATankerTruck::IsMod() {
    return true;
}

void ATankerTruck::Draw(Camera* camera) {
    s32 var_v0;
    s32 var_s2;
    s32 waypointCount;
    u16 temp_a1;

    waypointCount = gPathCountByPathIndex[0];
    if (!(gPlayers[camera->playerId].speed < 1.6666666666666667)) {
        temp_a1 = WaypointIndex;
        for (var_v0 = 0; var_v0 < 0x18; var_v0 += 3) {
            if (((sSomeNearestPathPoint + var_v0) % waypointCount) == temp_a1) {
                gPlayerTrackPositionFactorInstruction[camera->playerId].target =
                    player_track_position_factor_vehicle(SomeType, gTrackPositionFactor[camera->playerId], temp_a1);
                return;
            }
        }
    }
}

void ATankerTruck::Tick() {
    f32 temp_f0_2;
    f32 temp_f0_3;
    f32 sp5C;
    f32 sp58;
    f32 sp54;
    f32 temp_f2_2;
    s16 var_a1;
    s16 thing;
    Vec3f sp40;
    Vec3f sp34;

    sp5C = Position[0];
    sp58 = Position[1];
    sp54 = Position[2];
    sp40[0] = sp58;
    sp40[1] = 0.0f;
    sp40[2] = 0.0f;
    temp_f0_2 = func_80013C74(SomeType, WaypointIndex);
    if (SomeMultiplierTheSequel < temp_f0_2) {
        SomeMultiplierTheSequel = SomeMultiplierTheSequel + 0.06;
        if (temp_f0_2 < SomeMultiplierTheSequel) {
            SomeMultiplierTheSequel = temp_f0_2;
        }
    }
    if (temp_f0_2 < SomeMultiplierTheSequel) {
        SomeMultiplierTheSequel = SomeMultiplierTheSequel - 0.06;
        if (SomeMultiplierTheSequel < temp_f0_2) {
            SomeMultiplierTheSequel = temp_f0_2;
        }
    }
    if (gIsInExtra == 0) {
        var_a1 = func_8000D6D0(Position, (s16*) &WaypointIndex, Speed, SomeMultiplierTheSequel, 0, 3);
    } else {
        var_a1 = func_8000D940(Position, (s16*) &WaypointIndex, Speed, SomeMultiplierTheSequel, 0);
    }
    adjust_angle(&Rotation[1], var_a1, 100);
    temp_f0_3 = Position[0] - sp5C;
    temp_f2_2 = Position[2] - sp54;
    sp34[0] = Position[1];
    sp34[1] = 0.0f;
    sp34[2] = sqrtf((temp_f0_3 * temp_f0_3) + (temp_f2_2 * temp_f2_2));
    thing = get_angle_between_two_vectors(sp40, sp34);
    adjust_angle(&Rotation[0], -thing, 100);
    Velocity[0] = Position[0] - sp5C;
    Velocity[1] = Position[1] - sp58;
    Velocity[2] = Position[2] - sp54;
    struct Actor* vehicleActor = GET_ACTOR(ActorIndex);
    vehicleActor->pos[0] = Position[0];
    vehicleActor->pos[1] = Position[1];
    vehicleActor->pos[2] = Position[2];
    vehicleActor->rot[0] = Rotation[0];
    if (gIsMirrorMode != 0) {
        vehicleActor->rot[1] = -Rotation[1];
    } else {
        vehicleActor->rot[1] = Rotation[1];
    }
    vehicleActor->rot[2] = Rotation[2];
    vehicleActor->velocity[0] = Velocity[0];
    vehicleActor->velocity[1] = Velocity[1];
    vehicleActor->velocity[2] = Velocity[2];
}

void ATankerTruck::VehicleCollision(s32 playerId, Player* player) {
    f32 temp_f12;
    f32 temp_f14;
    f32 temp_f22;

    f32 spC4;
    f32 spC0;
    f32 spBC;

    if (((D_801631E0[playerId] != 1) || ((((player->type & PLAYER_HUMAN) != 0)) && !(player->type & PLAYER_CPU))) &&
        !(player->effects & 0x01000000)) {

        spC4 = player->pos[0];
        spC0 = player->pos[1];
        spBC = player->pos[2];

        temp_f12 = spC4 - Position[0];
        temp_f22 = spC0 - Position[1];
        temp_f14 = spBC - Position[2];

        if (((temp_f12) > -100.0) && ((temp_f12) < 100.0)) {
            if ((temp_f22 > -20.0) && (temp_f22 < 20.0)) {

                if (((temp_f14) > -100.0) && ((temp_f14) < 100.0)) {
                    if (is_collide_with_vehicle(Position[0], Position[2], Velocity[0], Velocity[2], SomeArg3, SomeArg4,
                                                spC4, spBC) == (s32) 1) {
                        player->triggers |= VERTICAL_TUMBLE_TRIGGER;
                    }
                }
            }
        }
        if ((player->type & PLAYER_HUMAN) && !(player->type & PLAYER_CPU)) {
            if (((temp_f12) > -300.0) && ((temp_f12) < 300.0) && ((temp_f22 > -20.0)) && (temp_f22 < 20.0) &&
                (((temp_f14) > -300.0)) && ((temp_f14) < 300.0)) {
                if ((sVehicleSoundRenderCounter > 0) && (SomeFlags == 0)) {
                    sVehicleSoundRenderCounter -= 1;
                    SomeFlags |= (RENDER_VEHICLE << playerId);
                    func_800C9D80(Position, Velocity, SoundBits);
                }
            } else {
                if (SomeFlags != 0) {
                    SomeFlags &= ~(RENDER_VEHICLE << playerId);
                    if (SomeFlags == 0) {
                        sVehicleSoundRenderCounter += 1;
                        func_800C9EF4(Position, SoundBits);
                    }
                }
            }

            if (((temp_f12) > -200.0) && ((temp_f12) < 200.0) && ((temp_f22 > -20.0)) && (temp_f22 < 20.0) &&
                (((temp_f14) > -200.0)) && ((temp_f14) < 200.0)) {
                if (!(SomeFlagsTheSequel & ((1 << playerId)))) {

                    s32 var_s1 = 0;
                    u16 path = gPathCountByPathIndex[0];
                    s32 t1;
                    s32 t2;

                    switch (gIsInExtra) {
                        case 0:
                            t1 = is_path_point_in_range(WaypointIndex, gNearestPathPointByPlayerId[playerId], 10, 0,
                                                        path);
                            if ((gIsPlayerWrongDirection[playerId] == 0) && (t1 > 0) && (player->speed < Speed)) {
                                var_s1 = 1;
                            }
                            if ((gIsPlayerWrongDirection[playerId] == 1) && (t1 > 0)) {
                                var_s1 = 1;
                            }
                            break;
                        case 1:
                            t2 = is_path_point_in_range(WaypointIndex, gNearestPathPointByPlayerId[playerId], 0, 10,
                                                        path);
                            if (t2 > 0) {
                                if (random_int(2) == 0) {
                                    // temp_v1_2 = gIsPlayerWrongDirection[playerId];
                                    if (gIsPlayerWrongDirection[playerId] == 0) {
                                        var_s1 = 1;
                                    }
                                    if ((gIsPlayerWrongDirection[playerId] == 1) && (player->speed < Speed)) {
                                        var_s1 = 1;
                                    }
                                } else {
                                    SomeFlagsTheSequel |= ((1 << playerId));
                                }
                            }
                            break;
                    }
                    if (var_s1 == 1) {

                        u32 soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3B);

                        switch (SoundBits) {
                            case SOUND_ARG_LOAD(0x51, 0x01, 0x80, 0x05):
                                soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3B);
                                if (random_int(4) == 0) {
                                    soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3C);
                                }
                                break;
                            case SOUND_ARG_LOAD(0x51, 0x01, 0x80, 0x02):
                                if (random_int(2) != 0) {
                                    soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3D);
                                } else {
                                    soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3E);
                                }
                                break;
                            case SOUND_ARG_LOAD(0x51, 0x01, 0x80, 0x03):
                                if (random_int(2) != 0) {
                                    soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3F);
                                } else {
                                    soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x40);
                                }
                                break;
                            case SOUND_ARG_LOAD(0x51, 0x01, 0x80, 0x04):
                                if (random_int(2) != 0) {
                                    soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x41);
                                } else {
                                    soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x42);
                                }
                                break;
                        }
                        SomeFlagsTheSequel |= ((1 << playerId));
                        func_800C98B8(Position, Velocity, soundBits2);
                    }
                }
            } else {
                if (SomeFlagsTheSequel & ((1 << playerId))) {
                    SomeFlagsTheSequel &= ~((1 << playerId));
                }
            }
        }
    }
}

void ATankerTruck::DrawEditorProperties() {
    ImGui::Text("Spawn Mode");
    ImGui::SameLine();

    int32_t type = static_cast<int32_t>(SpawnType);
    const char* items[] = { "POINT", "AUTO" };

    if (ImGui::Combo("##Type", &type, items, IM_ARRAYSIZE(items))) {
        SpawnType = static_cast<ATankerTruck::SpawnMode>(type);
    }

    if (SpawnType == ATankerTruck::SpawnMode::POINT) {
        ImGui::Text("Path Index");
        ImGui::SameLine();

        int pathIndex = static_cast<int>(PathIndex);
        if (ImGui::InputInt("##PathIndex", &pathIndex)) {
            if (pathIndex < 0) pathIndex = 0;
            PathIndex = static_cast<uint32_t>(pathIndex);
        }
        ImGui::SameLine();
        if (ImGui::Button(ICON_FA_UNDO "##ResetPathIndex")) {
            PathIndex = 0;
        }

        ImGui::Text("Path Point");
        ImGui::SameLine();

        int pathPoint = static_cast<int>(PathPoint);
        if (ImGui::InputInt("##PathPoint", &pathPoint)) {
            if (pathPoint < 0) pathPoint = 0;
            PathPoint = static_cast<uint32_t>(pathPoint);
        }
        ImGui::SameLine();
        if (ImGui::Button(ICON_FA_UNDO "##ResetPathPoint")) {
            PathPoint = 0;
        }
    }

    ImGui::Text("Speed");
    ImGui::SameLine();

    float speed = Speed;
    if (ImGui::DragFloat("##Speed", &speed, 0.1f)) {
        Speed = speed;
    }
    ImGui::SameLine();
    if (ImGui::Button(ICON_FA_UNDO "##ResetSpeed")) {
        Speed = 0.0f;
    }

    ImGui::Text("SpeedB");
    ImGui::SameLine();

    float speed2 = SpeedB;
    if (ImGui::DragFloat("##SpeedB", &speed2, 0.1f)) {
        SpeedB = speed2;
    }
    ImGui::SameLine();
    if (ImGui::Button(ICON_FA_UNDO "##ResetSpeedB")) {
        SpeedB = 0.0f;
    }
}