minetest_moon_phase/init.lua

116 lines
3.1 KiB
Lua
Raw Normal View History

2020-04-11 04:00:56 +02:00
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 DEFAULT_STYLE = "realistic" -- default texture style
2020-04-11 04:00:56 +02:00
moon_phases = {}
local state = minetest.get_mod_storage()
if not state:contains("day") then
2020-04-14 05:49:14 +02:00
state:from_table({fields = {
day = 1,
phase = 1,
change_time = 1
2020-04-14 05:49:14 +02:00
}})
end
2020-04-11 04:00:56 +02:00
-- 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
2020-04-11 04:00:56 +02:00
-- set the moon texture of a player to the given phase
local function set_texture(player, phase)
2020-04-11 04:00:56 +02:00
local sl = {}
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-11 04:00:56 +02:00
sl.name = "moon_phases:custom"
sl.moon_data = {
visible = true,
texture = texture,
scale = 0.8
2020-04-11 04:00:56 +02:00
}
if mod_skylayer then
skylayer.add_layer(player:get_player_name(), sl)
else
player:set_moon(sl.moon_data)
end
end
-- update moon textures of all online players
2020-04-11 04:00:56 +02:00
local function update_textures()
local phase = state:get_int("phase")
2020-04-11 04:00:56 +02:00
for _, player in ipairs(minetest.get_connected_players()) do
set_texture(player, phase)
2020-04-11 04:00:56 +02:00
end
end
-- check for day changes
2020-04-11 04:00:56 +02:00
local function handle_time_progression()
local time = minetest.get_timeofday()
local day = state:get_int("day")
local phase = state:get_int("phase")
local change_time = state:get_int("change_time") == 1
if time >= 0.5 and change_time then
day = day + 1
state:set_int("day", day)
if day % PHASE_LENGTH == 0 then
state:set_int("phase", (phase % 8) + 1)
state:set_int("change_time", 0)
2020-04-11 04:00:56 +02:00
update_textures()
end
elseif time < 0.5 and not change_time then
state:set_int("change_time", 1)
2020-04-11 04:00:56 +02:00
end
end
-- return the current moon phase
2020-04-11 04:00:56 +02:00
function moon_phases.get_phase()
return state:get_int("phase")
2020-04-11 04:00:56 +02:00
end
-- set the current moon phase
-- @param phase int Phase between 1 and 8
2020-04-11 04:00:56 +02:00
function moon_phases.set_phase(phase)
phase = math.floor(tonumber(phase))
if (not phase) or phase < 1 or phase > 8 then
2020-04-11 04:00:56 +02:00
return false
end
state:set_int("phase", phase)
2020-04-11 04:00:56 +02:00
update_textures()
return true
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()
meta_data:set_string("moon_phases:texture_style", style)
set_texture(player, state:get_int("phase"))
return true
end
-- set the moon texture of newly joined player
2020-04-11 04:00:56 +02:00
minetest.register_on_joinplayer(function(player)
local phase = state:get_int("phase")
set_texture(player, phase)
2020-04-11 04:00:56 +02:00
end)
-- check for day changes and call handlers
2020-04-11 04:00:56 +02:00
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer < GSCYCLE then return end
handle_time_progression()
timer = 0
end)
-- include API for chat commands
2020-04-11 04:00:56 +02:00
dofile(modpath .. "/commands.lua")