pulled hud code out into a separate library mod

This commit is contained in:
FaceDeer 2020-01-24 18:24:38 -07:00
parent e0b97b0f3c
commit bbcf1418a6
3 changed files with 20 additions and 263 deletions

257
hud.lua

@ -1,257 +0,0 @@
local worldpath = minetest.get_worldpath()
local areastore_filename = worldpath.."/volcano_areastore.txt"
local area_file = io.open(areastore_filename, "r")
local areastore = AreaStore()
if area_file then
areastore:from_file(areastore_filename)
end
local function volcano_save()
areastore:to_file(areastore_filename)
end
magma_conduits.name_volcano = function(pos, name, override)
local existing_area = areastore:get_areas_for_pos(pos, true, true)
local id = next(existing_area)
if id and not override then
return
end
local data
if id then
local data = minetest.deserialize(existing_area[id].data)
data.name = name
areastore:remove_area(id)
else
data = {name = name, discovered_by = {}}
end
areastore:insert_area(pos, pos, minetest.serialize(data), id)
volcano_save()
end
if not minetest.settings:get_bool("magma_conduits_show_volcanoes_in_hud", true) then
return
end
local modpath = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(modpath.."/intllib.lua")
local requires_mappingkit = minetest.settings:get_bool("magma_conduits_hud_requires_mapping_kit", true)
and minetest.registered_items["map:mapping_kit"] -- rather than test for the map modpath, test whether the mapping_kit has been registered.
local discovery_range = tonumber(minetest.settings:get("magma_conduits_volcano_discovery_range")) or 60
local visual_range = tonumber(minetest.settings:get("magma_conduits_volcano_visibility_range")) or 1200
local test_interval = 5 -- check every test_interval seconds
local player_huds = {}
-- Each player will have a table of [position_hash] = hud_id pairs in here
local add_hud_marker = function(player, player_name, pos, label)
local waypoints = player_huds[player_name] or {}
player_huds[player_name] = waypoints
local pos_hash = minetest.hash_node_position(pos)
if waypoints[pos_hash] then
return
end
local hud_id = player:hud_add({
hud_elem_type = "waypoint",
name = label,
text = "m",
number = 0xFFFFFF,
world_pos = pos})
waypoints[pos_hash] = hud_id
end
local remove_distant_hud_markers = function()
local players_to_remove = {}
for player_name, waypoints in pairs(player_huds) do
local player = minetest.get_player_by_name(player_name)
if player then
local has_map = (not requires_mappingkit) or (player:get_inventory():contains_item("main", "map:mapping_kit"))
local player_pos = player:get_pos()
local waypoints_to_remove = {}
for pos_hash, hud_id in pairs(waypoints) do
local pos = minetest.get_position_from_hash(pos_hash)
if (not has_map) or vector.distance(pos, player_pos) > visual_range then
table.insert(waypoints_to_remove, pos_hash)
player:hud_remove(hud_id)
end
end
for _, pos_hash in ipairs(waypoints_to_remove) do
waypoints[pos_hash] = nil
end
if not next(waypoints) then -- player's waypoint list is empty, remove it
table.insert(players_to_remove, player_name)
end
else
table.insert(players_to_remove, player_name)
end
end
for _, player_name in ipairs(players_to_remove) do
player_huds[player_name] = nil
end
end
-- For flushing outdated HUD markers when certain admin commands are performed.
local remove_all_hud_markers = function()
for player_name, waypoints in pairs(player_huds) do
local player = minetest.get_player_by_name(player_name)
if player then
for pos_hash, hud_id in pairs(waypoints) do
player:hud_remove(hud_id)
end
end
end
player_huds = {}
end
local elapsed = 0
minetest.register_globalstep(function(dtime)
elapsed = elapsed + dtime
if elapsed < test_interval then
return
end
elapsed = 0
local connected_players = minetest.get_connected_players()
local new_discovery = false
for _, player in ipairs(connected_players) do
local player_pos = player:get_pos()
local player_name = player:get_player_name()
local min_visual_edge = vector.subtract(player_pos, visual_range)
local max_visual_edge = vector.add(player_pos, visual_range)
local visual_volcanos = areastore:get_areas_in_area(min_visual_edge, max_visual_edge, true, true, true)
for id, volcano in pairs(visual_volcanos) do
local data = minetest.deserialize(volcano.data)
local distance = vector.distance(player_pos, volcano.min)
local discovered_by = data.discovered_by
local volcano_pos = vector.add(volcano.min, {x=0, y=2, z=0})
if distance < discovery_range and not discovered_by[player_name] then
-- Update areastore
data.discovered_by[player_name] = true
areastore:remove_area(id)
areastore:insert_area(volcano.min, volcano.min, minetest.serialize(data), id)
-- Mark that we'll need to save volcanoes
new_discovery = true
-- Notify player of their find
local note_name = data.name or "a volcano"
local discovery_note = S("You've discovered @1!", note_name)
local formspec = "size[4,1]" ..
"label[1.0,0.0;" .. minetest.formspec_escape(discovery_note) ..
"]button_exit[0.5,0.75;3,0.5;btn_ok;".. S("OK") .."]"
minetest.show_formspec(player_name, "magma_conduits:discovery_popup",
formspec)
minetest.chat_send_player(player_name, discovery_note)
minetest.log("action", "[magma_conduits] " .. player_name .. " discovered " .. note_name)
minetest.sound_play({name = "magma_conduits_chime01", gain = 0.25}, {to_player=player_name})
end
local has_map = (not requires_mappingkit) or (player:get_inventory():contains_item("main", "map:mapping_kit"))
if has_map and distance < visual_range and discovered_by[player_name] then
local volcano_name = data.name or "Volcano"
add_hud_marker(player, player_name, volcano_pos, volcano_name)
end
end
end
remove_distant_hud_markers()
if new_discovery then
volcano_save()
end
end)
----------------------------------------------------------------------
-- Chatcommands
local function get_nearest_volcano_within_range(pos, range, name)
local min_edge = vector.subtract(pos, range)
local max_edge = vector.add(pos, range)
local volcano_list = areastore:get_areas_in_area(min_edge, max_edge, true, true, true)
local min_dist = range + 1 -- start with number beyond range
local min_id = nil
local min_data = nil
local min_pos = nil
for id, volcano in pairs(volcano_list) do
local data = minetest.deserialize(volcano.data)
local distance = vector.distance(pos, volcano.min)
if distance < min_dist and data.discovered_by[name] then
min_dist = distance
min_id = id
min_data = data
min_pos = volcano.min
end
end
return min_pos, min_id, min_data
end
minetest.register_chatcommand("volcano_rename_nearest", {
description = S("Change the name of the nearest volcano within visible range"),
param = S("The new name for this volcano"),
privs = {["server"]=true},
func = function(name, param)
if param == "" then
minetest.chat_send_player(name, S("Please enter a new name"))
return
end
local player = minetest.get_player_by_name(name)
local player_pos = player:get_pos()
local min_pos, min_id, min_data = get_nearest_volcano_within_range(player_pos, visual_range, name)
if min_id ~= nil then
local oldname = min_data.name
min_data.name = param
areastore:remove_area(min_id)
areastore:insert_area(min_pos, min_pos, minetest.serialize(min_data), min_id)
volcano_save()
minetest.log("action", "[magma_conduits] Renamed " .. oldname .. " to " .. param)
minetest.chat_send_player(name, S("Volcano successfully renamed from @1 to @2.", oldname, param))
remove_all_hud_markers()
return
end
minetest.chat_send_player(name, S("No known volcanoes within @1m found.", visual_range))
end,
})
local function set_all_discovered(player_name, state)
local volcano_list = areastore:get_areas_in_area(
{x=-32000, y=-32000, z=-32000}, {x=32000, y=32000, z=32000}, true, true, true)
for id, volcano in pairs(volcano_list) do
local data = minetest.deserialize(volcano.data)
data.discovered_by[player_name] = state
areastore:remove_area(id)
areastore:insert_area(volcano.min, volcano.min, minetest.serialize(data), id)
end
volcano_save()
end
minetest.register_chatcommand("volcano_discover_all", {
description = S("Set all volcanoes as known to you or another player"),
param = S("player_name, or nothing for yourself"),
privs = {["server"]=true},
func = function(name, param)
if param ~= "" then
name = param
end
set_all_discovered(name, true)
end,
})
minetest.register_chatcommand("volcano_undiscover_all", {
description = S("Set all volcanoes as unknown to you or another player"),
param = S("player_name, or nothing for yourself"),
privs = {["server"]=true},
func = function(name, param)
if param ~= "" then
name = param
end
set_all_discovered(name, nil)
end,
})

