JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
a594547e484d1252606f18483c82acdba49a6842
[vor.git] / rocks.c
1 #include <SDL_image.h>
2 #include <math.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "common.h"
7 #include "config.h"
8 #include "file.h"
9 #include "globals.h"
10 #include "mt.h"
11 #include "rocks.h"
12 #include "shape.h"
13
14 SDL_Surface *load_image(char *filename);
15
16 struct rock_struct {
17         struct rock_struct *next;
18         float x,y,dx,dy;
19         SDL_Surface *image;
20         struct shape *shape;
21         int type_number;
22 }; 
23
24 struct rock_struct rocks[MAXROCKS], *free_rocks;
25
26 struct rock_struct **rock_buckets[2];
27 int n_buckets;
28 // we have two sets of buckets -- this variable tells which we are using.
29 int p;
30 int bw, bh;
31 int grid_size;
32
33 SDL_Surface *surf_rock[NROCKS];
34 struct shape rock_shapes[NROCKS];
35
36 // timers for rock generation.
37 float rtimers[4];
38
39 uint32_t nrocks;
40 float nrocks_timer;
41 float nrocks_inc_ticks = 2*60*20/(F_ROCKS-I_ROCKS);
42
43 // constants for rock generation.
44 #define KH (32*20)  // 32 s for a speed=1 rock to cross the screen horizontally.
45 #define KV (24*20)  // 24 s for a speed=1 rock to cross the screen vertically.
46 #define RDX 2.5  // range for rock dx values (+/-)
47 #define RDY 2.5  // range for rock dy values (+/-)
48
49 static inline struct rock_struct **
50 bucket(int x, int y, int p)
51 {
52         int b = (1+x/grid_size) + bw*(1+y/grid_size);
53         return &rock_buckets[p][b];
54 }
55
56 void
57 init_buckets(void)
58 {
59         bw = (XSIZE+2*grid_size-1) / grid_size;
60         bh = (YSIZE+2*grid_size-1) / grid_size;
61         n_buckets = bw * bh;
62         
63         rock_buckets[0] = malloc(n_buckets * sizeof(struct rock_struct *));
64         rock_buckets[1] = malloc(n_buckets * sizeof(struct rock_struct *));
65         if(!rock_buckets[0] || !rock_buckets[1]) {
66                 fprintf(stderr, "Can't allocate rock buckets.\n");
67                 exit(1);
68         }
69         p = 0;
70 }
71
72 void
73 transfer_rock(struct rock_struct *r, struct rock_struct **from, struct rock_struct **to)
74 {
75         *from = r->next;
76         r->next = *to;
77         *to = r;
78 }
79
80 void
81 reset_rocks(void)
82 {
83         int i;
84
85         for(i=0; i<MAXROCKS; i++) rocks[i].image = NULL;
86         rocks[0].next = NULL; free_rocks = &rocks[MAXROCKS-1];
87         for(i = 1; i<MAXROCKS; i++) rocks[i].next = &rocks[i-1];
88         for(i = 0; i<n_buckets; i++) {
89                 rock_buckets[0][i] = NULL;
90                 rock_buckets[1][i] = NULL;
91         }
92
93         nrocks = I_ROCKS;
94         nrocks_timer = 0;
95 }
96
97 #define ROCK_LEN sizeof("sprites/rockXX.png")
98
99 int
100 init_rocks(void)
101 {
102         int i;
103         char a[ROCK_LEN];
104         int maxw=0, maxh=0;
105
106         for(i = 0; i<NROCKS; i++) {
107                 snprintf(a, ROCK_LEN, "sprites/rock%02d.png", i);
108                 NULLERROR(surf_rock[i] = load_image(a));
109                 get_shape(surf_rock[i], &rock_shapes[i]);
110                 maxw = max(maxw, rock_shapes[i].w);
111                 maxh = max(maxh, rock_shapes[i].h);
112         }
113         grid_size = max(maxw, maxh) * 3 / 2;
114         init_buckets();
115         reset_rocks();
116         return 0;
117 }
118
119 enum { LEFT, RIGHT, TOP, BOTTOM };
120
121
122 // compute the number of rocks/tick that should be coming from each side,
123 // and the speed ranges of rocks coming from each side
124 void
125 rock_sides(float *ti, float *speed_min, float *speed_max)
126 {
127         float dx0,dx1, dy0,dy1;
128         float hfactor, vfactor;
129         int i;
130
131         for(i=0; i<4; i++) ti[i] = 0;
132         for(i=0; i<4; i++) speed_min[i] = 0;
133         for(i=0; i<4; i++) speed_max[i] = 0;
134         hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
135
136         dx0 = -RDX - screendx; dx1 = RDX - screendx;
137         dy0 = -RDY - screendy; dy1 = RDY - screendy;
138
139         if(dx0 < 0) {
140                 speed_max[RIGHT] = -dx0;
141                 if(dx1 < 0) {
142                         // Rocks moving left only. So the RIGHT side of the screen
143                         speed_min[RIGHT] = -dx1;
144                         ti[RIGHT] = -(dx0+dx1)/2;
145                 } else {
146                         // Rocks moving left and right
147                         speed_max[LEFT] = dx1;
148                         ti[RIGHT] = -dx0/2;
149                         ti[LEFT] = dx1/2;
150                 }
151         } else {
152                 // Rocks moving right only. So the LEFT side of the screen
153                 speed_min[LEFT] = dx0;
154                 speed_max[LEFT] = dx1;
155                 ti[LEFT] = (dx0+dx1)/2;
156         }
157         ti[LEFT] *= hfactor;
158         ti[RIGHT] *= hfactor;
159
160         if(dy0 < 0) {
161                 speed_max[BOTTOM] = -dy0;
162                 if(dy1 < 0) {
163                         // Rocks moving up only. So the BOTTOM of the screen
164                         speed_min[BOTTOM] = -dy1;
165                         ti[BOTTOM] = -(dy0+dy1)/2;
166                 } else {
167                         // Rocks moving up and down
168                         speed_max[TOP] = dy1;
169                         ti[BOTTOM] = -dy0/2;
170                         ti[TOP] = dy1/2;
171                 }
172         } else {
173                 // Rocks moving down only. so the TOP of the screen
174                 speed_min[TOP] = dy0;
175                 speed_max[TOP] = dy1;
176                 ti[TOP] = (dy0+dy1)/2;
177         }
178         ti[TOP] *= vfactor;
179         ti[BOTTOM] *= vfactor;
180 }
181
182 float
183 weighted_rnd_range(float min, float max) {
184         return sqrt(min * min + frnd() * (max * max - min * min));
185 }
186
187 void
188 new_rocks(void)
189 {
190         int i;
191         struct rock_struct *r;
192         float ti[4];
193         float rmin[4];
194         float rmax[4];
195
196         if(nrocks < F_ROCKS) {
197                 nrocks_timer += t_frame;
198                 if(nrocks_timer >= nrocks_inc_ticks) {
199                         nrocks_timer -= nrocks_inc_ticks;
200                         nrocks++;
201                 }
202         }
203
204         rock_sides(ti, rmin, rmax);
205
206         // increment timers
207         for(i=0; i<4; i++) rtimers[i] += ti[i]*t_frame;
208
209         // generate rocks
210         for(i=0; i<4; i++) {
211                 while(rtimers[i] >= 1) {
212                         rtimers[i] -= 1;
213                         if(!free_rocks) return;  // sorry, we ran out of rocks!
214                         r = free_rocks;
215                         r->type_number = urnd() % NROCKS;
216                         r->image = surf_rock[r->type_number];
217                         r->shape = &rock_shapes[r->type_number];
218                         switch(i) {
219                                 case RIGHT:
220                                         r->x = XSIZE;
221                                         r->y = frnd()*(YSIZE + r->image->h);
222
223                                         r->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
224                                         r->dy = RDY*crnd();
225                                         break;
226                                 case LEFT:
227                                         r->x = -r->image->w;
228                                         r->y = frnd()*(YSIZE + r->image->h);
229
230                                         r->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
231                                         r->dy = RDY*crnd();
232                                         break;
233                                 case BOTTOM:
234                                         r->x = frnd()*(XSIZE + r->image->w);
235                                         r->y = YSIZE;
236
237                                         r->dx = RDX*crnd();
238                                         r->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
239                                         break;
240                                 case TOP:
241                                         r->x = frnd()*(XSIZE + r->image->w);
242                                         r->y = -r->image->h;
243
244                                         r->dx = RDX*crnd();
245                                         r->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
246                                         break;
247                         }
248                         transfer_rock(r, &free_rocks, bucket(r->x, r->y, p));
249                 }
250         }
251 }
252
253 void
254 move_rocks(void)
255 {
256         int b;
257         struct rock_struct **head;
258         struct rock_struct *r;
259
260         // Move all the rocks
261         for(b=0; b<n_buckets; b++) {
262                 head=&rock_buckets[p][b]; r=*head;
263                 while(*head) {
264                         r=*head;
265
266                         // move
267                         r->x += (r->dx - screendx)*t_frame;
268                         r->y += (r->dy - screendy)*t_frame;
269
270                         // clip or resort into other bucket set
271                         // (either way we move it out of this list).
272                         if(r->x + r->image->w < 0 || r->x >= XSIZE
273                                         || r->y + r->image->h < 0 || r->y >= YSIZE) {
274                                 transfer_rock(r, head, &free_rocks);
275                                 r->image = NULL;
276                         } else transfer_rock(r, head, bucket(r->x, r->y, 1-p));
277                 }
278         }
279         p = 1-p;  // switch current set of buckets.
280 }
281
282 void
283 draw_rocks(void)
284 {
285         int i;
286         SDL_Rect dest;
287
288         for(i=0; i<MAXROCKS; i++) {
289                 if(!rocks[i].image) continue;
290                 dest.x = rocks[i].x; dest.y = rocks[i].y;
291                 SDL_BlitSurface(rocks[i].image,NULL,surf_screen,&dest);
292         }
293 }
294
295 int
296 hit_in_bucket(struct rock_struct *r, float x, float y, struct shape *shape)
297 {
298         for(; r; r=r->next) {
299                 if(collide(x - r->x, y - r->y, r->shape, shape)) return 1;
300         }
301         return 0;
302 }
303
304 int
305 hit_rocks(float x, float y, struct shape *shape)
306 {
307         struct rock_struct **b = bucket(x, y, p);
308         int bdx = ((int)x+shape->w)/grid_size - (int)x/grid_size;
309         int bdy = ((int)y+shape->h)/grid_size - (int)y/grid_size;
310         if(hit_in_bucket(*b, x, y, shape)) return 1;
311         if(hit_in_bucket(*(b-1), x, y, shape)) return 1;
312         if(hit_in_bucket(*(b-bw), x, y, shape)) return 1;
313         if(hit_in_bucket(*(b-bw-1), x, y, shape)) return 1;
314
315         if(bdx) {
316                 if(hit_in_bucket(*(b+1), x, y, shape)) return 1;
317                 if(hit_in_bucket(*(b+1-bw), x, y, shape)) return 1;
318         }
319         if(bdy) {
320                 if(hit_in_bucket(*(b+bw), x, y, shape)) return 1;
321                 if(hit_in_bucket(*(b+bw-1), x, y, shape)) return 1;
322         }
323         if(bdx && bdy && hit_in_bucket(*(b+bw+1), x, y, shape)) return 1;
324         return 0;
325 }
326
327 int
328 pixel_hit_in_bucket(struct rock_struct *r, float x, float y)
329 {
330         for(; r; r=r->next) {
331                 if(x < r->x || y < r->y) continue;
332                 if(pixel_collide(x - r->x, y - r->y, r->shape)) return 1;
333         }
334         return 0;
335 }
336
337 int
338 pixel_hit_rocks(float x, float y)
339 {
340         struct rock_struct **b = bucket(x, y, p);
341         if(pixel_hit_in_bucket(*b, x, y)) return 1;
342         if(pixel_hit_in_bucket(*(b-1), x, y)) return 1;
343         if(pixel_hit_in_bucket(*(b-bw), x, y)) return 1;
344         if(pixel_hit_in_bucket(*(b-bw-1), x, y)) return 1;
345         return 0;
346 }
347
348 void
349 blast_rocks(float x, float y, float radius, int onlyslow)
350 {
351         int b;
352         struct rock_struct *r;
353         float dx, dy, n;
354
355         if(onlyslow) return;
356
357         for(b=0; b<n_buckets; b++) {
358                 for(r=rock_buckets[p][b]; r; r=r->next) {
359                         if(r->x <= 0) continue;
360
361                         // This makes it so your explosion from dying magically doesn't leave
362                         // any rocks that aren't moving much on the x axis. If onlyslow is set,
363                         // only rocks that are barely moving will be pushed.
364                         if(onlyslow && (r->dx - screendx < -4 || r->dx - screendx > 3)) continue;
365
366                         dx = r->x - x;
367                         dy = r->y - y;
368
369                         n = sqrt(dx*dx + dy*dy);
370                         if(n < radius) {
371                                 n *= 15;
372                                 r->dx += 54.0*dx/n;
373                                 r->dy += 54.0*dy/n;
374                         }
375                 }
376         }
377 }