Remove n-gram predictor which didn't work for a while
[tennix.git] / game.h
blob943b3020daef8a0831225e4a01c7cf19237f3bee
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 enum {
50 REFEREE_NORMAL,
51 REFEREE_OUT,
52 REFEREE_PLAYER1,
53 REFEREE_PLAYER2,
54 REFEREE_COUNT
57 enum {
58 WINNER_NONE,
59 WINNER_PLAYER1,
60 WINNER_PLAYER2
63 enum {
64 SOUND_EVENT_GROUND = 1<<0,
65 SOUND_EVENT_OUT = 1<<1,
66 SOUND_EVENT_APPLAUSE = 1<<2,
67 SOUND_EVENT_RACKET = 1<<3
70 typedef struct {
71 const char* name;
72 const char* area;
73 const char* city;
74 image_id court_type;
75 unsigned int max_visitors;
76 const char* court_type_name;
77 image_id photo;
78 unsigned int photo_frames;
79 unsigned int worldmap_x;
80 unsigned int worldmap_y;
81 bool has_referee;
82 } Location;
84 typedef struct {
85 float x;
86 float y;
87 float z;
88 float move_x;
89 float move_y;
90 float move_z;
91 float restitution;
92 bool ground_hit;
93 int last_hit_by;
94 bool inhibit_gravity;
95 } Ball;
97 typedef struct {
98 InputDevice* input;
99 int input_device_index;
100 float x;
101 float y;
102 float power;
103 float power_up_factor;
104 float power_down_factor;
105 bool use_power;
106 unsigned int score;
107 unsigned char desire;
108 bool type; /* is this player ai-controlled or human? */
109 float ball_dest; /* prospective y-position of ball */
110 bool state_locked; /* enabled when user keeps pressing the "hit" key */
111 int game; /* score for the current game */
112 int sets[SETS_TO_WIN*2]; /* score for each set */
113 int mouse_x; /* x position of mouse */
114 int mouse_y; /* y position of mouse */
115 float accelerate; /* a value [0..1] how fast the user accelerates */
116 bool mouse_locked; /* on start, ignore unpressed mouse state */
117 } Player;
119 enum {
120 PLAYER_TYPE_HUMAN,
121 PLAYER_TYPE_AI
124 enum {
125 DESIRE_NORMAL,
126 DESIRE_TOPSPIN,
127 DESIRE_SMASH,
128 DESIRE_MAX
131 /* wait 2 seconds before we score the game */
132 #define SCORING_DELAY 2000
134 enum {
135 SCORE_UNDECIDED,
136 SCORE_EVENT_NET,
137 SCORE_EVENT_OUT,
138 SCORE_EVENT_GROUND_INVALID,
139 SCORE_EVENT_GROUND_VALID,
140 SCORE_EVENT_OFFSCREEN,
141 SCORE_EVENT_MAX
144 typedef struct {
145 Location* location;
146 int current_location; /* index of loc. in global location table */
147 Ball ball;
148 Player players[MAXPLAYERS];
149 Uint32 time;
150 bool was_stopped;
151 unsigned int serving_player;
152 const char* status;
153 char game_score_str[50];
154 char sets_score_str[50];
155 unsigned char referee;
156 unsigned int current_set;
157 int winner;
158 bool is_over;
159 image_id displayed_court_type;
160 unsigned int sound_events;
161 float joystick_y;
162 float joystick_x;
163 unsigned char joystick_a;
164 unsigned int rain;
165 unsigned int fog;
166 bool night;
167 int wind;
168 unsigned int windtime;
169 Uint32 timelimit;
170 bool text_changed;
171 unsigned char old_referee;
172 int score_event;
173 unsigned int score_time;
174 } GameState;
176 #define PI 3.1415
178 #define GAME_TICKS 15
180 #define GRAVITY -.03
182 #define RACKET_X_MID 15
183 #define RACKET_Y_MID 24
185 #define BALL_X_MID 9
186 #define BALL_Y_MID 9
188 #define GAME_X_MIN 41.0*2
189 #define GAME_X_MAX 270.0*2
190 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
192 #define GAME_Y_MIN 155.0
193 #define GAME_Y_MAX 330.0
194 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
196 /* net size and collision detection */
197 #define NET_X 320
198 #define NET_Y 107
199 #define NET_WIDTH 5
200 #define NET_HEIGHT 273
201 #define NET_TALLNESS 5
202 #define IN_NET_X(x) (x+BALL_X_MID >= NET_X && x-BALL_X_MID <= NET_X+NET_WIDTH)
203 #define IN_NET_Y(y) (y >= NET_Y && y <= NET_Y+NET_HEIGHT)
204 #define NET_COLLISION(x, y, z) (z<=NET_TALLNESS && IN_NET_X(x) && IN_NET_Y(y))
205 #define NET_COLLISION_BALL(b) (NET_COLLISION(b.x, b.y, b.z))
207 /* "in field" coordinates (left and right part of court) */
208 #define FIELD_LEFT_X 119
209 #define FIELD_LEFT_WIDTH 202
210 #define FIELD_LEFT_IS_VALID(x) (x >= FIELD_LEFT_X && x <= FIELD_LEFT_X+FIELD_LEFT_WIDTH)
211 #define FIELD_RIGHT_X 326
212 #define FIELD_RIGHT_WIDTH 202
213 #define FIELD_RIGHT_IS_VALID(x) (x >= FIELD_RIGHT_X && x <= FIELD_RIGHT_X+FIELD_RIGHT_WIDTH)
215 /* coordinates for one-vs-one game */
216 #define FIELD_SINGLE_Y 154
217 #define FIELD_SINGLE_HEIGHT 178
218 #define FIELD_SINGLE_IS_VALID(y) (y >= FIELD_SINGLE_Y && y <= FIELD_SINGLE_Y+FIELD_SINGLE_HEIGHT)
220 /* coordinates for two-vs-two game */
221 #define FIELD_DOUBLE_Y 115
222 #define FIELD_DOUBLE_HEIGHT 256
223 #define FIELD_DOUBLE_IS_VALID(y) (y >= FIELD_DOUBLE_Y && y <= FIELD_DOUBLE_Y+FIELD_DOUBLE_HEIGHT)
225 /* player = player that last hit the ball, */
226 #define GROUND_IS_VALID(player, x, y) (FIELD_SINGLE_IS_VALID(y) && ((player==1)?(FIELD_RIGHT_IS_VALID(x)):(FIELD_LEFT_IS_VALID(x))))
227 #define IS_OUT(x, y) (!(FIELD_SINGLE_IS_VALID(y) && (FIELD_RIGHT_IS_VALID(x) || FIELD_LEFT_IS_VALID(x))))
229 #define PLAYER_Y_MIN 0
230 #define PLAYER_Y_MAX HEIGHT
232 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
233 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
234 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
236 #define PLAYER_AREA_Y RACKET_Y_MID
237 #define PLAYER_AREA_X RACKET_X_MID
238 #define IS_NEAR_Y(py,by) (fabsf((py)-(by))<(float)PLAYER_AREA_Y)
239 #define IS_NEAR_Y_AI(py,by) (fabsf((py)-(by))<(float)PLAYER_AREA_Y/3.5)
240 #define IS_NEAR_X(px,bx) (fabsf((px)-(bx))<(float)PLAYER_AREA_X)
241 #define IS_NEAR_X_AI(px,bx) (fabsf((px)-(bx))<(float)PLAYER_AREA_X*2.)
243 #define PLAYER_MOVE_Y 5.0
244 #define PLAYER_ACCEL_DEFAULT 0.2
245 #define PLAYER_ACCEL_INCREASE 1.2
247 #define POWER_UP_FACTOR 1.1
248 #define POWER_DOWN_FACTOR 0.9
249 #define PLAYER_POWER_MAX 100
251 #define MOVE_Y_SEED 3
253 #define PLAYER(s, num) (s->players[num-1])
255 /* Comment out the following #define to enable mouse control */
256 #define ENABLE_MOUSE
258 /* GameState handling*/
259 GameState *gamestate_new();
261 int gamestate_save(GameState* s, const char* filename);
262 GameState* gamestate_load(const char* filename);
264 /* Game module functions */
265 void gameloop(GameState*);
266 void render( GameState*);
267 bool step( GameState*);
268 void limit_value( float*, float, float);
269 float get_move_y( GameState*, unsigned char);
270 void input_human(GameState*, int);
271 void input_ai(GameState*, int);
272 void game_setup_serve( GameState*);
274 int score_game(GameState*);
275 char* format_sets( GameState*);
276 char* format_game( GameState*);
277 char* format_status( GameState*);
278 int game_get_winner( GameState*);
280 #endif