forceloading.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, limit)
  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. limit = limit or tonumber(core.settings:get("max_forceloaded_blocks")) or 16
  44. if limit >= 0 and total_forceloaded >= limit then
  45. return false
  46. end
  47. total_forceloaded = total_forceloaded+1
  48. relevant_table[hash] = 1
  49. forceload_block(blockpos)
  50. return true
  51. end
  52. end
  53. function core.forceload_free_block(pos, transient)
  54. -- set changed flag
  55. forceload_blocks_changed = true
  56. local blockpos = get_blockpos(pos)
  57. local hash = core.hash_node_position(blockpos)
  58. local relevant_table, other_table = get_relevant_tables(transient)
  59. if relevant_table[hash] == nil then return end
  60. if relevant_table[hash] > 1 then
  61. relevant_table[hash] = relevant_table[hash] - 1
  62. elseif other_table[hash] ~= nil then
  63. relevant_table[hash] = nil
  64. else
  65. total_forceloaded = total_forceloaded-1
  66. relevant_table[hash] = nil
  67. forceload_free_block(blockpos)
  68. end
  69. end
  70. -- Keep the forceloaded areas after restart
  71. local wpath = core.get_worldpath()
  72. local function read_file(filename)
  73. local f = io.open(filename, "r")
  74. if f==nil then return {} end
  75. local t = f:read("*all")
  76. f:close()
  77. if t=="" or t==nil then return {} end
  78. return core.deserialize(t) or {}
  79. end
  80. blocks_forceloaded = read_file(wpath.."/force_loaded.txt")
  81. for _, __ in pairs(blocks_forceloaded) do
  82. total_forceloaded = total_forceloaded + 1
  83. end
  84. core.after(5, function()
  85. for hash, _ in pairs(blocks_forceloaded) do
  86. local blockpos = core.get_position_from_hash(hash)
  87. forceload_block(blockpos)
  88. end
  89. end)
  90. -- persists the currently forceloaded blocks to disk
  91. local function persist_forceloaded_blocks()
  92. local data = core.serialize(blocks_forceloaded)
  93. core.safe_file_write(wpath.."/force_loaded.txt", data)
  94. end
  95. -- periodical forceload persistence
  96. local function periodically_persist_forceloaded_blocks()
  97. -- only persist if the blocks actually changed
  98. if forceload_blocks_changed then
  99. persist_forceloaded_blocks()
  100. -- reset changed flag
  101. forceload_blocks_changed = false
  102. end
  103. -- recheck after some time
  104. core.after(10, periodically_persist_forceloaded_blocks)
  105. end
  106. -- persist periodically
  107. core.after(5, periodically_persist_forceloaded_blocks)
  108. -- persist on shutdown
  109. core.register_on_shutdown(persist_forceloaded_blocks)