Add some missing #include lines, use C99 as standard
[tennix.git] / game.h
blob235dab97e3fe3fd2859041e0899e3c73399d17b2
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"
31 #define SETS_TO_WIN 3
33 #define NGRAM_STEPS 6
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 #ifdef EXTENDED_REFEREE
50 enum {
51 REFEREE_NORMAL,
52 REFEREE_OUT,
53 REFEREE_PLAYER1,
54 REFEREE_PLAYER2,
55 REFEREE_COUNT
57 #else
58 enum {
59 REFEREE_NORMAL,
60 REFEREE_PLAYER1,
61 REFEREE_OUT,
62 REFEREE_PLAYER2,
63 REFEREE_COUNT
65 #endif
67 enum {
68 WINNER_NONE,
69 WINNER_PLAYER1,
70 WINNER_PLAYER2
73 typedef struct {
74 float x;
75 float y;
76 float z;
77 float move_x;
78 float move_y;
79 float move_z;
80 bool ground_hit;
81 int last_hit_by;
82 bool inhibit_gravity;
83 } Ball;
85 typedef struct {
86 float x;
87 float y;
88 unsigned char state;
89 unsigned int score;
90 unsigned char desire;
91 bool type; /* is this player ai-controlled or human? */
92 float ball_dest; /* prospective y-position of ball */
93 bool state_locked; /* enabled when user keeps pressing the "hit" key */
94 int game; /* score for the current game */
95 int sets[SETS_TO_WIN*2]; /* score for each set */
96 int mouse_x; /* x position of mouse */
97 int mouse_y; /* y position of mouse */
98 float accelerate; /* a value [0..1] how fast the user accelerates */
99 bool mouse_locked; /* on start, ignore unpressed mouse state */
100 } Player;
102 enum {
103 PLAYER_TYPE_HUMAN,
104 PLAYER_TYPE_AI
107 enum {
108 DESIRE_NORMAL,
109 DESIRE_TOPSPIN,
110 DESIRE_SMASH,
111 DESIRE_MAX
114 /* wait 2 seconds before we score the game */
115 #define SCORING_DELAY 2000
117 enum {
118 SCORE_UNDECIDED,
119 SCORE_EVENT_NET,
120 SCORE_EVENT_OUT,
121 SCORE_EVENT_GROUND_INVALID,
122 SCORE_EVENT_GROUND_VALID,
123 SCORE_EVENT_OFFSCREEN,
124 SCORE_EVENT_MAX
127 typedef struct {
128 Ball ball;
129 Player players[MAXPLAYERS];
130 Uint32 time;
131 bool was_stopped;
132 unsigned int serving_player;
133 const char* status;
134 char game_score_str[50];
135 char sets_score_str[50];
136 unsigned char referee;
137 unsigned int current_set;
138 int winner;
139 bool is_over;
140 unsigned int court_type;
141 unsigned int old_court_type;
142 unsigned int history[3];
143 unsigned int history_size;
144 bool history_is_locked;
145 unsigned char ngram[NGRAM_STEPS][NGRAM_STEPS][NGRAM_STEPS];
146 float ngram_prediction;
147 sound_id play_sound;
148 float joystick_y;
149 float joystick_x;
150 unsigned char joystick_a;
151 unsigned int rain;
152 unsigned int fog;
153 bool night;
154 int wind;
155 unsigned int windtime;
156 Uint32 timelimit;
157 bool text_changed;
158 unsigned char old_referee;
159 int score_event;
160 unsigned int score_time;
161 } GameState;
163 #define PI 3.1415
165 #define GAME_TICKS 15
167 #define GRAVITY -.03
168 #define RESTITUTION 0.6
170 #define RACKET_X_MID 15
171 #define RACKET_Y_MID 24
173 #define BALL_X_MID 9
174 #define BALL_Y_MID 9
176 #define GAME_X_MIN 41.0*2
177 #define GAME_X_MAX 270.0*2
178 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
180 #define GAME_Y_MIN 155.0
181 #define GAME_Y_MAX 330.0
182 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
184 /* net size and collision detection */
185 #define NET_X 320
186 #define NET_Y 107
187 #define NET_WIDTH 5
188 #define NET_HEIGHT 273
189 #define NET_TALLNESS 5
190 #define IN_NET_X(x) (x+BALL_X_MID >= NET_X && x-BALL_X_MID <= NET_X+NET_WIDTH)
191 #define IN_NET_Y(y) (y >= NET_Y && y <= NET_Y+NET_HEIGHT)
192 #define NET_COLLISION(x, y, z) (z<=NET_TALLNESS && IN_NET_X(x) && IN_NET_Y(y))
193 #define NET_COLLISION_BALL(b) (NET_COLLISION(b.x, b.y, b.z))
195 /* "in field" coordinates (left and right part of court) */
196 #define FIELD_LEFT_X 119
197 #define FIELD_LEFT_WIDTH 202
198 #define FIELD_LEFT_IS_VALID(x) (x >= FIELD_LEFT_X && x <= FIELD_LEFT_X+FIELD_LEFT_WIDTH)
199 #define FIELD_RIGHT_X 326
200 #define FIELD_RIGHT_WIDTH 202
201 #define FIELD_RIGHT_IS_VALID(x) (x >= FIELD_RIGHT_X && x <= FIELD_RIGHT_X+FIELD_RIGHT_WIDTH)
203 /* coordinates for one-vs-one game */
204 #define FIELD_SINGLE_Y 154
205 #define FIELD_SINGLE_HEIGHT 178
206 #define FIELD_SINGLE_IS_VALID(y) (y >= FIELD_SINGLE_Y && y <= FIELD_SINGLE_Y+FIELD_SINGLE_HEIGHT)
208 /* coordinates for two-vs-two game */
209 #define FIELD_DOUBLE_Y 115
210 #define FIELD_DOUBLE_HEIGHT 256
211 #define FIELD_DOUBLE_IS_VALID(y) (y >= FIELD_DOUBLE_Y && y <= FIELD_DOUBLE_Y+FIELD_DOUBLE_HEIGHT)
213 /* player = player that last hit the ball, */
214 #define GROUND_IS_VALID(player, x, y) (FIELD_SINGLE_IS_VALID(y) && ((player==1)?(FIELD_RIGHT_IS_VALID(x)):(FIELD_LEFT_IS_VALID(x))))
215 #define IS_OUT(x, y) (!(FIELD_SINGLE_IS_VALID(y) && (FIELD_RIGHT_IS_VALID(x) || FIELD_LEFT_IS_VALID(x))))
217 #define PLAYER_Y_MIN 0
218 #define PLAYER_Y_MAX HEIGHT
220 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
221 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
222 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
224 #define PLAYER_AREA_Y RACKET_Y_MID
225 #define PLAYER_AREA_X RACKET_X_MID
226 #define IS_NEAR_Y(py,by) (fabsf((py)-(by))<PLAYER_AREA_Y)
227 #define IS_NEAR_Y_AI(py,by) (fabsf((py)-(by))<PLAYER_AREA_Y/3.5)
228 #define IS_NEAR_X(px,bx) (fabsf((px)-(bx))<PLAYER_AREA_X)
229 #define IS_NEAR_X_AI(px,bx) (fabsf((px)-(bx))<PLAYER_AREA_X*2)
231 #define PLAYER_MOVE_Y 5.0
232 #define PLAYER_ACCEL_DEFAULT 0.2
233 #define PLAYER_ACCEL_INCREASE 1.2
234 #define PLAYER_STATE_MAX 7
235 #define PLAYER_POWERSHOT 6.2
237 #define MOVE_Y_SEED 3
239 #define PLAYER(s, num) (s->players[num-1])
241 /* Comment out the following #define to enable mouse control */
242 #define ENABLE_MOUSE
244 /* GameState handling*/
245 GameState *gamestate_new();
248 /* Game module functions */
249 void gameloop(GameState*);
250 void render( GameState*);
251 bool step( GameState*);
252 void limit_value( float*, float, float);
253 float get_move_y( GameState*, unsigned char);
254 void input_human( Player*, bool, bool, bool, bool, bool, bool, GameState*);
255 void input_ai( Player*, Ball*, Player*, GameState*);
256 void game_setup_serve( GameState*);
257 float ngram_predictor( GameState*);
259 void score_game(GameState*);
260 char* format_sets( GameState*);
261 char* format_game( GameState*);
262 char* format_status( GameState*);
263 int game_get_winner( GameState*);
265 #endif