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