diff --git a/.gitignore b/.gitignore index 5236e1e..70bb2a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *~ +*.swp diff --git a/api.lua b/api.lua index e8b9c7a..198979a 100644 --- a/api.lua +++ b/api.lua @@ -1,7 +1,33 @@ +local protection_detectors = {} + +-- Other protection mods should be able to display their protection in the hud +areas.registerHudHandler = function(handler) + protection_detectors[#protection_detectors + 1] = handler +end + +-- Generalized call to registered handlers to add their proeciton labels to the areas list +local detect_extra_protection = function(pos, area_list) + if #protection_detectors <= 0 then + return area_list + end + + for idx=1,#protection_detectors do + local func = protection_detectors[idx] + area_list = func(pos,area_list) + end + return area_list +end + +function areas:getRegisteredProtections(pos) + local res = {} + res = detect_extra_protection(pos, res) + return res +end --- Returns a list of areas that include the provided position. function areas:getAreasAtPos(pos) local res = {} + if self.store then local a = self.store:get_areas_for_pos(pos, false, true) for store_id, store_area in pairs(a) do @@ -13,9 +39,9 @@ function areas:getAreasAtPos(pos) for id, area in pairs(self.areas) do local ap1, ap2 = area.pos1, area.pos2 if - (px >= ap1.x and px <= ap2.x) and - (py >= ap1.y and py <= ap2.y) and - (pz >= ap1.z and pz <= ap2.z) then + (px >= ap1.x and px <= ap2.x) and + (py >= ap1.y and py <= ap2.y) and + (pz >= ap1.z and pz <= ap2.z) then res[id] = area end end diff --git a/api.md b/api.md new file mode 100644 index 0000000..459e3bc --- /dev/null +++ b/api.md @@ -0,0 +1,46 @@ +# API Extension + +Adding your protections to the HUD +---------------------------------- + +If you are providing an extra protection mod to work in conjunction with the +HUD feature of `areas`, you can register a callback to add your mod's code to +display your protection's existence. + +Registering a handler: + +* `areas.registerHudHandler(handler) --> nil` + +Handler specification: + +* `handler(pos,area_list) --> new_area_list` + * `pos` - the position at which to check for protection coverage by your mod + * `area_list` - the current list of protected areas + * `new_area_list` - the list of protected areas, updated with your entries + +Area list items: + +The area list item is a map table identified with an ID, and properties + +The ID should be in the format `modname:` and appended with an identifier for the protection. + +Each area list item should be a table with the following properties + +* `owner` - (required) the name of the protection owner +* `name` - (optional) the name of the area + +Example +------- + + local myhandler = function(pos,area_list) + local areaowner = find_my_protections(pos) + + if areaowner then + arealist["mymodname:first"] = { + name = "Protection name", + owner = areaowner, + } + end + end + + areas.register_hud_handler(myhandler) diff --git a/hud.lua b/hud.lua index 4908b92..ce83660 100644 --- a/hud.lua +++ b/hud.lua @@ -7,11 +7,24 @@ minetest.register_globalstep(function(dtime) local name = player:get_player_name() local pos = vector.round(player:getpos()) local areaStrings = {} + for id, area in pairs(areas:getAreasAtPos(pos)) do table.insert(areaStrings, ("%s [%u] (%s%s)") :format(area.name, id, area.owner, area.open and ":open" or "")) end + + for id, area in pairs(areas:getRegisteredProtections(pos)) do + table.insert( + areaStrings, ("%s [%s] (%s)") + :format( + area.name or "", + id , + area.owner + ) + ) + end + local areaString = "Areas:" if #areaStrings > 0 then areaString = areaString.."\n"..