JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added constant MAX_DUST_DEPTHS.
[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
111         rock_timer_increments(ti);
112
113         for(i=0; i<4; i++) {
114                 rtimers[i] += ti[i]*gamerate/20;
115                 if(rtimers[i] >= 1) {
116                         j=0;
117                         while(rockptr->active && j<MAXROCKS) {
118                                 if(++rockptr - rock >= MAXROCKS) rockptr = rock;
119                                 j++;
120                         }
121                         if(!rockptr->active) {
122                                 rtimers[i] -= 1;
123                                 rockptr->type_number = random() % NROCKS;
124                                 rockptr->image = surf_rock[rockptr->type_number];
125                                 rockptr->shape = &rock_shapes[rockptr->type_number];
126                                 switch(i) {
127                                         case RIGHT:
128                                                 rockptr->x = XSIZE;
129                                                 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
130                                                 break;
131                                         case LEFT:
132                                                 rockptr->x = -rockptr->image->w;
133                                                 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
134                                                 break;
135                                         case BOTTOM:
136                                                 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
137                                                 rockptr->y = YSIZE;
138                                                 break;
139                                         case TOP:
140                                                 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
141                                                 rockptr->y = -rockptr->image->h;
142                                                 break;
143                                 }
144
145                                 rockptr->dx = RDX*crnd();
146                                 rockptr->dy = RDY*crnd();
147
148                                 rockptr->active = 1;
149                         }
150                 }
151         }
152 }
153
154 void
155 move_rocks(void)
156 {
157         int i;
158
159         // Move all the rocks
160         for(i = 0; i < MAXROCKS; i++) {
161                 if(rock[i].active) {
162                         // move
163                         rock[i].x += (rock[i].dx-screendx)*gamerate;
164                         rock[i].y += (rock[i].dy-screendy)*gamerate;
165                         // clip
166                         if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE
167                                         || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) {
168                                 rock[i].active = 0;
169                         }
170                 }
171         }
172 }
173
174 void
175 draw_rocks(void)
176 {
177         int i;
178         SDL_Rect src, dest;
179
180         src.x = 0; src.y = 0;
181
182         for(i = 0; i<MAXROCKS; i++) {
183                 if(rock[i].active) {
184                         src.w = rock[i].image->w;
185                         src.h = rock[i].image->h;
186
187                         dest.w = src.w;
188                         dest.h = src.h;
189                         dest.x = (int) rock[i].x;
190                         dest.y = (int) rock[i].y;
191
192                         SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
193
194                 }
195         }
196 }
197
198 int
199 hit_rocks(float x, float y, struct shape *shape)
200 {
201         int i;
202
203         for(i=0; i<MAXROCKS; i++) {
204                 if(rock[i].active) {
205                         if(collide(x-rock[i].x, y-rock[i].y, rock[i].shape, shape)) 
206                                 return 1;
207                 }
208         }
209         return 0;
210 }
211
212 void
213 blast_rocks(float x, float y, float radius, int onlyslow)
214 {
215         int i;
216         float dx, dy, n;
217
218         for(i = 0; i<MAXROCKS; i++ ) {
219                 if(rock[i].x <= 0) continue;
220
221                 // This makes it so your explosion from dying magically doesn't leave
222                 // any rocks that aren't moving much on the x axis. If onlyslow is set,
223                 // only rocks that are barely moving will be pushed.
224                 if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue;
225
226                 dx = rock[i].x - x;
227                 dy = rock[i].y - y;
228
229                 n = sqrt(dx*dx + dy*dy);
230                 if(n < radius) {
231                         n *= 20;
232                         rock[i].dx += 54.0*dx/n;
233                         rock[i].dy += 54.0*dy/n;
234                 }
235         }
236 }