1
0
Fork 0

Codechange: rework CHTS-chunk save/load to be more like the others

pull/9342/head
Patric Stout 2021-06-06 14:01:42 +02:00 committed by Patric Stout
parent 8f323855b1
commit 174952440a
2 changed files with 40 additions and 20 deletions

View File

@ -28,12 +28,9 @@ struct Cheats {
Cheat switch_company; ///< change to another company Cheat switch_company; ///< change to another company
Cheat money; ///< get rich or poor Cheat money; ///< get rich or poor
Cheat crossing_tunnels; ///< allow tunnels that cross each other Cheat crossing_tunnels; ///< allow tunnels that cross each other
Cheat dummy1; ///< empty cheat (build while in pause mode)
Cheat no_jetcrash; ///< no jet will crash on small airports anymore Cheat no_jetcrash; ///< no jet will crash on small airports anymore
Cheat dummy2; ///< empty cheat (change the climate of the map)
Cheat change_date; ///< changes date ingame Cheat change_date; ///< changes date ingame
Cheat setup_prod; ///< setup raw-material production in game Cheat setup_prod; ///< setup raw-material production in game
Cheat dummy3; ///< empty cheat (enable running el-engines on normal rail)
Cheat edit_max_hl; ///< edit the maximum heightlevel; this is a cheat because of the fact that it needs to reset NewGRF game state and doing so as a simple configuration breaks the expectation of many Cheat edit_max_hl; ///< edit the maximum heightlevel; this is a cheat because of the fact that it needs to reset NewGRF game state and doing so as a simple configuration breaks the expectation of many
}; };

View File

@ -14,21 +14,38 @@
#include "../safeguards.h" #include "../safeguards.h"
static const SaveLoad _cheats_desc[] = {
SLE_VAR(Cheats, magic_bulldozer.been_used, SLE_BOOL),
SLE_VAR(Cheats, magic_bulldozer.value, SLE_BOOL),
SLE_VAR(Cheats, switch_company.been_used, SLE_BOOL),
SLE_VAR(Cheats, switch_company.value, SLE_BOOL),
SLE_VAR(Cheats, money.been_used, SLE_BOOL),
SLE_VAR(Cheats, money.value, SLE_BOOL),
SLE_VAR(Cheats, crossing_tunnels.been_used, SLE_BOOL),
SLE_VAR(Cheats, crossing_tunnels.value, SLE_BOOL),
SLE_NULL(1),
SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
SLE_VAR(Cheats, no_jetcrash.been_used, SLE_BOOL),
SLE_VAR(Cheats, no_jetcrash.value, SLE_BOOL),
SLE_NULL(1),
SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
SLE_VAR(Cheats, change_date.been_used, SLE_BOOL),
SLE_VAR(Cheats, change_date.value, SLE_BOOL),
SLE_VAR(Cheats, setup_prod.been_used, SLE_BOOL),
SLE_VAR(Cheats, setup_prod.value, SLE_BOOL),
SLE_NULL(1),
SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
SLE_VAR(Cheats, edit_max_hl.been_used, SLE_BOOL),
SLE_VAR(Cheats, edit_max_hl.value, SLE_BOOL),
};
/** /**
* Save the cheat values. * Save the cheat values.
*/ */
static void Save_CHTS() static void Save_CHTS()
{ {
/* Cannot use lengthof because _cheats is of type Cheats, not Cheat */ SlSetLength(std::size(_cheats_desc));
byte count = sizeof(_cheats) / sizeof(Cheat); SlObject(&_cheats, _cheats_desc);
Cheat *cht = (Cheat*) &_cheats;
Cheat *cht_last = &cht[count];
SlSetLength(count * 2);
for (; cht != cht_last; cht++) {
SlWriteByte(cht->been_used);
SlWriteByte(cht->value);
}
} }
/** /**
@ -36,15 +53,21 @@ static void Save_CHTS()
*/ */
static void Load_CHTS() static void Load_CHTS()
{ {
Cheat *cht = (Cheat*)&_cheats; size_t count = SlGetFieldLength();
size_t count = SlGetFieldLength() / 2; std::vector<SaveLoad> slt;
/* Cannot use lengthof because _cheats is of type Cheats, not Cheat */
if (count > sizeof(_cheats) / sizeof(Cheat)) SlErrorCorrupt("Too many cheat values");
for (uint i = 0; i < count; i++) { /* Cheats were added over the years without a savegame bump. They are
cht[i].been_used = (SlReadByte() != 0); * stored as 2 SLE_BOOLs per entry. "count" indicates how many SLE_BOOLs
cht[i].value = (SlReadByte() != 0); * are stored for this savegame. So read only "count" SLE_BOOLs (and in
* result "count / 2" cheats). */
for (auto &sld : _cheats_desc) {
count--;
slt.push_back(sld);
if (count == 0) break;
} }
SlObject(&_cheats, slt);
} }
/** Chunk handlers related to cheats. */ /** Chunk handlers related to cheats. */