init.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. --------------------------------------------------------------------------------
  2. -- Mapfix, MTS version.
  3. -- Authored by MustTest.
  4. -- License: MIT.
  5. --------------------------------------------------------------------------------
  6. -- Mod is reloadable.
  7. mapfix = mapfix or {}
  8. mapfix.players = mapfix.players or {}
  9. mapfix.modpath = minetest.get_modpath("mapfix")
  10. -- Localize for performance.
  11. local vector_round = vector.round
  12. local math_floor = math.floor
  13. -- Configurable settings.
  14. local MIN_TIMEOUT = 15
  15. local DEFAULT_RADIUS = 40
  16. local MAX_RADIUS = 60
  17. local function work(minp, maxp)
  18. local vm = minetest.get_voxel_manip(minp, maxp)
  19. vm:update_liquids()
  20. vm:write_to_map(true) -- Fix lighting while you're at it.
  21. return vm:get_emerged_area()
  22. end
  23. -- Export this function publicly.
  24. mapfix.work = work
  25. -- Public API function. May be called from other mods.
  26. function mapfix.execute(pos, radius)
  27. pos = vector_round(pos)
  28. radius = math_floor(radius)
  29. local minp = vector.subtract(pos, radius)
  30. local maxp = vector.add(pos, radius)
  31. work(minp, maxp)
  32. end
  33. -- Chat-command callback function.
  34. -- Handles minimum timeout and required privileges.
  35. -- Called by KoC.
  36. mapfix.command = function(pname, param)
  37. -- Profile function execution time.
  38. local t1 = os.clock()
  39. local player = minetest.get_player_by_name(pname)
  40. if not player or not player:is_player() then
  41. return
  42. end
  43. -- Determine radius.
  44. local radius = DEFAULT_RADIUS
  45. if param and param ~= "" then
  46. radius = tonumber(param:trim())
  47. if not radius then
  48. minetest.chat_send_player(pname, "# Server: Usage: /mapfix [<radius>].")
  49. return
  50. end
  51. end
  52. -- Check timeout delay for this user.
  53. if mapfix.players[pname] then
  54. local tnow = os.time()
  55. local prev = mapfix.players[pname]
  56. local diff = tnow - prev
  57. -- Check if function was called too soon.
  58. if diff < MIN_TIMEOUT then
  59. local remain = math.ceil(MIN_TIMEOUT - diff)
  60. -- Grammar adjustment.
  61. local str = "seconds"
  62. if remain == 1 then
  63. str = "second"
  64. end
  65. minetest.chat_send_player(pname,
  66. "# Server: Too soon to run /mapfix again! Wait " .. remain ..
  67. " more " .. str .. ".")
  68. return
  69. end
  70. -- Store time of last call to this function.
  71. mapfix.players[pname] = tnow
  72. else
  73. -- Store time of last call to this function.
  74. mapfix.players[pname] = os.time()
  75. end
  76. local pos = vector_round(player:get_pos())
  77. radius = math_floor(radius)
  78. -- Check privs.
  79. if radius > MAX_RADIUS then
  80. local privs = minetest.check_player_privs(pname, {mapfix=true})
  81. if not privs then
  82. minetest.chat_send_player(pname,
  83. "# Server: You cannot exceed radius " .. MAX_RADIUS ..
  84. "! Your privileges are insufficient.")
  85. return
  86. end
  87. elseif radius < 0 then
  88. minetest.chat_send_player(pname, "# Server: Radius cannot be negative!")
  89. return
  90. end
  91. local minp = vector.subtract(pos, radius)
  92. local maxp = vector.add(pos, radius)
  93. minetest.log("action",
  94. "Player <" .. pname .. "> executed /mapfix with radius " .. radius ..
  95. " at " .. minetest.pos_to_string(pos) .. ".")
  96. minp, maxp = work(minp, maxp)
  97. -- Calculate elapsed time.
  98. local t2 = os.clock()
  99. local totalms = math.ceil((t2 - t1) * 1000)
  100. minetest.chat_send_player(pname,
  101. "# Server: Liquid & light recalculation finished! Extents: " ..
  102. rc.pos_to_namestr(minp) .. " to " .. rc.pos_to_namestr(maxp) ..
  103. ". Radius: " .. radius .. ". Took " .. totalms .. " milliseconds.")
  104. end
  105. if not mapfix.registered then
  106. -- Privilege required in order to execute /mapfix for very large areas.
  107. minetest.register_privilege("mapfix", {
  108. description = "Player may execute /mapfix with an arbitrary radius.",
  109. give_to_singleplayer = false,
  110. })
  111. -- Allow players to use command from chat console.
  112. -- No privs required.
  113. minetest.register_chatcommand("mapfix", {
  114. params = "[<radius>]",
  115. description = "Request a recalculation of nearby liquids and light.",
  116. func = function(...)
  117. mapfix.command(...)
  118. end,
  119. })
  120. -- Register mod as reloadable.
  121. local c = "mapfix:core"
  122. local f = mapfix.modpath .. "/init.lua"
  123. reload.register_file(c, f, false)
  124. mapfix.registered = true
  125. end