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