+ else return mask_collide(xov, yov, a, b);
+}
+
+void
+collide_with_list(Sprite *s, Sprite *list)
+{
+ for(; list; list=list->next)
+ if(collide(s, list)) do_collision(s, list);
+}
+
+void
+collisions(void)
+{
+ int i, end = gw*gh;
+ Sprite *s;
+ for(i=0; i<end; i++) {
+ for(s=sprites[set][i]; s; s=s->next) {
+ collide_with_list(s, s->next);
+ if(i+1 < end) collide_with_list(s, sprites[set][i+1]);
+ if(i+gw < end) collide_with_list(s, sprites[set][i+gw]);
+ if(i+gw+1 < end) collide_with_list(s, sprites[set][i+gw+1]);
+ }
+ }
+}
+
+Sprite *
+hit_in_square(Sprite *r, Sprite *s)
+{
+ for(; r; r=r->next)
+ if(collide(r, s)) break;
+ return r;
+}
+
+Sprite *
+collides(Sprite *s)
+{
+ int l, r, t, b;
+ Sprite **sq;
+ Sprite *c;
+
+ l = (s->x + grid_size) / grid_size;
+ r = (s->x + s->w + grid_size) / grid_size;
+ t = (s->y + grid_size) / grid_size;
+ b = (s->y + s->h + grid_size) / grid_size;
+ sq = &sprites[set][l + t*gw];
+
+ if((c = hit_in_square(*sq, s))) return c;
+ if(l > 0 && (c = hit_in_square(*(sq-1), s))) return c;
+ if(t > 0 && (c = hit_in_square(*(sq-gw), s))) return c;
+ if(l > 0 && t > 0 && (c = hit_in_square(*(sq-1-gw), s))) return c;
+
+ if(r > l) {
+ if((c = hit_in_square(*(sq+1), s))) return c;
+ if(t > 0 && hit_in_square(*(sq+1-gw), s)) return c;
+ }
+ if(b > t) {
+ if((c = hit_in_square(*(sq+gw), s))) return c;
+ if(l > 0 && (c = hit_in_square(*(sq-1+gw), s))) return c;
+ }
+ if(r > l && b > t && (c = hit_in_square(*(sq+1+gw), s))) return c;
+ return NULL;