JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
rock generation mostly works now.
[vor.git] / rocks.c
1 #include <SDL_image.h>
2 #include <math.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "config.h"
7 #include "file.h"
8 #include "globals.h"
9 #include "rocks.h"
10 #include "shape.h"
11
12 struct rock_struct {
13         float x,y,dx,dy;
14         int active;
15         SDL_Surface *image;
16         struct shape *shape;
17         int type_number;
18 }; 
19
20 struct rock_struct rock[MAXROCKS], *rockptr = rock;
21
22 SDL_Surface *surf_rock[NROCKS];
23 struct shape rock_shapes[NROCKS];
24
25 // timers for rock generation.
26 float rtimers[4];
27 int nrocks;
28
29 // constants for rock generation.
30 #define KH 32.0  // 32 s for a speed=1 rock to cross the screen horizontally.
31 #define KV 24.0  // 24 s for a speed=1 rock to cross the screen vertically.
32 #define RDX 2.5  // range for rock dx values (+/-)
33 #define RDY 2.5  // range for rock dy values (+/-)
34
35 float rnd(void);
36
37 #define crnd() (2*(rnd()-0.5))
38
39
40 int
41 init_rocks(void)
42 {
43         int i;
44         char a[MAX_PATH_LEN];
45         SDL_Surface *temp;
46
47         for(i = 0; i<NROCKS; i++) {
48                 snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
49                 NULLERROR(temp = IMG_Load(a));
50                 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
51                 get_shape(surf_rock[i], &rock_shapes[i]);
52         }
53         nrocks = 41;
54         return 0;
55 }
56
57 void
58 reset_rocks(void)
59 {
60         int i;
61
62         for(i = 0; i<MAXROCKS; i++) rock[i].active = 0;
63 }
64
65 enum { LEFT, RIGHT, TOP, BOTTOM };
66
67 void
68 rock_timer_increments(float *ti)
69 {
70         float dx0,dx1, dy0,dy1;
71         int i;
72
73         for(i=0; i<4; i++) ti[i] = 0;
74
75         dx0 = -RDX - screendx; dx1 = RDX - screendx;
76         dy0 = -RDY - screendy; dy1 = RDY - screendy;
77
78         if(dx0 != 0) {
79                 if(dx0 < 0) {
80                         if(dx1 < 0) ti[RIGHT] = -(dx0+dx1)/2;
81                         else {
82                                 ti[RIGHT] = -dx0/2;
83                                 ti[LEFT] = dx1/2;
84                         }
85                 } else ti[LEFT] = (dx0+dx1)/2;
86         }
87
88         if(dy0 != 0) {
89                 if(dy0 < 0) {
90                         if(dy1 < 0) ti[BOTTOM] = -(dy0+dy1)/2;
91                         else {
92                                 ti[BOTTOM] = -dy0/2;
93                                 ti[TOP] = dy1/2;
94                         }
95                 } else ti[TOP] = (dy0+dy1)/2;
96         }
97 }
98
99 void
100 new_rocks(void)
101 {
102         int i,j;
103         float ti[4];
104         float x, y;
105
106         rock_timer_increments(ti);
107
108         for(i=0; i<4; i++) {
109                 rtimers[i] += ti[i]*gamerate/20;
110                 if(rtimers[i] >= 1) {
111                         j=0;
112                         while(rockptr->active && j<MAXROCKS) {
113                                 if(++rockptr - rock >= MAXROCKS) rockptr = rock;
114                                 j++;
115                         }
116                         if(!rockptr->active) {
117                                 rtimers[i] -= 1;
118                                 rockptr->type_number = random() % NROCKS;
119                                 rockptr->image = surf_rock[rockptr->type_number];
120                                 rockptr->shape = &rock_shapes[rockptr->type_number];
121                                 switch(i) {
122                                         case RIGHT:
123                                                 rockptr->x = XSIZE;
124                                                 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
125                                                 break;
126                                         case LEFT:
127                                                 rockptr->x = -rockptr->image->w;
128                                                 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
129                                                 break;
130                                         case BOTTOM:
131                                                 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
132                                                 rockptr->y = YSIZE;
133                                                 break;
134                                         case TOP:
135                                                 rockptr->x = rnd()*(XSIZE + rockptr->image->w);
136                                                 rockptr->y = -rockptr->image->h;
137                                                 break;
138                                 }
139
140                                 j=0;
141                                 do {
142                                         rockptr->dx = RDX*crnd();
143                                         rockptr->dy = RDY*crnd();
144                                         x = (rockptr->dx-screendx)*gamerate;
145                                         y = (rockptr->dy-screendy)*gamerate;
146                                         j++;
147                                 } while(x < -rockptr->image->w || x >= XSIZE
148                                                 || y < -rockptr->image->h || y >= YSIZE);
149                                 if(j > 1) printf("had to try %d times.\n", j);
150
151                                 rockptr->active = 1;
152                         }
153                 }
154         }
155 }
156
157 void
158 move_rocks(void)
159 {
160         int i;
161
162         // Move all the rocks
163         for(i = 0; i < MAXROCKS; i++) {
164                 if(rock[i].active) {
165                         // move
166                         rock[i].x += (rock[i].dx-screendx)*gamerate;
167                         rock[i].y += (rock[i].dy-screendy)*gamerate;
168                         // clip
169                         if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE
170                                         || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) {
171                                 rock[i].active = 0;
172                         }
173                 }
174         }
175 }
176
177 void
178 draw_rocks(void)
179 {
180         int i;
181         SDL_Rect src, dest;
182
183         src.x = 0; src.y = 0;
184
185         for(i = 0; i<MAXROCKS; i++) {
186                 if(rock[i].active) {
187                         src.w = rock[i].image->w;
188                         src.h = rock[i].image->h;
189
190                         dest.w = src.w;
191                         dest.h = src.h;
192                         dest.x = (int) rock[i].x;
193                         dest.y = (int) rock[i].y;
194
195                         SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
196
197                 }
198         }
199 }
200
201 int
202 hit_rocks(float x, float y, struct shape *shape)
203 {
204         int i;
205
206         for(i=0; i<MAXROCKS; i++) {
207                 if(rock[i].active) {
208                         if(collide(x-rock[i].x, y-rock[i].y, rock[i].shape, shape)) 
209                                 return 1;
210                 }
211         }
212         return 0;
213 }
214
215 void
216 blast_rocks(float x, float y, float radius, int onlyslow)
217 {
218         int i;
219         float dx, dy, n;
220
221         for(i = 0; i<MAXROCKS; i++ ) {
222                 if(rock[i].x <= 0) continue;
223
224                 // This makes it so your explosion from dying magically doesn't leave
225                 // any rocks that aren't moving much on the x axis. If onlyslow is set,
226                 // only rocks that are barely moving will be pushed.
227                 if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue;
228
229                 dx = rock[i].x - x;
230                 dy = rock[i].y - y;
231
232                 n = sqrt(dx*dx + dy*dy);
233                 if(n < radius) {
234                         n *= 20;
235                         rock[i].dx += 54.0*(dx+30)/n;
236                         rock[i].dy += 54.0*dy/n;
237                 }
238         }
239 }