Skip to content

Commit b09c8f9

Browse files
committed
create cheat manager
1 parent 6ef0826 commit b09c8f9

File tree

7 files changed

+137
-1
lines changed

7 files changed

+137
-1
lines changed

include/cheats.inl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// To add a new cheat define it here and add an implementation in the update function
2+
3+
DEFINE_CHEAT(Health, "Health")
4+
DEFINE_CHEAT(Rupees, "Rupees")
5+
DEFINE_CHEAT(Arrows, "Arrows")
6+
DEFINE_CHEAT(Bombs, "Bombs")
7+
DEFINE_CHEAT(Postcards, "Postcards")

include/gz_cheats.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include "gz_menu.hpp"
4+
5+
#include <types.h>
6+
7+
#define DEFINE_CHEAT(enum, _) GZCheat_##enum,
8+
typedef u32 GZCheat;
9+
enum GZCheat_ {
10+
#include "cheats.inl"
11+
GZCheat_Max
12+
};
13+
#undef DEFINE_CHEAT
14+
15+
class GZCheatManager {
16+
private:
17+
GZMenu mMenu;
18+
u32* mpCheatBitfield;
19+
20+
public:
21+
GZMenu* GetMenu() { return &this->mMenu; }
22+
23+
GZCheatManager();
24+
void SetCheatBitfieldPtr(u32* pBitfield);
25+
bool Check(GZCheat eCheat);
26+
void Toggle(GZCheat eCheat);
27+
void Update();
28+
};
29+
30+
extern GZCheatManager gCheatManager;

include/gz_settings.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef struct GZProfile {
3030
s16 mPositionIndex;
3131
Vec3p mLandPosSlots[MAX_POS_SLOTS];
3232
Vec3p mTrainPosSlots[MAX_POS_SLOTS];
33+
u32 mCheatBitfield[4]; // way more than enough
3334
} GZProfile;
3435

3536
// profiles first because they might take a lot of space

src/gz.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "gz.hpp"
22
#include "build.hpp"
3+
#include "gz_cheats.hpp"
34
#include "gz_commands.hpp"
45
#include "gz_settings.hpp"
56

@@ -18,4 +19,5 @@ void GZ::OnGameModeInit() {}
1819
void GZ::OnGameModeUpdate() {
1920
gCommandManager.Update();
2021
gSettings.Update();
22+
gCheatManager.Update();
2123
}

