source: libcaca/trunk/src/collide.c @ 37

Last change on this file since 37 was 37, checked in by Sam Hocevar, 20 years ago
  • I think I fucked up the $Id tags on my previous commit.
File size: 9.0 KB
Line 
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.6 2002/12/22 18:44:12 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
27void collide_weapons_tunnel( game *g, weapons *wp, tunnel *t, explosions *ex )
28{
29    int i, 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_NONE:
39                break;
40            case WEAPON_SEEKER:
41            case WEAPON_BOMB:
42                if( x <= t->left[y]
43                     || x >= t->right[y] )
44                {
45                    int damage = wp->type[i] == WEAPON_SEEKER ? 1 : 2;
46
47                    add_explosion( g, ex, x, y, 0, 1, wp->type[i] == WEAPON_SEEKER ? EXPLOSION_SMALL : EXPLOSION_MEDIUM );
48
49                    if( x <= t->left[y] )
50                    {
51                        t->right[y-2] -= damage - 1;
52                        t->left[y-1] -= damage;
53                        t->left[y] -= damage + 1;
54                        t->left[y+1] -= damage;
55                        t->right[y+2] -= damage - 1;
56                    }
57                    else
58                    {
59                        t->right[y-2] += damage - 1;
60                        t->right[y-1] += damage;
61                        t->right[y] += damage + 1;
62                        t->right[y+1] += damage;
63                        t->right[y+2] += damage - 1;
64                    }
65
66                    wp->type[i] = WEAPON_NONE;
67                }
68                break;
69            case WEAPON_LASER:
70                if( x <= t->left[y+1]
71                     || x >= t->right[y+1] )
72                {
73                    add_explosion( g, ex, x, y+1, 0, 1, EXPLOSION_SMALL );
74
75                    if( x <= t->left[y+1] )
76                    {
77                        t->left[y]--;
78                        t->left[y+1]-=2;
79                        t->left[y+2]--;
80                    }
81                    else
82                    {
83                        t->right[y]++;
84                        t->right[y+1]+=2;
85                        t->right[y+2]++;
86                    }
87
88                    wp->type[i] = WEAPON_NONE;
89                }
90                else if( x <= t->left[y]
91                          || x >= t->right[y] )
92                {
93                    add_explosion( g, ex, x, y, 0, 1, EXPLOSION_SMALL );
94
95                    if( x <= t->left[y] )
96                    {
97                        t->left[y-1]--;
98                        t->left[y]-=2;
99                        t->left[y+1]--;
100                    }
101                    else
102                    {
103                        t->right[y-1]++;
104                        t->right[y]+=2;
105                        t->right[y+1]++;
106                    }
107
108                    wp->type[i] = WEAPON_NONE;
109                }
110                break;
111            case WEAPON_NUKE:
112            case WEAPON_BEAM:
113                /* The nuke and the laser do not break the tunnel */
114                break;
115        }
116    }
117}
118
119void collide_weapons_aliens( game *g, weapons *wp, aliens *al, explosions *ex )
120{
121    int i, j, x, y;
122
123    for( i = 0; i < WEAPONS; i++ )
124    {
125        int ok = 0;
126        int r;
127
128        x = wp->x[i] >> 4;
129        y = wp->y[i] >> 4;
130
131        switch( wp->type[i] )
132        {
133            case WEAPON_NONE:
134                break;
135            case WEAPON_NUKE:
136                /* Big nuke */
137                r = (29 - wp->n[i]) * (29 - wp->n[i]) / 8;
138
139                for( j = 0; j < ALIENS; j++ )
140                {
141                    if( al->type[j] == ALIEN_NONE )
142                    {
143                        continue;
144                    }
145
146                    if( wp->n[i] == 0 /* Nuke destroys _everything_ */ ||
147                        (al->x[j] - x) * (al->x[j] - x)
148                          + 4 * (al->y[j] - y) * (al->y[j] - y)
149                        <= r * r )
150                    {
151                        /* Kill alien, not nuke */
152                        al->type[j] = ALIEN_NONE;
153                        add_explosion( g, ex, al->x[j], al->y[j], 0, 0, EXPLOSION_MEDIUM );
154                        add_bonus( g, g->bo, al->x[j], al->y[j], GET_RAND(0,5) ? BONUS_GREEN : BONUS_LIFE );
155                    }
156                }
157                break;
158            case WEAPON_BEAM:
159                r = (29 - wp->n[i]) * (29 - wp->n[i]) / 8;
160
161                for( j = 0; j < ALIENS; j++ )
162                {
163                    if( al->type[j] == ALIEN_NONE )
164                    {
165                        continue;
166                    }
167
168                    if( x >= al->x[j] && x <= al->x[j] + 4
169                         && y >= al->y[j] + 2 && y-5-r <= al->y[j] )
170                    {
171                        al->life[j] -= 4;
172                        if( al->life[j] <= 0 )
173                        {
174                            al->type[j] = ALIEN_NONE;
175                            add_explosion( g, ex, al->x[j], al->y[j], 0, 0, EXPLOSION_MEDIUM );
176                            add_bonus( g, g->bo, al->x[j], al->y[j], GET_RAND(0,5) ? BONUS_GREEN : BONUS_LIFE );
177                        }
178                    }
179                }
180                break;
181
182            case WEAPON_LASER:
183                for( j = 0; j < ALIENS; j++ )
184                {
185                    if( al->type[j] == ALIEN_NONE )
186                    {
187                        continue;
188                    }
189
190                    if( x >= al->x[j] && x <= al->x[j] + 4
191                         && y >= al->y[j] && y <= al->y[j] + 2 )
192                    {
193                        al->life[j]--;
194                        if( al->life[j] <= 0 )
195                        {
196                            al->type[j] = ALIEN_NONE;
197                            add_explosion( g, ex, al->x[j], al->y[j], 0, 0, EXPLOSION_MEDIUM );
198                            add_bonus( g, g->bo, al->x[j], al->y[j], GET_RAND(0,5) ? BONUS_GREEN : BONUS_LIFE );
199                        }
200                        ok = 1;
201                    }
202                    else if( x >= al->x[j] && x <= al->x[j] + 4
203                              && y+1 >= al->y[j] && y+1 <= al->y[j] + 2 )
204                    {
205                        al->life[j]--;
206                        if( al->life[j] <= 0 )
207                        {
208                            al->type[j] = ALIEN_NONE;
209                            add_explosion( g, ex, al->x[j], al->y[j]+1, 0, 0, EXPLOSION_MEDIUM );
210                            add_bonus( g, g->bo, al->x[j], al->y[j]+1, GET_RAND(0,5) ? BONUS_GREEN : BONUS_LIFE );
211                        }
212                        ok = 1;
213                    }
214                }
215
216                if( ok )
217                {
218                    add_explosion( g, ex, x, y+1, 0, 0, EXPLOSION_SMALL );
219                    wp->type[i] = WEAPON_NONE;
220                }
221                break;
222
223            case WEAPON_SEEKER:
224            case WEAPON_BOMB:
225                for( j = 0; j < ALIENS; j++ )
226                {
227                    if( al->type[j] == ALIEN_NONE )
228                    {
229                        continue;
230                    }
231
232                    if( x >= al->x[j] && x <= al->x[j] + 4
233                         && y >= al->y[j] && y <= al->y[j] + 2 )
234                    {
235                        al->life[j] -= wp->type[i] == WEAPON_BOMB ? 5 : 1;
236                        if( al->life[j] <= 0 )
237                        {
238                            al->type[j] = ALIEN_NONE;
239                            add_explosion( g, ex, al->x[j], al->y[j], 0, 0, EXPLOSION_MEDIUM );
240                            add_bonus( g, g->bo, al->x[j], al->y[j], GET_RAND(0,5) ? BONUS_GREEN : BONUS_LIFE );
241                        }
242                        ok = 1;
243                    }
244                }
245
246                if( ok )
247                {
248                    add_explosion( g, ex, x, y+1, 0, 0, wp->type[i] == WEAPON_SEEKER ? EXPLOSION_SMALL : EXPLOSION_MEDIUM );
249                    wp->type[i] = WEAPON_NONE;
250                }
251                break;
252        }
253    }
254}
255
256void collide_player_tunnel( game *g, player *p, tunnel *t, explosions *ex )
257{
258    if( p->x <= t->left[p->y] )
259    {
260        p->x += 2;
261        add_explosion( g, ex, p->x+1, p->y-2, 0, 0, EXPLOSION_SMALL );
262    }
263    else if( p->x + 5 >= t->right[p->y] )
264    {
265        p->x -= 2;
266        add_explosion( g, ex, p->x+4, p->y-2, 0, 0, EXPLOSION_SMALL );
267    }
268}
269
Note: See TracBrowser for help on using the repository browser.