1
0
Fork 0
MineClone2/mods/ENVIRONMENT/mcl_moon/init.lua

63 lines
1.6 KiB
Lua
Raw Permalink Normal View History

2020-04-08 19:32:37 +00:00
local MOON_PHASES = 8
local MOON_PHASES_HALF = MOON_PHASES / 2
local SHEET_W = 4
local SHEET_H = 2
2020-04-08 20:05:23 +00:00
-- Randomize initial moon phase, based on map seed
local mg_seed = minetest.get_mapgen_setting("seed")
local rand = PseudoRandom(mg_seed)
local phase_offset = rand:next(0, MOON_PHASES - 1)
minetest.log("info", "[mcl_moon] Moon phase offset of this world: "..phase_offset)
2020-04-08 19:32:37 +00:00
mcl_moon = {}
mcl_moon.MOON_PHASES = MOON_PHASES
2021-05-25 11:18:12 +00:00
function mcl_moon.get_moon_phase()
2020-04-08 19:32:37 +00:00
local after_midday = 0
2020-04-08 20:05:23 +00:00
-- Moon phase changes after midday
2020-04-08 19:32:37 +00:00
local tod = minetest.get_timeofday()
if tod > 0.5 then
after_midday = 1
end
2020-04-08 20:05:23 +00:00
return (minetest.get_day_count() + phase_offset + after_midday) % MOON_PHASES
2020-04-08 19:32:37 +00:00
end
2021-05-25 11:18:12 +00:00
local function get_moon_texture()
2020-04-08 19:32:37 +00:00
local phase = mcl_moon.get_moon_phase()
local x = phase % MOON_PHASES_HALF
local y
if phase >= MOON_PHASES_HALF then
y = 1
else
y = 0
end
return "mcl_moon_moon_phases.png^[sheet:"..SHEET_W.."x"..SHEET_H..":"..x..","..y
end
local timer = 0
local last_reported_phase = nil
minetest.register_globalstep(function(dtime)
timer = timer + dtime
2020-04-08 20:05:23 +00:00
if timer < 8 then
2020-04-08 19:32:37 +00:00
return
end
timer = 0
local phase = mcl_moon.get_moon_phase()
-- No-op when moon phase didn't change yet
if last_reported_phase == phase then
return
end
minetest.log("info", "[mcl_moon] New moon phase: "..phase)
last_reported_phase = phase
local moon_arg = {texture = get_moon_texture()}
local players = minetest.get_connected_players()
for p=1, #players do
players[p]:set_moon(moon_arg)
end
end)
minetest.register_on_joinplayer(function(player)
player:set_moon({texture = get_moon_texture(), scale=3.75})
end)