Add AreaStore custom ID API
authorShadowNinja <shadowninja@minetest.net>
Sat, 31 Oct 2015 00:38:22 +0000 (20:38 -0400)
committerShadowNinja <shadowninja@minetest.net>
Mon, 7 Mar 2016 21:33:20 +0000 (16:33 -0500)
builtin/game/features.lua
doc/lua_api.txt
src/script/lua_api/l_areastore.cpp
src/util/areastore.cpp
src/util/areastore.h

index a5f17e54043c3966e37beef07b7a73e42c43d40e..2aad458da04b19eefb3e231b7717c8521fa3ae41 100644 (file)
@@ -8,6 +8,7 @@ core.features = {
        use_texture_alpha = true,
        no_legacy_abms = true,
        texture_names_parens = true,
+       area_store_custom_ids = true,
 }
 
 function core.has_feature(arg)
index 50fa2527365819dcb3c771c615912e4c70e34538..65af515788b3faf42cbf66959c1aeb7180439dbe 100644 (file)
@@ -2727,7 +2727,7 @@ If you chose the parameter-less constructor, a fast implementation will be autom
 * `get_area(id, include_borders, include_data)`: returns the area with the id `id`. (optional) Boolean values `include_borders` and `include_data` control what's copied.
 * `get_areas_for_pos(pos, include_borders, include_data)`: returns all areas that contain the position `pos`. (optional) Boolean values `include_borders` and `include_data` control what's copied.
 * `get_areas_in_area(edge1, edge2, accept_overlap, include_borders, include_data)`: returns all areas that contain all nodes inside the area specified by `edge1` and `edge2` (inclusive). If `accept_overlap` is true, also areas are returned that have nodes in common with the specified area. (optional) Boolean values `include_borders` and `include_data` control what's copied.
-* `insert_area(edge1, edge2, data)`: inserts an area into the store. Returns the new area's ID, or nil if the insertion failed. The (inclusive) positions `edge1` and `edge2` describe the area, `data` is a string stored with the area.
+* `insert_area(edge1, edge2, data, [id])`: inserts an area into the store. Returns the new area's ID, or nil if the insertion failed. The (inclusive) positions `edge1` and `edge2` describe the area. `data` is a string stored with the area.  If passed, `id` will be used as the internal area ID, it must be a unique number between 0 and 2^32-2. If you use the `id` parameter you must always use it, or insertions are likely to fail due to conflicts.
 * `reserve(count)`: reserves resources for at most `count` many contained areas. Only needed for efficiency, and only some implementations profit.
 * `remove_area(id)`: removes the area with the given id from the store, returns success.
 * `set_cache_params(params)`: sets params for the included prefiltering cache. Calling invalidates the cache, so that its elements have to be newly generated.
index 261baf6c942d3a5fa75882006c19ed91bf2237f5..20e7875c7a4a0164f1eda851962cfe734fb05507 100644 (file)
@@ -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;
 
index 17addb3af615fd9dd9e0d42f4c5a168e096108d8..58f08a8c2e22918cdce829b1a45c5340177054fe 100644 (file)
@@ -160,7 +160,8 @@ void AreaStore::getAreasForPos(std::vector<Area *> *result, v3s16 pos)
 
 bool VectorAreaStore::insertArea(Area *a)
 {
-       a->id = getNextId();
+       if (a->id == U32_MAX)
+               a->id = getNextId();
        std::pair<AreaMap::iterator, bool> 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;
index ab6bd76a3d9a37536b38b5b4827f9bdee5cd51a9..bebecfd78d4d332967b7f98bf69c658455bd4c14 100644 (file)
@@ -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.