Buildings refactoring
This commit is contained in:
@@ -104,7 +104,7 @@ void BuildingsData::cleanup()
|
||||
}
|
||||
}
|
||||
|
||||
int BuildingsData::get_building_by_key(const String &key)
|
||||
int BuildingsData::get_building_by_key(const String &key) const
|
||||
{
|
||||
int i, index = -1;
|
||||
uint64_t key_hash = key.hash64();
|
||||
@@ -393,3 +393,62 @@ func fill_door_locations():
|
||||
e = e->next();
|
||||
}
|
||||
}
|
||||
|
||||
int BuildingsData::get_building_count() const
|
||||
{
|
||||
return buildings.size();
|
||||
}
|
||||
|
||||
const struct BuildingsData::building &
|
||||
BuildingsData::get_building(int index) const
|
||||
{
|
||||
assert(index >= 0 && index < buildings.size());
|
||||
return buildings[index];
|
||||
}
|
||||
|
||||
const struct BuildingsData::building &
|
||||
BuildingsData::get_building(const String &building_key) const
|
||||
{
|
||||
int index = get_building_by_key(building_key);
|
||||
assert(index >= 0 && index < buildings.size());
|
||||
return buildings[index];
|
||||
}
|
||||
|
||||
struct BuildingsData::building &BuildingsData::get_building(int index)
|
||||
{
|
||||
assert(index >= 0 && index < buildings.size());
|
||||
return buildings[index];
|
||||
}
|
||||
|
||||
struct BuildingsData::building *BuildingsData::get_building_ptr(int index)
|
||||
{
|
||||
assert(index >= 0 && index < buildings.size());
|
||||
return &buildings[index];
|
||||
}
|
||||
|
||||
int BuildingsData::create_building(const Dictionary &dict)
|
||||
{
|
||||
struct building b;
|
||||
building::from_dict(&b, dict);
|
||||
return create_building(b);
|
||||
}
|
||||
|
||||
int BuildingsData::create_building(const struct building &building)
|
||||
{
|
||||
int index = buildings.size();
|
||||
buildings.push_back(building);
|
||||
return index;
|
||||
}
|
||||
|
||||
int BuildingsData::create_building(const building *building)
|
||||
{
|
||||
assert(building);
|
||||
return create_building(*building);
|
||||
}
|
||||
|
||||
bool BuildingsData::destroy_building(int index)
|
||||
{
|
||||
assert(index >= 0 && index < buildings.size());
|
||||
buildings.erase(buildings.begin() + index);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -34,7 +34,19 @@ public:
|
||||
HashMap<String, Vector<Transform> > building_doors;
|
||||
HashMap<String, AABB> building_aabbs;
|
||||
/* Data for each building in a world */
|
||||
private:
|
||||
std::vector<struct building> buildings;
|
||||
|
||||
public:
|
||||
int get_building_count() const;
|
||||
const struct building &get_building(int index) const;
|
||||
const struct building &get_building(const String &building_key) const;
|
||||
struct building &get_building(int index);
|
||||
struct building *get_building_ptr(int index);
|
||||
int create_building(const Dictionary &dict);
|
||||
int create_building(const struct building &building);
|
||||
int create_building(const struct building *building);
|
||||
bool destroy_building(int index);
|
||||
struct checkpoint_data {
|
||||
HashMap<String, String> building_data;
|
||||
std::vector<struct building> buildings;
|
||||
@@ -59,6 +71,6 @@ public:
|
||||
int get_closest_building(const Transform &xform);
|
||||
static BuildingsData *get_singleton();
|
||||
static void cleanup();
|
||||
int get_building_by_key(const String &key);
|
||||
int get_building_by_key(const String &key) const;
|
||||
};
|
||||
#endif
|
||||
@@ -5,10 +5,11 @@ def can_build(env, platform):
|
||||
def configure(env):
|
||||
pass
|
||||
|
||||
def get_icons_path():
|
||||
return "icons"
|
||||
|
||||
def get_doc_classes():
|
||||
return []
|
||||
|
||||
|
||||
def get_doc_path():
|
||||
return "doc_classes"
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <scene/main/viewport.h>
|
||||
#include "from_string.h"
|
||||
#include "buildings_data.h"
|
||||
#include "editor_event.h"
|
||||
#include "road_lines_data.h"
|
||||
|
||||
ImmediateGeometry *RoadLinesData::debug_im = nullptr;
|
||||
@@ -533,7 +534,7 @@ void RoadLinesData::line_add_building(const String &line, const String &key,
|
||||
lb.y_rotation = y_rotation;
|
||||
lines[line].buildings.push_back(lb);
|
||||
// TODO: save/load
|
||||
BuildingsData::get_singleton()->buildings[index].line_name = line;
|
||||
BuildingsData::get_singleton()->get_building(index).line_name = line;
|
||||
}
|
||||
|
||||
void RoadLinesData::assign_close_buildings(const String &line)
|
||||
@@ -550,22 +551,24 @@ void RoadLinesData::assign_close_buildings(const String &line)
|
||||
if (lines[line].segments.size() == 0)
|
||||
return;
|
||||
print_line("assign_close_buildings: processing: " +
|
||||
itos(BuildingsData::get_singleton()->buildings.size()) +
|
||||
itos(BuildingsData::get_singleton()->get_building_count()) +
|
||||
" buildings");
|
||||
for (i = 0; i < (int)BuildingsData::get_singleton()->buildings.size();
|
||||
for (i = 0;
|
||||
i < (int)BuildingsData::get_singleton()->get_building_count();
|
||||
i++) {
|
||||
float dst = Math_INF;
|
||||
float result_offset = 0.0f;
|
||||
float side = 0.0f;
|
||||
String building_key;
|
||||
struct BuildingsData::building &data =
|
||||
BuildingsData::get_singleton()->buildings[i];
|
||||
BuildingsData::get_singleton()->get_building(i);
|
||||
// manually placed
|
||||
if (!data.generated)
|
||||
continue;
|
||||
// already assigned to another line
|
||||
const String &building_line =
|
||||
BuildingsData::get_singleton()->buildings[i].line_name;
|
||||
const String &building_line = BuildingsData::get_singleton()
|
||||
->get_building(i)
|
||||
.line_name;
|
||||
if (building_line != line && building_line != "")
|
||||
continue;
|
||||
if (line_has_building(line, data.key))
|
||||
@@ -670,14 +673,91 @@ Vector3 RoadLinesData::get_point_by_offsets(const String &line,
|
||||
}
|
||||
n_offset -= segment->length;
|
||||
}
|
||||
print_line("offset: " + String::num(n_offset));
|
||||
print_verbose("offset: " + String::num(n_offset));
|
||||
ret = lines[line].segments[selected_segment].p1 +
|
||||
lines[line].segments[selected_segment].dir * n_offset +
|
||||
lines[line].segments[selected_segment].tangent * normal_offset;
|
||||
print_line("data: " + (ret.operator String()));
|
||||
print_verbose("data: " + (ret.operator String()));
|
||||
return ret;
|
||||
}
|
||||
|
||||
void RoadLinesData::update_buildings_from_lines()
|
||||
{
|
||||
BuildingsData *bd = BuildingsData::get_singleton();
|
||||
List<String> keys;
|
||||
lines.get_key_list(&keys);
|
||||
List<String>::Element *e = keys.front();
|
||||
while (e) {
|
||||
int i, j;
|
||||
const String &line = e->get();
|
||||
for (i = 0; i < (int)lines[line].buildings.size(); i++) {
|
||||
const uint64_t &building_key_hash =
|
||||
lines[line].buildings[i].building_key_hash;
|
||||
bool found = false;
|
||||
int index = -1;
|
||||
for (j = 0; j < (int)bd->get_building_count(); j++) {
|
||||
if (bd->get_building(j).key_hash ==
|
||||
building_key_hash) {
|
||||
found = true;
|
||||
index = j;
|
||||
break;
|
||||
}
|
||||
struct BuildingsData::building &b =
|
||||
bd->get_building(index);
|
||||
if (found) {
|
||||
Vector3 pt = get_point_by_offsets(
|
||||
line,
|
||||
lines[line]
|
||||
.buildings[i]
|
||||
.line_offset,
|
||||
lines[line]
|
||||
.buildings[i]
|
||||
.normal_offset);
|
||||
Basis basis = Basis().rotated(
|
||||
Vector3(0, 1, 0),
|
||||
lines[line]
|
||||
.buildings[i]
|
||||
.y_rotation);
|
||||
b.xform.origin = pt;
|
||||
b.xform.basis = basis;
|
||||
Array args;
|
||||
args.push_back(index);
|
||||
EditorEvent::get_singleton()->event.emit(
|
||||
"building_updated", args);
|
||||
} else {
|
||||
Vector3 pt = get_point_by_offsets(
|
||||
line,
|
||||
lines[line]
|
||||
.buildings[i]
|
||||
.line_offset,
|
||||
lines[line]
|
||||
.buildings[i]
|
||||
.normal_offset);
|
||||
Basis basis = Basis().rotated(
|
||||
Vector3(0, 1, 0),
|
||||
lines[line]
|
||||
.buildings[i]
|
||||
.y_rotation);
|
||||
struct BuildingsData::building nb;
|
||||
Dictionary data;
|
||||
data["id"] =
|
||||
lines[line].buildings[i].id;
|
||||
data["xform"] = Transform(basis, pt);
|
||||
BuildingsData::building::from_dict(
|
||||
&nb, data);
|
||||
index = bd->get_building_count();
|
||||
bd->create_building(nb);
|
||||
Array args;
|
||||
args.push_back(index);
|
||||
EditorEvent::get_singleton()->event.emit(
|
||||
"building_created", args);
|
||||
}
|
||||
}
|
||||
}
|
||||
e = e->next();
|
||||
}
|
||||
}
|
||||
|
||||
void RoadLinesData::line_building_data::from_dict(line_building_data *b,
|
||||
const Dictionary &from)
|
||||
{
|
||||
|
||||
@@ -15,6 +15,7 @@ protected:
|
||||
|
||||
public:
|
||||
struct line_building_data {
|
||||
String id;
|
||||
String building_key;
|
||||
uint64_t building_key_hash;
|
||||
float line_offset;
|
||||
@@ -66,6 +67,7 @@ public:
|
||||
bool line_has_building(const String &line, const String &building_key);
|
||||
Vector3 get_point_by_offsets(const String &line, float dir_offset,
|
||||
float normal_offset);
|
||||
void update_buildings_from_lines();
|
||||
|
||||
private:
|
||||
void create_segments_from_lines();
|
||||
|
||||
@@ -799,7 +799,7 @@ void RoadLinesEditor::move_cursor_to_closest_building()
|
||||
int index = BuildingsData::get_singleton()->get_closest_building(
|
||||
Transform(Basis(), pt));
|
||||
const Transform &xform =
|
||||
BuildingsData::get_singleton()->buildings[index].xform;
|
||||
BuildingsData::get_singleton()->get_building(index).xform;
|
||||
set_cursor_position(xform.origin);
|
||||
Spatial *cursor = get_as_node<Spatial>(cursor_name);
|
||||
if (!cursor->is_visible())
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
void StreamWorld::create_tilemap()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < (int)data()->buildings.size(); i++) {
|
||||
int tile_x = data()->buildings[i].xform.origin.x / tile_size;
|
||||
int tile_z = data()->buildings[i].xform.origin.z / tile_size;
|
||||
for (i = 0; i < (int)data()->get_building_count(); i++) {
|
||||
int tile_x = data()->get_building(i).xform.origin.x / tile_size;
|
||||
int tile_z = data()->get_building(i).xform.origin.z / tile_size;
|
||||
std::tuple<int, int> key = std::make_tuple(tile_x, tile_z);
|
||||
if (tiles.find(key) != tiles.end())
|
||||
tiles[key].push_back(i);
|
||||
@@ -137,7 +137,7 @@ void StreamWorld::load_tile(int tx, int ty)
|
||||
const std::vector<int> &items = tiles[key];
|
||||
for (i = 0; i < (int)items.size(); i++) {
|
||||
print_line("load item: " + itos(i) + ": " + itos(items[i]) +
|
||||
": " + data()->buildings[items[i]].id);
|
||||
": " + data()->get_building(items[i]).id);
|
||||
load_building(items[i]);
|
||||
}
|
||||
}
|
||||
@@ -149,7 +149,7 @@ void StreamWorld::erase_tile(int tx, int ty)
|
||||
const std::vector<int> &items = tiles[key];
|
||||
for (i = 0; i < (int)items.size(); i++) {
|
||||
print_line("unload item: " + itos(i) + ": " + itos(items[i]) +
|
||||
": " + data()->buildings[items[i]].id);
|
||||
": " + data()->get_building(items[i]).id);
|
||||
unload_building(items[i]);
|
||||
}
|
||||
}
|
||||
@@ -166,7 +166,7 @@ void StreamWorld::unload_building(int id)
|
||||
|
||||
void StreamWorld::request_item(int type, int item)
|
||||
{
|
||||
String id = data()->buildings[item].id;
|
||||
String id = data()->get_building(item).id;
|
||||
if (id == "empty")
|
||||
return;
|
||||
String path = data()->building_data[id];
|
||||
@@ -230,8 +230,8 @@ void StreamWorld::update_items()
|
||||
item_nodes[data()->scenes[*key].buildings[i]] = psc;
|
||||
current_scene->add_child(psp);
|
||||
psp->set_global_transform(
|
||||
data()->buildings[data()->scenes[*key]
|
||||
.buildings[i]]
|
||||
data()->get_building(
|
||||
data()->scenes[*key].buildings[i])
|
||||
.xform);
|
||||
}
|
||||
key = data()->scenes.next(key);
|
||||
@@ -243,11 +243,11 @@ void StreamWorld::remove_building(int index)
|
||||
// TODO: implement
|
||||
int i;
|
||||
unload_building(index);
|
||||
data()->buildings.erase(data()->buildings.begin() + index);
|
||||
for (i = index; i < (int)data()->buildings.size(); i++) {
|
||||
data()->destroy_building(index);
|
||||
for (i = index; i < (int)data()->get_building_count(); i++) {
|
||||
item_nodes[i] = item_nodes[i + 1];
|
||||
}
|
||||
item_nodes.erase(data()->buildings.size());
|
||||
item_nodes.erase(data()->get_building_count());
|
||||
update_items();
|
||||
}
|
||||
|
||||
@@ -262,12 +262,12 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
int id = data()->get_closest_building(xform);
|
||||
Array ret_data;
|
||||
ret_data.resize(5);
|
||||
ret_data[0] = data()->buildings[id].xform;
|
||||
ret_data[0] = data()->get_building(id).xform;
|
||||
ret_data[1] = xform.origin.distance_squared_to(
|
||||
data()->buildings[id].xform.origin);
|
||||
ret_data[2] = data()->buildings[id].key;
|
||||
data()->get_building(id).xform.origin);
|
||||
ret_data[2] = data()->get_building(id).key;
|
||||
ret_data[3] = id;
|
||||
ret_data[4] = data()->buildings[id].id;
|
||||
ret_data[4] = data()->get_building(id).id;
|
||||
emit_signal("command_result", command, ret_data);
|
||||
} else if (command == "get_building_id_for_key") {
|
||||
if (args.size() == 0) {
|
||||
@@ -279,8 +279,8 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
Array ret_data;
|
||||
ret_data.resize(1);
|
||||
int id = -1, i;
|
||||
for (i = 0; i < (int)data()->buildings.size(); i++)
|
||||
if (data()->buildings[i].key.hash64() == khash) {
|
||||
for (i = 0; i < (int)data()->get_building_count(); i++)
|
||||
if (data()->get_building(i).key.hash64() == khash) {
|
||||
id = i;
|
||||
break;
|
||||
}
|
||||
@@ -292,7 +292,7 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
return;
|
||||
}
|
||||
int id = args[0];
|
||||
if (id < 0 || id >= (int)data()->buildings.size())
|
||||
if (id < 0 || id >= (int)data()->get_building_count())
|
||||
return;
|
||||
data()->update_building_transform(id, args[1]);
|
||||
if (item_nodes.has(id)) {
|
||||
@@ -327,9 +327,9 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
print_error("unknown building type: " + new_type);
|
||||
return;
|
||||
}
|
||||
String old_type = data()->buildings[id].id;
|
||||
String old_type = data()->get_building(id).id;
|
||||
unload_building(id);
|
||||
data()->buildings[id].id = new_type;
|
||||
data()->get_building(id).id = new_type;
|
||||
load_building(id);
|
||||
update_items();
|
||||
print_line("changed building: " + itos(id) +
|
||||
@@ -344,8 +344,8 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
// TODO: check that key is valid
|
||||
print_verbose("DICT: " + (JSON::print(building_dict, "\t")));
|
||||
BuildingsData::building::from_dict(&b, building_dict);
|
||||
data()->buildings.push_back(b);
|
||||
load_building(data()->buildings.size() - 1);
|
||||
data()->create_building(b);
|
||||
load_building(data()->get_building_count() - 1);
|
||||
} else if (command == "remove_building") {
|
||||
if (args.size() == 0) {
|
||||
print_error("bad command: not enough args: " + command);
|
||||
@@ -359,11 +359,11 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
return;
|
||||
}
|
||||
std::vector<int> erased_indices;
|
||||
erased_indices.reserve(data()->buildings.size());
|
||||
erased_indices.reserve(data()->get_building_count());
|
||||
String prefix = args[0];
|
||||
int i, j;
|
||||
for (i = 0; i < (int)data()->buildings.size(); i++) {
|
||||
if (data()->buildings[i].id.begins_with(prefix))
|
||||
for (i = 0; i < (int)data()->get_building_count(); i++) {
|
||||
if (data()->get_building(i).id.begins_with(prefix))
|
||||
erased_indices.push_back(i);
|
||||
}
|
||||
print_line("delete buildings: " + itos(erased_indices.size()) +
|
||||
@@ -371,11 +371,10 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
for (i = erased_indices.size() - 1; i >= 0; i--) {
|
||||
int index = erased_indices[i];
|
||||
unload_building(index);
|
||||
data()->buildings.erase(data()->buildings.begin() +
|
||||
index);
|
||||
for (j = index; j < (int)data()->buildings.size(); j++)
|
||||
data()->destroy_building(index);
|
||||
for (j = index; j < data()->get_building_count(); j++)
|
||||
item_nodes[j] = item_nodes[j + 1];
|
||||
item_nodes.erase(data()->buildings.size());
|
||||
item_nodes.erase(data()->get_building_count());
|
||||
}
|
||||
update_items();
|
||||
} else if (command == "remove_generated_stuff") {
|
||||
@@ -505,19 +504,19 @@ StreamWorld::~StreamWorld()
|
||||
void StreamWorld::remove_generated_stuff()
|
||||
{
|
||||
std::vector<int> erased_indices;
|
||||
erased_indices.reserve(data()->buildings.size());
|
||||
erased_indices.reserve(data()->get_building_count());
|
||||
int i, j;
|
||||
for (i = 0; i < (int)data()->buildings.size(); i++) {
|
||||
if (data()->buildings[i].generated)
|
||||
for (i = 0; i < data()->get_building_count(); i++) {
|
||||
if (data()->get_building(i).generated)
|
||||
erased_indices.push_back(i);
|
||||
}
|
||||
for (i = erased_indices.size() - 1; i >= 0; i--) {
|
||||
int index = erased_indices[i];
|
||||
unload_building(index);
|
||||
data()->buildings.erase(data()->buildings.begin() + index);
|
||||
for (j = index; j < (int)data()->buildings.size(); j++)
|
||||
data()->destroy_building(index);
|
||||
for (j = index; j < (int)data()->get_building_count(); j++)
|
||||
item_nodes[j] = item_nodes[j + 1];
|
||||
item_nodes.erase(data()->buildings.size());
|
||||
item_nodes.erase(data()->get_building_count());
|
||||
}
|
||||
update_items();
|
||||
}
|
||||
@@ -525,7 +524,7 @@ void StreamWorld::remove_generated_stuff()
|
||||
void StreamWorld::undo()
|
||||
{
|
||||
int id;
|
||||
for (id = 0; id < (int)data()->buildings.size(); id++)
|
||||
for (id = 0; id < (int)data()->get_building_count(); id++)
|
||||
unload_building(id);
|
||||
data()->undo();
|
||||
update_view();
|
||||
|
||||
Reference in New Issue
Block a user