Fix player bone positions and properties setting

The comparison and setting logic in the previous patch that set player
bone positions and properties conditionally incorrectly did not update
some values (like player eye level position) when they changed. This
patch fixes it and adds asserts to ensure the code works as intended.
This commit is contained in:
cora 2021-12-08 22:48:14 +01:00 committed by Nils Dagsson Moskopp
parent d77f31eab8
commit 5deaabdb47
No known key found for this signature in database
GPG Key ID: A3BC671C35191080
1 changed files with 66 additions and 3 deletions

View File

@ -40,9 +40,10 @@ local function close_enough(a,b)
return rt
end
local function set_properties_conditional(player,props)
local oldprops=player:get_properties()
local changed=true
local function props_changed(props,oldprops)
local changed=false
local p={}
for k,v in pairs(props) do
if not close_enough(v,oldprops[k]) then
@ -50,6 +51,68 @@ local function set_properties_conditional(player,props)
changed=true
end
end
return changed,p
end
--test if assert works
assert(true)
assert(not false)
--test data for == and ~=
local test_equal1=42
local test_equal2=42.0
local test_equal3=42.1
assert(test_equal1==test_equal1)
assert(test_equal1==test_equal2)
assert(test_equal1~=test_equal3)
--testdata for roundN
local test_round1=15
local test_round2=15.00199999999
local test_round3=15.00111111
local test_round4=15.00999999
assert(roundN(test_round1,2)==roundN(test_round1,2)) --test again if basic equality works because wth not
assert(roundN(test_round1,2)==roundN(test_round2,2))
assert(roundN(test_round1,2)==roundN(test_round3,2))
assert(roundN(test_round1,2)~=roundN(test_round4,2))
-- tests for close_enough
local test_cb = {-0.35,0,-0.35,0.35,0.8,0.35} --collisionboxes
local test_cb_close = {-0.351213,0,-0.35,0.35,0.8,0.351212}
local test_cb_diff = {-0.35,0,-1.35,0.35,0.8,0.35}
local test_eh = 1.65 --eye height
local test_eh_close = 1.65123123
local test_eh_diff = 1.35
local test_nt = { r = 225, b = 225, a = 225, g = 225 } --nametag
local test_nt_diff = { r = 225, b = 225, a = 0, g = 225 }
assert(close_enough(test_cb,test_cb_close))
assert(not close_enough(test_cb,test_cb_diff))
assert(close_enough(test_eh,test_eh_close))
assert(not close_enough(test_eh,test_eh_diff))
assert(not close_enough(test_nt,test_nt_diff)) --no floats involved here
--tests for props_changed
local test_properties_set1={collisionbox = {-0.35,0,-0.35,0.35,0.8,0.35}, eye_height = 0.65, nametag_color = { r = 225, b = 225, a = 225, g = 225 }}
local test_properties_set2={collisionbox = {-0.35,0,-0.35,0.35,0.8,0.35}, eye_height = 1.35, nametag_color = { r = 225, b = 225, a = 225, g = 225 }}
local test_p1,p=props_changed(test_properties_set1,test_properties_set1)
local test_p2,p=props_changed(test_properties_set1,test_properties_set2)
assert(not test_p1)
assert(test_p2)
-- we still don't really know if lua is lying to us! but at least everything *seems* to be ok
local function set_properties_conditional(player,props)
local changed,p=props_changed(props,player:get_properties())
if changed then
player:set_properties(p)
end