JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
f68a57a2c4c69b816eb9259eab9460f1bd5e3435
[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 struct rock_struct {
15         struct rock_struct *next;
16         float x,y,dx,dy;
17         SDL_Surface *image;
18         struct shape *shape;
19         int type_number;
20 }; 
21
22 struct rock_struct rocks[MAXROCKS], *free_rocks;
23
24 struct rock_struct **rock_buckets[2];
25 int n_buckets, p;
26 int bw, bh;
27 int grid_size;
28
29 SDL_Surface *surf_rock[NROCKS];
30 struct shape rock_shapes[NROCKS];
31
32 // timers for rock generation.
33 float rtimers[4];
34
35 uint32_t nrocks;
36 float nrocks_timer;
37 float nrocks_inc_ticks = 2*60*20/(F_ROCKS-I_ROCKS);
38
39 // constants for rock generation.
40 #define KH (32.0*20)  // 32 s for a speed=1 rock to cross the screen horizontally.
41 #define KV (24.0*20)  // 24 s for a speed=1 rock to cross the screen vertically.
42 #define RDX 2.5  // range for rock dx values (+/-)
43 #define RDY 2.5  // range for rock dy values (+/-)
44
45 static inline int bucket(int x, int y) { return (1+x/grid_size) + bw*(1+y/grid_size); }
46
47 void
48 init_buckets(void)
49 {
50         bw = (XSIZE+2*grid_size-1) / grid_size;
51         bh = (YSIZE+2*grid_size-1) / grid_size;
52         n_buckets = bw * bh;
53         
54         rock_buckets[0] = malloc(n_buckets * sizeof(struct rock_struct *));
55         rock_buckets[1] = malloc(n_buckets * sizeof(struct rock_struct *));
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 sort_rock(struct rock_struct *q, struct rock_struct *r, int p)
65 {
66         int b = bucket(r->x, r->y);
67         q->next = r->next;  // remove from old list
68         r->next = rock_buckets[p][b];  // insert into new list
69         rock_buckets[p][b] = r;
70 }
71
72 void
73 reset_rocks(void)
74 {
75         int i;
76
77         for(i=0; i<MAXROCKS; i++) rocks[i].image = NULL;
78         rocks[0].next = NULL; free_rocks = &rocks[MAXROCKS-1];
79         for(i = 1; i<MAXROCKS; i++) rocks[i].next = &rocks[i-1];
80         for(i = 0; i<n_buckets; i++) {
81                 rock_buckets[0][i] = NULL;
82                 rock_buckets[1][i] = NULL;
83         }
84
85         nrocks = I_ROCKS;
86         nrocks_timer = 0;
87 }
88
89 int
90 init_rocks(void)
91 {
92         int i;
93         char a[MAX_PATH_LEN];
94         SDL_Surface *temp;
95         int maxw=0, maxh=0;
96
97         for(i = 0; i<NROCKS; i++) {
98                 snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
99                 NULLERROR(temp = IMG_Load(a));
100                 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
101                 get_shape(surf_rock[i], &rock_shapes[i]);
102                 maxw = max(maxw, rock_shapes[i].w);
103                 maxh = max(maxh, rock_shapes[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;
183         struct rock_struct *r;
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;
207                         r->type_number = urnd() % NROCKS;
208                         r->image = surf_rock[r->type_number];
209                         r->shape = &rock_shapes[r->type_number];
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                         sort_rock((struct rock_struct *)&free_rocks, r, p);
241                 }
242         }
243 }
244
245 void
246 move_rocks(void)
247 {
248         int b;
249         struct rock_struct *q,*r;
250
251         // Move all the rocks
252         for(b=0; b<n_buckets; b++) {
253                 q=(struct rock_struct *)&rock_buckets[p][b]; r=q->next;
254                 while(r) {
255                         // move
256                         r->x += (r->dx - screendx)*t_frame;
257                         r->y += (r->dy - screendy)*t_frame;
258
259                         // clip
260                         if(r->x + r->image->w < 0 || r->x >= XSIZE
261                                         || r->y + r->image->h < 0 || r->y >= YSIZE) {
262                                 q->next = r->next;
263                                 r->next = free_rocks; free_rocks = r;
264                                 r->image = NULL;
265                         } else sort_rock(q,r,1-p);
266                         if(q->next == r) q = q->next;
267                         if(q) r = q->next; else r = NULL;
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(int b, float x, float y, struct shape *shape)
288 {
289         struct rock_struct *r;
290
291         for(r=rock_buckets[p][b]; r; r=r->next) {
292                 if(collide(x - r->x, y - r->y, r->shape, shape)) return 1;
293         }
294         return 0;
295 }
296
297 int
298 hit_rocks(float x, float y, struct shape *shape)
299 {
300         int b = bucket(x, y);
301         int bdx = ((int)x+shape->w)/grid_size - (int)x/grid_size;
302         int bdy = ((int)y+shape->h)/grid_size - (int)y/grid_size;
303         if(hit_in_bucket(b, x, y, shape)) return 1;
304         if(hit_in_bucket(b-1, x, y, shape)) return 1;
305         if(hit_in_bucket(b-bw, x, y, shape)) return 1;
306         if(hit_in_bucket(b-bw-1, x, y, shape)) return 1;
307
308         if(bdx) {
309                 if(hit_in_bucket(b+1, x, y, shape)) return 1;
310                 if(hit_in_bucket(b+1-bw, x, y, shape)) return 1;
311         }
312         if(bdy) {
313                 if(hit_in_bucket(b+bw, x, y, shape)) return 1;
314                 if(hit_in_bucket(b+bw-1, x, y, shape)) return 1;
315         }
316         if(bdx && bdy && hit_in_bucket(b+bw+1, x, y, shape)) return 1;
317         return 0;
318 }
319
320 int
321 pixel_hit_in_bucket(int b, float x, float y)
322 {
323         struct rock_struct *r;
324         for(r=rock_buckets[p][b]; r; r=r->next) {
325                 if(x < r->x || y < r->y) continue;
326                 if(pixel_collide(x - r->x, y - r->y, r->shape)) return 1;
327         }
328         return 0;
329 }
330
331 int
332 pixel_hit_rocks(float x, float y)
333 {
334         int b = bucket(x, y);
335         if(pixel_hit_in_bucket(b, x, y)) return 1;
336         if(pixel_hit_in_bucket(b-1, x, y)) return 1;
337         if(pixel_hit_in_bucket(b-bw, x, y)) return 1;
338         if(pixel_hit_in_bucket(b-bw-1, x, y)) return 1;
339         return 0;
340 }
341
342 void
343 blast_rocks(float x, float y, float radius, int onlyslow)
344 {
345         int b;
346         struct rock_struct *r;
347         float dx, dy, n;
348
349         if(onlyslow) return;
350
351         for(b=0; b<n_buckets; b++) {
352                 for(r=rock_buckets[p][b]; r; r=r->next) {
353                         if(r->x <= 0) continue;
354
355                         // This makes it so your explosion from dying magically doesn't leave
356                         // any rocks that aren't moving much on the x axis. If onlyslow is set,
357                         // only rocks that are barely moving will be pushed.
358                         if(onlyslow && (r->dx - screendx < -4 || r->dx - screendx > 3)) continue;
359
360                         dx = r->x - x;
361                         dy = r->y - y;
362
363                         n = sqrt(dx*dx + dy*dy);
364                         if(n < radius) {
365                                 n *= 15;
366                                 r->dx += 54.0*dx/n;
367                                 r->dy += 54.0*dy/n;
368                         }
369                 }
370         }
371 }