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