code cleanup and several new commands
parent
e135282330
commit
a47859c5e1
18
README.md
18
README.md
|
@ -28,6 +28,18 @@ usage
|
||||||
* .wp_unset NAME: remove a waypoint
|
* .wp_unset NAME: remove a waypoint
|
||||||
* .wp_list: list all waypoints
|
* .wp_list: list all waypoints
|
||||||
* .tw NAME: teleport to a waypoint
|
* .tw NAME: teleport to a waypoint
|
||||||
* .tw PLAYERNAME WAYPOINT_NAME: teleport another player to a waypoint
|
|
||||||
* .tr: teleport to a random location
|
* .tw_push NAME: teleport to a waypoint and save current location
|
||||||
* .tr ELEVATION: teleport to a random location at a given elevation (y value)
|
* .wp_push PLAYER/POSITION: teleport to a player or position and save current location
|
||||||
|
|
||||||
|
* .tw_pop; return to the last saved location
|
||||||
|
* .wp_pop: return to the last saved location
|
||||||
|
|
||||||
|
* .wp_stack: shows the locations in the stack
|
||||||
|
|
||||||
|
* .wp_stack_clear: clears the stack
|
||||||
|
|
||||||
|
* .wp_search DELTA: searches for saved waypoints near te current location with the given delta for all coordinates
|
||||||
|
* .wp_search: searches for saved waypoints near te current location with DELTA=10
|
||||||
|
|
||||||
|
|
||||||
|
|
231
init.lua
231
init.lua
|
@ -1,3 +1,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local mod_name = minetest.get_current_modname()
|
local mod_name = minetest.get_current_modname()
|
||||||
|
|
||||||
local function log(level, message)
|
local function log(level, message)
|
||||||
|
@ -9,6 +13,14 @@ log('action', 'CSM loading...')
|
||||||
local mod_storage = minetest.get_mod_storage()
|
local mod_storage = minetest.get_mod_storage()
|
||||||
|
|
||||||
|
|
||||||
|
local search_delta_default = 10
|
||||||
|
|
||||||
|
--
|
||||||
|
--
|
||||||
|
-- local functions
|
||||||
|
--
|
||||||
|
--
|
||||||
|
|
||||||
local function load_waypoints()
|
local function load_waypoints()
|
||||||
if string.find(mod_storage:get_string('waypoints'), 'return') then
|
if string.find(mod_storage:get_string('waypoints'), 'return') then
|
||||||
return minetest.deserialize(mod_storage:get_string('waypoints'))
|
return minetest.deserialize(mod_storage:get_string('waypoints'))
|
||||||
|
@ -17,6 +29,17 @@ local function load_waypoints()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function load_waypoints_stack()
|
||||||
|
if string.find(mod_storage:get_string('waypoints_stack'), 'return nil') then
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
if string.find(mod_storage:get_string('waypoints_stack'), 'return') then
|
||||||
|
return minetest.deserialize(mod_storage:get_string('waypoints_stack'))
|
||||||
|
else
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local waypoints = load_waypoints()
|
local waypoints = load_waypoints()
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,12 +59,7 @@ end
|
||||||
|
|
||||||
|
|
||||||
local function round(x)
|
local function round(x)
|
||||||
-- approved by kahan
|
return math.floor(x+0.5)
|
||||||
-- if x % 2 ~= 0.5 then
|
|
||||||
return math.floor(x+0.5)
|
|
||||||
-- else
|
|
||||||
-- return x - 0.5
|
|
||||||
-- end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,6 +91,93 @@ local function tostring_point(point)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
local function teleport_to(position_name)
|
||||||
|
local wpname = position_name
|
||||||
|
local waypoint = waypoints[wpname]
|
||||||
|
if waypoint ~= nil then
|
||||||
|
minetest.run_server_chatcommand('teleport', tostring_point(waypoint))
|
||||||
|
else
|
||||||
|
minetest.display_chat_message(('waypoint "%s" not found.'):format(wpname))
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
local function stack_push()
|
||||||
|
local point = minetest.localplayer:get_pos()
|
||||||
|
wp_stack = load_waypoints_stack()
|
||||||
|
count = #wp_stack +1
|
||||||
|
wp_stack[count] = point
|
||||||
|
mod_storage:set_string('waypoints_stack', minetest.serialize(wp_stack))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function stack_pop()
|
||||||
|
wp_stack = load_waypoints_stack()
|
||||||
|
count = 0
|
||||||
|
if nil ~= wp_stack then count = #wp_stack end
|
||||||
|
if count<1 then
|
||||||
|
minetest.display_chat_message('stack empty - no teleporting')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
minetest.run_server_chatcommand('teleport', tostring_point(wp_stack[count]))
|
||||||
|
wp_stack[count] = nil
|
||||||
|
mod_storage:set_string('waypoints_stack', minetest.serialize(wp_stack))
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
local function stack_show()
|
||||||
|
wp_stack = load_waypoints_stack()
|
||||||
|
count = 0
|
||||||
|
if nil ~= wp_stack then count = #wp_stack end
|
||||||
|
if count<1 then
|
||||||
|
minetest.display_chat_message('stack empty')
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
output = ""
|
||||||
|
for i = count,1,-1 do
|
||||||
|
output = output .. tostring(i) .. " " .. tostring_point(wp_stack[i]).."\n"
|
||||||
|
end
|
||||||
|
return true ,output
|
||||||
|
end
|
||||||
|
|
||||||
|
local function stack_clear()
|
||||||
|
mod_storage:set_string('waypoints_stack', minetest.serialize(nil))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function stack_search(d)
|
||||||
|
local delta = d
|
||||||
|
if delta then delta = tonumber(delta) end
|
||||||
|
if nil == delta then delta = search_delta_default end
|
||||||
|
if delta < 0 then delta = 0 end
|
||||||
|
|
||||||
|
here = minetest.localplayer:get_pos()
|
||||||
|
minetest.display_chat_message(
|
||||||
|
('%s : %s'):format("current position", tostring_point(here))
|
||||||
|
)
|
||||||
|
for name,pos in pairsByKeys(waypoints, lc_cmp) do
|
||||||
|
if math.abs(here.y-pos.y) <= delta then
|
||||||
|
if math.abs(here.x-pos.x) <= delta then
|
||||||
|
if math.abs(here.z-pos.z) <= delta then
|
||||||
|
minetest.display_chat_message(
|
||||||
|
('%s -> %s'):format(name, tostring_point(pos)))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
--
|
||||||
|
-- chat commands
|
||||||
|
--
|
||||||
|
--
|
||||||
|
|
||||||
|
|
||||||
minetest.register_chatcommand('wp_set', {
|
minetest.register_chatcommand('wp_set', {
|
||||||
params = '<name>',
|
params = '<name>',
|
||||||
description = 'set a waypoint',
|
description = 'set a waypoint',
|
||||||
|
@ -118,59 +223,73 @@ minetest.register_chatcommand('wp_list', {
|
||||||
|
|
||||||
|
|
||||||
minetest.register_chatcommand('tw', {
|
minetest.register_chatcommand('tw', {
|
||||||
params = '(<playernamename>) <waypoint>',
|
params = '<waypoint>',
|
||||||
description = 'teleport (a player) to a waypoint',
|
description = 'teleport to a waypoint',
|
||||||
func = safe(function(param)
|
func = safe(function(param)
|
||||||
-- local playername, wpname = string.match(param, '^(%S+)%s+(%S+)$')
|
safe(teleport_to(param))
|
||||||
-- if playername and wpname then
|
end),
|
||||||
-- local waypoint = waypoints[wpname]
|
}
|
||||||
-- if waypoint ~= nil then
|
)
|
||||||
-- local args = ('%s %s'):format(playername, tostring_point(waypoint))
|
|
||||||
-- minetest.run_server_chatcommand('teleport', args)
|
|
||||||
-- else
|
|
||||||
-- minetest.display_chat_message(('waypoint "%s" not found.'):format(wpname))
|
|
||||||
-- end
|
|
||||||
-- else
|
|
||||||
local wpname = param
|
|
||||||
local waypoint = waypoints[wpname]
|
|
||||||
if waypoint ~= nil then
|
|
||||||
minetest.run_server_chatcommand('teleport', tostring_point(waypoint))
|
|
||||||
else
|
|
||||||
minetest.display_chat_message(('waypoint "%s" not found.'):format(wpname))
|
|
||||||
end
|
|
||||||
-- end
|
|
||||||
|
|
||||||
end),
|
|
||||||
})
|
minetest.register_chatcommand('tw_push', {
|
||||||
|
params = '<waypoint>',
|
||||||
|
description = 'teleport to a waypoint and save old position',
|
||||||
|
func = safe(function(param)
|
||||||
|
stack_push()
|
||||||
|
safe(teleport_to(param))
|
||||||
|
end),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
minetest.register_chatcommand('wp_push', {
|
||||||
|
params = '<position/player>',
|
||||||
|
description = 'teleport to a position/player and save old position',
|
||||||
|
func = safe(function(param)
|
||||||
|
stack_push()
|
||||||
|
minetest.run_server_chatcommand('teleport', param)
|
||||||
|
end),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--
|
minetest.register_chatcommand('tw_pop', {
|
||||||
-- 2019-07-11 Och_Noe disabled
|
params = '',
|
||||||
-- random teleporting is too dangerous
|
description = 'return to the last saved position',
|
||||||
--
|
func = stack_pop,
|
||||||
-- minetest.register_chatcommand('tr', {
|
}
|
||||||
-- params = '<ELEV> | <PLAYER> | <PLAYER> <ELEV>',
|
)
|
||||||
-- description = '/teleport (a player) to a random location',
|
|
||||||
-- func = safe(function(param)
|
minetest.register_chatcommand('wp_pop', {
|
||||||
-- local x = math.random(-30912, 30927)
|
params = '',
|
||||||
-- local y = math.random(-30912, 30927)
|
description = 'return to the last saved position',
|
||||||
-- local z = math.random(-30912, 30927)
|
func = stack_pop,
|
||||||
-- local name = ''
|
}
|
||||||
--
|
)
|
||||||
-- if string.match(param, '^([%a%d_-]+) (%d+)$') ~= nil then
|
|
||||||
-- name, y = string.match(param, "^([%a%d_-]+) (%d+)$")
|
minetest.register_chatcommand('wp_stack', {
|
||||||
--
|
params = '',
|
||||||
-- elseif string.match(param, '^([%d-]+)$') then
|
description = 'shows the stack content',
|
||||||
-- y = string.match(param, '^([%d-]+)$')
|
func = stack_show,
|
||||||
--
|
}
|
||||||
-- elseif string.match(param, '^([%a%d_-]+)$') ~= nil then
|
)
|
||||||
-- name = string.match(param, '^([%a%d_-]+)$')
|
|
||||||
-- end
|
minetest.register_chatcommand('wp_stack_clear', {
|
||||||
--
|
params = '',
|
||||||
-- local args = ('%s %s %s %s'):format(name, x, y, z)
|
description = 'clears the position stack',
|
||||||
-- minetest.run_server_chatcommand('teleport', args)
|
func = stack_clear,
|
||||||
-- end),
|
}
|
||||||
-- })
|
)
|
||||||
|
|
||||||
|
|
||||||
|
minetest.register_chatcommand('wp_search', {
|
||||||
|
params = '(<delta>)',
|
||||||
|
description = 'search a waypoint near the current position',
|
||||||
|
func = stack_search,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue