hypermcpe/mods/worldedit/lib.lua
Kirill Sokolov 110cb2a896 Infinite world, LuaJIT mod API, WorldEdit port, skins and music
- Infinite world: streamed chunk cache, multi-region storage (old chunks.dat
  still loads), per-chunk entity/tile entity persistence, client chunk
  streaming, protocol version 10, view-distance server property
- LuaJIT mod host (mods/, lua/prelude.lua) with events, commands, world and
  player API; WorldEdit command set ported to it (mods/worldedit)
- /skin command, SetSkinPacket and bundled skins
- OGG music player (data/music is ignored, tools/convert-music.ps1)
- package-windows.ps1 builds client and dedicated server for Windows

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 21:08:09 +03:00

450 lines
13 KiB
Lua

-- WorldEdit for MinecraftPE: core library
-- (edit sessions, patterns, masks, region helpers, shapes, history)
local L = {}
local floor, ceil, abs, min, max, sqrt = math.floor, math.ceil, math.abs, math.min, math.max, math.sqrt
local get_block, set_block = mc.world.get_block, mc.world.set_block
L.DEFAULT_LIMIT = 5000000
-- =========================================================== player sessions
local sessions = {}
function L.session(player)
local s = sessions[player.name]
if not s then
s = {
pos1 = nil, pos2 = nil,
clipboard = nil,
undo = {}, redo = {},
limit = L.DEFAULT_LIMIT,
tools = {}, -- item id -> { type = "repl"/"info"/"brush"/..., ... }
superpick = nil, -- nil | "single" | "area" | "recursive"
superpick_range = 3,
brush_size = 5, brush_mask = nil, brush_material = nil, brush_range = 200,
selector = "cuboid",
}
sessions[player.name] = s
end
return s
end
-- ================================================================== helpers
function L.region(s)
if not s.pos1 or not s.pos2 then return nil end
local a, b = s.pos1, s.pos2
return {
x0 = min(a[1], b[1]), y0 = min(a[2], b[2]), z0 = min(a[3], b[3]),
x1 = max(a[1], b[1]), y1 = max(a[2], b[2]), z1 = max(a[3], b[3]),
}
end
function L.volume(r)
return (r.x1 - r.x0 + 1) * (r.y1 - r.y0 + 1) * (r.z1 - r.z0 + 1)
end
local function clampY(y) return max(0, min(127, y)) end
L.clampY = clampY
--- Loads every chunk the region touches.
function L.load_region(r)
for cx = floor(r.x0 / 16), floor(r.x1 / 16) do
for cz = floor(r.z0 / 16), floor(r.z1 / 16) do
mc.world.ensure_loaded(cx * 16 + 8, cz * 16 + 8, 0)
end
end
end
local DIRS = {
up = { 0, 1, 0 }, u = { 0, 1, 0 }, down = { 0, -1, 0 }, d = { 0, -1, 0 },
north = { 0, 0, -1 }, n = { 0, 0, -1 }, south = { 0, 0, 1 }, s = { 0, 0, 1 },
east = { 1, 0, 0 }, e = { 1, 0, 0 }, west = { -1, 0, 0 }, w = { -1, 0, 0 },
}
--- Direction word -> unit vector. "me"/nil = where the player looks (incl. up/down)
function L.direction(player, word)
word = word and string.lower(word)
if word and DIRS[word] then return DIRS[word] end
local yaw, pitch = player:look()
if pitch < -50 then return DIRS.up end
if pitch > 50 then return DIRS.down end
local _, dx, dz = player:facing()
return { dx, 0, dz }
end
--- Splits "-abc foo 5 -p 2" into flags and positional arguments.
--- `valued` is a set of flags that take a value: { p = true }
function L.parse(args, valued)
local flags, pos = {}, {}
local i = 1
while i <= #args do
local a = args[i]
if a:match("^%-%a+$") and not tonumber(a) then
for ch in a:sub(2):gmatch(".") do
if valued and valued[ch] then
i = i + 1
flags[ch] = args[i]
else
flags[ch] = true
end
end
else
pos[#pos + 1] = a
end
i = i + 1
end
return flags, pos
end
-- ================================================================== patterns
-- "stone" "5%stone,5%dirt:1,air" "35:14"
function L.pattern(text)
if not text or text == "" then return nil, "No pattern given." end
local entries, total = {}, 0
for part in text:gmatch("[^,]+") do
local weight, spec = part:match("^([%d%.]+)%%(.+)$")
weight = tonumber(weight) or 1
spec = spec or part
local id, data = mc.block.parse(spec)
if not id then return nil, "Unknown block: " .. spec end
entries[#entries + 1] = { id = id, data = data, w = weight }
total = total + weight
end
if #entries == 1 then
local e = entries[1]
return function() return e.id, e.data end, entries[1]
end
return function()
local r = math.random() * total
for _, e in ipairs(entries) do
r = r - e.w
if r <= 0 then return e.id, e.data end
end
local e = entries[#entries]
return e.id, e.data
end
end
-- masks: "stone" "stone,dirt" "!air" "#existing" "#solid"
function L.mask(text)
if not text or text == "" then return nil end
local negate = false
if text:sub(1, 1) == "!" then negate = true; text = text:sub(2) end
local test
if text == "#existing" then
test = function(id) return id ~= 0 end
elseif text == "#solid" then
test = function(id) return id ~= 0 and id ~= 8 and id ~= 9 and id ~= 10 and id ~= 11 end
elseif text == "#air" then
test = function(id) return id == 0 end
else
local set = {}
for part in text:gmatch("[^,]+") do
local id, data = mc.block.parse(part)
if not id then return nil, "Unknown block: " .. part end
set[#set + 1] = { id, data }
end
test = function(id, data)
for _, e in ipairs(set) do
if e[1] == id and (e[2] == nil or e[2] == data) then return true end
end
return false
end
end
if negate then return function(id, data) return not test(id, data) end end
return test
end
-- ============================================================== edit sessions
local Edit = {}
Edit.__index = Edit
function L.new_edit(session)
return setmetatable({ s = session, entries = {}, index = {}, count = 0, limit = session.limit }, Edit)
end
local OFFSET = 1000000
--- Sets a block (recording the old one for undo). Returns true when something changed.
function Edit:set(x, y, z, id, data)
if y < 0 or y > 127 then return false end
data = data or 0
local oid, od = get_block(x, y, z)
if oid == id and od == data then return false end
if self.limit >= 0 and self.count >= self.limit then
self.overflow = true
return false
end
if not self.bx then self.bx, self.bz = x, z end
local key = ((x - self.bx + OFFSET) * 4000000 + (z - self.bz + OFFSET)) * 128 + y
local e = self.index[key]
if not e then
e = { x, y, z, oid, od, id, data }
self.index[key] = e
self.entries[#self.entries + 1] = e
else
e[6], e[7] = id, data
end
self.count = self.count + 1
set_block(x, y, z, id, data, true)
return true
end
function Edit:get(x, y, z)
return get_block(x, y, z)
end
--- Writes the pattern into the block
function Edit:place(x, y, z, pattern)
local id, data = pattern()
return self:set(x, y, z, id, data)
end
--- Finishes the edit: relights/resends the touched chunks and stores it in the history
function Edit:commit()
mc.world.flush()
if #self.entries > 0 then
local undo = self.s.undo
undo[#undo + 1] = self.entries
if #undo > 40 then table.remove(undo, 1) end
self.s.redo = {}
end
return #self.entries
end
local function apply_history(entries, reverse)
local changed = 0
if reverse then
for i = #entries, 1, -1 do
local e = entries[i]
set_block(e[1], e[2], e[3], e[4], e[5], true)
changed = changed + 1
end
else
for i = 1, #entries do
local e = entries[i]
set_block(e[1], e[2], e[3], e[6], e[7], true)
changed = changed + 1
end
end
mc.world.flush()
return changed
end
function L.undo(s)
local e = table.remove(s.undo)
if not e then return nil end
s.redo[#s.redo + 1] = e
return apply_history(e, true)
end
function L.redo(s)
local e = table.remove(s.redo)
if not e then return nil end
s.undo[#s.undo + 1] = e
return apply_history(e, false)
end
-- ============================================================ region iteration
function L.each(r, fn)
for x = r.x0, r.x1 do
for z = r.z0, r.z1 do
for y = r.y0, r.y1 do
fn(x, y, z)
end
end
end
end
function L.walls_and_faces(r, edit, pattern, floorAndCeil)
L.each(r, function(x, y, z)
local wall = x == r.x0 or x == r.x1 or z == r.z0 or z == r.z1
local cap = y == r.y0 or y == r.y1
if wall or (floorAndCeil and cap) then edit:place(x, y, z, pattern) end
end)
end
-- ================================================================== shapes
local function lsq(x, y, z) return x * x + y * y + z * z end
--- WorldEdit's ellipsoid. (cx,cy,cz) is the centre block.
function L.sphere(edit, cx, cy, cz, pattern, rx, ry, rz, filled)
rx, ry, rz = rx + 0.5, ry + 0.5, rz + 0.5
local irx, iry, irz = 1 / rx, 1 / ry, 1 / rz
local crx, cry, crz = ceil(rx), ceil(ry), ceil(rz)
local nextXn = 0
for x = 0, crx do
local xn = nextXn
nextXn = (x + 1) * irx
local nextYn = 0
for y = 0, cry do
local yn = nextYn
nextYn = (y + 1) * iry
local nextZn = 0
for z = 0, crz do
local zn = nextZn
nextZn = (z + 1) * irz
if lsq(xn, yn, zn) > 1 then break end
local skip = false
if not filled then
if lsq(nextXn, yn, zn) <= 1 and lsq(xn, nextYn, zn) <= 1 and lsq(xn, yn, nextZn) <= 1 then
skip = true
end
end
if not skip then
for sx = -1, 1, 2 do
for sy = -1, 1, 2 do
for sz = -1, 1, 2 do
if (x ~= 0 or sx == 1) and (y ~= 0 or sy == 1) and (z ~= 0 or sz == 1) then
edit:place(cx + sx * x, cy + sy * y, cz + sz * z, pattern)
end
end
end
end
end
end
end
end
end
function L.cylinder(edit, cx, cy, cz, pattern, rx, rz, height, filled)
rx, rz = rx + 0.5, rz + 0.5
if height == 0 then return end
local irx, irz = 1 / rx, 1 / rz
local crx, crz = ceil(rx), ceil(rz)
local ys, ye = cy, cy + height - 1
if height < 0 then ys, ye = cy + height + 1, cy end
local nextXn = 0
for x = 0, crx do
local xn = nextXn
nextXn = (x + 1) * irx
local nextZn = 0
for z = 0, crz do
local zn = nextZn
nextZn = (z + 1) * irz
if lsq(xn, 0, zn) > 1 then break end
local skip = false
if not filled then
if lsq(nextXn, 0, zn) <= 1 and lsq(xn, 0, nextZn) <= 1 then skip = true end
end
if not skip then
for sx = -1, 1, 2 do
for sz = -1, 1, 2 do
if (x ~= 0 or sx == 1) and (z ~= 0 or sz == 1) then
for y = ys, ye do
edit:place(cx + sx * x, y, cz + sz * z, pattern)
end
end
end
end
end
end
end
end
function L.cone(edit, cx, cy, cz, pattern, rx, rz, height, filled)
for h = 0, height - 1 do
local f = 1 - h / height
L.cylinder(edit, cx, cy + h, cz, pattern, max(0, rx * f), max(0, rz * f), 1, filled)
end
end
function L.pyramid(edit, cx, cy, cz, pattern, size, filled)
local height = size
for y = 0, height do
size = size - 1
for x = 0, size do
for z = 0, size do
if (filled and z <= size and x <= size) or z == size or x == size then
edit:place(cx + x, cy + y, cz + z, pattern)
edit:place(cx - x, cy + y, cz + z, pattern)
edit:place(cx + x, cy + y, cz - z, pattern)
edit:place(cx - x, cy + y, cz - z, pattern)
end
end
end
end
end
--- Line through block space; thickness > 0 puts a sphere of that radius on every point.
function L.line(edit, a, b, pattern, radius, filled)
local dx, dy, dz = b[1] - a[1], b[2] - a[2], b[3] - a[3]
local steps = max(abs(dx), abs(dy), abs(dz))
local seen = {}
for i = 0, steps do
local t = steps == 0 and 0 or i / steps
local x = floor(a[1] + dx * t + 0.5)
local y = floor(a[2] + dy * t + 0.5)
local z = floor(a[3] + dz * t + 0.5)
if radius and radius > 0 then
L.sphere(edit, x, y, z, pattern, radius, radius, radius, filled ~= false)
else
edit:place(x, y, z, pattern)
end
end
end
-- ============================================================== block helpers
local function is_air(id) return id == 0 end
local LIQUID = { [8] = true, [9] = true, [10] = true, [11] = true }
function L.is_liquid(id) return LIQUID[id] == true end
function L.is_air(id) return is_air(id) end
--- Top block (y) that is not air within [y0, y1] for the column, or nil
function L.top_existing(x, z, y0, y1)
for y = y1, y0, -1 do
if get_block(x, y, z) ~= 0 then return y end
end
return nil
end
--- Top solid (non liquid/plant) block y in the column
local PLANTS = { [6] = true, [31] = true, [37] = true, [38] = true, [39] = true, [40] = true, [50] = true,
[51] = true, [59] = true, [83] = true, [78] = true, [81] = false, [18] = false }
function L.top_ground(x, z, y0, y1)
for y = y1, y0, -1 do
local id = get_block(x, y, z)
if id ~= 0 and not LIQUID[id] and not PLANTS[id] and id ~= 17 and id ~= 18 then return y end
end
return nil
end
-- ======================================================== rotation of block data
-- data values that point in a direction, rotated 90 degrees clockwise (y axis)
local ROT = {
stairs = { [0] = 2, [1] = 3, [2] = 1, [3] = 0 }, -- east,west,south,north
torch = { [1] = 3, [2] = 4, [3] = 2, [4] = 1 },
ladder = { [2] = 5, [3] = 4, [4] = 2, [5] = 3 }, -- chest, furnace, ladder, signs
door = { [0] = 1, [1] = 2, [2] = 3, [3] = 0 },
trapdoor = { [0] = 3, [1] = 2, [2] = 0, [3] = 1 },
}
local KIND = {
[53] = "stairs", [67] = "stairs", [108] = "stairs", [109] = "stairs", [114] = "stairs", [128] = "stairs",
[156] = "stairs",
[50] = "torch", [75] = "torch", [76] = "torch",
[65] = "ladder", [54] = "ladder", [61] = "ladder", [62] = "ladder", [68] = "ladder", [86] = "ladder",
[91] = "ladder",
[64] = "door", [71] = "door", [107] = "door",
[96] = "trapdoor",
}
function L.rotate_data(id, data, turns)
local kind = KIND[id]
if not kind then return data end
local map = ROT[kind]
turns = turns % 4
for _ = 1, turns do
local flags, d = 0, data
if kind == "stairs" then
flags = bit.band(d, 4); d = bit.band(d, 3)
elseif kind == "door" or kind == "trapdoor" then
flags = bit.band(d, bit.bnot(3)); d = bit.band(d, 3)
end
d = map[d] or d
data = bit.bor(d, flags)
end
return data
end
return L