Skip to content

Commit 44362f5

Browse files
committed
Make Constants a class and move in seperate files
Makes init/teardown simpler (and avoids even more global state) Header cleanup
1 parent a1db578 commit 44362f5

20 files changed

Lines changed: 427 additions & 324 deletions

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ add_library(${PROJECT_NAME} OBJECT
170170
src/game_config.h
171171
src/game_config_game.cpp
172172
src/game_config_game.h
173+
src/game_constants.h
174+
src/game_constants.cpp
173175
src/game_destiny.cpp
174176
src/game_destiny.h
175177
src/game_dynrpg.cpp

src/exe_reader.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
*/
1717

1818
// All of this code is unused on EMSCRIPTEN. *Do not use it*!
19+
#include "player.h"
20+
#include <unordered_map>
1921
#ifndef EMSCRIPTEN
2022

2123
#include "exe_reader.h"
2224
#include "image_bmp.h"
2325
#include "output.h"
24-
#include "utils.h"
2526
#include <algorithm>
2627
#include <iostream>
27-
#include <fstream>
2828
#include <zlib.h>
2929

3030
namespace {
@@ -565,13 +565,13 @@ int EXEReader::FileInfo::GetEngineType(int& mp_version) const {
565565
return Player::EngineNone;
566566
}
567567

568-
std::map<Player::GameConstantType, int32_t> EXEReader::GetOverriddenGameConstants() {
569-
std::map<Player::GameConstantType, int32_t> game_constants;
568+
std::unordered_map<Game_Constants::ConstantType, int32_t> EXEReader::GetOverriddenGameConstants() {
569+
std::unordered_map<Game_Constants::ConstantType, int32_t> game_constants;
570570

571-
auto apply_known_config = [&](Player::Constants::KnownPatchConfigurations conf) {
572-
Output::Debug("Assuming known patch config '{}'", Player::Constants::kKnownPatchConfigurations.tag(static_cast<int>(conf)));
573-
auto it_conf = Player::Constants::known_patch_configurations.find(conf);
574-
assert(it_conf != Player::Constants::known_patch_configurations.end());
571+
auto apply_known_config = [&](Game_Constants::KnownPatchConfigurations conf) {
572+
Output::Debug("Assuming known patch config '{}'", Game_Constants::kKnownPatchConfigurations.tag(static_cast<int>(conf)));
573+
auto it_conf = Game_Constants::known_patch_configurations.find(conf);
574+
assert(it_conf != Game_Constants::known_patch_configurations.end());
575575

576576
for (auto it = it_conf->second.begin(); it != it_conf->second.end(); ++it) {
577577
game_constants[it->first] = it->second;
@@ -589,23 +589,23 @@ std::map<Player::GameConstantType, int32_t> EXEReader::GetOverriddenGameConstant
589589
switch (file_info.code_size) {
590590
case 0x9CC00: // RM2K 1.62
591591
if (check_for_string(file_info.code_ofs + 0x07DAA6, "XXX" /* 3x "POP EAX" */)) {
592-
apply_known_config(Player::Constants::KnownPatchConfigurations::StatDelimiter);
592+
apply_known_config(Game_Constants::KnownPatchConfigurations::StatDelimiter);
593593
}
594594
break;
595595
case 0xC8E00: // RM2K3 1.0.8.0
596596
// For all known Italian translations, the "WhiteDragon" patch seems to be the only one
597597
// to translate this string in RPG_RT. So this segment can be used to reliably detect
598598
// the patch without having to read all individual constant values from the EXE
599599
if (check_for_string(file_info.code_ofs + 0x08EBE0, "NoTitolo")) {
600-
apply_known_config(Player::Constants::KnownPatchConfigurations::Rm2k3_Italian_WD_108);
600+
apply_known_config(Game_Constants::KnownPatchConfigurations::Rm2k3_Italian_WD_108);
601601
}
602602
if (check_for_string(file_info.code_ofs + 0x09D279, "XXX" /* 3x "POP EAX" */)) {
603-
apply_known_config(Player::Constants::KnownPatchConfigurations::StatDelimiter);
603+
apply_known_config(Game_Constants::KnownPatchConfigurations::StatDelimiter);
604604
}
605605
break;
606606
case 0xC9000: // RM2K3 1.0.9.1
607607
if (check_for_string(file_info.code_ofs + 0x09C5AD, "XXX" /* 3x "POP EAX" */)) {
608-
apply_known_config(Player::Constants::KnownPatchConfigurations::StatDelimiter);
608+
apply_known_config(Game_Constants::KnownPatchConfigurations::StatDelimiter);
609609
}
610610
break;
611611
}

src/exe_reader.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
#ifndef EP_EXE_READER_H
1919
#define EP_EXE_READER_H
2020

21+
#include "filesystem_stream.h"
22+
#include "game_constants.h"
2123
#include <cstdint>
24+
#include <lcf/enum_tags.h>
2225
#include <string>
2326
#include <vector>
24-
#include "player.h"
2527

2628
/**
2729
* Extracts resources from an EXE.
@@ -71,7 +73,7 @@ class EXEReader {
7173

7274
const FileInfo& GetFileInfo();
7375

74-
std::map<Player::GameConstantType, int32_t> GetOverriddenGameConstants();
76+
std::unordered_map<Game_Constants::ConstantType, int32_t> GetOverriddenGameConstants();
7577

7678
private:
7779
// Bounds-checked unaligned reader primitives.

src/game_actor.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
// Headers
1919
#include <algorithm>
20-
#include <sstream>
2120
#include <iterator>
2221
#include "game_actor.h"
2322
#include "game_battle.h"
23+
#include "game_constants.h"
2424
#include "game_party.h"
2525
#include "sprite_actor.h"
2626
#include "main_data.h"
@@ -37,23 +37,23 @@
3737
#include "game_message_terms.h"
3838

3939
int Game_Actor::MaxHpValue() const {
40-
return Player::Constants::MaxActorHpValue();
40+
return Main_Data::game_constants->MaxActorHpValue();
4141
}
4242

4343
int Game_Actor::MaxSpValue() const {
44-
return Player::Constants::MaxActorSpValue();
44+
return Main_Data::game_constants->MaxActorSpValue();
4545
}
4646

4747
int Game_Actor::MaxStatBattleValue() const {
48-
return Player::Constants::MaxStatBattleValue();
48+
return Main_Data::game_constants->MaxStatBattleValue();
4949
}
5050

5151
int Game_Actor::MaxStatBaseValue() const {
52-
return Player::Constants::MaxStatBaseValue();
52+
return Main_Data::game_constants->MaxStatBaseValue();
5353
}
5454

5555
int Game_Actor::MaxExpValue() const {
56-
return Player::Constants::MaxExpValue();
56+
return Main_Data::game_constants->MaxExpValue();
5757
}
5858

5959
Game_Actor::Game_Actor(int actor_id) {
@@ -722,7 +722,7 @@ int Game_Actor::GetAccessoryId() const {
722722
}
723723

724724
int Game_Actor::GetMaxLevel() const {
725-
return Utils::Clamp<int32_t>(Player::Constants::MaxLevel(), 1, dbActor->final_level);
725+
return Utils::Clamp<int32_t>(Main_Data::game_constants->MaxLevel(), 1, dbActor->final_level);
726726
}
727727

728728
void Game_Actor::SetExp(int _exp) {

src/game_actors.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717

1818
// Headers
1919
#include "system.h"
20+
#include <cassert>
2021
#include <vector>
2122
#include "game_actors.h"
2223
#include "main_data.h"
2324
#include "output.h"
2425

2526
Game_Actors::Game_Actors() {
27+
assert(Main_Data::game_constants != nullptr && "Game Constants must be initialized");
28+
2629
data.reserve(lcf::Data::actors.size());
2730
for (size_t i = 0; i < lcf::Data::actors.size(); i++) {
2831
data.emplace_back(Game_Actor(i + 1));

src/game_battlealgorithm.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,20 @@
1717
*/
1818

1919
#include <cassert>
20-
#include <cmath>
2120
#include <cstdlib>
2221
#include <algorithm>
23-
#include <sstream>
2422
#include "game_actor.h"
2523
#include "game_battle.h"
2624
#include "game_battlealgorithm.h"
2725
#include "game_battler.h"
26+
#include "game_constants.h"
2827
#include "game_enemy.h"
29-
#include "game_enemyparty.h"
3028
#include "game_party.h"
3129
#include "game_party_base.h"
3230
#include "game_switches.h"
3331
#include "game_system.h"
3432
#include "main_data.h"
3533
#include "game_message_terms.h"
36-
#include "output.h"
3734
#include "player.h"
3835
#include <lcf/reader_util.h>
3936
#include <lcf/rpg/animation.h>
@@ -52,7 +49,7 @@
5249
#include "feature.h"
5350

5451
static inline int MaxDamageValue() {
55-
return Player::Constants::MaxDamageValue();
52+
return Main_Data::game_constants->MaxDamageValue();
5653
}
5754

5855
Game_BattleAlgorithm::AlgorithmBase::AlgorithmBase(Type ty, Game_Battler* source, Game_Battler* target) :

src/game_constants.cpp

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* This file is part of EasyRPG Player.
3+
*
4+
* EasyRPG Player is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* EasyRPG Player is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
// Headers
19+
#include "game_constants.h"
20+
#include "game_variables.h"
21+
#include "output.h"
22+
#include "player.h"
23+
24+
std::array<Game_Variables::Var_t, 2> Game_Constants::GetVariableLimits() {
25+
Game_Variables::Var_t min_var = lcf::Data::system.easyrpg_variable_min_value;
26+
TryGetOverriddenConstant(ConstantType::MinVarLimit, min_var);
27+
if (min_var == 0) {
28+
if (!Player::IsPatchManiac() || Player::game_config.patch_maniac.Get() == 2) {
29+
min_var = Player::IsRPG2k3() ? Game_Variables::min_2k3 : Game_Variables::min_2k;
30+
} else {
31+
min_var = std::numeric_limits<Game_Variables::Var_t>::min();
32+
}
33+
}
34+
Game_Variables::Var_t max_var = lcf::Data::system.easyrpg_variable_max_value;
35+
TryGetOverriddenConstant(ConstantType::MaxVarLimit, max_var);
36+
if (max_var == 0) {
37+
if (!Player::IsPatchManiac() || Player::game_config.patch_maniac.Get() == 2) {
38+
max_var = Player::IsRPG2k3() ? Game_Variables::max_2k3 : Game_Variables::max_2k;
39+
} else {
40+
max_var = std::numeric_limits<Game_Variables::Var_t>::max();
41+
}
42+
}
43+
44+
return {min_var, max_var};
45+
}
46+
47+
int32_t Game_Constants::MaxActorHpValue() {
48+
auto val = lcf::Data::system.easyrpg_max_actor_hp;
49+
TryGetOverriddenConstant(ConstantType::MaxActorHP, val);
50+
if (val == -1) {
51+
return Player::IsRPG2k() ? 999 : 9'999;
52+
}
53+
return val;
54+
}
55+
56+
int32_t Game_Constants::MaxActorSpValue() {
57+
auto val = lcf::Data::system.easyrpg_max_actor_sp;
58+
TryGetOverriddenConstant(ConstantType::MaxActorSP, val);
59+
if (val == -1) {
60+
return 999;
61+
}
62+
return val;
63+
}
64+
65+
int32_t Game_Constants::MaxEnemyHpValue() {
66+
auto val = lcf::Data::system.easyrpg_max_enemy_hp;
67+
if (val == -1) {
68+
// Upper bound is an editor limit, not enforced by the engine
69+
return std::numeric_limits<int32_t>::max();
70+
}
71+
return val;
72+
}
73+
74+
int32_t Game_Constants::MaxEnemySpValue() {
75+
auto val = lcf::Data::system.easyrpg_max_enemy_sp;
76+
if (val == -1) {
77+
// Upper bound is an editor limit, not enforced by the engine
78+
return std::numeric_limits<int32_t>::max();
79+
}
80+
return val;
81+
}
82+
83+
int32_t Game_Constants::MaxStatBaseValue() {
84+
auto val = lcf::Data::system.easyrpg_max_stat_base_value;
85+
TryGetOverriddenConstant(ConstantType::MaxStatBaseValue, val);
86+
if (val == -1) {
87+
return 999;
88+
}
89+
return val;
90+
}
91+
92+
int32_t Game_Constants::MaxStatBattleValue() {
93+
auto val = lcf::Data::system.easyrpg_max_stat_battle_value;
94+
TryGetOverriddenConstant(ConstantType::MaxStatBattleValue, val);
95+
if (val == -1) {
96+
return 9'999;
97+
}
98+
return val;
99+
}
100+
101+
int32_t Game_Constants::MaxDamageValue() {
102+
auto val = lcf::Data::system.easyrpg_max_damage;
103+
TryGetOverriddenConstant(ConstantType::MaxDamageValue, val);
104+
if (val == -1) {
105+
return (Player::IsRPG2k() ? 999 : 9'999);
106+
}
107+
return val;
108+
}
109+
110+
int32_t Game_Constants::MaxExpValue() {
111+
auto val = lcf::Data::system.easyrpg_max_exp;
112+
TryGetOverriddenConstant(ConstantType::MaxExpValue, val);
113+
if (val == -1) {
114+
return Player::IsRPG2k() ? 999'999 : 9'999'999;
115+
}
116+
return val;
117+
}
118+
119+
int32_t Game_Constants::MaxLevel() {
120+
auto max_level = Player::IsRPG2k() ? max_level_2k : max_level_2k3;
121+
if (TryGetOverriddenConstant(ConstantType::MaxLevel, max_level)) {
122+
return max_level;
123+
}
124+
if (lcf::Data::system.easyrpg_max_level > -1) {
125+
max_level = lcf::Data::system.easyrpg_max_level;
126+
}
127+
return max_level;
128+
}
129+
130+
int32_t Game_Constants::MaxGoldValue() {
131+
int32_t max_gold = 999'999;
132+
if (TryGetOverriddenConstant(ConstantType::MaxGoldValue, max_gold)) {
133+
return max_gold;
134+
}
135+
return max_gold;
136+
}
137+
138+
int32_t Game_Constants::MaxItemCount() {
139+
int32_t max_item_count = (lcf::Data::system.easyrpg_max_item_count == -1 ? 99 : lcf::Data::system.easyrpg_max_item_count);;
140+
TryGetOverriddenConstant(ConstantType::MaxItemCount, max_item_count);
141+
return max_item_count;
142+
}
143+
144+
int32_t Game_Constants::MaxSaveFiles() {
145+
int32_t max_savefiles = Utils::Clamp<int32_t>(lcf::Data::system.easyrpg_max_savefiles, 3, 99);
146+
TryGetOverriddenConstant(ConstantType::MaxSaveFiles, max_savefiles);
147+
return max_savefiles;
148+
}
149+
150+
bool Game_Constants::TryGetOverriddenConstant(ConstantType const_type, int32_t& out_value) {
151+
auto it = constant_overrides.find(const_type);
152+
if (it != constant_overrides.end()) {
153+
out_value = (*it).second;
154+
}
155+
return it != constant_overrides.end();
156+
}
157+
158+
void Game_Constants::OverrideGameConstant(ConstantType const_type, int32_t value) {
159+
constant_overrides[const_type] = value;
160+
}
161+
162+
void Game_Constants::PrintActiveOverrides() {
163+
std::vector<std::tuple<std::string, int32_t>> overridden_constants;
164+
165+
auto it = kConstantType.begin();
166+
int32_t value;
167+
while (it != kConstantType.end()) {
168+
auto const_type = static_cast<ConstantType>((*it).value);
169+
if (!TryGetOverriddenConstant(const_type, value)) {
170+
++it;
171+
continue;
172+
}
173+
overridden_constants.push_back(std::make_tuple((*it).name, value));
174+
++it;
175+
}
176+
177+
if (!overridden_constants.empty()) {
178+
std::string out = "Overridden Game Constants: ";
179+
bool first = true;
180+
for (const auto& p : overridden_constants) {
181+
if (!first) {
182+
out += ", ";
183+
}
184+
out += fmt::format("{}: {}", std::get<std::string>(p), std::get<int32_t>(p));
185+
first = false;
186+
}
187+
Output::DebugStr(out);
188+
}
189+
}

0 commit comments

Comments
 (0)