2020-04-24 01:38:13 +02:00
|
|
|
local mod_climate_api = minetest.get_modpath("climate_api") ~= nil
|
2020-04-11 04:00:56 +02:00
|
|
|
local mod_skylayer = minetest.get_modpath("skylayer") ~= nil
|
|
|
|
|
|
|
|
local modpath = minetest.get_modpath("moon_phases");
|
|
|
|
|
2020-05-13 16:07:00 +02:00
|
|
|
local GSCYCLE = 0.5 -- global step cycle in seconds
|
|
|
|
local DEFAULT_LENGTH = 4 -- default moon cycle length in days
|
2020-04-24 01:38:13 +02:00
|
|
|
local DEFAULT_STYLE = "classic" -- default texture style
|
|
|
|
local PHASE_COUNT = 8 -- number of phases to go through
|
|
|
|
|
|
|
|
-- retrieve mod configuration
|
2020-05-13 16:07:00 +02:00
|
|
|
local PHASE_LENGTH = tonumber(minetest.settings:get("moon_phases_cycle") or DEFAULT_LENGTH)
|
2020-04-24 01:38:13 +02:00
|
|
|
local TEXTURE_STYLE = minetest.settings:get("moon_phases_style") or DEFAULT_STYLE
|
|
|
|
|
|
|
|
local sky_color = {
|
|
|
|
"#1d293aff",
|
|
|
|
"#1c4b8dff",
|
|
|
|
nil,
|
|
|
|
"#579dffff",
|
|
|
|
nil,
|
|
|
|
"#1c4b8dff",
|
|
|
|
"#1d293aff",
|
|
|
|
"#000000ff"
|
|
|
|
}
|
|
|
|
|
|
|
|
local horizon_color = {
|
|
|
|
"#243347ff",
|
|
|
|
"#235fb3ff",
|
|
|
|
nil,
|
|
|
|
"#73aeffff",
|
|
|
|
nil,
|
|
|
|
"#3079dfff",
|
|
|
|
"#173154ff",
|
|
|
|
"#000000ff"
|
|
|
|
}
|
2020-04-12 00:35:36 +02:00
|
|
|
|
2020-04-11 04:00:56 +02:00
|
|
|
moon_phases = {}
|
2020-05-13 16:07:00 +02:00
|
|
|
local phase = 1
|
2020-04-12 00:35:36 +02:00
|
|
|
local state = minetest.get_mod_storage()
|
2020-04-24 01:38:13 +02:00
|
|
|
|
|
|
|
-- calculate current moon phase from date
|
|
|
|
-- and stored date offset
|
|
|
|
local function calculate_phase()
|
|
|
|
local time = minetest.get_timeofday()
|
|
|
|
local day = minetest.get_day_count() + state:get_int("date_offset")
|
|
|
|
if time > 0.5 then
|
|
|
|
day = day + 1
|
|
|
|
end
|
|
|
|
return ((math.ceil(day / PHASE_LENGTH) - 1) % PHASE_COUNT) + 1
|
2020-04-12 00:35:36 +02:00
|
|
|
end
|
2020-04-11 04:00:56 +02:00
|
|
|
|
2020-04-24 01:38:13 +02:00
|
|
|
-- return the current moon phase
|
|
|
|
function moon_phases.get_phase()
|
|
|
|
return phase
|
|
|
|
end
|
2020-04-11 04:00:56 +02:00
|
|
|
|
2020-04-12 01:45:37 +02:00
|
|
|
-- set the moon texture of a player to the given phase
|
|
|
|
local function set_texture(player, phase)
|
2020-04-24 01:38:13 +02:00
|
|
|
if not player.get_stars then return end -- check for new sky API
|
2020-04-12 01:45:37 +02:00
|
|
|
local meta_data = player:get_meta()
|
|
|
|
local style = meta_data:get_string("moon_phases:texture_style")
|
|
|
|
if style ~= "classic" and style ~= "realistic" then
|
|
|
|
style = TEXTURE_STYLE
|
|
|
|
end
|
|
|
|
local texture = "moon_" .. phase .. "_" .. style .. ".png"
|
2020-04-24 01:38:13 +02:00
|
|
|
local name = "moon_phases:cycle"
|
|
|
|
local sky = {}
|
|
|
|
sky.sky_data = {
|
|
|
|
type = "regular",
|
|
|
|
sky_color = {
|
|
|
|
night_sky = sky_color[phase],
|
|
|
|
night_horizon = horizon_color[phase]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sky.moon_data = {
|
2020-04-11 04:00:56 +02:00
|
|
|
visible = true,
|
2020-04-12 00:35:36 +02:00
|
|
|
texture = texture,
|
|
|
|
scale = 0.8
|
2020-04-11 04:00:56 +02:00
|
|
|
}
|
2020-04-24 01:38:13 +02:00
|
|
|
local playername = player:get_player_name()
|
|
|
|
if mod_climate_api then
|
|
|
|
sky.priority = 0
|
2020-05-13 16:07:00 +02:00
|
|
|
climate_api.skybox.add(playername, name, sky)
|
2020-04-24 01:38:13 +02:00
|
|
|
elseif mod_skylayer then
|
|
|
|
sky.name = name
|
|
|
|
skylayer.add_layer(playername, sky)
|
2020-04-11 04:00:56 +02:00
|
|
|
else
|
2020-04-24 01:38:13 +02:00
|
|
|
player:set_sky(sky.sky_data)
|
|
|
|
player:set_moon(sky.moon_data)
|
2020-04-11 04:00:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-12 00:35:36 +02:00
|
|
|
-- check for day changes
|
2020-04-11 04:00:56 +02:00
|
|
|
local function handle_time_progression()
|
2020-04-24 01:38:13 +02:00
|
|
|
local n_phase = calculate_phase()
|
|
|
|
if n_phase ~= phase then
|
|
|
|
phase = n_phase
|
|
|
|
for _, player in ipairs(minetest.get_connected_players()) do
|
|
|
|
set_texture(player, phase)
|
2020-04-11 04:00:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-12 00:35:36 +02:00
|
|
|
-- set the current moon phase
|
2020-04-24 01:38:13 +02:00
|
|
|
-- @param phase int Phase between 1 and PHASE_COUNT
|
|
|
|
function moon_phases.set_phase(nphase)
|
|
|
|
nphase = math.floor(tonumber(nphase))
|
|
|
|
if (not nphase) or nphase < 1 or nphase > PHASE_COUNT then
|
2020-04-11 04:00:56 +02:00
|
|
|
return false
|
|
|
|
end
|
2020-04-24 01:38:13 +02:00
|
|
|
local day = minetest.get_day_count()
|
|
|
|
local date_offset = state:get_int("date_offset")
|
|
|
|
local progress = (day + date_offset) % PHASE_LENGTH
|
|
|
|
local phase_offset = (nphase - phase + PHASE_COUNT) % PHASE_COUNT
|
|
|
|
local add_offset = ((phase_offset * PHASE_LENGTH) + date_offset - progress)
|
|
|
|
state:set_int("date_offset", add_offset)
|
|
|
|
handle_time_progression()
|
2020-04-11 04:00:56 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-04-12 01:45:37 +02:00
|
|
|
-- set the moon's texture style for the given player
|
|
|
|
function moon_phases.set_style(player, style)
|
2020-04-24 01:38:13 +02:00
|
|
|
if style ~= nil and style ~= "classic" and style ~= "realistic" then
|
2020-04-12 01:45:37 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
local meta_data = player:get_meta()
|
|
|
|
meta_data:set_string("moon_phases:texture_style", style)
|
|
|
|
set_texture(player, state:get_int("phase"))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-04-12 00:35:36 +02:00
|
|
|
-- set the moon texture of newly joined player
|
2020-04-11 04:00:56 +02:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
2020-04-12 01:45:37 +02:00
|
|
|
set_texture(player, phase)
|
2020-04-11 04:00:56 +02:00
|
|
|
end)
|
|
|
|
|
2020-04-12 00:35:36 +02:00
|
|
|
-- check for day changes and call handlers
|
2020-04-24 01:38:13 +02:00
|
|
|
local timer = math.huge
|
2020-04-11 04:00:56 +02:00
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
timer = timer + dtime
|
|
|
|
if timer < GSCYCLE then return end
|
|
|
|
handle_time_progression()
|
|
|
|
timer = 0
|
|
|
|
end)
|
|
|
|
|
2020-05-13 16:07:00 +02:00
|
|
|
-- make moon phase available to weather effects
|
|
|
|
if mod_climate_api then
|
|
|
|
climate_api.register_global_influence("moonphase", moon_phases.get_phase)
|
|
|
|
end
|
|
|
|
|
2020-04-12 00:35:36 +02:00
|
|
|
-- include API for chat commands
|
2020-05-13 16:07:00 +02:00
|
|
|
dofile(modpath .. "/commands.lua")
|