forceloading.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. -- Prevent anyone else accessing those functions
  2. local forceload_block = core.forceload_block
  3. local forceload_free_block = core.forceload_free_block
  4. core.forceload_block = nil
  5. core.forceload_free_block = nil
  6. local blocks_forceloaded
  7. local blocks_temploaded = {}
  8. local total_forceloaded = 0
  9. -- true, if the forceloaded blocks got changed (flag for persistence on-disk)
  10. local forceload_blocks_changed = false
  11. local BLOCKSIZE = core.MAP_BLOCKSIZE
  12. local function get_blockpos(pos)
  13. return {
  14. x = math.floor(pos.x/BLOCKSIZE),
  15. y = math.floor(pos.y/BLOCKSIZE),
  16. z = math.floor(pos.z/BLOCKSIZE)}
  17. end
  18. -- When we create/free a forceload, it's either transient or persistent. We want
  19. -- to add to/remove from the table that corresponds to the type of forceload, but
  20. -- we also need the other table because whether we forceload a block depends on
  21. -- both tables.
  22. -- This function returns the "primary" table we are adding to/removing from, and
  23. -- the other table.
  24. local function get_relevant_tables(transient)
  25. if transient then
  26. return blocks_temploaded, blocks_forceloaded
  27. else
  28. return blocks_forceloaded, blocks_temploaded
  29. end
  30. end
  31. function core.forceload_block(pos, transient)
  32. -- set changed flag
  33. forceload_blocks_changed = true
  34. local blockpos = get_blockpos(pos)
  35. local hash = core.hash_node_position(blockpos)
  36. local relevant_table, other_table = get_relevant_tables(transient)
  37. if relevant_table[hash] ~= nil then
  38. relevant_table[hash] = relevant_table[hash] + 1
  39. return true
  40. elseif other_table[hash] ~= nil then
  41. relevant_table[hash] = 1
  42. else
  43. if total_forceloaded >= (tonumber(core.settings:get("max_forceloaded_blocks")) or 16) then
  44. return false
  45. end
  46. total_forceloaded = total_forceloaded+1
  47. relevant_table[hash] = 1
  48. forceload_block(blockpos)
  49. return true
  50. end
  51. end
  52. function core.forceload_free_block(pos, transient)
  53. -- set changed flag
  54. forceload_blocks_changed = true
  55. local blockpos = get_blockpos(pos)
  56. local hash = core.hash_node_position(blockpos)
  57. local relevant_table, other_table = get_relevant_tables(transient)
  58. if relevant_table[hash] == nil then return end
  59. if relevant_table[hash] > 1 then
  60. relevant_table[hash] = relevant_table[hash] - 1
  61. elseif other_table[hash] ~= nil then
  62. relevant_table[hash] = nil
  63. else
  64. total_forceloaded = total_forceloaded-1
  65. relevant_table[hash] = nil
  66. forceload_free_block(blockpos)
  67. end
  68. end
  69. -- Keep the forceloaded areas after restart
  70. local wpath = core.get_worldpath()
  71. local function read_file(filename)
  72. local f = io.open(filename, "r")
  73. if f==nil then return {} end
  74. local t = f:read("*all")
  75. f:close()
  76. if t=="" or t==nil then return {} end
  77. return core.deserialize(t) or {}
  78. end
  79. blocks_forceloaded = read_file(wpath.."/force_loaded.txt")
  80. for _, __ in pairs(blocks_forceloaded) do
  81. total_forceloaded = total_forceloaded + 1
  82. end
  83. core.after(5, function()
  84. for hash, _ in pairs(blocks_forceloaded) do
  85. local blockpos = core.get_position_from_hash(hash)
  86. forceload_block(blockpos)
  87. end
  88. end)
  89. -- persists the currently forceloaded blocks to disk
  90. local function persist_forceloaded_blocks()
  91. local data = core.serialize(blocks_forceloaded)
  92. core.safe_file_write(wpath.."/force_loaded.txt", data)
  93. end
  94. -- periodical forceload persistence
  95. local function periodically_persist_forceloaded_blocks()
  96. -- only persist if the blocks actually changed
  97. if forceload_blocks_changed then
  98. persist_forceloaded_blocks()
  99. -- reset changed flag
  100. forceload_blocks_changed = false
  101. end
  102. -- recheck after some time
  103. core.after(10, periodically_persist_forceloaded_blocks)
  104. end
  105. -- persist periodically
  106. core.after(5, periodically_persist_forceloaded_blocks)
  107. -- persist on shutdown
  108. core.register_on_shutdown(persist_forceloaded_blocks)