Tennix 0.3.3: Dynamic Text Engine
[tennix.git] / game.c
blob3cf9a071857a6d616744c72cab8a07b2a98b1b4f
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007 Thomas Perl <thp@perli.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 **/
24 #include <stdio.h>
25 #include <math.h>
27 #include "tennix.h"
28 #include "game.h"
29 #include "graphics.h"
30 #include "input.h"
31 #include "sound.h"
33 void game( bool singleplayer) {
34 GameState s = {
35 { 0, 0, 0.0, 0.0, 0.0 },
36 { 0, 0, 0, 0, 0 },
37 { GAME_X_MIN-RACKET_X_MID*2, GAME_Y_MID, 0, 0, 1, DESIRE_NORMAL, PLAYER_TYPE_HUMAN },
38 { GAME_X_MAX+RACKET_X_MID*2, GAME_Y_MID, 0, 0, 0, DESIRE_NORMAL, PLAYER_TYPE_HUMAN },
42 true,
43 "welcome to tennix " VERSION
46 Uint32 ot = SDL_GetTicks();
47 Uint32 nt;
48 Uint32 dt = GAME_TICKS;
49 Uint32 diff;
50 Uint32 accumulator = 0;
51 bool quit = false;
53 if( singleplayer) {
54 #ifdef DEBUG
55 s.player1.type = PLAYER_TYPE_AI;
56 #endif
57 s.player2.type = PLAYER_TYPE_AI;
60 game_setup_serve( &s);
61 sound_audience();
63 while( !quit) {
64 nt = SDL_GetTicks();
65 diff = nt-ot;
66 if( diff > 2000) {
67 diff = 0;
70 accumulator += diff;
71 ot = nt;
73 while( accumulator >= dt) {
74 quit = step( &s);
75 s.time += dt;
76 accumulator -= dt;
78 if( s.was_stopped) {
79 ot = SDL_GetTicks();
80 s.was_stopped = false;
84 render( &s);
89 bool step( GameState* s) {
90 Uint8 *keys;
91 SDL_Event e;
93 if( get_phase( s) < 1.0) {
94 if( !s->ground.jump) {
95 sound_ground();
97 if( IS_OUT_Y( s->ball.y)) {
98 /* out - responsibilities stay the same */
99 s->status = "out -- don't hit the ball!";
100 sound_out();
101 } else {
102 /* not out - responsibilities change */
103 s->player1.responsible = !(s->player2.responsible = !s->player2.responsible);
104 s->status = "";
107 s->ground.jump = 3;
108 s->ground.x = s->ball.x;
109 s->ground.y = s->ball.y;
110 } else {
111 if( s->ground.jump && !(s->time%5)) s->ground.jump--;
114 if( IS_OUT_X(s->ball.x) || IS_OFFSCREEN_Y(s->ball.y)) {
115 if( IS_OFFSCREEN( s->ball.x, s->ball.y)) {
116 s->player1_serves = s->player1.responsible;
118 (s->player1.responsible)?(s->player2.score++):(s->player1.score++);
120 if( s->player1.responsible) {
121 if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
122 s->status = "computer scores";
123 } else {
124 s->status = "player 2 scores";
126 } else {
127 if( s->player1.type == PLAYER_TYPE_HUMAN && s->player2.type == PLAYER_TYPE_AI) {
128 s->status = "player scores";
129 } else {
130 s->status = "player 1 scores";
134 game_setup_serve( s);
135 sound_applause();
136 SDL_Delay( 500);
137 start_fade();
138 s->was_stopped = true;
141 if( IS_OUT_X(s->ball.x)) {
142 if( s->ball.move_x <= 0 && IS_NEAR_X( s->player1.x, s->ball.x) && IS_NEAR_Y( s->player1.y, s->ball.y) && s->player1.state) {
143 s->ball.x = GAME_X_MIN;
144 if( s->player1.state == PLAYER_STATE_MAX) {
145 s->ball.move_x = PLAYER_POWERSHOT;
146 } else {
147 s->ball.move_x = 2.5 + 2.0*s->player1.state/PLAYER_STATE_MAX;
149 s->ball.move_y = get_move_y( s, 1);
150 s->player2.responsible = !(s->player1.responsible = 1);
151 s->ball.jump += 1.0-2.0*(s->player1.state<5);
152 sound_applause_stop();
153 sound_racket();
154 } else if( s->ball.move_x >= 0 && IS_NEAR_X( s->player2.x, s->ball.x) && IS_NEAR_Y( s->player2.y, s->ball.y) && s->player2.state) {
155 s->ball.x = GAME_X_MAX;
156 if( s->player2.state == PLAYER_STATE_MAX) {
157 s->ball.move_x = -PLAYER_POWERSHOT;
158 } else {
159 s->ball.move_x = -(2.5 + 2.0*s->player2.state/PLAYER_STATE_MAX);
161 s->ball.move_y = get_move_y( s, 2);
162 s->player1.responsible = !(s->player2.responsible = 1);
163 s->ball.jump += 1.0-2.0*(s->player2.state<5);
164 sound_applause_stop();
165 sound_racket();
170 /* Update ball_dest for debugging purposes */
171 get_move_y( s, 1);
172 get_move_y( s, 2);
174 s->ball.x += s->ball.move_x;
175 s->ball.y += s->ball.move_y;
177 if( s->player1.state) s->player1.state--;
178 if( s->player2.state) s->player2.state--;
180 SDL_PollEvent( &e);
181 keys = SDL_GetKeyState( NULL);
183 if( !is_fading()) {
184 if( s->player1.type == PLAYER_TYPE_HUMAN) {
185 input_human( &s->player1, keys['w'], keys['s'], keys['d']);
186 } else {
187 input_ai( &s->player1, &s->ball, &s->player2);
190 if( s->player2.type == PLAYER_TYPE_HUMAN) {
191 input_human( &s->player2, keys['o'], keys['l'], keys['k']);
192 } else {
193 input_ai( &s->player2, &s->ball, &s->player1);
197 if( keys['f']) SDL_WM_ToggleFullScreen( screen);
198 if( keys['y']) SDL_SaveBMP( screen, "screenshot.bmp");
200 if( keys[SDLK_ESCAPE] || keys['q']) return true;
202 limit_value( &s->player1.y, PLAYER_Y_MIN, PLAYER_Y_MAX);
203 limit_value( &s->player2.y, PLAYER_Y_MIN, PLAYER_Y_MAX);
204 limit_value( &s->ball.jump, BALL_JUMP_MIN, BALL_JUMP_MAX);
206 return false;
209 void render( GameState* s) {
210 clearscr();
211 show_image( GR_COURT, 0, 0, 255);
212 show_image( GR_SHADOW, s->ball.x-BALL_X_MID, s->ball.y + get_phase( s) - BALL_Y_MID, 255);
214 show_sprite( GR_RACKET, (!s->player1.state), 4, s->player1.x-RACKET_X_MID, s->player1.y-RACKET_Y_MID, 255);
215 show_sprite( GR_RACKET, (!s->player2.state)+2, 4, s->player2.x-RACKET_X_MID, s->player2.y-RACKET_Y_MID, 255);
217 if( s->ground.jump) {
218 show_sprite( GR_GROUND, s->ground.jump-1, 3, s->ground.x - BALL_X_MID, s->ground.y - BALL_Y_MID, 128);
221 if( s->ball.move_x > 0) {
222 show_sprite( GR_BALL, (s->time/1000)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
223 } else if( s->ball.move_x < 0) {
224 show_sprite( GR_BALL, 3-(s->time/1000)%4, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
225 } else {
226 show_sprite( GR_BALL, 0, 4, s->ball.x-BALL_X_MID, s->ball.y-BALL_Y_MID, 255);
229 show_digit( s->player1.score/10%10, 140*2, 14, 100);
230 show_digit( s->player1.score%10, 148*2, 14, 100);
231 show_digit( 10, 156*2, 14, 100);
232 show_digit( s->player2.score/10%10, 164*2, 14, 100);
233 show_digit( s->player2.score%10, 172*2, 14, 100);
235 font_draw_string( GR_SMALLISH_FONT, s->status, (WIDTH-font_get_string_width( GR_SMALLISH_FONT, s->status))/2, HEIGHT-50, s->time/30, ANIMATION_WAVE);
237 #ifdef DEBUG
238 line_horiz( s->player1.y, 255, 0, 0);
239 line_horiz( s->player2.y, 0, 0, 255);
240 line_horiz( s->ball.y, 0, 255, 0);
242 line_vert( s->player1.x, 255, 0, 0);
243 line_vert( s->player2.x, 0, 0, 255);
244 line_vert( s->ball.x, 0, 255, 0);
246 line_horiz( s->player1.ball_dest, 255, 0, 255);
247 line_horiz( s->player2.ball_dest, 0, 255, 255);
249 line_horiz( GAME_Y_MIN, 100, 100, 100);
250 line_horiz( GAME_Y_MAX, 100, 100, 100);
251 #endif
253 updatescr();
256 void limit_value( float* value, float min, float max) {
257 if( *value < min) {
258 *value = min;
259 } else if( *value > max) {
260 *value = max;
264 float get_phase( GameState* s) {
265 float pos, fract;
266 float x, min, max, direction;
268 x = s->ball.x;
269 min = GAME_X_MIN;
270 max = GAME_X_MAX;
271 direction = s->ball.move_x;
273 pos = (direction>0)?(1-GROUND_PHASE):(GROUND_PHASE);
275 fract = (x-min)/(max-min);
277 if( fract < pos) {
278 fract = fract/pos;
279 return fabsf( cosf(PI*fract/2))*PHASE_AMP*s->ball.jump;
280 } else {
281 fract = (pos-fract)/(1-pos);
282 return fabsf( sinf(PI*fract/2))*PHASE_AMP*s->ball.jump;
286 float get_move_y( GameState* s, unsigned char player) {
287 float pct, dest, x_len, y_len;
288 float py, by, pa, move_x;
290 py = (player==1)?(s->player1.y):(s->player2.y);
291 by = s->ball.y;
292 pa = RACKET_Y_MID*2;
293 move_x = s->ball.move_x;
295 /* -1.0 .. 1.0 for racket hit position */
296 pct = (by-py)/(pa/2);
297 limit_value( &pct, -1.0, 1.0);
299 /* Y destination for ball */
300 dest = GAME_Y_MID + pct*(GAME_Y_MAX-GAME_Y_MIN);
301 if( player == 1) {
302 s->player1.ball_dest = dest;
303 } else {
304 s->player2.ball_dest = dest;
307 /* lengths for the ball's journey */
308 if( player == 1) {
309 x_len = fabsf(GAME_X_MAX - s->ball.x);
310 y_len = dest - by + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
311 } else {
312 x_len = s->ball.x - GAME_X_MIN;
313 y_len = by - dest + MOVE_Y_SEED-rand()%MOVE_Y_SEED*2;
316 /* return the should-be value for move_y */
317 return (y_len*move_x)/(x_len);
320 void input_human( Player* player, bool up, bool down, bool hit) {
321 if( up) {
322 player->y -= 6;
325 if( down) {
326 player->y += 6;
329 if( hit && !player->state) {
330 player->state = PLAYER_STATE_MAX;
334 void input_ai( Player* player, Ball* ball, Player* opponent) {
335 float fact = 1.5;
336 float target;
338 if( fabsf( player->y - ball->y) > RACKET_Y_MID*5) {
339 fact = 4;
342 target = GAME_Y_MID + (opponent->ball_dest - GAME_Y_MID)/10;
344 if( player->responsible) {
345 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, ball->y)) {
346 if( player->y < ball->y) {
347 player->y += fmin( 2*fact, ball->y - player->y);
348 } else if( player->y > ball->y) {
349 player->y -= fmin( 2*fact, player->y - ball->y);
353 if( (ball->move_x != 0 || IS_NEAR_Y_AI( player->y, ball->y)) && IS_NEAR_X_AI( player->x, ball->x) && !player->state && rand()%4) {
354 player->state = PLAYER_STATE_MAX;
356 } else if( ball->move_x == 0) {
357 if( player->desire == DESIRE_NORMAL && !IS_NEAR_Y_AI( player->y, target)) {
358 if( player->y < target) {
359 player->y += fmin( fact, (target-player->y)/40.0);
360 } else if( player->y > target) {
361 player->y -= fmin( fact, (player->y-target)/40.0);
364 } else {/*
365 if( player->desire == DESIRE_NORMAL) {
366 if( !IS_NEAR_Y_AI( player->y, target)) {
367 player->y += (target - player->y)/40.0;
373 void game_setup_serve( GameState* s) {
374 s->ball.jump = 7.5;
375 s->ball.y = GAME_Y_MID;
376 s->ball.move_x = 0.0;
377 s->ball.move_y = 0.0;
379 if( s->player1_serves) {
380 s->player1.responsible = true;
381 s->player1.ball_dest = 0.0;
382 s->ball.x = GAME_X_MIN-RACKET_X_MID*1.5;
383 } else {
384 s->player1.responsible = false;
385 s->player2.ball_dest = 0.0;
386 s->ball.x = GAME_X_MAX+RACKET_X_MID*1.5;
389 s->player2.responsible = !(s->player1.responsible);