pick up rope fixes (#16)

Fix more known rope bugs

Merge branch 'main' into intro_sequence

Merge branch 'intro_sequence' into level-29

Co-authored-by: Nyeogmi <economicsbat@gmail.com>
Reviewed-on: #16
This commit is contained in:
Kistaro Windrider 2022-12-30 00:35:03 +00:00
parent 5c7f48baf5
commit 06bdaa6179

View File

@ -7,6 +7,7 @@ real_modules={}
frame=0 frame=0
function _init() function _init()
-- printh("restarting")
_doall("init") end _doall("init") end
function _update() function _update()
frame+=1 frame+=1
@ -398,7 +399,10 @@ function level:reanchor()
end end
shellsort(self._anch_keys) shellsort(self._anch_keys)
for point in self:anchor_points() do for point in self:anchor_points() do
if (point.ax_old and player.rope) player.rope:be_pushed_by(point,point.ax_old,point.ay_old) if point.ax_old and player.rope and (point.ax_old != point.ax or point.ay_old != point.ay) then
-- printh("moving: "..tostring({point.ax_old,point.ay_old}).."=>"..tostring({point.ax,point.ay}))
player.rope:be_pushed_by(point,point.ax_old,point.ay_old)
end
point.ax_old,point.ay_old=nil,nil point.ax_old,point.ay_old=nil,nil
end end
@ -967,8 +971,9 @@ function rope:draw(artificial_px,artificial_py)
if (n1.associated_with.ady>0) y-=1 if (n1.associated_with.ady>0) y-=1
end end
rectfill(x-1,y-1,x+1,y+1,12) rectfill(x-1,y-1,x+1,y+1,12)
print("ax="..n1.ax..",ay="..n1.ay,0,sy) print("ax="..n1.ax..",ay="..n1.ay,72,sy)
sy+=7 print(tostring(n1.associated_with and (not n1.associated_with.dropped and n1.associated_with.ax==n1.ax and n1.associated_with.ay==n1.ay)),76,sy+7)
sy+=14
local n0=n1.prev local n0=n1.prev
local n2=n1.next local n2=n1.next
@ -1002,7 +1007,7 @@ function rope:draw(artificial_px,artificial_py)
pset(x+p.adx,y,11) pset(x+p.adx,y,11)
pset(x,y+p.ady,11) pset(x,y+p.ady,11)
end end
]]-- ]]
end end
function rope:drag_dst(x,y) function rope:drag_dst(x,y)
@ -1021,6 +1026,21 @@ function rope:drag(n1,ax_new,ay_new)
end end
function rope:relax() function rope:relax()
local n0=self.src
while true do
if (n0==nil) break
if n0.associated_with and n0.associated_with.dropped then
for i in level:anchor_points() do
if i.ax==n0.ax and i.ay==n0.ay then
n0.associated_with=i
break
end
end
end
n0=n0.next
end
local n0=self.src local n0=self.src
while true do while true do
local n1=n0.next local n1=n0.next
@ -1056,8 +1076,9 @@ function rope:relax()
local would,x1_new,y1_new=would_stick(x0,y0,n1.associated_with,x2,y2) local would,x1_new,y1_new=would_stick(x0,y0,n1.associated_with,x2,y2)
if not would and not (n1.ax==x1_new and n1.ay==y1_new) then if not would and not (n1.ax==x1_new and n1.ay==y1_new) then
-- printh("dragging: "..tostring{n1.associated_with, {x1_new, y1_new}})
-- printh("relaxing: "..tostring(n0.associated_with).."->"..tostring(n1.associated_with).."->"..tostring(n2.associated_with)) -- printh("relaxing: "..tostring(n0.associated_with).."->"..tostring(n1.associated_with).."->"..tostring(n2.associated_with))
self:_drag(n1,x1_new,y1_new) self:_drag(n1,x1_new,y1_new,n1.ax,n1.ay)
n0=n1.prev n0=n1.prev
n2=n1.next n2=n1.next
n0.next=n2 n0.next=n2
@ -1143,13 +1164,8 @@ function would_stick(x0,y0,anchor,x2,y2)
local dx=x2-x0 local dx=x2-x0
local dy=y2-y0 local dy=y2-y0
-- there is no reason for an acute angle to stick around in this world if (x1==x0 and y1==y0) return false
--[[ if (x1==x2 and y1==y2) return false
local ang0=atan2(x2-x1,y2-y1)
local ang2=atan2(x0-x1,y0-y1)
local diff=abs((ang0-ang2 + 0.5)%1-0.5)
if (diff<0.25) return false,x0,y0,0,0
]]--
local adx,ady local adx,ady
local x1_new,y1_new local x1_new,y1_new
@ -1186,25 +1202,28 @@ function rope:be_pushed_by(anchor,ax_old,ay_old)
local nx0,ny0=n0.ax,n0.ay local nx0,ny0=n0.ax,n0.ay
local nx1,ny1=n1.ax,n1.ay local nx1,ny1=n1.ax,n1.ay
local nxmn,nxmx = _mnmx(nx0,nx1)
local nymn,nymx = _mnmx(ny0,ny1)
-- printh(tostring({anchor,nxmn,nxmx,nymn,nymx}))
if if
(ax_new==ax_old and nx0<=anchor.ax and anchor.ax<=nx1) and (ax_new!=ax_old or (nxmn<=anchor.ax and anchor.ax<=nxmx)) and
(ay_new==ay_old and ny0<=anchor.ay and anchor.ay<=ny1) and (ay_new!=ay_old or (nymn<=anchor.ay and anchor.ay<=nymx)) and
not (anchor.ax==nx0 and anchor.ay==ny0) and
not (anchor.ax==nx1 and anchor.ay==ny1) and
(_which_side(ax_old,ay_old,nx0,ny0,nx1,ny1)!= (_which_side(ax_old,ay_old,nx0,ny0,nx1,ny1)!=
_which_side(ax_new,ay_new,nx0,ny0,nx1,ny1) _which_side(ax_new,ay_new,nx0,ny0,nx1,ny1)
) and would_stick(nx0,ny0,anchor,nx1,ny1) ) and would_stick(nx0,ny0,anchor,nx1,ny1)
then then
-- printh("found (in): "..tostring({{nx0,ny0},{nx1,ny1}, anchor}))
local nx05,ny05 local nx05,ny05
if ax_new==ax_old then if ax_new==ax_old then
nx05=anchor.ax nx05=anchor.ax
ny05=ny0+(nx05-nx0)/(nx1-nx0) * (ny1-ny0) ny05=ny0+(nx05-nx0)/(nx1-nx0) * (ny1-ny0)
--printh("found (x): "..tostring({nx05,ny05})) -- printh("found (x): "..tostring({nx05,ny05}))
elseif ay_new==ay_old then elseif ay_new==ay_old then
ny05=anchor.ay ny05=anchor.ay
nx05=nx0+(ny05-ny0)/(ny1-ny0) * (nx1-nx0) nx05=nx0+(ny05-ny0)/(ny1-ny0) * (nx1-nx0)
--printh("found (y): "..tostring({nx05,ny05})) -- printh("found (y): "..tostring({nx05,ny05}))
else else
assert(false,"wtf?") assert(false,"wtf?")
end end
@ -1222,8 +1241,14 @@ function rope:be_pushed_by(anchor,ax_old,ay_old)
end end
end end
function rope:_drag(n1,ax1_new,ay1_new) function rope:_drag(n1,ax1_new,ay1_new,ax_removing,ay_removing)
local function _sweep_radar(ax_pivot,ay_pivot,ax_far0,ay_far0,ax_far1,ay_far1) local function _sweep_radar(ax_lhs,ay_lhs,ax_rhs,ay_rhs,ax_pivot,ay_pivot,ax_far0,ay_far0,ax_far1,ay_far1)
local function _uncreatable(anchor)
return (anchor.ax==ax_lhs and anchor.ay==ay_lhs) or
(anchor.ax==ax_rhs and anchor.ay==ay_rhs) or
(anchor.ax==ax_removing and anchor.ay==ay_removing)
end
if (ax_far0==ax_far1 and ay_far0==ay_far1) return nil if (ax_far0==ax_far1 and ay_far0==ay_far1) return nil
if ax_far0==ax_far1 then if ax_far0==ax_far1 then
@ -1234,9 +1259,7 @@ function rope:_drag(n1,ax1_new,ay1_new)
for ay_far_new in _stepfrom(ay_far0,ay_far1) do for ay_far_new in _stepfrom(ay_far0,ay_far1) do
for anchor in level:anchor_points() do for anchor in level:anchor_points() do
if if
not (anchor.ax==ax_pivot and anchor.ay==ay_pivot) and not _uncreatable(anchor) and
-- not (anchor.ax==ax_far0 and anchor.ay==ay_far0) and
not (anchor.ax==ax_far1 and anchor.ay==ay_far1) and
(ax0<=anchor.ax and anchor.ax<=ax1) and (ax0<=anchor.ax and anchor.ax<=ax1) and
would_stick(ax_pivot,ay_pivot,anchor,ax_far,ay_far_new) and would_stick(ax_pivot,ay_pivot,anchor,ax_far,ay_far_new) and
( (
@ -1257,11 +1280,9 @@ function rope:_drag(n1,ax1_new,ay1_new)
for ax_far_new in _stepfrom(ax_far0,ax_far1) do for ax_far_new in _stepfrom(ax_far0,ax_far1) do
for anchor in level:anchor_points() do for anchor in level:anchor_points() do
if if
not (anchor.ax==ax_pivot and anchor.ay==ay_pivot) and not _uncreatable(anchor) and
-- not (anchor.ax==ax_far0 and anchor.ay==ay_far0) and
not (anchor.ax==ax_far1 and anchor.ay==ay_far1) and
would_stick(ax_pivot,ay_pivot,anchor,ax_far_new,ay_far) and
(ay0<=anchor.ay and anchor.ay<=ay1) and (ay0<=anchor.ay and anchor.ay<=ay1) and
would_stick(ax_pivot,ay_pivot,anchor,ax_far_new,ay_far) and
( (
_which_side(anchor.ax,anchor.ay,ax_pivot,ay_pivot,ax_far_old,ay_far) != _which_side(anchor.ax,anchor.ay,ax_pivot,ay_pivot,ax_far_old,ay_far) !=
_which_side(anchor.ax,anchor.ay,ax_pivot,ay_pivot,ax_far_new,ay_far) _which_side(anchor.ax,anchor.ay,ax_pivot,ay_pivot,ax_far_new,ay_far)
@ -1282,7 +1303,10 @@ function rope:_drag(n1,ax1_new,ay1_new)
local n0=n1.prev local n0=n1.prev
while true do while true do
if (n0==nil) break if (n0==nil) break
local anch=_sweep_radar(n0.ax,n0.ay,ax1_old,ay1_old,ax1_new,ay1_new) local anch=_sweep_radar(
n0.ax,n0.ay,n1.ax,n1.ay,
n0.ax,n0.ay,ax1_old,ay1_old,ax1_new,ay1_new
)
if (anch==nil) break if (anch==nil) break
local n05={ax=anch.ax,ay=anch.ay,associated_with=anch,prev=n0,next=n1} local n05={ax=anch.ax,ay=anch.ay,associated_with=anch,prev=n0,next=n1}
-- printh("creating pre: "..tostring(n0.associated_with).."->"..tostring(n05.associated_with).."->"..tostring(n1.associated_with)) -- printh("creating pre: "..tostring(n0.associated_with).."->"..tostring(n05.associated_with).."->"..tostring(n1.associated_with))
@ -1294,7 +1318,10 @@ function rope:_drag(n1,ax1_new,ay1_new)
local n2=n1.next local n2=n1.next
while true do while true do
if (n2==nil) break if (n2==nil) break
local anch=_sweep_radar(n2.ax,n2.ay,ax1_old,ay1_old,ax1_new,ay1_new) local anch=_sweep_radar(
n1.ax,n1.ay,n2.ax,n2.ay,
n2.ax,n2.ay,ax1_old,ay1_old,ax1_new,ay1_new
)
if (anch==nil) break if (anch==nil) break
local n15={ax=anch.ax,ay=anch.ay,associated_with=anch,prev=n1,next=n2} local n15={ax=anch.ax,ay=anch.ay,associated_with=anch,prev=n1,next=n2}
-- printh("creating post: "..tostring(n1.associated_with).."->"..tostring(n15.associated_with).."->"..tostring(n2.associated_with)) -- printh("creating post: "..tostring(n1.associated_with).."->"..tostring(n15.associated_with).."->"..tostring(n2.associated_with))