File: init.lua

package info (click to toggle)
minetest-mod-advmarkers-csm 0.0~git20220709.a6f6170-2
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 92 kB
  • sloc: makefile: 2
file content (500 lines) | stat: -rw-r--r-- 16,603 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
--
-- Minetest advmarkers CSM
--
-- Copyright © 2019-2020 by luk3yx
--

advmarkers = {}

-- Get the mod storage
local storage = minetest.get_mod_storage()

-- Convert positions to/from strings
local abs, type = math.abs, type
local function pos_to_string(pos)
    if type(pos) == 'table' then
        pos = minetest.pos_to_string(vector.round(pos))
    end
    if type(pos) == 'string' then
        return pos
    end
end

local function dir_ok(n)
    return type(n) == 'number' and abs(n) < 31000
end
local function string_to_pos(pos)
    if type(pos) == 'string' then
        pos = minetest.string_to_pos(pos)
    end
    if type(pos) == 'table' and dir_ok(pos.x) and dir_ok(pos.y) and
            dir_ok(pos.z) then
        return vector.round(pos)
    end
end

-- Set the HUD position
local hud_id
function advmarkers.set_hud_pos(pos, title)
    pos = string_to_pos(pos)
    if not pos then return end

    -- Fall back to /mrkr if hud_add doesn't exist (Minetest 0.4).
    if not minetest.localplayer or not minetest.localplayer.hud_add or
            not minetest.localplayer.hud_change then
        minetest.run_server_chatcommand('mrkr', tostring(pos.x) .. ' ' ..
            tostring(pos.y) .. ' ' .. tostring(pos.z))
        return
    end

    if not title then
        title = pos.x .. ', ' .. pos.y .. ', ' .. pos.z
    end
    if hud_id then
        minetest.localplayer:hud_change(hud_id, 'name', title)
        minetest.localplayer:hud_change(hud_id, 'world_pos', pos)
    else
        hud_id = minetest.localplayer:hud_add({
            hud_elem_type = 'waypoint',
            name          = title,
            text          = 'm',
            number        = 0x00ffff,
            world_pos     = pos
        })
    end
    minetest.display_chat_message('Waypoint set to ' ..
        minetest.colorize('#00ffff', title))
    return true
end

-- Add a waypoint
function advmarkers.set_waypoint(pos, name)
    pos = pos_to_string(pos)
    if not pos then return end
    storage:set_string('marker-' .. tostring(name), pos)
    return true
end
advmarkers.set_marker = advmarkers.set_waypoint

-- Delete a waypoint
function advmarkers.delete_waypoint(name)
    storage:set_string('marker-' .. tostring(name), '')
end
advmarkers.delete_marker = advmarkers.delete_waypoint

-- Get a waypoint
function advmarkers.get_waypoint(name)
    return string_to_pos(storage:get_string('marker-' .. tostring(name)))
end
advmarkers.get_marker = advmarkers.get_waypoint

-- Rename a waypoint and re-interpret the position.
function advmarkers.rename_waypoint(oldname, newname)
    oldname, newname = tostring(oldname), tostring(newname)
    local pos = advmarkers.get_waypoint(oldname)
    if not pos or not advmarkers.set_waypoint(pos, newname) then return end
    if oldname ~= newname then
        advmarkers.delete_waypoint(oldname)
    end
    return true
end
advmarkers.rename_marker = advmarkers.rename_waypoint

-- Display a waypoint
function advmarkers.display_waypoint(name)
    return advmarkers.set_hud_pos(advmarkers.get_waypoint(name), name)
end
advmarkers.display_marker = advmarkers.display_waypoint

-- Export waypoints
function advmarkers.export(raw)
    local s = storage:to_table().fields
    if raw == 'M' then
        s = minetest.compress(minetest.serialize(s))
        s = 'M' .. minetest.encode_base64(s)
    elseif not raw then
        s = minetest.compress(minetest.write_json(s))
        s = 'J' .. minetest.encode_base64(s)
    end
    return s
end

