Skip to content

Commit 78cb3a4

Browse files
committed
Adds Logic Framework + clangs
1 parent 598e8bc commit 78cb3a4

20 files changed

Lines changed: 346 additions & 205 deletions

src/game/save_file.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@ struct RandoSaveCheck {
2323
bool obtained;
2424
};
2525

26+
typedef enum {
27+
SAVETYPE_VANILLA,
28+
SAVETYPE_RANDO,
29+
} SaveType;
30+
2631
struct RandoSaveData {
27-
bool isRando;
28-
//struct RandoSaveCheck randoSaveChecks[RC_MAX];
32+
bool placeHolder;
33+
// struct RandoSaveCheck randoSaveChecks[RC_MAX];
34+
// u32 randoSaveOptions[RO_MAX];
2935
};
3036

3137
struct ShipSaveData {
38+
SaveType saveType;
3239
struct RandoSaveData randoSaveData;
3340
};
3441

src/port/Rando/ActorBehavior/ActorBehavior.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// - Star will spin on an alternate axis and be uncollectable.
99

1010
void LogOutSpawns(std::string type, int16_t model, int16_t posX, int16_t posY, int16_t posZ) {
11-
if (model != MODEL_STAR && model != MODEL_RED_COIN && model != MODEL_RED_COIN_NO_SHADOW
12-
&& model != MODEL_BLUE_COIN && model != MODEL_BLUE_COIN_NO_SHADOW) {
11+
if (model != MODEL_STAR && model != MODEL_RED_COIN && model != MODEL_RED_COIN_NO_SHADOW &&
12+
model != MODEL_BLUE_COIN && model != MODEL_BLUE_COIN_NO_SHADOW) {
1313
return;
1414
}
1515
std::string locationStr = std::to_string(posX) + ", " + std::to_string(posY) + ", " + std::to_string(posZ);
@@ -20,32 +20,32 @@ void LogOutSpawns(std::string type, int16_t model, int16_t posX, int16_t posY, i
2020
void Rando::ActorBehavior::Init() {
2121
REGISTER_LISTENER(SpawnMacroObject, EVENT_PRIORITY_NORMAL, [](IEvent* event) {
2222
SpawnMacroObject* ev = (SpawnMacroObject*)event;
23-
if (!IS_RANDO) {
24-
return;
25-
}
26-
int16_t model = *(ev->model);
27-
28-
LogOutSpawns("Macro", model, ev->posX, ev->posY, ev->posZ);
23+
if (!IS_RANDO(selectedFileNum)) {
24+
return;
25+
}
26+
int16_t model = *(ev->model);
2927

30-
// RandoCheckId randoCheckId = Rando::StaticData::GetCheckByLocation(ev->posX, ev->posY, ev->posZ);
31-
// if (randoCheckId != RC_UNKNOWN) {
28+
LogOutSpawns("Macro", model, ev->posX, ev->posY, ev->posZ);
29+
30+
// RandoCheckId randoCheckId = Rando::StaticData::GetCheckByLocation(ev->posX, ev->posY, ev->posZ);
31+
// if (randoCheckId != RC_UNKNOWN) {
3232
// RandoItemId randoItemId = Rando::StaticData::GetShuffledRandoItem(randoCheckId);
33-
// if (randoItemId != RI_UNKNOWN) {
33+
// if (randoItemId != RI_UNKNOWN) {
3434
// int16_t modelId = Rando::StaticData::GetModelByRandoItem(randoItemId);
3535
// *(ev->model) = modelId;
3636
// *(ev->behavior) = Rando::StaticData::GetBehaviorByModel(modelId);
37-
// }
38-
// }
39-
});
37+
// }
38+
// }
39+
});
4040

41-
REGISTER_LISTENER(SpawnObject, EVENT_PRIORITY_NORMAL, [](IEvent* event) {
41+
REGISTER_LISTENER(SpawnObject, EVENT_PRIORITY_NORMAL, [](IEvent* event) {
4242
SpawnObject* ev = (SpawnObject*)event;
4343
struct SpawnInfo* info = *(ev->spawnInfo);
4444

4545
uint32_t model = *(ev->model);
46-
46+
4747
LogOutSpawns("Spawn", model, info->startPos[0], info->startPos[1], info->startPos[2]);
48-
48+
4949
// RandoCheckId randoCheckId =
5050
// Rando::StaticData::GetCheckByLocation(info->startPos[0], info->startPos[1], info->startPos[2]);
5151
// if (randoCheckId != RC_UNKNOWN) {
@@ -66,4 +66,3 @@ void Rando::ActorBehavior::Init() {
6666
LogOutSpawns("Star", MODEL_STAR, ev->posX, ev->posY, ev->posZ);
6767
});
6868
}
69-

src/port/Rando/GhostshipMenuRando.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "port/ui/GhostshipMenu.h"
2+
#include "port/Rando/Rando.h"
23

34
namespace GhostshipGui {
45

@@ -17,6 +18,26 @@ void GhostshipMenu::AddMenuRando() {
1718
.CVar(CVAR_RANDOMIZER_SETTING("Enabled"))
1819
.RaceDisable(false)
1920
.Options(CheckboxOptions().Tooltip("Enables the randomizer feature."));
21+
AddWidget(path, "Logic Type", WIDGET_CVAR_COMBOBOX)
22+
.CVar(Rando::StaticData::Options[RO_LOGIC].cvar)
23+
.RaceDisable(false)
24+
.Options(ComboboxOptions()
25+
.Tooltip("Sets the Logic type for the seed.")
26+
.ComboMap(Rando::StaticData::logicOptions)
27+
.DefaultIndex(RO_LOGIC_GLITCHLESS));
28+
29+
path = { "Rando", "Shuffle Options", SECTION_COLUMN_1 };
30+
AddSidebarEntry("Rando", path.sidebarName, 1);
31+
path.column = SECTION_COLUMN_1;
32+
33+
AddWidget(path, "Shuffle Stars", WIDGET_CVAR_CHECKBOX)
34+
.CVar(Rando::StaticData::Options[RO_SHUFFLE_STARS].cvar)
35+
.RaceDisable(false)
36+
.Options(CheckboxOptions().Tooltip("Shuffles Stars into the Item Pool."));
37+
AddWidget(path, "Shuffle Red Coins", WIDGET_CVAR_CHECKBOX)
38+
.CVar(Rando::StaticData::Options[RO_SHUFFLE_COINS_RED].cvar)
39+
.RaceDisable(false)
40+
.Options(CheckboxOptions().Tooltip("Shuffles Red Coins into the Item Pool."));
2041
}
2142

2243
} // namespace GhostshipGui
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "Logic.h"
2+
#include <libultraship/bridge/consolevariablebridge.h>
3+
#include <sstream>
4+
#include <random>
5+
6+
extern "C" {
7+
#include "port/ShipUtils.h"
8+
}
9+
10+
namespace Rando {
11+
12+
namespace Logic {
13+
14+
std::map<RandoCheckId, RandoItemId> shuffledList;
15+
std::vector<RandoCheckId> shuffledChecks;
16+
std::vector<RandoItemId> shuffledItems;
17+
18+
void shuffleRandoItems(std::vector<RandoItemId>& shuffledItems) {
19+
std::random_device rd;
20+
std::mt19937 g(rd());
21+
22+
std::shuffle(shuffledItems.begin(), shuffledItems.end(), g);
23+
}
24+
25+
void GenerateShuffleList() {
26+
Rando::Logic::shuffledList.clear();
27+
28+
for (auto& [randoCheckId, randoStaticItem] : Rando::StaticData::Checks) {
29+
if (randoCheckId == RC_UNKNOWN) {
30+
continue;
31+
}
32+
RandoItemType randoItemType = Rando::StaticData::Items[randoStaticItem.randoItemId].randoItemType;
33+
34+
// TODO: Swap to RANDO_SAVE_OPTIONS once Save File is converted to JSON
35+
if (randoItemType == RITYPE_STAR &&
36+
CVarGetInteger(Rando::StaticData::Options[RO_SHUFFLE_STARS].cvar, 0) == RO_GENERIC_OFF) {
37+
continue;
38+
}
39+
40+
if (randoItemType == RITYPE_COIN_RED &&
41+
CVarGetInteger(Rando::StaticData::Options[RO_SHUFFLE_COINS_RED].cvar, 0) == RO_GENERIC_OFF) {
42+
continue;
43+
}
44+
45+
shuffledChecks.push_back(randoCheckId);
46+
shuffledItems.push_back(randoStaticItem.randoItemId);
47+
}
48+
49+
shuffleRandoItems(shuffledItems);
50+
for (int i = 0; i < shuffledChecks.size(); i++) {
51+
Rando::Logic::shuffledList.insert({ shuffledChecks[i], shuffledItems[i] });
52+
}
53+
}
54+
55+
} // namespace Logic
56+
57+
} // namespace Rando

src/port/Rando/Logic/Logic.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef RANDO_LOGIC_H
2+
#define RANDO_LOGIC_H
3+
4+
#include "port/Rando/Rando.h"
5+
#include "port/ShipUtils.h"
6+
7+
namespace Rando {
8+
9+
namespace Logic {
10+
11+
extern std::map<RandoCheckId, RandoItemId> shuffledList;
12+
extern std::vector<RandoCheckId> shuffledChecks;
13+
extern std::vector<RandoItemId> shuffledItems;
14+
15+
void shuffleRandoItems(std::vector<RandoItemId>& shuffledItems);
16+
void GenerateShuffleList();
17+
18+
// Logic Operators
19+
20+
} // namespace Logic
21+
22+
} // namespace Rando
23+
24+
#endif // RANDO_LOGIC_H

src/port/Rando/Logic/NoLogic.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "Logic.h"
2+
3+
extern "C" {
4+
#include "port/ShipUtils.h"
5+
}
6+
7+
namespace Rando {
8+
9+
namespace Logic {
10+
11+
void ApplyNoLogicToSaveContext(std::vector<RandoCheckId>& checkPool, std::vector<RandoItemId>& itemPool) {
12+
// for (size_t i = 0; i < itemPool.size(); i++) {
13+
// std::swap(itemPool[i], itemPool[Ship_Random(0, itemPool.size() - 1)]);
14+
// }
15+
//
16+
// for (auto& randoCheckId : checkPool) {
17+
// if (randoCheckId == RC_UNKNOWN) {
18+
// continue;
19+
// }
20+
//
21+
// RANDO_SAVE_CHECKS[randoCheckId].shuffled = true;
22+
// RANDO_SAVE_CHECKS[randoCheckId].randoItemId = itemPool.back();
23+
// itemPool.pop_back();
24+
// }
25+
}
26+
27+
} // namespace Logic
28+
29+
} // namespace Rando

src/port/Rando/MiscBehavior/MiscBehavior.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ void Rando::MiscBehavior::Init() {
55
Rando::MiscBehavior::OnFileLoad();
66
Rando::MiscBehavior::OnFileSave();
77
}
8-

src/port/Rando/MiscBehavior/OnFileLoad.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ extern struct SaveBuffer gSaveBuffer;
77
void Rando::MiscBehavior::OnFileLoad() {
88
REGISTER_LISTENER(OnGameFileLoad, EVENT_PRIORITY_NORMAL, [](IEvent* event) {
99
OnGameFileLoad* ev = (OnGameFileLoad*)event;
10-
if (!IS_RANDO) {
10+
SPDLOG_INFO("File Loaded: {}", std::to_string(ev->fileNum));
11+
if (!CVarGetInteger("gRandoSettings.Enabled", 0)) {
1112
return;
1213
}
13-
SPDLOG_INFO("File Loaded");
14-
if (!gSaveBuffer.files[ev->fileNum]->shipSaveData.randoSaveData.isRando) {
15-
gSaveBuffer.files[ev->fileNum]->shipSaveData.randoSaveData.isRando = true;
16-
Rando::StaticData::ShuffleItemList();
14+
if (!IS_RANDO(selectedFileNum)) {
15+
SPDLOG_INFO("isRando = false");
16+
gSaveBuffer.files[ev->fileNum]->shipSaveData.saveType = SAVETYPE_RANDO;
17+
Rando::Logic::GenerateShuffleList();
1718
}
1819

1920
// TODO: Inject Save File with spoiler data
2021
// gSaveBuffer.files[ev->fileNum]->shipSaveData.randoSaveData.isRando = true;
21-
22+
2223
// bcopy(&gSaveBuffer.files[ev->fileNum][0], &gSaveBuffer.files[ev->fileNum][1],
2324
// sizeof(gSaveBuffer.files[ev->fileNum][1]));
24-
25+
2526
// write_eeprom_data(&gSaveBuffer.menuData[ev->fileNum], sizeof(gSaveBuffer.menuData[ev->fileNum]));
26-
2727
});
2828
}

src/port/Rando/MiscBehavior/OnFileSave.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
void Rando::MiscBehavior::OnFileSave() {
44
REGISTER_LISTENER(OnGameFileSave, EVENT_PRIORITY_NORMAL, [](IEvent* event) {
55
OnGameFileSave* ev = (OnGameFileSave*)event;
6-
if (!IS_RANDO) {
6+
if (!IS_RANDO(selectedFileNum)) {
77
return;
88
}
99
SPDLOG_INFO("File Saved");
1010

1111
// TODO: Inject Save File with spoiler data
1212
// gSaveBuffer.files[ev->fileNum]->shipSaveData.randoSaveData.isRando = true;
13-
13+
1414
// bcopy(&gSaveBuffer.files[ev->fileNum][0], &gSaveBuffer.files[ev->fileNum][1],
1515
// sizeof(gSaveBuffer.files[ev->fileNum][1]));
16-
16+
1717
// write_eeprom_data(&gSaveBuffer.menuData[ev->fileNum], sizeof(gSaveBuffer.menuData[ev->fileNum]));
18-
1918
});
2019
}

src/port/Rando/Rando.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
// #include <ship/window/FileDropMgr.h>
1010
#include <ship/Context.h>
1111

12+
int16_t selectedFileNum = 0;
13+
1214
// When a save is loaded, we want to unregister all hooks and re-register them if it's a rando save
1315
// void OnSaveLoadHandler(s16 fileNum) {
1416
// Rando::MiscBehavior::OnFileLoad();
1517
// Rando::ActorBehavior::OnFileLoad();
1618
// Rando::CheckTracker::OnFileLoad();
17-
//
19+
//
1820
// // Re-initalizes enhancements that are effected by the save being rando or not
1921
// ShipInit::Init("IS_RANDO");
2022
// }
@@ -36,6 +38,6 @@ void Rando::Init() {
3638
// return randoCheckId;
3739
// }
3840
// }
39-
//
41+
//
4042
// return RC_UNKNOWN;
4143
// }

0 commit comments

Comments
 (0)