summaryrefslogtreecommitdiff
path: root/src/engine/RaceManager.cpp
blob: 733b252a533094add5970cad90d7fcf6cad5de1d (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
#include "RaceManager.h"

#include "AllActors.h"
#include "World.h"
#include "port/Game.h"
#include "engine/editor/Editor.h"
#include "engine/editor/SceneManager.h"
#include "engine/RandomItemTable.h"

extern "C" {
#include "racing/render_courses.h"
}

RaceManager::RaceManager(World& world) : WorldContext(world) {
}

std::unordered_map<uintptr_t, std::shared_ptr<Vtx>> mirroredVtxCache;

// Populates a collision mesh for mirror mode
extern "C" void add_triangle_to_collision_mesh(Vtx* vtx1, Vtx* vtx2, Vtx* vtx3, Vtx** outVtx1, Vtx** outVtx2, Vtx** outVtx3) {
    if (gIsMirrorMode != 0) {
        auto getOrCreateMirrored = [](Vtx* original) -> Vtx* {
            uintptr_t key = reinterpret_cast<uintptr_t>(original);
            
            auto it = mirroredVtxCache.find(key);
            if (it != mirroredVtxCache.end()) {
                return it->second.get();
            }

            auto newVtx = std::make_shared<Vtx>(*original);
            newVtx->v.ob[0] = -newVtx->v.ob[0];

            mirroredVtxCache[key] = newVtx;

            return newVtx.get();
        };

        Vtx* m1 = getOrCreateMirrored(vtx1);
        Vtx* m2 = getOrCreateMirrored(vtx2);
        Vtx* m3 = getOrCreateMirrored(vtx3);

        // don't invert winding here, already done in the gfx
        *outVtx1 = m1;
        *outVtx2 = m2;
        *outVtx3 = m3;

    } else {
        // Pas de miroir, on passe les originaux
        *outVtx1 = vtx1;
        *outVtx2 = vtx2;
        *outVtx3 = vtx3;
    }
}

void RaceManager::Load() {
    auto* track = GetWorld()->GetTrack();
    if (track) {
        mirroredVtxCache.clear();
        track->Load();
    } else {
        printf("[RaceManager] [Load] Track was nullptr\n");
    }
}

void RaceManager::PreInit() {
    // Ruleset options
    if (CVarGetInteger("gDisableItemboxes", false) == true) {
        gPlaceItemBoxes = false;
    } else {
        gPlaceItemBoxes = true;
    }

    RaceManager::SetItemTables();
}

void RaceManager::BeginPlay() {
    auto track = WorldContext.GetTrack();

    if (track) {
        // Do not spawn finishline in credits or battle mode. And if bSpawnFinishline.
        if ((gGamestate != CREDITS_SEQUENCE) && (gModeSelection != BATTLE)) {
            if (track->bSpawnFinishline) {
                if (track->FinishlineSpawnPoint.has_value()) {
                    AFinishline* finishline = AFinishline::Spawn(track->FinishlineSpawnPoint.value(), IRotator(0, 0, 0));
                    finishline->bIsFinishline = true;
                } else {
                    AFinishline* finishline = AFinishline::Spawn();
                    finishline->bIsFinishline = true;
                }
            }
        }
        gEditor.AddLight("Sun", nullptr, D_800DC610[1].l->l.dir);

        track->BeginPlay();
    }
}

void RaceManager::PostInit() {
    // Ruleset options
    if (CVarGetInteger("gAllThwompsAreMarty", false) == true) {
        for (auto& object : GetWorld()->Objects) {
            if (OThwomp* thwomp = dynamic_cast<OThwomp*>(object.get())) {
                gObjectList[thwomp->_objectIndex].unk_0D5 = OThwomp::States::JAILED; // Sets all the thwomp behaviour flags to marty
                thwomp->Behaviour = OThwomp::States::JAILED;
            }
        }
    }

    if (CVarGetInteger("gAllBombKartsChase", false) == true) {
        for (auto& object : GetWorld()->Objects) {
            if (OBombKart* kart = dynamic_cast<OBombKart*>(object.get())) {
                kart->Behaviour = OBombKart::States::CHASE;
            }
        }
    }

    if (CVarGetInteger("gGoFish", false) == true) {
        OTrophy::Spawn(FVector(0,0,0), OTrophy::TrophyType::GOLD, OTrophy::Behaviour::GO_FISH);
    }
}

void RaceManager::SetItemTables() {
    std::optional<std::string> humanTableName;
    std::optional<std::string> cpuTableName;

    switch(gModeSelection) {
        case GRAND_PRIX:
            if (CVarGetInteger("gHarderCPU", false) == true) {
                humanTableName = "mk:hard_cpu_grand_prix";
                cpuTableName = "mk:hard_cpu_grand_prix";
            } else { // normal gameplay
                humanTableName = "mk:human_grand_prix";
                cpuTableName = "mk:cpu_grand_prix";
            }
            break;
        case VERSUS:
            switch (gPlayerCountSelection1) {
                case TWO_PLAYERS_SELECTED:
                    humanTableName = "mk:versus_2p";
                    cpuTableName = "mk:versus_2p"; // Required for demo mode to work
                    break;
                case THREE_PLAYERS_SELECTED:
                    humanTableName = "mk:versus_3p";
                    cpuTableName = "mk:versus_3p";
                    break;
                case FOUR_PLAYERS_SELECTED:
                    humanTableName = "mk:versus_4p";
                    cpuTableName = "mk:versus_4p";
                    break;
            }
            break;
        case BATTLE:
            humanTableName = "mk:battle";
            break;
    }
    if (humanTableName.has_value()) {
        mHumanItemTable = gItemTableRegistry.Get(humanTableName.value());
    } else {
        mHumanItemTable = nullptr;
    }
    if (cpuTableName.has_value()) {
        mCPUItemTable = gItemTableRegistry.Get(cpuTableName.value());
    } else {
        mCPUItemTable = nullptr;
    }
    printf("[RaceManager] Selected human item probability table %s\n", humanTableName.value_or("none").c_str());
    printf("[RaceManager] Selected cpu item probability table %s\n", cpuTableName.value_or("none").c_str());
}

extern "C" int16_t RaceManager_GetRandomHumanItem(uint32_t rank) {
    auto& raceManager = GetWorld()->GetRaceManager();

    auto* table = raceManager.GetHumanItemTable();
    if (nullptr == table) {
        printf("[RaceManager_GetRandomHumanItem] Item table nullptr, giving player a none item\n");
        return ITEM_NONE;
    }

    return table->Roll(rank);
}

extern "C" int16_t RaceManager_GetRandomCPUItem(uint32_t rank) {
    auto& raceManager = GetWorld()->GetRaceManager();
    auto* table = raceManager.GetCPUItemTable();
    if (nullptr == table) {
        printf("[RaceManager_GetRandomCPUItem] Item table nullptr, giving player a none item\n");
        return ITEM_NONE;
    }

    return table->Roll(rank);
}