summaryrefslogtreecommitdiff
path: root/builtin/common/misc_helpers.lua
diff options
context:
space:
mode:
authorkwolekr <kwolekr@minetest.net>2015-01-15 18:03:43 -0500
committerkwolekr <kwolekr@minetest.net>2015-01-15 18:05:13 -0500
commit5aeeb219e3fc2f37d3996a3df3b660afcc5f7599 (patch)
treea7b74445f76c6a7095e9f3d8ffea8af3727ad662 /builtin/common/misc_helpers.lua
parent9736548720a96c9c7f739edb0435d9ba4ad80652 (diff)
downloadminetest-5aeeb219e3fc2f37d3996a3df3b660afcc5f7599.tar.gz
minetest-5aeeb219e3fc2f37d3996a3df3b660afcc5f7599.tar.bz2
minetest-5aeeb219e3fc2f37d3996a3df3b660afcc5f7599.zip
Simplify deleteblocks chat command argument parsing
Add optional core.pos_to_string decimal place rounding Move core.string_to_pos to builtin/common/misc_helpers.lua for consistency
Diffstat (limited to 'builtin/common/misc_helpers.lua')
-rw-r--r--builtin/common/misc_helpers.lua41
1 files changed, 39 insertions, 2 deletions
diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua
index a86631e1d..deeba788e 100644
--- a/builtin/common/misc_helpers.lua
+++ b/builtin/common/misc_helpers.lua
@@ -498,10 +498,47 @@ function core.explode_scrollbar_event(evt)
end
--------------------------------------------------------------------------------
-function core.pos_to_string(pos)
- return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
+function core.pos_to_string(pos, decimal_places)
+ local x = pos.x
+ local y = pos.y
+ local z = pos.z
+ if decimal_places ~= nil then
+ x = string.format("%." .. decimal_places .. "f", x)
+ y = string.format("%." .. decimal_places .. "f", y)
+ z = string.format("%." .. decimal_places .. "f", z)
+ end
+ return "(" .. x .. "," .. y .. "," .. z .. ")"
+end
+
+--------------------------------------------------------------------------------
+function core.string_to_pos(value)
+ if value == nil then
+ return nil
+ end
+
+ local p = {}
+ p.x, p.y, p.z = string.match(value, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
+ if p.x and p.y and p.z then
+ p.x = tonumber(p.x)
+ p.y = tonumber(p.y)
+ p.z = tonumber(p.z)
+ return p
+ end
+ local p = {}
+ p.x, p.y, p.z = string.match(value, "^%( *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *%)$")
+ if p.x and p.y and p.z then
+ p.x = tonumber(p.x)
+ p.y = tonumber(p.y)
+ p.z = tonumber(p.z)
+ return p
+ end
+ return nil
end
+assert(core.string_to_pos("10.0, 5, -2").x == 10)
+assert(core.string_to_pos("( 10.0, 5, -2)").z == -2)
+assert(core.string_to_pos("asd, 5, -2)") == nil)
+
--------------------------------------------------------------------------------
function table.copy(t, seen)
local n = {}