debug.lua 3.9 KB

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