JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
ditched the separate shape struct.
[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 static struct rock prototypes[NROCKS];
23
24 // timers for rock generation.
25 static float rtimers[4];
26
27 uint32_t nrocks;
28 float nrocks_timer;
29 float nrocks_inc_ticks = 2*60*20/(F_ROCKS-I_ROCKS);
30
31 // constants for rock generation.
32 #define KH (32*20)  // 32 s for a speed=1 rock to cross the screen horizontally.
33 #define KV (24*20)  // 24 s for a speed=1 rock to cross the screen vertically.
34 #define RDX 2.5  // range for rock dx values (+/-)
35 #define RDY 2.5  // range for rock dy values (+/-)
36
37 static inline struct rock **
38 bucket(int x, int y, int p)
39 {
40         int b = (x+grid_size)/grid_size + bw*((y+grid_size)/grid_size);
41         return &rock_buckets[p][b];
42 }
43
44 void
45 init_buckets(void)
46 {
47         int scr_grid_w = (XSIZE+2*grid_size-1) / grid_size;
48         int scr_grid_h = (YSIZE+2*grid_size-1) / grid_size;
49         bw = 1 + scr_grid_w + 1;
50         bh = 1 + scr_grid_h + 1;
51         n_buckets = bw * bh;
52         
53         rock_buckets[0] = malloc(n_buckets * sizeof(struct rock *));
54         rock_buckets[1] = malloc(n_buckets * sizeof(struct rock *));
55         if(!rock_buckets[0] || !rock_buckets[1]) {
56                 fprintf(stderr, "Can't allocate rock buckets.\n");
57                 exit(1);
58         }
59         p = 0;
60 }
61
62 void
63 transfer_rock(struct rock *r, struct rock **from, struct rock **to)
64 {
65         *from = r->next;
66         r->next = *to;
67         *to = r;
68 }
69
70 void
71 reset_rocks(void)
72 {
73         int i;
74
75         for(i=0; i<MAXROCKS; i++) rocks[i].image = NULL;
76
77         rocks[0].next = NULL;
78         for(i=1; i<MAXROCKS; i++) rocks[i].next = &rocks[i-1];
79         free_rocks = &rocks[MAXROCKS-1];
80
81         for(i = 0; i<n_buckets; i++) {
82                 rock_buckets[0][i] = NULL;
83                 rock_buckets[1][i] = NULL;
84         }
85
86         nrocks = I_ROCKS;
87         nrocks_timer = 0;
88 }
89
90 #define ROCK_LEN sizeof("sprites/rockXX.png")
91
92 int
93 init_rocks(void)
94 {
95         int i;
96         char a[ROCK_LEN];
97         int maxw=0, maxh=0;
98
99         for(i = 0; i<NROCKS; i++) {
100                 snprintf(a, ROCK_LEN, "sprites/rock%02d.png", i);
101                 load_sprite(SPRITE(&prototypes[i]), a);
102                 maxw = max(maxw, prototypes[i].w);
103                 maxh = max(maxh, prototypes[i].h);
104         }
105         grid_size = max(maxw, maxh) * 3 / 2;
106         init_buckets();
107         reset_rocks();
108         return 0;
109 }
110
111 enum { LEFT, RIGHT, TOP, BOTTOM };
112
113
114 // compute the number of rocks/tick that should be coming from each side,
115 // and the speed ranges of rocks coming from each side
116 void
117 rock_sides(float *ti, float *speed_min, float *speed_max)
118 {
119         float dx0,dx1, dy0,dy1;
120         float hfactor, vfactor;
121         int i;
122
123         for(i=0; i<4; i++) ti[i] = 0;
124         for(i=0; i<4; i++) speed_min[i] = 0;
125         for(i=0; i<4; i++) speed_max[i] = 0;
126         hfactor = (float)nrocks/KH; vfactor = (float)nrocks/KV;
127
128         dx0 = -RDX - screendx; dx1 = RDX - screendx;
129         dy0 = -RDY - screendy; dy1 = RDY - screendy;
130
131         if(dx0 < 0) {
132                 speed_max[RIGHT] = -dx0;
133                 if(dx1 < 0) {
134                         // Rocks moving left only. So the RIGHT side of the screen
135                         speed_min[RIGHT] = -dx1;
136                         ti[RIGHT] = -(dx0+dx1)/2;
137                 } else {
138                         // Rocks moving left and right
139                         speed_max[LEFT] = dx1;
140                         ti[RIGHT] = -dx0/2;
141                         ti[LEFT] = dx1/2;
142                 }
143         } else {
144                 // Rocks moving right only. So the LEFT side of the screen
145                 speed_min[LEFT] = dx0;
146                 speed_max[LEFT] = dx1;
147                 ti[LEFT] = (dx0+dx1)/2;
148         }
149         ti[LEFT] *= hfactor;
150         ti[RIGHT] *= hfactor;
151
152         if(dy0 < 0) {
153                 speed_max[BOTTOM] = -dy0;
154                 if(dy1 < 0) {
155                         // Rocks moving up only. So the BOTTOM of the screen
156                         speed_min[BOTTOM] = -dy1;
157                         ti[BOTTOM] = -(dy0+dy1)/2;
158                 } else {
159                         // Rocks moving up and down
160                         speed_max[TOP] = dy1;
161                         ti[BOTTOM] = -dy0/2;
162                         ti[TOP] = dy1/2;
163                 }
164         } else {
165                 // Rocks moving down only. so the TOP of the screen
166                 speed_min[TOP] = dy0;
167                 speed_max[TOP] = dy1;
168                 ti[TOP] = (dy0+dy1)/2;
169         }
170         ti[TOP] *= vfactor;
171         ti[BOTTOM] *= vfactor;
172 }
173
174 float
175 weighted_rnd_range(float min, float max) {
176         return sqrt(min * min + frnd() * (max * max - min * min));
177 }
178
179 void
180 new_rocks(void)
181 {
182         int i, type;
183         struct rock *r, **tmp;
184         float ti[4];
185         float rmin[4];
186         float rmax[4];
187
188         if(nrocks < F_ROCKS) {
189                 nrocks_timer += t_frame;
190                 if(nrocks_timer >= nrocks_inc_ticks) {
191                         nrocks_timer -= nrocks_inc_ticks;
192                         nrocks++;
193                 }
194         }
195
196         rock_sides(ti, rmin, rmax);
197
198         // increment timers
199         for(i=0; i<4; i++) rtimers[i] += ti[i]*t_frame;
200
201         // generate rocks
202         for(i=0; i<4; i++) {
203                 while(rtimers[i] >= 1) {
204                         rtimers[i] -= 1;
205                         if(!free_rocks) return;  // sorry, we ran out of rocks!
206                         r = free_rocks; free_rocks = r->next;
207                         type = urnd() % NROCKS;
208                         *r = prototypes[type];
209                         r->type = type;
210                         switch(i) {
211                                 case RIGHT:
212                                         r->x = XSIZE;
213                                         r->y = frnd()*(YSIZE + r->image->h);
214
215                                         r->dx = -weighted_rnd_range(rmin[i], rmax[i]) + screendx;
216                                         r->dy = RDY*crnd();
217                                         break;
218                                 case LEFT:
219                                         r->x = -r->image->w;
220                                         r->y = frnd()*(YSIZE + r->image->h);
221
222                                         r->dx = weighted_rnd_range(rmin[i], rmax[i]) + screendx;
223                                         r->dy = RDY*crnd();
224                                         break;
225                                 case BOTTOM:
226                                         r->x = frnd()*(XSIZE + r->image->w);
227                                         r->y = YSIZE;
228
229                                         r->dx = RDX*crnd();
230                                         r->dy = -weighted_rnd_range(rmin[i], rmax[i]) + screendy;
231                                         break;
232                                 case TOP:
233                                         r->x = frnd()*(XSIZE + r->image->w);
234                                         r->y = -r->image->h;
235
236                                         r->dx = RDX*crnd();
237                                         r->dy = weighted_rnd_range(rmin[i], rmax[i]) + screendy;
238                                         break;
239                         }
240                         tmp = bucket(r->x, r->y, p);
241                         r->next = *tmp; *tmp = r;
242                 }
243         }
244 }
245
246 void
247 move_rocks(void)
248 {
249         int b;
250         struct rock **head;
251         struct rock *r;
252
253         // Move all the rocks
254         for(b=0; b<n_buckets; b++) {
255                 head=&rock_buckets[p][b]; r=*head;
256                 while(*head) {
257                         r=*head;
258
259                         // move
260                         r->x += (r->dx - screendx)*t_frame;
261                         r->y += (r->dy - screendy)*t_frame;
262
263                         // clip it, or sort it into the other bucket set
264                         // (either way we move it out of this list).
265                         if(r->x + r->image->w < 0 || r->x >= XSIZE
266                                         || r->y + r->image->h < 0 || r->y >= YSIZE) {
267                                 transfer_rock(r, head, &free_rocks);
268                                 r->image = NULL;
269                         } else transfer_rock(r, head, bucket(r->x, r->y, 1-p));
270                 }
271         }
272         p = 1-p;  // switch current set of buckets.
273 }
274
275 void
276 draw_rocks(void)
277 {
278         int i;
279         SDL_Rect dest;
280
281         for(i=0; i<MAXROCKS; i++) {
282                 if(!rocks[i].image) continue;
283                 dest.x = rocks[i].x; dest.y = rocks[i].y;
284                 SDL_BlitSurface(rocks[i].image,NULL,surf_screen,&dest);
285         }
286 }
287
288 int
289 hit_in_bucket(struct rock *r, Sprite *s)
290 {
291         for(; r; r=r->next) {
292                 if(collide(SPRITE(r), s)) return true;
293         }
294         return false;
295 }
296
297 int
298 hit_rocks(Sprite *s)
299 {
300         int l, r, t, b;
301         struct rock **bucket;
302
303         l = (s->x + grid_size) / grid_size;
304         r = (s->x + s->w + grid_size) / grid_size;
305         t = (s->y + grid_size) / grid_size;
306         b = (s->y + s->h + grid_size) / grid_size;
307         bucket = &rock_buckets[p][l + t*bw];
308
309         if(hit_in_bucket(*bucket, s)) return true;
310         if(l > 0 && hit_in_bucket(*(bucket-1), s)) return true;
311         if(t > 0 && hit_in_bucket(*(bucket-bw), s)) return true;
312         if(l > 0 && t > 0 && hit_in_bucket(*(bucket-1-bw), s)) return true;
313
314         if(r > l) {
315                 if(hit_in_bucket(*(bucket+1), s)) return true;
316                 if(t > 0 && hit_in_bucket(*(bucket+1-bw), s)) return true;
317         }
318         if(b > t) {
319                 if(hit_in_bucket(*(bucket+bw), s)) return true;
320                 if(l > 0 && hit_in_bucket(*(bucket-1+bw), s)) return true;
321         }
322         if(r > l && b > t && hit_in_bucket(*(bucket+1+bw), s)) return true;
323         return false;
324 }
325
326 int
327 pixel_hit_in_bucket(struct rock *r, float x, float y)
328 {
329         for(; r; r=r->next) {
330                 if(pixel_collide(SPRITE(r), x, y)) 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) {
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 }