1 | /* |
---|
2 | * ttyvaders Textmode shoot'em up |
---|
3 | * Copyright (c) 2002 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: collide.c,v 1.11 2003/01/06 12:22:58 sam Exp $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or modify |
---|
9 | * it under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; either version 2 of the License, or |
---|
11 | * (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | |
---|
25 | #include "common.h" |
---|
26 | |
---|
27 | void collide_weapons_tunnel( game *g, weapons *wp, tunnel *t, explosions *ex ) |
---|
28 | { |
---|
29 | int i, j, x, y; |
---|
30 | |
---|
31 | for( i = 0; i < WEAPONS; i++ ) |
---|
32 | { |
---|
33 | x = wp->x[i] >> 4; |
---|
34 | y = wp->y[i] >> 4; |
---|
35 | |
---|
36 | switch( wp->type[i] ) |
---|
37 | { |
---|
38 | case WEAPON_LIGHTNING: |
---|
39 | case WEAPON_NONE: |
---|
40 | break; |
---|
41 | case WEAPON_SEEKER: |
---|
42 | case WEAPON_BOMB: |
---|
43 | case WEAPON_FRAGBOMB: |
---|
44 | if( y < 0 || y >= g->h ) |
---|
45 | { |
---|
46 | break; |
---|
47 | } |
---|
48 | |
---|
49 | if( x <= t->left[y] |
---|
50 | || x >= t->right[y] ) |
---|
51 | { |
---|
52 | int damage = wp->type[i] == WEAPON_SEEKER ? 1 : 2; |
---|
53 | |
---|
54 | add_explosion( g, ex, x, y, 0, 1, wp->type[i] == WEAPON_SEEKER ? EXPLOSION_SMALL : EXPLOSION_MEDIUM ); |
---|
55 | |
---|
56 | if( x <= t->left[y] ) |
---|
57 | { |
---|
58 | if( y-2 >= 0 ) t->left[y-2] -= damage - 1; |
---|
59 | if( y-1 >= 0 ) t->left[y-1] -= damage; |
---|
60 | t->left[y] -= damage + 1; |
---|
61 | if( y+1 < g->h ) t->left[y+1] -= damage; |
---|
62 | if( y+2 < g->h ) t->left[y+2] -= damage - 1; |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | if( y-2 >= 0 ) t->right[y-2] += damage - 1; |
---|
67 | if( y-1 >= 0 ) t->right[y-1] += damage; |
---|
68 | t->right[y] += damage + 1; |
---|
69 | if( y+1 < g->h ) t->right[y+1] += damage; |
---|
70 | if( y+2 < g->h ) t->right[y+2] += damage - 1; |
---|
71 | } |
---|
72 | |
---|
73 | if( wp->type[i] == WEAPON_FRAGBOMB ) |
---|
74 | { |
---|
75 | wp->n[i] = -1; |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | wp->type[i] = WEAPON_NONE; |
---|
80 | } |
---|
81 | } |
---|
82 | break; |
---|
83 | case WEAPON_LASER: |
---|
84 | for( j = GET_MIN( 0, wp->vy[i] >> 4 ) ; |
---|
85 | j < GET_MAX( 0, wp->vy[i] >> 4 ) ; |
---|
86 | j++ ) |
---|
87 | { |
---|
88 | if( y+j >= g->h || y+j < 0 ) |
---|
89 | { |
---|
90 | continue; |
---|
91 | } |
---|
92 | |
---|
93 | if( x <= t->left[y+j] || x >= t->right[y+j] ) |
---|
94 | { |
---|
95 | add_explosion( g, ex, x, y+j, 0, 1, EXPLOSION_SMALL ); |
---|
96 | wp->type[i] = WEAPON_NONE; |
---|
97 | |
---|
98 | if( x <= t->left[y+j] ) |
---|
99 | { |
---|
100 | if( y+j-1 >= 0 ) t->left[y+j-1]--; |
---|
101 | t->left[y+j] -= 2; |
---|
102 | if( y+j+1 < g->h ) t->left[y+j+1]--; |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | if( y+j-1 >= 0 ) t->right[y+j-1]++; |
---|
107 | t->right[y+j] += 2; |
---|
108 | if( y+j+1 < g->h ) t->right[y+j+1]++; |
---|
109 | } |
---|
110 | break; |
---|
111 | } |
---|
112 | } |
---|
113 | break; |
---|
114 | case WEAPON_BEAM: |
---|
115 | if( wp->n[i] > 19 ) |
---|
116 | { |
---|
117 | break; |
---|
118 | } |
---|
119 | |
---|
120 | j = (29 - wp->n[i]) * (29 - wp->n[i]) / 8; |
---|
121 | j = GET_MIN( y, j ); |
---|
122 | |
---|
123 | for( ; j > 0 ; j-- ) |
---|
124 | { |
---|
125 | if( x - 2 <= t->left[y-j] ) |
---|
126 | { |
---|
127 | add_explosion( g, ex, GET_MIN(t->left[y-j], x+3), y-j, 0, 1, EXPLOSION_SMALL ); |
---|
128 | t->left[y-j] -= GET_RAND(0,3); |
---|
129 | } |
---|
130 | else if( x + 3 >= t->right[y-j] ) |
---|
131 | { |
---|
132 | add_explosion( g, ex, GET_MAX(t->right[y-j], x-2), y-j, 0, 1, EXPLOSION_SMALL ); |
---|
133 | t->right[y-j] += GET_RAND(0,3); |
---|
134 | } |
---|
135 | } |
---|
136 | break; |
---|
137 | |
---|
138 | case WEAPON_NUKE: |
---|
139 | /* The nuke does not break the tunnel */ |
---|
140 | break; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | void collide_weapons_aliens( game *g, weapons *wp, aliens *al, explosions *ex ) |
---|
146 | { |
---|
147 | int i, j, k, x, y; |
---|
148 | |
---|
149 | for( i = 0; i < WEAPONS; i++ ) |
---|
150 | { |
---|
151 | int ok = 0; |
---|
152 | int r; |
---|
153 | |
---|
154 | x = wp->x[i] >> 4; |
---|
155 | y = wp->y[i] >> 4; |
---|
156 | |
---|
157 | switch( wp->type[i] ) |
---|
158 | { |
---|
159 | case WEAPON_LIGHTNING: |
---|
160 | case WEAPON_NONE: |
---|
161 | break; |
---|
162 | case WEAPON_NUKE: |
---|
163 | /* Big nuke */ |
---|
164 | r = (29 - wp->n[i]) * (29 - wp->n[i]) / 8; |
---|
165 | |
---|
166 | for( j = 0; j < ALIENS; j++ ) |
---|
167 | { |
---|
168 | if( al->type[j] == ALIEN_NONE || al->life[j] < 0 ) |
---|
169 | { |
---|
170 | continue; |
---|
171 | } |
---|
172 | |
---|
173 | if( wp->n[i] == 0 /* Nuke destroys _everything_ */ || |
---|
174 | (al->x[j] - x) * (al->x[j] - x) |
---|
175 | + 4 * (al->y[j] - y) * (al->y[j] - y) |
---|
176 | <= r * r ) |
---|
177 | { |
---|
178 | /* Kill alien, not nuke */ |
---|
179 | al->life[j] -= 10; |
---|
180 | } |
---|
181 | } |
---|
182 | break; |
---|
183 | |
---|
184 | case WEAPON_BEAM: |
---|
185 | if( wp->n[i] > 19 ) |
---|
186 | { |
---|
187 | break; |
---|
188 | } |
---|
189 | |
---|
190 | r = (29 - wp->n[i]) * (29 - wp->n[i]) / 8; |
---|
191 | |
---|
192 | for( j = 0; j < ALIENS; j++ ) |
---|
193 | { |
---|
194 | if( al->type[j] == ALIEN_NONE || al->life[j] < 0 ) |
---|
195 | { |
---|
196 | continue; |
---|
197 | } |
---|
198 | |
---|
199 | if( x >= al->x[j] && x <= al->x[j] + 4 |
---|
200 | && y >= al->y[j] + 2 && y-5-r <= al->y[j] ) |
---|
201 | { |
---|
202 | al->life[j] -= 4; |
---|
203 | } |
---|
204 | } |
---|
205 | break; |
---|
206 | |
---|
207 | case WEAPON_LASER: |
---|
208 | for( j = 0; j < ALIENS; j++ ) |
---|
209 | { |
---|
210 | if( al->type[j] == ALIEN_NONE || al->life[j] < 0 ) |
---|
211 | { |
---|
212 | continue; |
---|
213 | } |
---|
214 | |
---|
215 | for( k = GET_MIN( 0, wp->vy[i] >> 4 ) ; |
---|
216 | k < GET_MAX( 0, wp->vy[i] >> 4 ) ; |
---|
217 | k++ ) |
---|
218 | { |
---|
219 | if( x >= al->x[j] && x <= al->x[j] + 4 |
---|
220 | && y+k >= al->y[j] && y+k <= al->y[j] + 2 ) |
---|
221 | { |
---|
222 | al->life[j]--; |
---|
223 | ok = 1; |
---|
224 | break; |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | if( ok ) |
---|
230 | { |
---|
231 | add_explosion( g, ex, x, y+1, 0, 0, EXPLOSION_SMALL ); |
---|
232 | wp->type[i] = WEAPON_NONE; |
---|
233 | } |
---|
234 | break; |
---|
235 | |
---|
236 | case WEAPON_SEEKER: |
---|
237 | case WEAPON_BOMB: |
---|
238 | case WEAPON_FRAGBOMB: |
---|
239 | for( j = 0; j < ALIENS; j++ ) |
---|
240 | { |
---|
241 | if( al->type[j] == ALIEN_NONE || al->life[j] < 0 ) |
---|
242 | { |
---|
243 | continue; |
---|
244 | } |
---|
245 | |
---|
246 | if( x >= al->x[j] && x <= al->x[j] + 4 |
---|
247 | && y >= al->y[j] && y <= al->y[j] + 2 ) |
---|
248 | { |
---|
249 | al->life[j] -= wp->type[i] == WEAPON_SEEKER ? 1 : 5; |
---|
250 | ok = 1; |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | if( ok ) |
---|
255 | { |
---|
256 | add_explosion( g, ex, x, y+1, 0, 0, wp->type[i] == WEAPON_SEEKER ? EXPLOSION_SMALL : EXPLOSION_MEDIUM ); |
---|
257 | |
---|
258 | if( wp->type[i] == WEAPON_FRAGBOMB ) |
---|
259 | { |
---|
260 | wp->n[i] = -1; |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | wp->type[i] = WEAPON_NONE; |
---|
265 | } |
---|
266 | } |
---|
267 | break; |
---|
268 | } |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | void collide_player_tunnel( game *g, player *p, tunnel *t, explosions *ex ) |
---|
273 | { |
---|
274 | if( p->dead ) |
---|
275 | { |
---|
276 | return; |
---|
277 | } |
---|
278 | |
---|
279 | if( p->x <= t->left[p->y] ) |
---|
280 | { |
---|
281 | p->x += 3; |
---|
282 | p->vx = 2; |
---|
283 | add_explosion( g, ex, p->x+1, p->y-1, 0, 0, EXPLOSION_SMALL ); |
---|
284 | p->life -= 180; |
---|
285 | } |
---|
286 | else if( p->x + 5 >= t->right[p->y] ) |
---|
287 | { |
---|
288 | p->x -= 3; |
---|
289 | p->vx = -2; |
---|
290 | add_explosion( g, ex, p->x+4, p->y-1, 0, 0, EXPLOSION_SMALL ); |
---|
291 | p->life -= 180; |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|