init.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. -- Start fresh.
  2. -- Any mods wishing to register additional ores must run after this one.
  3. minetest.clear_registered_ores()
  4. minetest.clear_registered_biomes()
  5. minetest.clear_registered_decorations()
  6. mapgen = mapgen or {}
  7. mapgen.modpath = minetest.get_modpath("mapgen")
  8. mapgen.blames = mapgen.blames or {}
  9. local vector_distance = vector.distance
  10. local reload_or_dofile = function(name, path)
  11. if minetest.get_modpath("reload") then
  12. reload.register_file(name, path)
  13. else
  14. dofile(path)
  15. end
  16. end
  17. function mapgen.nearest_player(minp, maxp)
  18. local x = minp.x + maxp.x / 2
  19. local y = minp.y + maxp.y / 2
  20. local z = minp.z + maxp.z / 2
  21. local p = {x=x, y=y, z=z}
  22. local z = minetest.get_connected_players()
  23. local g = {}
  24. for k, v in ipairs(z) do
  25. g[#g+1] = v
  26. end
  27. table.sort(g, function(a, b)
  28. return vector_distance(a:get_pos(), p) < vector_distance(b:get_pos(), p)
  29. end)
  30. if #g > 0 then
  31. return g[1]
  32. end
  33. end
  34. function mapgen.most_blamed()
  35. local t = {}
  36. for k, v in pairs(mapgen.blames) do
  37. t[#t+1] = {name=k, count=v}
  38. end
  39. table.sort(t, function(a, b)
  40. return a.count > b.count
  41. end)
  42. if #t > 0 then
  43. return t[1].name
  44. end
  45. end
  46. if not minetest.is_singleplayer() then
  47. if not mapgen.chat_registered then
  48. -- Set up vars.
  49. mapgen.report_time = mapgen.report_time or 0
  50. mapgen.report_chunks = mapgen.report_chunks or 0
  51. local function notify_chat(minp, maxp, seed)
  52. local player = mapgen.nearest_player(minp, maxp)
  53. if player then
  54. local pname = player:get_player_name()
  55. if mapgen.blames[pname] then
  56. mapgen.blames[pname] = mapgen.blames[pname] + 1
  57. else
  58. mapgen.blames[pname] = 1
  59. end
  60. end
  61. local time = os.time() -- Time since epoc in seconds.
  62. if (time - mapgen.report_time) > 120 and mapgen.report_chunks > 0 then
  63. local blamed = mapgen.most_blamed()
  64. if blamed and mapgen.report_chunks >= 5 then
  65. minetest.chat_send_all(
  66. "# Server: Mapgen scrambling. Blame <" .. rename.gpn(blamed) .. "> for lag. Chunks: " ..
  67. mapgen.report_chunks .. ".")
  68. end
  69. mapgen.report_time = time
  70. mapgen.report_chunks = 0
  71. mapgen.blames = {} -- Clear blames.
  72. end
  73. mapgen.report_chunks = mapgen.report_chunks + 1
  74. end
  75. -- Inform players periodically.
  76. minetest.register_on_generated(notify_chat)
  77. mapgen.chat_registered = true
  78. end
  79. end
  80. if not mapgen.files_registered then
  81. local mp = mapgen.modpath
  82. -- These files are reloadable. Their functions can be changed at runtime.
  83. reload_or_dofile("mapgen:shrubs", mp .. "/shrubs.lua")
  84. reload_or_dofile("mapgen:papyrus", mp .. "/papyrus.lua")
  85. reload_or_dofile("mapgen:grass", mp .. "/grass.lua")
  86. if minetest.get_modpath("flowers") then
  87. reload_or_dofile("mapgen:flowers", mp .. "/flowers.lua")
  88. reload_or_dofile("mapgen:mushrooms", mp .. "/mushrooms.lua")
  89. end
  90. -- Ore and biome registration.
  91. dofile(mp .. "/mg_alias.lua")
  92. dofile(mp .. "/mapgen.lua")
  93. dofile(mp .. "/biome.lua")
  94. minetest.register_on_generated(function(minp, maxp, seed)
  95. mapgen.generate_dry_shrubs(minp, maxp, seed)
  96. mapgen.generate_papyrus(minp, maxp, seed)
  97. mapgen.generate_grass(minp, maxp, seed)
  98. end)
  99. if minetest.get_modpath("flowers") then
  100. minetest.register_on_generated(function(minp, maxp, seed)
  101. mapgen.generate_flowers(minp, maxp, seed)
  102. mapgen.generate_mushrooms(minp, maxp, seed)
  103. end)
  104. end
  105. mapgen.files_registered = true
  106. end