Skip to content

Commit 9e4e707

Browse files
committedSep 1, 2014
Update Mapgen VoxelManipulator on buffer invalidation
1 parent 3fa4f78 commit 9e4e707

File tree

6 files changed

+75
-10
lines changed

6 files changed

+75
-10
lines changed
 

‎doc/lua_api.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,8 @@ methods:
20992099
^ To be used only by a VoxelManip object from minetest.get_mapgen_object
21002100
^ (p1, p2) is the area in which lighting is set; defaults to the whole area if left out
21012101
- update_liquids(): Update liquid flow
2102+
- was_modified(): Returns true or false if the data in the voxel manipulator had been modified since
2103+
the last read from map, due to a call to minetest.set_data() on the loaded area elsewhere
21022104

21032105
VoxelArea: A helper class for voxel areas
21042106
- Can be created via VoxelArea:new{MinEdge=pmin, MaxEdge=pmax}

‎src/environment.cpp

+29-10
Original file line numberDiff line numberDiff line change
@@ -778,44 +778,63 @@ bool ServerEnvironment::setNode(v3s16 p, const MapNode &n)
778778
{
779779
INodeDefManager *ndef = m_gamedef->ndef();
780780
MapNode n_old = m_map->getNodeNoEx(p);
781+
781782
// Call destructor
782-
if(ndef->get(n_old).has_on_destruct)
783+
if (ndef->get(n_old).has_on_destruct)
783784
m_script->node_on_destruct(p, n_old);
785+
784786
// Replace node
785-
bool succeeded = m_map->addNodeWithEvent(p, n);
786-
if(!succeeded)
787+
if (!m_map->addNodeWithEvent(p, n))
787788
return false;
789+
790+
// Update active VoxelManipulator if a mapgen thread
791+
m_map->updateVManip(p);
792+
788793
// Call post-destructor
789-
if(ndef->get(n_old).has_after_destruct)
794+
if (ndef->get(n_old).has_after_destruct)
790795
m_script->node_after_destruct(p, n_old);
796+
791797
// Call constructor
792-
if(ndef->get(n).has_on_construct)
798+
if (ndef->get(n).has_on_construct)
793799
m_script->node_on_construct(p, n);
800+
794801
return true;
795802
}
796803

797804
bool ServerEnvironment::removeNode(v3s16 p)
798805
{
799806
INodeDefManager *ndef = m_gamedef->ndef();
800807
MapNode n_old = m_map->getNodeNoEx(p);
808+
801809
// Call destructor
802-
if(ndef->get(n_old).has_on_destruct)
810+
if (ndef->get(n_old).has_on_destruct)
803811
m_script->node_on_destruct(p, n_old);
812+
804813
// Replace with air
805814
// This is slightly optimized compared to addNodeWithEvent(air)
806-
bool succeeded = m_map->removeNodeWithEvent(p);
807-
if(!succeeded)
815+
if (!m_map->removeNodeWithEvent(p))
808816
return false;
817+
818+
// Update active VoxelManipulator if a mapgen thread
819+
m_map->updateVManip(p);
820+
809821
// Call post-destructor
810-
if(ndef->get(n_old).has_after_destruct)
822+
if (ndef->get(n_old).has_after_destruct)
811823
m_script->node_after_destruct(p, n_old);
824+
812825
// Air doesn't require constructor
813826
return true;
814827
}
815828

816829
bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n)
817830
{
818-
return m_map->addNodeWithEvent(p, n, false);
831+
if (!m_map->addNodeWithEvent(p, n, false))
832+
return false;
833+
834+
// Update active VoxelManipulator if a mapgen thread
835+
m_map->updateVManip(p);
836+
837+
return true;
819838
}
820839

821840
std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius)

‎src/map.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -2757,6 +2757,28 @@ MapBlock *ServerMap::getBlockOrEmerge(v3s16 p3d)
27572757
void ServerMap::prepareBlock(MapBlock *block) {
27582758
}
27592759

2760+
// N.B. This requires no synchronization, since data will not be modified unless
2761+
// the VoxelManipulator being updated belongs to the same thread.
2762+
void ServerMap::updateVManip(v3s16 pos)
2763+
{
2764+
Mapgen *mg = m_emerge->getCurrentMapgen();
2765+
if (!mg)
2766+
return;
2767+
2768+
ManualMapVoxelManipulator *vm = mg->vm;
2769+
if (!vm)
2770+
return;
2771+
2772+
if (!vm->m_area.contains(pos))
2773+
return;
2774+
2775+
s32 idx = vm->m_area.index(pos);
2776+
vm->m_data[idx] = getNodeNoEx(pos);
2777+
vm->m_flags[idx] &= ~VOXELFLAG_NO_DATA;
2778+
2779+
vm->m_is_dirty = true;
2780+
}
2781+
27602782
s16 ServerMap::findGroundLevel(v2s16 p2d)
27612783
{
27622784
#if 0
@@ -3523,6 +3545,7 @@ void ServerMap::PrintInfo(std::ostream &out)
35233545

35243546
ManualMapVoxelManipulator::ManualMapVoxelManipulator(Map *map):
35253547
VoxelManipulator(),
3548+
m_is_dirty(false),
35263549
m_create_area(false),
35273550
m_map(map)
35283551
{
@@ -3617,6 +3640,8 @@ void ManualMapVoxelManipulator::initialEmerge(v3s16 blockpos_min,
36173640

36183641
m_loaded_blocks[p] = flags;
36193642
}
3643+
3644+
m_is_dirty = false;
36203645
}
36213646

36223647
void ManualMapVoxelManipulator::blitBackAll(

‎src/map.h

+4
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ class ServerMap : public Map
493493
// Database version
494494
void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
495495

496+
void updateVManip(v3s16 pos);
497+
496498
// For debug printing
497499
virtual void PrintInfo(std::ostream &out);
498500

@@ -550,6 +552,8 @@ class ManualMapVoxelManipulator : public VoxelManipulator
550552
void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
551553
bool overwrite_generated = true);
552554

555+
bool m_is_dirty;
556+
553557
protected:
554558
bool m_create_area;
555559
Map *m_map;

‎src/script/lua_api/l_vmanip.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,18 @@ int LuaVoxelManip::l_update_map(lua_State *L)
304304
return 0;
305305
}
306306

307+
int LuaVoxelManip::l_was_modified(lua_State *L)
308+
{
309+
NO_MAP_LOCK_REQUIRED;
310+
311+
LuaVoxelManip *o = checkobject(L, 1);
312+
ManualMapVoxelManipulator *vm = o->vm;
313+
314+
lua_pushboolean(L, vm->m_is_dirty);
315+
316+
return 1;
317+
}
318+
307319
LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm)
308320
{
309321
this->vm = mmvm;
@@ -396,5 +408,6 @@ const luaL_reg LuaVoxelManip::methods[] = {
396408
luamethod(LuaVoxelManip, set_light_data),
397409
luamethod(LuaVoxelManip, get_param2_data),
398410
luamethod(LuaVoxelManip, set_param2_data),
411+
luamethod(LuaVoxelManip, was_modified),
399412
{0,0}
400413
};

‎src/script/lua_api/l_vmanip.h

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class LuaVoxelManip : public ModApiBase {
5858
static int l_get_param2_data(lua_State *L);
5959
static int l_set_param2_data(lua_State *L);
6060

61+
static int l_was_modified(lua_State *L);
62+
6163
public:
6264
LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mapgen_vm);
6365
LuaVoxelManip(Map *map);

0 commit comments

Comments
 (0)
Please sign in to comment.