Remove datastorage dependency, update mod.conf, adjust moon size

This commit is contained in:
Till Affeldt 2020-04-12 00:35:36 +02:00
parent e07af4cdd2
commit b81669c9da
6 changed files with 52 additions and 71 deletions

View File

@ -15,7 +15,7 @@ minetest.register_chatcommand("moonphase", {
minetest.register_chatcommand("set_moonphase", {
params = "<phase>",
description = "Set moon phase to given value",
privs = {moonphase = true},
privs = { moonphase = true },
func = function(playername, param)
if param == nil or param == "" then
minetest.chat_send_player(playername, "Provide a number between 1 and 8")

View File

@ -1,48 +0,0 @@
local mod_datastorage = minetest.get_modpath("datastorage") ~= nil
local default_state = {
change_time = true,
day = 1,
phase = 1
}
local function use_datastorage()
local state = datastorage.get("moon_phases", "moon_state")
for key, val in pairs(default_state) do
if type(state[key]) == "nil" then
state[key] = val
end
end
return state
end
local storage
local function use_filesystem()
local file_name = minetest.get_worldpath() .. "/moon_phases"
minetest.register_on_shutdown(function()
local file = io.open(file_name, "w")
file:write(minetest.serialize(storage))
file:close()
end)
local file = io.open(file_name, "r")
if file ~= nil then
storage = minetest.deserialize(file:read("*a"))
file:close()
if type(storage) == "table" then
return storage
end
end
storage = default_state
return storage
end
local function get_storage()
if mod_datastorage then
return use_datastorage()
else
return use_filesystem()
end
end
return get_storage

View File

@ -1,2 +0,0 @@
skylayer?
datastorage?

View File

@ -1,3 +0,0 @@
Changes the moon to follow a cycle between eight different phases.
Expect the sky to change every four nights (or configure a custom schedule).
Requires at least Minetest 5.2.0

View File

@ -1,13 +1,23 @@
local mod_skylayer = minetest.get_modpath("skylayer") ~= nil
local modpath = minetest.get_modpath("moon_phases");
local state = dofile(modpath .. "/datastorage.lua")()
local GSCYCLE = 0.5
local GSCYCLE = 0.5 -- global step cycle
local DEFAULT_LENGTH = 4 -- default cycle length
local DEFAULT_STYLE = "classic" -- default texture style
moon_phases = {}
local state = minetest.get_mod_storage()
if not state:contains("day") then
state:from_table({
day = 1,
phase = 1,
change_time = 1
})
end
-- retrieve and parse mod configuration
local function get_cycle_config()
local DEFAULT_LENGTH = 4
local config = minetest.settings:get("moon_phases_cycle") or DEFAULT_LENGTH
config = math.floor(tonumber(config))
if (not config) or config < 0 then
@ -19,12 +29,14 @@ end
local PHASE_LENGTH = get_cycle_config()
-- set the moon texture of a player to the given texture
local function set_texture(player, texture)
local sl = {}
sl.name = "moon_phases:custom"
sl.moon_data = {
visible = true,
texture = texture
texture = texture,
scale = 0.8
}
if mod_skylayer then
skylayer.add_layer(player:get_player_name(), sl)
@ -33,44 +45,59 @@ local function set_texture(player, texture)
end
end
-- update moon textures of all online players
local function update_textures()
local phase = state:get_int("phase")
for _, player in ipairs(minetest.get_connected_players()) do
set_texture(player, "moon_" .. state.phase .. ".png")
set_texture(player, "moon_" .. phase .. ".png")
end
end
-- check for day changes
local function handle_time_progression()
local time = minetest.get_timeofday()
if time >= 0.5 and state.change_time then
state.day = state.day + 1
if state.day % PHASE_LENGTH == 0 then
state.phase = (state.phase % 8) + 1
state.change_time = false
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)
update_textures()
end
elseif time < 0.5 and not state.change_time then
state.change_time = true
elseif time < 0.5 and not change_time then
state:set_int("change_time", 1)
end
end
-- return the current moon phase
function moon_phases.get_phase()
return state.phase
return state:get_int("phase")
end
-- set the current moon phase
-- @param phase int Phase between 1 and 8
function moon_phases.set_phase(phase)
phase = math.floor(tonumber(phase))
if (not phase) or phase < 0 or phase > 8 then
if (not phase) or phase < 1 or phase > 8 then
return false
end
state.phase = phase
state:set_int("phase", phase)
update_textures()
return true
end
-- set the moon texture of newly joined player
minetest.register_on_joinplayer(function(player)
set_texture(player, "moon_" .. state.phase .. ".png")
local phase = state:get_int("phase")
-- phase might not have been set at server start
if phase < 1 then phase = 1 end
set_texture(player, "moon_" .. phase .. ".png")
end)
-- check for day changes and call handlers
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
@ -79,4 +106,5 @@ minetest.register_globalstep(function(dtime)
timer = 0
end)
-- include API for chat commands
dofile(modpath .. "/commands.lua")

View File

@ -1,4 +1,10 @@
name = moon_phases
title = Moon Phases
author = TestificateMods
release = 1
release = 10100
description = """
Changes the moon to follow a cycle between eight different phases.
Expect the sky to change every four nights (or configure a custom schedule).
Requires at least Minetest 5.2.0
"""
optional_depends = skylayer