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