JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
number of rocks on screen now starts at 25, gradually increasing
[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 void
72 rock_timer_increments(float *ti)
73 {
74         float dx0,dx1, dy0,dy1;
75         float hfactor, vfactor;
76         int i;
77
78         for(i=0; i<4; i++) ti[i] = 0;
79         hfactor = nrocks/KH; vfactor = nrocks/KV;
80
81         dx0 = -RDX - screendx; dx1 = RDX - screendx;
82         dy0 = -RDY - screendy; dy1 = RDY - screendy;
83
84         if(dx0 != 0) {
85                 if(dx0 < 0) {
86                         if(dx1 < 0) ti[RIGHT] = -(dx0+dx1)/2;
87                         else {
88                                 ti[RIGHT] = -dx0/2;
89                                 ti[LEFT] = dx1/2;
90                         }
91                 } else ti[LEFT] = (dx0+dx1)/2;
92         }
93         ti[LEFT] *= hfactor;
94         ti[RIGHT] *= hfactor;
95
96         if(dy0 != 0) {
97                 if(dy0 < 0) {
98                         if(dy1 < 0) ti[BOTTOM] = -(dy0+dy1)/2;
99                         else {
100                                 ti[BOTTOM] = -dy0/2;
101                                 ti[TOP] = dy1/2;
102                         }
103                 } else ti[TOP] = (dy0+dy1)/2;
104         }
105         ti[TOP] *= vfactor;
106         ti[BOTTOM] *= vfactor;
107 }
108
109 void
110 new_rocks(void)
111 {
112         int i,j;
113         float ti[4];
114
115         if(nrocks < F_ROCKS) {
116                 nrocks_timer += ticks_since_last;
117                 if(nrocks_timer >= nrocks_inc_ticks) {
118                         nrocks_timer -= nrocks_inc_ticks;
119                         nrocks++;
120                 }
121         }
122
123         rock_timer_increments(ti);
124
125         for(i=0; i<4; i++) {
126                 rtimers[i] += ti[i]*gamerate/20;
127                 if(rtimers[i] >= 1) {
128                         j=0;
129                         while(rockptr->active && j<MAXROCKS) {
130                                 if(++rockptr - rock >= MAXROCKS) rockptr = rock;
131                                 j++;
132                         }
133                         if(!rockptr->active) {
134                                 rtimers[i] -= 1;
135                                 rockptr->type_number = random() % NROCKS;
136                                 rockptr->image = surf_rock[rockptr->type_number];
137                                 rockptr->shape = &rock_shapes[rockptr->type_number];
138                                 switch(i) {
139                                         case RIGHT:
140                                                 rockptr->x = XSIZE;
141                                                 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
142                                                 break;
143                                         case LEFT:
144                                                 rockptr->x = -rockptr->image->w;
145                                                 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
146                                                 break;
147                                         case BOTTOM:
148                                                 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
149                                                 rockptr->y = YSIZE;
150                                                 break;
151                                         case TOP:
152                                                 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
153                                                 rockptr->y = -rockptr->image->h;
154                                                 break;
155                                 }
156
157                                 rockptr->dx = RDX*crnd();
158                                 rockptr->dy = RDY*crnd();
159
160                                 rockptr->active = 1;
161                         }
162                 }
163         }
164 }
165
166 void
167 move_rocks(void)
168 {
169         int i;
170
171         // Move all the rocks
172         for(i = 0; i < MAXROCKS; i++) {
173                 if(rock[i].active) {
174                         // move
175                         rock[i].x += (rock[i].dx-screendx)*gamerate;
176                         rock[i].y += (rock[i].dy-screendy)*gamerate;
177                         // clip
178                         if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE
179                                         || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) {
180                                 rock[i].active = 0;
181                         }
182                 }
183         }
184 }
185
186 void
187 draw_rocks(void)
188 {
189         int i;
190         SDL_Rect src, dest;
191
192         src.x = 0; src.y = 0;
193
194         for(i = 0; i<MAXROCKS; i++) {
195                 if(rock[i].active) {
196                         src.w = rock[i].image->w;
197                         src.h = rock[i].image->h;
198
199                         dest.w = src.w;
200                         dest.h = src.h;
201                         dest.x = (int) rock[i].x;
202                         dest.y = (int) rock[i].y;
203
204                         SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
205
206                 }
207         }
208 }
209
210 int
211 hit_rocks(float x, float y, struct shape *shape)
212 {
213         int i;
214
215         for(i=0; i<MAXROCKS; i++) {
216                 if(rock[i].active) {
217                         if(collide(x-rock[i].x, y-rock[i].y, rock[i].shape, shape)) 
218                                 return 1;
219                 }
220         }
221         return 0;
222 }
223
224 void
225 blast_rocks(float x, float y, float radius, int onlyslow)
226 {
227         int i;
228         float dx, dy, n;
229
230         if(onlyslow) return;
231
232         for(i = 0; i<MAXROCKS; i++ ) {
233                 if(rock[i].x <= 0) continue;
234
235                 // This makes it so your explosion from dying magically doesn't leave
236                 // any rocks that aren't moving much on the x axis. If onlyslow is set,
237                 // only rocks that are barely moving will be pushed.
238                 if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue;
239
240                 dx = rock[i].x - x;
241                 dy = rock[i].y - y;
242
243                 n = sqrt(dx*dx + dy*dy);
244                 if(n < radius) {
245                         n *= 15;
246                         rock[i].dx += 54.0*dx/n;
247                         rock[i].dy += 54.0*dy/n;
248                 }
249         }
250 }