Now access buildings externally only by string keys
This commit is contained in:
@@ -24,11 +24,10 @@ void StreamWorld::create_tilemap()
|
||||
tile_size;
|
||||
std::tuple<int, int> tkey =
|
||||
std::make_tuple(tile_x, tile_z);
|
||||
int index = data()->get_building_by_key(key);
|
||||
if (tiles.find(tkey) != tiles.end())
|
||||
tiles[tkey].push_back(index);
|
||||
tiles[tkey].push_back(key);
|
||||
else {
|
||||
std::vector<int> data = { index };
|
||||
std::vector<String> data = { key };
|
||||
tiles[tkey] = data;
|
||||
}
|
||||
},
|
||||
@@ -152,11 +151,12 @@ void StreamWorld::load_tile(int tx, int ty)
|
||||
{
|
||||
int i;
|
||||
std::tuple<int, int> key = std::make_tuple(tx, ty);
|
||||
const std::vector<int> &items = tiles[key];
|
||||
const std::vector<String> &items = tiles[key];
|
||||
for (i = 0; i < (int)items.size(); i++) {
|
||||
print_line("load item: " + itos(i) + ": " + itos(items[i]) +
|
||||
print_line("load item: " + itos(i) + ": key: " + (items[i]) +
|
||||
": " /* + data()->get_building(items[i]).id */);
|
||||
load_building(items[i]);
|
||||
const String &bkey = items[i];
|
||||
load_building(bkey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,27 +164,27 @@ void StreamWorld::erase_tile(int tx, int ty)
|
||||
{
|
||||
int i;
|
||||
std::tuple<int, int> key = std::make_tuple(tx, ty);
|
||||
const std::vector<int> &items = tiles[key];
|
||||
const std::vector<String> &items = tiles[key];
|
||||
for (i = 0; i < (int)items.size(); i++) {
|
||||
print_line("unload item: " + itos(i) + ": " + itos(items[i]) +
|
||||
print_line("unload item: " + itos(i) + ": key: " + items[i] +
|
||||
": " /* + data()->get_building(items[i]).id */);
|
||||
unload_building(items[i]);
|
||||
const String &bkey = items[i];
|
||||
unload_building(bkey);
|
||||
}
|
||||
}
|
||||
|
||||
void StreamWorld::load_building(int id)
|
||||
void StreamWorld::load_building(const String &key)
|
||||
{
|
||||
request_item(0, id);
|
||||
request_item(0, key);
|
||||
}
|
||||
|
||||
void StreamWorld::unload_building(int id)
|
||||
void StreamWorld::unload_building(const String &key)
|
||||
{
|
||||
request_item(1, id);
|
||||
request_item(1, key);
|
||||
}
|
||||
|
||||
void StreamWorld::request_item(int type, int item)
|
||||
void StreamWorld::request_item(int type, const String &bkey)
|
||||
{
|
||||
const String &bkey = data()->item_nodes_get_key(item);
|
||||
String id = data()->get_building(bkey).id;
|
||||
if (id == "empty")
|
||||
return;
|
||||
@@ -196,7 +196,7 @@ void StreamWorld::request_item(int type, int item)
|
||||
data()->add_scene_item(id, bkey);
|
||||
break;
|
||||
case 1: {
|
||||
print_line("Removing " + itos(item));
|
||||
print_line("Removing key:" + bkey);
|
||||
if (data()->has_scene(id)) {
|
||||
data()->remove_scene_item(id, bkey);
|
||||
}
|
||||
@@ -238,12 +238,11 @@ void StreamWorld::update_items()
|
||||
}
|
||||
}
|
||||
|
||||
void StreamWorld::remove_building(int index)
|
||||
void StreamWorld::remove_building(const String &key)
|
||||
{
|
||||
// TODO: implement
|
||||
unload_building(index);
|
||||
String key = data()->item_nodes_get_key(index);
|
||||
data()->destroy_building(index);
|
||||
unload_building(key);
|
||||
data()->destroy_building(key);
|
||||
data()->item_nodes_item_removed(key);
|
||||
update_items();
|
||||
}
|
||||
@@ -263,29 +262,15 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
ret_data[1] = xform.origin.distance_squared_to(
|
||||
data()->get_building(key).xform.origin);
|
||||
ret_data[2] = key;
|
||||
ret_data[3] = data()->get_building_by_key(key);
|
||||
ret_data[3] = key;
|
||||
ret_data[4] = data()->get_building(key).id;
|
||||
emit_signal("command_result", command, ret_data);
|
||||
} else if (command == "get_building_id_for_key") {
|
||||
if (args.size() == 0) {
|
||||
print_error("bad command: not enough args: " + command);
|
||||
return;
|
||||
}
|
||||
String key = args[1];
|
||||
Array ret_data;
|
||||
ret_data.resize(1);
|
||||
int id = data()->get_building_by_key(key);
|
||||
ret_data[0] = id;
|
||||
emit_signal("command_result", command, ret_data);
|
||||
} else if (command == "update_building_transform") {
|
||||
if (args.size() == 0) {
|
||||
print_error("bad command: not enough args: " + command);
|
||||
return;
|
||||
}
|
||||
int id = args[0];
|
||||
if (id < 0 || id >= (int)data()->get_building_count())
|
||||
return;
|
||||
const String &key = data()->item_nodes_get_key(id);
|
||||
String key = args[0];
|
||||
data()->update_building_transform(key, args[1]);
|
||||
Node *node = data()->item_nodes_get_node(key);
|
||||
Spatial *bnode = Object::cast_to<Spatial>(node);
|
||||
@@ -310,7 +295,7 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
print_error("bad command: not enough args: " + command);
|
||||
return;
|
||||
}
|
||||
int id = args[0];
|
||||
String key = args[0];
|
||||
String new_type = args[1];
|
||||
Dictionary buildings_data =
|
||||
config.get_value("buildings", "building_data");
|
||||
@@ -318,13 +303,12 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
print_error("unknown building type: " + new_type);
|
||||
return;
|
||||
}
|
||||
String key = data()->item_nodes_get_key(id);
|
||||
String old_type = data()->get_building(key).id;
|
||||
unload_building(id);
|
||||
unload_building(key);
|
||||
data()->get_building(key).id = new_type;
|
||||
load_building(id);
|
||||
load_building(key);
|
||||
update_items();
|
||||
print_line("changed building: " + itos(id) +
|
||||
print_line("changed building: key:" + key +
|
||||
" from: " + old_type + " to: " + new_type);
|
||||
} else if (command == "create_building") {
|
||||
if (args.size() == 0) {
|
||||
@@ -337,43 +321,38 @@ void StreamWorld::run_command(const String &command, const Array &args)
|
||||
print_verbose("DICT: " + (JSON::print(building_dict, "\t")));
|
||||
BuildingsData::building::from_dict(&b, building_dict);
|
||||
data()->create_building(b);
|
||||
load_building(data()->get_building_count() - 1);
|
||||
assert(b.key.length() > 0);
|
||||
load_building(b.key);
|
||||
} else if (command == "remove_building") {
|
||||
if (args.size() == 0) {
|
||||
print_error("bad command: not enough args: " + command);
|
||||
return;
|
||||
}
|
||||
int id = args[0];
|
||||
remove_building(id);
|
||||
const String &key = args[0];
|
||||
remove_building(key);
|
||||
} else if (command == "remove_buildings_by_prefix") {
|
||||
if (args.size() == 0) {
|
||||
print_error("bad command: not enough args: " + command);
|
||||
return;
|
||||
}
|
||||
std::vector<int> erased_indices;
|
||||
erased_indices.reserve(data()->get_building_count());
|
||||
std::vector<String> erased_keys;
|
||||
erased_keys.reserve(data()->get_building_count());
|
||||
String prefix = args[0];
|
||||
int i;
|
||||
data()->for_each_building(
|
||||
[&erased_indices, prefix](const String &key,
|
||||
void *data) {
|
||||
[&erased_keys, prefix](const String &key, void *data) {
|
||||
if (data()->get_building(key).id.begins_with(
|
||||
prefix)) {
|
||||
int index = data()->get_building_by_key(
|
||||
key);
|
||||
assert(index >= 0);
|
||||
erased_indices.push_back(index);
|
||||
erased_keys.push_back(key);
|
||||
}
|
||||
},
|
||||
nullptr);
|
||||
print_line("delete buildings: " + itos(erased_indices.size()) +
|
||||
print_line("delete buildings: " + itos(erased_keys.size()) +
|
||||
" prefix: " + prefix);
|
||||
for (i = erased_indices.size() - 1; i >= 0; i--) {
|
||||
int index = erased_indices[i];
|
||||
unload_building(index);
|
||||
String key = data()->item_nodes_get_key(index);
|
||||
data()->destroy_building(index);
|
||||
data()->item_nodes_item_removed(key);
|
||||
for (i = erased_keys.size() - 1; i >= 0; i--) {
|
||||
unload_building(erased_keys[i]);
|
||||
data()->destroy_building(erased_keys[i]);
|
||||
data()->item_nodes_item_removed(erased_keys[i]);
|
||||
}
|
||||
update_items();
|
||||
} else if (command == "remove_generated_stuff") {
|
||||
@@ -463,7 +442,7 @@ StreamWorld::StreamWorld()
|
||||
tile_map_t::iterator map_it = tiles.begin();
|
||||
while (map_it != tiles.end()) {
|
||||
std::tuple<int, int> key = map_it->first;
|
||||
std::vector<int> &tile_buildings = map_it->second;
|
||||
std::vector<String> &tile_buildings = map_it->second;
|
||||
print_line("x: " + itos(std::get<0>(key)) +
|
||||
" y: " + itos(std::get<1>(key)) + " " +
|
||||
itos(tile_buildings.size()));
|
||||
@@ -483,23 +462,19 @@ StreamWorld::~StreamWorld()
|
||||
|
||||
void StreamWorld::remove_generated_stuff()
|
||||
{
|
||||
std::vector<int> erased_indices;
|
||||
erased_indices.reserve(data()->get_building_count());
|
||||
std::vector<String> erased_keys;
|
||||
erased_keys.reserve(data()->get_building_count());
|
||||
int i;
|
||||
data()->for_each_building(
|
||||
[&erased_indices](const String &key, void *mdata) {
|
||||
if (data()->get_building(key).generated) {
|
||||
int index = data()->get_building_by_key(key);
|
||||
assert(index >= 0);
|
||||
erased_indices.push_back(index);
|
||||
}
|
||||
[&erased_keys](const String &key, void *mdata) {
|
||||
if (data()->get_building(key).generated)
|
||||
erased_keys.push_back(key);
|
||||
},
|
||||
nullptr);
|
||||
for (i = erased_indices.size() - 1; i >= 0; i--) {
|
||||
int index = erased_indices[i];
|
||||
unload_building(index);
|
||||
String key = data()->item_nodes_get_key(index);
|
||||
data()->destroy_building(index);
|
||||
for (i = erased_keys.size() - 1; i >= 0; i--) {
|
||||
const String &key = erased_keys[i];
|
||||
unload_building(key);
|
||||
data()->destroy_building(key);
|
||||
data()->item_nodes_item_removed(key);
|
||||
}
|
||||
update_items();
|
||||
@@ -507,9 +482,9 @@ void StreamWorld::remove_generated_stuff()
|
||||
|
||||
void StreamWorld::undo()
|
||||
{
|
||||
int id;
|
||||
for (id = 0; id < (int)data()->get_building_count(); id++)
|
||||
unload_building(id);
|
||||
data()->for_each_building([this](const String &key,
|
||||
void *mdata) { unload_building(key); },
|
||||
nullptr);
|
||||
data()->undo();
|
||||
update_view();
|
||||
update_items();
|
||||
|
||||
Reference in New Issue
Block a user