mirror of
https://github.com/t-affeldt/minetest_moon_phase.git
synced 2025-07-18 08:20:28 +02:00
Compare commits
4 Commits
v2.0.0
...
79992a0445
Author | SHA1 | Date | |
---|---|---|---|
79992a0445 | |||
f9182cae3f | |||
3e6abe5e42 | |||
b0aed595e5 |
@ -1,106 +0,0 @@
|
||||
local name = "moon_phases:phase"
|
||||
local mod_climate_api = minetest.get_modpath("climate_api") ~= nil
|
||||
|
||||
local GSCYCLE = 0.5 -- global step cycle
|
||||
local DEFAULT_LENGTH = 4 -- default cycle length
|
||||
local DEFAULT_STYLE = "realistic" -- default texture style
|
||||
local PHASE_COUNT = 8 -- number of phases to go through
|
||||
|
||||
-- retrieve mod configuration
|
||||
local PHASE_LENGTH = minetest.settings:get("moon_phases_cycle") or DEFAULT_LENGTH
|
||||
local TEXTURE_STYLE = minetest.settings:get("moon_phases_style") or DEFAULT_STYLE
|
||||
|
||||
local moon_phases = {}
|
||||
local state = minetest.get_mod_storage()
|
||||
local phase = 1
|
||||
|
||||
-- return the current moon phase
|
||||
function moon_phases.get_phase()
|
||||
return phase
|
||||
end
|
||||
|
||||
-- set the current moon phase
|
||||
-- @param phase int Phase between 1 and PHASE_COUNT
|
||||
function moon_phases.set_phase(nphase)
|
||||
phase = math.floor(tonumber(nphase))
|
||||
if (not nphase) or nphase < 1 or nphase > PHASE_COUNT then
|
||||
return false
|
||||
end
|
||||
local day = params.day_count
|
||||
local date_offset = state:get_int("date_offset")
|
||||
local progress = (day + date_offset - 1) % PHASE_LENGTH + 1
|
||||
local phase_offset = (nphase - phase + PHASE_COUNT) % PHASE_COUNT
|
||||
local add_offset = (phase_offset * PHASE_LENGTH) - progress
|
||||
if add_offset == 0 then add_offset = nil end
|
||||
phase = nphase
|
||||
state:set_int("date_offset", add_offset)
|
||||
end
|
||||
|
||||
-- set the moon's texture style for the given player
|
||||
function moon_phases.set_style(player, style)
|
||||
if style ~= "classic" and style ~= "realistic" then
|
||||
return false
|
||||
end
|
||||
local meta_data = player:get_meta()
|
||||
if style == DEFAULT_STYLE then style = nil end
|
||||
meta_data:set_string("moon_phases:texture_style", style)
|
||||
return true
|
||||
end
|
||||
|
||||
-- calculate the current sky layout for a given player
|
||||
local function generate_effects(params)
|
||||
local override = {}
|
||||
|
||||
local time = params.time
|
||||
local day = params.day_count
|
||||
local date_offset = state:get_int("date_offset")
|
||||
day = day + date_offset
|
||||
if time > 0.5 then
|
||||
day = day + 1
|
||||
end
|
||||
|
||||
local meta_data = params.player:get_meta()
|
||||
local style = meta_data:get_string("moon_phases:texture_style")
|
||||
if style ~= "classic" and style ~= "realistic" then
|
||||
style = TEXTURE_STYLE
|
||||
end
|
||||
|
||||
phase = ((math.ceil(day / PHASE_LENGTH) - 1) % PHASE_COUNT) + 1
|
||||
override["climate_api:skybox"] = {
|
||||
moon_data = {
|
||||
texture = "moon_" .. phase .. "_" .. style .. ".png",
|
||||
scale = 0.8
|
||||
}
|
||||
}
|
||||
return override
|
||||
end
|
||||
|
||||
local function update_sky(player)
|
||||
local params = {}
|
||||
params.time = minetest.get_timeofday()
|
||||
params.day_count = minetest.get_day_count()
|
||||
params.player = player
|
||||
local sky = generate_effects(params)
|
||||
player:set_moon(sky.moon_data)
|
||||
end
|
||||
|
||||
local timer = 0
|
||||
local function handle_time_progression(dtime)
|
||||
timer = timer + dtime
|
||||
if timer < GSCYCLE then return end
|
||||
for _, player in ipairs(minetest.get_connected_players()) do
|
||||
update_sky(player)
|
||||
end
|
||||
timer = 0
|
||||
end
|
||||
|
||||
if mod_climate_api then
|
||||
-- register moon cycles as weather preset
|
||||
climate_api.register_weather(name, {}, generate_effects)
|
||||
else
|
||||
-- set the moon texture of newly joined player
|
||||
minetest.register_on_joinplayer(update_sky)
|
||||
|
||||
-- check for changes and update player skies
|
||||
minetest.register_globalstep(handle_time_progression)
|
||||
end
|
19
init.lua
19
init.lua
@ -3,13 +3,13 @@ local mod_skylayer = minetest.get_modpath("skylayer") ~= nil
|
||||
|
||||
local modpath = minetest.get_modpath("moon_phases");
|
||||
|
||||
local GSCYCLE = 0.5 -- global step cycle
|
||||
local DEFAULT_LENGTH = 4 -- default cycle length
|
||||
local GSCYCLE = 0.5 -- global step cycle in seconds
|
||||
local DEFAULT_LENGTH = 4 -- default moon cycle length in days
|
||||
local DEFAULT_STYLE = "classic" -- default texture style
|
||||
local PHASE_COUNT = 8 -- number of phases to go through
|
||||
|
||||
-- retrieve mod configuration
|
||||
local PHASE_LENGTH = 4--minetest.settings:get("moon_phases_cycle") or DEFAULT_LENGTH
|
||||
local PHASE_LENGTH = tonumber(minetest.settings:get("moon_phases_cycle") or DEFAULT_LENGTH)
|
||||
local TEXTURE_STYLE = minetest.settings:get("moon_phases_style") or DEFAULT_STYLE
|
||||
|
||||
local sky_color = {
|
||||
@ -35,6 +35,7 @@ local horizon_color = {
|
||||
}
|
||||
|
||||
moon_phases = {}
|
||||
local phase = 1
|
||||
local state = minetest.get_mod_storage()
|
||||
|
||||
-- calculate current moon phase from date
|
||||
@ -48,8 +49,6 @@ local function calculate_phase()
|
||||
return ((math.ceil(day / PHASE_LENGTH) - 1) % PHASE_COUNT) + 1
|
||||
end
|
||||
|
||||
local phase = 1
|
||||
|
||||
-- return the current moon phase
|
||||
function moon_phases.get_phase()
|
||||
return phase
|
||||
@ -81,7 +80,7 @@ local function set_texture(player, phase)
|
||||
local playername = player:get_player_name()
|
||||
if mod_climate_api then
|
||||
sky.priority = 0
|
||||
climate_api.skybox.add_layer(playername, name, sky)
|
||||
climate_api.skybox.add(playername, name, sky)
|
||||
elseif mod_skylayer then
|
||||
sky.name = name
|
||||
skylayer.add_layer(playername, sky)
|
||||
@ -96,8 +95,6 @@ local function handle_time_progression()
|
||||
local n_phase = calculate_phase()
|
||||
if n_phase ~= phase then
|
||||
phase = n_phase
|
||||
minetest.log(dump2(phase, "htp: phase"))
|
||||
minetest.log(dump2(n_phase, "htp: n_phase"))
|
||||
for _, player in ipairs(minetest.get_connected_players()) do
|
||||
set_texture(player, phase)
|
||||
end
|
||||
@ -126,7 +123,6 @@ function moon_phases.set_style(player, style)
|
||||
if style ~= nil and style ~= "classic" and style ~= "realistic" then
|
||||
return false
|
||||
end
|
||||
--if style == DEFAULT_STYLE then style = nil end
|
||||
local meta_data = player:get_meta()
|
||||
meta_data:set_string("moon_phases:texture_style", style)
|
||||
set_texture(player, state:get_int("phase"))
|
||||
@ -147,5 +143,10 @@ minetest.register_globalstep(function(dtime)
|
||||
timer = 0
|
||||
end)
|
||||
|
||||
-- make moon phase available to weather effects
|
||||
if mod_climate_api then
|
||||
climate_api.register_global_influence("moonphase", moon_phases.get_phase)
|
||||
end
|
||||
|
||||
-- include API for chat commands
|
||||
dofile(modpath .. "/commands.lua")
|
Reference in New Issue
Block a user