init.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. local mod_skylayer = minetest.get_modpath("skylayer") ~= nil
  2. local modpath = minetest.get_modpath("moon_phases");
  3. local GSCYCLE = 0.5 -- global step cycle
  4. local DEFAULT_LENGTH = 4 -- default cycle length
  5. local DEFAULT_STYLE = "classic" -- default texture style
  6. moon_phases = {}
  7. local state = minetest.get_mod_storage()
  8. if not state:contains("day") then
  9. state:from_table({
  10. day = 1,
  11. phase = 1,
  12. change_time = 1
  13. })
  14. end
  15. -- retrieve and parse mod configuration
  16. local function get_cycle_config()
  17. local config = minetest.settings:get("moon_phases_cycle") or DEFAULT_LENGTH
  18. config = math.floor(tonumber(config))
  19. if (not config) or config < 0 then
  20. minetest.log("warning", "[Moon Phases] Invalid cycle configuration")
  21. return DEFAULT_LENGTH
  22. end
  23. return config
  24. end
  25. local PHASE_LENGTH = get_cycle_config()
  26. -- set the moon texture of a player to the given texture
  27. local function set_texture(player, texture)
  28. local sl = {}
  29. sl.name = "moon_phases:custom"
  30. sl.moon_data = {
  31. visible = true,
  32. texture = texture,
  33. scale = 0.8
  34. }
  35. if mod_skylayer then
  36. skylayer.add_layer(player:get_player_name(), sl)
  37. else
  38. player:set_moon(sl.moon_data)
  39. end
  40. end
  41. -- update moon textures of all online players
  42. local function update_textures()
  43. local phase = state:get_int("phase")
  44. for _, player in ipairs(minetest.get_connected_players()) do
  45. set_texture(player, "moon_" .. phase .. ".png")
  46. end
  47. end
  48. -- check for day changes
  49. local function handle_time_progression()
  50. local time = minetest.get_timeofday()
  51. local day = state:get_int("day")
  52. local phase = state:get_int("phase")
  53. local change_time = state:get_int("change_time") == 1
  54. if time >= 0.5 and change_time then
  55. day = day + 1
  56. state:set_int("day", day)
  57. if day % PHASE_LENGTH == 0 then
  58. state:set_int("phase", (phase % 8) + 1)
  59. state:set_int("change_time", 0)
  60. update_textures()
  61. end
  62. elseif time < 0.5 and not change_time then
  63. state:set_int("change_time", 1)
  64. end
  65. end
  66. -- return the current moon phase
  67. function moon_phases.get_phase()
  68. return state:get_int("phase")
  69. end
  70. -- set the current moon phase
  71. -- @param phase int Phase between 1 and 8
  72. function moon_phases.set_phase(phase)
  73. phase = math.floor(tonumber(phase))
  74. if (not phase) or phase < 1 or phase > 8 then
  75. return false
  76. end
  77. state:set_int("phase", phase)
  78. update_textures()
  79. return true
  80. end
  81. -- set the moon texture of newly joined player
  82. minetest.register_on_joinplayer(function(player)
  83. local phase = state:get_int("phase")
  84. -- phase might not have been set at server start
  85. if phase < 1 then phase = 1 end
  86. set_texture(player, "moon_" .. phase .. ".png")
  87. end)
  88. -- check for day changes and call handlers
  89. local timer = 0
  90. minetest.register_globalstep(function(dtime)
  91. timer = timer + dtime
  92. if timer < GSCYCLE then return end
  93. handle_time_progression()
  94. timer = 0
  95. end)
  96. -- include API for chat commands
  97. dofile(modpath .. "/commands.lua")