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