793a7865d259f00589ebbbff7926d25f561aa13b
[tennix.git] / game.h
blob793a7865d259f00589ebbbff7926d25f561aa13b
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
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 #ifndef __GAME_H
25 #define __GAME_H
27 #include <math.h>
28 #include "tennix.h"
29 #include "sound.h"
30 #include "graphics.h"
31 #include "input.h"
33 #define SETS_TO_WIN 3
35 #define MAXPLAYERS 2
37 #ifdef DELUXE_EDITION
38 # define BALL_STATES 16
39 #else
40 # define BALL_STATES 4
41 #endif
43 typedef unsigned char bool;
44 enum {
45 false,
46 true
49 typedef unsigned char referee_t;
50 enum {
51 REFEREE_NORMAL,
52 REFEREE_OUT,
53 REFEREE_PLAYER1,
54 REFEREE_PLAYER2,
55 REFEREE_COUNT
58 enum {
59 WINNER_NONE,
60 WINNER_PLAYER1,
61 WINNER_PLAYER2
64 enum {
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;
77 typedef struct {
78 const char* name;
79 const char* area;
80 const char* city;
81 image_id court_type;
82 unsigned int max_visitors;
83 const char* court_type_name;
84 image_id photo;
85 unsigned int photo_frames;
86 unsigned int worldmap_x;
87 unsigned int worldmap_y;
88 bool has_referee;
89 } Location;
91 typedef struct {
92 float x;
93 float y;
94 float z;
95 float move_x;
96 float move_y;
97 float move_z;
98 float restitution;
99 bool ground_hit;
100 int last_hit_by;
101 bool inhibit_gravity;
102 } Ball;
104 typedef struct {
105 InputDevice* input;
106 int input_device_index;
107 float x;
108 float y;
109 float power;
110 float power_up_factor;
111 float power_down_factor;
112 bool use_power;
113 unsigned int score;
114 unsigned char desire;
115 bool type; /* is this player ai-controlled or human? */
116 int game; /* score for the current game */
117 int sets[SETS_TO_WIN*2]; /* score for each set */
118 float accelerate; /* a value [0..1] how fast the user accelerates */
119 } Player;
121 enum {
122 PLAYER_TYPE_HUMAN,
123 PLAYER_TYPE_AI
126 enum {
127 DESIRE_NORMAL,
128 DESIRE_TOPSPIN,
129 DESIRE_SMASH,
130 DESIRE_MAX
133 /* wait 2 seconds before we score the game */
134 #define SCORING_DELAY 2000
136 enum {
137 SCORE_UNDECIDED,
138 SCORE_EVENT_NET,
139 SCORE_EVENT_OUT,
140 SCORE_EVENT_GROUND_INVALID,
141 SCORE_EVENT_GROUND_VALID,
142 SCORE_EVENT_OFFSCREEN,
143 SCORE_EVENT_MAX
146 typedef unsigned int eventcounter_t;
147 enum {
148 EVENTCOUNTER_RENDERSTATE_START = 0,
149 EVENTCOUNTER_GAMESTATE_START = 1
152 typedef unsigned char statusmessage_t;
153 enum {
154 STATUSMSG_NONE,
155 STATUSMSG_WELCOME,
156 STATUSMSG_DEFAULT,
157 STATUSMSG_NET,
158 STATUSMSG_OUT,
159 STATUSMSG_FAULT,
160 STATUSMSG_P1SCORES,
161 STATUSMSG_P2SCORES,
162 STATUSMSG_VOLLEY,
163 STATUSMSG_DIDNTCATCH
166 typedef struct {
167 Location* location;
168 int current_location; /* index of loc. in global location table */
169 Ball ball;
170 Player players[MAXPLAYERS];
171 Uint32 time;
172 unsigned int serving_player;
173 referee_t referee;
174 unsigned int current_set;
175 int winner;
176 soundevent_t sound_events;
177 unsigned int rain;
178 unsigned int fog;
179 bool night;
180 int wind;
181 int score_event;
182 unsigned int score_time;
183 eventcounter_t ec_game;
184 eventcounter_t ec_sets;
185 statusmessage_t status_message;
186 } GameState;
188 typedef struct {
189 soundevent_t sound_events;
190 referee_t referee;
191 eventcounter_t ec_game;
192 eventcounter_t ec_sets;
193 statusmessage_t status_message;
194 const char* text_status;
195 const char* text_game;
196 const char* text_sets;
197 } RenderState;
199 #define PI 3.1415
201 #define GAME_TICKS 15
203 #define GRAVITY -.03
205 #define RACKET_X_MID 15
206 #define RACKET_Y_MID 24
208 #define BALL_X_MID 9
209 #define BALL_Y_MID 9
211 #define GAME_X_MIN 41.0*2
212 #define GAME_X_MAX 270.0*2
213 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
215 #define GAME_Y_MIN 155.0
216 #define GAME_Y_MAX 330.0
217 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
219 /* net size and collision detection */
220 #define NET_X 320
221 #define NET_Y 107
222 #define NET_WIDTH 5
223 #define NET_HEIGHT 273
224 #define NET_TALLNESS 5
225 #define IN_NET_X(x) (x+BALL_X_MID >= NET_X && x-BALL_X_MID <= NET_X+NET_WIDTH)
226 #define IN_NET_Y(y) (y >= NET_Y && y <= NET_Y+NET_HEIGHT)
227 #define NET_COLLISION(x, y, z) (z<=NET_TALLNESS && IN_NET_X(x) && IN_NET_Y(y))
228 #define NET_COLLISION_BALL(b) (NET_COLLISION(b.x, b.y, b.z))
230 /* "in field" coordinates (left and right part of court) */
231 #define FIELD_LEFT_X 119
232 #define FIELD_LEFT_WIDTH 202
233 #define FIELD_LEFT_IS_VALID(x) (x >= FIELD_LEFT_X && x <= FIELD_LEFT_X+FIELD_LEFT_WIDTH)
234 #define FIELD_RIGHT_X 326
235 #define FIELD_RIGHT_WIDTH 202
236 #define FIELD_RIGHT_IS_VALID(x) (x >= FIELD_RIGHT_X && x <= FIELD_RIGHT_X+FIELD_RIGHT_WIDTH)
238 /* coordinates for one-vs-one game */
239 #define FIELD_SINGLE_Y 154
240 #define FIELD_SINGLE_HEIGHT 178
241 #define FIELD_SINGLE_IS_VALID(y) (y >= FIELD_SINGLE_Y && y <= FIELD_SINGLE_Y+FIELD_SINGLE_HEIGHT)
243 /* coordinates for two-vs-two game */
244 #define FIELD_DOUBLE_Y 115
245 #define FIELD_DOUBLE_HEIGHT 256
246 #define FIELD_DOUBLE_IS_VALID(y) (y >= FIELD_DOUBLE_Y && y <= FIELD_DOUBLE_Y+FIELD_DOUBLE_HEIGHT)
248 /* player = player that last hit the ball, */
249 #define GROUND_IS_VALID(player, x, y) (FIELD_SINGLE_IS_VALID(y) && ((player==1)?(FIELD_RIGHT_IS_VALID(x)):(FIELD_LEFT_IS_VALID(x))))
250 #define IS_OUT(x, y) (!(FIELD_SINGLE_IS_VALID(y) && (FIELD_RIGHT_IS_VALID(x) || FIELD_LEFT_IS_VALID(x))))
252 #define PLAYER_Y_MIN 0
253 #define PLAYER_Y_MAX HEIGHT
255 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
256 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
257 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
259 #define PLAYER_AREA_Y RACKET_Y_MID
260 #define PLAYER_AREA_X RACKET_X_MID
261 #define IS_NEAR_Y(py,by) (fabsf((py)-(by))<(float)PLAYER_AREA_Y)
262 #define IS_NEAR_Y_AI(py,by) (fabsf((py)-(by))<(float)PLAYER_AREA_Y/3.5)
263 #define IS_NEAR_X(px,bx) (fabsf((px)-(bx))<(float)PLAYER_AREA_X)
264 #define IS_NEAR_X_AI(px,bx) (fabsf((px)-(bx))<(float)PLAYER_AREA_X*2.)
266 #define PLAYER_MOVE_Y 5.0
267 #define PLAYER_ACCEL_DEFAULT 0.2
268 #define PLAYER_ACCEL_INCREASE 1.2
270 #define POWER_UP_FACTOR 1.1
271 #define POWER_DOWN_FACTOR 0.9
272 #define PLAYER_POWER_MAX 100
274 #define MOVE_Y_SEED 3
276 #define PLAYER(s, num) (s->players[num-1])
278 /* Comment out the following #define to enable mouse control */
279 #define ENABLE_MOUSE
281 /* GameState handling*/
282 GameState *gamestate_new();
284 int gamestate_save(GameState* s, const char* filename);
285 GameState* gamestate_load(const char* filename);
287 /* Game module functions */
288 void gameloop(GameState*);
289 void render(const GameState*, RenderState*);
290 bool step( GameState*);
291 void limit_value( float*, float, float);
292 float get_move_y( GameState*, unsigned char);
293 void input_human(GameState*, int);
294 void input_ai(GameState*, int);
295 void game_setup_serve( GameState*);
297 int score_game(GameState*);
298 int game_get_winner( GameState*);
300 /* Helper functions for the renderer */
301 const char* format_sets(const GameState*);
302 const char* format_game(const GameState*);
303 const char* format_status(const GameState*);
305 #endif