13 struct rock rocks[MAXROCKS], *free_rocks;
15 struct rock **rock_buckets[2];
17 // we have two sets of buckets -- this variable tells which we are using.
22 static struct rock prototypes[NROCKS];
24 // timers for rock generation.
25 static float rtimers[4];
29 float nrocks_inc_ticks = 2*60*20/(F_ROCKS-I_ROCKS);
31 // constants for rock generation.
32 #define KH (32*20) // 32 s for a speed=1 rock to cross the screen horizontally.
33 #define KV (24*20) // 24 s for a speed=1 rock to cross the screen vertically.
34 #define RDX 2.5 // range for rock dx values (+/-)
35 #define RDY 2.5 // range for rock dy values (+/-)
37 static inline struct rock **
38 bucket(int x, int y, int p)
40 int b = (x+grid_size)/grid_size + bw*((y+grid_size)/grid_size);
41 return &rock_buckets[p][b];
47 int scr_grid_w = (XSIZE+2*grid_size-1) / grid_size;
48 int scr_grid_h = (YSIZE+2*grid_size-1) / grid_size;
49 bw = 1 + scr_grid_w + 1;
50 bh = 1 + scr_grid_h + 1;
53 rock_buckets[0] = malloc(n_buckets * sizeof(struct rock *));
54 rock_buckets[1] = malloc(n_buckets * sizeof(struct rock *));
55 if(!rock_buckets[0] || !rock_buckets[1]) {
56 fprintf(stderr, "Can't allocate rock buckets.\n");
63 transfer_rock(struct rock *r, struct rock **from, struct rock **to)
75 for(i=0; i<MAXROCKS; i++) rocks[i].image = NULL;
78 for(i=1; i<MAXROCKS; i++) rocks[i].next = &rocks[i-1];
79 free_rocks = &rocks[MAXROCKS-1];
81 for(i = 0; i<n_buckets; i++) {
82 rock_buckets[0][i] = NULL;
83 rock_buckets[1][i] = NULL;
90 #define ROCK_LEN sizeof("sprites/rockXX.png")
99 for(i = 0; i<NROCKS; i++) {
100 snprintf(a, ROCK_LEN, "sprites/rock%02d.png", i);
101 load_sprite(SPRITE(&prototypes[i]), a);
102 maxw = max(maxw, prototypes[i].w);
103 maxh = max(maxh, prototypes[i].h);
105 grid_size = max(maxw, maxh) * 3 / 2;
111 enum { LEFT, RIGHT, TOP, BOTTOM };
114 // compute the number of rocks/tick that should be coming from each side,
115 // and the speed ranges of rocks coming from each side
117 rock_sides(float *ti, float *speed_min, float *speed_max)
119 float dx0,dx1, dy0,dy1;
120 float hfactor, vfactor;
123 for(i=0; i<4; i++) ti[i] = 0;
124 for(i=0; i<4; i++) speed_min[i] = 0;
125 for(i=0; i<4; i++) speed_max[i] = 0;
126 hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
128 dx0 = -RDX - screendx; dx1 = RDX - screendx;
129 dy0 = -RDY - screendy; dy1 = RDY - screendy;
132 speed_max[RIGHT] = -dx0;
134 // Rocks moving left only. So the RIGHT side of the screen
135 speed_min[RIGHT] = -dx1;
136 ti[RIGHT] = -(dx0+dx1)/2;
138 // Rocks moving left and right
139 speed_max[LEFT] = dx1;
144 // Rocks moving right only. So the LEFT side of the screen
145 speed_min[LEFT] = dx0;
146 speed_max[LEFT] = dx1;
147 ti[LEFT] = (dx0+dx1)/2;
150 ti[RIGHT] *= hfactor;
153 speed_max[BOTTOM] = -dy0;
155 // Rocks moving up only. So the BOTTOM of the screen
156 speed_min[BOTTOM] = -dy1;
157 ti[BOTTOM] = -(dy0+dy1)/2;
159 // Rocks moving up and down
160 speed_max[TOP] = dy1;
165 // Rocks moving down only. so the TOP of the screen
166 speed_min[TOP] = dy0;
167 speed_max[TOP] = dy1;
168 ti[TOP] = (dy0+dy1)/2;
171 ti[BOTTOM] *= vfactor;
175 weighted_rnd_range(float min, float max) {
176 return sqrt(min * min + frnd() * (max * max - min * min));
183 struct rock *r, **tmp;
188 if(nrocks < F_ROCKS) {
189 nrocks_timer += t_frame;
190 if(nrocks_timer >= nrocks_inc_ticks) {
191 nrocks_timer -= nrocks_inc_ticks;
196 rock_sides(ti, rmin, rmax);
199 for(i=0; i<4; i++) rtimers[i] += ti[i]*t_frame;
203 while(rtimers[i] >= 1) {
205 if(!free_rocks) return; // sorry, we ran out of rocks!
206 r = free_rocks; free_rocks = r->next;
207 type = urnd() % NROCKS;
208 *r = prototypes[type];
213 r->y = frnd()*(YSIZE + r->image->h);
215 r->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
220 r->y = frnd()*(YSIZE + r->image->h);
222 r->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
226 r->x = frnd()*(XSIZE + r->image->w);
230 r->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
233 r->x = frnd()*(XSIZE + r->image->w);
237 r->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
240 tmp = bucket(r->x, r->y, p);
241 r->next = *tmp; *tmp = r;
253 // Move all the rocks
254 for(b=0; b<n_buckets; b++) {
255 head=&rock_buckets[p][b]; r=*head;
260 r->x += (r->dx - screendx)*t_frame;
261 r->y += (r->dy - screendy)*t_frame;
263 // clip it, or sort it into the other bucket set
264 // (either way we move it out of this list).
265 if(r->x + r->image->w < 0 || r->x >= XSIZE
266 || r->y + r->image->h < 0 || r->y >= YSIZE) {
267 transfer_rock(r, head, &free_rocks);
269 } else transfer_rock(r, head, bucket(r->x, r->y, 1-p));
272 p = 1-p; // switch current set of buckets.
281 for(i=0; i<MAXROCKS; i++) {
282 if(!rocks[i].image) continue;
283 dest.x = rocks[i].x; dest.y = rocks[i].y;
284 SDL_BlitSurface(rocks[i].image,NULL,surf_screen,&dest);
289 hit_in_bucket(struct rock *r, Sprite *s)
291 for(; r; r=r->next) {
292 if(collide(SPRITE(r), s)) return true;
301 struct rock **bucket;
303 l = (s->x + grid_size) / grid_size;
304 r = (s->x + s->w + grid_size) / grid_size;
305 t = (s->y + grid_size) / grid_size;
306 b = (s->y + s->h + grid_size) / grid_size;
307 bucket = &rock_buckets[p][l + t*bw];
309 if(hit_in_bucket(*bucket, s)) return true;
310 if(l > 0 && hit_in_bucket(*(bucket-1), s)) return true;
311 if(t > 0 && hit_in_bucket(*(bucket-bw), s)) return true;
312 if(l > 0 && t > 0 && hit_in_bucket(*(bucket-1-bw), s)) return true;
315 if(hit_in_bucket(*(bucket+1), s)) return true;
316 if(t > 0 && hit_in_bucket(*(bucket+1-bw), s)) return true;
319 if(hit_in_bucket(*(bucket+bw), s)) return true;
320 if(l > 0 && hit_in_bucket(*(bucket-1+bw), s)) return true;
322 if(r > l && b > t && hit_in_bucket(*(bucket+1+bw), s)) return true;
327 pixel_hit_in_bucket(struct rock *r, float x, float y)
329 for(; r; r=r->next) {
330 if(pixel_collide(SPRITE(r), x, y)) return 1;
336 pixel_hit_rocks(float x, float y)
340 struct rock **bucket;
342 ix = x + grid_size; iy = y + grid_size;
343 l = ix / grid_size; t = iy / grid_size;
344 bucket = &rock_buckets[p][l + t*bw];
345 if(pixel_hit_in_bucket(*bucket, x, y)) return true;
346 if(l > 0 && pixel_hit_in_bucket(*(bucket-1), x, y)) return true;
347 if(t > 0 && pixel_hit_in_bucket(*(bucket-bw), x, y)) return true;
348 if(l > 0 && t > 0 && pixel_hit_in_bucket(*(bucket-1-bw), x, y)) return true;
353 blast_rocks(float x, float y, float radius, int onlyslow)
361 for(b=0; b<n_buckets; b++) {
362 for(r=rock_buckets[p][b]; r; r=r->next) {
363 if(r->x <= 0) continue;
365 // This makes it so your explosion from dying magically doesn't leave
366 // any rocks that aren't moving much on the x axis. If onlyslow is set,
367 // only rocks that are barely moving will be pushed.
368 if(onlyslow && (r->dx - screendx < -4 || r->dx - screendx > 3)) continue;
373 n = sqrt(dx*dx + dy*dy);