5 * Copyright (C) 2003, 2007 Thomas Perl <thp@perli.net>
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,
33 void game( bool singleplayer
) {
35 { 0, 0, 0.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
},
43 "welcome to tennix " VERSION
46 Uint32 ot
= SDL_GetTicks();
48 Uint32 dt
= GAME_TICKS
;
50 Uint32 accumulator
= 0;
55 s
.player1
.type
= PLAYER_TYPE_AI
;
57 s
.player2
.type
= PLAYER_TYPE_AI
;
60 game_setup_serve( &s
);
73 while( accumulator
>= dt
) {
80 s
.was_stopped
= false;
89 bool step( GameState
* s
) {
93 if( get_phase( s
) < 1.0) {
94 if( !s
->ground
.jump
) {
97 if( IS_OUT_Y( s
->ball
.y
)) {
98 /* out - responsibilities stay the same */
99 s
->status
= "out -- don't hit the ball!";
102 /* not out - responsibilities change */
103 s
->player1
.responsible
= !(s
->player2
.responsible
= !s
->player2
.responsible
);
108 s
->ground
.x
= s
->ball
.x
;
109 s
->ground
.y
= s
->ball
.y
;
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";
124 s
->status
= "player 2 scores";
127 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
&& s
->player2
.type
== PLAYER_TYPE_AI
) {
128 s
->status
= "player scores";
130 s
->status
= "player 1 scores";
134 game_setup_serve( s
);
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
;
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();
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
;
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();
170 /* Update ball_dest for debugging purposes */
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
--;
181 keys
= SDL_GetKeyState( NULL
);
184 if( s
->player1
.type
== PLAYER_TYPE_HUMAN
) {
185 input_human( &s
->player1
, keys
['w'], keys
['s'], keys
['d']);
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']);
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
);
209 void render( GameState
* s
) {
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);
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
);
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);
256 void limit_value( float* value
, float min
, float max
) {
259 } else if( *value
> max
) {
264 float get_phase( GameState
* s
) {
266 float x
, min
, max
, direction
;
271 direction
= s
->ball
.move_x
;
273 pos
= (direction
>0)?(1-GROUND_PHASE
):(GROUND_PHASE
);
275 fract
= (x
-min
)/(max
-min
);
279 return fabsf( cosf(PI
*fract
/2))*PHASE_AMP
*s
->ball
.jump
;
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
);
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
);
302 s
->player1
.ball_dest
= dest
;
304 s
->player2
.ball_dest
= dest
;
307 /* lengths for the ball's journey */
309 x_len
= fabsf(GAME_X_MAX
- s
->ball
.x
);
310 y_len
= dest
- by
+ MOVE_Y_SEED
-rand()%MOVE_Y_SEED
*2;
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
) {
329 if( hit
&& !player
->state
) {
330 player
->state
= PLAYER_STATE_MAX
;
334 void input_ai( Player
* player
, Ball
* ball
, Player
* opponent
) {
338 if( fabsf( player
->y
- ball
->y
) > RACKET_Y_MID
*5) {
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);
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
) {
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;
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
);