15 struct rock_struct *next;
22 struct rock_struct rocks[MAXROCKS], *free_rocks;
24 struct rock_struct **rock_buckets[2];
29 SDL_Surface *surf_rock[NROCKS];
30 struct shape rock_shapes[NROCKS];
32 // timers for rock generation.
37 float nrocks_inc_ticks = 2*60*20/(F_ROCKS-I_ROCKS);
39 // constants for rock generation.
40 #define KH (32.0*20) // 32 s for a speed=1 rock to cross the screen horizontally.
41 #define KV (24.0*20) // 24 s for a speed=1 rock to cross the screen vertically.
42 #define RDX 2.5 // range for rock dx values (+/-)
43 #define RDY 2.5 // range for rock dy values (+/-)
45 static inline int bucket(int x, int y) { return (1+x/grid_size) + bw*(1+y/grid_size); }
50 bw = (XSIZE+2*grid_size-1) / grid_size;
51 bh = (YSIZE+2*grid_size-1) / grid_size;
54 rock_buckets[0] = malloc(n_buckets * sizeof(struct rock_struct *));
55 rock_buckets[1] = malloc(n_buckets * sizeof(struct rock_struct *));
56 if(!rock_buckets[0] || !rock_buckets[1]) {
57 fprintf(stderr, "Can't allocate rock buckets.\n");
64 sort_rock(struct rock_struct *q, struct rock_struct *r, int p)
66 int b = bucket(r->x, r->y);
67 q->next = r->next; // remove from old list
68 r->next = rock_buckets[p][b]; // insert into new list
69 rock_buckets[p][b] = r;
77 for(i=0; i<MAXROCKS; i++) rocks[i].image = NULL;
78 rocks[0].next = NULL; free_rocks = &rocks[MAXROCKS-1];
79 for(i = 1; i<MAXROCKS; i++) rocks[i].next = &rocks[i-1];
80 for(i = 0; i<n_buckets; i++) {
81 rock_buckets[0][i] = NULL;
82 rock_buckets[1][i] = NULL;
97 for(i = 0; i<NROCKS; i++) {
98 snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
99 NULLERROR(temp = IMG_Load(a));
100 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
101 get_shape(surf_rock[i], &rock_shapes[i]);
102 maxw = max(maxw, rock_shapes[i].w);
103 maxh = max(maxh, rock_shapes[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_struct *r;
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!
207 r->type_number = urnd() % NROCKS;
208 r->image = surf_rock[r->type_number];
209 r->shape = &rock_shapes[r->type_number];
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 sort_rock((struct rock_struct *)&free_rocks, r, p);
249 struct rock_struct *q,*r;
251 // Move all the rocks
252 for(b=0; b<n_buckets; b++) {
253 q=(struct rock_struct *)&rock_buckets[p][b]; r=q->next;
256 r->x += (r->dx - screendx)*t_frame;
257 r->y += (r->dy - screendy)*t_frame;
260 if(r->x + r->image->w < 0 || r->x >= XSIZE
261 || r->y + r->image->h < 0 || r->y >= YSIZE) {
263 r->next = free_rocks; free_rocks = r;
265 } else sort_rock(q,r,1-p);
266 if(q->next == r) q = q->next;
267 if(q) r = q->next; else r = NULL;
270 p = 1-p; // switch current set of buckets.
279 for(i=0; i<MAXROCKS; i++) {
280 if(!rocks[i].image) continue;
281 dest.x = rocks[i].x; dest.y = rocks[i].y;
282 SDL_BlitSurface(rocks[i].image,NULL,surf_screen,&dest);
287 hit_in_bucket(int b, float x, float y, struct shape *shape)
289 struct rock_struct *r;
291 for(r=rock_buckets[p][b]; r; r=r->next) {
292 if(collide(x - r->x, y - r->y, r->shape, shape)) return 1;
298 hit_rocks(float x, float y, struct shape *shape)
300 int b = bucket(x, y);
301 int bdx = ((int)x+shape->w)/grid_size - (int)x/grid_size;
302 int bdy = ((int)y+shape->h)/grid_size - (int)y/grid_size;
303 if(hit_in_bucket(b, x, y, shape)) return 1;
304 if(hit_in_bucket(b-1, x, y, shape)) return 1;
305 if(hit_in_bucket(b-bw, x, y, shape)) return 1;
306 if(hit_in_bucket(b-bw-1, x, y, shape)) return 1;
309 if(hit_in_bucket(b+1, x, y, shape)) return 1;
310 if(hit_in_bucket(b+1-bw, x, y, shape)) return 1;
313 if(hit_in_bucket(b+bw, x, y, shape)) return 1;
314 if(hit_in_bucket(b+bw-1, x, y, shape)) return 1;
316 if(bdx && bdy && hit_in_bucket(b+bw+1, x, y, shape)) return 1;
321 pixel_hit_in_bucket(int b, float x, float y)
323 struct rock_struct *r;
324 for(r=rock_buckets[p][b]; r; r=r->next) {
325 if(x < r->x || y < r->y) continue;
326 if(pixel_collide(x - r->x, y - r->y, r->shape)) return 1;
332 pixel_hit_rocks(float x, float y)
334 int b = bucket(x, y);
335 if(pixel_hit_in_bucket(b, x, y)) return 1;
336 if(pixel_hit_in_bucket(b-1, x, y)) return 1;
337 if(pixel_hit_in_bucket(b-bw, x, y)) return 1;
338 if(pixel_hit_in_bucket(b-bw-1, x, y)) return 1;
343 blast_rocks(float x, float y, float radius, int onlyslow)
346 struct rock_struct *r;
351 for(b=0; b<n_buckets; b++) {
352 for(r=rock_buckets[p][b]; r; r=r->next) {
353 if(r->x <= 0) continue;
355 // This makes it so your explosion from dying magically doesn't leave
356 // any rocks that aren't moving much on the x axis. If onlyslow is set,
357 // only rocks that are barely moving will be pushed.
358 if(onlyslow && (r->dx - screendx < -4 || r->dx - screendx > 3)) continue;
363 n = sqrt(dx*dx + dy*dy);