Add support for defaults flag

This commit is contained in:
Wuzzy 2022-04-20 04:28:06 +02:00
parent f76245ed1c
commit 458fd6e2d4

View File

@ -70,7 +70,7 @@ local default_noiseparams = {
octaves = 2,
persistence = 0.5,
lacunarity = 2.0,
flags = "noeased,noabsvalue",
flags = "defaults,noabsvalue",
}
-- Holds the currently used Perlin noise
@ -130,32 +130,41 @@ local sidelen_pos = function(pos, sidelen)
return newpos
end
local build_flags_string = function(eased, absvalue)
local flags = ""
local build_flags_string = function(defaults, eased, absvalue)
local flagst = {}
if defaults then
table.insert(flagst, "defaults")
end
if eased then
flags = "eased"
table.insert(flagst, "eased")
else
flags = "noeased"
table.insert(flagst, "noeased")
end
if absvalue then
flags = flags .. ",absvalue"
table.insert(flagst, "absvalue")
else
flags = flags .. ",noabsvalue"
table.insert(flagst, "noabsvalue")
end
local flags = table.concat(flagst, ",")
return flags
end
local parse_flags_string = function(flags)
local ftable = string.split(flags, ",")
local eased, absvalue = false, false
local defaults, eased, absvalue = false, false, false
for f=1, #ftable do
local s = string.trim(ftable[f])
if s == "eased" then
if s == "defaults" then
defaults = true
elseif s == "eased" then
eased = true
elseif s == "absvalue" then
absvalue = true
end
end
return { eased = eased, absvalue = absvalue }
if not defaults and not eased and not absvalue then
defaults = true
end
return { defaults = defaults, eased = eased, absvalue = absvalue }
end
-- Sets the currently active Perlin noise.
@ -1046,8 +1055,10 @@ local show_noise_formspec = function(player, noiseparams, profile_id)
local flags = np.flags
local flags_table = parse_flags_string(flags)
local eased = tostring(flags_table.eased)
local absvalue = tostring(flags_table.absvalue)
minetest.log("error", dump(flags_table))
local flag_defaults = tostring(flags_table.defaults)
local flag_eased = tostring(flags_table.eased)
local flag_absvalue = tostring(flags_table.absvalue)
local noiseparams_list = {}
local counter = 1
@ -1118,8 +1129,9 @@ local show_noise_formspec = function(player, noiseparams, profile_id)
field[0.25,2.4;2,0.75;octaves;]]..F(S("Octaves"))..[[;]]..octaves..[[]
field[3.25,2.4;2,0.75;persistence;]]..F(S("Persistence"))..[[;]]..persistence..[[]
field[6.25,2.4;2,0.75;lacunarity;]]..F(S("Lacunarity"))..[[;]]..lacunarity..[[]
checkbox[0.25,3.55;eased;]]..F(S("eased"))..[[;]]..eased..[[]
checkbox[3.25,3.55;absvalue;]]..F(S("absvalue"))..[[;]]..absvalue..[[]
checkbox[0.25,3.55;defaults;]]..F(S("defaults"))..[[;]]..flag_defaults..[[]
checkbox[2.25,3.55;eased;]]..F(S("eased"))..[[;]]..flag_eased..[[]
checkbox[4.25,3.55;absvalue;]]..F(S("absvalue"))..[[;]]..flag_absvalue..[[]
button[6.25,3.35;2.0,0.5;analyze;]]..F(S("Analyze"))..[[]
container_end[]
@ -1239,6 +1251,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
-- Handle checkboxes
local name = player:get_player_name()
local flags_touched = false
if fields.defaults == "true" then
formspec_states[name].defaults = true
return
elseif fields.defaults == "false" then
formspec_states[name].defaults = false
return
end
if fields.eased == "true" then
formspec_states[name].eased = true
return
@ -1304,6 +1323,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local value_max = tonumber(fields.value_max)
local nodetype = tonumber(fields.nodetype)
if (offset and scale and spread and octaves and persistence) then
local defaults = formspec_states[name].defaults
local eased = formspec_states[name].eased
local absvalue = formspec_states[name].absvalue
local noiseparams = {
@ -1314,7 +1334,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
octaves = octaves,
persistence = persistence,
lacunarity = lacunarity,
flags = build_flags_string(eased, absvalue),
flags = build_flags_string(defaults, eased, absvalue),
}
noiseparams = fix_noiseparams(noiseparams)
-- Open analyze window
@ -1494,7 +1514,9 @@ end)
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
formspec_states[name] = {
defaults = true,
eased = false,
absvalue = false,
}
end)
minetest.register_on_leaveplayer(function(player)