init.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. local invisible = gdac_invis.is_invisible(blamed)
  66. if not invisible then
  67. minetest.chat_send_all(
  68. "# Server: Mapgen scrambling. Blame <" .. rename.gpn(blamed) .. "> for lag. Chunks: " ..
  69. mapgen.report_chunks .. ".")
  70. end
  71. end
  72. mapgen.report_time = time
  73. mapgen.report_chunks = 0
  74. mapgen.blames = {} -- Clear blames.
  75. end
  76. mapgen.report_chunks = mapgen.report_chunks + 1
  77. end
  78. -- Inform players periodically.
  79. minetest.register_on_generated(notify_chat)
  80. mapgen.chat_registered = true
  81. end
  82. end
  83. if not mapgen.files_registered then
  84. local mp = mapgen.modpath
  85. -- These files are reloadable. Their functions can be changed at runtime.
  86. reload_or_dofile("mapgen:shrubs", mp .. "/shrubs.lua")
  87. reload_or_dofile("mapgen:papyrus", mp .. "/papyrus.lua")
  88. reload_or_dofile("mapgen:grass", mp .. "/grass.lua")
  89. if minetest.get_modpath("flowers") then
  90. reload_or_dofile("mapgen:flowers", mp .. "/flowers.lua")
  91. reload_or_dofile("mapgen:mushrooms", mp .. "/mushrooms.lua")
  92. end
  93. -- Ore and biome registration.
  94. dofile(mp .. "/mg_alias.lua")
  95. dofile(mp .. "/mapgen.lua")
  96. dofile(mp .. "/biome.lua")
  97. minetest.register_on_generated(function(minp, maxp, seed)
  98. mapgen.generate_dry_shrubs(minp, maxp, seed)
  99. mapgen.generate_papyrus(minp, maxp, seed)
  100. mapgen.generate_grass(minp, maxp, seed)
  101. end)
  102. if minetest.get_modpath("flowers") then
  103. minetest.register_on_generated(function(minp, maxp, seed)
  104. mapgen.generate_flowers(minp, maxp, seed)
  105. mapgen.generate_mushrooms(minp, maxp, seed)
  106. end)
  107. end
  108. mapgen.files_registered = true
  109. end