init.lua 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. local mode = core.settings:get("helper_mode")
  2. if mode == "devtest" then
  3. -- Provide feedback to script by creating files in world path
  4. core.after(0, function()
  5. io.close(io.open(core.get_worldpath() .. "/startup", "w"))
  6. end)
  7. local function callback(test_ok)
  8. if not test_ok then
  9. io.close(io.open(core.get_worldpath() .. "/test_failure", "w"))
  10. end
  11. io.close(io.open(core.get_worldpath() .. "/done", "w"))
  12. core.request_shutdown("", false, 2)
  13. end
  14. -- If tests are enabled exit when they're done, otherwise exit on player join
  15. if core.settings:get_bool("devtest_unittests_autostart") and core.global_exists("unittests") then
  16. unittests.on_finished = callback
  17. else
  18. core.register_on_joinplayer(function() callback(true) end)
  19. end
  20. elseif mode == "mapgen" then
  21. -- Stress-test mapgen by constantly generating new area
  22. local csize = tonumber(core.settings:get("chunksize")) * core.MAP_BLOCKSIZE
  23. local MINP, MAXP = vector.new(0, -csize, 0), vector.new(csize*3, csize*2, csize)
  24. local DIR = "x"
  25. local pstart = vector.new(0, 0, 0)
  26. local next_, callback
  27. next_ = function(arg)
  28. print("emerging " .. core.pos_to_string(pstart))
  29. core.emerge_area(
  30. vector.add(pstart, MINP), vector.add(pstart, MAXP),
  31. callback, arg
  32. )
  33. end
  34. local trig = {}
  35. callback = function(blockpos, action, calls_rem, n)
  36. if action == core.EMERGE_CANCELLED or action == core.EMERGE_ERRORED then
  37. return
  38. end
  39. if calls_rem <= 20 and not trig[n] then
  40. trig[n] = true
  41. pstart[DIR] = pstart[DIR] + (MAXP[DIR] - MINP[DIR])
  42. next_(n + 1)
  43. end
  44. end
  45. core.after(0, next_, 1)
  46. elseif mode == "error" then
  47. local n = tonumber(core.settings:get("error_type"))
  48. local error_lua = core.get_modpath(core.get_current_modname()) .. "/error.lua"
  49. if n == 1 then
  50. print("=> error during startup <=")
  51. error("intentional")
  52. elseif n == 2 then
  53. print("=> error on first step <=")
  54. core.after(0, error, "intentional")
  55. elseif n == 3 then
  56. print("=> error in async script <=")
  57. core.register_async_dofile(error_lua)
  58. elseif n == 4 then
  59. print("=> error in mapgen script <=")
  60. core.register_mapgen_script(error_lua)
  61. else
  62. assert(false)
  63. end
  64. end