JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
oops, I wasn't using nrocks in my timing calculations. doh\!
[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 int nrocks;
28
29 // constants for rock generation.
30 #define KH 32.0  // 32 s for a speed=1 rock to cross the screen horizontally.
31 #define KV 24.0  // 24 s for a speed=1 rock to cross the screen vertically.
32 #define RDX 2.5  // range for rock dx values (+/-)
33 #define RDY 2.5  // range for rock dy values (+/-)
34
35 float rnd(void);
36
37 #define crnd() (2*(rnd()-0.5))
38
39
40 int
41 init_rocks(void)
42 {
43         int i;
44         char a[MAX_PATH_LEN];
45         SDL_Surface *temp;
46
47         for(i = 0; i<NROCKS; i++) {
48                 snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
49                 NULLERROR(temp = IMG_Load(a));
50                 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
51                 get_shape(surf_rock[i], &rock_shapes[i]);
52         }
53         nrocks = 41;
54         return 0;
55 }
56
57 void
58 reset_rocks(void)
59 {
60         int i;
61
62         for(i = 0; i<MAXROCKS; i++) rock[i].active = 0;
63 }
64
65 enum { LEFT, RIGHT, TOP, BOTTOM };
66
67 void
68 rock_timer_increments(float *ti)
69 {
70         float dx0,dx1, dy0,dy1;
71         float hfactor, vfactor;
72         int i;
73
74         for(i=0; i<4; i++) ti[i] = 0;
75         hfactor = nrocks/KH; vfactor = nrocks/KV;
76
77         dx0 = -RDX - screendx; dx1 = RDX - screendx;
78         dy0 = -RDY - screendy; dy1 = RDY - screendy;
79
80         if(dx0 != 0) {
81                 if(dx0 < 0) {
82                         if(dx1 < 0) ti[RIGHT] = -(dx0+dx1)/2;
83                         else {
84                                 ti[RIGHT] = -dx0/2;
85                                 ti[LEFT] = dx1/2;
86                         }
87                 } else ti[LEFT] = (dx0+dx1)/2;
88         }
89         ti[LEFT] *= hfactor;
90         ti[RIGHT] *= hfactor;
91
92         if(dy0 != 0) {
93                 if(dy0 < 0) {
94                         if(dy1 < 0) ti[BOTTOM] = -(dy0+dy1)/2;
95                         else {
96                                 ti[BOTTOM] = -dy0/2;
97                                 ti[TOP] = dy1/2;
98                         }
99                 } else ti[TOP] = (dy0+dy1)/2;
100         }
101         ti[TOP] *= vfactor;
102         ti[BOTTOM] *= vfactor;
103 }
104
105 void
106 new_rocks(void)
107 {
108         int i,j;
109         float ti[4];
110         float x, y;
111
112         rock_timer_increments(ti);
113
114         for(i=0; i<4; i++) {
115                 rtimers[i] += ti[i]*gamerate/20;
116                 if(rtimers[i] >= 1) {
117                         j=0;
118                         while(rockptr->active && j<MAXROCKS) {
119                                 if(++rockptr - rock >= MAXROCKS) rockptr = rock;
120                                 j++;
121                         }
122                         if(!rockptr->active) {
123                                 rtimers[i] -= 1;
124                                 rockptr->type_number = random() % NROCKS;
125                                 rockptr->image = surf_rock[rockptr->type_number];
126                                 rockptr->shape = &rock_shapes[rockptr->type_number];
127                                 switch(i) {
128                                         case RIGHT:
129                                                 rockptr->x = XSIZE;
130                                                 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
131                                                 break;
132                                         case LEFT:
133                                                 rockptr->x = -rockptr->image->w;
134                                                 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
135                                                 break;
136                                         case BOTTOM:
137                                                 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
138                                                 rockptr->y = YSIZE;
139                                                 break;
140                                         case TOP:
141                                                 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
142                                                 rockptr->y = -rockptr->image->h;
143                                                 break;
144                                 }
145
146                                 j=0;
147                                 do {
148                                         rockptr->dx = RDX*crnd();
149                                         rockptr->dy = RDY*crnd();
150                                         x = (rockptr->dx-screendx)*gamerate;
151                                         y = (rockptr->dy-screendy)*gamerate;
152                                         j++;
153                                 } while(x < -rockptr->image->w || x >= XSIZE
154                                                 || y < -rockptr->image->h || y >= YSIZE);
155                                 if(j > 1) fprintf(stderr, "had to try %d times.\n", j);
156
157                                 rockptr->active = 1;
158                         }
159                 }
160         }
161 }
162
163 void
164 move_rocks(void)
165 {
166         int i;
167
168         // Move all the rocks
169         for(i = 0; i < MAXROCKS; i++) {
170                 if(rock[i].active) {
171                         // move
172                         rock[i].x += (rock[i].dx-screendx)*gamerate;
173                         rock[i].y += (rock[i].dy-screendy)*gamerate;
174                         // clip
175                         if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE
176                                         || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) {
177                                 rock[i].active = 0;
178                         }
179                 }
180         }
181 }
182
183 void
184 draw_rocks(void)
185 {
186         int i;
187         SDL_Rect src, dest;
188
189         src.x = 0; src.y = 0;
190
191         for(i = 0; i<MAXROCKS; i++) {
192                 if(rock[i].active) {
193                         src.w = rock[i].image->w;
194                         src.h = rock[i].image->h;
195
196                         dest.w = src.w;
197                         dest.h = src.h;
198                         dest.x = (int) rock[i].x;
199                         dest.y = (int) rock[i].y;
200
201                         SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
202
203                 }
204         }
205 }
206
207 int
208 hit_rocks(float x, float y, struct shape *shape)
209 {
210         int i;
211
212         for(i=0; i<MAXROCKS; i++) {
213                 if(rock[i].active) {
214                         if(collide(x-rock[i].x, y-rock[i].y, rock[i].shape, shape)) 
215                                 return 1;
216                 }
217         }
218         return 0;
219 }
220
221 void
222 blast_rocks(float x, float y, float radius, int onlyslow)
223 {
224         int i;
225         float dx, dy, n;
226
227         for(i = 0; i<MAXROCKS; i++ ) {
228                 if(rock[i].x <= 0) continue;
229
230                 // This makes it so your explosion from dying magically doesn't leave
231                 // any rocks that aren't moving much on the x axis. If onlyslow is set,
232                 // only rocks that are barely moving will be pushed.
233                 if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue;
234
235                 dx = rock[i].x - x;
236                 dy = rock[i].y - y;
237
238                 n = sqrt(dx*dx + dy*dy);
239                 if(n < radius) {
240                         n *= 20;
241                         rock[i].dx += 54.0*(dx+30)/n;
242                         rock[i].dy += 54.0*dy/n;
243                 }
244         }
245 }