Assorted token golf in rope logic #25

Merged
pyrex merged 7 commits from kistaro/chameleonic:rope_golf into main 2023-01-03 01:06:57 +00:00
Showing only changes of commit b1cc74fe3b - Show all commits

View File

@ -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