20 struct rock_struct rock[MAXROCKS], *rockptr = rock;
22 SDL_Surface *surf_rock[NROCKS];
23 struct shape rock_shapes[NROCKS];
25 // timers for rock generation.
29 int32_t rsum, rsamples;
33 uint32_t nrocks_timer;
34 uint32_t nrocks_inc_ticks = 2*60*1000/(F_ROCKS-I_ROCKS);
36 // constants for rock generation.
37 #define KH 32.0 // 32 s for a speed=1 rock to cross the screen horizontally.
38 #define KV 24.0 // 24 s for a speed=1 rock to cross the screen vertically.
39 #define RDX 2.5 // range for rock dx values (+/-)
40 #define RDY 2.5 // range for rock dy values (+/-)
44 #define crnd() (2*(rnd()-0.5))
54 for(i = 0; i<NROCKS; i++) {
55 snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
56 NULLERROR(temp = IMG_Load(a));
57 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
58 get_shape(surf_rock[i], &rock_shapes[i]);
68 for(i = 0; i<MAXROCKS; i++) rock[i].active = 0;
72 rsum = 0; rsamples = 0;
75 enum { LEFT, RIGHT, TOP, BOTTOM };
78 // compute the number of rocks/seccond that should be coming from each side
80 // compute the speed ranges of rocks coming from each side
82 rock_sides(float *ti, float *speed_min, float *speed_max)
84 float dx0,dx1, dy0,dy1;
85 float hfactor, vfactor;
88 for(i=0; i<4; i++) ti[i] = 0;
89 for(i=0; i<4; i++) speed_min[i] = 0;
90 for(i=0; i<4; i++) speed_max[i] = 0;
91 hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
93 dx0 = -RDX - screendx; dx1 = RDX - screendx;
94 dy0 = -RDY - screendy; dy1 = RDY - screendy;
98 speed_max[RIGHT] = -dx0;
100 // Rocks moving left only. So the RIGHT side of the screen
101 speed_min[RIGHT] = -dx1;
102 ti[RIGHT] = -(dx0+dx1)/2;
104 // Rocks moving left and right
105 speed_max[LEFT] = dx1;
110 // Rocks moving right only. So the LEFT side of the screen
111 speed_min[LEFT] = dx0;
112 speed_max[LEFT] = dx1;
113 ti[LEFT] = (dx0+dx1)/2;
117 ti[RIGHT] *= hfactor;
121 speed_max[BOTTOM] = -dy0;
123 // Rocks moving up only. So the BOTTOM of the screen
124 speed_min[BOTTOM] = -dy1;
125 ti[BOTTOM] = -(dy0+dy1)/2;
127 // Rocks moving up and down
128 speed_max[TOP] = dy1;
133 // Rocks moving down only. so the TOP of the screen
134 speed_min[TOP] = dy0;
135 speed_max[TOP] = dy1;
136 ti[TOP] = (dy0+dy1)/2;
140 ti[BOTTOM] *= vfactor;
144 weighted_rnd_range(float min, float max) {
145 return sqrt(min * min + rnd() * (max * max - min * min));
156 if(nrocks < F_ROCKS) {
157 nrocks_timer += ticks_since_last;
158 if(nrocks_timer >= nrocks_inc_ticks) {
159 nrocks_timer -= nrocks_inc_ticks;
164 rock_sides(ti, rmin, rmax);
166 // loop through the four sides of the screen
168 // see if we generate a rock for this side this frame
169 rtimers[i] += ti[i]*gamerate/20;
170 while(rtimers[i] >= 1) {
173 while(rockptr->active && j<MAXROCKS) {
174 if(++rockptr - rock >= MAXROCKS) rockptr = rock;
177 if(!rockptr->active) {
178 rockptr->type_number = random() % NROCKS;
179 rockptr->image = surf_rock[rockptr->type_number];
180 rockptr->shape = &rock_shapes[rockptr->type_number];
184 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
186 rockptr->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
187 rockptr->dy = RDY*crnd();
190 rockptr->x = -rockptr->image->w;
191 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
193 rockptr->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
194 rockptr->dy = RDY*crnd();
197 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
200 rockptr->dx = RDX*crnd();
201 rockptr->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
204 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
205 rockptr->y = -rockptr->image->h;
207 rockptr->dx = RDX*crnd();
208 rockptr->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
224 // Move all the rocks
225 for(i = 0; i < MAXROCKS; i++) {
228 rock[i].x += (rock[i].dx-screendx)*gamerate;
229 rock[i].y += (rock[i].dy-screendy)*gamerate;
231 if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE
232 || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) {
239 if(lrcnt == -1 && rcnt == nrocks) lrcnt = 0;
240 if(rcnt != lrcnt && lrcnt != -1) {
242 rsum += rcnt-nrocks; rsamples++;
243 ravg = (float) rsum / rsamples;
244 printf("%.2f%%\n", 100.0 * ravg / nrocks);
255 src.x = 0; src.y = 0;
257 for(i = 0; i<MAXROCKS; i++) {
259 src.w = rock[i].image->w;
260 src.h = rock[i].image->h;
264 dest.x = (int) rock[i].x;
265 dest.y = (int) rock[i].y;
267 SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
274 hit_rocks(float x, float y, struct shape *shape)
278 for(i=0; i<MAXROCKS; i++) {
280 if(collide(x-rock[i].x, y-rock[i].y, rock[i].shape, shape))
288 blast_rocks(float x, float y, float radius, int onlyslow)
295 for(i = 0; i<MAXROCKS; i++ ) {
296 if(rock[i].x <= 0) continue;
298 // This makes it so your explosion from dying magically doesn't leave
299 // any rocks that aren't moving much on the x axis. If onlyslow is set,
300 // only rocks that are barely moving will be pushed.
301 if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue;
306 n = sqrt(dx*dx + dy*dy);
309 rock[i].dx += 54.0*dx/n;
310 rock[i].dy += 54.0*dy/n;