14 SDL_Surface *load_image(char *filename);
17 struct rock_struct *next;
24 struct rock_struct rocks[MAXROCKS], *free_rocks;
26 struct rock_struct **rock_buckets[2];
28 // we have two sets of buckets -- this variable tells which we are using.
33 SDL_Surface *surf_rock[NROCKS];
34 struct shape rock_shapes[NROCKS];
36 // timers for rock generation.
41 float nrocks_inc_ticks = 2*60*20/(F_ROCKS-I_ROCKS);
43 // constants for rock generation.
44 #define KH (32*20) // 32 s for a speed=1 rock to cross the screen horizontally.
45 #define KV (24*20) // 24 s for a speed=1 rock to cross the screen vertically.
46 #define RDX 2.5 // range for rock dx values (+/-)
47 #define RDY 2.5 // range for rock dy values (+/-)
49 static inline struct rock_struct **
50 bucket(int x, int y, int p)
52 int b = (1+x/grid_size) + bw*(1+y/grid_size);
53 return &rock_buckets[p][b];
59 int scr_grid_w = (XSIZE+2*grid_size-1) / grid_size;
60 int scr_grid_h = (YSIZE+2*grid_size-1) / grid_size;
61 bw = 1 + scr_grid_w + 1;
62 bh = 1 + scr_grid_h + 1;
65 rock_buckets[0] = malloc(n_buckets * sizeof(struct rock_struct *));
66 rock_buckets[1] = malloc(n_buckets * sizeof(struct rock_struct *));
67 if(!rock_buckets[0] || !rock_buckets[1]) {
68 fprintf(stderr, "Can't allocate rock buckets.\n");
75 transfer_rock(struct rock_struct *r, struct rock_struct **from, struct rock_struct **to)
87 for(i=0; i<MAXROCKS; i++) rocks[i].image = NULL;
88 rocks[0].next = NULL; free_rocks = &rocks[MAXROCKS-1];
89 for(i = 1; i<MAXROCKS; i++) rocks[i].next = &rocks[i-1];
90 for(i = 0; i<n_buckets; i++) {
91 rock_buckets[0][i] = NULL;
92 rock_buckets[1][i] = NULL;
99 #define ROCK_LEN sizeof("sprites/rockXX.png")
108 for(i = 0; i<NROCKS; i++) {
109 snprintf(a, ROCK_LEN, "sprites/rock%02d.png", i);
110 NULLERROR(surf_rock[i] = load_image(a));
111 get_shape(surf_rock[i], &rock_shapes[i]);
112 maxw = max(maxw, rock_shapes[i].w);
113 maxh = max(maxh, rock_shapes[i].h);
115 grid_size = max(maxw, maxh) * 3 / 2;
121 enum { LEFT, RIGHT, TOP, BOTTOM };
124 // compute the number of rocks/tick that should be coming from each side,
125 // and the speed ranges of rocks coming from each side
127 rock_sides(float *ti, float *speed_min, float *speed_max)
129 float dx0,dx1, dy0,dy1;
130 float hfactor, vfactor;
133 for(i=0; i<4; i++) ti[i] = 0;
134 for(i=0; i<4; i++) speed_min[i] = 0;
135 for(i=0; i<4; i++) speed_max[i] = 0;
136 hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
138 dx0 = -RDX - screendx; dx1 = RDX - screendx;
139 dy0 = -RDY - screendy; dy1 = RDY - screendy;
142 speed_max[RIGHT] = -dx0;
144 // Rocks moving left only. So the RIGHT side of the screen
145 speed_min[RIGHT] = -dx1;
146 ti[RIGHT] = -(dx0+dx1)/2;
148 // Rocks moving left and right
149 speed_max[LEFT] = dx1;
154 // Rocks moving right only. So the LEFT side of the screen
155 speed_min[LEFT] = dx0;
156 speed_max[LEFT] = dx1;
157 ti[LEFT] = (dx0+dx1)/2;
160 ti[RIGHT] *= hfactor;
163 speed_max[BOTTOM] = -dy0;
165 // Rocks moving up only. So the BOTTOM of the screen
166 speed_min[BOTTOM] = -dy1;
167 ti[BOTTOM] = -(dy0+dy1)/2;
169 // Rocks moving up and down
170 speed_max[TOP] = dy1;
175 // Rocks moving down only. so the TOP of the screen
176 speed_min[TOP] = dy0;
177 speed_max[TOP] = dy1;
178 ti[TOP] = (dy0+dy1)/2;
181 ti[BOTTOM] *= vfactor;
185 weighted_rnd_range(float min, float max) {
186 return sqrt(min * min + frnd() * (max * max - min * min));
193 struct rock_struct *r;
198 if(nrocks < F_ROCKS) {
199 nrocks_timer += t_frame;
200 if(nrocks_timer >= nrocks_inc_ticks) {
201 nrocks_timer -= nrocks_inc_ticks;
206 rock_sides(ti, rmin, rmax);
209 for(i=0; i<4; i++) rtimers[i] += ti[i]*t_frame;
213 while(rtimers[i] >= 1) {
215 if(!free_rocks) return; // sorry, we ran out of rocks!
217 r->type_number = urnd() % NROCKS;
218 r->image = surf_rock[r->type_number];
219 r->shape = &rock_shapes[r->type_number];
223 r->y = frnd()*(YSIZE + r->image->h);
225 r->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
230 r->y = frnd()*(YSIZE + r->image->h);
232 r->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
236 r->x = frnd()*(XSIZE + r->image->w);
240 r->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
243 r->x = frnd()*(XSIZE + r->image->w);
247 r->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
250 transfer_rock(r, &free_rocks, bucket(r->x, r->y, p));
259 struct rock_struct **head;
260 struct rock_struct *r;
262 // Move all the rocks
263 for(b=0; b<n_buckets; b++) {
264 head=&rock_buckets[p][b]; r=*head;
269 r->x += (r->dx - screendx)*t_frame;
270 r->y += (r->dy - screendy)*t_frame;
272 // clip or resort into other bucket set
273 // (either way we move it out of this list).
274 if(r->x + r->image->w < 0 || r->x >= XSIZE
275 || r->y + r->image->h < 0 || r->y >= YSIZE) {
276 transfer_rock(r, head, &free_rocks);
278 } else transfer_rock(r, head, bucket(r->x, r->y, 1-p));
281 p = 1-p; // switch current set of buckets.
290 for(i=0; i<MAXROCKS; i++) {
291 if(!rocks[i].image) continue;
292 dest.x = rocks[i].x; dest.y = rocks[i].y;
293 SDL_BlitSurface(rocks[i].image,NULL,surf_screen,&dest);
298 hit_in_bucket(struct rock_struct *r, float x, float y, struct shape *shape)
300 for(; r; r=r->next) {
301 if(collide(x - r->x, y - r->y, r->shape, shape)) return 1;
307 hit_rocks(float x, float y, struct shape *shape)
309 struct rock_struct **b = bucket(x, y, p);
310 int bdx = ((int)x+shape->w)/grid_size - (int)x/grid_size;
311 int bdy = ((int)y+shape->h)/grid_size - (int)y/grid_size;
312 if(hit_in_bucket(*b, x, y, shape)) return 1;
313 if(hit_in_bucket(*(b-1), x, y, shape)) return 1;
314 if(hit_in_bucket(*(b-bw), x, y, shape)) return 1;
315 if(hit_in_bucket(*(b-bw-1), x, y, shape)) return 1;
318 if(hit_in_bucket(*(b+1), x, y, shape)) return 1;
319 if(hit_in_bucket(*(b+1-bw), x, y, shape)) return 1;
322 if(hit_in_bucket(*(b+bw), x, y, shape)) return 1;
323 if(hit_in_bucket(*(b+bw-1), x, y, shape)) return 1;
325 if(bdx && bdy && hit_in_bucket(*(b+bw+1), x, y, shape)) return 1;
330 pixel_hit_in_bucket(struct rock_struct *r, float x, float y)
332 for(; r; r=r->next) {
333 if(x < r->x || y < r->y) continue;
334 if(pixel_collide(x - r->x, y - r->y, r->shape)) return 1;
340 pixel_hit_rocks(float x, float y)
342 struct rock_struct **b = bucket(x, y, p);
343 if(pixel_hit_in_bucket(*b, x, y)) return 1;
344 if(pixel_hit_in_bucket(*(b-1), x, y)) return 1;
345 if(pixel_hit_in_bucket(*(b-bw), x, y)) return 1;
346 if(pixel_hit_in_bucket(*(b-bw-1), x, y)) return 1;
351 blast_rocks(float x, float y, float radius, int onlyslow)
354 struct rock_struct *r;
359 for(b=0; b<n_buckets; b++) {
360 for(r=rock_buckets[p][b]; r; r=r->next) {
361 if(r->x <= 0) continue;
363 // This makes it so your explosion from dying magically doesn't leave
364 // any rocks that aren't moving much on the x axis. If onlyslow is set,
365 // only rocks that are barely moving will be pushed.
366 if(onlyslow && (r->dx - screendx < -4 || r->dx - screendx > 3)) continue;
371 n = sqrt(dx*dx + dy*dy);