New input selection UI + Mouse support, updated TODO list
[tennix.git] / game.h
blobde6f37f75b673b68cf77d5409d95b8681d4408d2
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 NGRAM_STEPS 6
37 #define MAXPLAYERS 2
39 #ifdef DELUXE_EDITION
40 # define BALL_STATES 16
41 #else
42 # define BALL_STATES 4
43 #endif
45 typedef unsigned char bool;
46 enum {
47 false,
48 true
51 #ifdef EXTENDED_REFEREE
52 enum {
53 REFEREE_NORMAL,
54 REFEREE_OUT,
55 REFEREE_PLAYER1,
56 REFEREE_PLAYER2,
57 REFEREE_COUNT
59 #else
60 enum {
61 REFEREE_NORMAL,
62 REFEREE_PLAYER1,
63 REFEREE_OUT,
64 REFEREE_PLAYER2,
65 REFEREE_COUNT
67 #endif
69 enum {
70 WINNER_NONE,
71 WINNER_PLAYER1,
72 WINNER_PLAYER2
75 enum {
76 SOUND_EVENT_GROUND = 1<<0,
77 SOUND_EVENT_OUT = 1<<1,
78 SOUND_EVENT_APPLAUSE = 1<<2,
79 SOUND_EVENT_RACKET = 1<<3
82 typedef struct {
83 const char* name;
84 const char* area;
85 const char* city;
86 image_id court_type;
87 unsigned int max_visitors;
88 const char* court_type_name;
89 image_id photo;
90 unsigned int photo_frames;
91 unsigned int worldmap_x;
92 unsigned int worldmap_y;
93 bool has_referee;
94 } Location;
96 typedef struct {
97 float x;
98 float y;
99 float z;
100 float move_x;
101 float move_y;
102 float move_z;
103 float restitution;
104 bool ground_hit;
105 int last_hit_by;
106 bool inhibit_gravity;
107 } Ball;
109 typedef struct {
110 InputDevice* input;
111 int input_device_index;
112 float x;
113 float y;
114 float power;
115 float power_up_factor;
116 float power_down_factor;
117 bool use_power;
118 unsigned int score;
119 unsigned char desire;
120 bool type; /* is this player ai-controlled or human? */
121 float ball_dest; /* prospective y-position of ball */
122 bool state_locked; /* enabled when user keeps pressing the "hit" key */
123 int game; /* score for the current game */
124 int sets[SETS_TO_WIN*2]; /* score for each set */
125 int mouse_x; /* x position of mouse */
126 int mouse_y; /* y position of mouse */
127 float accelerate; /* a value [0..1] how fast the user accelerates */
128 bool mouse_locked; /* on start, ignore unpressed mouse state */
129 } Player;
131 enum {
132 PLAYER_TYPE_HUMAN,
133 PLAYER_TYPE_AI
136 enum {
137 DESIRE_NORMAL,
138 DESIRE_TOPSPIN,
139 DESIRE_SMASH,
140 DESIRE_MAX
143 /* wait 2 seconds before we score the game */
144 #define SCORING_DELAY 2000
146 enum {
147 SCORE_UNDECIDED,
148 SCORE_EVENT_NET,
149 SCORE_EVENT_OUT,
150 SCORE_EVENT_GROUND_INVALID,
151 SCORE_EVENT_GROUND_VALID,
152 SCORE_EVENT_OFFSCREEN,
153 SCORE_EVENT_MAX
156 typedef struct {
157 Location* location;
158 Ball ball;
159 Player players[MAXPLAYERS];
160 Uint32 time;
161 bool was_stopped;
162 unsigned int serving_player;
163 const char* status;
164 char game_score_str[50];
165 char sets_score_str[50];
166 unsigned char referee;
167 unsigned int current_set;
168 int winner;
169 bool is_over;
170 image_id displayed_court_type;
171 unsigned int history[3];
172 unsigned int history_size;
173 bool history_is_locked;
174 unsigned char ngram[NGRAM_STEPS][NGRAM_STEPS][NGRAM_STEPS];
175 float ngram_prediction;
176 unsigned int sound_events;
177 float joystick_y;
178 float joystick_x;
179 unsigned char joystick_a;
180 unsigned int rain;
181 unsigned int fog;
182 bool night;
183 int wind;
184 unsigned int windtime;
185 Uint32 timelimit;
186 bool text_changed;
187 unsigned char old_referee;
188 int score_event;
189 unsigned int score_time;
190 } GameState;
192 #define PI 3.1415
194 #define GAME_TICKS 15
196 #define GRAVITY -.03
198 #define RACKET_X_MID 15
199 #define RACKET_Y_MID 24
201 #define BALL_X_MID 9
202 #define BALL_Y_MID 9
204 #define GAME_X_MIN 41.0*2
205 #define GAME_X_MAX 270.0*2
206 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
208 #define GAME_Y_MIN 155.0
209 #define GAME_Y_MAX 330.0
210 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
212 /* net size and collision detection */
213 #define NET_X 320
214 #define NET_Y 107
215 #define NET_WIDTH 5
216 #define NET_HEIGHT 273
217 #define NET_TALLNESS 5
218 #define IN_NET_X(x) (x+BALL_X_MID >= NET_X && x-BALL_X_MID <= NET_X+NET_WIDTH)
219 #define IN_NET_Y(y) (y >= NET_Y && y <= NET_Y+NET_HEIGHT)
220 #define NET_COLLISION(x, y, z) (z<=NET_TALLNESS && IN_NET_X(x) && IN_NET_Y(y))
221 #define NET_COLLISION_BALL(b) (NET_COLLISION(b.x, b.y, b.z))
223 /* "in field" coordinates (left and right part of court) */
224 #define FIELD_LEFT_X 119
225 #define FIELD_LEFT_WIDTH 202
226 #define FIELD_LEFT_IS_VALID(x) (x >= FIELD_LEFT_X && x <= FIELD_LEFT_X+FIELD_LEFT_WIDTH)
227 #define FIELD_RIGHT_X 326
228 #define FIELD_RIGHT_WIDTH 202
229 #define FIELD_RIGHT_IS_VALID(x) (x >= FIELD_RIGHT_X && x <= FIELD_RIGHT_X+FIELD_RIGHT_WIDTH)
231 /* coordinates for one-vs-one game */
232 #define FIELD_SINGLE_Y 154
233 #define FIELD_SINGLE_HEIGHT 178
234 #define FIELD_SINGLE_IS_VALID(y) (y >= FIELD_SINGLE_Y && y <= FIELD_SINGLE_Y+FIELD_SINGLE_HEIGHT)
236 /* coordinates for two-vs-two game */
237 #define FIELD_DOUBLE_Y 115
238 #define FIELD_DOUBLE_HEIGHT 256
239 #define FIELD_DOUBLE_IS_VALID(y) (y >= FIELD_DOUBLE_Y && y <= FIELD_DOUBLE_Y+FIELD_DOUBLE_HEIGHT)
241 /* player = player that last hit the ball, */
242 #define GROUND_IS_VALID(player, x, y) (FIELD_SINGLE_IS_VALID(y) && ((player==1)?(FIELD_RIGHT_IS_VALID(x)):(FIELD_LEFT_IS_VALID(x))))
243 #define IS_OUT(x, y) (!(FIELD_SINGLE_IS_VALID(y) && (FIELD_RIGHT_IS_VALID(x) || FIELD_LEFT_IS_VALID(x))))
245 #define PLAYER_Y_MIN 0
246 #define PLAYER_Y_MAX HEIGHT
248 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
249 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
250 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
252 #define PLAYER_AREA_Y RACKET_Y_MID
253 #define PLAYER_AREA_X RACKET_X_MID
254 #define IS_NEAR_Y(py,by) (absf((py)-(by))<(float)PLAYER_AREA_Y)
255 #define IS_NEAR_Y_AI(py,by) (absf((py)-(by))<(float)PLAYER_AREA_Y/3.5)
256 #define IS_NEAR_X(px,bx) (absf((px)-(bx))<(float)PLAYER_AREA_X)
257 #define IS_NEAR_X_AI(px,bx) (absf((px)-(bx))<(float)PLAYER_AREA_X*2.)
259 #define PLAYER_MOVE_Y 5.0
260 #define PLAYER_ACCEL_DEFAULT 0.2
261 #define PLAYER_ACCEL_INCREASE 1.2
263 #define POWER_UP_FACTOR 1.1
264 #define POWER_DOWN_FACTOR 0.9
265 #define PLAYER_POWER_MAX 100
267 #define MOVE_Y_SEED 3
269 #define PLAYER(s, num) (s->players[num-1])
271 /* Comment out the following #define to enable mouse control */
272 #define ENABLE_MOUSE
274 /* GameState handling*/
275 GameState *gamestate_new();
278 /* Game module functions */
279 void gameloop(GameState*);
280 void render( GameState*);
281 bool step( GameState*);
282 void limit_value( float*, float, float);
283 float get_move_y( GameState*, unsigned char);
284 void input_human(GameState*, int);
285 void input_ai(GameState*, int);
286 void game_setup_serve( GameState*);
287 float ngram_predictor( GameState*);
289 int score_game(GameState*);
290 char* format_sets( GameState*);
291 char* format_game( GameState*);
292 char* format_status( GameState*);
293 int game_get_winner( GameState*);
295 #endif