29 #include "eng3d/serializer.hpp"
30 #include "eng3d/utils.hpp"
45 for(
auto& terrain : gs.
world->terrain_types) {
46 if(terrain.is_water_body)
continue;
57 const uint32_t color = std::byteswap<std::uint32_t>((province.
color & 0x00ffffff) << 8);
58 std::string rgo_size_out =
"";
59 for(
size_t i = 0; i < province.
rgo_size.size(); i++) {
60 const auto& commodity = gs.
world->commodities[i];
63 rgo_size_out +=
string_format(
"{\"%s\",%zu},", commodity.ref_name.c_str(), size);
67 const char *terrain_type_ref_name = gs.
world->terrain_types[province.
terrain_type_id].ref_name.c_str();
69 terrain_type_ref_name =
"sea";
71 fprintf(fp,
"province=Province:new{ref_name=\"%s\",name=translate(\"%s\"),color=0x%x,terrain=tt_%s,rgo_size={%s}}\n",
75 terrain_type_ref_name,
76 rgo_size_out.c_str());
77 fprintf(fp,
"province:register()\n");
87 for(
const auto& building_type : gs.
world->building_types) {
88 const auto& building = province.
buildings[building_type];
90 fprintf(fp,
"province:create_building(bt_%s,%i)\n", building_type.ref_name.c_str(), (
int)building.level);
94 for(
const auto& pop : province.
pops)
95 fprintf(fp,
"province:add_pop(pt_%s,%f,%f)\n", gs.
world->pop_types[pop.type_id].ref_name.c_str(), pop.size, pop.literacy);
96 for(
const auto& language : gs.
world->languages)
98 fprintf(fp,
"province:set_language(c_%s,%f)\n", language.ref_name.c_str(), province.
languages[language]);
99 for(
const auto& religion : gs.
world->religions)
101 fprintf(fp,
"province:set_religion(r_%s,%f)\n", religion.ref_name.c_str(), province.
religions[religion]);
103 for(
const auto& nucleus_id : province.
nuclei)
104 fprintf(fp,
"province:add_nucleus(n_%s)\n", gs.
world->nations[nucleus_id].ref_name.c_str());
106 fprintf(fp,
"province:give_to(n_%s)\n", gs.
world->nations[province.
owner_id].ref_name.c_str());
108 fprintf(fp,
"n_%s:set_capital(province)\n", gs.
world->nations[province.
owner_id].ref_name.c_str());
112 fprintf(fp,
"province:add_unit(ut_%s,%zu)\n", gs.
world->unit_types[unit.type_id].ref_name.c_str(), (
size_t)unit.size);
118 std::filesystem::create_directory(
"editor");
119 std::filesystem::create_directory(
"editor/lua");
120 std::filesystem::create_directory(
"editor/lua/entities");
121 std::filesystem::create_directory(
"editor/map");
123 auto fp = std::unique_ptr<FILE, int (*)(FILE*)>(fopen(
"editor/lua/entities/provinces.lua",
"wt"), fclose);
125 for(
const auto& building_type : gs.
world->building_types)
126 fprintf(fp.get(),
"bt_%s=BuildingType:get(\"%s\")\n", building_type.ref_name.c_str(), building_type.ref_name.c_str());
127 for(
const auto& unit_type : gs.
world->unit_types)
128 fprintf(fp.get(),
"ut_%s=UnitType:get(\"%s\")\n", unit_type.ref_name.c_str(), unit_type.ref_name.c_str());
129 for(
const auto& terrain_type : gs.
world->terrain_types)
130 fprintf(fp.get(),
"tt_%s=TerrainType:get(\"%s\")\n", terrain_type.ref_name.c_str(), terrain_type.ref_name.c_str());
131 for(
const auto& pop_type : gs.
world->pop_types)
132 fprintf(fp.get(),
"pt_%s=PopType:get(\"%s\")\n", pop_type.ref_name.c_str(), pop_type.ref_name.c_str());
133 for(
const auto& language : gs.
world->languages)
134 fprintf(fp.get(),
"c_%s=Language:get(\"%s\")\n", language.ref_name.c_str(), language.ref_name.c_str());
135 for(
const auto& religion : gs.
world->religions)
136 fprintf(fp.get(),
"r_%s=Religion:get(\"%s\")\n", religion.ref_name.c_str(), religion.ref_name.c_str());
137 for(
const auto& nation : gs.
world->nations)
138 fprintf(fp.get(),
"n_%s=Nation:get(\"%s\")\n", nation.ref_name.c_str(), nation.ref_name.c_str());
141 for(
auto& province : gs.
world->provinces) {
143 save_province(gs, fp.get(), province);
146 for(
auto& province : gs.
world->provinces) {
148 save_province(gs, fp.get(), province);
154 fp = std::unique_ptr<FILE, int (*)(FILE*)>(fopen(
"editor/lua/entities/terrain_types.lua",
"wt"), fclose);
156 for(
const auto& terrain_type : gs.
world->terrain_types) {
157 const uint32_t color = std::byteswap<std::uint32_t>((terrain_type.color & 0x00ffffff) << 8);
158 fprintf(fp.get(),
"TerrainType:new{ref_name=\"%s\",name=translate(\"%s\"),color=0x%x,is_water_body=%s}:register()\n", terrain_type.ref_name.c_str(), terrain_type.name.c_str(), (
unsigned int)color, terrain_type.is_water_body ?
"true" :
"false");
164 fp = std::unique_ptr<FILE, int (*)(FILE*)>(fopen(
"editor/lua/entities/religions.lua",
"wt"), fclose);
166 for(
const auto& religion : gs.
world->religions) {
167 const uint32_t color = std::byteswap<std::uint32_t>((religion.color & 0x00ffffff) << 8);
168 fprintf(fp.get(),
"Religion:new{ref_name=\"%s\",name=translate(\"%s\"),color=0x%x}:register()\n", religion.ref_name.c_str(), religion.name.c_str(), (
unsigned int)color);
174 fp = std::unique_ptr<FILE, int (*)(FILE*)>(fopen(
"editor/lua/entities/pop_types.lua",
"wt"), fclose);
176 for(
const auto& pop_type : gs.
world->pop_types) {
177 fprintf(fp.get(),
"PopType:new{ ref_name=\"%s\",name=translate(\"%s\"),social_value=%f}\n", pop_type.ref_name.c_str(), pop_type.name.c_str(), pop_type.social_value);
183 fp = std::unique_ptr<FILE, int (*)(FILE*)>(fopen(
"editor/lua/entities/unit_types.lua",
"wt"), fclose);
185 fprintf(fp.get(),
"local v = {}\n");
186 for(
const auto& unit_type : gs.
world->unit_types) {
187 if(unit_type.is_ground ==
true && unit_type.is_naval ==
false) {
188 fprintf(fp.get(),
"v=UnitType:new{ref_name=\"%s\",name=translate(\"%s\"),defense=%f,attack=%f,health=%f,speed=%f}\n", unit_type.ref_name.c_str(), unit_type.name.c_str(), unit_type.defense, unit_type.attack, unit_type.max_health, unit_type.speed);
189 }
else if(unit_type.is_ground ==
false && unit_type.is_naval ==
true) {
190 fprintf(fp.get(),
"v=BoatType:new{ref_name=\"%s\",name=translate(\"%s\"),defense=%f,attack=%f,health=%f,speed=%f}\n", unit_type.ref_name.c_str(), unit_type.name.c_str(), unit_type.defense, unit_type.attack, unit_type.max_health, unit_type.speed);
191 }
else if(unit_type.is_ground ==
true && unit_type.is_naval ==
true) {
192 fprintf(fp.get(),
"v=AirplaneType:new{ref_name=\"%s\",name=translate(\"%s\"),defense=%f,attack=%f,health=%f,speed=%f}\n", unit_type.ref_name.c_str(), unit_type.name.c_str(), unit_type.defense, unit_type.attack, unit_type.max_health, unit_type.speed);
194 fprintf(fp.get(),
"v:register()\n");
195 for(
const auto& [good_id, amount] : unit_type.req_goods)
196 fprintf(fp.get(),
"v:requires_good(Commodity:get(\"%s\"), %f)\n", gs.
world->commodities[good_id].ref_name.c_str(), amount);
202 fp = std::unique_ptr<FILE, int (*)(FILE*)>(fopen(
"editor/lua/entities/good_types.lua",
"wt"), fclose);
203 fprintf(fp.get(),
"-- Generated by editor :)\n");
204 for(
const auto& good_type : gs.
world->commodities)
205 fprintf(fp.get(),
"Commodity:new{ref_name=\"%s\",name=translate(\"%s\")}:register()\n", good_type.ref_name.c_str(), good_type.name.c_str());
208 gs.
ui_ctx.
prompt(
"Save",
"Editor data saved! (check editor folder)");
214 ar.to_file(savefile_path);
229 gs.
world->events.clear();
231 for(
auto& nation : gs.
world->nations)
232 nation.inbox.clear();
std::atomic< bool > paused
A single province, which is used to simulate economy in a "bulk-tiles" way instead of doing economica...
std::vector< float > religions
Percentage of each religion prescence on the pops, from 0 to 1.
std::array< Pop, 7 > pops
std::vector< ProvinceId > neighbour_ids
std::vector< uint32_t > rgo_size
std::vector< NationId > nuclei
std::vector< float > languages
Percentage of each languages from 0 to 1.
bool is_populated() const
TerrainTypeId terrain_type_id
std::vector< Building > buildings
void prompt(const std::string &title, const std::string &text)
Eng3D::Freelist< Unit > units
std::vector< UnitId > get_province_units(ProvinceId province_id) const
std::vector< std::pair< Decision, NationId > > taken_decisions
T::Id get_id(const T &obj) const
Get the id of an object, this is a template for all types except for tiles and locally-stored types (...
void deserialize(Eng3D::Deser::Archive &ar, T &obj)
void serialize(Eng3D::Deser::Archive &ar, const T &obj)
std::string string_format(const std::string_view format, Args &&... args)
String formatter.
void save(GameState &gs, const std::string &savefile_path)
void load(GameState &gs, const std::string &savefile_path)
Base class that serves as archiver, stores (in memory) the data required for serialization/deserializ...
void from_file(const ::std::string &path)
const char * c_str() const
constexpr Id get_id() const
Eng3D::StringRef ref_name