14 static struct rock rocks[MAXROCKS];
15 static struct rock prototypes[NROCKS];
17 // timers for rock generation.
18 static float rtimers[4];
20 uint32_t nrocks = I_ROCKS;
21 float nrocks_timer = 0;
22 float nrocks_inc_ticks = 2*60*20/(F_ROCKS-I_ROCKS);
24 // constants for rock generation.
25 #define KH (32*20) // 32 s for a speed=1 rock to cross the screen horizontally.
26 #define KV (24*20) // 24 s for a speed=1 rock to cross the screen vertically.
27 #define RDX 2.5 // range for rock dx values (+/-)
28 #define RDY 2.5 // range for rock dy values (+/-)
37 #define ROCK_LEN sizeof("sprites/rockXX.png")
45 for(i=0; i<NROCKS; i++) {
46 snprintf(a, ROCK_LEN, "sprites/rock%02d.png", i);
47 load_sprite(SPRITE(&prototypes[i]), a);
48 prototypes[i].sprite_type = ROCK;
49 prototypes[i].flags = MOVE|DRAW|COLLIDE;
52 memset(rocks, 0, MAXROCKS*sizeof(struct rock));
54 for(i=1; i<MAXROCKS; i++) rocks[i].next = &rocks[i-1];
55 free_sprites[ROCK] = SPRITE(&rocks[MAXROCKS-1]);
60 enum { LEFT, RIGHT, TOP, BOTTOM };
63 // compute the number of rocks/tick that should be coming from each side,
64 // and the speed ranges of rocks coming from each side
66 rock_sides(float *ti, float *speed_min, float *speed_max)
68 float dx0,dx1, dy0,dy1;
69 float hfactor, vfactor;
72 for(i=0; i<4; i++) ti[i] = 0;
73 for(i=0; i<4; i++) speed_min[i] = 0;
74 for(i=0; i<4; i++) speed_max[i] = 0;
75 hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
77 dx0 = -RDX - screendx; dx1 = RDX - screendx;
78 dy0 = -RDY - screendy; dy1 = RDY - screendy;
81 speed_max[RIGHT] = -dx0;
83 // Rocks moving left only. So the RIGHT side of the screen
84 speed_min[RIGHT] = -dx1;
85 ti[RIGHT] = -(dx0+dx1)/2;
87 // Rocks moving left and right
88 speed_max[LEFT] = dx1;
93 // Rocks moving right only. So the LEFT side of the screen
94 speed_min[LEFT] = dx0;
95 speed_max[LEFT] = dx1;
96 ti[LEFT] = (dx0+dx1)/2;
102 speed_max[BOTTOM] = -dy0;
104 // Rocks moving up only. So the BOTTOM of the screen
105 speed_min[BOTTOM] = -dy1;
106 ti[BOTTOM] = -(dy0+dy1)/2;
108 // Rocks moving up and down
109 speed_max[TOP] = dy1;
114 // Rocks moving down only. so the TOP of the screen
115 speed_min[TOP] = dy0;
116 speed_max[TOP] = dy1;
117 ti[TOP] = (dy0+dy1)/2;
120 ti[BOTTOM] *= vfactor;
124 weighted_rnd_range(float min, float max) {
125 return sqrt(min * min + frnd() * (max * max - min * min));
137 if(nrocks < F_ROCKS) {
138 nrocks_timer += t_frame;
139 if(nrocks_timer >= nrocks_inc_ticks) {
140 nrocks_timer -= nrocks_inc_ticks;
145 rock_sides(ti, rmin, rmax);
148 for(i=0; i<4; i++) rtimers[i] += ti[i]*t_frame;
152 while(rtimers[i] >= 1) {
154 if(!free_sprites[ROCK]) return; // sorry, we ran out of rocks!
155 r = (struct rock *) remove_sprite(&free_sprites[ROCK]);
156 type = urnd() % NROCKS;
157 *r = prototypes[type];
162 r->y = frnd()*(YSIZE + r->image->h);
164 r->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
169 r->y = frnd()*(YSIZE + r->image->h);
171 r->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
175 r->x = frnd()*(XSIZE + r->image->w);
179 r->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
182 r->x = frnd()*(XSIZE + r->image->w);
186 r->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
189 add_sprite(SPRITE(r));
199 for(i=0; i<MAXROCKS; i++) draw_sprite(SPRITE(&rocks[i]));
203 blast_rocks(float x, float y, float radius)
209 for(i=0; i<MAXROCKS; i++) {
210 if(!rocks[i].flags) continue;
211 r = SPRITE(&rocks[i]);
212 if(r->x <= 0) continue;
217 n = sqrt(dx*dx + dy*dy);
222 r->flags &= ~COLLIDE;