@ -1,4 +1,4 @@
name = magma_conduits
description = Removes default mapgen lava and adds widely-spaced volcanoes and lava "veins".
depends = default
optional_depends = namegen, intllib, doc, map
optional_depends = namegen, intllib, doc, map, named_waypoints

@ -7,10 +7,23 @@
local modpath = minetest.get_modpath(minetest.get_current_modname())
dofile(modpath .. "/volcano_lava.lua") -- https://github.com/minetest/minetest/issues/7864, https://github.com/minetest/minetest/issues/7878
dofile(modpath .. "/hud.lua")
local S, NS = dofile(modpath.."/intllib.lua")
--dofile(modpath .. "/hud.lua")
local named_waypoints_modpath = minetest.get_modpath("named_waypoints")
if named_waypoints_modpath then
named_waypoints.register_named_waypoints("volcanoes", {
default_name = S("a volcano"),
default_color = 0xFFFF88,
visibility_requires_item = "map:mapping_kit",
visibility_volume_radius = 1200,
discovery_volume_radius = 60,
on_discovery = named_waypoints.default_discovery_popup
})
end
local depth_root = magma_conduits.config.volcano_min_depth
local depth_base = -50 -- point where the mountain root starts expanding
local depth_maxwidth = -30 -- point of maximum width
@ -115,8 +128,7 @@ local get_volcano = function(pos)
return nil
end
local name = S("a volcano")
local name
local location = scatter_2d(corner_xz, volcano_region_size, radius_cone_max)
local depth_peak = math.random(depth_minpeak, depth_maxpeak)
local depth_lava
@ -139,7 +151,9 @@ local get_volcano = function(pos)
local slope = math.random() * (slope_max - slope_min) + slope_min
local caldera = math.random() * (caldera_max - caldera_min) + caldera_min
magma_conduits.name_volcano({x=location.x, y=depth_peak, z=location.z}, name)
if named_waypoints_modpath then
named_waypoints.add_waypoint("volcanoes", {x=location.x, y=depth_peak, z=location.z}, {name=name})
end
math.randomseed(next_seed)
return {location = location, depth_peak = depth_peak, depth_lava = depth_lava, slope = slope, state = state, caldera = caldera}