Skip to content

Commit f062bbd

Browse files
committedSep 23, 2015
Add /emergeblocks command and core.emerge_area() Lua API
1 parent 596484d commit f062bbd

File tree

9 files changed

+162
-49
lines changed

9 files changed

+162
-49
lines changed
 

‎builtin/common/misc_helpers.lua

+30
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,36 @@ assert(core.string_to_pos("10.0, 5, -2").x == 10)
554554
assert(core.string_to_pos("( 10.0, 5, -2)").z == -2)
555555
assert(core.string_to_pos("asd, 5, -2)") == nil)
556556

557+
--------------------------------------------------------------------------------
558+
function core.string_to_area(value)
559+
local p1, p2 = unpack(value:split(") ("))
560+
if p1 == nil or p2 == nil then
561+
return nil
562+
end
563+
564+
p1 = core.string_to_pos(p1 .. ")")
565+
p2 = core.string_to_pos("(" .. p2)
566+
if p1 == nil or p2 == nil then
567+
return nil
568+
end
569+
570+
return p1, p2
571+
end
572+
573+
local function test_string_to_area()
574+
local p1, p2 = core.string_to_area("(10.0, 5, -2) ( 30.2, 4, -12.53)")
575+
assert(p1.x == 10.0 and p1.y == 5 and p1.z == -2)
576+
assert(p2.x == 30.2 and p2.y == 4 and p2.z == -12.53)
577+
578+
p1, p2 = core.string_to_area("(10.0, 5, -2 30.2, 4, -12.53")
579+
assert(p1 == nil and p2 == nil)
580+
581+
p1, p2 = core.string_to_area("(10.0, 5,) -2 fgdf2, 4, -12.53")
582+
assert(p1 == nil and p2 == nil)
583+
end
584+
585+
test_string_to_area()
586+
557587
--------------------------------------------------------------------------------
558588
function table.copy(t, seen)
559589
local n = {}

‎builtin/game/chatcommands.lua

+41-29
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ core.register_on_chat_message(function(name, message)
5151
return true -- Handled chat message
5252
end)
5353

54+
-- Parses a "range" string in the format of "here (number)" or
55+
-- "(x1, y1, z1) (x2, y2, z2)", returning two position vectors
56+
local function parse_range_str(player_name, str)
57+
local p1, p2
58+
local args = str:split(" ")
59+
60+
if args[1] == "here" then
61+
p1, p2 = core.get_player_radius_area(player_name, tonumber(args[2]))
62+
if p1 == nil then
63+
return false, "Unable to get player " .. player_name .. " position"
64+
end
65+
else
66+
p1, p2 = core.string_to_area(str)
67+
if p1 == nil then
68+
return false, "Incorrect area format. Expected: (x1,y1,z1) (x2,y2,z2)"
69+
end
70+
end
71+
72+
return p1, p2
73+
end
74+
5475
--
5576
-- Chat commands
5677
--
@@ -415,40 +436,31 @@ core.register_chatcommand("set", {
415436
end,
416437
})
417438

