5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
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,
38 # define BALL_STATES 16
40 # define BALL_STATES 4
43 typedef unsigned char bool;
49 typedef unsigned char referee_t
;
65 SOUND_EVENT_NONE
= 0<<0,
66 SOUND_EVENT_GROUND
= 1<<0,
67 SOUND_EVENT_OUT
= 1<<1,
68 SOUND_EVENT_APPLAUSE
= 1<<2,
69 SOUND_EVENT_RACKET
= 1<<3
72 #define SOUND_EVENT_COUNT 4
74 typedef unsigned char soundevent_t
;
82 unsigned int max_visitors
;
83 const char* court_type_name
;
85 unsigned int photo_frames
;
86 unsigned int worldmap_x
;
87 unsigned int worldmap_y
;
104 bool inhibit_gravity
;
109 int input_device_index
;
113 float power_up_factor
;
114 float power_down_factor
;
117 unsigned char desire
;
118 bool type
; /* is this player ai-controlled or human? */
119 int game
; /* score for the current game */
120 int sets
[SETS_TO_WIN
*2]; /* score for each set */
121 float accelerate
; /* a value [0..1] how fast the user accelerates */
136 /* wait 2 seconds before we score the game */
137 #define SCORING_DELAY 2000
143 SCORE_EVENT_GROUND_INVALID
,
144 SCORE_EVENT_GROUND_VALID
,
145 SCORE_EVENT_OFFSCREEN
,
149 typedef unsigned int eventcounter_t
;
151 EVENTCOUNTER_RENDERSTATE_START
= 0,
152 EVENTCOUNTER_GAMESTATE_START
= 1
155 typedef unsigned char statusmessage_t
;
170 const Location
* location
;
171 int current_location
; /* index of loc. in global location table */
173 Player players
[MAXPLAYERS
];
175 unsigned int serving_player
;
177 unsigned int current_set
;
179 soundevent_t sound_events
;
181 unsigned int score_time
;
182 eventcounter_t ec_game
;
183 eventcounter_t ec_sets
;
184 statusmessage_t status_message
;
188 soundevent_t sound_events
;
190 eventcounter_t ec_game
;
191 eventcounter_t ec_sets
;
192 statusmessage_t status_message
;
193 const char* text_status
;
194 const char* text_game
;
195 const char* text_sets
;
200 #define GAME_TICKS 15
204 #define RACKET_X_MID 15
205 #define RACKET_Y_MID 24
210 #define GAME_X_MIN 41.0*2
211 #define GAME_X_MAX 270.0*2
212 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
214 #define GAME_Y_MIN 155.0
215 #define GAME_Y_MAX 330.0
216 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
218 /* net size and collision detection */
222 #define NET_HEIGHT 273
223 #define NET_TALLNESS 5
224 #define IN_NET_X(x) (x+BALL_X_MID >= NET_X && x-BALL_X_MID <= NET_X+NET_WIDTH)
225 #define IN_NET_Y(y) (y >= NET_Y && y <= NET_Y+NET_HEIGHT)
226 #define NET_COLLISION(x, y, z) (z<=NET_TALLNESS && IN_NET_X(x) && IN_NET_Y(y))
227 #define NET_COLLISION_BALL(b) (NET_COLLISION(b.x, b.y, b.z))
229 /* "in field" coordinates (left and right part of court) */
230 #define FIELD_LEFT_X 119
231 #define FIELD_LEFT_WIDTH 202
232 #define FIELD_LEFT_IS_VALID(x) (x >= FIELD_LEFT_X && x <= FIELD_LEFT_X+FIELD_LEFT_WIDTH)
233 #define FIELD_RIGHT_X 326
234 #define FIELD_RIGHT_WIDTH 202
235 #define FIELD_RIGHT_IS_VALID(x) (x >= FIELD_RIGHT_X && x <= FIELD_RIGHT_X+FIELD_RIGHT_WIDTH)
237 /* coordinates for one-vs-one game */
238 #define FIELD_SINGLE_Y 154
239 #define FIELD_SINGLE_HEIGHT 178
240 #define FIELD_SINGLE_IS_VALID(y) (y >= FIELD_SINGLE_Y && y <= FIELD_SINGLE_Y+FIELD_SINGLE_HEIGHT)
242 /* coordinates for two-vs-two game */
243 #define FIELD_DOUBLE_Y 115
244 #define FIELD_DOUBLE_HEIGHT 256
245 #define FIELD_DOUBLE_IS_VALID(y) (y >= FIELD_DOUBLE_Y && y <= FIELD_DOUBLE_Y+FIELD_DOUBLE_HEIGHT)
247 /* player = player that last hit the ball, */
248 #define GROUND_IS_VALID(player, x, y) (FIELD_SINGLE_IS_VALID(y) && ((player==1)?(FIELD_RIGHT_IS_VALID(x)):(FIELD_LEFT_IS_VALID(x))))
249 #define IS_OUT(x, y) (!(FIELD_SINGLE_IS_VALID(y) && (FIELD_RIGHT_IS_VALID(x) || FIELD_LEFT_IS_VALID(x))))
251 #define PLAYER_Y_MIN 0
252 #define PLAYER_Y_MAX HEIGHT
254 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
255 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
256 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
258 #define PLAYER_AREA_Y RACKET_Y_MID
259 #define PLAYER_AREA_X RACKET_X_MID
260 #define IS_NEAR_Y(py,by) (fabsf((py)-(by))<(float)PLAYER_AREA_Y)
261 #define IS_NEAR_Y_AI(py,by) (fabsf((py)-(by))<(float)PLAYER_AREA_Y/3.5)
262 #define IS_NEAR_X(px,bx) (fabsf((px)-(bx))<(float)PLAYER_AREA_X)
263 #define IS_NEAR_X_AI(px,bx) (fabsf((px)-(bx))<(float)PLAYER_AREA_X*2.)
265 #define PLAYER_MOVE_Y 5.0
266 #define PLAYER_ACCEL_DEFAULT 0.2
267 #define PLAYER_ACCEL_INCREASE 1.2
269 #define POWER_UP_FACTOR 1.1
270 #define POWER_DOWN_FACTOR 0.9
271 #define PLAYER_POWER_MAX 100
273 #define PLAYER(s, num) (s->players[num-1])
275 /* Comment out the following #define to enable mouse control */
278 /* GameState handling*/
279 GameState
*gamestate_new();
281 int gamestate_save(GameState
* s
, const char* filename
);
282 GameState
* gamestate_load(const char* filename
);
284 /* Game module functions */
285 void gameloop(GameState
*);
286 void render(const GameState
*, RenderState
*);
287 bool step( GameState
*);
288 void limit_value( float*, float, float);
289 float get_move_y( GameState
*, unsigned char);
290 void input_human(GameState
*, int);
291 void input_ai(GameState
*, int);
292 void game_setup_serve( GameState
*);
294 int score_game(GameState
*);
295 int game_get_winner( GameState
*);
297 /* Helper functions for the renderer */
298 const char* format_sets(const GameState
*);
299 const char* format_game(const GameState
*);
300 const char* format_status(const GameState
*);