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