node_box_visualizer.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. local S = minetest.get_translator("testtools")
  2. minetest.register_entity("testtools:visual_box", {
  3. initial_properties = {
  4. visual = "cube",
  5. textures = {
  6. "blank.png", "blank.png", "blank.png",
  7. "blank.png", "blank.png", "blank.png",
  8. },
  9. use_texture_alpha = true,
  10. physical = false,
  11. pointable = false,
  12. static_save = false,
  13. },
  14. on_activate = function(self)
  15. self.timestamp = minetest.get_us_time() + 5000000
  16. end,
  17. on_step = function(self)
  18. if minetest.get_us_time() >= self.timestamp then
  19. self.object:remove()
  20. end
  21. end,
  22. })
  23. local BOX_TYPES = {"node_box", "collision_box", "selection_box"}
  24. local DEFAULT_BOX_TYPE = "selection_box"
  25. local function visualizer_on_use(itemstack, user, pointed_thing)
  26. if pointed_thing.type ~= "node" then
  27. return
  28. end
  29. local meta = itemstack:get_meta()
  30. local box_type = meta:get("box_type") or DEFAULT_BOX_TYPE
  31. local result = minetest.get_node_boxes(box_type, pointed_thing.under)
  32. local t = "testtools_visual_" .. box_type .. ".png"
  33. for _, box in ipairs(result) do
  34. local box_min = pointed_thing.under + vector.new(box[1], box[2], box[3])
  35. local box_max = pointed_thing.under + vector.new(box[4], box[5], box[6])
  36. local box_center = (box_min + box_max) / 2
  37. local obj = minetest.add_entity(box_center, "testtools:visual_box")
  38. if not obj then
  39. break
  40. end
  41. obj:set_properties({
  42. textures = {t, t, t, t, t, t},
  43. -- Add a small offset to avoid Z-fighting.
  44. visual_size = vector.add(box_max - box_min, 0.01),
  45. })
  46. end
  47. end
  48. local function visualizer_on_place(itemstack, placer, pointed_thing)
  49. local meta = itemstack:get_meta()
  50. local prev_value = meta:get("box_type") or DEFAULT_BOX_TYPE
  51. local prev_index = table.indexof(BOX_TYPES, prev_value)
  52. assert(prev_index ~= -1)
  53. local new_value = BOX_TYPES[(prev_index % #BOX_TYPES) + 1]
  54. meta:set_string("box_type", new_value)
  55. minetest.chat_send_player(placer:get_player_name(), S("[Node Box Visualizer] box_type = @1", new_value))
  56. return itemstack
  57. end
  58. minetest.register_tool("testtools:node_box_visualizer", {
  59. description = S("Node Box Visualizer") .. "\n" ..
  60. S("Punch: Show node/collision/selection boxes of the pointed node") .. "\n" ..
  61. S("Place: Change selected box type (default: selection box)"),
  62. inventory_image = "testtools_node_box_visualizer.png",
  63. groups = { testtool = 1, disable_repair = 1 },
  64. on_use = visualizer_on_use,
  65. on_place = visualizer_on_place,
  66. on_secondary_use = visualizer_on_place,
  67. })