2013-08-06 08:25:01 +02:00
|
|
|
--Global config and function table.
|
|
|
|
snow = {
|
2015-06-03 22:09:32 +02:00
|
|
|
snowball_gravity = 100/109,
|
|
|
|
snowball_velocity = 19,
|
2013-12-09 07:19:31 +01:00
|
|
|
sleds = true,
|
2013-08-06 08:25:01 +02:00
|
|
|
enable_snowfall = true,
|
2013-12-09 04:56:35 +01:00
|
|
|
lighter_snowfall = false,
|
2013-08-06 08:25:01 +02:00
|
|
|
debug = false,
|
|
|
|
smooth_biomes = true,
|
|
|
|
christmas_content = true,
|
|
|
|
smooth_snow = true,
|
|
|
|
min_height = 3,
|
2015-11-21 23:37:28 +01:00
|
|
|
mapgen_rarity = 18,
|
|
|
|
mapgen_size = 210,
|
2013-08-06 08:25:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
--Config documentation.
|
|
|
|
local doc = {
|
2015-06-03 22:09:32 +02:00
|
|
|
snowball_gravity = "The gravity of thrown snowballs",
|
|
|
|
snowball_velocity = "How fast players throw snowballs",
|
2013-12-09 07:19:31 +01:00
|
|
|
sleds = "Disable this to prevent sleds from being riden.",
|
2013-08-06 08:25:01 +02:00
|
|
|
enable_snowfall = "Enables falling snow.",
|
2013-12-09 04:56:35 +01:00
|
|
|
lighter_snowfall = "Reduces the amount of resources and fps used by snowfall.",
|
2015-05-29 10:27:59 +02:00
|
|
|
debug = "Enables debug output. Currently it only prints mgv6 info.",
|
|
|
|
smooth_biomes = "Enables smooth transition of biomes (mgv6)",
|
2013-08-06 08:25:01 +02:00
|
|
|
smooth_snow = "Disable this to stop snow from being smoothed.",
|
2015-05-29 10:27:59 +02:00
|
|
|
christmas_content = "Disable this to remove christmas saplings from being found.",
|
|
|
|
min_height = "The minumum height a snow biome will generate (mgv7)",
|
2015-11-21 23:37:28 +01:00
|
|
|
mapgen_rarity = "mapgen rarity in %",
|
|
|
|
mapgen_size = "size of the generated… (has an effect to the rarity, too)",
|
2013-08-06 08:25:01 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 23:37:28 +01:00
|
|
|
local function value_from_string(v)
|
|
|
|
if v == "true" then
|
|
|
|
v = true
|
|
|
|
elseif v == "false" then
|
|
|
|
v = false
|
|
|
|
else
|
|
|
|
local a_number = tonumber(v)
|
|
|
|
if a_number then
|
|
|
|
v = a_number
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
|
|
|
|
local allowed_types = {string = true, number = true, boolean = true}
|
|
|
|
|
2013-08-06 08:25:01 +02:00
|
|
|
--Manage config.
|
|
|
|
--Saves contents of config to file.
|
|
|
|
local function saveConfig(path, config, doc)
|
|
|
|
local file = io.open(path,"w")
|
2015-11-21 23:37:28 +01:00
|
|
|
if not file then
|
|
|
|
minetest.log("error", "[snow] could not open config file for writing at "..path)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for i,v in pairs(config) do
|
|
|
|
if allowed_types[type(v)] then
|
|
|
|
if doc and doc[i] then
|
|
|
|
file:write("# "..doc[i].."\n")
|
2013-08-06 08:25:01 +02:00
|
|
|
end
|
2015-11-21 23:37:28 +01:00
|
|
|
file:write(i.." = "..tostring(v).."\n")
|
2013-08-06 08:25:01 +02:00
|
|
|
end
|
|
|
|
end
|
2015-11-21 23:37:28 +01:00
|
|
|
file:close()
|
2013-08-06 08:25:01 +02:00
|
|
|
end
|
|
|
|
--Loads config and returns config values inside table.
|
|
|
|
local function loadConfig(path)
|
|
|
|
local file = io.open(path,"r")
|
2015-11-21 23:37:28 +01:00
|
|
|
if not file then
|
|
|
|
--Create config file.
|
|
|
|
return
|
|
|
|
end
|
|
|
|
io.close(file)
|
|
|
|
local config = {}
|
|
|
|
for line in io.lines(path) do
|
|
|
|
if line:sub(1,1) ~= "#" then
|
|
|
|
local i, v = line:match("^(%S*) = (%S*)")
|
|
|
|
if i and v then
|
|
|
|
config[i] = value_from_string(v)
|
2013-08-06 08:25:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-11-21 23:37:28 +01:00
|
|
|
return config
|
2013-08-06 08:25:01 +02:00
|
|
|
end
|
|
|
|
|
2015-11-21 23:37:28 +01:00
|
|
|
local modpath = minetest.get_modpath("snow")
|
|
|
|
|
2015-05-29 10:27:59 +02:00
|
|
|
minetest.register_on_shutdown(function()
|
2015-11-21 23:37:28 +01:00
|
|
|
saveConfig(modpath.."/config.txt", snow, doc)
|
2015-05-29 10:27:59 +02:00
|
|
|
end)
|
2013-08-06 08:25:01 +02:00
|
|
|
|
2015-11-21 23:37:28 +01:00
|
|
|
local config = loadConfig(modpath.."/config.txt")
|
2013-08-06 08:25:01 +02:00
|
|
|
if config then
|
|
|
|
for i,v in pairs(config) do
|
2013-12-12 05:14:00 +01:00
|
|
|
if type(snow[i]) == type(v) then
|
|
|
|
snow[i] = v
|
2015-11-21 23:37:28 +01:00
|
|
|
else
|
|
|
|
minetest.log("error", "[snow] wrong type of setting "..i)
|
2013-12-12 05:14:00 +01:00
|
|
|
end
|
2013-08-06 08:25:01 +02:00
|
|
|
end
|
|
|
|
else
|
2015-11-21 23:37:28 +01:00
|
|
|
saveConfig(modpath.."/config.txt", snow, doc)
|
2013-08-06 08:25:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
for i,v in pairs(snow) do
|
2015-11-21 23:37:28 +01:00
|
|
|
if allowed_types[type(v)] then
|
2013-08-06 08:25:01 +02:00
|
|
|
local v = minetest.setting_get("snow_"..i)
|
|
|
|
if v ~= nil then
|
2015-11-21 23:37:28 +01:00
|
|
|
snow[i] = value_from_string(v)
|
2013-08-06 08:25:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-12 05:14:00 +01:00
|
|
|
|
|
|
|
--MENU
|
|
|
|
|
2015-11-21 23:37:28 +01:00
|
|
|
local function get_formspec()
|
2013-12-12 05:14:00 +01:00
|
|
|
local p = -0.5
|
|
|
|
local formspec = "label[0,-0.3;Settings:]"
|
|
|
|
for i,v in pairs(snow) do
|
|
|
|
local t = type(v)
|
2015-05-23 19:53:30 +02:00
|
|
|
if t == "string"
|
|
|
|
or t == "number" then
|
2013-12-12 05:14:00 +01:00
|
|
|
p = p + 1.5
|
|
|
|
formspec = formspec.."field[0.3,"..p..";2,1;snow:"..i..";"..i..";"..v.."]"
|
|
|
|
elseif t == "boolean" then
|
|
|
|
p = p + 0.5
|
|
|
|
formspec = formspec.."checkbox[0,"..p..";snow:"..i..";"..i..";"..tostring(v).."]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
p = p + 1
|
|
|
|
formspec = "size[4,"..p..";]\n"..formspec
|
|
|
|
return formspec
|
|
|
|
end
|
|
|
|
|
2015-11-21 23:37:28 +01:00
|
|
|
minetest.register_chatcommand("snow", {
|
|
|
|
description = "Show a menu for various actions",
|
|
|
|
privs = {server=true},
|
|
|
|
func = function(name)
|
|
|
|
minetest.chat_send_player(name, "Showing snow menu…")
|
|
|
|
minetest.show_formspec(name, "snow:menu", get_formspec())
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2013-12-12 05:14:00 +01:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
2015-05-23 19:53:30 +02:00
|
|
|
if formname ~= "snow:menu" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for i,v in pairs(snow) do
|
|
|
|
local t = type(v)
|
2015-11-21 23:37:28 +01:00
|
|
|
if allowed_types[t] then
|
2015-05-23 19:53:30 +02:00
|
|
|
local field = fields["snow:"..i]
|
|
|
|
if field then
|
|
|
|
if t == "string" then
|
|
|
|
snow[i] = field
|
2015-11-21 23:37:28 +01:00
|
|
|
elseif t == "number" then
|
|
|
|
local valid_number = tonumber(field)
|
|
|
|
if valid_number then
|
|
|
|
snow[i] = valid_number
|
|
|
|
end
|
|
|
|
elseif t == "boolean" then
|
2015-05-23 19:53:30 +02:00
|
|
|
if field == "true" then
|
|
|
|
snow[i] = true
|
|
|
|
elseif field == "false" then
|
|
|
|
snow[i] = false
|
2013-12-12 05:14:00 +01:00
|
|
|
end
|
2015-05-23 19:53:30 +02:00
|
|
|
end
|
2013-12-12 05:14:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|