From 3ce6642a26d43fd368c24cdfc5460557258fb44f Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 30 Oct 2015 20:38:22 -0400 Subject: Add AreaStore custom ID API --- src/script/lua_api/l_areastore.cpp | 5 ++++- src/util/areastore.cpp | 6 ++++-- src/util/areastore.h | 7 ++++--- 3 files changed, 12 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/script/lua_api/l_areastore.cpp b/src/script/lua_api/l_areastore.cpp index 261baf6c9..20e7875c7 100644 --- a/src/script/lua_api/l_areastore.cpp +++ b/src/script/lua_api/l_areastore.cpp @@ -164,7 +164,7 @@ int LuaAreaStore::l_get_areas_in_area(lua_State *L) return 1; } -// insert_area(edge1, edge2, data) +// insert_area(edge1, edge2, data, id) int LuaAreaStore::l_insert_area(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -179,6 +179,9 @@ int LuaAreaStore::l_insert_area(lua_State *L) a.data = std::string(data, d_len); + if (lua_isnumber(L, 5)) + a.id = lua_tonumber(L, 5); + if (!ast->insertArea(&a)) return 0; diff --git a/src/util/areastore.cpp b/src/util/areastore.cpp index 17addb3af..58f08a8c2 100644 --- a/src/util/areastore.cpp +++ b/src/util/areastore.cpp @@ -160,7 +160,8 @@ void AreaStore::getAreasForPos(std::vector *result, v3s16 pos) bool VectorAreaStore::insertArea(Area *a) { - a->id = getNextId(); + if (a->id == U32_MAX) + a->id = getNextId(); std::pair res = areas_map.insert(std::make_pair(a->id, *a)); if (!res.second) @@ -232,7 +233,8 @@ static inline SpatialIndex::Point get_spatial_point(const v3s16 pos) bool SpatialAreaStore::insertArea(Area *a) { - a->id = getNextId(); + if (a->id == U32_MAX) + a->id = getNextId(); if (!areas_map.insert(std::make_pair(a->id, *a)).second) // ID is not unique return false; diff --git a/src/util/areastore.h b/src/util/areastore.h index ab6bd76a3..bebecfd78 100644 --- a/src/util/areastore.h +++ b/src/util/areastore.h @@ -38,9 +38,9 @@ with this program; if not, write to the Free Software Foundation, Inc., struct Area { - Area() {} + Area() : id(U32_MAX) {} Area(const v3s16 &mine, const v3s16 &maxe) : - minedge(mine), maxedge(maxe) + id(U32_MAX), minedge(mine), maxedge(maxe) { sortBoxVerticies(minedge, maxedge); } @@ -68,7 +68,8 @@ public: size_t size() const { return areas_map.size(); } /// Add an area to the store. - /// Updates the area's ID. + /// Updates the area's ID if it hasn't already been set. + /// @return Whether the area insertion was successful. virtual bool insertArea(Area *a) = 0; /// Removes an area from the store by ID. -- cgit v1.2.3