2019-07-14 00:20:04 +02:00
|
|
|
--
|
|
|
|
-- Minetest advmarkers SSCSM
|
|
|
|
--
|
2019-10-17 08:09:19 +02:00
|
|
|
-- Copyright © 2019 by luk3yx
|
2020-10-01 02:53:47 +02:00
|
|
|
-- License: https://luk3yx.mit-license.org/@2019
|
2019-10-17 08:09:19 +02:00
|
|
|
--
|
2019-07-14 00:20:04 +02:00
|
|
|
|
|
|
|
advmarkers = {}
|
|
|
|
local data = {}
|
2020-06-03 02:14:48 +02:00
|
|
|
local loaded = false
|
2019-07-14 00:20:04 +02:00
|
|
|
|
2019-10-15 06:50:38 +02:00
|
|
|
assert(not sscsm.restrictions or not sscsm.restrictions.chat_messages,
|
|
|
|
'The advmarkers SSCSM needs to be able to send chat messages!')
|
|
|
|
|
2019-07-14 00:20:04 +02:00
|
|
|
local function string_to_pos(pos)
|
|
|
|
if type(pos) == 'string' then
|
|
|
|
pos = minetest.string_to_pos(pos)
|
|
|
|
end
|
|
|
|
if type(pos) == 'table' then
|
|
|
|
return vector.round(pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Display a waypoint
|
|
|
|
function advmarkers.display_waypoint(name)
|
2020-10-09 06:23:29 +02:00
|
|
|
if data[name] then
|
2020-11-22 06:49:19 +01:00
|
|
|
sscsm.com_send('advmarkers:display', name)
|
2019-07-14 00:20:04 +02:00
|
|
|
return true
|
|
|
|
end
|
2020-10-09 06:23:29 +02:00
|
|
|
return false
|
2019-07-14 00:20:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Get a waypoint
|
|
|
|
function advmarkers.get_waypoint(name)
|
2020-10-09 06:23:29 +02:00
|
|
|
return data[name]
|
2019-07-14 00:20:04 +02:00
|
|
|
end
|
|
|
|
advmarkers.get_marker = advmarkers.get_waypoint
|
|
|
|
|
|
|
|
-- Delete a waypoint
|
|
|
|
function advmarkers.delete_waypoint(name)
|
2020-10-09 06:23:29 +02:00
|
|
|
if data[name] ~= nil then
|
|
|
|
data[name] = nil
|
2020-11-22 06:49:19 +01:00
|
|
|
sscsm.com_send('advmarkers:delete', name)
|
2019-07-14 00:20:04 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
advmarkers.delete_marker = advmarkers.delete_waypoint
|
|
|
|
|
|
|
|
-- Set a waypoint
|
|
|
|
function advmarkers.set_waypoint(pos, name)
|
2020-10-09 06:23:29 +02:00
|
|
|
pos = string_to_pos(pos)
|
2019-07-14 00:20:04 +02:00
|
|
|
if not pos then return end
|
|
|
|
name = tostring(name)
|
2020-10-09 06:23:29 +02:00
|
|
|
data[name] = pos
|
2020-11-22 06:49:19 +01:00
|
|
|
sscsm.com_send('advmarkers:set', {
|
|
|
|
name = name,
|
|
|
|
pos = minetest.pos_to_string(pos),
|
|
|
|
})
|
2019-07-14 00:20:04 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
advmarkers.set_marker = advmarkers.set_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
|
|
|
|
|
|
|
|
-- Get waypoint names
|
|
|
|
function advmarkers.get_waypoint_names(sorted)
|
|
|
|
local res = {}
|
2020-06-03 02:14:48 +02:00
|
|
|
for name, _ in pairs(data) do
|
2020-10-09 06:23:29 +02:00
|
|
|
table.insert(res, name)
|
2019-07-14 00:20:04 +02:00
|
|
|
end
|
|
|
|
if sorted or sorted == nil then table.sort(res) end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Display the formspec
|
|
|
|
local formspec_list = {}
|
|
|
|
local selected_name = false
|
|
|
|
function advmarkers.display_formspec()
|
2020-06-03 02:14:48 +02:00
|
|
|
if not loaded then
|
|
|
|
minetest.run_server_chatcommand('wp')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-07-14 00:20:04 +02:00
|
|
|
local formspec = 'size[5.25,8]' ..
|
|
|
|
'label[0,0;Waypoint list ' ..
|
|
|
|
minetest.colorize('#888888', '(SSCSM)') .. ']' ..
|
|
|
|
'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 markers
|
|
|
|
local selected = 1
|
|
|
|
formspec_list = advmarkers.get_waypoint_names()
|
|
|
|
for id, name in ipairs(formspec_list) 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 = formspec .. '##' .. minetest.formspec_escape(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Close the text list and display the selected marker position
|
|
|
|
formspec = formspec .. ';' .. tostring(selected) .. ']'
|
|
|
|
if selected_name then
|
|
|
|
local pos = advmarkers.get_marker(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
|
2020-10-09 06:23:29 +02:00
|
|
|
formspec = formspec ..
|
|
|
|
'button_exit[0,7.5;5.25,0.5;quit;Close dialog]' ..
|
2019-07-14 00:20:04 +02:00
|
|
|
'label[0,6.75;No waypoints. Add one with "/add_wp".]'
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Display the formspec
|
|
|
|
return minetest.show_formspec('advmarkers-sscsm', formspec)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Register chatcommands
|
2020-06-03 02:14:48 +02:00
|
|
|
local function mrkr_cmd(param)
|
2019-07-14 00:20:04 +02:00
|
|
|
if param == '' then return advmarkers.display_formspec() end
|
|
|
|
minetest.run_server_chatcommand('wp', param)
|
|
|
|
end
|
|
|
|
sscsm.register_chatcommand('mrkr', mrkr_cmd)
|
|
|
|
sscsm.register_chatcommand('wp', mrkr_cmd)
|
|
|
|
sscsm.register_chatcommand('wps', mrkr_cmd)
|
|
|
|
sscsm.register_chatcommand('waypoint', mrkr_cmd)
|
|
|
|
sscsm.register_chatcommand('waypoints', mrkr_cmd)
|
|
|
|
|
|
|
|
-- Set the HUD
|
|
|
|
minetest.register_on_formspec_input(function(formname, fields)
|
|
|
|
if formname == 'advmarkers-ignore' then
|
|
|
|
return true
|
|
|
|
elseif formname ~= 'advmarkers-sscsm' 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-sscsm', 'size[6,3]' ..
|
2019-07-14 05:24:07 +02:00
|
|
|
'label[0.35,0.2;Rename waypoint]' ..
|
2019-07-14 00:20:04 +02:00
|
|
|
'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 waypoint.'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
elseif fields.teleport then
|
|
|
|
minetest.show_formspec('advmarkers-sscsm', 'size[6,2.2]' ..
|
|
|
|
'label[0.35,0.25;' .. minetest.formspec_escape(
|
|
|
|
'Teleport to a waypoint\n - ' .. name
|
|
|
|
) .. ']' ..
|
|
|
|
'button[0,1.25;3,1;cancel;Cancel]' ..
|
|
|
|
'button_exit[3,1.25;3,1;teleport_confirm;Teleport]')
|
|
|
|
elseif fields.teleport_confirm then
|
|
|
|
-- Teleport with /teleport
|
|
|
|
local pos = advmarkers.get_waypoint(name)
|
|
|
|
if pos then
|
|
|
|
minetest.run_server_chatcommand('teleport',
|
|
|
|
pos.x .. ', ' .. pos.y .. ', ' .. pos.z)
|
|
|
|
else
|
|
|
|
minetest.display_chat_message('Error teleporting to waypoint!')
|
|
|
|
end
|
|
|
|
elseif fields.delete then
|
|
|
|
minetest.show_formspec('advmarkers-sscsm', 'size[6,2]' ..
|
2020-10-09 06:23:29 +02:00
|
|
|
'label[0.35,0.25;Are you sure you want to delete this ' ..
|
|
|
|
'waypoint?]' ..
|
2019-07-14 00:20:04 +02:00
|
|
|
'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
|
2020-06-03 02:14:48 +02:00
|
|
|
minetest.display_chat_message('Please select a waypoint.')
|
2019-07-14 00:20:04 +02:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- Update the waypoint list
|
2020-06-03 02:14:48 +02:00
|
|
|
sscsm.register_on_com_receive('advmarkers:update', function(msg)
|
2020-06-04 00:50:14 +02:00
|
|
|
data = msg or {}
|
2020-06-03 02:14:48 +02:00
|
|
|
loaded = true
|
2019-07-14 00:20:04 +02:00
|
|
|
end)
|