From 71b150cb33658cfcb8e57bad0001fa6e220f065d Mon Sep 17 00:00:00 2001 From: Nyeogmi Date: Sat, 31 Dec 2022 16:18:24 -0800 Subject: [PATCH] Golf the sweep code --- chameleonic.p8 | 73 +++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 45 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index ae900a5..ee6e2ac 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -1053,8 +1053,6 @@ function rope:relax() local n1=n0.next if (not n1) break local n2=n1.next - - if n0.ax==n1.ax and n0.ay==n1.ay then n0.next=n2 if (n2) n2.prev=n0 @@ -1160,8 +1158,6 @@ function would_stick(anchor,x0,y0,x1,y1,x2,y2) x1,y1=x1 or anchor.ax,y1 or anchor.ay local dx,dy=x2-x0,y2-y0 - if (x1==x0 and y1==y0) return - if (x1==x2 and y1==y2) return local function switch_ends() dx,dy,x0,y0,x2,y2=-dx,-dy,x2,y2,x0,y0 @@ -1259,57 +1255,44 @@ function rope:_be_pushed_by1(ax_old,ay_old,ax_new,ay_new,anch) end function rope:_drag(n1,ax1_new,ay1_new,ax_removing,ay_removing) - 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 _sweep_radar(ax_lhs,ay_lhs,ax_rhs,ay_rhs,ax_pivot,ay_pivot,ax_src,ay_src,ax_dst,ay_dst) + if (ax_src==ax_dst and ay_src==ay_dst) return + 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) + 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 - - if ax_far0==ax_far1 then - local ax_far=ax_far0 - local ax0,ax1=_mnmx(ax_pivot,ax_far) - - ay_far_old=ay_far0 - for ay_far_new in _stepfrom(ay_far0,ay_far1) do + local function _sweep(extent_old,extent_new,cb_in_bounds,cb_would_stick,cb_side) + for extent_new in _stepfrom(extent_old, extent_new) do for anchor in level:anchor_points() do - if - not _uncreatable(anchor) and - (ax0<=anchor.ax and anchor.ax<=ax1) and - would_stick(anchor,ax_pivot,ay_pivot,nil,nil,ax_far,ay_far_new) and - ( - _which_side(anchor.ax,anchor.ay,ax_pivot,ay_pivot,ax_far,ay_far_old) != - _which_side(anchor.ax,anchor.ay,ax_pivot,ay_pivot,ax_far,ay_far_new) - ) + if not _uncreatable(anchor) and cb_in_bounds(anchor) and + cb_would_stick(anchor,extent_new) and + cb_side(anchor,extent_old)!=cb_side(anchor,extent_new) then return anchor end end - ay_far_old=ay_far_new + extent_old=extent_new end - elseif ay_far0==ay_far1 then - local ay_far=ay_far0 - local ay0,ay1=_mnmx(ay_pivot,ay_far) + end - ax_far_old=ax_far0 - for ax_far_new in _stepfrom(ax_far0,ax_far1) do - for anchor in level:anchor_points() do - if - not _uncreatable(anchor) and - (ay0<=anchor.ay and anchor.ay<=ay1) and - would_stick(anchor,ax_pivot,ay_pivot,nil,nil,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_new,ay_far) - ) - then - return anchor - end - end - ax_far_old=ax_far_new - end + if ax_src==ax_dst then + return _sweep( + ay_src,ay_dst, + function(anchor) return mid(ax_pivot,anchor.ax,ax_dst)==anchor.ax end, + function(anchor,ay) return would_stick(anchor,ax_pivot,ay_pivot,nil,nil,ax_dst,ay) end, + function(anchor,ay) return _which_side(anchor.ax,anchor.ay,ax_pivot,ay_pivot,ax_dst,ay) end + ) + else + assert(ay_src==ay_dst) + return _sweep( + ax_src,ax_dst, + function(anchor) return mid(ay_pivot,anchor.ay,ay_dst)==anchor.ay end, + function(anchor,ax) return would_stick(anchor,ax_pivot,ay_pivot,nil,nil,ax,ay_dst) end, + function(anchor,ax) return _which_side(anchor.ax,anchor.ay,ax_pivot,ay_pivot,ax,ay_dst) end + ) end end