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