block_queue_checks.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. -- Iterate through the mapblock log,
  2. -- populating blocks with new stuff in the process.
  3. minetest.register_globalstep(function(dtime)
  4. if not biome_lib.block_log[1] then return end -- the block log is empty
  5. if math.random(100) > biome_lib.queue_ratio then return end
  6. for s = 1, biome_lib.entries_per_step do
  7. biome_lib.generate_block()
  8. end
  9. end)
  10. -- Periodically wake-up the queue to give old blocks a chance to time-out
  11. -- if the player isn't currently exploring (i.e. they're just playing in one area)
  12. function biome_lib.wake_up_queue()
  13. if #biome_lib.block_recheck_list > 1
  14. and #biome_lib.block_log == 0 then
  15. biome_lib.block_log[#biome_lib.block_log + 1] =
  16. table.copy(biome_lib.block_recheck_list[#biome_lib.block_recheck_list])
  17. biome_lib.block_recheck_list[#biome_lib.block_recheck_list] = nil
  18. biome_lib.run_block_recheck_list = true
  19. biome_lib.dbg("Woke-up the map queue to give old blocks a chance to time-out.", 3)
  20. end
  21. minetest.after(biome_lib.block_queue_wakeup_time, biome_lib.wake_up_queue)
  22. end
  23. biome_lib.wake_up_queue()
  24. -- Play out the entire log all at once on shutdown
  25. -- to prevent unpopulated map areas
  26. local function format_time(t)
  27. if t > 59999999 then
  28. return os.date("!%M minutes and %S seconds", math.ceil(t/1000000))
  29. else
  30. return os.date("!%S seconds", math.ceil(t/1000000))
  31. end
  32. end
  33. function biome_lib.check_remaining_time()
  34. if minetest.get_us_time() > (biome_lib.shutdown_last_timestamp + 10000000) then -- report progress every 10s
  35. biome_lib.shutdown_last_timestamp = minetest.get_us_time()
  36. local entries_remaining = #biome_lib.block_log + #biome_lib.block_recheck_list
  37. local total_purged = biome_lib.starting_count - entries_remaining
  38. local elapsed_time = biome_lib.shutdown_last_timestamp - biome_lib.shutdown_start_time
  39. biome_lib.dbg(string.format("%i entries, approximately %s remaining.",
  40. entries_remaining, format_time(elapsed_time/total_purged * entries_remaining)))
  41. end
  42. end
  43. --Purge the block log at shutdown
  44. minetest.register_on_shutdown(function()
  45. biome_lib.shutdown_start_time = minetest.get_us_time()
  46. biome_lib.shutdown_last_timestamp = minetest.get_us_time()+1
  47. biome_lib.starting_count = #biome_lib.block_log + #biome_lib.block_recheck_list
  48. if biome_lib.starting_count == 0 then
  49. return
  50. end
  51. biome_lib.dbg("Stand by, purging the mapblock log "..
  52. "(there are "..biome_lib.starting_count.." entries) ...", 0)
  53. while #biome_lib.block_log > 0 do
  54. biome_lib.generate_block(true)
  55. biome_lib.check_remaining_time()
  56. end
  57. if #biome_lib.block_recheck_list > 0 then
  58. biome_lib.block_log = table.copy(biome_lib.block_recheck_list)
  59. biome_lib.block_recheck_list = {}
  60. while #biome_lib.block_log > 0 do
  61. biome_lib.generate_block(true)
  62. biome_lib.check_remaining_time()
  63. end
  64. end
  65. biome_lib.dbg("Log purge completed after "..
  66. format_time(minetest.get_us_time() - biome_lib.shutdown_start_time)..".", 0)
  67. end)
  68. -- "Record" the map chunks being generated by the core mapgen,
  69. -- split into individual mapblocks to reduce lag
  70. minetest.register_on_generated(function(minp, maxp, blockseed)
  71. local timestamp = minetest.get_us_time()
  72. for y = 0, 4 do
  73. local miny = minp.y + y*16
  74. if miny >= biome_lib.mapgen_elevation_limit.min
  75. and (miny + 15) <= biome_lib.mapgen_elevation_limit.max then
  76. for x = 0, 4 do
  77. local minx = minp.x + x*16
  78. for z = 0, 4 do
  79. local minz = minp.z + z*16
  80. local bmin = {x=minx, y=miny, z=minz}
  81. local bmax = {x=minx + 15, y=miny + 15, z=minz + 15}
  82. biome_lib.block_log[#biome_lib.block_log + 1] = { bmin, bmax, true, timestamp }
  83. biome_lib.block_log[#biome_lib.block_log + 1] = { bmin, bmax, false, timestamp }
  84. end
  85. end
  86. else
  87. biome_lib.dbg("Did not enqueue mapblocks at elevation "..miny.."m, they're out of range of any generate_plant() calls.", 4)
  88. end
  89. end
  90. biome_lib.run_block_recheck_list = true
  91. end)
  92. if biome_lib.debug_log_level >= 3 then
  93. biome_lib.last_count = 0
  94. function biome_lib.show_pending_block_count()
  95. if biome_lib.last_count ~= #biome_lib.block_log then
  96. biome_lib.dbg(string.format("Pending block counts - ready to process: %-8icurrently deferred: %i",
  97. #biome_lib.block_log, #biome_lib.block_recheck_list), 3)
  98. biome_lib.last_count = #biome_lib.block_log
  99. biome_lib.queue_idle_flag = false
  100. elseif not biome_lib.queue_idle_flag then
  101. if #biome_lib.block_recheck_list > 0 then
  102. biome_lib.dbg("Mapblock queue only contains blocks that can't yet be processed.", 3)
  103. biome_lib.dbg("Idling the queue until new blocks arrive or the next wake-up call occurs.", 3)
  104. else
  105. biome_lib.dbg("Mapblock queue has run dry.", 3)
  106. biome_lib.dbg("Idling the queue until new blocks arrive.", 3)
  107. end
  108. biome_lib.queue_idle_flag = true
  109. end
  110. minetest.after(1, biome_lib.show_pending_block_count)
  111. end
  112. biome_lib.show_pending_block_count()
  113. end