From b1cc74fe3be94837c5eff77aecf105a2c5e6785f Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 2 Jan 2023 15:35:10 -0800 Subject: [PATCH] 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