-- Import waypoints
function advmarkers.import(s)
    if type(s) ~= 'table' then
        local ver = s:sub(1, 1)
        if ver ~= 'M' and ver ~= 'J' then return end
        s = minetest.decode_base64(s:sub(2))
        local success, msg = pcall(minetest.decompress, s)
        if not success then return end
        if ver == 'M' then
            s = minetest.deserialize(msg, true)
        else
            s = minetest.parse_json(msg)
        end
    end

    -- Iterate over waypoints to preserve existing ones and check for errors.
    if type(s) == 'table' then
        for name, pos in pairs(s) do
            if type(name) == 'string' and type(pos) == 'string' and
              name:sub(1, 7) == 'marker-' and minetest.string_to_pos(pos) and
              storage:get_string(name) ~= pos then
                -- Prevent collisions
                local c = 0
                while #storage:get_string(name) > 0 and c < 50 do
                    name = name .. '_'
                    c = c + 1
                end

                -- Sanity check
                if c < 50 then
                    storage:set_string(name, pos)
                end
            end
        end
        return true
    end
end

-- Get the waypoints formspec
local formspec_list = {}
local selected_name = false
function advmarkers.display_formspec()
    local formspec = 'size[5.25,8]' ..
                     'label[0,0;Waypoint list]' ..
                     'button_exit[0,7.5;1.3125,0.5;display;Display]' ..
                     'button[1.3125,7.5;1.3125,0.5;teleport;Teleport]' ..
                     'button[2.625,7.5;1.3125,0.5;rename;Rename]' ..
                     'button[3.9375,7.5;1.3125,0.5;delete;Delete]' ..
                     'textlist[0,0.75;5,6;marker;'

    -- Iterate over all the waypoints
    local selected = 1
    formspec_list = {}

    local waypoints = {}
    for name, _ in pairs(storage:to_table().fields) do
        if name:sub(1, 7) == 'marker-' then
            table.insert(waypoints, name:sub(8))
        end
    end
    table.sort(waypoints)

    for id, name in ipairs(waypoints) do
        if id > 1 then
            formspec = formspec .. ','
        end
        if not selected_name then
            selected_name = name
        end
        if name == selected_name then
            selected = id
        end
        formspec_list[#formspec_list + 1] = name
        formspec = formspec .. '##' .. minetest.formspec_escape(name)
    end

    -- Close the text list and display the selected waypoint position
    formspec = formspec .. ';' .. tostring(selected) .. ']'
    if selected_name then
        local pos = advmarkers.get_waypoint(selected_name)
        if pos then
            pos = minetest.formspec_escape(tostring(pos.x) .. ', ' ..
            tostring(pos.y) .. ', ' .. tostring(pos.z))
            pos = 'Waypoint position: ' .. pos
            formspec = formspec .. 'label[0,6.75;' .. pos .. ']'
        end
    else
        -- Draw over the buttons
        formspec = formspec .. 'button_exit[0,7.5;5.25,0.5;quit;Close dialog]' ..
            'label[0,6.75;No waypoints. Add one with ".add_mrkr".]'
    end

    -- Display the formspec
    return minetest.show_formspec('advmarkers-csm', formspec)
end

function advmarkers.get_chatcommand_pos(pos)
    if pos == 'h' or pos == 'here' then
        pos = minetest.localplayer:get_pos()
    elseif pos == 't' or pos == 'there' then
        if not advmarkers.last_coords then
            return false, 'No "there" position found!'
        end
        pos = advmarkers.last_coords
    else
        pos = string_to_pos(pos)
        if not pos then
            return false, 'Invalid position!'
        end
    end
    return pos
end

local function register_chatcommand_alias(old, ...)
    local def = assert(minetest.registered_chatcommands[old])
    def.name = nil
    for i = 1, select('#', ...) do
        minetest.register_chatcommand(select(i, ...), table.copy(def))
    end
end

-- Open the waypoints GUI
minetest.register_chatcommand('mrkr', {
    params      = '',
    description = 'Open the advmarkers GUI',
    func = function(param)
        if param == '' then
            advmarkers.display_formspec()
        else
            local pos, err = advmarkers.get_chatcommand_pos(param)
            if not pos then
                return false, err
            end
            if not advmarkers.set_hud_pos(pos) then
                return false, 'Error setting the waypoint!'
            end
        end
    end
})

register_chatcommand_alias('mrkr', 'wp', 'wps', 'waypoint', 'waypoints')

-- Add a waypoint
minetest.register_chatcommand('add_mrkr', {
    params      = '<pos / "here" / "there"> <name>',
    description = 'Adds a waypoint.',
    func = function(param)
        local s, e = param:find(' ')
        if not s or not e then
            return false, 'Invalid syntax! See .help add_mrkr for more info.'
        end
        local pos = param:sub(1, s - 1)
        local name = param:sub(e + 1)

        -- Validate the position
        local pos, err = advmarkers.get_chatcommand_pos(pos)
        if not pos then
            return false, err
        end

        -- Validate the name
        if not name or #name < 1 then
            return false, 'Invalid name!'
        end

        -- Set the waypoint
        return advmarkers.set_waypoint(pos, name), 'Done!'
    end
})

register_chatcommand_alias('add_mrkr', 'add_wp', 'add_waypoint')

-- Set the HUD
minetest.register_on_formspec_input(function(formname, fields)
    if formname == 'advmarkers-ignore' then
        return true
    elseif formname ~= 'advmarkers-csm' then
        return
    end
    local name = false
    if fields.marker then
        local event = minetest.explode_textlist_event(fields.marker)
        if event.index then
            name = formspec_list[event.index]
        end
    else
        name = selected_name
    end

    if name then
        if fields.display then
            if not advmarkers.display_waypoint(name) then
                minetest.display_chat_message('Error displaying waypoint!')
            end
        elseif fields.rename then
            minetest.show_formspec('advmarkers-csm', 'size[6,3]' ..
                'label[0.35,0.2;Rename waypoint]' ..
                'field[0.3,1.3;6,1;new_name;New name;' ..
                minetest.formspec_escape(name) .. ']' ..
                'button[0,2;3,1;cancel;Cancel]' ..
                'button[3,2;3,1;rename_confirm;Rename]')
        elseif fields.rename_confirm then
            if fields.new_name and #fields.new_name > 0 then
                if advmarkers.rename_waypoint(name, fields.new_name) then
                    selected_name = fields.new_name
                else
                    minetest.display_chat_message('Error renaming waypoint!')
                end
                advmarkers.display_formspec()
            else
                minetest.display_chat_message(
                    'Please enter a new name for the marker.'
                )
            end
        elseif fields.teleport then
            minetest.show_formspec('advmarkers-csm', 'size[6,2.2]' ..
                'label[0.35,0.25;' .. minetest.formspec_escape(
                    'Teleport to a waypoint\n - ' .. name
                ) .. ']' ..
                'button[0,1.25;2,1;cancel;Cancel]' ..
                'button_exit[2,1.25;1,1;teleport_tpj;/tpj]' ..
                'button_exit[3,1.25;1,1;teleport_tpc;/tpc]' ..
                'button_exit[4,1.25;2,1;teleport_confirm;/teleport]')
        elseif fields.teleport_tpj then
            -- Teleport with /tpj
            local pos = advmarkers.get_waypoint(name)
            if pos and minetest.localplayer then
                local cpos = minetest.localplayer:get_pos()
                for _, dir in ipairs({'x', 'y', 'z'}) do
                    local distance = pos[dir] - cpos[dir]
                    minetest.run_server_chatcommand('tpj', dir .. ' ' ..
                        tostring(distance))
                end
            else
                minetest.display_chat_message('Error teleporting to waypoint!')
            end
        elseif fields.teleport_confirm or fields.teleport_tpc then
            -- Teleport with /teleport
            local pos = advmarkers.get_waypoint(name)
            local cmd
            if fields.teleport_confirm then
                cmd = 'teleport'
            else
                cmd = 'tpc'
            end
            if pos and minetest.localplayer then
                minetest.run_server_chatcommand(cmd,
                    pos.x .. ',' .. pos.y .. ',' .. pos.z)
            else
                minetest.display_chat_message('Error teleporting to waypoint!')
            end
        elseif fields.delete then
            minetest.show_formspec('advmarkers-csm', 'size[6,2]' ..
                'label[0.35,0.25;Are you sure you want to delete this waypoint?]' ..
                'button[0,1;3,1;cancel;Cancel]' ..
                'button[3,1;3,1;delete_confirm;Delete]')
        elseif fields.delete_confirm then
            advmarkers.delete_waypoint(name)
            selected_name = false
            advmarkers.display_formspec()
        elseif fields.cancel then
            advmarkers.display_formspec()
        elseif name ~= selected_name then
            selected_name = name
            advmarkers.display_formspec()
        end
    elseif fields.display or fields.delete then
        minetest.display_chat_message('Please select a waypoint.')
    end
    return true
end)

-- Auto-add waypoints on death.
minetest.register_on_death(function()
    if minetest.localplayer then
        local name = 'Death waypoint'
        local pos  = minetest.localplayer:get_pos()
        advmarkers.last_coords = pos
        advmarkers.set_waypoint(pos, name)
        minetest.display_chat_message('Added waypoint "' .. name .. '".')
    end
end)

-- Allow string exporting
minetest.register_chatcommand('mrkr_export', {
    params      = '[old]',
    description = 'Exports an advmarkers string containing all your markers.',
    func = function(param)
        local export
        if param == 'old' then
            export = advmarkers.export('M')
        else
            export = advmarkers.export()
        end
        minetest.show_formspec('advmarkers-ignore',
            'field[_;Your waypoint export string;' ..
            minetest.formspec_escape(export) .. ']')
    end
})

register_chatcommand_alias('mrkr_export', 'wp_export', 'waypoint_export')

-- String importing
minetest.register_chatcommand('mrkr_import', {
    params      = '<advmarkers string>',
    description = 'Imports an advmarkers string. This will not overwrite ' ..
        'existing markers that have the same name.',
    func = function(param)
        if advmarkers.import(param) then
            return true, 'Waypoints imported!'
        else
            return false, 'Invalid advmarkers string!'
        end
    end
})

register_chatcommand_alias('mrkr_export', 'wp_import', 'waypoint_import')

-- Upload waypoints to the advmarkers server-side mod.
minetest.register_chatcommand('mrkr_upload', {
    params      = '',
    description = 'Uploads all waypoints to this server\'s advmarkers storage.',
    func = function(param)
        local data = advmarkers.export()
        minetest.run_server_chatcommand('mrkr_import', data)
    end
})

register_chatcommand_alias('mrkr_export', 'wp_upload', 'waypoint_upload')

-- Find co-ordinates sent in chat messages
if not minetest.registered_on_receiving_chat_message then
    minetest.registered_on_receiving_chat_message =
        minetest.registered_on_receiving_chat_messages
end

table.insert(minetest.registered_on_receiving_chat_message, 1, function(msg)
    if msg:byte(1) == 1 or #msg > 1000 then return end
    local raw_pos = msg:match('%-?[0-9%.]+, *%-?[0-9%.]+, *%-?[0-9%.]+')
    if raw_pos then
        local pos = string_to_pos(raw_pos)
        if pos then
            advmarkers.last_coords = pos
        end
    end
end)

-- Add '.mrkrthere'
minetest.register_chatcommand('mrkrthere', {
    params      = '',
    description = 'Adds a (temporary) waypoint at the "there" position.',
    func = function(param)
        if not advmarkers.last_coords then
            return false, 'No "there" position found!'
        elseif not advmarkers.set_hud_pos(advmarkers.last_coords) then
            return false, 'Error setting the waypoint!'
        end
    end
})

minetest.register_chatcommand('clrmrkr', {
    params = '',
    description = 'Hides the displayed waypoint.',
    func = function(param)
        if hud_id then
            minetest.localplayer:hud_remove(hud_id)
            hud_id = nil
            return true, 'Hidden the currently displayed waypoint.'
        elseif not minetest.localplayer.hud_add then
            minetest.run_server_chatcommand('clrmrkr')
            return
        elseif not hud_id then
            return false, 'No waypoint is currently being displayed!'
        end
    end,
})

register_chatcommand_alias('clrmrkr', 'clear_marker', 'clrwp',
    'clear_waypoint')