auto_update.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. local import_mod = ...
  2. local function get_mod_chunk_mtime(chunk_pos)
  3. local _,_,mtime = import_mod.read_chunk_header(chunk_pos)
  4. return mtime
  5. end
  6. local function get_world_chunk_mtime(chunk_pos)
  7. local mtime = import_mod.storage:get_int(minetest.pos_to_string(chunk_pos))
  8. if mtime == 0 then
  9. return nil
  10. else
  11. return mtime
  12. end
  13. end
  14. local cache = {}
  15. local function check_player_pos(player)
  16. local ppos = player:get_pos()
  17. local chunk_pos = import_mod.get_chunkpos(ppos)
  18. -- cache access
  19. local cache_key = minetest.pos_to_string(chunk_pos)
  20. if cache[cache_key] then
  21. return
  22. end
  23. cache[cache_key] = true
  24. -- retrieve timestamps
  25. local mod_mtime = get_mod_chunk_mtime(chunk_pos)
  26. local world_mtime = get_world_chunk_mtime(chunk_pos)
  27. if not mod_mtime then
  28. -- the chunk isn't available in the mod
  29. return
  30. end
  31. if world_mtime and world_mtime >= mod_mtime then
  32. -- world chunk is same or newer (?) than the one in the mod
  33. return
  34. end
  35. local mapblock_min, mapblock_max = import_mod.get_mapblock_bounds_from_chunk(chunk_pos)
  36. local min = import_mod.get_mapblock_bounds_from_mapblock(mapblock_min)
  37. local _, max = import_mod.get_mapblock_bounds_from_mapblock(mapblock_max)
  38. minetest.delete_area(min, max)
  39. end
  40. local function check_players()
  41. for _, player in ipairs(minetest.get_connected_players()) do
  42. check_player_pos(player)
  43. end
  44. minetest.after(1, check_players)
  45. end
  46. print("[modgen] map auto-update enabled")
  47. minetest.after(1, check_players)