Change from table indirection to getter/setter functions for dynamically updating local variables

This commit is contained in:
teknomunk 2024-11-17 07:06:03 -06:00
parent 26b16b412e
commit 49bf10b6c7
15 changed files with 160 additions and 71 deletions

View File

@ -34,17 +34,29 @@ mcl_damage = {
}
}
local damage_enabled = vl_tuning.setting("damage_enabled", "bool",{
default = minetest.settings:get_bool("enabled_damage",true)
local damage_enabled = true
vl_tuning.setting("damage_enabled", "bool",{
default = minetest.settings:get_bool("enabled_damage",true),
set = function(val) damage_enabled = val end,
get = function() return damage_enabled end,
})
local fall_damage_enabled = vl_tuning.setting("gamerule:fallDamage", "bool", {
default = true
local fall_damage_enabled = true
vl_tuning.setting("gamerule:fallDamage", "bool", {
default = true,
set = function(val) fall_damage_enabled = val end,
get = function() return fall_damage_enabled end,
})
local drowning_damage_enabled = vl_tuning.setting("gamerule:drowningDamage", "bool", {
default = true
local drowning_damage_enabled = true
vl_tuning.setting("gamerule:drowningDamage", "bool", {
default = true,
set = function(val) drowning_damage_enabled = val end,
get = function() return drowning_damage_enabled end,
})
local fire_damage_enabled = vl_tuning.setting("gamerule:fireDamage", "bool", {
default = true
local fire_damage_enabled
vl_tuning.setting("gamerule:fireDamage", "bool", {
default = true,
set = function(val) fire_damage_enabled = val end,
get = function() return fire_damage_enabled end,
})
function mcl_damage.register_modifier(func, priority)
@ -153,7 +165,7 @@ function mcl_damage.register_type(name, def)
end
minetest.register_on_player_hpchange(function(player, hp_change, mt_reason)
if not damage_enabled[1] then return 0 end
if not damage_enabled then return 0 end
if hp_change < 0 then
if player:get_hp() <= 0 then
return 0
@ -165,11 +177,11 @@ end, true)
minetest.register_on_player_hpchange(function(player, hp_change, mt_reason)
-- Check if damage is enabled
if not damage_enabled[1] then return 0 end
if not damage_enabled then return 0 end
local mcl_reason = mcl_damage.from_mt(mt_reason)
if not fire_damage_enabled[1] and mcl_reason.type == "fire" then return 0 end
--if not drowning_damage_enabled[1] and mcl_reason.type == "drown" then return 0 end
--if not fall_damage_enabled[1] and mcl_reason.type == "fall" then return 0 end
if not fire_damage_enabled and mcl_reason.type == "fire" then return 0 end
--if not drowning_damage_enabled and mcl_reason.type == "drown" then return 0 end
--if not fall_damage_enabled and mcl_reason.type == "fall" then return 0 end
--minetest.log("action", "mcl_reason = "..dump(mcl_reason)..", mt_reason = "..dump(mt_reason))

View File

@ -5,6 +5,8 @@ local storage = minetest.get_mod_storage()
local mod = {}
vl_tuning = mod
local DEBUG = false
-- All registered tunable parameters
local tunables = {}
vl_tuning.registered_settings = tunables
@ -35,12 +37,14 @@ function tunable_class:set(value, no_hook)
local new_value = self_type.from_string(value)
if new_value == nil then new_value = self.default end
self[1] = new_value
self.setter(new_value)
else
self[1] = value
self.setter(value)
end
minetest.log("action", "[vl_tuning] Set "..self.setting.." to "..dump(self[1]))
if DEBUG then
minetest.log("action", "[vl_tuning] Set "..self.setting.." to "..dump(self.getter()))
end
-- Call on_change hook
if not no_hook then
@ -49,10 +53,10 @@ function tunable_class:set(value, no_hook)
end
-- Persist value
storage:set_string(self.setting,self_type.to_string(self[1]))
storage:set_string(self.setting,self_type.to_string(self.getter()))
end
function tunable_class:get_string()
return self.type.to_string(self[1])
return self.type.to_string(self.getter())
end
function mod.setting(setting, setting_type, def )
@ -61,20 +65,28 @@ function mod.setting(setting, setting_type, def )
if tunable then return tunable end
assert(setting_type)
assert(def)
assert(type(def.set) == "function", "Tunable requires set method")
assert(type(def.get) == "function", "Tunable required get method")
-- Setup the tunable data
tunable = table.copy(def)
tunable.setting = setting
tunable.setter = def.set
tunable.getter = def.get
tunable.type = tunable_types[setting_type]
tunable.setting_type = setting_type
tunable[1] = tunable.default
if tunable.default then
tunable.set(tunable.default)
end
setmetatable(tunable, {__index=tunable_class})
-- Load the setting value from mod storage
local setting_value = storage:get_string(setting)
if setting_value and setting_value ~= "" then
tunable:set(setting_value, true)
minetest.log("action", "[vl_tuning] Loading "..setting.." = "..dump(setting_value).." ("..dump(tunable[1])..")")
if DEBUG then
minetest.log("action", "[vl_tuning] Loading "..setting.." = "..dump(setting_value).." ("..dump(tunable[1])..")")
end
end
-- Add to the list of all available settings
@ -104,7 +116,9 @@ minetest.register_chatcommand("set_setting", {
return false, S("Setting @1 doesn't exist", params[1])
end
minetest.log("action", "[vl_tuning] "..name.." set ".. params[1] .." to "..params[2])
if DEBUG then
minetest.log("action", "[vl_tuning] "..name.." set ".. params[1] .." to "..params[2])
end
tunable:set(params[2])
return true
end
@ -145,7 +159,9 @@ minetest.register_chatcommand("gamerule", {
local value = params[2]
if value then
minetest.log("action", "[vl_tuning] Setting game rule "..params[1].." to "..params[2])
if DEBUG then
minetest.log("action", "[vl_tuning] Setting game rule "..params[1].." to "..params[2])
end
tunable:set(params[2])
return true
else
@ -156,3 +172,9 @@ minetest.register_chatcommand("gamerule", {
dofile(modpath.."/settings.lua")
dofile(modpath.."/gui.lua")
mod.setting("debug:vl_tuning:report_value_changes", "bool", {
default = false,
set = function(val) DEBUG = val end,
get = function() return DEBUG end,
})

View File

@ -2,11 +2,17 @@ local modname = minetest.get_current_modname()
local S = minetest.get_translator(modname)
local mod = vl_tuning
mod.keep_inventory = vl_tuning.setting("gamerule:keepInventory", "bool", {
mod.keep_inventory = {}
vl_tuning.setting("gamerule:keepInventory", "bool", {
default = minetest.settings:get_bool("mcl_keepInventory", false),
set = function(val) mod.keep_inventory[1] = val end,
get = function() return mod.keep_inventory[1] end,
})
mod.respawn_blocks_explode = vl_tuning.setting("gamerule:respawnBlocksExplode", "bool", {
mod.respawn_blocks_explode = {}
vl_tuning.setting("gamerule:respawnBlocksExplode", "bool", {
description = S("Prevents beds/respawn anchors from exploding in other dimensions."),
default = true,
set = function(val) mod.respawn_blocks_explode[1] = val end,
get = function() return mod.respawn_blocks_explode[1] end,
})

View File

@ -4,13 +4,19 @@ local math, vector, minetest, mcl_mobs = math, vector, minetest, mcl_mobs
local mob_class = mcl_mobs.mob_class
local validate_vector = mcl_util.validate_vector
local gamerule_maxEntityCramming = vl_tuning.setting("gamerule:maxEntityCramming", "number", {
local gamerule_maxEntityCramming = 24
vl_tuning.setting("gamerule:maxEntityCramming", "number", {
description = S("The maximum number of pushable entities a mob or player can push, before taking 6♥♥♥ entity cramming damage per half-second."),
default = 24,
set = function(val) gamerule_maxEntityCramming = val end,
get = function() return gamerule_maxEntityCramming end,
})
local gamerule_doMobLoot = vl_tuning.setting("gamerule:doMobLoot", "bool", {
local gamerule_doMobLoot
vl_tuning.setting("gamerule:doMobLoot", "bool", {
description = S("Whether mobs should drop items and experience orbs."),
default = true,
set = function(val) gamerule_doMobLoot = val end,
get = function() return gamerule_doMobLoot end,
})
local CRAMMING_DAMAGE = 3
@ -482,7 +488,7 @@ function mob_class:check_for_death(cause, cmi_cause)
-- TODO other env damage shouldn't drop xp
-- "rain", "water", "drowning", "suffocation"
if not gamerule_doMobLoot[1] then return end
if not gamerule_doMobLoot then return end
-- dropped cooked item if mob died in fire or lava
if cause == "lava" or cause == "fire" then
@ -912,7 +918,7 @@ function mob_class:check_entity_cramming()
local l = o:get_luaentity()
if l and l.is_mob and l.health > 0 then table.insert(mobs,l) end
end
local clear = #mobs < gamerule_maxEntityCramming[1]
local clear = #mobs < gamerule_maxEntityCramming
local ncram = {}
for _,l in pairs(mobs) do
if l then
@ -926,7 +932,7 @@ function mob_class:check_entity_cramming()
end
end
for i,l in pairs(ncram) do
if i > gamerule_maxEntityCramming[1] then
if i > gamerule_maxEntityCramming then
l.cram = true
else
l.cram = nil

View File

@ -3,8 +3,11 @@ local S = minetest.get_translator("mcl_mobs")
local math, vector, minetest, mcl_mobs = math, vector, minetest, mcl_mobs
local mob_class = mcl_mobs.mob_class
local gamerule_doMobSpawning = vl_tuning.setting("gamerule:doMobSpawning", "bool", {
description = S("Whether mobs should spawn naturally, or via global spawning logic, such as for cats, phantoms, patrols, wandering traders, or zombie sieges. Does not affect special spawning attempts, like monster spawners, raids, or iron golems."), default = true
local gamerule_doMobSpawning = true
vl_tuning.setting("gamerule:doMobSpawning", "bool", {
description = S("Whether mobs should spawn naturally, or via global spawning logic, such as for cats, phantoms, patrols, wandering traders, or zombie sieges. Does not affect special spawning attempts, like monster spawners, raids, or iron golems."), default = true,
set = function(val) gamerule_doMobSpawning = val end,
get = function() return gamerule_doMobSpawning end,
})
local modern_lighting = minetest.settings:get_bool("mcl_mobs_modern_lighting", true)
@ -1013,7 +1016,7 @@ if mobs_spawn then
local timer = 0
minetest.register_globalstep(function(dtime)
if not gamerule_doMobSpawning[1] then return end
if not gamerule_doMobSpawning then return end
timer = timer + dtime
if timer < WAIT_FOR_SPAWN_ATTEMPT then return end

View File

@ -2,8 +2,11 @@
mcl_raids = {}
local S = minetest.get_translator(minetest.get_current_modname())
local gamerule_disableRaids = vl_tuning.setting("gamerule:disableRaids", "bool", {
local gamerule_disableRaids = false
vl_tuning.setting("gamerule:disableRaids", "bool", {
description = S("Whether raids are disabled"), default = false,
set = function(val) gamerule_disableRaids = val end,
get = function() return gamerule_disableRaids end,
})
-- Define the amount of illagers to spawn each wave.
@ -295,7 +298,7 @@ mcl_events.register_event("raid",{
exclusive_to_area = 128,
enable_bossbar = true,
cond_start = function(self)
if gamerule_disableRaids[1] then return false end
if gamerule_disableRaids then return false end
--minetest.log("Cond start raid")
local r = {}

View File

@ -8,28 +8,39 @@ local NIGHT_VISION_RATIO = 0.45
local DEBUG = false
-- Settings
local minimum_update_interval = { 250e3 }
local minimum_update_interval = 250e3
vl_tuning.setting("minimum_sky_update_interval", "number", {
default = 0.25,
set = function(val) minimum_update_interval = val * 1e6 end,
get = function() return minimum_update_interval * 1e-6 end,
})
-- Module state
local mods_loaded = false
-- Daylight cycle handling
local fixed_time = vl_tuning.setting("fixed_daylight_time", "number", {
local fixed_time = 0.5
local fixed_time_setting = vl_tuning.setting("fixed_daylight_time", "number", {
description = S("Time of day to use when gamerule:doDaylightCycle == false"),
default = 0.5
default = 0.5,
set = function(val) fixed_time = val end,
get = function() return fixed_time end,
})
local gamerule_doDaylightCycle = vl_tuning.setting("gamerule:doDaylightCycle", "bool",{
local gamerule_doDaylightCycle = true
vl_tuning.setting("gamerule:doDaylightCycle", "bool",{
description = S("Whether the daylight cycle and moon phases progress"),
default = true,
set = function(val) gamerule_doDaylightCycle = val end,
get = function() return gamerule_doDaylightCycle end,
on_change = function(self)
if not self[1] then
fixed_time:set(minetest.get_timeofday())
fixed_time_setting:set(minetest.get_timeofday())
end
end
})
local function daylightCycle()
if not gamerule_doDaylightCycle[1] and fixed_time[1] then
minetest.set_timeofday(fixed_time[1])
if not gamerule_doDaylightCycle and fixed_time then
minetest.set_timeofday(fixed_time)
end
minetest.after(1, daylightCycle)
end
@ -170,7 +181,7 @@ function skycolor.update_player_sky_color(player)
local skycolor_data = get_skycolor_info(player)
local last_update = skycolor_data.last_update or 0
local now_us = minetest.get_us_time()
if (now_us - last_update) < minimum_update_interval[1] then return end
if (now_us - last_update) < minimum_update_interval then return end
skycolor_data.last_update = now_us
local sky_data = {

View File

@ -7,9 +7,12 @@ mcl_weather.snow = {}
local PARTICLES_COUNT_SNOW = tonumber(minetest.settings:get("mcl_weather_snow_particles")) or 100
mcl_weather.snow.init_done = false
local mgname = minetest.get_mapgen_setting("mg_name")
local gamerule_snowAccumulationHeight = vl_tuning.setting("gamerule:snowAccumulationHeight", "number", {
local gamerule_snowAccumulationHeight = 1
vl_tuning.setting("gamerule:snowAccumulationHeight", "number", {
description = S("The maximum number of snow layers that can be accumulated on each block"),
default = 1, min = 0, max = 8
default = 1, min = 0, max = 8,
set = function(val) gamerule_snowAccumulationHeight = val end,
get = function() return gamerule_snowAccumulationHeight end,
})
local snow_biomes = {
@ -147,7 +150,7 @@ minetest.register_abm({
if node.name:find("snow") then
local l = node.name:sub(-1)
l = tonumber(l)
if l < gamerule_snowAccumulationHeight[1] then
if l < gamerule_snowAccumulationHeight then
if node.name == "mcl_core:snow" then
nn={name = "mcl_core:snow_2"}
elseif l and l < 7 then

View File

@ -2,8 +2,11 @@ local S = minetest.get_translator(minetest.get_current_modname())
local math = math
local gamerule_doWeatherCycle = vl_tuning.setting("gamerule:doWeatherCycle", "bool", {
description = S("Whether the weather can change naturally. The /weather command can still change weather."), default = true
local gamerule_doWeatherCycle = true
vl_tuning.setting("gamerule:doWeatherCycle", "bool", {
description = S("Whether the weather can change naturally. The /weather command can still change weather."), default = true,
set = function(val) gamerule_doWeatherCycle = val end,
get = function() return gamerule_doWeatherCycle end,
})
-- weather states, 'none' is default, other states depends from active mods
@ -130,7 +133,7 @@ end
local t, wci = 0, mcl_weather.check_interval
minetest.register_globalstep(function(dtime)
if not gamerule_doWeatherCycle[1] then return end
if not gamerule_doWeatherCycle then return end
t = t + dtime
if t < wci then return end

View File

@ -19,12 +19,18 @@ local modpath = minetest.get_modpath(modname)
local S = minetest.get_translator(modname)
-- Tunable parameters
local notif_delay = vl_tuning.setting("award_display_time", "number", {
description = S("Amount of time award notification are displayed"), default = 3, min = 2, max = 10
local notif_delay = 3
vl_tuning.setting("award_display_time", "number", {
description = S("Amount of time award notification are displayed"), default = 3, min = 2, max = 10,
set = function(val) notif_delay = val end,
get = function() return notif_delay end,
})
local announce_in_chat = vl_tuning.setting("gamerule:announceAdvancements", "bool", {
local announce_in_chat = true
vl_tuning.setting("gamerule:announceAdvancements", "bool", {
description = S("Whether advancements should be announced in chat"),
default = minetest.settings:get_bool("mcl_showAdvancementMessages", true),
set = function(val) announce_in_chat = val end,
get = function() return announce_in_chat end,
})
-- The global award namespace
@ -226,7 +232,7 @@ function awards.unlock(name, award)
-- Get award
minetest.log("action", name.." has gotten award "..award)
if announce_in_chat[1] then
if announce_in_chat then
minetest.chat_send_all(S("@1 has made the advancement @2", name, minetest.colorize(mcl_colors.GREEN, "[" .. (awdef.title or award) .. "]")))
end
data.unlocked[award] = award
@ -371,7 +377,7 @@ function awards.unlock(name, award)
direction = 0,
z_index = 102,
})
minetest.after(notif_delay[1], function(name)
minetest.after(notif_delay, function(name)
local player = minetest.get_player_by_name(name)
if not player then
return

View File

@ -1,9 +1,12 @@
local S = minetest.get_translator(minetest.get_current_modname())
local ASSIST_TIMEOUT_SEC = 5
local gamerule_showDeathMessages = vl_tuning.setting("gamerule:showDeathMessages", "bool", {
local gamerule_showDeathMessages = true
vl_tuning.setting("gamerule:showDeathMessages", "bool", {
description = S("Whether death messages are put into chat when a player dies. Also affects whether a message is sent to the pet's owner when the pet dies."),
default = minetest.settings:get_bool("mcl_showDeathMessages", true),
set = function(val) gamerule_showDeathMessages = val end,
get = function() return gamerule_showDeathMessages end,
})
mcl_death_messages = {
@ -208,7 +211,7 @@ local function fallback_translator(s)
end
mcl_damage.register_on_death(function(obj, reason)
if not gamerule_showDeathMessages[1] then return end
if not gamerule_showDeathMessages then return end
local send_to

View File

@ -126,7 +126,7 @@ function ARROW_ENTITY.on_step(self, dtime)
local node = minetest.get_node(dpos)
if self._stuck then
self._stucktimer = self._stucktimer + dtime
self._stucktimer = self._stucktimer or 0 + dtime
self._stuckrechecktimer = self._stuckrechecktimer + dtime
if self._stucktimer > ARROW_TIMEOUT then
mcl_burning.extinguish(self.object)

View File

@ -1202,9 +1202,12 @@ end
---------------------
-- Vine generating --
---------------------
local do_vines_spread = vl_tuning.setting("gamerule:doVinesSpread", "bool", {
local do_vines_spread = true
vl_tuning.setting("gamerule:doVinesSpread", "bool", {
description = S("Whether vines can spread to other blocks. Cave vines, weeping vines, and twisting vines are not affected."),
default = true,
set = function(val) do_vines_spread = val end,
get = function() return do_vines_spread end,
})
minetest.register_abm({
label = "Vine growth",
@ -1212,10 +1215,9 @@ minetest.register_abm({
interval = 47,
chance = 4,
action = function(pos, node, active_object_count, active_object_count_wider)
-- First of all, check if we are even supported, otherwise, decay.
if not do_vines_spread[1] then return end
if not do_vines_spread then return end
-- First of all, check if we are even supported, otherwise, let's die!
-- First of all, check if we are even supported, otherwise, decay.
if not mcl_core.check_vines_supported(pos, node) then
minetest.remove_node(pos)
vinedecay_particles(pos, node)

View File

@ -7,8 +7,11 @@ local modpath = minetest.get_modpath(modname)
local S = minetest.get_translator(modname)
local has_mcl_portals = minetest.get_modpath("mcl_portals")
local gamerule_doFireTick = vl_tuning.setting("gamerule:doFireTick", "bool", {
description = S("Whether fire should spread and naturally extinguish"), default = true
local gamerule_doFireTick = true
vl_tuning.setting("gamerule:doFireTick", "bool", {
description = S("Whether fire should spread and naturally extinguish"), default = true,
set = function(val) gamerule_doFireTick = val end,
get = function() return gamerule_doFireTick end,
})
local set_node = minetest.set_node
@ -369,7 +372,7 @@ else -- Fire enabled
chance = 12,
catch_up = false,
action = function(pos)
if not gamerule_doFireTick[1] then return end
if not gamerule_doFireTick then return end
local p = get_ignitable(pos)
if p then
@ -388,7 +391,7 @@ else -- Fire enabled
chance = 9,
catch_up = false,
action = function(pos)
if not gamerule_doFireTick[1] then return end
if not gamerule_doFireTick then return end
local p=get_ignitable_by_lava(pos)
if p then
@ -404,7 +407,7 @@ else -- Fire enabled
chance = 3,
catch_up = false,
action = function(pos)
if not gamerule_doFireTick[1] then return end
if not gamerule_doFireTick then return end
local p=has_flammable(pos)
if p then
@ -427,7 +430,7 @@ else -- Fire enabled
chance = 18,
catch_up = false,
action = function(pos)
if not gamerule_doFireTick[1] then return end
if not gamerule_doFireTick then return end
local p = has_flammable(pos)
if not p then

View File

@ -3,11 +3,17 @@ local modpath = minetest.get_modpath(modname)
local S = minetest.get_translator(modname)
local max_tick_timer = vl_tuning.setting("health_regen_delay", "number", {
local max_tick_timer
vl_tuning.setting("health_regen_delay", "number", {
default = tonumber(minetest.settings:get("mcl_health_regen_delay")) or 0.5,
set = function(val) max_tick_timer = val end,
get = function() return max_tick_timer end,
})
local natural_regeneration = vl_tuning.setting("gamerule:naturalRegeneration", "bool", {
local natural_regeneration = true
vl_tuning.setting("gamerule:naturalRegeneration", "bool", {
default = true,
set = function(val) natural_regeneration = val end,
get = function() return natural_regeneration end,
})
mcl_hunger = {}
@ -259,7 +265,7 @@ minetest.register_globalstep(function(dtime)
end
if food_level >= 18 then -- slow regeneration
if natural_regeneration[1] and player_health > 0 and player_health < player:get_properties().hp_max then
if natural_regeneration and player_health > 0 and player_health < player:get_properties().hp_max then
player:set_hp(player_health+1)
mcl_hunger.exhaust(player_name, mcl_hunger.EXHAUST_REGEN)
mcl_hunger.update_exhaustion_hud(player)
@ -275,8 +281,8 @@ minetest.register_globalstep(function(dtime)
end
end
elseif food_tick_timer > max_tick_timer[1] and food_level == 20 and food_saturation_level > 0 then -- fast regeneration
if natural_regeneration[1] and player_health > 0 and player_health < player:get_properties().hp_max then
elseif food_tick_timer > max_tick_timer and food_level == 20 and food_saturation_level > 0 then -- fast regeneration
if natural_regeneration and player_health > 0 and player_health < player:get_properties().hp_max then
food_tick_timer = 0
player:set_hp(player_health+1)
mcl_hunger.exhaust(player_name, mcl_hunger.EXHAUST_REGEN)