Optimize code and modernize API calls

* Remove is_connected() and use minetest counterpart

* Less calls to minetest.get_connected_players()

* Deprecated: get_player_velocity

* Luacheck: unused assignements

* Luacheck: unused variable

* Luacheck: already declared variable

---------

Co-authored-by: mazes 80 <>
This commit is contained in:
mazes-80 2023-02-22 03:10:33 +01:00 committed by GitHub
parent a56116e8ab
commit 59bf43aa28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 33 deletions

View File

@ -43,7 +43,7 @@ local function check_hit(player, ray)
) )
origin = vector.add(origin, windpos) origin = vector.add(origin, windpos)
end end
local ray = minetest.raycast(origin, ppos) ray = minetest.raycast(origin, ppos)
local obj = ray:next() local obj = ray:next()
-- found nothing -- found nothing
if obj == nil then return false end if obj == nil then return false end

View File

@ -165,7 +165,7 @@ local function parse_config(player, particles)
-- correct spawn coordinates to adjust for player movement -- correct spawn coordinates to adjust for player movement
if config.adjust_for_velocity then if config.adjust_for_velocity then
local velocity = player:get_player_velocity() local velocity = player:get_velocity()
config.minpos = vector.add(config.minpos, velocity) config.minpos = vector.add(config.minpos, velocity)
config.maxpos = vector.add(config.maxpos, velocity) config.maxpos = vector.add(config.maxpos, velocity)
end end

View File

@ -1,7 +1,3 @@
local mod_player_monoids = minetest.get_modpath("player_monoids") ~= nil
local mod_playerphysics = minetest.get_modpath("playerphysics") ~= nil
local mod_pova = minetest.get_modpath("pova") ~= nil
local utility = {} local utility = {}
function utility.rangelim(value, min, max) function utility.rangelim(value, min, max)

View File

@ -1,15 +1,6 @@
local GSCYCLE = 0.03 * climate_mod.settings.tick_speed -- only process event loop after this amount of time local GSCYCLE = 0.03 * climate_mod.settings.tick_speed -- only process event loop after this amount of time
local WORLD_CYCLE = 30.00 * climate_mod.settings.tick_speed -- only update global environment influences after this amount of time local WORLD_CYCLE = 30.00 * climate_mod.settings.tick_speed -- only update global environment influences after this amount of time
local function is_connected(playername)
local connected = minetest.get_connected_players()
for _, player in ipairs(connected) do
local name = player:get_player_name()
if playername == name then return true end
end
return false
end
local gs_timer = 0 local gs_timer = 0
local world_timer = 0 local world_timer = 0
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
@ -29,7 +20,7 @@ minetest.register_globalstep(function(dtime)
-- skip weather changes for offline players -- skip weather changes for offline players
for effect, data in pairs(previous_effects) do for effect, data in pairs(previous_effects) do
for playername, _ in pairs(data) do for playername, _ in pairs(data) do
if not is_connected(playername) then if not minetest.get_player_by_name(playername) then
previous_effects[effect][playername] = nil previous_effects[effect][playername] = nil
end end
end end

View File

@ -19,7 +19,6 @@ soundloop.play = function(player, sound, fade)
if sounds[player] == nil then sounds[player] = {} end if sounds[player] == nil then sounds[player] = {} end
if sounds[player][sound.name] == nil then if sounds[player][sound.name] == nil then
step = sound.gain / fade step = sound.gain / fade
start_gain = 0
elseif sounds[player][sound.name] ~= sound.gain then elseif sounds[player][sound.name] ~= sound.gain then
minetest.sound_stop(sounds[player][sound.name].handle) minetest.sound_stop(sounds[player][sound.name].handle)
start_gain = sounds[player][sound.name].gain start_gain = sounds[player][sound.name].gain

View File

@ -68,7 +68,7 @@ local function is_weather_active(player, weather, env)
end end
local function get_weather_effects(player, weather_config, env) local function get_weather_effects(player, weather_config, env)
local config = {} local config
local effects = {} local effects = {}
if type(weather_config.effects) == "function" then if type(weather_config.effects) == "function" then
config = weather_config.effects(env) config = weather_config.effects(env)
@ -85,22 +85,19 @@ end
function trigger.get_active_effects() function trigger.get_active_effects()
local environments = {} local environments = {}
local effects = {}
climate_mod.current_weather = {}
for _, player in ipairs(minetest.get_connected_players()) do for _, player in ipairs(minetest.get_connected_players()) do
local playername = player:get_player_name() local pname = player:get_player_name()
local hp = player:get_hp() local hp = player:get_hp()
-- skip weather presets for dead players -- skip weather presets for dead players
if hp ~= nil and hp > 0 then if hp ~= nil and hp > 0 then
environments[playername] = trigger.get_player_environment(player) environments[pname] = trigger.get_player_environment(player)
end end
end
local effects = {}
climate_mod.current_weather = {}
for wname, wconfig in pairs(climate_mod.weathers) do
for _, player in ipairs(minetest.get_connected_players()) do
local pname = player:get_player_name()
local env = environments[pname] local env = environments[pname]
if env ~= nil then if env ~= nil then
for wname, wconfig in pairs(climate_mod.weathers) do
if is_weather_active(player, wname, env) then if is_weather_active(player, wname, env) then
if climate_mod.current_weather[pname] == nil then if climate_mod.current_weather[pname] == nil then
climate_mod.current_weather[pname] = {} climate_mod.current_weather[pname] = {}