Skip to content

Commit

Permalink
add a location logger
Browse files Browse the repository at this point in the history
this is handy in my more complicated mapgens to find examples of things generated "in the wild".
  • Loading branch information
FaceDeer committed Jul 29, 2022
1 parent 5fe751e commit 4e226f1
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dofile(MP.."/lines.lua")
dofile(MP.."/place_schematic.lua")
dofile(MP.."/noise_manager.lua")
dofile(MP.."/metrics.lua")

dofile(MP.."/log_location.lua")

local map_data = {}
local map_param2_data = {}
Expand Down
108 changes: 108 additions & 0 deletions log_location.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
if not minetest.settings:get_bool("mapgen_helper_log_locations", false) then
mapgen_helper.log_first_location = function()
return
end
return
end

mapgen_helper.log_location_enabled = true

local worldpath = minetest.get_worldpath()
local locations = {}

local save_data = function()
local file = io.open(worldpath.."/mapgen_helper_test_locations.json", "w")
if file then
local data = {}
data.locations = locations
file:write(minetest.serialize(data))
file:close()
end
end

local read_data = function()
local file = io.open(worldpath.."/mapgen_helper_test_locations.json", "r")
if file then
local data = minetest.deserialize(file:read("*all"))
locations = data.locations or {}
file:close()
else
locations = {}
end
end
read_data()

mapgen_helper.log_first_location = function(name, pos, desc, notes)
if not locations[name] then
locations[name] = {pos=pos, desc=desc, notes=notes}
minetest.log("info", "[mapgen_helper] recorded location " .. name .. " at " .. minetest.pos_to_string(pos) .. " with desc '" .. (desc or "") .. "'")
save_data()
end
end

minetest.register_chatcommand("mapgen_helper_loc", {
params = "[location name]",
description = "sends the player to the named test location, or lists recorded locations if no parameter is given",
privs = {server=true},
func = function(name, param)
if not param or param == "" then
minetest.chat_send_all("test locations available:")
local names = {}
for name, loc in pairs(locations) do
table.insert(names, name)
end
table.sort(names)
for number, name in ipairs(names) do
minetest.chat_send_all("\t"..name .. " - " .. (locations[name].desc or ""))
end
return
end
local loc = locations[param]
if not loc then
minetest.chat_send_all("test location " .. param .. " not found")
return
end
local player = minetest.get_player_by_name(name)
player:set_pos(loc.pos)
if loc.notes then
minetest.chat_send_player(name, loc.notes)
end
end,
})

local tour_index = 0
minetest.register_chatcommand("mapgen_helper_tour", {
params = "<next|prev>",
description = "cycles through the various recorded mapgen test locations, forward or backward",
privs = {server=true},
func = function(name, param)
local names = {}
for name, loc in pairs(locations) do
table.insert(names, name)
end
table.sort(names)
if param == "prev" then
tour_index = tour_index - 1
if tour_index < 1 then
tour_index = #names
end
else
tour_index = tour_index + 1
if tour_index > #names then
tour_index = 1
end
end

local loc = locations[names[tour_index]]
local player = minetest.get_player_by_name(name)
player:set_pos(loc.pos)
minetest.chat_send_player(name, "Teleport to test location " .. names[tour_index])
if loc.notes then
minetest.chat_send_player(name, loc.notes)
end
end
})

minetest.register_on_shutdown(function()
save_data()
end)
4 changes: 2 additions & 2 deletions metrics.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local worldpath = minetest.get_worldpath()

if not minetest.settings:get_bool("mapgen_helper_record_time", false) then
mapgen_helper.record_time = function()
return
end
return
end

local worldpath = minetest.get_worldpath()

local persist = minetest.settings:get_bool("mapgen_helper_persist_recorded_time", false)
local filename = "mapgen_helper_metrics.lua"
local filename_old = "mapgen_helper_metrics_old.lua"
Expand Down
3 changes: 2 additions & 1 deletion settingtypes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mapgen_helper_record_time (Enable time-taken logging) bool false
mapgen_helper_persist_recorded_time (Persist time-taken logging between server sessions) bool false
mapgen_helper_record_time_resolution (Resolution of time-taken histogram) float 0.1
mapgen_helper_record_time_resolution (Resolution of time-taken histogram) float 0.1
mapgen_helper_log_locations (Enable location logging) bool false

0 comments on commit 4e226f1

Please sign in to comment.