From b1cc74fe3be94837c5eff77aecf105a2c5e6785f Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 2 Jan 2023 15:35:10 -0800 Subject: [PATCH 1/7] Remove unused vars, convert tug_crate arg to table The tug_crate conversion is for performance. `foreach(tbl, predefined_func)` is substantially faster than a standard `for` loop using the `all` iterator. However, if the function inside the foreach is defined inline, it's much slower due to closure-construction overhead (even though nothing is being closed over). Converting `tug_crate` to take a table as an argument allows foreach to feed right into it, and it also naturally suggests a rewrite a few lines down to get rid of duplicative listing of `mx0,my0,dmx,dmy`, saving several tokens. I'm going to take a look at can_move to see if it's worth making iits mx0,my0,dmx,dmy arguments into a table as well. --- chameleonic.p8 | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index 11ed61c..ec2a908 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -652,7 +652,9 @@ function level:can_move( return true end -function level:tug_crate(mx0,my0,dmx,dmy) +-- argument is array-like: mx0,my0,dmx,dmy +function level:tug_crate(t) + local mx0,my0,dmx,dmy=unpack(t) local mxy0=_mix{mx0,my0} local existing=self._crates[mxy0] if (not existing) return @@ -1540,22 +1542,19 @@ function rope:_tug(hypothetically) local blocks = {} for i=#ancs-1,2,-1 do local ops_before_trash,blocks_before_trash=self:_calc_push(ancs[i+1],ancs[i],ancs[i-1],ancs[i-2]) - local ops_to_do,corners={} + local ops = {} for b in all(blocks_before_trash) do add(blocks, b) end if #ops_before_trash>0 then - ops_to_do=ops_before_trash + ops=ops_before_trash else local ops_after_trash,blocks_after_trash=self:_calc_push(ancs[i-2],ancs[i-1],ancs[i],ancs[i+1]) - ops_to_do=ops_after_trash + ops=ops_after_trash for b in all(blocks_after_trash) do add(blocks,b) end end - local ops=ops_to_do - if #ops>0 then if (hypothetically) return ancs,i-1,ops,blocks - - for o in all(ops) do level:tug_crate(unpack(o)) end + foreach(ops, level:tug_crate) return true end end @@ -1604,13 +1603,14 @@ function rope:_tug(hypothetically) end if not invalid_move then + local mv = {mx0,my0,dmx,dmy} if level:can_move(false,mx0,my0,dmx,dmy,1,0) then - if (hypothetically) return ancs,0,{{mx0,my0,dmx,dmy}},blocks + if (hypothetically) return ancs,0,{mv},blocks - level:tug_crate(mx0,my0,dmx,dmy) + level:tug_crate(mv) return true else - add(blocks, {mx0,my0,dmx,dmy}) + add(blocks, mv) end end end -- 2.34.1 From d792831370447ce0263aee90b96487787619a7bd Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 2 Jan 2023 15:57:27 -0800 Subject: [PATCH 2/7] can_move also takes a rope operation table this is approximately token-neutral but performance-saving. each function parameter makes its call cost worse. When can_move is called inside a loop, we already have a table and we unpack to call can_move; moving the unpack into can_move saves us marshalling cost. It requires us to construct a table in a different spot (where we were not previously doing so) but that spot is not in a loop. --- chameleonic.p8 | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index ec2a908..03c6dec 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -633,10 +633,12 @@ end ropecheck=split"-0.6,0.4,0.4" +-- argument "o" is a rope operation: +-- array of [mx0,my0,dmx,dmy] function level:can_move( - is_player, - mx0,my0,dmx,dmy,exclude_src,exclude_dst + is_player,o,exclude_src,exclude_dst ) + local mx0,my0,dmx,dmy=unpack(o) local mx1,my1=mx0+dmx,my0+dmy if (is_player and self:win_at(mx1,my1)) return true if (is_player and self:get_open_pit(mx1,my1)) return wrongbleep:adequately_warned() @@ -652,7 +654,8 @@ function level:can_move( return true end --- argument is array-like: mx0,my0,dmx,dmy +-- argument is a rope operation: +-- array of [mx0,my0,dmx,dmy] function level:tug_crate(t) local mx0,my0,dmx,dmy=unpack(t) local mxy0=_mix{mx0,my0} @@ -736,7 +739,7 @@ function player:update() else local x,y=self.x,self.y local function try_move(dx,dy,f) - if level:can_move(true,x,y,dx,dy,0,2) then + if level:can_move(true,{x,y,dx,dy},0,2) then self.todo=f self.cooldown=3 local t=f[#f] @@ -1604,7 +1607,7 @@ function rope:_tug(hypothetically) if not invalid_move then local mv = {mx0,my0,dmx,dmy} - if level:can_move(false,mx0,my0,dmx,dmy,1,0) then + if level:can_move(false,mv,1,0) then if (hypothetically) return ancs,0,{mv},blocks level:tug_crate(mv) @@ -1693,7 +1696,7 @@ function rope:_calc_push( else local crate=level:get_crate(mx,my) if crate then - if not level:can_move(false,mx,my,dmx,dmy,0,0) then + if not level:can_move(false,o,0,0) then add(blocked,o) break end -- 2.34.1 From 78f0a96529c8a8f361849253a6dd26fc1944945c Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 2 Jan 2023 15:58:23 -0800 Subject: [PATCH 3/7] calc_push op loop golf Reorganizing conditionals saves tokens here. --- chameleonic.p8 | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index 03c6dec..993b06b 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -1690,17 +1690,11 @@ function rope:_calc_push( local ops2,blocked={},{} for o in all(ops) do - local mx,my,dmx,dmy=unpack(o) - if not level:mcoll(mx,my) then - -- great! - else - local crate=level:get_crate(mx,my) - if crate then - if not level:can_move(false,o,0,0) then - add(blocked,o) - break - end - else + local mx,my=unpack(o) + if level:mcoll(mx,my) + if not (level:get_crate(mx, my)) break + if not level:can_move(false,o,0,0) then + add(blocked,o) break end add(ops2,o) -- 2.34.1 From cb36faac23a68cadc478f0cb196e5a2928450e24 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 2 Jan 2023 16:04:42 -0800 Subject: [PATCH 4/7] Fix syntax errors. This also saves a few tokens and cycles by turning level:tug_crate into a free function. It's not _pretty_ but it's the least bad option. --- chameleonic.p8 | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index 993b06b..7d9380a 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -655,14 +655,16 @@ function level:can_move( end -- argument is a rope operation: --- array of [mx0,my0,dmx,dmy] -function level:tug_crate(t) +-- array of [mx0,my0,dmx,dmy] +-- must be a free function +-- to use as a foreach target +function level_tug_crate(t) local mx0,my0,dmx,dmy=unpack(t) local mxy0=_mix{mx0,my0} local existing=self._crates[mxy0] if (not existing) return - self._crates[mxy0]=nil + level._crates[mxy0]=nil local mx1,my1=mx0+dmx,my0+dmy local px1,py1=mx1*8,my1*8 existing.todo={ @@ -673,7 +675,7 @@ function level:tug_crate(t) {px=px1,py=py1} } - self._crates[_mix{mx1,my1}]=existing + level._crates[_mix{mx1,my1}]=existing end -->8 @@ -1557,7 +1559,7 @@ function rope:_tug(hypothetically) if #ops>0 then if (hypothetically) return ancs,i-1,ops,blocks - foreach(ops, level:tug_crate) + foreach(ops, level_tug_crate) return true end end @@ -1610,7 +1612,7 @@ function rope:_tug(hypothetically) if level:can_move(false,mv,1,0) then if (hypothetically) return ancs,0,{mv},blocks - level:tug_crate(mv) + level_tug_crate(mv) return true else add(blocks, mv) @@ -1691,8 +1693,8 @@ function rope:_calc_push( local ops2,blocked={},{} for o in all(ops) do local mx,my=unpack(o) - if level:mcoll(mx,my) - if not (level:get_crate(mx, my)) break + if level:mcoll(mx,my) then + if (not level:get_crate(mx, my)) break if not level:can_move(false,o,0,0) then add(blocked,o) break -- 2.34.1 From b2dbd9ec6087c4624538e87be8cf8d358efe1717 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 2 Jan 2023 16:22:10 -0800 Subject: [PATCH 5/7] _calc_push golf redux Removed unnecessary variable declarations and conditional cases by using an "assume, alternate, verify assumption" pattern and reusing ax0/ay0 when they would never be referenced again. --- chameleonic.p8 | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index 7d9380a..7f64592 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -1645,21 +1645,16 @@ function rope:_calc_push( smy=-smy end - local mx,dmx + local dmx=1 -- maybe push right? if anch.adx==-1 and a0.x>an.x+7 then -- push left - mx=ax0-1 - dmx=-1 - elseif anch.adx==1 and a0.x=an.x-7 then return {} end for my=my0,my1,smy do - add(ops,{mx,my,dmx,0}) + add(ops,{ax0,my,dmx,0}) end end @@ -1671,22 +1666,16 @@ function rope:_calc_push( smx=-smx end - local my,dmy + local dmy=1 -- maybe push down? if anch.ady==-1 and a0.y>an.y+6 then -- push up - my=ay0-1 - dmy=-1 - - elseif anch.ady==1 and a0.y=an.y-6 then return {} end for mx=mx0,mx1,smx do - add(ops,{mx,my,0,dmy}) + add(ops,{mx,ay0,0,dmy}) end end -- 2.34.1 From 49284d44a8426b022b85fd850846218faa16b77b Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 2 Jan 2023 16:25:18 -0800 Subject: [PATCH 6/7] Fix syntax errors. --- chameleonic.p8 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index 7f64592..084084c 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -659,12 +659,13 @@ end -- must be a free function -- to use as a foreach target function level_tug_crate(t) + local self=level local mx0,my0,dmx,dmy=unpack(t) local mxy0=_mix{mx0,my0} local existing=self._crates[mxy0] if (not existing) return - level._crates[mxy0]=nil + self._crates[mxy0]=nil local mx1,my1=mx0+dmx,my0+dmy local px1,py1=mx1*8,my1*8 existing.todo={ @@ -675,7 +676,7 @@ function level_tug_crate(t) {px=px1,py=py1} } - level._crates[_mix{mx1,my1}]=existing + self._crates[_mix{mx1,my1}]=existing end -->8 @@ -1669,7 +1670,7 @@ function rope:_calc_push( local dmy=1 -- maybe push down? if anch.ady==-1 and a0.y>an.y+6 then -- push up - ay0,dmy=ay0-1,dmy=-1 + ay0,dmy=ay0-1,-1 elseif anch.ady!=1 or a0.y>=an.y-6 then return {} end -- 2.34.1 From a177660344c65f7578c4edc1edf92597da248aec Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 2 Jan 2023 16:26:30 -0800 Subject: [PATCH 7/7] Slightly more efficient fix to level_tug_crate. --- chameleonic.p8 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index 084084c..228d081 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -659,8 +659,7 @@ end -- must be a free function -- to use as a foreach target function level_tug_crate(t) - local self=level - local mx0,my0,dmx,dmy=unpack(t) + local self,mx0,my0,dmx,dmy=level,unpack(t) local mxy0=_mix{mx0,my0} local existing=self._crates[mxy0] if (not existing) return -- 2.34.1