From 325d7444e7bd9c16b713507a68e8a4d70b465d8a Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Fri, 20 Jun 2025 15:09:18 -0700 Subject: [PATCH] invert eship/pbullet collision Also mildly trims up linked_list impl. --- vacuum_gambit.p8 | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/vacuum_gambit.p8 b/vacuum_gambit.p8 index 18357e2..5bdae5d 100644 --- a/vacuum_gambit.p8 +++ b/vacuum_gambit.p8 @@ -60,7 +60,7 @@ function mknew(tt) end -- intrusive singly-linked list. --- cannot be nested! +-- cannot be nested or crossed! linked_list = mknew{ is_linked_list=true, init = function(x) @@ -93,9 +93,7 @@ end -- strip calls f(x) for each -- node, removing each node for --- which f(x) returns true. it --- returns the new tail; nil --- if the list is now empty. +-- which f(x) returns true. function linked_list:strip(f) local p, n = self, self.next while n do @@ -107,7 +105,6 @@ function linked_list:strip(f) n = n.next end self.tail = p - return p end -- optimized special case - @@ -258,31 +255,35 @@ function updategame() -- many bullets and many enemy ships; -- use bucket collider for efficiency - local pbullet_collider = collider.new() - local p, n = pbullets, pbullets.next + + local eship_collider = collider.new() + -- save tokens, lose time: + -- eships:strip(function(s) eship_collider:insert(s) end) + local p, n = eships, eships.next while n do n.prev = p - pbullet_collider:insert(n) + eship_collider:insert(n) p = n n = p.next end - eships:strip( - function(es) - for pb in all(pbullet_collider:get_collisions(es)) do - if pb:hitship(es) then - pbullet_collider:hide(pb) - pb.prev.next = pb.next - if pb.next then - pb.next.prev = pb.prev - else - pbullets.tail = pb.prev - 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 - if (es:hitbullet(pb)) return true end + if (pb:hitship(es)) return true end - ) + end) intangibles_fg:strip(call_move)