From 4a53992c3a3d9c00de9bf2ee56043dfa64d040e0 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Sat, 17 Dec 2022 17:43:45 -0800 Subject: [PATCH] Implement levels:anchor_in as a generator. This gets rid of the (substantial!) overhead of preparing a list and then iterating over it, in exchange for a bit of bookkeeping to simulate the prior behavior of the for loop, which is much cheaper. --- chameleonic.p8 | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index 87dfc40..993918b 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -270,14 +270,17 @@ function level:anchor_points() end function level:anchors_in(px0,py0,px1,py1) - ancs={} - for ax=px0\4,(px1+3)\4 do - for ay=py0\4,(py1+3)\4 do - local anc=self._anch[_amix(ax,ay)] - if (anc!=nil) add(ancs, anc) + local anch,ax,xlim,sy,ylim=self._anch,px0\4,(px1+3)\4,py0\4,(py1+3)\4 + local ay=sy-1 + return function() + while true do + ay+=1 + if(ay>ylim)ay,ax=sy,ax+1 + if(ax>xlim)return nil + local anc=anch[_amix(ax,ay)] + if (anc) return anc end end - return ancs end function level:get_open_pit(mx,my) @@ -955,7 +958,7 @@ function rope:_find_needed_anchors(i) local anchors_bydist={} local x0,x2=_mnmx(a0.x,a2.x) local y0,y2=_mnmx(a0.y,a2.y) - for a1 in all(level:anchors_in(x0-1,y0-1,x2+1,y2+1)) do + for a1 in level:anchors_in(x0-1,y0-1,x2+1,y2+1) do add(anchors_bydist,{el=a1,key=_linedist(a0,a1,a2)}) end shellsort(anchors_bydist)