use minetest. Get modpath less times, move stairsplus testing out of that file, update util.lua and make mapgen rarity and size better configurable

This commit is contained in:
HybridDog 2015-11-21 23:37:28 +01:00
parent 097f5f5419
commit 4a30e67f46
4 changed files with 93 additions and 81 deletions

View File

@ -39,41 +39,43 @@ http://github.com/Splizard/minetest-mod-snow/
-- Original Lua Files -- Original Lua Files
--dofile(minetest.get_modpath("snow").."/util.lua") --dofile(modpath.."/util.lua")
--dofile(minetest.get_modpath("snow").."/mapgen.lua") --dofile(modpath.."/mapgen.lua")
--dofile(minetest.get_modpath("snow").."/sled.lua") --dofile(modpath.."/sled.lua")
-- "falling_snow.lua" disabled since weather functions minetest.get_heat(pos) and minetest.get_humidity(pos) -- "falling_snow.lua" disabled since weather functions minetest.get_heat(pos) and minetest.get_humidity(pos)
-- have been removed from Minetest. -- have been removed from Minetest.
-- Until something else can be figured out, use paramat's "Snowdrift" mod instead. -- Until something else can be figured out, use paramat's "Snowdrift" mod instead.
-- dofile(minetest.get_modpath("snow").."/falling_snow.lua") -- dofile(modpath.."/falling_snow.lua")
-- Original init.lua File Broken into Smaller Files -- Original init.lua File Broken into Smaller Files
dofile(minetest.get_modpath("snow").."/src/abms.lua") local modpath = minetest.get_modpath("snow")
dofile(minetest.get_modpath("snow").."/src/aliases.lua") dofile(modpath.."/src/abms.lua")
dofile(minetest.get_modpath("snow").."/src/crafting.lua") dofile(modpath.."/src/aliases.lua")
dofile(minetest.get_modpath("snow").."/src/snowball.lua") dofile(modpath.."/src/crafting.lua")
dofile(modpath.."/src/snowball.lua")
-- The formspec menu didn't work when util.lua was the very first "dofile" so I moved -- The formspec menu didn't work when util.lua was the very first "dofile" so I moved
-- it and all the other original "dofiles", in order, to the bottom of the list. ~ LazyJ -- it and all the other original "dofiles", in order, to the bottom of the list. ~ LazyJ
-- Minetest would crash if the mapgen was called upon before the rest of other snow lua files so -- Minetest would crash if the mapgen was called upon before the rest of other snow lua files so
-- I put it lower on the list and that seems to do the trick. ~ LazyJ -- I put it lower on the list and that seems to do the trick. ~ LazyJ
dofile(minetest.get_modpath("snow").."/src/util.lua") dofile(modpath.."/src/util.lua")
-- To get Xmas tree saplings, the "christmas_content", true or false, in "util.lua" has to be determined first. -- To get Xmas tree saplings, the "christmas_content", true or false, in "util.lua" has to be determined first.
-- That means "nodes.lua", where the saplings are controlled, has to come after "util.lua". ~ LazyJ -- That means "nodes.lua", where the saplings are controlled, has to come after "util.lua". ~ LazyJ
dofile(minetest.get_modpath("snow").."/src/nodes.lua") dofile(modpath.."/src/nodes.lua")
dofile(minetest.get_modpath("snow").."/src/basic_stairs_slabs.lua") dofile(modpath.."/src/basic_stairs_slabs.lua")
dofile(minetest.get_modpath("snow").."/src/mapgen.lua") dofile(modpath.."/src/mapgen.lua")
dofile(minetest.get_modpath("snow").."/src/sled.lua") dofile(modpath.."/src/sled.lua")
dofile(minetest.get_modpath("snow").."/src/falling_snow.lua") dofile(modpath.."/src/falling_snow.lua")
-- Check for "MoreBlocks". If not found, skip this next "dofile". -- Check for "MoreBlocks". If not found, skip this next "dofile".
if minetest.get_modpath("moreblocks") then if rawget(_G, "stairsplus")
and minetest.get_modpath("moreblocks") then
dofile(minetest.get_modpath("snow").."/src/stairsplus.lua") dofile(modpath.."/src/stairsplus.lua")
end end
@ -138,9 +140,7 @@ local function is_uneven(pos)
local num = minetest.get_node_level(pos) local num = minetest.get_node_level(pos)
local get_node = minetest.get_node local get_node = minetest.get_node
local add_node = minetest.add_node local add_node = minetest.add_node
local found
local foundx local foundx
local foundy
local foundz local foundz
for z = -1,1 do for z = -1,1 do
for x = -1,1 do for x = -1,1 do
@ -161,7 +161,6 @@ local function is_uneven(pos)
if not (x == 0 and z == 0) if not (x == 0 and z == 0)
and node.name == "default:snow" and node.name == "default:snow"
and minetest.get_node_level(p) < num then and minetest.get_node_level(p) < num then
found = true
foundx = x foundx = x
foundz = z foundz = z
elseif node.name == "air" elseif node.name == "air"
@ -173,9 +172,9 @@ local function is_uneven(pos)
end end
end end
end end
if found then if foundx then
local p = {x=pos.x+foundx, y=pos.y, z=pos.z+foundz} local p = {x=pos.x+foundx, y=pos.y, z=pos.z+foundz}
if is_uneven(p) ~= true then if not is_uneven(p) then
minetest.add_node_level(p, 7) minetest.add_node_level(p, 7)
end end
return true return true

