summaryrefslogtreecommitdiff
path: root/src/engine/vehicles/Train.cpp
blob: a4b0e2ce27c11a349af11eb1d9401e89a66b2c4b (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394

#include <libultraship.h>
#include <libultra/gbi.h>
#include "Train.h"
#include <vector>

#include "engine/tracks/Track.h"
#include "engine/vehicles/Utils.h"
#include "engine/World.h"
#include "port/Game.h"

extern "C" {
#include "macros.h"
#include "main.h"
#include "defines.h"
#include "code_80005FD0.h"
#include "vehicles.h"
#include "racing/actors.h"
#include "racing/math_util.h"
#include "sounds.h"
#include "update_objects.h"
#include "waypoints.h"
#include "code_80057C60.h"
#include "math_util_2.h"
#include "render_objects.h"
#include "assets/models/common_data.h"
//  #include "common_structs.h"
}

// The two counts are so we can spawn trains at specific points or use auto distribution
size_t ATrain::_count = 0;
//       pathIndex,     array of spawn points
std::map<uint32_t, std::vector<uint32_t>> ATrain::TrainCounts;

ATrain::ATrain(const SpawnParams& params) : AActor(params) {
    Name = "Train";
    ResourceName = "mk:train";
    BoundingBoxSize = 2.0f;
    TrainCarStuff* ptr1;
    TrackPathPoint* pos;

    Index = _count;

    PassengerCarsCount = params.Count.value_or(0);
    bool tender = params.Bool.value_or(true);

    // The path to spawn the train at
    uint32_t pathIndex = params.PathIndex.value_or(0);
    // The point along the path to spawn the train at
    uint32_t pathPoint = 0;

    SpawnType = static_cast<SpawnMode>(params.Type.value_or(SpawnMode::POINT));

    switch(SpawnType) {
        case SpawnMode::POINT: // Spawn train at a specific path point
            pathPoint = params.PathPoint.value_or(0);
            TrainCounts[pathIndex].push_back(pathPoint);
            break;
        case SpawnMode::AUTO: // Automatically distribute trains based on a specific path point
            printf("vehicle path size %d\n", gVehiclePathSize);
            pathPoint = GetVehiclePathPointDistributed(TrainCounts[pathIndex], gVehiclePathSize);
            TrainCounts[pathIndex].push_back(pathPoint);
            printf("train spawn path point: %d\n", pathPoint);
            break;
    }

    // Set to the default value
    std::fill(SmokeParticles, SmokeParticles + 128, NULL_OBJECT_ID);

    for (size_t i = 0; i < PassengerCarsCount; i++) {
        PassengerCars.push_back(TrainCarStuff());
    }

    // 120.0f is about the maximum usable value
    for (size_t i = 0; i < PassengerCars.size(); i++) {
        pathPoint += 4;
        ptr1 = &PassengerCars[i];
        pos = &gVehicle2DPathPoint[pathPoint];
        set_vehicle_pos_path_point(ptr1, pos, pathPoint);
    }
    // Smaller offset for the tender
    pathPoint += 3;
    pos = &gVehicle2DPathPoint[pathPoint];
    set_vehicle_pos_path_point(&this->Tender, pos, pathPoint);
    pathPoint += 4;
    pos = &gVehicle2DPathPoint[pathPoint];
    set_vehicle_pos_path_point(&Locomotive, pos, pathPoint);

    // Only use locomotive unless overwritten below.
    // Fall back in-case someone tries to spawn a train with carriages but no tender; not allowed.
    if (PassengerCarsCount > 0) {
        tender = HAS_TENDER;
    }

    Tender.isActive = static_cast<bool>(tender);

    for (size_t i = 0; i < PassengerCarsCount; i++) {
        PassengerCars[i].isActive = 1;
    }

    AnotherSmokeTimer = 0;

    TrainCarStuff* tempLocomotive;
    TrainCarStuff* tempTender;
    TrainCarStuff* tempPassengerCar;
    Vec3s trainCarRot;
    s16 trainCarYRot;
    f32 origXPos;
    f32 origZPos;

    tempLocomotive = &Locomotive;
    origXPos = tempLocomotive->position[0];
    origZPos = tempLocomotive->position[2];
    trainCarYRot =
        update_vehicle_following_path(tempLocomotive->position, (s16*) &tempLocomotive->waypointIndex, Speed);
    tempLocomotive->velocity[0] = tempLocomotive->position[0] - origXPos;
    tempLocomotive->velocity[2] = tempLocomotive->position[2] - origZPos;
    vec3s_set(trainCarRot, 0, trainCarYRot, 0);
    tempLocomotive->actorIndex =
        add_actor_to_empty_slot(tempLocomotive->position, trainCarRot, tempLocomotive->velocity, ACTOR_TRAIN_ENGINE);

    tempTender = &Tender;
    if (tempTender->isActive == 1) {
        origXPos = tempTender->position[0];
        origZPos = tempTender->position[2];
        trainCarYRot = update_vehicle_following_path(tempTender->position, (s16*) &tempTender->waypointIndex, Speed);
        tempTender->velocity[0] = tempTender->position[0] - origXPos;
        tempTender->velocity[2] = tempTender->position[2] - origZPos;
        vec3s_set(trainCarRot, 0, trainCarYRot, 0);
        tempTender->actorIndex =
            add_actor_to_empty_slot(tempTender->position, trainCarRot, tempTender->velocity, ACTOR_TRAIN_TENDER);
    }

    for (size_t i = 0; i < PassengerCars.size(); i++) {
        tempPassengerCar = &PassengerCars[i];
        if (tempPassengerCar->isActive == 1) {
            origXPos = tempPassengerCar->position[0];
            origZPos = tempPassengerCar->position[2];
            trainCarYRot = update_vehicle_following_path(tempPassengerCar->position,
                                                         (s16*) &tempPassengerCar->waypointIndex, Speed);
            tempPassengerCar->velocity[0] = tempPassengerCar->position[0] - origXPos;
            tempPassengerCar->velocity[2] = tempPassengerCar->position[2] - origZPos;
            vec3s_set(trainCarRot, 0, trainCarYRot, 0);
            tempPassengerCar->actorIndex = add_actor_to_empty_slot(
                tempPassengerCar->position, trainCarRot, tempPassengerCar->velocity, ACTOR_TRAIN_PASSENGER_CAR);
        }
    }

    _count++;
}

void ATrain::SetSpawnParams(SpawnParams& params) {
    AActor::SetSpawnParams(params);
    params.Name = "mk:train";
    params.Type = static_cast<uint16_t>(SpawnType);
    params.Bool = Tender.isActive;
    params.Speed = Speed;
    params.Count = PassengerCarsCount;
    params.PathIndex = PathIndex;
    params.PathPoint = PathPoint;
}

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

void ATrain::SyncComponents(TrainCarStuff* trainCar, s16 orientationY) {
    struct TrainCar* trainCarActor;

    //! @todo: Change actorIndex to ptr to TrainCar actor
    trainCarActor = (struct TrainCar*) GET_ACTOR(trainCar->actorIndex);
    trainCarActor->pos[0] = trainCar->position[0];
    trainCarActor->pos[1] = trainCar->position[1];
    trainCarActor->pos[2] = trainCar->position[2];
    if (gIsMirrorMode != 0) {
        trainCarActor->rot[1] = -orientationY;
    } else {
        trainCarActor->rot[1] = orientationY;
    }
    trainCarActor->velocity[0] = trainCar->velocity[0];
    trainCarActor->velocity[1] = trainCar->velocity[1];
    trainCarActor->velocity[2] = trainCar->velocity[2];
}

void ATrain::Tick() {
    TrainCarStuff* car;
    u16 oldWaypointIndex;
    s16 orientationYUpdate;
    s32 j;
    Vec3f smokePos;
    FVector temp_f20 = {
        Locomotive.position[0],
        Locomotive.position[1],
        Locomotive.position[2]
    };

    AnotherSmokeTimer += 1;

    oldWaypointIndex = (u16) Locomotive.waypointIndex;

    orientationYUpdate = update_vehicle_following_path(Locomotive.position, (s16*) &Locomotive.waypointIndex, Speed);

    Locomotive.velocity[0] = Locomotive.position[0] - temp_f20.x;
    Locomotive.velocity[1] = Locomotive.position[1] - temp_f20.y;
    Locomotive.velocity[2] = Locomotive.position[2] - temp_f20.z;

    sync_train_components(&Locomotive, orientationYUpdate);

    if ((oldWaypointIndex != Locomotive.waypointIndex) &&
        ((Locomotive.waypointIndex == 0x00BE) || (Locomotive.waypointIndex == 0x0140))) { // play crossing bell sound
        func_800C98B8(Locomotive.position, Locomotive.velocity, SOUND_ARG_LOAD(0x19, 0x01, 0x80, 0x0E));
    } else if (random_int(100) == 0) { // play train whistle sound
        func_800C98B8(Locomotive.position, Locomotive.velocity, SOUND_ARG_LOAD(0x19, 0x01, 0x80, 0x0D));
    }

    SomeFlags = set_vehicle_render_distance_flags(Locomotive.position, TRAIN_SMOKE_RENDER_DISTANCE, SomeFlags);
    // Renders locomotive smoke on all screens if any player is within range.
    if ((((s16) AnotherSmokeTimer % 5) == 0) && (SomeFlags != 0)) {
        smokePos[0] = Locomotive.position[0];
        smokePos[1] = (f32) ((f64) Locomotive.position[1] + 65.0);
        smokePos[2] = (f32) ((f64) Locomotive.position[2] + 25.0);
        adjust_position_by_angle(smokePos, Locomotive.position, orientationYUpdate);
        // spawn_train_smoke(Index, smokePos, 1.1f);
        AddSmoke(Index, smokePos, 1.1f);
    }

    car = &Tender;

    if (car->isActive == 1) {
        temp_f20.x = car->position[0];
        temp_f20.y = car->position[1];
        temp_f20.z = car->position[2];
        orientationYUpdate = update_vehicle_following_path(car->position, (s16*) &car->waypointIndex, Speed);
        car->velocity[0] = car->position[0] - temp_f20.x;
        car->velocity[1] = car->position[1] - temp_f20.y;
        car->velocity[2] = car->position[2] - temp_f20.z;
        sync_train_components(car, orientationYUpdate);
    }

    for (j = 0; j < PassengerCars.size(); j++) {
        car = &PassengerCars[j];
        if (car->isActive == 1) {
            temp_f20.x = car->position[0];
            temp_f20.y = car->position[1];
            temp_f20.z = car->position[2];

            orientationYUpdate = update_vehicle_following_path(car->position, (s16*) &car->waypointIndex, Speed);
            car->velocity[0] = car->position[0] - temp_f20.x;
            car->velocity[1] = car->position[1] - temp_f20.y;
            car->velocity[2] = car->position[2] - temp_f20.z;
            sync_train_components(car, orientationYUpdate);
        }
    }
}

void ATrain::VehicleCollision(s32 playerId, Player* player) {
    TrainCarStuff* trainCar;
    f32 playerPosX;
    f32 playerPosZ;
    f32 x_dist;
    f32 z_dist;
    s32 trainIndex;

    if (D_801631E0[playerId] != 1) {
        if (!(player->effects & 0x01000000)) {
            playerPosX = player->pos[0];
            playerPosZ = player->pos[2];
            trainCar = &Locomotive;
            x_dist = playerPosX - trainCar->position[0];
            z_dist = playerPosZ - trainCar->position[2];
            if ((x_dist > -100.0) && (x_dist < 100.0)) {
                if ((z_dist > -100.0) && (z_dist < 100.0)) {
                    if (is_collide_with_vehicle(trainCar->position[0], trainCar->position[2], trainCar->velocity[0],
                                                trainCar->velocity[2], 60.0f, 20.0f, playerPosX, playerPosZ) == 1) {
                        player->triggers |= VERTICAL_TUMBLE_TRIGGER;
                    }
                    trainCar = &Tender;
                    if (trainCar->isActive == 1) {
                        if (is_collide_with_vehicle(trainCar->position[0], trainCar->position[2], trainCar->velocity[0],
                                                    trainCar->velocity[2], 30.0f, 20.0f, playerPosX, playerPosZ) == 1) {
                            player->triggers |= VERTICAL_TUMBLE_TRIGGER;
                        }
                    }
                }
            }

            for (size_t i = 0; i < PassengerCars.size(); i++) {
                trainCar = &PassengerCars[i];
                x_dist = playerPosX - trainCar->position[0];
                z_dist = playerPosZ - trainCar->position[2];
                if (trainCar->isActive == 1) {
                    if ((x_dist > -100.0) && (x_dist < 100.0)) {
                        if ((z_dist > -100.0) && (z_dist < 100.0)) {
                            if (is_collide_with_vehicle(trainCar->position[0], trainCar->position[2],
                                                        trainCar->velocity[0], trainCar->velocity[2], 30.0f, 20.0f,
                                                        playerPosX, playerPosZ) == 1) {
                                player->triggers |= VERTICAL_TUMBLE_TRIGGER;
                            }
                        }
                    }
                }
            }
        }
    }
}

void ATrain::Draw(Camera* camera) {
}

s32 ATrain::AddSmoke(s32 trainIndex, Vec3f pos, f32 velocity) {
    s32 objectIndex = add_unused_obj_index(SmokeParticles, &NextParticlePtr, gObjectParticle2_SIZE);
    if (objectIndex != NULL_OBJECT_ID) {
        init_train_smoke(objectIndex, pos, velocity);
    }
    return objectIndex;
}

void ATrain::DrawEditorProperties() {
    ImGui::Text("Passenger Cars");
    ImGui::SameLine();

    int count = static_cast<int>(PassengerCarsCount);
    if (ImGui::InputInt("##Count", &count)) {
        // Clamp to uint32_t range (only lower bound needed if assuming positive values)
        if (count < 0) count = 0;
        PassengerCarsCount = static_cast<uint32_t>(count);
    }
    ImGui::SameLine();
    if (ImGui::Button(ICON_FA_UNDO "##ResetCount")) {
        PassengerCarsCount = 0;
    }

    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<ATrain::SpawnMode>(type);
    }

    if (type == ATrain::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("Has Tender");
    ImGui::SameLine();

    bool theBool = HasTender;
    if (ImGui::Checkbox("##Bool", &theBool)) {
        HasTender = static_cast<TenderStatus>(theBool);
    }
    ImGui::SameLine();
    if (ImGui::Button(ICON_FA_UNDO "##ResetBool")) {
        HasTender = TenderStatus::NO_TENDER;
    }

    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;
    }
}