418-
core.register_chatcommand("deleteblocks", {
439+
core.register_chatcommand("emergeblocks", {
419440
params = "(here [radius]) | (<pos1> <pos2>)",
420-
description = "delete map blocks contained in area pos1 to pos2",
441+
description = "starts loading (or generating, if inexistent) map blocks "
442+
.. "contained in area pos1 to pos2",
421443
privs = {server=true},
422444
func = function(name, param)
423-
local p1 = {}
424-
local p2 = {}
425-
local args = param:split(" ")
426-
if args[1] == "here" then
427-
local player = core.get_player_by_name(name)
428-
if player == nil then
429-
core.log("error", "player is nil")
430-
return false, "Unable to get current position; player is nil"
431-
end
432-
p1 = player:getpos()
433-
p2 = p1
434-
435-
if #args >= 2 then
436-
local radius = tonumber(args[2]) or 0
437-
p1 = vector.add(p1, radius)
438-
p2 = vector.subtract(p2, radius)
439-
end
440-
else
441-
local pos1, pos2 = unpack(param:split(") ("))
442-
if pos1 == nil or pos2 == nil then
443-
return false, "Incorrect area format. Expected: (x1,y1,z1) (x2,y2,z2)"
444-
end
445+
local p1, p2 = parse_range_str(name, param)
446+
if p1 == false then
447+
return false, p2
448+
end
445449

446-
p1 = core.string_to_pos(pos1 .. ")")
447-
p2 = core.string_to_pos("(" .. pos2)
450+
core.emerge_area(p1, p2)
451+
return true, "Started emerge of area ranging from " ..
452+
core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
453+
end,
454+
})
448455

449-
if p1 == nil or p2 == nil then
450-
return false, "Incorrect area format. Expected: (x1,y1,z1) (x2,y2,z2)"
451-
end
456+
core.register_chatcommand("deleteblocks", {
457+
params = "(here [radius]) | (<pos1> <pos2>)",
458+
description = "delete map blocks contained in area pos1 to pos2",
459+
privs = {server=true},
460+
func = function(name, param)
461+
local p1, p2 = parse_range_str(name, param)
462+
if p1 == false then
463+
return false, p2
452464
end
453465

454466
if core.delete_area(p1, p2) then

‎builtin/game/misc.lua

+19
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ function core.get_connected_players()
109109
return temp_table
110110
end
111111

112+
-- Returns two position vectors representing a box of `radius` in each
113+
-- direction centered around the player corresponding to `player_name`
114+
function core.get_player_radius_area(player_name, radius)
115+
local player = core.get_player_by_name(player_name)
116+
if player == nil then
117+
return nil
118+
end
119+
120+
local p1 = player:getpos()
121+
local p2 = p1
122+
123+
if radius then
124+
p1 = vector.subtract(p1, radius)
125+
p2 = vector.add(p2, radius)
126+
end
127+
128+
return p1, p2
129+
end
130+
112131
function core.hash_node_position(pos)
113132
return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
114133
end

‎doc/lua_api.txt

+5
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,8 @@ Helper functions
17081708
* Convert position to a printable string
17091709
* `minetest.string_to_pos(string)`: returns a position
17101710
* Same but in reverse. Returns `nil` if the string can't be parsed to a position.
1711+
* `minetest.string_to_area("(X1, Y1, Z1) (X2, Y2, Z2)")`: returns two positions
1712+
* Converts a string representing an area box into two positions
17111713
* `minetest.formspec_escape(string)`: returns a string
17121714
* escapes the characters "[", "]", "\", "," and ";", which can not be used in formspecs
17131715
* `minetest.is_yes(arg)`
@@ -2020,6 +2022,9 @@ and `minetest.auth_reload` call the authetification handler.
20202022
* `pos1` and `pos2` are optional and default to mapchunk minp and maxp.
20212023
* `minetest.clear_objects()`
20222024
* clear all objects in the environments
2025+
* `minetest.emerge_area(pos1, pos2)`
2026+
* queues all mapblocks in the area from pos1 to pos2, inclusive, for emerge
2027+
* i.e. asynchronously loads blocks from disk, or if inexistent, generates them
Has conversations. Original line has conversations.
20232028
* `minetest.delete_area(pos1, pos2)`
20242029
* delete all mapblocks in the area from pos1 to pos2, inclusive
20252030
* `minetest.line_of_sight(pos1, pos2, stepsize)`: returns `boolean, pos`

‎src/emerge.cpp

+30-10
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,13 @@ void EmergeManager::stopThreads()
235235
}
236236

237237

238-
bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate)
238+
bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p,
239+
bool allow_generate, bool force_queue_block)
239240
{
240241
std::map<v3s16, BlockEmergeData *>::const_iterator iter;
241242
BlockEmergeData *bedata;
242-
u16 count;
243+
u16 count_global = 0;
244+
u16 count_peer = 0;
243245
u8 flags = 0;
244246
int idx = 0;
245247

@@ -249,14 +251,17 @@ bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate
249251
{
250252
MutexAutoLock queuelock(queuemutex);
251253

252-
count = blocks_enqueued.size();
253-
if (count >= qlimit_total)
254-
return false;
254+
count_global = blocks_enqueued.size();
255+
count_peer = peer_queue_count[peer_id];
255256

256-
count = peer_queue_count[peer_id];
257-
u16 qlimit_peer = allow_generate ? qlimit_generate : qlimit_diskonly;
258-
if (count >= qlimit_peer)
259-
return false;
257+
if (!force_queue_block) {
258+
if (count_global >= qlimit_total)
259+
return false;
260+
261+
u16 qlimit_peer = allow_generate ? qlimit_generate : qlimit_diskonly;
262+
if (count_peer >= qlimit_peer)
263+
return false;
264+
}
260265

261266
iter = blocks_enqueued.find(p);
262267
if (iter != blocks_enqueued.end()) {
@@ -270,7 +275,7 @@ bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate
270275
bedata->peer_requested = peer_id;
271276
blocks_enqueued.insert(std::make_pair(p, bedata));
272277

273-
peer_queue_count[peer_id] = count + 1;
278+
peer_queue_count[peer_id] = count_peer + 1;
274279

275280
// insert into the EmergeThread queue with the least items
276281
int lowestitems = emergethread[0]->blockqueue.size();
@@ -289,6 +294,21 @@ bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate
289294
return true;
290295
}
291296

297+
v3s16 EmergeManager::getContainingChunk(v3s16 blockpos)
298+
{
299+
return getContainingChunk(blockpos, params.chunksize);
300+
}
301+
302+
303+
v3s16 EmergeManager::getContainingChunk(v3s16 blockpos, s16 chunksize)
304+
{
305+
s16 coff = -chunksize / 2;
306+
v3s16 chunk_offset(coff, coff, coff);
307+
308+
return getContainerPos(blockpos - chunk_offset, chunksize)
309+
* chunksize + chunk_offset;
310+
}
311+
292312

293313
int EmergeManager::getGroundLevelAtPoint(v2s16 p)
294314
{

‎src/emerge.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,13 @@ class EmergeManager {
109109
static void getMapgenNames(std::list<const char *> &mgnames);
110110
void startThreads();
111111
void stopThreads();
112-
bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate);
112+
bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate,
113+
bool force_queue_block=false);
113114

114-
//mapgen helper methods
115+
v3s16 getContainingChunk(v3s16 blockpos);
116+
static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
117+
118+
// mapgen helper methods
115119
Biome *getBiomeAtPoint(v3s16 p);
116120
int getGroundLevelAtPoint(v2s16 p);
117121
bool isBlockUnderground(v3s16 blockpos);

‎src/map.cpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -2259,14 +2259,9 @@ bool ServerMap::initBlockMake(BlockMakeData *data, v3s16 blockpos)
22592259
bool enable_mapgen_debug_info = m_emerge->mapgen_debug_info;
22602260
EMERGE_DBG_OUT("initBlockMake(): " PP(blockpos) " - " PP(blockpos));
22612261

2262-
s16 chunksize = m_emerge->params.chunksize;
2263-
s16 coffset = -chunksize / 2;
2264-
v3s16 chunk_offset(coffset, coffset, coffset);
2265-
v3s16 blockpos_div = getContainerPos(blockpos - chunk_offset, chunksize);
2266-
v3s16 blockpos_min = blockpos_div * chunksize;
2267-
v3s16 blockpos_max = blockpos_div * chunksize + v3s16(1,1,1)*(chunksize-1);
2268-
blockpos_min += chunk_offset;
2269-
blockpos_max += chunk_offset;
2262+
s16 csize = m_emerge->params.chunksize;
2263+
v3s16 blockpos_min = EmergeManager::getContainingChunk(blockpos, csize);
2264+
v3s16 blockpos_max = blockpos_min + v3s16(1, 1, 1) * (csize - 1);
22702265

22712266
v3s16 extra_borders(1,1,1);
22722267

‎src/script/lua_api/l_env.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3333
#include "util/pointedthing.h"
3434
#include "content_sao.h"
3535
#include "treegen.h"
36+
#include "emerge.h"
3637
#include "pathfinder.h"
3738

3839
#define GET_ENV_PTR ServerEnvironment* env = \
@@ -751,6 +752,29 @@ int ModApiEnvMod::l_line_of_sight(lua_State *L)
751752
return 1;
752753
}
753754

755+
756+
// emerge_area(p1, p2)
757+
// emerge mapblocks in area p1..p2
758+
int ModApiEnvMod::l_emerge_area(lua_State *L)
759+
{
760+
GET_ENV_PTR;
761+
762+
EmergeManager *emerge = getServer(L)->getEmergeManager();
763+
764+
v3s16 bpmin = getNodeBlockPos(read_v3s16(L, 1));
765+
v3s16 bpmax = getNodeBlockPos(read_v3s16(L, 2));
766+
sortBoxVerticies(bpmin, bpmax);
767+
768+
for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
769+
for (s16 y = bpmin.Y; y <= bpmax.Y; y++)
770+
for (s16 x = bpmin.X; x <= bpmax.X; x++) {
771+
v3s16 chunkpos(x, y, z);
772+
emerge->enqueueBlockEmerge(PEER_ID_INEXISTENT, chunkpos, false, true);
Has conversations. Original line has conversations.
773+
}
774+
775+
return 0;
776+
}
777+
754778
// delete_area(p1, p2)
755779
// delete mapblocks in area p1..p2
756780
int ModApiEnvMod::l_delete_area(lua_State *L)
@@ -954,6 +978,7 @@ void ModApiEnvMod::Initialize(lua_State *L, int top)
954978
API_FCT(find_node_near);
955979
API_FCT(find_nodes_in_area);
956980
API_FCT(find_nodes_in_area_under_air);
981+
API_FCT(emerge_area);
957982
API_FCT(delete_area);
958983
API_FCT(get_perlin);
959984
API_FCT(get_perlin_map);

‎src/script/lua_api/l_env.h

+3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ class ModApiEnvMod : public ModApiBase {
125125
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
126126
static int l_find_nodes_in_area_under_air(lua_State *L);
127127

128+
// emerge_area(p1, p2)
129+
static int l_emerge_area(lua_State *L);
130+
128131
// delete_area(p1, p2) -> true/false
129132
static int l_delete_area(lua_State *L);
130133

0 commit comments

Comments
 (0)
Please sign in to comment.