View File

@ -21,8 +21,8 @@ local function upper_rarity(rarity)
return math.sign(rarity)*math.sin(math.abs(rarity)*math.pi/2) return math.sign(rarity)*math.sin(math.abs(rarity)*math.pi/2)
end end
local rarity = 18 --snow.mapgen_rarity local rarity = snow.mapgen_rarity
local size = 210 --snow.mapgen_size local size = snow.mapgen_size
local nosmooth_rarity = 1-rarity/50 local nosmooth_rarity = 1-rarity/50
local perlin_scale = size*100/rarity local perlin_scale = size*100/rarity

View File

@ -50,12 +50,9 @@ There is one in each of the "stairsplus.register_all" sections.
-- First, let's run a check to see if MoreBlocks is installed; we're going to need it for the -- First, let's run a check to see if MoreBlocks is installed; we're going to need it for the
-- next section of stairsplus stuff. ~LazyJ -- next section of stairsplus stuff. ~LazyJ
if (minetest.get_modpath("moreblocks"))
and rawget(_G, "stairsplus")
-- 'If' MoreBlocks was found and stairsplus is available, well, 'then' go ahead with this next part: -- 'If' MoreBlocks was found and stairsplus is available, well, 'then' go ahead with this next part:
then
--[[ Leave commented out - For reference only. ~ LazyJ --[[ Leave commented out - For reference only. ~ LazyJ
function stairsplus.register_all(modname, subname, recipeitem, fields) function stairsplus.register_all(modname, subname, recipeitem, fields)
@ -336,7 +333,7 @@ for _, name in pairs(snow_nodes) do
}) })
end end
else -- from clear up at the top, the MoreBlocks check. "Else", if MoreBlocks wasn't found, skip -- from clear up at the top, the MoreBlocks check. "Else", if MoreBlocks wasn't found, skip
-- down to here, "return" nothing and "end" this script. ~ LazyJ -- down to here, "return" nothing and "end" this script. ~ LazyJ
return
end

View File

