104 lines
3.0 KiB
Lua
Raw Normal View History

2018-12-16 02:01:19 +00:00
-- 0 is the lowest priority
-- inf is the highest priority
2018-12-16 02:29:35 +00:00
player_physics_table = {}
player_physics_multipliers = {}
2018-12-16 02:01:19 +00:00
local function check_for_entry(pName)
if player_physics_table[pName] == nil then
2018-12-16 02:29:35 +00:00
local pTable = {speed = 1, jump = 1, gravity = 1, sneak = true, sneak_glitch = true, new_move = false}
2018-12-16 02:01:19 +00:00
player_physics_table[pName] = {default = {priority = 0, physics = pTable}}
end
end
local function check_for_multiplier(pName)
if player_physics_multipliers[pName] == nil then
player_physics_multipliers[pName] = {}
end
end
2018-12-16 02:29:35 +00:00
local function get_highest_priority(t, element)
2018-12-16 02:01:19 +00:00
local highestPriority = -1
local highest = nil
2018-12-16 02:29:35 +00:00
for id, pTable in pairs(t) do
2018-12-18 23:04:22 +00:00
if pTable.physics[element] ~= nil then
2018-12-16 02:29:35 +00:00
if pTable.priority > highestPriority then
highestPriority = pTable.priority
highest = pTable.physics[element]
end
2018-12-16 02:01:19 +00:00
end
end
return highest, highestPriority
end
local function apply_physics_override(t, element, to, targetP)
for id, pTable in pairs(t) do
2018-12-18 23:04:22 +00:00
if pTable.mults[element] ~= nil then
if pTable.priority >= targetP then
to = pTable.mults[element] * to
end
end
end
return to
2018-12-16 02:29:35 +00:00
end
local function update_physics(player)
-- Get player name
2018-12-16 02:29:35 +00:00
local n = player:get_player_name()
-- Get table of physics overrides
2018-12-16 02:29:35 +00:00
local requests = player_physics_table[n]
-- Get active physics overrides
local speed, speedP = get_highest_priority(requests, "speed")
local jump, jumpP = get_highest_priority(requests, "jump")
local gravity, gravityP = get_highest_priority(requests, "gravity")
2018-12-18 23:04:22 +00:00
local sneak, _ = get_highest_priority(requests, "sneak")
local sneak_glitch, _ = get_highest_priority(requests, "sneak_glitch")
local new_move, _ = get_highest_priority(requests, "new_move")
-- Apply multipliers
local mults = player_physics_multipliers[n]
if mults then
speed = apply_physics_override(mults, "speed", speed, speedP)
jump = apply_physics_override(mults, "jump", jump, jumpP)
gravity = apply_physics_override(mults, "gravity", gravity, gravityP)
end
-- Condense final player physics
2018-12-16 02:29:35 +00:00
local highest = {
speed = speed,
jump = jump,
gravity = gravity,
sneak = sneak,
sneak_glitch = sneak_glitch,
2018-12-18 23:04:22 +00:00
new_move = new_move,
2018-12-16 02:29:35 +00:00
}
2018-12-16 02:01:19 +00:00
player:set_physics_override(highest)
end
function set_player_physics(player, phys, priority, id)
local pName = player:get_player_name()
check_for_entry(pName)
player_physics_table[pName][id] = {priority = priority, physics = phys}
update_physics(player)
end
function reset_player_physics(player, id)
local pName = player:get_player_name()
check_for_entry(pName)
player_physics_table[pName][id] = nil
update_physics(player)
end
function set_player_physics_multiplier(player, mults, priority, id)
local pName = player:get_player_name()
check_for_entry(pName)
check_for_multiplier(pName)
player_physics_multipliers[pName][id] = {priority = priority, mults = mults}
update_physics(player)
end
function remove_player_physics_multiplier(player, id)
local pName = player:get_player_name()
check_for_entry(pName)
player_physics_multipliers[pName][id] = nil
update_physics(player)
end