2
0

forceloading.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. local BLOCKSIZE = core.MAP_BLOCKSIZE
  10. local function get_blockpos(pos)
  11. return {
  12. x = math.floor(pos.x/BLOCKSIZE),
  13. y = math.floor(pos.y/BLOCKSIZE),
  14. z = math.floor(pos.z/BLOCKSIZE)}
  15. end
  16. -- When we create/free a forceload, it's either transient or persistent. We want
  17. -- to add to/remove from the table that corresponds to the type of forceload, but
  18. -- we also need the other table because whether we forceload a block depends on
  19. -- both tables.
  20. -- This function returns the "primary" table we are adding to/removing from, and
  21. -- the other table.
  22. local function get_relevant_tables(transient)
  23. if transient then
  24. return blocks_temploaded, blocks_forceloaded
  25. else
  26. return blocks_forceloaded, blocks_temploaded
  27. end
  28. end
  29. function core.forceload_block(pos, transient)
  30. local blockpos = get_blockpos(pos)
  31. local hash = core.hash_node_position(blockpos)
  32. local relevant_table, other_table = get_relevant_tables(transient)
  33. if relevant_table[hash] ~= nil then
  34. relevant_table[hash] = relevant_table[hash] + 1
  35. return true
  36. elseif other_table[hash] ~= nil then
  37. relevant_table[hash] = 1
  38. else
  39. if total_forceloaded >= (tonumber(core.settings:get("max_forceloaded_blocks")) or 16) then
  40. return false
  41. end
  42. total_forceloaded = total_forceloaded+1
  43. relevant_table[hash] = 1
  44. forceload_block(blockpos)
  45. return true
  46. end
  47. end
  48. function core.forceload_free_block(pos, transient)
  49. local blockpos = get_blockpos(pos)
  50. local hash = core.hash_node_position(blockpos)
  51. local relevant_table, other_table = get_relevant_tables(transient)
  52. if relevant_table[hash] == nil then return end
  53. if relevant_table[hash] > 1 then
  54. relevant_table[hash] = relevant_table[hash] - 1
  55. elseif other_table[hash] ~= nil then
  56. relevant_table[hash] = nil
  57. else
  58. total_forceloaded = total_forceloaded-1
  59. relevant_table[hash] = nil
  60. forceload_free_block(blockpos)
  61. end
  62. end
  63. -- Keep the forceloaded areas after restart
  64. local wpath = core.get_worldpath()
  65. local function read_file(filename)
  66. local f = io.open(filename, "r")
  67. if f==nil then return {} end
  68. local t = f:read("*all")
  69. f:close()
  70. if t=="" or t==nil then return {} end
  71. return core.deserialize(t) or {}
  72. end
  73. local function write_file(filename, table)
  74. local f = io.open(filename, "w")
  75. f:write(core.serialize(table))
  76. f:close()
  77. end
  78. blocks_forceloaded = read_file(wpath.."/force_loaded.txt")
  79. for _, __ in pairs(blocks_forceloaded) do
  80. total_forceloaded = total_forceloaded + 1
  81. end
  82. core.after(5, function()
  83. for hash, _ in pairs(blocks_forceloaded) do
  84. local blockpos = core.get_position_from_hash(hash)
  85. forceload_block(blockpos)
  86. end
  87. end)
  88. core.register_on_shutdown(function()
  89. write_file(wpath.."/force_loaded.txt", blocks_forceloaded)
  90. end)