@ -10,6 +10,8 @@ snow = {
christmas_content = true, christmas_content = true,
smooth_snow = true, smooth_snow = true,
min_height = 3, min_height = 3,
mapgen_rarity = 18,
mapgen_size = 210,
} }
--Config documentation. --Config documentation.
@ -24,74 +26,88 @@ local doc = {
smooth_snow = "Disable this to stop snow from being smoothed.", smooth_snow = "Disable this to stop snow from being smoothed.",
christmas_content = "Disable this to remove christmas saplings from being found.", christmas_content = "Disable this to remove christmas saplings from being found.",
min_height = "The minumum height a snow biome will generate (mgv7)", min_height = "The minumum height a snow biome will generate (mgv7)",
mapgen_rarity = "mapgen rarity in %",
mapgen_size = "size of the generated… (has an effect to the rarity, too)",
} }
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}
--Manage config. --Manage config.
--Saves contents of config to file. --Saves contents of config to file.
local function saveConfig(path, config, doc) local function saveConfig(path, config, doc)
local file = io.open(path,"w") local file = io.open(path,"w")
if file then if not file then
for i,v in pairs(config) do minetest.log("error", "[snow] could not open config file for writing at "..path)
local t = type(v) return
if t == "string" or t == "number" or t == "boolean" then end
if doc and doc[i] then for i,v in pairs(config) do
file:write("# "..doc[i].."\n") if allowed_types[type(v)] then
end if doc and doc[i] then
file:write(i.." = "..tostring(v).."\n") file:write("# "..doc[i].."\n")
end end
file:write(i.." = "..tostring(v).."\n")
end end
end end
file:close()
end end
--Loads config and returns config values inside table. --Loads config and returns config values inside table.
local function loadConfig(path) local function loadConfig(path)
local config = {}
local file = io.open(path,"r") local file = io.open(path,"r")
if file then if not file then
io.close(file) --Create config file.
for line in io.lines(path) do return
if line:sub(1,1) ~= "#" then end
local i, v = line:match("^(%S*) = (%S*)") io.close(file)
if i and v then local config = {}
if v == "true" then v = true end for line in io.lines(path) do
if v == "false" then v = false end if line:sub(1,1) ~= "#" then
if tonumber(v) then v = tonumber(v) end local i, v = line:match("^(%S*) = (%S*)")
config[i] = v if i and v then
end config[i] = value_from_string(v)
end end
end end
return config
else
--Create config file.
return nil
end end
return config
end end
local modpath = minetest.get_modpath("snow")
minetest.register_on_shutdown(function() minetest.register_on_shutdown(function()
saveConfig(minetest.get_modpath("snow").."/config.txt", snow, doc) saveConfig(modpath.."/config.txt", snow, doc)
end) end)
local config = loadConfig(minetest.get_modpath("snow").."/config.txt") local config = loadConfig(modpath.."/config.txt")
if config then if config then
for i,v in pairs(config) do for i,v in pairs(config) do
if type(snow[i]) == type(v) then if type(snow[i]) == type(v) then
snow[i] = v snow[i] = v
else
minetest.log("error", "[snow] wrong type of setting "..i)
end end
end end
else else
saveConfig(minetest.get_modpath("snow").."/config.txt", snow, doc) saveConfig(modpath.."/config.txt", snow, doc)
end end
for i,v in pairs(snow) do for i,v in pairs(snow) do
local t = type(v) if allowed_types[type(v)] then
if t == "string"
or t == "number"
or t == "boolean" then
local v = minetest.setting_get("snow_"..i) local v = minetest.setting_get("snow_"..i)
if v ~= nil then if v ~= nil then
if v == "true" then v = true end snow[i] = value_from_string(v)
if v == "false" then v = false end
if tonumber(v) then v = tonumber(v) end
snow[i] = v
end end
end end
end end
@ -99,7 +115,7 @@ end
--MENU --MENU
local get_formspec = function() local function get_formspec()
local p = -0.5 local p = -0.5
local formspec = "label[0,-0.3;Settings:]" local formspec = "label[0,-0.3;Settings:]"
for i,v in pairs(snow) do for i,v in pairs(snow) do
@ -118,22 +134,32 @@ local get_formspec = function()
return formspec return formspec
end end
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,
})
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "snow:menu" then if formname ~= "snow:menu" then
return return
end end
for i,v in pairs(snow) do for i,v in pairs(snow) do
local t = type(v) local t = type(v)
if t == "string" or t == "number" or t == "boolean" then if allowed_types[t] then
local field = fields["snow:"..i] local field = fields["snow:"..i]
if field then if field then
if t == "string" then if t == "string" then
snow[i] = field snow[i] = field
end elseif t == "number" then
if t == "number" then local valid_number = tonumber(field)
snow[i] = tonumber(field) if valid_number then
end snow[i] = valid_number
if t == "boolean" then end
elseif t == "boolean" then
if field == "true" then if field == "true" then
snow[i] = true snow[i] = true
elseif field == "false" then elseif field == "false" then
@ -144,13 +170,3 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end end
end end
end) end)
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,
})