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.
This commit is contained in:
Kistaro Windrider 2023-01-02 15:35:10 -08:00
parent 0aeeb1975b
commit b1cc74fe3b
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -652,7 +652,9 @@ function level:can_move(
return true return true
end 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 mxy0=_mix{mx0,my0}
local existing=self._crates[mxy0] local existing=self._crates[mxy0]
if (not existing) return if (not existing) return
@ -1540,22 +1542,19 @@ function rope:_tug(hypothetically)
local blocks = {} local blocks = {}
for i=#ancs-1,2,-1 do 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_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 for b in all(blocks_before_trash) do add(blocks, b) end
if #ops_before_trash>0 then if #ops_before_trash>0 then
ops_to_do=ops_before_trash ops=ops_before_trash
else else
local ops_after_trash,blocks_after_trash=self:_calc_push(ancs[i-2],ancs[i-1],ancs[i],ancs[i+1]) 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 for b in all(blocks_after_trash) do add(blocks,b) end
end end
local ops=ops_to_do
if #ops>0 then if #ops>0 then
if (hypothetically) return ancs,i-1,ops,blocks if (hypothetically) return ancs,i-1,ops,blocks
foreach(ops, level:tug_crate)
for o in all(ops) do level:tug_crate(unpack(o)) end
return true return true
end end
end end
@ -1604,13 +1603,14 @@ function rope:_tug(hypothetically)
end end
if not invalid_move then 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,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 return true
else else
add(blocks, {mx0,my0,dmx,dmy}) add(blocks, mv)
end end
end end
end end