From: Elias Fleckenstein Date: Thu, 2 Jun 2022 18:54:02 +0000 (+0200) Subject: Merge branch 'master' of https://github.com/minetest/minetest X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=827b9f8d7054158b058679999d77c1345162a293;p=dragonfireclient.git Merge branch 'master' of https://github.com/minetest/minetest --- 827b9f8d7054158b058679999d77c1345162a293 diff --cc builtin/common/misc_helpers.lua index 542b2040d,d2356b505..f8a905c7b --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@@ -786,11 -701,70 +785,79 @@@ function core.is_nan(number return number ~= number end + --[[ Helper function for parsing an optionally relative number + of a chat command parameter, using the chat command tilde notation. + + Parameters: + * arg: String snippet containing the number; possible values: + * "": return as number + * "~": return relative_to + + * "~": return relative_to + * Anything else will return `nil` + * relative_to: Number to which the `arg` number might be relative to + + Returns: + A number or `nil`, depending on `arg. + + Examples: + * `core.parse_relative_number("5", 10)` returns 5 + * `core.parse_relative_number("~5", 10)` returns 15 + * `core.parse_relative_number("~", 10)` returns 10 + ]] + function core.parse_relative_number(arg, relative_to) + if not arg then + return nil + elseif arg == "~" then + return relative_to + elseif string.sub(arg, 1, 1) == "~" then + local number = tonumber(string.sub(arg, 2)) + if not number then + return nil + end + if core.is_nan(number) or number == math.huge or number == -math.huge then + return nil + end + return relative_to + number + else + local number = tonumber(arg) + if core.is_nan(number) or number == math.huge or number == -math.huge then + return nil + end + return number + end + end + + --[[ Helper function to parse coordinates that might be relative + to another position; supports chat command tilde notation. + Intended to be used in chat command parameter parsing. + + Parameters: + * x, y, z: Parsed x, y, and z coordinates as strings + * relative_to: Position to which to compare the position + + Syntax of x, y and z: + * "": return as number + * "~": return + player position on this axis + * "~": return player position on this axis + + Returns: a vector or nil for invalid input or if player does not exist + ]] + function core.parse_coordinates(x, y, z, relative_to) + if not relative_to then + x, y, z = tonumber(x), tonumber(y), tonumber(z) + return x and y and z and { x = x, y = y, z = z } + end + local rx = core.parse_relative_number(x, relative_to.x) + local ry = core.parse_relative_number(y, relative_to.y) + 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