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 = NORMAL_I_ROCKS;
21 uint32_t initial_rocks = NORMAL_I_ROCKS;
22 uint32_t final_rocks = NORMAL_F_ROCKS;
23 float nrocks_timer = 0;
24 float nrocks_inc_ticks = 2*60*20/(NORMAL_F_ROCKS-NORMAL_I_ROCKS);
26 // constants for rock generation.
27 #define KH (32*20) // 32 s for a speed=1 rock to cross the screen horizontally.
28 #define KV (24*20) // 24 s for a speed=1 rock to cross the screen vertically.
29 #define RDX 2.5 // range for rock dx values (+/-)
30 #define RDY 2.5 // range for rock dy values (+/-)
35 nrocks = initial_rocks;
36 nrocks_inc_ticks = 2*60*20/(final_rocks-initial_rocks);
40 #define ROCK_LEN sizeof("rockXX.png")
48 for(i=0; i<NROCKS; i++) {
49 snprintf(a, ROCK_LEN, "rock%02d.png", i);
50 load_sprite(SPRITE(&prototypes[i]), a);
51 prototypes[i].sprite_type = ROCK;
52 prototypes[i].flags = MOVE|DRAW|COLLIDE;
55 memset(rocks, 0, MAXROCKS*sizeof(struct rock));
57 for(i=1; i<MAXROCKS; i++) rocks[i].next = &rocks[i-1];
58 free_sprites[ROCK] = SPRITE(&rocks[MAXROCKS-1]);
63 enum { LEFT, RIGHT, TOP, BOTTOM };
66 // compute the number of rocks/tick that should be coming from each side,
67 // and the speed ranges of rocks coming from each side
69 rock_sides(float *ti, float *speed_min, float *speed_max)
71 float dx0,dx1, dy0,dy1;
72 float hfactor, vfactor;
75 for(i=0; i<4; i++) ti[i] = 0;
76 for(i=0; i<4; i++) speed_min[i] = 0;
77 for(i=0; i<4; i++) speed_max[i] = 0;
78 hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
80 dx0 = -RDX - screendx; dx1 = RDX - screendx;
81 dy0 = -RDY - screendy; dy1 = RDY - screendy;
84 speed_max[RIGHT] = -dx0;
86 // Rocks moving left only. So the RIGHT side of the screen
87 speed_min[RIGHT] = -dx1;
88 ti[RIGHT] = -(dx0+dx1)/2;
90 // Rocks moving left and right
91 speed_max[LEFT] = dx1;
96 // Rocks moving right only. So the LEFT side of the screen
97 speed_min[LEFT] = dx0;
98 speed_max[LEFT] = dx1;
99 ti[LEFT] = (dx0+dx1)/2;
102 ti[RIGHT] *= hfactor;
105 speed_max[BOTTOM] = -dy0;
107 // Rocks moving up only. So the BOTTOM of the screen
108 speed_min[BOTTOM] = -dy1;
109 ti[BOTTOM] = -(dy0+dy1)/2;
111 // Rocks moving up and down
112 speed_max[TOP] = dy1;
117 // Rocks moving down only. so the TOP of the screen
118 speed_min[TOP] = dy0;
119 speed_max[TOP] = dy1;
120 ti[TOP] = (dy0+dy1)/2;
123 ti[BOTTOM] *= vfactor;
127 weighted_rnd_range(float min, float max) {
128 return sqrt(min * min + frnd() * (max * max - min * min));
140 if(nrocks < final_rocks) {
141 nrocks_timer += t_frame;
142 if(nrocks_timer >= nrocks_inc_ticks) {
143 nrocks_timer -= nrocks_inc_ticks;
148 rock_sides(ti, rmin, rmax);
151 for(i=0; i<4; i++) rtimers[i] += ti[i]*t_frame;
155 while(rtimers[i] >= 1) {
157 if(!free_sprites[ROCK]) return; // sorry, we ran out of rocks!
158 r = (struct rock *) remove_sprite(&free_sprites[ROCK]);
159 type = urnd() % NROCKS;
160 *r = prototypes[type];
162 r->life = r->area * 300;
166 r->y = frnd()*(YSIZE + r->image->h);
168 r->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
173 r->y = frnd()*(YSIZE + r->image->h);
175 r->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
179 r->x = (frnd()*(XSIZE + r->image->w)) - r->image->w;
183 r->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
186 r->x = (frnd() * (XSIZE + r->image->w)) - r->image->w;
190 r->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
193 add_sprite(SPRITE(r));
203 for(i=0; i<MAXROCKS; i++) draw_sprite(SPRITE(&rocks[i]));