A minetest mod for mapmaking
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.4KB

  1. local marker_lookup = {};
  2. -- Format marker ids to allow their use as formspec element ids.
  3. -- We're mostly concerned with guarding against the : character because it is
  4. -- common for ids and has an alternate meaning in formspecs.
  5. --
  6. -- id: The id to format
  7. --
  8. -- Returns the formatted id
  9. local function format_marker_id(id)
  10. return id:gsub(":", "_");
  11. end
  12. -- Find the marker data for a given id
  13. --
  14. -- id: The id to search for
  15. --
  16. -- Returns the marker data, or nil if not found
  17. local function get_marker(id)
  18. if not id then
  19. return nil;
  20. end
  21. id = format_marker_id(id);
  22. for _,marker in pairs(marker_lookup) do
  23. if marker.id == id then
  24. return marker;
  25. end
  26. end
  27. return nil;
  28. end
  29. -- Get the number of registered markers
  30. --
  31. -- Returns the length of the marker table
  32. local function get_marker_count()
  33. return #marker_lookup;
  34. end
  35. -- Get all registered markers
  36. --
  37. -- Returns a copy of the marker table
  38. local function get_registered_markers()
  39. return table.copy(marker_lookup);
  40. end
  41. -- Register a marker with textures to display
  42. --
  43. -- id: A string containing the id of the marker
  44. -- name: A string containing the displayedname of the marker
  45. -- textures: A table of texture names.
  46. -- These should correspond with detail levels,
  47. -- any detail level past the length of the table will return the last texture
  48. local function add_marker(id, name, textures)
  49. if not id then
  50. return nil;
  51. end
  52. id = format_marker_id(id);
  53. local existing_marker = get_marker(id);
  54. if existing_marker then
  55. existing_marker.name = name;
  56. existing_marker.textures = textures;
  57. else
  58. marker_lookup[#marker_lookup+1] = {
  59. id = id,
  60. name = name,
  61. textures = textures,
  62. };
  63. end
  64. end
  65. -- Get the texture name (minus extension) for the given marker and detail level.
  66. --
  67. -- id: A string containing the marker id
  68. -- detail: The detail level
  69. -- Returns a string with a texture name, or nil if no matching marker was found.
  70. local function get_marker_texture(id, detail)
  71. if not id then
  72. return nil;
  73. end
  74. id = format_marker_id(id);
  75. local marker = get_marker(id);
  76. if marker then
  77. return marker.textures[math.min(detail, #marker.textures)];
  78. end
  79. return nil;
  80. end
  81. return {
  82. add = add_marker,
  83. count = get_marker_count,
  84. get = get_marker,
  85. get_all = get_registered_markers,
  86. get_texture = get_marker_texture,
  87. };