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