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