1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
local mod_name = minetest.get_current_modname()
local function log(level, message)
minetest.log(level, ('[%s] %s'):format(mod_name, message))
end
log('action', 'CSM loading...')
local mod_storage = minetest.get_mod_storage()
local function load_waypoints()
if string.find(mod_storage:get_string('waypoints'), 'return') then
return minetest.deserialize(mod_storage:get_string('waypoints'))
else
return {}
end
end
local waypoints = load_waypoints()
local function safe(func)
-- wrap a function w/ logic to avoid crashing the game
local f = function(...)
local status, out = pcall(func, ...)
if status then
return out
else
log('warning', 'Error (func): ' .. out)
return nil
end
end
return f
end
local function round(x)
-- approved by kahan
-- if x % 2 ~= 0.5 then
return math.floor(x+0.5)
-- else
-- return x - 0.5
-- end
end
local function pairsByKeys(t, f)
local a = {}
for n in pairs(t) do
table.insert(a, n)
end
table.sort(a, f)
local i = 0
return function()
i = i + 1
if a[i] == nil then
return nil
else
return a[i], t[a[i]]
end
end
end
local function lc_cmp(a, b)
return a:lower() < b:lower()
end
local function tostring_point(point)
return ('%i %i.5 %i'):format(round(point.x), round(point.y), round(point.z))
end
minetest.register_chatcommand('wp_set', {
params = '<name>',
description = 'set a waypoint',
func = safe(function(param)
waypoints = load_waypoints()
local point = minetest.localplayer:get_pos()
waypoints[param] = point
mod_storage:set_string('waypoints', minetest.serialize(waypoints))
minetest.display_chat_message(
('set waypoint "%s" to "%s"'):format(param, tostring_point(point))
)
end),
})
minetest.register_chatcommand('wp_unset', {
params = '<name>',
description = 'remove a waypoint',
func = safe(function(param)
waypoints = load_waypoints()
waypoints[param] = nil
mod_storage:set_string('waypoints', minetest.serialize(waypoints))
minetest.display_chat_message(
('removed waypoint "%s"'):format(param)
)
end),
})
minetest.register_chatcommand('wp_list', {
params = '',
description = 'lists waypoints',
func = safe(function(_)
for name, point in pairsByKeys(waypoints, lc_cmp) do
minetest.display_chat_message(
('%s -> %s'):format(name, tostring_point(point))
)
end
end),
})
minetest.register_chatcommand('tw', {
params = '(<playernamename>) <waypoint>',
description = 'teleport (a player) to a waypoint',
func = safe(function(param)
-- local playername, wpname = string.match(param, '^(%S+)%s+(%S+)$')
-- if playername and wpname then
-- 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),
})
--
-- 2019-07-11 Och_Noe disabled
-- random teleporting is too dangerous
--
-- minetest.register_chatcommand('tr', {
-- params = '<ELEV> | <PLAYER> | <PLAYER> <ELEV>',
-- description = '/teleport (a player) to a random location',
-- func = safe(function(param)
-- local x = math.random(-30912, 30927)
-- local y = math.random(-30912, 30927)
-- local z = math.random(-30912, 30927)
-- local name = ''
--
-- if string.match(param, '^([%a%d_-]+) (%d+)$') ~= nil then
-- name, y = string.match(param, "^([%a%d_-]+) (%d+)$")
--
-- elseif string.match(param, '^([%d-]+)$') then
-- y = string.match(param, '^([%d-]+)$')
--
-- elseif string.match(param, '^([%a%d_-]+)$') ~= nil then
-- name = string.match(param, '^([%a%d_-]+)$')
-- end
--
-- local args = ('%s %s %s %s'):format(name, x, y, z)
-- minetest.run_server_chatcommand('teleport', args)
-- end),
-- })
|