Skip to content

Commit 3ce6642

Browse files
committedMar 7, 2016
Add AreaStore custom ID API
1 parent 821551a commit 3ce6642

File tree

5 files changed

+14
-7
lines changed

5 files changed

+14
-7
lines changed
 

Diff for: ‎builtin/game/features.lua

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ core.features = {
88
use_texture_alpha = true,
99
no_legacy_abms = true,
1010
texture_names_parens = true,
11+
area_store_custom_ids = true,
1112
}
1213

1314
function core.has_feature(arg)

Diff for: ‎doc/lua_api.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2727,7 +2727,7 @@ If you chose the parameter-less constructor, a fast implementation will be autom
27272727
* `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.
27282728
* `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.
27292729
* `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.
2730-
* `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.
2730+
* `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.
27312731
* `reserve(count)`: reserves resources for at most `count` many contained areas. Only needed for efficiency, and only some implementations profit.
27322732
* `remove_area(id)`: removes the area with the given id from the store, returns success.
27332733
* `set_cache_params(params)`: sets params for the included prefiltering cache. Calling invalidates the cache, so that its elements have to be newly generated.

Diff for: ‎src/script/lua_api/l_areastore.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ int LuaAreaStore::l_get_areas_in_area(lua_State *L)
164164
return 1;
165165
}
166166

167-
// insert_area(edge1, edge2, data)
167+
// insert_area(edge1, edge2, data, id)
168168
int LuaAreaStore::l_insert_area(lua_State *L)
169169
{
170170
NO_MAP_LOCK_REQUIRED;
@@ -179,6 +179,9 @@ int LuaAreaStore::l_insert_area(lua_State *L)
179179

180180
a.data = std::string(data, d_len);
181181

182+
if (lua_isnumber(L, 5))
183+
a.id = lua_tonumber(L, 5);
184+
182185
if (!ast->insertArea(&a))
183186
return 0;
184187

Diff for: ‎src/util/areastore.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ void AreaStore::getAreasForPos(std::vector<Area *> *result, v3s16 pos)
160160

161161
bool VectorAreaStore::insertArea(Area *a)
162162
{
163-
a->id = getNextId();
163+
if (a->id == U32_MAX)
164+
a->id = getNextId();
164165
std::pair<AreaMap::iterator, bool> res =
165166
areas_map.insert(std::make_pair(a->id, *a));
166167
if (!res.second)
@@ -232,7 +233,8 @@ static inline SpatialIndex::Point get_spatial_point(const v3s16 pos)
232233

233234
bool SpatialAreaStore::insertArea(Area *a)
234235
{
235-
a->id = getNextId();
236+
if (a->id == U32_MAX)
237+
a->id = getNextId();
236238
if (!areas_map.insert(std::make_pair(a->id, *a)).second)
237239
// ID is not unique
238240
return false;

Diff for: ‎src/util/areastore.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3838

3939

4040
struct Area {
41-
Area() {}
41+
Area() : id(U32_MAX) {}
4242
Area(const v3s16 &mine, const v3s16 &maxe) :
43-
minedge(mine), maxedge(maxe)
43+
id(U32_MAX), minedge(mine), maxedge(maxe)
4444
{
4545
sortBoxVerticies(minedge, maxedge);
4646
}
@@ -68,7 +68,8 @@ class AreaStore {
6868
size_t size() const { return areas_map.size(); }
6969

7070
/// Add an area to the store.
71-
/// Updates the area's ID.
71+
/// Updates the area's ID if it hasn't already been set.
72+
/// @return Whether the area insertion was successful.
7273
virtual bool insertArea(Area *a) = 0;
7374

7475
/// Removes an area from the store by ID.

0 commit comments

Comments
 (0)
Please sign in to comment.