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 uint32_t nrocks_timer;
30 uint32_t nrocks_inc_ticks = 2*60*1000/(F_ROCKS-I_ROCKS);
32 // constants for rock generation.
33 #define KH 32.0 // 32 s for a speed=1 rock to cross the screen horizontally.
34 #define KV 24.0 // 24 s for a speed=1 rock to cross the screen vertically.
35 #define RDX 2.5 // range for rock dx values (+/-)
36 #define RDY 2.5 // range for rock dy values (+/-)
40 #define crnd() (2*(rnd()-0.5))
50 for(i = 0; i<NROCKS; i++) {
51 snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
52 NULLERROR(temp = IMG_Load(a));
53 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
54 get_shape(surf_rock[i], &rock_shapes[i]);
64 for(i = 0; i<MAXROCKS; i++) rock[i].active = 0;
69 enum { LEFT, RIGHT, TOP, BOTTOM };
72 // compute the number of rocks/seccond that should be coming from each side
74 // compute the speed ranges of rocks coming from each side
76 rock_sides(float *ti, float *speed_min, float *speed_max)
78 float dx0,dx1, dy0,dy1;
79 float hfactor, vfactor;
82 for(i=0; i<4; i++) ti[i] = 0;
83 for(i=0; i<4; i++) speed_min[i] = 0;
84 for(i=0; i<4; i++) speed_max[i] = 0;
85 hfactor = nrocks/KH; vfactor = nrocks/KV;
87 dx0 = -RDX - screendx; dx1 = RDX - screendx;
88 dy0 = -RDY - screendy; dy1 = RDY - screendy;
92 speed_max[RIGHT] = -dx0;
94 // Rocks moving left only. So the RIGHT side of the screen
95 speed_min[RIGHT] = -dx1;
96 ti[RIGHT] = -(dx0+dx1)/2;
98 // Rocks moving left and right
99 speed_max[LEFT] = dx1;
104 // Rocks moving right only. So the LEFT side of the screen
105 speed_min[LEFT] = dx0;
106 speed_max[LEFT] = dx1;
107 ti[LEFT] = (dx0+dx1)/2;
111 ti[RIGHT] *= hfactor;
115 speed_max[BOTTOM] = -dy0;
117 // Rocks moving up only. So the BOTTOM of the screen
118 speed_min[BOTTOM] = -dy1;
119 ti[BOTTOM] = -(dy0+dy1)/2;
121 // Rocks moving up and down
122 speed_max[TOP] = dy1;
127 // Rocks moving down only. so the TOP of the screen
128 speed_min[TOP] = dy0;
129 speed_max[TOP] = dy1;
130 ti[TOP] = (dy0+dy1)/2;
134 ti[BOTTOM] *= vfactor;
138 weighted_rnd_range(float min, float max) {
139 return sqrt(min * min + rnd() * (max * max - min * min));
150 if(nrocks < F_ROCKS) {
151 nrocks_timer += ticks_since_last;
152 if(nrocks_timer >= nrocks_inc_ticks) {
153 nrocks_timer -= nrocks_inc_ticks;
158 rock_sides(ti, rmin, rmax);
160 // loop through the four sides of the screen
162 // see if we generate a rock for this side this frame
163 rtimers[i] += ti[i]*gamerate/20;
164 while(rtimers[i] >= 1) {
167 while(rockptr->active && j<MAXROCKS) {
168 if(++rockptr - rock >= MAXROCKS) rockptr = rock;
171 if(!rockptr->active) {
172 rockptr->type_number = random() % NROCKS;
173 rockptr->image = surf_rock[rockptr->type_number];
174 rockptr->shape = &rock_shapes[rockptr->type_number];
178 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
180 rockptr->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
181 rockptr->dy = RDY*crnd();
184 rockptr->x = -rockptr->image->w;
185 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
187 rockptr->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
188 rockptr->dy = RDY*crnd();
191 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
194 rockptr->dx = RDY*crnd();
195 rockptr->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
198 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
199 rockptr->y = -rockptr->image->h;
201 rockptr->dx = RDY*crnd();
202 rockptr->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
217 // Move all the rocks
218 for(i = 0; i < MAXROCKS; i++) {
221 rock[i].x += (rock[i].dx-screendx)*gamerate;
222 rock[i].y += (rock[i].dy-screendy)*gamerate;
224 if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE
225 || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) {
238 src.x = 0; src.y = 0;
240 for(i = 0; i<MAXROCKS; i++) {
242 src.w = rock[i].image->w;
243 src.h = rock[i].image->h;
247 dest.x = (int) rock[i].x;
248 dest.y = (int) rock[i].y;
250 SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
257 hit_rocks(float x, float y, struct shape *shape)
261 for(i=0; i<MAXROCKS; i++) {
263 if(collide(x-rock[i].x, y-rock[i].y, rock[i].shape, shape))
271 blast_rocks(float x, float y, float radius, int onlyslow)
278 for(i = 0; i<MAXROCKS; i++ ) {
279 if(rock[i].x <= 0) continue;
281 // This makes it so your explosion from dying magically doesn't leave
282 // any rocks that aren't moving much on the x axis. If onlyslow is set,
283 // only rocks that are barely moving will be pushed.
284 if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue;
289 n = sqrt(dx*dx + dy*dy);
292 rock[i].dx += 54.0*dx/n;
293 rock[i].dy += 54.0*dy/n;