13 struct rock rocks[MAXROCKS];
16 Sprite **rock_buckets[2];
18 // we have two sets of buckets -- this variable tells which we are using.
23 static struct rock prototypes[NROCKS];
25 // timers for rock generation.
26 static float rtimers[4];
30 float nrocks_inc_ticks = 2*60*20/(F_ROCKS-I_ROCKS);
32 // constants for rock generation.
33 #define KH (32*20) // 32 s for a speed=1 rock to cross the screen horizontally.
34 #define KV (24*20) // 24 s for a speed=1 rock to cross the screen vertically.
35 #define RDX 2.5 // range for rock dx values (+/-)
36 #define RDY 2.5 // range for rock dy values (+/-)
38 static inline Sprite **
39 bucket(int x, int y, int p)
41 int b = (x+grid_size)/grid_size + bw*((y+grid_size)/grid_size);
42 return &rock_buckets[p][b];
48 int scr_grid_w = (XSIZE+2*grid_size-1) / grid_size;
49 int scr_grid_h = (YSIZE+2*grid_size-1) / grid_size;
50 bw = 1 + scr_grid_w + 1;
51 bh = 1 + scr_grid_h + 1;
54 rock_buckets[0] = malloc(n_buckets * sizeof(struct rock *));
55 rock_buckets[1] = malloc(n_buckets * sizeof(struct rock *));
56 if(!rock_buckets[0] || !rock_buckets[1]) {
57 fprintf(stderr, "Can't allocate rock buckets.\n");
64 insert_sprite(Sprite **head, Sprite *this)
70 static inline Sprite *
71 remove_sprite(Sprite **head)
83 for(i=0; i<MAXROCKS; i++) rocks[i].image = NULL;
86 for(i=1; i<MAXROCKS; i++) rocks[i].next = &rocks[i-1];
87 free_rocks = SPRITE(&rocks[MAXROCKS-1]);
89 for(i = 0; i<n_buckets; i++) {
90 rock_buckets[0][i] = NULL;
91 rock_buckets[1][i] = NULL;
98 #define ROCK_LEN sizeof("sprites/rockXX.png")
107 for(i = 0; i<NROCKS; i++) {
108 snprintf(a, ROCK_LEN, "sprites/rock%02d.png", i);
109 load_sprite(SPRITE(&prototypes[i]), a);
110 prototypes[i].type = ROCK_SPRITE;
111 maxw = max(maxw, prototypes[i].w);
112 maxh = max(maxh, prototypes[i].h);
114 grid_size = max(maxw, maxh) * 3 / 2;
120 enum { LEFT, RIGHT, TOP, BOTTOM };
123 // compute the number of rocks/tick that should be coming from each side,
124 // and the speed ranges of rocks coming from each side
126 rock_sides(float *ti, float *speed_min, float *speed_max)
128 float dx0,dx1, dy0,dy1;
129 float hfactor, vfactor;
132 for(i=0; i<4; i++) ti[i] = 0;
133 for(i=0; i<4; i++) speed_min[i] = 0;
134 for(i=0; i<4; i++) speed_max[i] = 0;
135 hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
137 dx0 = -RDX - screendx; dx1 = RDX - screendx;
138 dy0 = -RDY - screendy; dy1 = RDY - screendy;
141 speed_max[RIGHT] = -dx0;
143 // Rocks moving left only. So the RIGHT side of the screen
144 speed_min[RIGHT] = -dx1;
145 ti[RIGHT] = -(dx0+dx1)/2;
147 // Rocks moving left and right
148 speed_max[LEFT] = dx1;
153 // Rocks moving right only. So the LEFT side of the screen
154 speed_min[LEFT] = dx0;
155 speed_max[LEFT] = dx1;
156 ti[LEFT] = (dx0+dx1)/2;
159 ti[RIGHT] *= hfactor;
162 speed_max[BOTTOM] = -dy0;
164 // Rocks moving up only. So the BOTTOM of the screen
165 speed_min[BOTTOM] = -dy1;
166 ti[BOTTOM] = -(dy0+dy1)/2;
168 // Rocks moving up and down
169 speed_max[TOP] = dy1;
174 // Rocks moving down only. so the TOP of the screen
175 speed_min[TOP] = dy0;
176 speed_max[TOP] = dy1;
177 ti[TOP] = (dy0+dy1)/2;
180 ti[BOTTOM] *= vfactor;
184 weighted_rnd_range(float min, float max) {
185 return sqrt(min * min + frnd() * (max * max - min * min));
197 if(nrocks < F_ROCKS) {
198 nrocks_timer += t_frame;
199 if(nrocks_timer >= nrocks_inc_ticks) {
200 nrocks_timer -= nrocks_inc_ticks;
205 rock_sides(ti, rmin, rmax);
208 for(i=0; i<4; i++) rtimers[i] += ti[i]*t_frame;
212 while(rtimers[i] >= 1) {
214 if(!free_rocks) return; // sorry, we ran out of rocks!
215 r = (struct rock *) remove_sprite(&free_rocks);
216 type = urnd() % NROCKS;
217 *r = prototypes[type];
222 r->y = frnd()*(YSIZE + r->image->h);
224 r->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
229 r->y = frnd()*(YSIZE + r->image->h);
231 r->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
235 r->x = frnd()*(XSIZE + r->image->w);
239 r->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
242 r->x = frnd()*(XSIZE + r->image->w);
246 r->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
249 insert_sprite(bucket(r->x, r->y, p), SPRITE(r));
260 // Move all the rocks
261 for(b=0; b<n_buckets; b++) {
262 head=&rock_buckets[p][b]; r=*head;
267 r->x += (r->dx - screendx)*t_frame;
268 r->y += (r->dy - screendy)*t_frame;
270 // clip it, or sort it into the other bucket set
271 // (either way we move it out of this list).
272 if(r->x + r->image->w < 0 || r->x >= XSIZE
273 || r->y + r->image->h < 0 || r->y >= YSIZE) {
274 insert_sprite(&free_rocks, remove_sprite(head));
276 } else insert_sprite(bucket(r->x, r->y, 1-p), remove_sprite(head));
279 p = 1-p; // switch current set of buckets.
288 for(i=0; i<MAXROCKS; i++) {
289 if(!rocks[i].image) continue;
290 dest.x = rocks[i].x; dest.y = rocks[i].y;
291 SDL_BlitSurface(rocks[i].image,NULL,surf_screen,&dest);
296 hit_in_bucket(Sprite *r, Sprite *s)
298 for(; r; r=r->next) {
299 if(collide(r, s)) return true;
310 l = (s->x + grid_size) / grid_size;
311 r = (s->x + s->w + grid_size) / grid_size;
312 t = (s->y + grid_size) / grid_size;
313 b = (s->y + s->h + grid_size) / grid_size;
314 bucket = &rock_buckets[p][l + t*bw];
316 if(hit_in_bucket(*bucket, s)) return true;
317 if(l > 0 && hit_in_bucket(*(bucket-1), s)) return true;
318 if(t > 0 && hit_in_bucket(*(bucket-bw), s)) return true;
319 if(l > 0 && t > 0 && hit_in_bucket(*(bucket-1-bw), s)) return true;
322 if(hit_in_bucket(*(bucket+1), s)) return true;
323 if(t > 0 && hit_in_bucket(*(bucket+1-bw), s)) return true;
326 if(hit_in_bucket(*(bucket+bw), s)) return true;
327 if(l > 0 && hit_in_bucket(*(bucket-1+bw), s)) return true;
329 if(r > l && b > t && hit_in_bucket(*(bucket+1+bw), s)) return true;
334 pixel_hit_in_bucket(Sprite *r, float x, float y)
336 for(; r; r=r->next) {
337 if(pixel_collide(r, x, y)) return 1;
343 pixel_hit_rocks(float x, float y)
349 ix = x + grid_size; iy = y + grid_size;
350 l = ix / grid_size; t = iy / grid_size;
351 bucket = &rock_buckets[p][l + t*bw];
352 if(pixel_hit_in_bucket(*bucket, x, y)) return true;
353 if(l > 0 && pixel_hit_in_bucket(*(bucket-1), x, y)) return true;
354 if(t > 0 && pixel_hit_in_bucket(*(bucket-bw), x, y)) return true;
355 if(l > 0 && t > 0 && pixel_hit_in_bucket(*(bucket-1-bw), x, y)) return true;
360 blast_rocks(float x, float y, float radius, int onlyslow)
368 for(b=0; b<n_buckets; b++) {
369 for(r=rock_buckets[p][b]; r; r=r->next) {
370 if(r->x <= 0) continue;
372 // This makes it so your explosion from dying magically doesn't leave
373 // any rocks that aren't moving much on the x axis. If onlyslow is set,
374 // only rocks that are barely moving will be pushed.
375 if(onlyslow && (r->dx - screendx < -4 || r->dx - screendx > 3)) continue;
380 n = sqrt(dx*dx + dy*dy);