tidy code

This commit is contained in:
tenplus1 2024-08-14 11:43:07 +01:00
parent 1c220e4d4b
commit 0de1209966
2 changed files with 14 additions and 17 deletions

View File

@ -1,15 +1,19 @@
-- global
pova = {debug = false}
-- local
local pova_list = {}
local min, max = math.min, math.max
-- time each override loop runs, 0 to disable
local pova_loop = minetest.settings:get_bool("pova_loop") or 1.0
-- if enabled activate main loop that totals override list on timer
if pova_loop > 0 then
local timer = 0
@ -18,9 +22,7 @@ if pova_loop > 0 then
timer = timer + dtime
if timer < pova_loop then
return
end
if timer < pova_loop then return end
timer = 0
@ -31,19 +33,17 @@ if pova_loop > 0 then
end)
end
-- global functions
pova.add_override = function(name, item, def)
-- nil check
if not name or not item then return end
-- priority defaults to 50 when not included
def.priority = tonumber(def.priority) or 50
-- if same item is assigned with lower priority then change ignored
if pova_list[name][item]
and pova_list[name][item].priority
if pova_list[name][item] and pova_list[name][item].priority
and pova_list[name][item].priority > def.priority then
return
end
@ -66,9 +66,7 @@ pova.do_override = function(player)
local name = player and player:get_player_name()
-- somehow player is missing on list
if not name or not pova_list[name] then
return
end
if not name or not pova_list[name] then return end
-- set base defaults
local speed, jump, gravity = 1, 1, 1
@ -84,8 +82,7 @@ pova.do_override = function(player)
for id, var in pairs(pova_list[name]) do
-- skip any custom names
if var
and id ~= "default" and id ~= "min" and id ~= "max" and id ~= "force" then
if var and id ~= "default" and id ~= "min" and id ~= "max" and id ~= "force" then
-- add any additional changes
speed = speed + (pova_list[name][id].speed or 0)
@ -124,14 +121,15 @@ pova.do_override = function(player)
player:set_physics_override({speed = speed, jump = jump, gravity = gravity})
end
-- set player table on join
minetest.register_on_joinplayer(function(player)
pova_list[ player:get_player_name() ] = {}
pova.do_override(player)
end)
-- reset player table on respawn
minetest.register_on_respawnplayer(function(player)
if player then
pova_list[ player:get_player_name() ] = {}
@ -140,17 +138,19 @@ minetest.register_on_respawnplayer(function(player)
end)
-- blank player table on leave
minetest.register_on_leaveplayer(function(player)
if player then
pova_list[ player:get_player_name() ] = nil
end
end)
-- axe tool to showcase features
minetest.register_craftitem("pova:axe", {
description = "Test Axe (left to apply, right to remove effects)",
inventory_image = "default_tool_steelaxe.png",
groups = {not_in_creative_inventory = 1},
on_use = function(itemstack, user, pointed_thing)
@ -198,5 +198,4 @@ minetest.register_craftitem("pova:axe", {
end
})
print("[MOD] Pova loaded")

View File

@ -1,5 +1,3 @@
name = pova
description = Gives mod makers a set of functions to safely apply player physics overrides.
depends =
optional_depends =
min_minetest_version = 5.0