@@ -83,6 +83,21 @@ void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
83
83
lua_pop (L, 1 ); // Pop error handler
84
84
}
85
85
86
+ void LuaEmergeAreaCallback (v3s16 blockpos, EmergeAction action, void *param)
87
+ {
88
+ ScriptCallbackState *state = (ScriptCallbackState *)param;
89
+ assert (state != NULL );
90
+ assert (state->script != NULL );
91
+ assert (state->refcount > 0 );
92
+
93
+ state->refcount --;
94
+
95
+ state->script ->on_emerge_area_completion (blockpos, action, state);
96
+
97
+ if (state->refcount == 0 )
98
+ delete state;
99
+ }
100
+
86
101
// Exported functions
87
102
88
103
// set_node(pos, node)
@@ -748,24 +763,46 @@ int ModApiEnvMod::l_line_of_sight(lua_State *L)
748
763
return 1 ;
749
764
}
750
765
751
-
752
- // emerge_area(p1, p2)
753
- // emerge mapblocks in area p1..p2
766
+ // emerge_area(p1, p2, [callback, context])
767
+ // emerge mapblocks in area p1..p2, calls callback with context upon completion
754
768
int ModApiEnvMod::l_emerge_area (lua_State *L)
755
769
{
756
770
GET_ENV_PTR;
757
771
772
+ EmergeCompletionCallback callback = NULL ;
773
+ ScriptCallbackState *state = NULL ;
774
+
758
775
EmergeManager *emerge = getServer (L)->getEmergeManager ();
759
776
760
777
v3s16 bpmin = getNodeBlockPos (read_v3s16 (L, 1 ));
761
778
v3s16 bpmax = getNodeBlockPos (read_v3s16 (L, 2 ));
762
779
sortBoxVerticies (bpmin, bpmax);
763
780
781
+ size_t num_blocks = VoxelArea (bpmin, bpmax).getVolume ();
782
+ assert (num_blocks != 0 );
783
+
784
+ if (lua_isfunction (L, 3 )) {
785
+ callback = LuaEmergeAreaCallback;
786
+
787
+ lua_pushvalue (L, 3 );
788
+ int callback_ref = luaL_ref (L, LUA_REGISTRYINDEX);
789
+
790
+ lua_pushvalue (L, 4 );
791
+ int args_ref = luaL_ref (L, LUA_REGISTRYINDEX);
792
+
793
+ state = new ScriptCallbackState;
794
+ state->script = getServer (L)->getScriptIface ();
795
+ state->callback_ref = callback_ref;
796
+ state->args_ref = args_ref;
797
+ state->refcount = num_blocks;
798
+ state->origin = getScriptApiBase (L)->getOrigin ();
799
+ }
800
+
764
801
for (s16 z = bpmin.Z ; z <= bpmax.Z ; z++)
765
802
for (s16 y = bpmin.Y ; y <= bpmax.Y ; y++)
766
803
for (s16 x = bpmin.X ; x <= bpmax.X ; x++) {
767
- v3s16 chunkpos (x, y, z);
768
- emerge-> enqueueBlockEmerge (PEER_ID_INEXISTENT, chunkpos, false , true );
804
+ emerge-> enqueueBlockEmergeEx ( v3s16 (x, y, z), PEER_ID_INEXISTENT,
805
+ BLOCK_EMERGE_ALLOW_GEN | BLOCK_EMERGE_FORCE_QUEUE, callback, state );
769
806
}
770
807
771
808
return 0 ;
0 commit comments