debug.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. function defense:toggle_debug(on)
  2. self.debug = on
  3. if self.debug then
  4. regeneration.rate = 100
  5. minetest.set_timeofday(0.3)
  6. return true, "Debug mode activated"
  7. else
  8. regeneration.rate = self.regeneration_rate
  9. return true, "Debug mode deactivated"
  10. end
  11. end
  12. minetest.register_chatcommand("debug", {
  13. description = "Toggle Defense mod debug mode",
  14. privs = {server=true},
  15. func = function(name)
  16. return defense:toggle_debug(not defense.debug)
  17. end,
  18. })
  19. -- Pathfinder debugger
  20. local pf_player = nil
  21. local pf_class_name = nil
  22. local pf_update_interval = 1.0
  23. minetest.register_chatcommand("debug_pf", {
  24. description = "Debug the pathfinder",
  25. params = "<class>",
  26. privs = {server=true},
  27. func = function(name, class)
  28. if class and class ~= "" then
  29. if defense.pathfinder.classes[class] then
  30. pf_class_name = class
  31. pf_player = minetest.get_player_by_name(name)
  32. return true, "Pathfinder debugger for " .. pf_class_name .. " activated"
  33. else
  34. return false, "No pathfinder class of that name"
  35. end
  36. else
  37. pf_class_name = nil
  38. pf_player = nil
  39. return true, "Pathfinder debugger deactivated"
  40. end
  41. end,
  42. })
  43. minetest.register_node("defense:debug_pf", {
  44. drawtype = "allfaces",
  45. tiles = {"defense_debug_path.png"},
  46. light_source = 14,
  47. groups = {dig_immediate = 3},
  48. drop = "",
  49. walkable = false,
  50. })
  51. minetest.register_abm({
  52. nodenames = {"defense:debug_pf"},
  53. interval = 2.0,
  54. chance = 1,
  55. action = function(pos)
  56. minetest.remove_node(pos)
  57. end,
  58. })
  59. local function pf_update()
  60. if pf_class_name then
  61. local pathfinder = defense.pathfinder
  62. local pos = pf_player:get_pos()
  63. local sector = pathfinder.find_containing_sector(pathfinder.classes[pf_class_name], math.floor(pos.x + 0.5), math.floor(pos.y + 0.5), math.floor(pos.z + 0.5))
  64. if sector then
  65. local distance_str = sector.distance
  66. if sector.distance == nil then
  67. distance_str = "nil"
  68. end
  69. local bounds_str = "(" .. sector.min_x .. "," .. sector.min_y .. "," .. sector.min_z .. ";" .. sector.max_x .. "," .. sector.max_y .. "," .. sector.max_z .. ")"
  70. local links_str = ""
  71. for i,l in pairs(sector.links) do
  72. links_str = links_str .. " " .. i
  73. end
  74. links_str = "[" .. links_str .. " ]"
  75. defense:log("You are in sector " .. sector.id .. " {d=" .. distance_str .. " b=" .. bounds_str .. " l=" .. links_str .. "}")
  76. for z = sector.min_z,sector.max_z do
  77. for y = sector.min_y,sector.max_y do
  78. for x = sector.min_x,sector.max_x do
  79. if (x == sector.min_x or x == sector.max_x)
  80. and (y == sector.min_y or y == sector.max_y)
  81. and (z == sector.min_z or z == sector.max_z) then
  82. local pos = {x=x,y=y,z=z}
  83. local node = minetest.get_node_or_nil(pos)
  84. if node and node.name == "air" then
  85. minetest.set_node(pos, {name="defense:debug_pf"})
  86. end
  87. end
  88. end
  89. end
  90. end
  91. else
  92. defense:log("You are not in a sector")
  93. end
  94. end
  95. end
  96. local pf_last_update_time = 0
  97. minetest.register_globalstep(function(dtime)
  98. local gt = minetest.get_gametime()
  99. if pf_last_update_time + pf_update_interval < gt then
  100. pf_update()
  101. pf_last_update_time = gt
  102. end
  103. end)