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 // constants for rock generation.
30 #define KH 32.0 // 32 s for a speed=1 rock to cross the screen horizontally.
31 #define KV 24.0 // 24 s for a speed=1 rock to cross the screen vertically.
32 #define RDX 2.5 // range for rock dx values (+/-)
33 #define RDY 2.5 // range for rock dy values (+/-)
37 #define crnd() (2*(rnd()-0.5))
47 for(i = 0; i<NROCKS; i++) {
48 snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
49 NULLERROR(temp = IMG_Load(a));
50 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
51 get_shape(surf_rock[i], &rock_shapes[i]);
62 for(i = 0; i<MAXROCKS; i++) rock[i].active = 0;
65 enum { LEFT, RIGHT, TOP, BOTTOM };
68 rock_timer_increments(float *ti)
70 float dx0,dx1, dy0,dy1;
73 for(i=0; i<4; i++) ti[i] = 0;
75 dx0 = -RDX - screendx; dx1 = RDX - screendx;
76 dy0 = -RDY - screendy; dy1 = RDY - screendy;
80 if(dx1 < 0) ti[RIGHT] = -(dx0+dx1)/2;
85 } else ti[LEFT] = (dx0+dx1)/2;
90 if(dy1 < 0) ti[BOTTOM] = -(dy0+dy1)/2;
95 } else ti[TOP] = (dy0+dy1)/2;
106 rock_timer_increments(ti);
109 rtimers[i] += ti[i]*gamerate/20;
110 if(rtimers[i] >= 1) {
112 while(rockptr->active && j<MAXROCKS) {
113 if(++rockptr - rock >= MAXROCKS) rockptr = rock;
116 if(!rockptr->active) {
118 rockptr->type_number = random() % NROCKS;
119 rockptr->image = surf_rock[rockptr->type_number];
120 rockptr->shape = &rock_shapes[rockptr->type_number];
124 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
127 rockptr->x = -rockptr->image->w;
128 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
131 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
135 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
136 rockptr->y = -rockptr->image->h;
142 rockptr->dx = RDX*crnd();
143 rockptr->dy = RDY*crnd();
144 x = (rockptr->dx-screendx)*gamerate;
145 y = (rockptr->dy-screendy)*gamerate;
147 } while(x < -rockptr->image->w || x >= XSIZE
148 || y < -rockptr->image->h || y >= YSIZE);
149 if(j > 1) printf("had to try %d times.\n", j);
162 // Move all the rocks
163 for(i = 0; i < MAXROCKS; i++) {
166 rock[i].x += (rock[i].dx-screendx)*gamerate;
167 rock[i].y += (rock[i].dy-screendy)*gamerate;
169 if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE
170 || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) {
183 src.x = 0; src.y = 0;
185 for(i = 0; i<MAXROCKS; i++) {
187 src.w = rock[i].image->w;
188 src.h = rock[i].image->h;
192 dest.x = (int) rock[i].x;
193 dest.y = (int) rock[i].y;
195 SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
202 hit_rocks(float x, float y, struct shape *shape)
206 for(i=0; i<MAXROCKS; i++) {
208 if(collide(x-rock[i].x, y-rock[i].y, rock[i].shape, shape))
216 blast_rocks(float x, float y, float radius, int onlyslow)
221 for(i = 0; i<MAXROCKS; i++ ) {
222 if(rock[i].x <= 0) continue;
224 // This makes it so your explosion from dying magically doesn't leave
225 // any rocks that aren't moving much on the x axis. If onlyslow is set,
226 // only rocks that are barely moving will be pushed.
227 if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue;
232 n = sqrt(dx*dx + dy*dy);
235 rock[i].dx += 54.0*(dx+30)/n;
236 rock[i].dy += 54.0*dy/n;