From 723c0f791cd96eb7f12bc2ab3943cacec0d6deaa Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Fri, 20 Jun 2025 17:48:02 -0700 Subject: [PATCH] Refactor collider to collaborate with linked_list. The only use of a collider is intertwined with the ship list, so I can combine the "prepare to yoink" and "populate collider" and "iterate list" steps, and I can combine the "hide" and "yoink" steps. This doesn't save tokens now but it's about to, when I use the eship collider to test pship collision. --- vacuum_gambit.p8 | 58 +++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/vacuum_gambit.p8 b/vacuum_gambit.p8 index e0c345b..f52b41c 100644 --- a/vacuum_gambit.p8 +++ b/vacuum_gambit.p8 @@ -259,28 +259,11 @@ function updategame() -- many bullets and many enemy ships; -- use bucket collider for efficiency + local eship_collider = collider.new{from=eships} - local eship_collider, pp = collider.new(), eships - eships:strip(function(s) - eship_collider:insert(s) - s.prev = pp - pp = s -end) - - -- lose tokens, save time: - -- unroll `strip` to avoid - -- function call overhead pbullets:strip(function(pb) for es in all(eship_collider:get_collisions(pb)) do - if es:hitbullet(pb) then - eship_collider:hide(es) - es.prev.next=es.next - if es.next then - es.next.prev = es.prev - else - eships.tail = es.prev - end - end + if (es:hitbullet(pb)) eship_collider:yoink(es) if (pb:hitship(es)) return true end end) @@ -1446,6 +1429,21 @@ end collider = mknew{ init = function(x) x.suppress = {} + local p, n = x.from, x.from.next + while n do + -- insert + for i in all(collider_indexes(hurtbox(n))) do + local a = x[i] + if not a then + a = {} + x[i] = a + end + add(a, n) + end + -- prepare yoink + n.prev = p + p = n + end end, } @@ -1459,21 +1457,15 @@ function collider_indexes(box) return ret end -function collider:insert(item) - -- todo: separate "big items" list? - local bdx = collider_indexes(hurtbox(item)) - for i in all(bdx) do - local x = self[i] - if not x then - x = {} - self[i] = x - end - add(x, item) - end -end - -function collider:hide(item) +function collider:yoink(item) self.suppress[item]=true + local p,n = item.prev,item.next + p.next = n + if n then + n.prev = p + else + self.from.tail = p + end end function collider:get_collisions(item)