realtest/mods/seasons/init.lua

101 lines
2.3 KiB
Lua
Raw Normal View History

2012-11-17 20:43:41 +06:00
local DAY_LENGTH_TIME = 1200
2012-11-17 19:26:16 +06:00
realtest.get_day = function()
2012-11-17 20:43:41 +06:00
local f = io.open(minetest.get_worldpath()..'/season', "r")
if f then
local n = f:read("*n")
2012-11-17 19:26:16 +06:00
io.close(f)
end
2012-11-17 20:43:41 +06:00
return n or 1
2012-11-17 19:26:16 +06:00
end
2012-11-17 20:43:41 +06:00
realtest.set_day = function(n)
local f = io.open(minetest.get_worldpath()..'/season', "w")
f:write((n % 360).."\n")
f:write(math.floor(n / 360))
io.close(f)
end
realtest.get_day_and_year = function()
local f = io.open(minetest.get_worldpath()..'/season', "r")
local n = 1
local y = 0
if f then
n = f:read("*n")
y = f:read("*n")
io.close(f)
end
return y, n
end
realtest.get_year = function()
local f = io.open(minetest.get_worldpath()..'/season', "r")
if f then
f:read("*n")
local y = f:read("*n")
io.close(f)
end
return y or 0
end
realtest.set_year = function(y)
local n = realtest.get_day()
local f = io.open(minetest.get_worldpath()..'/season', "w")
f:write(n.."\n")
f:write(y)
2012-11-17 19:26:16 +06:00
io.close(f)
end
realtest.get_season = function()
local day = realtest.get_day()
2012-11-17 20:43:41 +06:00
if day > 0 and day < 91 then return "spring" end
if day > 90 and day < 181 then return "summer" end
if day > 180 and day < 241 then return "autumn" end
if day > 240 and day < 361 then return "winter" end
2012-11-17 19:26:16 +06:00
end
2012-11-17 20:43:41 +06:00
local add_day = function()
if realtest.get_day() + 1 >= 360 then
realtest.set_year(realtest.get_year() + 1)
realtest.set_day(1)
else
realtest.set_day(realtest.get_day() + 1)
end
minetest.after(DAY_LENGTH_TIME, add_day)
2012-11-17 19:26:16 +06:00
end
2012-11-17 20:43:41 +06:00
if minetest.setting_get("time_speed") ~= 0 then
minetest.after(DAY_LENGTH_TIME, add_day)
end
2012-11-17 19:26:16 +06:00
minetest.register_chatcommand("calendar", {
2012-11-17 20:43:41 +06:00
params = "",
2012-11-17 19:26:16 +06:00
description = "get the calendar",
privs = {server=true},
func = function(name, param)
minetest.chat_send_player(name, "Day: "..realtest.get_day())
minetest.chat_send_player(name, "Season: "..realtest.get_season())
2012-11-17 20:43:41 +06:00
minetest.chat_send_player(name, "Year: "..realtest.get_year())
2012-11-17 19:26:16 +06:00
end,
})
minetest.register_chatcommand("setday", {
params = "<day>",
description = "set the day",
privs = {server=true},
func = function(name, param)
realtest.set_day(param)
2012-11-17 20:43:41 +06:00
minetest.chat_send_player(name, "Day was changed to "..param)
end,
})
minetest.register_chatcommand("setyear", {
params = "<year>",
description = "set the year",
privs = {server=true},
func = function(name, param)
realtest.set_year(param)
minetest.chat_send_player(name, "Year was changed to "..param)
2012-11-17 19:26:16 +06:00
end,
2012-11-17 20:43:41 +06:00
})