readme, and fix various bugs

Not bad for completely untested code!
This commit is contained in:
FaceDeer 2020-01-24 18:24:04 -07:00
parent 61e200d84c
commit 602141295f
2 changed files with 69 additions and 7 deletions

View File

@ -1,9 +1,10 @@
local worldpath = minetest.get_worldpath()
--local modpath = minetest.get_modpath(minetest.get_current_modname())
local S = minetest.get_translator("named_waypoints")
named_wayponts = {}
named_waypoints = {}
local test_interval = 5
local player_huds = {} -- Each player will have a table of [position_hash] = hud_id pairs in here
local waypoint_defs = {} -- the registered definition tables
@ -15,7 +16,7 @@ local wielded_string = "wielded"
--waypoint_def = {
-- default_name = , -- a string that's used if a waypoint's data doesn't have a "name" property
-- color = , -- if not defined, defaults to 0xFFFFFFFF
-- default_color = , -- if not defined, defaults to 0xFFFFFFFF
-- visibility_requires_item = , -- item, if not defined then nothing is required
-- visibility_item_location = , -- "inventory", "hotbar", "wielded" (defaults to inventory if not provided)
-- visibility_volume_radius = , -- required.
@ -150,6 +151,10 @@ end
local grouplen = #"group:"
local function test_items(player, item, location)
if not item then
return true
end
location = location or inventory_string
local group
if item:sub(1,grouplen) == "group:" then
@ -211,7 +216,7 @@ local function test_range(player_pos, waypoint_pos, volume_radius, volume_height
((player_pos.x - waypoint_pos.x)*(player_pos.x - waypoint_pos.x))+
((player_pos.z - waypoint_pos.z)*(player_pos.z - waypoint_pos.z))) <= volume_radius
else
return vector.distance(player_pos, waypoint_pos <= volume_radius)
return vector.distance(player_pos, waypoint_pos) <= volume_radius
end
end
@ -301,7 +306,8 @@ minetest.register_globalstep(function(dtime)
local disc_loc = waypoint_def.discovery_item_location
local on_discovery = waypoint_def.on_discovery
local color = waypoint_def.color
local default_color = waypoint_def.default_color
local default_name = waypoint_def.default_name
for _, player in ipairs(connected_players) do
local player_pos = player:get_pos()
@ -315,7 +321,7 @@ minetest.register_globalstep(function(dtime)
local data = minetest.deserialize(area_data.data)
local discovered_by = data.discovered_by or {}
if (not discovered_by or discovered_by[player_name]) and
if not discovered_by[player_name] and
test_items(player, disc_inv, disc_loc)
and test_range(player_pos, pos, disc_radius, disc_height) then
@ -343,7 +349,8 @@ minetest.register_globalstep(function(dtime)
if (not disc_radius or (discovered_by and discovered_by[player_name])) and
test_items(player, vis_inv, vis_loc)
and test_range(player_pos, pos, vis_radius, vis_height) then
add_hud_marker(waypoint_type, player, player_name, pos, data.name or waypoint_def.default_name, color)
add_hud_marker(waypoint_type, player, player_name, pos,
data.name or default_name, data.color or default_color)
end
end
end

55
readme.md Normal file
View File

@ -0,0 +1,55 @@
## API
### named_waypoints.register_named_waypoints = function(waypoints_type, waypoints_def)
waypoints_def = {
default_name = , -- a string that's used as the waypoint's label if a waypoint's data doesn't have a "name" property
default_color = , -- label text color. If not defined defaults to 0xFFFFFFFF (opaque white)
visibility_requires_item = , -- item string or "group:groupname", the player needs to have an item that matches this in their inventory to see waypoints on their HUD. If not defined then there's no requirement to see waypoints on the HUD.
visibility_item_location = , -- "inventory", "hotbar", or "wielded" (defaults to inventory if not defined)
visibility_volume_radius = , -- required. The radius within which a player will see the waypoint marked on their HUD
visibility_volume_height = , -- if defined, then visibility check is done in a cylindrical volume rather than a sphere. Height extends both upward and downward from player position.
discovery_requires_item = ,-- item string or "group:groupname", an item matching this is needed in player inventory for a waypoint to be marked as "discovered" for that player
discovery_item_location = ,-- -- "inventory", "hotbar", "wielded" (defaults to inventory if not defined)
discovery_volume_radius = , -- radius within which a waypoint can be auto-discovered by a player. "discovered_by" property is used in waypoint_data to store discovery info. If this is not defined then discovery is not required - waypoints will always be visible.
discovery_volume_height = , -- if defined, then discovery check is done in a cylindrical volume rather than a sphere
on_discovery = function(player, pos, waypoint_data, waypoint_def) -- use "named_waypoints.default_discovery_popup" for a generic discovery notification
}
### named_waypoints.add_waypoint = function(waypoints_type, pos, waypoint_data)
waypoint_data is a freeform table you can put whatever you want into (though it will be stored as a serialized string so don't get fancy if you can avoid it). There are three properties on waypoint_data with special meaning:
name = a string that will be used as the label for this waypoint (if not defined, named_waypoints will fall back to the "default_name" property of the waypoint definition)
color = a hex integer that defines the colour of this waypoint (if not defined, named_waypoints will fall back to the "default_color" property of the waypoint definition, which itself falls back to 0xFFFFFFFF - opaque white)
discovered_by = a set containing the names of players that have discovered this waypoint (provided discovery_volume_radius was defined)
If there's already a waypoint at pos this function will return false.
### named_waypoints.update_waypoint = function(waypoints_type, pos, waypoint_data)
The same as add_waypoint, but if there's already a waypoint at pos then the values of any fields in waypoint_data will replace the corresponding fields in the existing waypoint
### named_waypoints.get_waypoint = function(waypoints_type, pos)
Returns the waypoint_data of the waypoint at pos, or nil if there isn't one.
### named_waypoints.get_waypoints_in_area = function(waypoints_type, minp, maxp)
Returns a table with values of {pos = pos, data = waypoint_data} for all waypoints in the region specified.
### named_waypoints.remove_waypoint = function(waypoints_type, pos)
Deletes the waypoint at pos
### named_waypoints.reset_hud_markers = function(waypoint_type)
Causes all player HUD markers to be invalidated and refreshed.