Compare commits

...

3 Commits

Author SHA1 Message Date
d2e1e9274c Fix misc other rope bugs 2022-12-18 16:07:15 -08:00
97e580e7db Sorta working 2022-12-18 15:18:07 -08:00
581e345e31 More rope refactors 2022-12-18 14:53:58 -08:00

View File

@ -309,6 +309,7 @@ function level:reanchor(remove)
end end
end end
end end
if (player.rope!=nil) player.rope:make_dirty()
end end
function level:win_at(mx,my) function level:win_at(mx,my)
@ -759,6 +760,7 @@ function rope:new(
ancs={}, ancs={},
dst={x=x,y=y,todo={}}, dst={x=x,y=y,todo={}},
state={name="cast",dx=dx,dy=dy}, state={name="cast",dx=dx,dy=dy},
dirty=true,
latch=nil, latch=nil,
latch_frame=0, latch_frame=0,
} }
@ -772,12 +774,6 @@ end
function rope:done() function rope:done()
return self.state.name=="done" return self.state.name=="done"
--[[
return self.latch_frame>=2 and (
self.latch==nil or
self.under_destruction
)
]]
end end
function rope:busy() function rope:busy()
@ -788,6 +784,14 @@ function rope:busy()
end end
function rope:update() function rope:update()
local was_busy=self:busy()
for i=0,#self.ancs+1 do
local anc=self:_anc(i)
_apply(anc,anc.todo,i)
end
local is_busy=self:busy()
if (was_busy and not is_busy) self.dirty=true
if self.state.name=="cast" then if self.state.name=="cast" then
self:continue_cast() self:continue_cast()
elseif self.state.name=="latched" then elseif self.state.name=="latched" then
@ -796,26 +800,6 @@ function rope:update()
self.latch_frame=10 self.latch_frame=10
end end
self:_make_consistent()
elseif self.state.name=="destroy" then -- destroy
self.state.frame+=1
if (self.state.frame>=5) self.state={name="done"}
else --
assert(false, "todo")
end
end
function rope:destroy()
if (self.state.name=="destroy" or self.state.name=="done") return
self.state={name="destroy",frame=0}
end
function rope:_make_consistent()
for i=0,#self.ancs+1 do
local anc=self:_anc(i)
_apply(anc,anc.todo,i)
end
if (self.latch==nil) self:destroy() return if (self.latch==nil) self:destroy() return
if if
@ -830,20 +814,24 @@ function rope:_make_consistent()
if #self.latch.rec.todo==0 then if #self.latch.rec.todo==0 then
if self.latch.rec.dead==true then if self.latch.rec.dead==true then
self:destroy() self:destroy()
return
end
for i=0,#self.ancs do
local a0=self:_anc(i)
local a1=self:_anc(i+1)
if not self:_can_stretch(a0, a1) then
self:destroy()
return
end end
end end
end end
if (not is_busy) self:_tidy_up_gen()
elseif self.state.name=="destroy" then -- destroy
self.state.frame+=1
if (self.state.frame>=5) self.state={name="done"}
else
-- done state
end end
end end
function rope:destroy()
if (self.state.name=="destroy" or self.state.name=="done") return
self.state={name="destroy",frame=0}
end
function rope:continue_cast() function rope:continue_cast()
local dx,dy=self.state.dx,self.state.dy local dx,dy=self.state.dx,self.state.dy
local x0=self.src.x local x0=self.src.x
@ -875,7 +863,7 @@ function rope:_reindex()
end end
function rope:draw() function rope:draw()
local points=self:_anchors_simplified() local points,highlight=self:_tug(true)
if (self.state.name=="done") return if (self.state.name=="done") return
local perc_to_show=1.0 local perc_to_show=1.0
if (self.state.name=="destroy") perc_to_show=(1.0-self.state.frame/5)^2 if (self.state.name=="destroy") perc_to_show=(1.0-self.state.frame/5)^2
@ -902,19 +890,28 @@ function rope:draw()
local coef=min(len_here/dist_base,1.0) local coef=min(len_here/dist_base,1.0)
dx,dy=dx*coef,dy*coef dx,dy=dx*coef,dy*coef
linefill(x,y,x+0.25*dx,y+0.25*dy,1.0,8) local color=8
linefill(x+0.25*dx,y+0.25*dy,x+1*dx,y+1*dy,0.5,8) if (highlight==i) color=14
linefill(x+0.9*dx,y+0.9*dy,x+dx,y+dy,1.0,8)
circfill(x+dx+0.5,y+dy+0.5,1.0,8) linefill(x,y,x+0.25*dx,y+0.25*dy,1.0,color)
linefill(x+0.25*dx,y+0.25*dy,x+1*dx,y+1*dy,0.5,color)
linefill(x+0.9*dx,y+0.9*dy,x+dx,y+dy,1.0,color)
circfill(x+dx+0.5,y+dy+0.5,1.0,color)
end end
end end
for i,p in ipairs(self.ancs) do for i=0,#self.ancs+1 do
rectfill(p.x-1,p.y-1,p.x+1,p.y+1,12) p=self:_anc(i)
print(p.id..":"..p.x..","..p.y..","..#p.todo,0,-8+i*8,9) local c=12
if (p.dirty) c=13
rectfill(p.x-1,p.y-1,p.x+1,p.y+1,c)
print(tostr(p.id)..":"..p.x..","..p.y..","..#p.todo,0,-8+i*8,9)
end end
for _,p in pairs(level._anch) do for _,p in pairs(level._anch) do
pset(p.x,p.y,11) pset(p.x,p.y,11)
end end
print("dirty:"..tostr(self.dirty),32,0,9)
print("busy:"..tostr(self:busy()),32,7,9)
print("state:"..tostr(self.state.name),32,14,9)
if self.all_ops!=nil then if self.all_ops!=nil then
for i,o in ipairs(self.all_ops) do for i,o in ipairs(self.all_ops) do
rect(o.mx*8,o.my*8,o.mx*8+7,o.my*8+7,4) rect(o.mx*8,o.my*8,o.mx*8+7,o.my*8+7,4)
@ -957,7 +954,26 @@ function rope:drag(
end end
end end
function rope:make_dirty()
for a=0,#self.ancs+1 do
self:_anc(a).dirty=true
end
self.dirty=true
end
function rope:_tidy_up_gen() function rope:_tidy_up_gen()
local invalid=false
for i=0,#self.ancs do
local a0=self:_anc(i)
local a1=self:_anc(i+1)
if not self:_can_stretch(a0,a1) then
invalid=true
break
end
end
if (invalid) self:make_dirty()
local busy=self:busy()
if (not self:latched()) return if (not self:latched()) return
if (not self.dirty) return if (not self.dirty) return
@ -969,7 +985,7 @@ function rope:_tidy_up_gen()
local anc=self:_anc(a) local anc=self:_anc(a)
if anc.dirty then if anc.dirty then
anc.seen=true anc.seen=true
if self[f](self,a) then if self[f](self,a,busy) then
settled=false anc.changed=true settled=false anc.changed=true
end end
end end
@ -1012,11 +1028,22 @@ function rope:_tidy_up_gen()
for a=0,#self.ancs+1,1 do for a=0,#self.ancs+1,1 do
local anc=self:_anc(a) local anc=self:_anc(a)
if (anc.seen) anc.dirty=anc.changed if (anc.seen) anc.dirty=anc.changed
if (anc.dirty) settled=false
end end
if (settled) break if (settled) break
end end
if (self:busy()) return
for i=0,#self.ancs do
local a0=self:_anc(i)
local a1=self:_anc(i+1)
if not self:_can_stretch(a0,a1) then
self:destroy()
end
end
self.dirty=false self.dirty=false
end end
@ -1080,7 +1107,7 @@ function rope:_find_touched_anchors(i)
return false return false
end end
function rope:_elide_point(i) function rope:_elide_point(i,busy)
if (i<=0) return false if (i<=0) return false
if (#self.ancs<i) return false if (#self.ancs<i) return false
@ -1089,10 +1116,10 @@ function rope:_elide_point(i)
local a2=self:_anc(i+1) local a2=self:_anc(i+1)
local level_anc=level:point_anchor(a1.x,a1.y) local level_anc=level:point_anchor(a1.x,a1.y)
if _point_eq(a0,a1) or _point_eq(a1,a2) or level_anc==nil then if _point_eq(a0,a1) or _point_eq(a1,a2) or (not busy and level_anc==nil) then
-- do it unconditionally -- do it unconditionally
else else
if _linedist(a0,a1,a2) < 0.1 then if _linedist(a0,a1,a2) < 0.01 then
return false return false
end end
@ -1111,6 +1138,8 @@ function rope:_elide_point(i)
end end
deli(self.ancs,i) deli(self.ancs,i)
a0.dirty=true a0.changed=true
a2.dirty=true a2.changed=true
return true return true
end end
@ -1318,13 +1347,13 @@ function rope:tug_orientxy()
end end
function rope:tug() function rope:tug()
self:_make_consistent() self:_tidy_up_gen()
if (not self:latched()) return if (not self:latched()) return
self:_tug() self:_tug()
self:_make_consistent() self:_tidy_up_gen()
end end
function rope:_tug() function rope:_tug(hypothetically)
local ancs=self:_anchors_simplified() local ancs=self:_anchors_simplified()
local touched={} local touched={}
@ -1334,24 +1363,46 @@ function rope:_tug()
add(self.all_ops,o) add(self.all_ops,o)
end end
local can_do=true local find_all=function(lhs,rhs,di)
for o in all(ops) do local ops2do={}
for i=lhs,rhs,di do
local o=ops[i]
if not level:mcoll(o.mx,o.my) then if not level:mcoll(o.mx,o.my) then
-- great! -- great!
else else
local crate=level:get_crate(o.mx,o.my) local crate=level:get_crate(o.mx,o.my)
if crate==nil or touched[_mix(o.mx,o.my)] then if crate==nil or touched[_mix(o.mx,o.my)] then
can_do=false break
else else
if not level:can_move(false,o.mx,o.my,o.dmx,o.dmy,0,0) then if not level:can_move(false,o.mx,o.my,o.dmx,o.dmy,0,0) then
can_do=false break
end end
end end
add(ops2do,o)
end end
if (not can_do) break end
return ops2do
end end
if can_do and #ops>=1 then local ops_before_trash=find_all(1,#ops,1)
local ops_to_do={}
local corners={}
if #ops_before_trash>0 then
ops_to_do=ops_before_trash
corners[i-1]=true
else
ops_to_do=find_all(#ops,1,-1)
corners[i]=true
end
local do_all=#ops==#ops_to_do
ops=ops_to_do
if (do_all) corners[i-1]=true corners[i]=true
if #ops>0 then
if (hypothetically) return ancs,i-1
local dmx,dmy=ops[1].dmx,ops[1].dmy local dmx,dmy=ops[1].dmx,ops[1].dmy
for o in all(ops) do for o in all(ops) do
touched[_mix(o.mx,o.my)]=true touched[_mix(o.mx,o.my)]=true
@ -1369,16 +1420,19 @@ function rope:_tug()
if force or not level:pcoll(x,y) then if force or not level:pcoll(x,y) then
s.x=x s.x=x
s.y=y s.y=y
self.dirty=true
end end
s.dirty=true
self.dirty=true
return true return true
end} end}
end end
local dmxh,dmyh=dmx,dmy
if (not corners[node]) dmxh,dmyh=0,0
anc.todo={ anc.todo={
{}, {},
upd(x0+dmx*2,y0+dmy*2), upd(x0+dmxh*2,y0+dmyh*2),
upd(x0+dmx*7,y0+dmy*7), upd(x0+dmxh*7,y0+dmyh*7),
upd(x0+dmx*8,y0+dmy*8), upd(x0+dmxh*8,y0+dmyh*8),
} }
end end
for node=ancs[i-1].ix-1,ancs[i].ix+1 do for node=ancs[i-1].ix-1,ancs[i].ix+1 do
@ -1389,22 +1443,28 @@ function rope:_tug()
end end
local latch=self.latch local latch=self.latch
if (latch==nil) return if (latch==nil) return ancs,nil
if latch.el=="crate" then if latch.el=="crate" then
local dmx,dmy= local dmx,dmy=
sgn0(latch.dx), sgn0(latch.dx),
sgn0(latch.dy) sgn0(latch.dy)
local lanc=ancs[2] local obj_anc=ancs[1]
local pull_anc=ancs[2]
local pull_dx=pull_anc.x-obj_anc.x
local pull_dy=pull_anc.y-obj_anc.y
local mx0=latch.rec.mx local mx0=latch.rec.mx
local my0=latch.rec.my local my0=latch.rec.my
local mxa=(lanc.x+dmx)\8 local mxa=(pull_anc.x+dmx)\8
local mya=(lanc.y+dmy)\8 local mya=(pull_anc.y+dmy)\8
local too_far=false local too_far=false
if if
(dmx!=0 and sgn0(pull_dx)!=dmx) or
(dmy!=0 and sgn0(pull_dy)!=dmy) or
sgn0(mx0-mxa)!= sgn0(mx0-mxa)!=
sgn0(mx0+dmx-mxa) or sgn0(mx0+dmx-mxa) or
@ -1418,14 +1478,18 @@ function rope:_tug()
not touched[_mix(mx0,my0)] and not touched[_mix(mx0,my0)] and
level:can_move(false,mx0,my0,dmx,dmy,1,0) level:can_move(false,mx0,my0,dmx,dmy,1,0)
then then
if (hypothetically) return ancs,1
level:tug_crate( level:tug_crate(
mx0,my0, mx0,my0,
dmx,dmy dmx,dmy
) )
-- be busy for 4 ticks while the crate moves -- be busy for 4 ticks while the crate moves
self:_anc(0).todo={{},{},{},{}} self:_anc(0).todo={{},{},{},{},{}}
end end
end end
return ancs,nil
end end
function rope:_calc_push( function rope:_calc_push(
@ -1435,7 +1499,11 @@ function rope:_calc_push(
if a0.x==a1.x then if a0.x==a1.x then
local y0,y1=_mnmx(a0.y,a1.y) local y0,y1=_mnmx(a0.y,a1.y)
local my0,my1=(y0+1)\8,(y1-1)\8 local my0,my1,smy=(y0+1)\8,(y1-1)\8,1
if a0.y>a1.y then
my0,my1=my1,my0
smy=-smy
end
local mx,dmx local mx,dmx
if a0.x%8==0 and a0.x>an.x+7 then if a0.x%8==0 and a0.x>an.x+7 then
@ -1450,14 +1518,18 @@ function rope:_calc_push(
return {} return {}
end end
for my=my0,my1,1 do for my=my0,my1,smy do
add(ops,{mx=mx,my=my,dmx=dmx,dmy=0}) add(ops,{mx=mx,my=my,dmx=dmx,dmy=0})
end end
end end
if a0.y==a1.y then if a0.y==a1.y then
local x0,x1=_mnmx(a0.x,a1.x) local x0,x1=_mnmx(a0.x,a1.x)
local mx0,mx1=(x0+1)\8,(x1-1)\8 local mx0,mx1,smx=(x0+1)\8,(x1-1)\8,1
if a0.x>a1.x then
mx0,mx1=mx1,mx0
smx=-smx
end
local my,dmy local my,dmy
if a0.y%8==0 and a0.y>an.y+6 then if a0.y%8==0 and a0.y>an.y+6 then
@ -1465,7 +1537,7 @@ function rope:_calc_push(
my=(a0.y-1)\8 my=(a0.y-1)\8
dmy=-1 dmy=-1
elseif a0.y%8==7 and a0.y<an.y-3 then elseif a0.y%8==7 and a0.y<an.y-6 then
-- push down -- push down
my=(a0.y+1)\8 my=(a0.y+1)\8
dmy=1 dmy=1
@ -1473,7 +1545,7 @@ function rope:_calc_push(
return {} return {}
end end
for mx=mx0,mx1,1 do for mx=mx0,mx1,smx do
add(ops,{mx=mx,my=my,dmx=0,dmy=dmy}) add(ops,{mx=mx,my=my,dmx=0,dmy=dmy})
end end
end end
@ -1685,18 +1757,18 @@ __map__
0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c000000000044000c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c000000000044000c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c000000000000000c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c0000000000000000000000000000000c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0100000000001c000c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c00000000001c0000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c000000000000000c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c0000000000000000000000000000410000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c000000000000000c0c0c0c0c0c0c0c0c00000000000020210000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c000000000000000c0c0c0c0c0c0c0c0c00000000000020210000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c000000000000000000000c0c0c0c0c0c00000000000030310000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c000000000000000000000c0c0c0c0c0c00000000000030310000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c0c0c0c0c0c0c0c0000000c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c0c0c0c0c0c0c0c0000000c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c0c0c0c0c0c0c0c00000000000000120100000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c0000000000000c00000000000000120100000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 3d00000c4f00003f0c000c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c0000000000000000000c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 010000000000000c0c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c0c0c0c00000c0c0c0c0c0c0c0c0c0c0c00000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0c0c0c0c00410c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__ __sfx__
01280000050550c05511055180551d05500000000000000000055070550c0550f055130550f0550c0550705501055080550d055140551905500000000000000000055070550f0551305518055130550f0550c055 01280000050550c05511055180551d05500000000000000000055070550c0550f055130550f0550c0550705501055080550d055140551905500000000000000000055070550f0551305518055130550f0550c055
0128000000000000001f0001f055200551f0551d055180551b055000000000000000000000000000000000000000000000000001b0551d0551b05519055140551805500000000000000000000000000000000000 0128000000000000001f0001f055200551f0551d055180551b055000000000000000000000000000000000000000000000000001b0551d0551b05519055140551805500000000000000000000000000000000000