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 bw = (XSIZE+2*grid_size-1) / grid_size;
60 bh = (YSIZE+2*grid_size-1) / grid_size;
63 rock_buckets[0] = malloc(n_buckets * sizeof(struct rock_struct *));
64 rock_buckets[1] = malloc(n_buckets * sizeof(struct rock_struct *));
65 if(!rock_buckets[0] || !rock_buckets[1]) {
66 fprintf(stderr, "Can't allocate rock buckets.\n");
73 transfer_rock(struct rock_struct *r, struct rock_struct **from, struct rock_struct **to)
85 for(i=0; i<MAXROCKS; i++) rocks[i].image = NULL;
86 rocks[0].next = NULL; free_rocks = &rocks[MAXROCKS-1];
87 for(i = 1; i<MAXROCKS; i++) rocks[i].next = &rocks[i-1];
88 for(i = 0; i<n_buckets; i++) {
89 rock_buckets[0][i] = NULL;
90 rock_buckets[1][i] = NULL;
97 #define ROCK_LEN sizeof("sprites/rockXX.png")
106 for(i = 0; i<NROCKS; i++) {
107 snprintf(a, ROCK_LEN, "sprites/rock%02d.png", i);
108 NULLERROR(surf_rock[i] = load_image(a));
109 get_shape(surf_rock[i], &rock_shapes[i]);
110 maxw = max(maxw, rock_shapes[i].w);
111 maxh = max(maxh, rock_shapes[i].h);
113 grid_size = max(maxw, maxh) * 3 / 2;
119 enum { LEFT, RIGHT, TOP, BOTTOM };
122 // compute the number of rocks/tick that should be coming from each side,
123 // and the speed ranges of rocks coming from each side
125 rock_sides(float *ti, float *speed_min, float *speed_max)
127 float dx0,dx1, dy0,dy1;
128 float hfactor, vfactor;
131 for(i=0; i<4; i++) ti[i] = 0;
132 for(i=0; i<4; i++) speed_min[i] = 0;
133 for(i=0; i<4; i++) speed_max[i] = 0;
134 hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
136 dx0 = -RDX - screendx; dx1 = RDX - screendx;
137 dy0 = -RDY - screendy; dy1 = RDY - screendy;
140 speed_max[RIGHT] = -dx0;
142 // Rocks moving left only. So the RIGHT side of the screen
143 speed_min[RIGHT] = -dx1;
144 ti[RIGHT] = -(dx0+dx1)/2;
146 // Rocks moving left and right
147 speed_max[LEFT] = dx1;
152 // Rocks moving right only. So the LEFT side of the screen
153 speed_min[LEFT] = dx0;
154 speed_max[LEFT] = dx1;
155 ti[LEFT] = (dx0+dx1)/2;
158 ti[RIGHT] *= hfactor;
161 speed_max[BOTTOM] = -dy0;
163 // Rocks moving up only. So the BOTTOM of the screen
164 speed_min[BOTTOM] = -dy1;
165 ti[BOTTOM] = -(dy0+dy1)/2;
167 // Rocks moving up and down
168 speed_max[TOP] = dy1;
173 // Rocks moving down only. so the TOP of the screen
174 speed_min[TOP] = dy0;
175 speed_max[TOP] = dy1;
176 ti[TOP] = (dy0+dy1)/2;
179 ti[BOTTOM] *= vfactor;
183 weighted_rnd_range(float min, float max) {
184 return sqrt(min * min + frnd() * (max * max - min * min));
191 struct rock_struct *r;
196 if(nrocks < F_ROCKS) {
197 nrocks_timer += t_frame;
198 if(nrocks_timer >= nrocks_inc_ticks) {
199 nrocks_timer -= nrocks_inc_ticks;
204 rock_sides(ti, rmin, rmax);
207 for(i=0; i<4; i++) rtimers[i] += ti[i]*t_frame;
211 while(rtimers[i] >= 1) {
213 if(!free_rocks) return; // sorry, we ran out of rocks!
215 r->type_number = urnd() % NROCKS;
216 r->image = surf_rock[r->type_number];
217 r->shape = &rock_shapes[r->type_number];
221 r->y = frnd()*(YSIZE + r->image->h);
223 r->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
228 r->y = frnd()*(YSIZE + r->image->h);
230 r->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
234 r->x = frnd()*(XSIZE + r->image->w);
238 r->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
241 r->x = frnd()*(XSIZE + r->image->w);
245 r->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
248 transfer_rock(r, &free_rocks, bucket(r->x, r->y, p));
257 struct rock_struct **head;
258 struct rock_struct *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 or resort into 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 transfer_rock(r, head, &free_rocks);
276 } else transfer_rock(r, head, bucket(r->x, r->y, 1-p));
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(struct rock_struct *r, float x, float y, struct shape *shape)
298 for(; r; r=r->next) {
299 if(collide(x - r->x, y - r->y, r->shape, shape)) return 1;
305 hit_rocks(float x, float y, struct shape *shape)
307 struct rock_struct **b = bucket(x, y, p);
308 int bdx = ((int)x+shape->w)/grid_size - (int)x/grid_size;
309 int bdy = ((int)y+shape->h)/grid_size - (int)y/grid_size;
310 if(hit_in_bucket(*b, x, y, shape)) return 1;
311 if(hit_in_bucket(*(b-1), x, y, shape)) return 1;
312 if(hit_in_bucket(*(b-bw), x, y, shape)) return 1;
313 if(hit_in_bucket(*(b-bw-1), x, y, shape)) return 1;
316 if(hit_in_bucket(*(b+1), x, y, shape)) return 1;
317 if(hit_in_bucket(*(b+1-bw), x, y, shape)) return 1;
320 if(hit_in_bucket(*(b+bw), x, y, shape)) return 1;
321 if(hit_in_bucket(*(b+bw-1), x, y, shape)) return 1;
323 if(bdx && bdy && hit_in_bucket(*(b+bw+1), x, y, shape)) return 1;
328 pixel_hit_in_bucket(struct rock_struct *r, float x, float y)
330 for(; r; r=r->next) {
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)
340 struct rock_struct **b = bucket(x, y, p);
341 if(pixel_hit_in_bucket(*b, x, y)) return 1;
342 if(pixel_hit_in_bucket(*(b-1), x, y)) return 1;
343 if(pixel_hit_in_bucket(*(b-bw), x, y)) return 1;
344 if(pixel_hit_in_bucket(*(b-bw-1), x, y)) return 1;
349 blast_rocks(float x, float y, float radius, int onlyslow)
352 struct rock_struct *r;
357 for(b=0; b<n_buckets; b++) {
358 for(r=rock_buckets[p][b]; r; r=r->next) {
359 if(r->x <= 0) continue;
361 // This makes it so your explosion from dying magically doesn't leave
362 // any rocks that aren't moving much on the x axis. If onlyslow is set,
363 // only rocks that are barely moving will be pushed.
364 if(onlyslow && (r->dx - screendx < -4 || r->dx - screendx > 3)) continue;
369 n = sqrt(dx*dx + dy*dy);