diff options
Diffstat (limited to 'builtin/common')
-rw-r--r-- | builtin/common/chatcommands.lua | 45 | ||||
-rw-r--r-- | builtin/common/misc_helpers.lua | 93 | ||||
-rw-r--r-- | builtin/common/voxelarea.lua | 132 |
3 files changed, 270 insertions, 0 deletions
diff --git a/builtin/common/chatcommands.lua b/builtin/common/chatcommands.lua index 7c3da0601..817f1f526 100644 --- a/builtin/common/chatcommands.lua +++ b/builtin/common/chatcommands.lua @@ -69,6 +69,51 @@ function core.override_chatcommand(name, redefinition) core.registered_chatcommands[name] = chatcommand end +if INIT == "client" then + function core.register_list_command(command, desc, setting) + local def = {} + def.description = desc + def.params = "del <item> | add <item> | list" + function def.func(param) + local list = (minetest.settings:get(setting) or ""):split(",") + if param == "list" then + return true, table.concat(list, ", ") + else + local sparam = param:split(" ") + local cmd = sparam[1] + local item = sparam[2] + if cmd == "del" then + if not item then + return false, "Missing item." + end + local i = table.indexof(list, item) + if i == -1 then + return false, item .. " is not on the list." + else + table.remove(list, i) + core.settings:set(setting, table.concat(list, ",")) + return true, "Removed " .. item .. " from the list." + end + elseif cmd == "add" then + if not item then + return false, "Missing item." + end + local i = table.indexof(list, item) + if i ~= -1 then + return false, item .. " is already on the list." + else + table.insert(list, item) + core.settings:set(setting, table.concat(list, ",")) + return true, "Added " .. item .. " to the list." + end + end + end + return false, "Invalid usage. (See .help " .. command .. ")" + end + core.register_chatcommand(command, def) + end +end + local function format_help_line(cmd, def) local cmd_marker = INIT == "client" and "." or "/" local msg = core.colorize("#00ffff", cmd_marker .. cmd) diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index d2356b505..f8a905c7b 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -516,6 +516,17 @@ function table.shuffle(t, from, to, random) end +function table.combine(t, other) + other = other or {} + for k, v in pairs(other) do + if type(v) == "table" and type(t[k]) == "table" then + table.combine(t[k], v) + else + t[k] = v + end + end +end + -------------------------------------------------------------------------------- -- mainmenu only functions -------------------------------------------------------------------------------- @@ -583,6 +594,66 @@ function core.colorize(color, message) return table.concat(lines, "\n") .. core.get_color_escape_sequence("#ffffff") end +local function rgb_to_hex(rgb) + local hexadecimal = "#" + + for key, value in pairs(rgb) do + local hex = "" + + while(value > 0)do + local index = math.fmod(value, 16) + 1 + value = math.floor(value / 16) + hex = string.sub("0123456789ABCDEF", index, index) .. hex + end + + if(string.len(hex) == 0)then + hex = "00" + elseif(string.len(hex) == 1)then + hex = "0" .. hex + end + + hexadecimal = hexadecimal .. hex + end + + return hexadecimal +end + +local function color_from_hue(hue) + local h = hue / 60 + local c = 255 + local x = (1 - math.abs(h % 2 - 1)) * 255 + + local i = math.floor(h) + if i == 0 then + return rgb_to_hex({c, x, 0}) + elseif i == 1 then + return rgb_to_hex({x, c, 0}) + elseif i == 2 then + return rgb_to_hex({0, c, x}) + elseif i == 3 then + return rgb_to_hex({0, x, c}) + elseif i == 4 then + return rgb_to_hex({x, 0, c}) + else + return rgb_to_hex({c, 0, x}) + end +end + +function core.rainbow(input) + local step = 360 / input:len() + local hue = 0 + local output = "" + for i = 1, input:len() do + local char = input:sub(i, i) + if char:match("%s") then + output = output .. char + else + output = output .. core.get_color_escape_sequence(color_from_hue(hue)) .. char + end + hue = hue + step + end + return output +end function core.strip_foreground_colors(str) return (str:gsub(ESCAPE_CHAR .. "%(c@[^)]+%)", "")) @@ -636,6 +707,19 @@ function core.get_translator(textdomain) return function(str, ...) return core.translate(textdomain or "", str, ...) end end +function core.get_pointed_thing_position(pointed_thing, above) + if pointed_thing.type == "node" then + if above then + -- The position where a node would be placed + return pointed_thing.above + end + -- The position where a node would be dug + return pointed_thing.under + elseif pointed_thing.type == "object" then + return pointed_thing.ref and pointed_thing.ref:get_pos() + end +end + -------------------------------------------------------------------------------- -- Returns the exact coordinate of a pointed surface -------------------------------------------------------------------------------- @@ -768,3 +852,12 @@ function core.parse_coordinates(x, y, z, relative_to) local rz = core.parse_relative_number(z, relative_to.z) return rx and ry and rz and { x = rx, y = ry, z = rz } end + +function core.inventorycube(img1, img2, img3) + img2 = img2 or img1 + img3 = img3 or img1 + return "[inventorycube" + .. "{" .. img1:gsub("%^", "&") + .. "{" .. img2:gsub("%^", "&") + .. "{" .. img3:gsub("%^", "&") +end diff --git a/builtin/common/voxelarea.lua b/builtin/common/voxelarea.lua new file mode 100644 index 000000000..64436bf1a --- /dev/null +++ b/builtin/common/voxelarea.lua @@ -0,0 +1,132 @@ +VoxelArea = { + MinEdge = vector.new(1, 1, 1), + MaxEdge = vector.new(0, 0, 0), + ystride = 0, + zstride = 0, +} + +function VoxelArea:new(o) + o = o or {} + setmetatable(o, self) + self.__index = self + + local e = o:getExtent() + o.ystride = e.x + o.zstride = e.x * e.y + + return o +end + +function VoxelArea:getExtent() + local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge + return vector.new( + MaxEdge.x - MinEdge.x + 1, + MaxEdge.y - MinEdge.y + 1, + MaxEdge.z - MinEdge.z + 1 + ) +end + +function VoxelArea:getVolume() + local e = self:getExtent() + return e.x * e.y * e.z +end + +function VoxelArea:index(x, y, z) + local MinEdge = self.MinEdge + local i = (z - MinEdge.z) * self.zstride + + (y - MinEdge.y) * self.ystride + + (x - MinEdge.x) + 1 + return math.floor(i) +end + +function VoxelArea:indexp(p) + local MinEdge = self.MinEdge + local i = (p.z - MinEdge.z) * self.zstride + + (p.y - MinEdge.y) * self.ystride + + (p.x - MinEdge.x) + 1 + return math.floor(i) +end + +function VoxelArea:position(i) + local p = {} + local MinEdge = self.MinEdge + + i = i - 1 + + p.z = math.floor(i / self.zstride) + MinEdge.z + i = i % self.zstride + + p.y = math.floor(i / self.ystride) + MinEdge.y + i = i % self.ystride + + p.x = math.floor(i) + MinEdge.x + + return p +end + +function VoxelArea:contains(x, y, z) + local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge + return (x >= MinEdge.x) and (x <= MaxEdge.x) and + (y >= MinEdge.y) and (y <= MaxEdge.y) and + (z >= MinEdge.z) and (z <= MaxEdge.z) +end + +function VoxelArea:containsp(p) + local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge + return (p.x >= MinEdge.x) and (p.x <= MaxEdge.x) and + (p.y >= MinEdge.y) and (p.y <= MaxEdge.y) and + (p.z >= MinEdge.z) and (p.z <= MaxEdge.z) +end + +function VoxelArea:containsi(i) + return (i >= 1) and (i <= self:getVolume()) +end + +function VoxelArea:iter(minx, miny, minz, maxx, maxy, maxz) + local i = self:index(minx, miny, minz) - 1 + local xrange = maxx - minx + 1 + local nextaction = i + 1 + xrange + + local y = 0 + local yrange = maxy - miny + 1 + local yreqstride = self.ystride - xrange + + local z = 0 + local zrange = maxz - minz + 1 + local multistride = self.zstride - ((yrange - 1) * self.ystride + xrange) + + return function() + -- continue i until it needs to jump + i = i + 1 + if i ~= nextaction then + return i + end + + -- continue y until maxy is exceeded + y = y + 1 + if y ~= yrange then + -- set i to index(minx, miny + y, minz + z) - 1 + i = i + yreqstride + nextaction = i + xrange + return i + end + + -- continue z until maxz is exceeded + z = z + 1 + if z == zrange then + -- cuboid finished, return nil + return + end + + -- set i to index(minx, miny, minz + z) - 1 + i = i + multistride + + y = 0 + nextaction = i + xrange + return i + end +end + +function VoxelArea:iterp(minp, maxp) + return self:iter(minp.x, minp.y, minp.z, maxp.x, maxp.y, maxp.z) +end |