|
|
|
|
@@ -1,4 +1,3 @@
|
|
|
|
|
#include <core/io/config_file.h>
|
|
|
|
|
#include <core/io/json.h>
|
|
|
|
|
#include <core/os/file_access.h>
|
|
|
|
|
#include <core/set.h>
|
|
|
|
|
@@ -30,15 +29,20 @@ void StreamWorld::read_buildings_json(const String &buildings_path)
|
|
|
|
|
while (e) {
|
|
|
|
|
struct building b;
|
|
|
|
|
String key = e->get();
|
|
|
|
|
b.key = key;
|
|
|
|
|
b.id = json[key].get("id");
|
|
|
|
|
if (b.id == "empty") {
|
|
|
|
|
Dictionary entry = json[key];
|
|
|
|
|
String id = entry.get("id", "empty");
|
|
|
|
|
if (id == "empty") {
|
|
|
|
|
e = e->next();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
building::from_dict(&b, json[key], key);
|
|
|
|
|
#if 0
|
|
|
|
|
b.key = key;
|
|
|
|
|
b.id = json[key].get("id");
|
|
|
|
|
String aabb_s = json[key].get("aabb");
|
|
|
|
|
b.xform = from_string<Transform>(key);
|
|
|
|
|
b.aabb = from_string<AABB>(aabb_s);
|
|
|
|
|
#endif
|
|
|
|
|
buildings.push_back(b);
|
|
|
|
|
e = e->next();
|
|
|
|
|
}
|
|
|
|
|
@@ -58,6 +62,111 @@ void StreamWorld::read_buildings_json(const String &buildings_path)
|
|
|
|
|
print_line("entries count: " + itos(buildings.size()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StreamWorld::save_buildings_json(const String &buildings_path)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
String buildings_json = FileAccess::get_file_as_string(buildings_path);
|
|
|
|
|
String backup_path = buildings_path + ".bak";
|
|
|
|
|
String store_path = buildings_path + ".new";
|
|
|
|
|
FileAccess *fa = FileAccess::open(backup_path, FileAccess::WRITE);
|
|
|
|
|
fa->store_string(buildings_json);
|
|
|
|
|
fa->close();
|
|
|
|
|
Dictionary json;
|
|
|
|
|
for (i = 0; i < (int)buildings.size(); i++) {
|
|
|
|
|
String key;
|
|
|
|
|
VariantWriter::write_to_string(buildings[i].xform, key);
|
|
|
|
|
Dictionary dict = buildings[i].to_dict();
|
|
|
|
|
dict["index"] = i;
|
|
|
|
|
json[key] = dict;
|
|
|
|
|
}
|
|
|
|
|
String json_string = JSON::print(json, "\t", false);
|
|
|
|
|
fa = FileAccess::open(store_path, FileAccess::WRITE);
|
|
|
|
|
fa->store_string(json_string);
|
|
|
|
|
fa->close();
|
|
|
|
|
print_line("entries count: " + itos(buildings.size()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StreamWorld::building::from_dict(building *b, const Dictionary &dict,
|
|
|
|
|
const String &key)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
b->key = key;
|
|
|
|
|
b->xform = from_string<Transform>(key);
|
|
|
|
|
b->id = dict.get("id", "empty");
|
|
|
|
|
b->pattern_id = dict.get("pattern_id", 0);
|
|
|
|
|
Array residents = dict.get("residents", Array());
|
|
|
|
|
b->residents.resize(residents.size());
|
|
|
|
|
for (i = 0; i < (int)residents.size(); i++)
|
|
|
|
|
b->residents.write[i] = residents[i];
|
|
|
|
|
Array workers = dict.get("workers", Array());
|
|
|
|
|
b->workers.resize(workers.size());
|
|
|
|
|
for (i = 0; i < (int)workers.size(); i++)
|
|
|
|
|
b->workers.write[i] = workers[i];
|
|
|
|
|
Array guests = dict.get("guests", Array());
|
|
|
|
|
b->guests.resize(guests.size());
|
|
|
|
|
for (i = 0; i < (int)guests.size(); i++)
|
|
|
|
|
b->guests.write[i] = guests[i];
|
|
|
|
|
String doors_s = dict.get("doors", "[ ]");
|
|
|
|
|
Array doors = from_string<Array>(doors_s);
|
|
|
|
|
b->doors.resize(doors.size());
|
|
|
|
|
for (i = 0; i < (int)doors.size(); i++)
|
|
|
|
|
b->doors.write[i] = from_string<Transform>(doors[i]);
|
|
|
|
|
String aabb_s = dict.get("aabb", AABB());
|
|
|
|
|
b->aabb = from_string<AABB>(aabb_s);
|
|
|
|
|
Array worktime = dict.get("worktime", Array());
|
|
|
|
|
if (worktime.size() == 0) {
|
|
|
|
|
worktime.resize(2);
|
|
|
|
|
worktime[0] = 0;
|
|
|
|
|
worktime[1] = 23;
|
|
|
|
|
}
|
|
|
|
|
b->worktime[0] = worktime[0];
|
|
|
|
|
b->worktime[1] = worktime[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T> static inline void v2a(Array &ret, const Vector<T> &data)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
ret.resize(data.size());
|
|
|
|
|
for (i = 0; i < (int)data.size(); i++)
|
|
|
|
|
ret[i] = data[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dictionary StreamWorld::building::to_dict() const
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
Dictionary ret;
|
|
|
|
|
ret["id"] = id;
|
|
|
|
|
ret["pattern_id"] = pattern_id;
|
|
|
|
|
Array r, w, g;
|
|
|
|
|
v2a(r, residents);
|
|
|
|
|
ret["residents"] = r;
|
|
|
|
|
v2a(w, workers);
|
|
|
|
|
ret["workers"] = w;
|
|
|
|
|
v2a(g, guests);
|
|
|
|
|
ret["guests"] = g;
|
|
|
|
|
Array d;
|
|
|
|
|
v2a(d, doors);
|
|
|
|
|
String doors_s;
|
|
|
|
|
Array d2;
|
|
|
|
|
d2.resize(d.size());
|
|
|
|
|
for (i = 0; i < (int)d.size(); i++) {
|
|
|
|
|
String tmp;
|
|
|
|
|
VariantWriter::write_to_string(d[i], tmp);
|
|
|
|
|
d2[i] = tmp;
|
|
|
|
|
}
|
|
|
|
|
VariantWriter::write_to_string(d2, doors_s);
|
|
|
|
|
ret["doors"] = doors_s;
|
|
|
|
|
String aabb_s;
|
|
|
|
|
VariantWriter::write_to_string(aabb, aabb_s);
|
|
|
|
|
ret["aabb"] = aabb_s;
|
|
|
|
|
Array wt;
|
|
|
|
|
wt.resize(2);
|
|
|
|
|
wt[0] = worktime[0];
|
|
|
|
|
wt[1] = worktime[1];
|
|
|
|
|
ret["worktime"] = wt;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StreamWorld::create_tilemap()
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
@@ -342,7 +451,9 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
|
|
|
|
} else if (command == "buildings_undo") {
|
|
|
|
|
/* TODO */
|
|
|
|
|
} else if (command == "buildings_save") {
|
|
|
|
|
/* TODO */
|
|
|
|
|
String buildings_path =
|
|
|
|
|
config.get_value("buildings", "buildings_path");
|
|
|
|
|
save_buildings_json(buildings_path);
|
|
|
|
|
} else
|
|
|
|
|
print_error("No command " + command);
|
|
|
|
|
}
|
|
|
|
|
@@ -424,7 +535,6 @@ StreamWorld::StreamWorld()
|
|
|
|
|
, view_distance(0)
|
|
|
|
|
, initialized(false)
|
|
|
|
|
{
|
|
|
|
|
ConfigFile config;
|
|
|
|
|
Error result = config.load("res://config/stream.conf");
|
|
|
|
|
ERR_FAIL_COND_MSG(result != OK, "Failed to load config");
|
|
|
|
|
Dictionary buildings_data =
|
|
|
|
|
|