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