commands.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. -- Add chat command to place all the nodes in DevTest
  2. local function advance_pos(pos, start_pos, advance_z)
  3. if advance_z then
  4. pos.z = pos.z + 2
  5. pos.x = start_pos.x
  6. else
  7. pos.x = pos.x + 2
  8. end
  9. if pos.x > 30900 or pos.x - start_pos.x > 46 then
  10. pos.x = start_pos.x
  11. pos.z = pos.z + 2
  12. end
  13. if pos.z > 30900 then
  14. -- We ran out of space! Aborting
  15. aborted = true
  16. return false
  17. end
  18. return pos
  19. end
  20. local function place_nodes(param)
  21. local nodes = param.nodes
  22. local name = param.name
  23. local pos = param.pos
  24. local start_pos = param.start_pos
  25. table.sort(nodes)
  26. core.chat_send_player(name, "Placing nodes …")
  27. local nodes_placed = 0
  28. local aborted = false
  29. for n=1, #nodes do
  30. local itemstring = nodes[n]
  31. local def = core.registered_nodes[itemstring]
  32. local p2_max = 0
  33. if param.param ~= "no_param2" then
  34. -- Also test the param2 values of the nodes
  35. -- ... but we only use permissible param2 values
  36. if def.paramtype2 == "wallmounted" then
  37. p2_max = 5
  38. elseif def.paramtype2 == "facedir" then
  39. p2_max = 23
  40. elseif def.paramtype2 == "4dir" then
  41. p2_max = 3
  42. elseif def.paramtype2 == "glasslikeliquidlevel" then
  43. p2_max = 63
  44. elseif def.paramtype2 == "meshoptions" and def.drawtype == "plantlike" then
  45. p2_max = 63
  46. elseif def.paramtype2 == "leveled" then
  47. p2_max = 127
  48. elseif def.paramtype2 == "degrotate" and (def.drawtype == "plantlike" or def.drawtype == "mesh") then
  49. p2_max = 239
  50. elseif def.paramtype2 == "colorfacedir" or
  51. def.paramtype2 == "colorwallmounted" or
  52. def.paramtype2 == "colordegrotate" or
  53. def.paramtype2 == "color4dir" or
  54. def.paramtype2 == "color" then
  55. p2_max = 255
  56. end
  57. end
  58. for p2 = 0, p2_max do
  59. -- Skip undefined param2 values
  60. if not ((def.paramtype2 == "meshoptions" and p2 % 8 > 4) or
  61. (def.paramtype2 == "colorwallmounted" and p2 % 8 > 5) or
  62. ((def.paramtype2 == "colorfacedir" or def.paramtype2 == "colordegrotate")
  63. and p2 % 32 > 23)) then
  64. core.set_node(pos, { name = itemstring, param2 = p2 })
  65. nodes_placed = nodes_placed + 1
  66. pos = advance_pos(pos, start_pos)
  67. if not pos then
  68. aborted = true
  69. break
  70. end
  71. end
  72. end
  73. if aborted then
  74. break
  75. end
  76. end
  77. if aborted then
  78. core.chat_send_player(name, "Not all nodes could be placed, please move further away from the world boundary. Nodes placed: "..nodes_placed)
  79. end
  80. core.chat_send_player(name, "Nodes placed: "..nodes_placed..".")
  81. end
  82. local function after_emerge(blockpos, action, calls_remaining, param)
  83. if calls_remaining == 0 then
  84. place_nodes(param)
  85. end
  86. end
  87. core.register_chatcommand("test_place_nodes", {
  88. params = "[ no_param2 ]",
  89. description = "Test: Place all nodes (except dummy and callback nodes) and optionally their permissible param2 variants",
  90. func = function(name, param)
  91. local player = core.get_player_by_name(name)
  92. if not player then
  93. return false, "No player."
  94. end
  95. local pos = vector.floor(player:get_pos())
  96. pos.x = math.ceil(pos.x + 3)
  97. pos.z = math.ceil(pos.z + 3)
  98. pos.y = math.ceil(pos.y + 1)
  99. local start_pos = table.copy(pos)
  100. if pos.x > 30800 then
  101. return false, "Too close to world boundary (+X). Please move to X < 30800."
  102. end
  103. if pos.z > 30800 then
  104. return false, "Too close to world boundary (+Z). Please move to Z < 30800."
  105. end
  106. local aborted = false
  107. local nodes = {}
  108. local emerge_estimate = 0
  109. for itemstring, def in pairs(core.registered_nodes) do
  110. if itemstring ~= "ignore" and
  111. -- Skip callback test and dummy nodes
  112. -- to avoid clutter and chat spam
  113. core.get_item_group(itemstring, "callback_test") == 0 and
  114. core.get_item_group(itemstring, "dummy") == 0 then
  115. table.insert(nodes, itemstring)
  116. if def.paramtype2 == 0 then
  117. emerge_estimate = emerge_estimate + 1
  118. else
  119. emerge_estimate = emerge_estimate + 255
  120. end
  121. end
  122. end
  123. -- Emerge area to make sure that all nodes are being placed.
  124. -- Note we will emerge much more than we need to (overestimation),
  125. -- the estimation code could be improved performance-wise …
  126. local length = 16 + math.ceil(emerge_estimate / 24) * 2
  127. core.emerge_area(start_pos,
  128. { x = start_pos.x + 46, y = start_pos.y, z = start_pos.z + length },
  129. after_emerge, { nodes = nodes, name = name, pos = pos, start_pos = start_pos, param = param })
  130. return true, "Emerging area …"
  131. end,
  132. })