14 SDL_Surface *load_image(char *filename);
16 struct rock rocks[MAXROCKS], *free_rocks;
18 struct rock **rock_buckets[2];
20 // we have two sets of buckets -- this variable tells which we are using.
25 SDL_Surface *surf_rock[NROCKS];
26 struct shape rock_shapes[NROCKS];
28 // timers for rock generation.
33 float nrocks_inc_ticks = 2*60*20/(F_ROCKS-I_ROCKS);
35 // constants for rock generation.
36 #define KH (32*20) // 32 s for a speed=1 rock to cross the screen horizontally.
37 #define KV (24*20) // 24 s for a speed=1 rock to cross the screen vertically.
38 #define RDX 2.5 // range for rock dx values (+/-)
39 #define RDY 2.5 // range for rock dy values (+/-)
41 static inline struct rock **
42 bucket(int x, int y, int p)
44 int b = (x+grid_size)/grid_size + bw*((y+grid_size)/grid_size);
45 return &rock_buckets[p][b];
51 int scr_grid_w = (XSIZE+2*grid_size-1) / grid_size;
52 int scr_grid_h = (YSIZE+2*grid_size-1) / grid_size;
53 bw = 1 + scr_grid_w + 1;
54 bh = 1 + scr_grid_h + 1;
57 rock_buckets[0] = malloc(n_buckets * sizeof(struct rock *));
58 rock_buckets[1] = malloc(n_buckets * sizeof(struct rock *));
59 if(!rock_buckets[0] || !rock_buckets[1]) {
60 fprintf(stderr, "Can't allocate rock buckets.\n");
67 transfer_rock(struct rock *r, struct rock **from, struct rock **to)
69 *from = &r->next->rock;
70 r->next = SPRITE(*to);
79 for(i=0; i<MAXROCKS; i++) rocks[i].image = NULL;
80 rocks[0].next = NULL; free_rocks = &rocks[MAXROCKS-1];
81 for(i = 1; i<MAXROCKS; i++) rocks[i].next = SPRITE(&rocks[i-1]);
82 for(i = 0; i<n_buckets; i++) {
83 rock_buckets[0][i] = NULL;
84 rock_buckets[1][i] = NULL;
91 #define ROCK_LEN sizeof("sprites/rockXX.png")
100 for(i = 0; i<NROCKS; i++) {
101 snprintf(a, ROCK_LEN, "sprites/rock%02d.png", i);
102 NULLERROR(surf_rock[i] = load_image(a));
103 get_shape(surf_rock[i], &rock_shapes[i]);
104 maxw = max(maxw, rock_shapes[i].w);
105 maxh = max(maxh, rock_shapes[i].h);
107 grid_size = max(maxw, maxh) * 3 / 2;
113 enum { LEFT, RIGHT, TOP, BOTTOM };
116 // compute the number of rocks/tick that should be coming from each side,
117 // and the speed ranges of rocks coming from each side
119 rock_sides(float *ti, float *speed_min, float *speed_max)
121 float dx0,dx1, dy0,dy1;
122 float hfactor, vfactor;
125 for(i=0; i<4; i++) ti[i] = 0;
126 for(i=0; i<4; i++) speed_min[i] = 0;
127 for(i=0; i<4; i++) speed_max[i] = 0;
128 hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
130 dx0 = -RDX - screendx; dx1 = RDX - screendx;
131 dy0 = -RDY - screendy; dy1 = RDY - screendy;
134 speed_max[RIGHT] = -dx0;
136 // Rocks moving left only. So the RIGHT side of the screen
137 speed_min[RIGHT] = -dx1;
138 ti[RIGHT] = -(dx0+dx1)/2;
140 // Rocks moving left and right
141 speed_max[LEFT] = dx1;
146 // Rocks moving right only. So the LEFT side of the screen
147 speed_min[LEFT] = dx0;
148 speed_max[LEFT] = dx1;
149 ti[LEFT] = (dx0+dx1)/2;
152 ti[RIGHT] *= hfactor;
155 speed_max[BOTTOM] = -dy0;
157 // Rocks moving up only. So the BOTTOM of the screen
158 speed_min[BOTTOM] = -dy1;
159 ti[BOTTOM] = -(dy0+dy1)/2;
161 // Rocks moving up and down
162 speed_max[TOP] = dy1;
167 // Rocks moving down only. so the TOP of the screen
168 speed_min[TOP] = dy0;
169 speed_max[TOP] = dy1;
170 ti[TOP] = (dy0+dy1)/2;
173 ti[BOTTOM] *= vfactor;
177 weighted_rnd_range(float min, float max) {
178 return sqrt(min * min + frnd() * (max * max - min * min));
190 if(nrocks < F_ROCKS) {
191 nrocks_timer += t_frame;
192 if(nrocks_timer >= nrocks_inc_ticks) {
193 nrocks_timer -= nrocks_inc_ticks;
198 rock_sides(ti, rmin, rmax);
201 for(i=0; i<4; i++) rtimers[i] += ti[i]*t_frame;
205 while(rtimers[i] >= 1) {
207 if(!free_rocks) return; // sorry, we ran out of rocks!
209 r->type = urnd() % NROCKS;
210 r->image = surf_rock[r->type];
211 r->shape = &rock_shapes[r->type];
215 r->y = frnd()*(YSIZE + r->image->h);
217 r->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
222 r->y = frnd()*(YSIZE + r->image->h);
224 r->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
228 r->x = frnd()*(XSIZE + r->image->w);
232 r->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
235 r->x = frnd()*(XSIZE + r->image->w);
239 r->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
242 transfer_rock(r, &free_rocks, bucket(r->x, r->y, p));
254 // Move all the rocks
255 for(b=0; b<n_buckets; b++) {
256 head=&rock_buckets[p][b]; r=*head;
261 r->x += (r->dx - screendx)*t_frame;
262 r->y += (r->dy - screendy)*t_frame;
264 // clip it, or sort it into the other bucket set
265 // (either way we move it out of this list).
266 if(r->x + r->image->w < 0 || r->x >= XSIZE
267 || r->y + r->image->h < 0 || r->y >= YSIZE) {
268 transfer_rock(r, head, &free_rocks);
270 } else transfer_rock(r, head, bucket(r->x, r->y, 1-p));
273 p = 1-p; // switch current set of buckets.
282 for(i=0; i<MAXROCKS; i++) {
283 if(!rocks[i].image) continue;
284 dest.x = rocks[i].x; dest.y = rocks[i].y;
285 SDL_BlitSurface(rocks[i].image,NULL,surf_screen,&dest);
290 hit_in_bucket(struct rock *r, float x, float y, struct shape *shape)
292 for(; r; r=&r->next->rock) {
293 if(collide(x - r->x, y - r->y, r->shape, shape)) return 1;
299 hit_rocks(float x, float y, struct shape *shape)
303 struct rock **bucket;
305 ix = x + grid_size; iy = y + grid_size;
306 l = ix / grid_size; r = (ix+shape->w)/grid_size;
307 t = iy / grid_size; b = (iy+shape->h)/grid_size;
308 bucket = &rock_buckets[p][l + t*bw];
310 if(hit_in_bucket(*bucket, x, y, shape)) return true;
311 if(l > 0 && hit_in_bucket(*(bucket-1), x, y, shape)) return true;
312 if(t > 0 && hit_in_bucket(*(bucket-bw), x, y, shape)) return true;
313 if(l > 0 && t > 0 && hit_in_bucket(*(bucket-1-bw), x, y, shape)) return true;
316 if(hit_in_bucket(*(bucket+1), x, y, shape)) return true;
317 if(t > 0 && hit_in_bucket(*(bucket+1-bw), x, y, shape)) return true;
320 if(hit_in_bucket(*(bucket+bw), x, y, shape)) return true;
321 if(l > 0 && hit_in_bucket(*(bucket-1+bw), x, y, shape)) return true;
323 if(r > l && b > t && hit_in_bucket(*(bucket+1+bw), x, y, shape)) return true;
328 pixel_hit_in_bucket(struct rock *r, float x, float y)
330 for(; r; r=&r->next->rock) {
331 if(x < r->x || y < r->y) continue;
332 if(pixel_collide(x - r->x, y - r->y, r->shape)) return 1;
338 pixel_hit_rocks(float x, float y)
342 struct rock **bucket;
344 ix = x + grid_size; iy = y + grid_size;
345 l = ix / grid_size; t = iy / grid_size;
346 bucket = &rock_buckets[p][l + t*bw];
347 if(pixel_hit_in_bucket(*bucket, x, y)) return true;
348 if(l > 0 && pixel_hit_in_bucket(*(bucket-1), x, y)) return true;
349 if(t > 0 && pixel_hit_in_bucket(*(bucket-bw), x, y)) return true;
350 if(l > 0 && t > 0 && pixel_hit_in_bucket(*(bucket-1-bw), x, y)) return true;
355 blast_rocks(float x, float y, float radius, int onlyslow)
363 for(b=0; b<n_buckets; b++) {
364 for(r=rock_buckets[p][b]; r; r=&r->next->rock) {
365 if(r->x <= 0) continue;
367 // This makes it so your explosion from dying magically doesn't leave
368 // any rocks that aren't moving much on the x axis. If onlyslow is set,
369 // only rocks that are barely moving will be pushed.
370 if(onlyslow && (r->dx - screendx < -4 || r->dx - screendx > 3)) continue;
375 n = sqrt(dx*dx + dy*dy);