From 13f946689e28e99ac71172925f63f4320798a0ee Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 5 Mar 2018 16:07:07 -0500 Subject: Added entity indexing Changed the world format so that map objects are indexed (per map). The next available map object index is cached for each map. --- tools/mapedit/src/map.cpp | 14 ++++++++++++++ tools/mapedit/src/map.h | 4 ++++ tools/mapedit/src/object.cpp | 15 +++++++++++++-- tools/mapedit/src/object.h | 8 +++++++- tools/mapedit/src/widget.cpp | 7 ++++++- tools/mapedit/src/world.cpp | 20 +++++++++++++++++++- 6 files changed, 63 insertions(+), 5 deletions(-) (limited to 'tools/mapedit') diff --git a/tools/mapedit/src/map.cpp b/tools/mapedit/src/map.cpp index a099e29..7e6c3c0 100644 --- a/tools/mapedit/src/map.cpp +++ b/tools/mapedit/src/map.cpp @@ -188,6 +188,11 @@ const Map::Adjacent& Map::getAdjacent(MoveDir direction) const } } +size_t Map::getNextObjectIndex() const +{ + return nextObjectIndex; +} + void Map::setTitle(std::string title, bool dirty) { this->title = title; @@ -271,3 +276,12 @@ void Map::setAdjacent(MoveDir direction, MoveType type, int map, bool dirty) } } +size_t Map::getAndIncrementNextObjectIndex() +{ + return nextObjectIndex++; +} + +void Map::setNextObjectIndex(size_t v) +{ + nextObjectIndex = v; +} diff --git a/tools/mapedit/src/map.h b/tools/mapedit/src/map.h index 9c14218..1ae905f 100644 --- a/tools/mapedit/src/map.h +++ b/tools/mapedit/src/map.h @@ -89,6 +89,7 @@ class Map { bool getHidden() const; const std::map& getAdjacents() const; const Adjacent& getAdjacent(MoveDir direction) const; + size_t getNextObjectIndex() const; void setTitle(std::string title, bool dirty = true); void setTileAt(int x, int y, int tile, bool dirty = true); @@ -100,6 +101,8 @@ class Map { void setExpanded(bool exp); void setHidden(bool hid); void setAdjacent(MoveDir direction, MoveType type, int map = -1, bool dirty = true); + size_t getAndIncrementNextObjectIndex(); + void setNextObjectIndex(size_t v); private: int id; @@ -113,6 +116,7 @@ class Map { bool hidden = false; std::map adjacents; const Adjacent defaultAdjacent {}; + size_t nextObjectIndex = 0; }; class MapPtrCtr : public wxTreeItemData { diff --git a/tools/mapedit/src/object.cpp b/tools/mapedit/src/object.cpp index 8ed29af..aeb8fc1 100644 --- a/tools/mapedit/src/object.cpp +++ b/tools/mapedit/src/object.cpp @@ -184,9 +184,15 @@ bool MapObject::operator!=(const MapObject& other) const return id != other.id; } -MapObjectEntry::MapObjectEntry(const MapObject& object, int posx, int posy) : object(object) +MapObjectEntry::MapObjectEntry( + const MapObject& object, + int posx, + int posy, + size_t index) : + object(object), + position(std::make_pair(posx, posy)), + index(index) { - position = std::make_pair(posx, posy); } const MapObject& MapObjectEntry::getObject() const @@ -209,6 +215,11 @@ const std::map& MapObjectEntry::getItems() co return items; } +size_t MapObjectEntry::getIndex() const +{ + return index; +} + void MapObjectEntry::addItem(std::string id, Item& item) { items[id] = item; diff --git a/tools/mapedit/src/object.h b/tools/mapedit/src/object.h index a870a2e..a87fa96 100644 --- a/tools/mapedit/src/object.h +++ b/tools/mapedit/src/object.h @@ -67,7 +67,11 @@ class MapObject { class MapObjectEntry { public: - MapObjectEntry(const MapObject& object, int posx, int posy); + MapObjectEntry( + const MapObject& object, + int posx, + int posy, + size_t index); struct Item { MapObject::Input::Type type; @@ -78,6 +82,7 @@ class MapObjectEntry { std::pair getPosition() const; Item& getItem(std::string str); const std::map& getItems() const; + size_t getIndex() const; void setPosition(int x, int y); void addItem(std::string id, Item& item); @@ -89,6 +94,7 @@ class MapObjectEntry { const MapObject& object; std::pair position; std::map items; + size_t index; }; class VariableChoiceValidator : public wxValidator { diff --git a/tools/mapedit/src/widget.cpp b/tools/mapedit/src/widget.cpp index 8d74b39..85838e6 100644 --- a/tools/mapedit/src/widget.cpp +++ b/tools/mapedit/src/widget.cpp @@ -256,7 +256,12 @@ void MapeditWidget::OnClick(wxMouseEvent& event) int x = (event.GetPosition().x + vX - EDITOR_SPACING_X*scale) / scale - (addingEntity->getWidth() / 2); int y = (event.GetPosition().y + vY - EDITOR_SPACING_Y*scale) / scale - (addingEntity->getHeight() / 2); - auto data = std::make_shared(*addingEntity, x, y); + auto data = std::make_shared( + *addingEntity, + x, + y, + map->getAndIncrementNextObjectIndex()); + frame->commitAction(std::make_shared("Add " + addingEntity->getName(), [=] () { map->addObject(data); diff --git a/tools/mapedit/src/world.cpp b/tools/mapedit/src/world.cpp index 79f5a58..9983731 100644 --- a/tools/mapedit/src/world.cpp +++ b/tools/mapedit/src/world.cpp @@ -94,6 +94,11 @@ World::World(std::string filename) map->setTitle((char*) titleKey, false); xmlFree(titleKey); + xmlChar* noiKey = xmlGetProp(node, (xmlChar*) "nextObject"); + if (noiKey == 0) throw MapLoadException("map missing nextObject attribute"); + map->setNextObjectIndex(atoi((char*) noiKey)); + xmlFree(noiKey); + for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) { if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) @@ -125,7 +130,12 @@ World::World(std::string filename) int ypos = atoi((char*) yKey); xmlFree(yKey); - auto data = std::make_shared(obj, xpos, ypos); + xmlChar* indexKey = xmlGetProp(mapNode, (const xmlChar*) "index"); + if (indexKey == 0) throw MapLoadException("entity missing index attribute"); + int objIndex = atoi((char*) indexKey); + xmlFree(indexKey); + + auto data = std::make_shared(obj, xpos, ypos, objIndex); map->addObject(data, false); @@ -312,6 +322,10 @@ void World::save(std::string name, wxTreeCtrl* mapTree) // title= rc = xmlTextWriterWriteAttribute(writer, (xmlChar*) "title", (xmlChar*) map.getTitle().c_str()); if (rc < 0) throw MapWriteException(name); + + // nextObject= + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "nextObject", "%zu", map.getNextObjectIndex()); + if (rc < 0) throw MapWriteException(name); // getPosition().second); if (rc < 0) throw MapWriteException(name); + // index= + rc = xmlTextWriterWriteFormatAttribute(writer, (xmlChar*) "index", "%zu", object->getIndex()); + if (rc < 0) throw MapWriteException(name); + for (auto item : object->getItems()) { //