src/gz_cheats.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "gz_cheats.hpp"
2+
#include "gz_settings.hpp"
3+
#include "mem.hpp"
4+
5+
#include <Unknown/UnkStruct_027e0ce0.hpp>
6+
#include <Unknown/UnkStruct_ov024_020d86b0.hpp>
7+
8+
static bool CheckCheat(int itemIndex) {
9+
if (itemIndex < GZCheat_Max) {
10+
return gCheatManager.Check(itemIndex);
11+
}
12+
13+
return false;
14+
}
15+
16+
static void ToggleCheat(u32 params) {
17+
if (params < GZCheat_Max) {
18+
gCheatManager.Toggle(params);
19+
}
20+
}
21+
22+
#define DEFINE_CHEAT(enum, name) {name, GZMenuItemType_Bool, CheckCheat, ToggleCheat, GZCheat_##enum, NULL, 0},
23+
static GZMenuItem sCheatMenuItems[GZCheat_Max] = {
24+
#include "cheats.inl"
25+
};
26+
#undef DEFINE_CHEAT
27+
28+
GZCheatManager gCheatManager;
29+
30+
GZCheatManager::GZCheatManager() {
31+
this->mMenu.title = "Cheats";
32+
this->mMenu.parent = gMenuManager.GetMainMenu();
33+
this->mMenu.entries = sCheatMenuItems;
34+
this->mMenu.mCount = ARRAY_LEN(sCheatMenuItems);
35+
this->mMenu.needSaveFile = true;
36+
gCheatManager.SetCheatBitfieldPtr(gSettings.mProfiles[gSettings.mProfileHeader.curProfileIndex].mCheatBitfield);
37+
}
38+
39+
void GZCheatManager::SetCheatBitfieldPtr(u32* pBitfield) { this->mpCheatBitfield = pBitfield; }
40+
41+
bool GZCheatManager::Check(GZCheat eCheat) {
42+
return this->mpCheatBitfield != NULL ? this->mpCheatBitfield[0] & (1 << eCheat) : false;
43+
}
44+
45+
void GZCheatManager::Toggle(GZCheat eCheat) {
46+
u32 value = 1 << eCheat;
47+
48+
if (this->mpCheatBitfield == NULL) {
49+
return;
50+
}
51+
52+
if (this->mpCheatBitfield[0] & value) {
53+
this->mpCheatBitfield[0] &= ~value;
54+
} else {
55+
this->mpCheatBitfield[0] |= value;
56+
}
57+
}
58+
59+
void GZCheatManager::Update() {
60+
if (data_027e0ce0 != NULL && data_027e0ce0->mUnk_28 != NULL) {
61+
if (this->Check(GZCheat_Health)) {
62+
//! TODO: find out what updates the hearts visually
63+
data_027e0ce0->mHealth = data_027e0ce0->mHealthMax;
64+
}
65+
66+
if (this->Check(GZCheat_Rupees)) {
67+
data_027e0ce0->mUnk_28->mNumRupees = 9999;
68+
}
69+
70+
if (this->Check(GZCheat_Arrows)) {
71+
data_027e0ce0->mUnk_28->mArrowAmount = data_027e0ce0->mUnk_28->func_ov000_020a8728();
72+
}
73+
74+
if (this->Check(GZCheat_Bombs)) {
75+
data_027e0ce0->mUnk_28->mBombAmount = data_027e0ce0->mUnk_28->func_ov000_020a8748();
76+
}
77+
}
78+
79+
if (data_ov024_020d86b0 != NULL) {
80+
if (this->Check(GZCheat_Postcards)) {
81+
data_ov024_020d86b0->mNumPostcards = 127;
82+
}
83+
}
84+
}

src/gz_menu.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "gz_menu.hpp"
22
#include "build.hpp"
33
#include "gz.hpp"
4+
#include "gz_cheats.hpp"
45
#include "gz_commands.hpp"
56
#include "gz_settings.hpp"
67

@@ -16,6 +17,12 @@
1617
/*
1718
Layout:
1819
- Main Menu
20+
- Cheats
21+
- Health
22+
- Rupees
23+
- Arrows
24+
- Bombs
25+
- Postcards
1926
- Inventory
2027
- Whirlwind
2128
- Boomerang
@@ -82,6 +89,7 @@ extern GZMenu sAboutMenu;
8289
// -- main menu items --
8390

8491
static GZMenuItem sMainMenuItems[] = {
92+
{"Cheats", GZMenuItemType_Default, NULL, NULL, 0, gCheatManager.GetMenu(), 0},
8593
{"Inventory", GZMenuItemType_Default, NULL, NULL, 0, &sInventoryMenu, 0},
8694
{"Collection", GZMenuItemType_Default, NULL, NULL, 0, &sCollectionMenu, 0},
8795
{"Commands", GZMenuItemType_Default, NULL, NULL, 0, &gCommandManager.mMenu, 0},
@@ -244,6 +252,8 @@ static void PrevProfile(u32 params) {
244252
} else {
245253
gSettings.mProfileHeader.curProfileIndex = 0;
246254
}
255+
256+
gCheatManager.SetCheatBitfieldPtr(gSettings.mProfiles[gSettings.mProfileHeader.curProfileIndex].mCheatBitfield);
247257
}
248258

249259
static void NextProfile(u32 params) {
@@ -252,6 +262,8 @@ static void NextProfile(u32 params) {
252262
if (gSettings.mProfileHeader.curProfileIndex >= ARRAY_LEN(gSettings.mProfiles)) {
253263
gSettings.mProfileHeader.curProfileIndex = ARRAY_LEN(gSettings.mProfiles) - 1;
254264
}
265+
266+
gCheatManager.SetCheatBitfieldPtr(gSettings.mProfiles[gSettings.mProfileHeader.curProfileIndex].mCheatBitfield);
255267
}
256268

257269
static void LoadDefaultProfile(u32 params) {

0 commit comments

Comments
 (0)