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.
31 uint32_t nrocks_timer;
32 uint32_t nrocks_inc_ticks = 2*60*1000/(F_ROCKS-I_ROCKS);
34 // constants for rock generation.
35 #define KH 32.0 // 32 s for a speed=1 rock to cross the screen horizontally.
36 #define KV 24.0 // 24 s for a speed=1 rock to cross the screen vertically.
37 #define RDX 2.5 // range for rock dx values (+/-)
38 #define RDY 2.5 // range for rock dy values (+/-)
42 #define crnd() (2*(rnd()-0.5))
52 for(i = 0; i<NROCKS; i++) {
53 snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
54 NULLERROR(temp = IMG_Load(a));
55 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
56 get_shape(surf_rock[i], &rock_shapes[i]);
66 for(i = 0; i<MAXROCKS; i++) rock[i].active = 0;
72 enum { LEFT, RIGHT, TOP, BOTTOM };
75 // compute the number of rocks/seccond that should be coming from each side
77 // compute the speed ranges of rocks coming from each side
79 rock_sides(float *ti, float *speed_min, float *speed_max)
81 float dx0,dx1, dy0,dy1;
82 float hfactor, vfactor;
85 for(i=0; i<4; i++) ti[i] = 0;
86 for(i=0; i<4; i++) speed_min[i] = 0;
87 for(i=0; i<4; i++) speed_max[i] = 0;
88 hfactor = nrocks/KH; vfactor = nrocks/KV;
90 dx0 = -RDX - screendx; dx1 = RDX - screendx;
91 dy0 = -RDY - screendy; dy1 = RDY - screendy;
95 speed_max[RIGHT] = -dx0;
97 // Rocks moving left only. So the RIGHT side of the screen
98 speed_min[RIGHT] = -dx1;
99 ti[RIGHT] = -(dx0+dx1)/2;
101 // Rocks moving left and right
102 speed_max[LEFT] = dx1;
107 // Rocks moving right only. So the LEFT side of the screen
108 speed_min[LEFT] = dx0;
109 speed_max[LEFT] = dx1;
110 ti[LEFT] = (dx0+dx1)/2;
114 ti[RIGHT] *= hfactor;
118 speed_max[BOTTOM] = -dy0;
120 // Rocks moving up only. So the BOTTOM of the screen
121 speed_min[BOTTOM] = -dy1;
122 ti[BOTTOM] = -(dy0+dy1)/2;
124 // Rocks moving up and down
125 speed_max[TOP] = dy1;
130 // Rocks moving down only. so the TOP of the screen
131 speed_min[TOP] = dy0;
132 speed_max[TOP] = dy1;
133 ti[TOP] = (dy0+dy1)/2;
137 ti[BOTTOM] *= vfactor;
141 weighted_rnd_range(float min, float max) {
142 return sqrt(min * min + rnd() * (max * max - min * min));
153 if(nrocks < F_ROCKS) {
154 nrocks_timer += ticks_since_last;
155 if(nrocks_timer >= nrocks_inc_ticks) {
156 nrocks_timer -= nrocks_inc_ticks;
161 rock_sides(ti, rmin, rmax);
163 // loop through the four sides of the screen
165 // see if we generate a rock for this side this frame
166 rtimers[i] += ti[i]*gamerate/20;
167 while(rtimers[i] >= 1) {
170 while(rockptr->active && j<MAXROCKS) {
171 if(++rockptr - rock >= MAXROCKS) rockptr = rock;
174 if(!rockptr->active) {
175 rockptr->type_number = random() % NROCKS;
176 rockptr->image = surf_rock[rockptr->type_number];
177 rockptr->shape = &rock_shapes[rockptr->type_number];
181 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
183 rockptr->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
184 rockptr->dy = RDY*crnd();
187 rockptr->x = -rockptr->image->w;
188 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
190 rockptr->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
191 rockptr->dy = RDY*crnd();
194 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
197 rockptr->dx = RDX*crnd();
198 rockptr->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
201 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
202 rockptr->y = -rockptr->image->h;
204 rockptr->dx = RDX*crnd();
205 rockptr->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
221 // Move all the rocks
222 for(i = 0; i < MAXROCKS; i++) {
225 rock[i].x += (rock[i].dx-screendx)*gamerate;
226 rock[i].y += (rock[i].dy-screendy)*gamerate;
228 if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE
229 || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) {
235 // if(rcnt < nrocks) printf("-%d.\n", nrocks-rcnt);
236 // else printf("%d.\n", rcnt-nrocks);
245 src.x = 0; src.y = 0;
247 for(i = 0; i<MAXROCKS; i++) {
249 src.w = rock[i].image->w;
250 src.h = rock[i].image->h;
254 dest.x = (int) rock[i].x;
255 dest.y = (int) rock[i].y;
257 SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
264 hit_rocks(float x, float y, struct shape *shape)
268 for(i=0; i<MAXROCKS; i++) {
270 if(collide(x-rock[i].x, y-rock[i].y, rock[i].shape, shape))
278 blast_rocks(float x, float y, float radius, int onlyslow)
285 for(i = 0; i<MAXROCKS; i++ ) {
286 if(rock[i].x <= 0) continue;
288 // This makes it so your explosion from dying magically doesn't leave
289 // any rocks that aren't moving much on the x axis. If onlyslow is set,
290 // only rocks that are barely moving will be pushed.
291 if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue;
296 n = sqrt(dx*dx + dy*dy);
299 rock[i].dx += 54.0*dx/n;
300 rock[i].dy += 54.0*dy/n;