Enhance Flags write support for ldb
Add is2k3 field to flags.csv
Support writing different 2k vs 2k3 size
(example: trooppage condition flags)
Correctly writes empty flags to ldb file
MODIFY
generator/csv/flags.csv
generator/csv/flags.csv
-#Structure,Field
-TroopPageCondition,switch_a
-TroopPageCondition,switch_b
-TroopPageCondition,variable
-TroopPageCondition,turn
-TroopPageCondition,fatigue
-TroopPageCondition,enemy_hp
-TroopPageCondition,actor_hp
-TroopPageCondition,turn_enemy
-TroopPageCondition,turn_actor
-TroopPageCondition,command_actor
-Terrain,back_party
-Terrain,back_enemies
-Terrain,lateral_party
-Terrain,lateral_enemies
-EventPageCondition,switch_a
-EventPageCondition,switch_b
-EventPageCondition,variable
-EventPageCondition,item
-EventPageCondition,actor
-EventPageCondition,timer
-EventPageCondition,timer2
-SavePicture,erase_on_map_change
-SavePicture,erase_on_battle_end
-SavePicture,unused_bit
-SavePicture,unused_bit2
-SavePicture,affected_by_tint
-SavePicture,affected_by_flash
-SavePicture,affected_by_shake
+#Structure,Field,Is2k3
+TroopPageCondition,switch_a,0
+TroopPageCondition,switch_b,0
+TroopPageCondition,variable,0
+TroopPageCondition,turn,0
+TroopPageCondition,fatigue,0
+TroopPageCondition,enemy_hp,0
+TroopPageCondition,actor_hp,0
+TroopPageCondition,turn_enemy,1
+TroopPageCondition,turn_actor,1
+TroopPageCondition,command_actor,1
+Terrain,back_party,1
+Terrain,back_enemies,1
+Terrain,lateral_party,1
+Terrain,lateral_enemies,1
+EventPageCondition,switch_a,0
+EventPageCondition,switch_b,0
+EventPageCondition,variable,0
+EventPageCondition,item,0
+EventPageCondition,actor,0
+EventPageCondition,timer,0
+EventPageCondition,timer2,1
+SavePicture,erase_on_map_change,1
+SavePicture,erase_on_battle_end,1
+SavePicture,unused_bit,1
+SavePicture,unused_bit2,1
+SavePicture,affected_by_tint,1
+SavePicture,affected_by_flash,1
+SavePicture,affected_by_shake,1

MODIFY
generator/generate.py
generator/generate.py
@@ -232,7 +232,7 @@ def get_enums(filename='enums.csv'):
return new_result
def get_flags(filename='flags.csv'):
- return process_file(filename, namedtuple("Flag", "field"))
+ return process_file(filename, namedtuple("Flag", "field is2k3"))
def get_setup(filename='setup.csv'):
return process_file(filename, namedtuple("Setup", "method headers"))

MODIFY
generator/templates/flag_reader.tmpl
generator/templates/flag_reader.tmpl
@@ -17,7 +17,7 @@ char const* const Flags<flags_type>::name("{{ struct_name }}_Flags");
template <>
const Flags<flags_type>::Flag* Flags<flags_type>::flags[] = {
{%- for flag in flags[struct_name] %}
- new Flags<flags_type>::Flag(&flags_type::{{ flag.field }}, "{{ flag.field }}"),
+ new Flags<flags_type>::Flag(&flags_type::{{ flag.field }}, "{{ flag.field }}", {{ flag.is2k3 }}),
{%- if loop.last %}
NULL
{%- endif %}

MODIFY
src/generated/ldb_terrain_flags.cpp
src/generated/ldb_terrain_flags.cpp
Binary files a/src/generated/ldb_terrain_flags.cpp and b/src/generated/ldb_terrain_flags.cpp differ

MODIFY
src/generated/ldb_trooppagecondition_flags.cpp
src/generated/ldb_trooppagecondition_flags.cpp
Binary files a/src/generated/ldb_trooppagecondition_flags.cpp and b/src/generated/ldb_trooppagecondition_flags.cpp differ

MODIFY
src/generated/lmu_eventpagecondition_flags.cpp
src/generated/lmu_eventpagecondition_flags.cpp
Binary files a/src/generated/lmu_eventpagecondition_flags.cpp and b/src/generated/lmu_eventpagecondition_flags.cpp differ

MODIFY
src/generated/lsd_savepicture_flags.cpp
src/generated/lsd_savepicture_flags.cpp
Binary files a/src/generated/lsd_savepicture_flags.cpp and b/src/generated/lsd_savepicture_flags.cpp differ

MODIFY
src/reader_flags.cpp
src/reader_flags.cpp
#include "rpg_eventpagecondition.h"
#include "rpg_terrain.h"
#include "rpg_savepicture.h"
+#include "data.h"
// Templates
@@ -40,31 +41,45 @@ void Flags<S>::ReadLcf(S& obj, LcfReader& stream, uint32_t length) {
template <class S>
void Flags<S>::WriteLcf(const S& obj, LcfWriter& stream) {
- uint8_t bitflag = 0;
+ const bool is2k3 = (Data::system.ldb_id == 2003);
+ uint8_t buffer[max_size] = {};
+
+ int bitidx = 0;
+ int byteidx = 0;
for (int i = 0; flags[i] != NULL; i++) {
- if (i % 8 == 0) {
- if (i > 0)
- stream.Write(bitflag);
- bitflag = 0;
+ if (!is2k3 && flags[i]->is2k3) {
+ continue;
+ }
+ auto ref = flags[i]->ref;
+ auto& byteflag = buffer[byteidx];
+ auto value = obj.*ref;
+ byteflag |= (value << bitidx);
+
+ ++bitidx;
+ if (bitidx == 8) {
+ stream.Write(byteflag);
+ ++byteidx;
+ bitidx = 0;
}
- bool S::*ref = flags[i]->ref;
- if (obj.*ref)
- bitflag |= (1 << (i % 8));
}
- if (bitflag != 0 || max_size == 1)
- stream.Write(bitflag);
+ if (bitidx != 0) {
+ stream.Write(buffer[max_size-1]);
+ }
}
template <class S>
int Flags<S>::LcfSize(const S& obj, LcfWriter& /* stream */) {
- int result = 0;
+ const bool is2k3 = (Data::system.ldb_id == 2003);
+ int num_bits = 0;
for (int i = 0; flags[i] != NULL; i++) {
- bool S::*ref = flags[i]->ref;
- if (obj.*ref)
- result = i / 8;
+ if (!is2k3 && flags[i]->is2k3) {
+ continue;
+ }
+ ++num_bits;
}
- return result + 1;
+ auto num_bytes = (num_bits - 1) / 8 + 1;
+ return num_bytes;
}
template <class S>

MODIFY
src/reader_struct.h
src/reader_struct.h
@@ -595,9 +595,10 @@ template <class S>
class Flags {
public:
struct Flag {
- Flag(bool S::*ref, const char* const name) : ref(ref), name(name) {}
+ Flag(bool S::*ref, const char* const name, bool is2k2) : ref(ref), name(name), is2k3(is2k3) {}
bool S::*ref;
const char* const name;
+ bool is2k3;
};
private:

Enhance Flags write support for ldb

- Add is2k3 field to flags.csv
- Support writing different 2k vs 2k3 size
    (example: trooppage condition flags)
- Correctly writes empty flags to ldb file