JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed the speed probabilities and direction for new rocks
[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/seccond 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 = nrocks/KH; vfactor = nrocks/KV;
86
87         dx0 = -RDX - screendx; dx1 = RDX - screendx;
88         dy0 = -RDY - screendy; dy1 = RDY - screendy;
89
90         if(dx0 != 0) {
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         }
110         ti[LEFT] *= hfactor;
111         ti[RIGHT] *= hfactor;
112
113         if(dy0 != 0) {
114                 if(dy0 < 0) {
115                         speed_max[BOTTOM] = -dy0;
116                         if(dy1 < 0) {
117                                 // Rocks moving up only. So the BOTTOM of the screen
118                                 speed_min[BOTTOM] = -dy1;
119                                 ti[BOTTOM] = -(dy0+dy1)/2;
120                         } else {
121                                 // Rocks moving up and down
122                                 speed_max[TOP] = dy1;
123                                 ti[BOTTOM] = -dy0/2;
124                                 ti[TOP] = dy1/2;
125                         }
126                 } else {
127                         // Rocks moving down only. so the TOP of the screen
128                         speed_min[TOP] = dy0;
129                         speed_max[TOP] = dy1;
130                         ti[TOP] = (dy0+dy1)/2;
131                 }
132         }
133         ti[TOP] *= vfactor;
134         ti[BOTTOM] *= vfactor;
135 }
136
137 float
138 weighted_rnd_range(float min, float max) {
139         return sqrt(min * min + rnd() * (max * max - min * min));
140 }
141
142 void
143 new_rocks(void)
144 {
145         int i,j;
146         float ti[4];
147         float rmin[4];
148         float rmax[4];
149
150         if(nrocks < F_ROCKS) {
151                 nrocks_timer += ticks_since_last;
152                 if(nrocks_timer >= nrocks_inc_ticks) {
153                         nrocks_timer -= nrocks_inc_ticks;
154                         nrocks++;
155                 }
156         }
157
158         rock_sides(ti, rmin, rmax);
159
160         // loop through the four sides of the screen
161         for(i=0; i<4; i++) {
162                 // see if we generate a rock for this side this frame
163                 rtimers[i] += ti[i]*gamerate/20;
164                 while(rtimers[i] >= 1) {
165                         rtimers[i] -= 1;
166                         j=0;
167                         while(rockptr->active && j<MAXROCKS) {
168                                 if(++rockptr - rock >= MAXROCKS) rockptr = rock;
169                                 j++;
170                         }
171                         if(!rockptr->active) {
172                                 rockptr->type_number = random() % NROCKS;
173                                 rockptr->image = surf_rock[rockptr->type_number];
174                                 rockptr->shape = &rock_shapes[rockptr->type_number];
175                                 switch(i) {
176                                         case RIGHT:
177                                                 rockptr->x = XSIZE;
178                                                 rockptr->y = rnd()*(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 LEFT:
184                                                 rockptr->x = -rockptr->image->w;
185                                                 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
186
187                                                 rockptr->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
188                                                 rockptr->dy = RDY*crnd();
189                                                 break;
190                                         case BOTTOM:
191                                                 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
192                                                 rockptr->y = YSIZE;
193
194                                                 rockptr->dx = RDY*crnd();
195                                                 rockptr->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
196                                                 break;
197                                         case TOP:
198                                                 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
199                                                 rockptr->y = -rockptr->image->h;
200
201                                                 rockptr->dx = RDY*crnd();
202                                                 rockptr->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
203                                                 break;
204                                 }
205
206                                 rockptr->active = 1;
207                         }
208                 }
209         }
210 }
211
212 void
213 move_rocks(void)
214 {
215         int i;
216
217         // Move all the rocks
218         for(i = 0; i < MAXROCKS; i++) {
219                 if(rock[i].active) {
220                         // move
221                         rock[i].x += (rock[i].dx-screendx)*gamerate;
222                         rock[i].y += (rock[i].dy-screendy)*gamerate;
223                         // clip
224                         if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE
225                                         || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) {
226                                 rock[i].active = 0;
227                         }
228                 }
229         }
230 }
231
232 void
233 draw_rocks(void)
234 {
235         int i;
236         SDL_Rect src, dest;
237
238         src.x = 0; src.y = 0;
239
240         for(i = 0; i<MAXROCKS; i++) {
241                 if(rock[i].active) {
242                         src.w = rock[i].image->w;
243                         src.h = rock[i].image->h;
244
245                         dest.w = src.w;
246                         dest.h = src.h;
247                         dest.x = (int) rock[i].x;
248                         dest.y = (int) rock[i].y;
249
250                         SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
251
252                 }
253         }
254 }
255
256 int
257 hit_rocks(float x, float y, struct shape *shape)
258 {
259         int i;
260
261         for(i=0; i<MAXROCKS; i++) {
262                 if(rock[i].active) {
263                         if(collide(x-rock[i].x, y-rock[i].y, rock[i].shape, shape)) 
264                                 return 1;
265                 }
266         }
267         return 0;
268 }
269
270 void
271 blast_rocks(float x, float y, float radius, int onlyslow)
272 {
273         int i;
274         float dx, dy, n;
275
276         if(onlyslow) return;
277
278         for(i = 0; i<MAXROCKS; i++ ) {
279                 if(rock[i].x <= 0) continue;
280
281                 // This makes it so your explosion from dying magically doesn't leave
282                 // any rocks that aren't moving much on the x axis. If onlyslow is set,
283                 // only rocks that are barely moving will be pushed.
284                 if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue;
285
286                 dx = rock[i].x - x;
287                 dy = rock[i].y - y;
288
289                 n = sqrt(dx*dx + dy*dy);
290                 if(n < radius) {
291                         n *= 15;
292                         rock[i].dx += 54.0*dx/n;
293                         rock[i].dy += 54.0*dy/n;
294                 }
295         }
296 }