upgrades.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. -------------------------------------------------------------------------------------------------------
  2. -- For upgrading old worlds to the new format. Do this before registering named_waypoints
  3. local worldpath = minetest.get_worldpath()
  4. local v1filename = worldpath.."/settlements.txt"
  5. local v2filename = worldpath.."/settlements_areastore.txt"
  6. local v3filename = worldpath.."/named_waypoints_settlements.txt"
  7. -- delayed to account for circular dependency on medieval settlement (for namegen)
  8. -- and to allow settlements to be registered
  9. local function upgrade(settlement_list)
  10. local medieval_def = settlements.registered_settlements["medieval"]
  11. for id, data in pairs(settlement_list) do
  12. local pos = data.min
  13. local dat = minetest.deserialize(data.data)
  14. if not dat.name then
  15. dat.settlement_type = dat.settlement_type or "medieval"
  16. if medieval_def then
  17. dat.name = medieval_def.generate_name(pos)
  18. end
  19. end
  20. named_waypoints.add_waypoint("settlements", pos, dat)
  21. end
  22. end
  23. local v3file = io.open(v3filename, "r")
  24. if not v3file then -- if the v3 file exists, no upgrade should be done
  25. local v2file = io.open(v2filename, "r")
  26. if v2file then
  27. minetest.log("action", "[settlements] found v2 settlements file but no v3 settlements file, upgrading")
  28. -- v2 file exists but not v3, upgrade v2 to v3
  29. local v2area = AreaStore()
  30. v2area:from_file(v2filename)
  31. local v2settlements = v2area:get_areas_in_area({x=-32000, y=-32000, z=-32000}, {x=32000, y=32000, z=32000}, true, true, true)
  32. minetest.after(5, upgrade, v2settlements)
  33. else
  34. local v1file = io.open(v1filename, "r")
  35. if v1file then
  36. minetest.log("action", "[settlements] found v1 settlements file but no v2 or v3 settlements file, upgrading")
  37. local settlements = minetest.deserialize(v1file:read("*all"))
  38. local v1settlements = {}
  39. for _, pos in ipairs(settlements) do
  40. table.insert(v1settlements, {min = pos, data = minetest.serialize({})})
  41. end
  42. minetest.after(5, upgrade, v1settlements)
  43. end
  44. end
  45. end