1 | /* |
---|
2 | * ttyvaders Textmode shoot'em up |
---|
3 | * Copyright (c) 2002 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: main.c,v 1.9 2002/12/23 09:28:37 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 <stdio.h> |
---|
24 | #include <stdlib.h> |
---|
25 | |
---|
26 | #include <string.h> |
---|
27 | #include <unistd.h> |
---|
28 | |
---|
29 | #include "common.h" |
---|
30 | |
---|
31 | static void start_game (game *); |
---|
32 | |
---|
33 | int main (int argc, char **argv) |
---|
34 | { |
---|
35 | game *g = malloc(sizeof(game)); |
---|
36 | |
---|
37 | //srand(time(NULL)); |
---|
38 | |
---|
39 | if( init_graphics() ) |
---|
40 | { |
---|
41 | return 1; |
---|
42 | } |
---|
43 | |
---|
44 | /* Initialize our program */ |
---|
45 | init_game(g); |
---|
46 | |
---|
47 | /* Go ! */ |
---|
48 | start_game(g); |
---|
49 | |
---|
50 | /* Clean up */ |
---|
51 | end_graphics(); |
---|
52 | |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | static void start_game (game *g) |
---|
57 | { |
---|
58 | int i; |
---|
59 | int quit = 0; |
---|
60 | int poz = 0; |
---|
61 | int skip = 0; |
---|
62 | int purcompteur = 0; |
---|
63 | |
---|
64 | g->sf = create_starfield( g ); |
---|
65 | g->wp = malloc(sizeof(weapons)); |
---|
66 | g->ex = malloc(sizeof(explosions)); |
---|
67 | g->bo = malloc(sizeof(bonus)); |
---|
68 | g->t = create_tunnel( g, g->w, g->h ); |
---|
69 | g->p = create_player( g ); |
---|
70 | g->al = malloc(sizeof(aliens)); |
---|
71 | |
---|
72 | init_weapons( g, g->wp ); |
---|
73 | init_explosions( g, g->ex ); |
---|
74 | init_aliens( g, g->al ); |
---|
75 | |
---|
76 | /* Temporary stuff */ |
---|
77 | for( i = 0; i < 5; i++ ) |
---|
78 | { |
---|
79 | add_alien( g, g->al, rand() % g->w, rand() % g->h / 2, ALIEN_POOLP ); |
---|
80 | } |
---|
81 | |
---|
82 | g->t->w = 25; |
---|
83 | |
---|
84 | while( !quit ) |
---|
85 | { |
---|
86 | char key; |
---|
87 | |
---|
88 | while( ( key = get_key() ) ) |
---|
89 | { |
---|
90 | switch( key ) |
---|
91 | { |
---|
92 | case 'q': |
---|
93 | quit = 1; |
---|
94 | break; |
---|
95 | case 'p': |
---|
96 | poz = !poz; |
---|
97 | break; |
---|
98 | case '\t': |
---|
99 | ceo_alert(); |
---|
100 | poz = 1; |
---|
101 | break; |
---|
102 | case 's': |
---|
103 | skip = 1; |
---|
104 | break; |
---|
105 | case 'h': |
---|
106 | g->p->dir = -3; |
---|
107 | break; |
---|
108 | case 'j': |
---|
109 | if( g->p->y < g->h - 2 ) g->p->y += 1; |
---|
110 | break; |
---|
111 | case 'k': |
---|
112 | if( g->p->y > 1 ) g->p->y -= 1; |
---|
113 | break; |
---|
114 | case 'l': |
---|
115 | g->p->dir = 3; |
---|
116 | break; |
---|
117 | case 'n': |
---|
118 | if( g->p->nuke == 0 ) |
---|
119 | { |
---|
120 | g->p->nuke = 40; |
---|
121 | add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, 0, WEAPON_NUKE ); |
---|
122 | } |
---|
123 | break; |
---|
124 | case '\r': |
---|
125 | if( g->p->nuke == 0 ) |
---|
126 | { |
---|
127 | g->p->nuke = 40; |
---|
128 | add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, 0, WEAPON_BEAM ); |
---|
129 | } |
---|
130 | break; |
---|
131 | case 'd': |
---|
132 | if( g->p->nuke == 0 ) |
---|
133 | { |
---|
134 | g->p->nuke = 40; |
---|
135 | add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, -16, WEAPON_FRAGBOMB ); |
---|
136 | } |
---|
137 | break; |
---|
138 | case 'b': |
---|
139 | if( g->p->weapon == 0 ) |
---|
140 | { |
---|
141 | g->p->weapon = 4; |
---|
142 | add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, -16, WEAPON_BOMB ); |
---|
143 | } |
---|
144 | case ' ': |
---|
145 | if( g->p->weapon == 0 ) |
---|
146 | { |
---|
147 | g->p->weapon = 4; |
---|
148 | add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_LASER ); |
---|
149 | add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 0, -16, WEAPON_LASER ); |
---|
150 | /* Extra shtuph */ |
---|
151 | add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, -24, -16, WEAPON_SEEKER ); |
---|
152 | add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 24, -16, WEAPON_SEEKER ); |
---|
153 | /* More shtuph */ |
---|
154 | add_weapon( g, g->wp, (g->p->x + 1) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER ); |
---|
155 | add_weapon( g, g->wp, (g->p->x + 4) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER ); |
---|
156 | /* Even more shtuph */ |
---|
157 | add_weapon( g, g->wp, (g->p->x + 2) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER ); |
---|
158 | add_weapon( g, g->wp, (g->p->x + 3) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER ); |
---|
159 | /* Extra shtuph */ |
---|
160 | add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, -32, 0, WEAPON_SEEKER ); |
---|
161 | add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 32, 0, WEAPON_SEEKER ); |
---|
162 | } |
---|
163 | break; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | usleep(40000); |
---|
168 | |
---|
169 | if( !poz || skip ) |
---|
170 | { |
---|
171 | skip = 0; |
---|
172 | |
---|
173 | /* XXX: to be removed */ |
---|
174 | if( GET_RAND(0,10) == 0 ) |
---|
175 | { |
---|
176 | int list[3] = { ALIEN_POOLP, ALIEN_BOOL, ALIEN_BRAH }; |
---|
177 | |
---|
178 | add_alien( g, g->al, 0, rand() % g->h / 2, list[GET_RAND(0,3)] ); |
---|
179 | } |
---|
180 | |
---|
181 | /* Update game rules */ |
---|
182 | if( g->t->right[1] - g->t->left[1] == g->t->w ) |
---|
183 | { |
---|
184 | g->t->w = 85 - g->t->w; |
---|
185 | } |
---|
186 | |
---|
187 | /* Scroll and update positions */ |
---|
188 | collide_player_tunnel( g, g->p, g->t, g->ex ); |
---|
189 | update_player( g, g->p ); |
---|
190 | collide_player_tunnel( g, g->p, g->t, g->ex ); |
---|
191 | |
---|
192 | update_starfield( g, g->sf ); |
---|
193 | update_bonus( g, g->bo ); |
---|
194 | update_aliens( g, g->al ); |
---|
195 | |
---|
196 | collide_weapons_tunnel( g, g->wp, g->t, g->ex ); |
---|
197 | collide_weapons_aliens( g, g->wp, g->al, g->ex ); |
---|
198 | update_weapons( g, g->wp ); |
---|
199 | collide_weapons_tunnel( g, g->wp, g->t, g->ex ); |
---|
200 | collide_weapons_aliens( g, g->wp, g->al, g->ex ); |
---|
201 | |
---|
202 | update_explosions( g, g->ex ); |
---|
203 | /*if(purcompteur%2)*/ update_tunnel( g, g->t ); |
---|
204 | } |
---|
205 | |
---|
206 | /* Clear screen */ |
---|
207 | clear_graphics(); |
---|
208 | |
---|
209 | /* Print starfield, tunnel, aliens, player and explosions */ |
---|
210 | draw_starfield( g, g->sf ); |
---|
211 | draw_tunnel( g, g->t ); |
---|
212 | draw_bonus( g, g->bo ); |
---|
213 | draw_aliens( g, g->al ); |
---|
214 | draw_explosions( g, g->ex ); |
---|
215 | draw_weapons( g, g->wp ); |
---|
216 | draw_player( g, g->p ); |
---|
217 | |
---|
218 | /* Refresh */ |
---|
219 | refresh_graphics(); |
---|
220 | |
---|
221 | purcompteur++; |
---|
222 | } |
---|
223 | |
---|
224 | free_starfield( g, g->sf ); |
---|
225 | |
---|
226 | #if 0 |
---|
227 | free_player( p ); |
---|
228 | free_tunnel( g->t ); |
---|
229 | #endif |
---|
230 | } |
---|
231 | |
---|