JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
cleaned up bang dots code a bit
[vor.git] / rocks.c
1 #include <SDL_image.h>
2 #include <math.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "common.h"
7 #include "config.h"
8 #include "file.h"
9 #include "globals.h"
10 #include "mt.h"
11 #include "rocks.h"
12 #include "shape.h"
13
14 struct rock_struct {
15         float x,y,dx,dy;
16         int active;
17         SDL_Surface *image;
18         struct shape *shape;
19         int type_number;
20 }; 
21
22 struct rock_struct rock[MAXROCKS], *rockptr = rock;
23
24 SDL_Surface *surf_rock[NROCKS];
25 struct shape rock_shapes[NROCKS];
26
27 // timers for rock generation.
28 float rtimers[4];
29
30 uint32_t nrocks;
31 float nrocks_timer;
32 float nrocks_inc_ticks = 2*60*20/(F_ROCKS-I_ROCKS);
33
34 // constants for rock generation.
35 #define KH (32.0*20)  // 32 s for a speed=1 rock to cross the screen horizontally.
36 #define KV (24.0*20)  // 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 (+/-)
39
40 int
41 init_rocks(void)
42 {
43         int i;
44         char a[MAX_PATH_LEN];
45         SDL_Surface *temp;
46
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]);
52         }
53         reset_rocks();
54         return 0;
55 }
56
57 void
58 reset_rocks(void)
59 {
60         int i;
61
62         for(i = 0; i<MAXROCKS; i++) rock[i].active = 0;
63         nrocks = I_ROCKS;
64         nrocks_timer = 0;
65 }
66
67 enum { LEFT, RIGHT, TOP, BOTTOM };
68
69
70 // compute the number of rocks/second that should be coming from each side
71
72 // compute the speed ranges of rocks coming from each side
73 void
74 rock_sides(float *ti, float *speed_min, float *speed_max)
75 {
76         float dx0,dx1, dy0,dy1;
77         float hfactor, vfactor;
78         int i;
79
80         for(i=0; i<4; i++) ti[i] = 0;
81         for(i=0; i<4; i++) speed_min[i] = 0;
82         for(i=0; i<4; i++) speed_max[i] = 0;
83         hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
84
85         dx0 = -RDX - screendx; dx1 = RDX - screendx;
86         dy0 = -RDY - screendy; dy1 = RDY - screendy;
87
88         if(dx0 < 0) {
89                 speed_max[RIGHT] = -dx0;
90                 if(dx1 < 0) {
91                         // Rocks moving left only. So the RIGHT side of the screen
92                         speed_min[RIGHT] = -dx1;
93                         ti[RIGHT] = -(dx0+dx1)/2;
94                 } else {
95                         // Rocks moving left and right
96                         speed_max[LEFT] = dx1;
97                         ti[RIGHT] = -dx0/2;
98                         ti[LEFT] = dx1/2;
99                 }
100         } else {
101                 // Rocks moving right only. So the LEFT side of the screen
102                 speed_min[LEFT] = dx0;
103                 speed_max[LEFT] = dx1;
104                 ti[LEFT] = (dx0+dx1)/2;
105         }
106         ti[LEFT] *= hfactor;
107         ti[RIGHT] *= hfactor;
108
109         if(dy0 < 0) {
110                 speed_max[BOTTOM] = -dy0;
111                 if(dy1 < 0) {
112                         // Rocks moving up only. So the BOTTOM of the screen
113                         speed_min[BOTTOM] = -dy1;
114                         ti[BOTTOM] = -(dy0+dy1)/2;
115                 } else {
116                         // Rocks moving up and down
117                         speed_max[TOP] = dy1;
118                         ti[BOTTOM] = -dy0/2;
119                         ti[TOP] = dy1/2;
120                 }
121         } else {
122                 // Rocks moving down only. so the TOP of the screen
123                 speed_min[TOP] = dy0;
124                 speed_max[TOP] = dy1;
125                 ti[TOP] = (dy0+dy1)/2;
126         }
127         ti[TOP] *= vfactor;
128         ti[BOTTOM] *= vfactor;
129 }
130
131 float
132 weighted_rnd_range(float min, float max) {
133         return sqrt(min * min + frnd() * (max * max - min * min));
134 }
135
136 void
137 new_rocks(void)
138 {
139         int i,j;
140         float ti[4];
141         float rmin[4];
142         float rmax[4];
143
144         if(nrocks < F_ROCKS) {
145                 nrocks_timer += t_frame;
146                 if(nrocks_timer >= nrocks_inc_ticks) {
147                         nrocks_timer -= nrocks_inc_ticks;
148                         nrocks++;
149                 }
150         }
151
152         rock_sides(ti, rmin, rmax);
153
154         // loop through the four sides of the screen
155         for(i=0; i<4; i++) {
156                 // see if we generate a rock for this side this frame
157                 rtimers[i] += ti[i]*t_frame;
158                 while(rtimers[i] >= 1) {
159                         rtimers[i] -= 1;
160                         j=0;
161                         while(rockptr->active && j<MAXROCKS) {
162                                 if(++rockptr - rock >= MAXROCKS) rockptr = rock;
163                                 j++;
164                         }
165                         if(!rockptr->active) {
166                                 rockptr->type_number = random() % NROCKS;
167                                 rockptr->image = surf_rock[rockptr->type_number];
168                                 rockptr->shape = &rock_shapes[rockptr->type_number];
169                                 switch(i) {
170                                         case RIGHT:
171                                                 rockptr->x = XSIZE;
172                                                 rockptr->y = frnd()*(YSIZE + rockptr->image->h);
173
174                                                 rockptr->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
175                                                 rockptr->dy = RDY*crnd();
176                                                 break;
177                                         case LEFT:
178                                                 rockptr->x = -rockptr->image->w;
179                                                 rockptr->y = frnd()*(YSIZE + rockptr->image->h);
180
181                                                 rockptr->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
182                                                 rockptr->dy = RDY*crnd();
183                                                 break;
184                                         case BOTTOM:
185                                                 rockptr->x = frnd()*(XSIZE + rockptr->image->w);
186                                                 rockptr->y = YSIZE;
187
188                                                 rockptr->dx = RDX*crnd();
189                                                 rockptr->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
190                                                 break;
191                                         case TOP:
192                                                 rockptr->x = frnd()*(XSIZE + rockptr->image->w);
193                                                 rockptr->y = -rockptr->image->h;
194
195                                                 rockptr->dx = RDX*crnd();
196                                                 rockptr->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
197                                                 break;
198                                 }
199
200                                 rockptr->active = 1;
201                         }
202                 }
203         }
204 }
205
206 void
207 move_rocks(void)
208 {
209         int i;
210
211         // Move all the rocks
212         for(i = 0; i < MAXROCKS; i++) {
213                 if(rock[i].active) {
214                         // move
215                         rock[i].x += (rock[i].dx-screendx)*t_frame;
216                         rock[i].y += (rock[i].dy-screendy)*t_frame;
217                         // clip
218                         if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE
219                                         || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) {
220                                 rock[i].active = 0;
221                         }
222                 }
223         }
224 }
225
226 void
227 draw_rocks(void)
228 {
229         int i;
230         SDL_Rect src, dest;
231
232         src.x = 0; src.y = 0;
233
234         for(i = 0; i<MAXROCKS; i++) {
235                 if(rock[i].active) {
236                         src.w = rock[i].image->w;
237                         src.h = rock[i].image->h;
238
239                         dest.w = src.w;
240                         dest.h = src.h;
241                         dest.x = (int) rock[i].x;
242                         dest.y = (int) rock[i].y;
243
244                         SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
245
246                 }
247         }
248 }
249
250 int
251 hit_rocks(float x, float y, struct shape *shape)
252 {
253         int i;
254
255         for(i=0; i<MAXROCKS; i++) {
256                 if(rock[i].active) {
257                         if(collide(x-rock[i].x, y-rock[i].y, rock[i].shape, shape)) 
258                                 return 1;
259                 }
260         }
261         return 0;
262 }
263
264 void
265 blast_rocks(float x, float y, float radius, int onlyslow)
266 {
267         int i;
268         float dx, dy, n;
269
270         if(onlyslow) return;
271
272         for(i = 0; i<MAXROCKS; i++ ) {
273                 if(rock[i].x <= 0) continue;
274
275                 // This makes it so your explosion from dying magically doesn't leave
276                 // any rocks that aren't moving much on the x axis. If onlyslow is set,
277                 // only rocks that are barely moving will be pushed.
278                 if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue;
279
280                 dx = rock[i].x - x;
281                 dy = rock[i].y - y;
282
283                 n = sqrt(dx*dx + dy*dy);
284                 if(n < radius) {
285                         n *= 15;
286                         rock[i].dx += 54.0*dx/n;
287                         rock[i].dy += 54.0*dy/n;
288                 }
289         }
290 }