commands.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. minetest.register_chatcommand("test_inv", {
  2. params = "",
  3. description = "Test: Modify player's inventory formspec",
  4. func = function(name, param)
  5. local player = minetest.get_player_by_name(name)
  6. if not player then
  7. return false, "No player."
  8. end
  9. player:set_inventory_formspec(
  10. "size[13,7.5]"..
  11. "image[6,0.6;1,2;player.png]"..
  12. "list[current_player;main;5,3.5;8,4;]"..
  13. "list[current_player;craft;8,0;3,3;]"..
  14. "list[current_player;craftpreview;12,1;1,1;]"..
  15. "list[detached:test_inventory;main;0,0;4,6;0]"..
  16. "button[0.5,7;2,1;button1;Button 1]"..
  17. "button_exit[2.5,7;2,1;button2;Exit Button]")
  18. return true, "Done."
  19. end,
  20. })
  21. minetest.register_chatcommand("test_bulk_set_node", {
  22. params = "",
  23. description = "Test: Bulk-set 9×9×9 stone nodes",
  24. func = function(name, param)
  25. local player = minetest.get_player_by_name(name)
  26. if not player then
  27. return false, "No player."
  28. end
  29. local pos_list = {}
  30. local ppos = player:get_pos()
  31. local i = 1
  32. for x=2,10 do
  33. for y=2,10 do
  34. for z=2,10 do
  35. pos_list[i] = {x=ppos.x + x,y = ppos.y + y,z = ppos.z + z}
  36. i = i + 1
  37. end
  38. end
  39. end
  40. minetest.bulk_set_node(pos_list, {name = "mapgen_stone"})
  41. return true, "Done."
  42. end,
  43. })
  44. minetest.register_chatcommand("bench_bulk_set_node", {
  45. params = "",
  46. description = "Benchmark: Bulk-set 99×99×99 stone nodes",
  47. func = function(name, param)
  48. local player = minetest.get_player_by_name(name)
  49. if not player then
  50. return false, "No player."
  51. end
  52. local pos_list = {}
  53. local ppos = player:get_pos()
  54. local i = 1
  55. for x=2,100 do
  56. for y=2,100 do
  57. for z=2,100 do
  58. pos_list[i] = {x=ppos.x + x,y = ppos.y + y,z = ppos.z + z}
  59. i = i + 1
  60. end
  61. end
  62. end
  63. minetest.chat_send_player(name, "Benchmarking minetest.bulk_set_node. Warming up ...");
  64. -- warm up with stone to prevent having different callbacks
  65. -- due to different node topology
  66. minetest.bulk_set_node(pos_list, {name = "mapgen_stone"})
  67. minetest.chat_send_player(name, "Warming up finished, now benchmarking ...");
  68. local start_time = minetest.get_us_time()
  69. for i=1,#pos_list do
  70. minetest.set_node(pos_list[i], {name = "mapgen_stone"})
  71. end
  72. local middle_time = minetest.get_us_time()
  73. minetest.bulk_set_node(pos_list, {name = "mapgen_stone"})
  74. local end_time = minetest.get_us_time()
  75. local msg = string.format("Benchmark results: minetest.set_node loop: %.2f ms; minetest.bulk_set_node: %.2f ms",
  76. ((middle_time - start_time)) / 1000,
  77. ((end_time - middle_time)) / 1000
  78. )
  79. return true, msg
  80. end,
  81. })
  82. local function advance_pos(pos, start_pos, advance_z)
  83. if advance_z then
  84. pos.z = pos.z + 2
  85. pos.x = start_pos.x
  86. else
  87. pos.x = pos.x + 2
  88. end
  89. if pos.x > 30900 or pos.x - start_pos.x > 46 then
  90. pos.x = start_pos.x
  91. pos.z = pos.z + 2
  92. end
  93. if pos.z > 30900 then
  94. -- We ran out of space! Aborting
  95. aborted = true
  96. return false
  97. end
  98. return pos
  99. end
  100. local function place_nodes(param)
  101. local nodes = param.nodes
  102. local name = param.name
  103. local pos = param.pos
  104. local start_pos = param.start_pos
  105. table.sort(nodes)
  106. minetest.chat_send_player(name, "Placing nodes …")
  107. local nodes_placed = 0
  108. local aborted = false
  109. for n=1, #nodes do
  110. local itemstring = nodes[n]
  111. local def = minetest.registered_nodes[itemstring]
  112. local p2_max = 0
  113. if param.param ~= "no_param2" then
  114. -- Also test the param2 values of the nodes
  115. -- ... but we only use permissible param2 values
  116. if def.paramtype2 == "wallmounted" then
  117. p2_max = 5
  118. elseif def.paramtype2 == "facedir" then
  119. p2_max = 23
  120. elseif def.paramtype2 == "glasslikeliquidlevel" then
  121. p2_max = 63
  122. elseif def.paramtype2 == "meshoptions" and def.drawtype == "plantlike" then
  123. p2_max = 63
  124. elseif def.paramtype2 == "leveled" then
  125. p2_max = 127
  126. elseif def.paramtype2 == "degrotate" and def.drawtype == "plantlike" then
  127. p2_max = 179
  128. elseif def.paramtype2 == "colorfacedir" or
  129. def.paramtype2 == "colorwallmounted" or
  130. def.paramtype2 == "color" then
  131. p2_max = 255
  132. end
  133. end
  134. for p2 = 0, p2_max do
  135. -- Skip undefined param2 values
  136. if not ((def.paramtype2 == "meshoptions" and p2 % 8 > 4) or
  137. (def.paramtype2 == "colorwallmounted" and p2 % 8 > 5) or
  138. (def.paramtype2 == "colorfacedir" and p2 % 32 > 23)) then
  139. minetest.set_node(pos, { name = itemstring, param2 = p2 })
  140. nodes_placed = nodes_placed + 1
  141. pos = advance_pos(pos, start_pos)
  142. if not pos then
  143. aborted = true
  144. break
  145. end
  146. end
  147. end
  148. if aborted then
  149. break
  150. end
  151. end
  152. if aborted then
  153. minetest.chat_send_player(name, "Not all nodes could be placed, please move further away from the world boundary. Nodes placed: "..nodes_placed)
  154. end
  155. minetest.chat_send_player(name, "Nodes placed: "..nodes_placed..".")
  156. end
  157. local function after_emerge(blockpos, action, calls_remaining, param)
  158. if calls_remaining == 0 then
  159. place_nodes(param)
  160. end
  161. end
  162. minetest.register_chatcommand("test_place_nodes", {
  163. params = "[ no_param2 ]",
  164. description = "Test: Place all non-experimental nodes and optionally their permissible param2 variants",
  165. func = function(name, param)
  166. local player = minetest.get_player_by_name(name)
  167. if not player then
  168. return false, "No player."
  169. end
  170. local pos = vector.floor(player:get_pos())
  171. pos.x = math.ceil(pos.x + 3)
  172. pos.z = math.ceil(pos.z + 3)
  173. pos.y = math.ceil(pos.y + 1)
  174. local start_pos = table.copy(pos)
  175. if pos.x > 30800 then
  176. return false, "Too close to world boundary (+X). Please move to X < 30800."
  177. end
  178. if pos.z > 30800 then
  179. return false, "Too close to world boundary (+Z). Please move to Z < 30800."
  180. end
  181. local aborted = false
  182. local nodes = {}
  183. local emerge_estimate = 0
  184. for itemstring, def in pairs(minetest.registered_nodes) do
  185. if itemstring ~= "ignore" and string.sub(itemstring, 1, 13) ~= "experimental:" then
  186. table.insert(nodes, itemstring)
  187. if def.paramtype2 == 0 then
  188. emerge_estimate = emerge_estimate + 1
  189. else
  190. emerge_estimate = emerge_estimate + 255
  191. end
  192. end
  193. end
  194. -- Emerge area to make sure that all nodes are being placed.
  195. -- Note we will emerge much more than we need to (overestimation),
  196. -- the estimation code could be improved performance-wise …
  197. local length = 16 + math.ceil(emerge_estimate / 24) * 2
  198. minetest.emerge_area(start_pos,
  199. { x = start_pos.x + 46, y = start_pos.y, z = start_pos.z + length },
  200. after_emerge, { nodes = nodes, name = name, pos = pos, start_pos = start_pos, param = param })
  201. return true, "Emerging area …"
  202. end,
  203. })
  204. core.register_on_chatcommand(function(name, command, params)
  205. minetest.log("caught command '"..command.."', issued by '"..name.."'. Parameters: '"..params.."'")
  206. end)