New input system with automatic joystick detection
[tennix.git] / game.h
blob20cdb5619d07f68f36a7439bee91759581efc729
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 typedef struct {
76 const char* name;
77 const char* area;
78 const char* city;
79 image_id court_type;
80 unsigned int max_visitors;
81 const char* court_type_name;
82 image_id photo;
83 unsigned int photo_frames;
84 unsigned int worldmap_x;
85 unsigned int worldmap_y;
86 bool has_referee;
87 } Location;
89 typedef struct {
90 float x;
91 float y;
92 float z;
93 float move_x;
94 float move_y;
95 float move_z;
96 float restitution;
97 bool ground_hit;
98 int last_hit_by;
99 bool inhibit_gravity;
100 } Ball;
102 typedef struct {
103 InputDevice* input;
104 int input_device_index;
105 float x;
106 float y;
107 unsigned int power;
108 float power_up_factor;
109 float power_down_factor;
110 bool use_power;
111 unsigned int score;
112 unsigned char desire;
113 bool type; /* is this player ai-controlled or human? */
114 float ball_dest; /* prospective y-position of ball */
115 bool state_locked; /* enabled when user keeps pressing the "hit" key */
116 int game; /* score for the current game */
117 int sets[SETS_TO_WIN*2]; /* score for each set */
118 int mouse_x; /* x position of mouse */
119 int mouse_y; /* y position of mouse */
120 float accelerate; /* a value [0..1] how fast the user accelerates */
121 bool mouse_locked; /* on start, ignore unpressed mouse state */
122 } Player;
124 enum {
125 PLAYER_TYPE_HUMAN,
126 PLAYER_TYPE_AI
129 enum {
130 DESIRE_NORMAL,
131 DESIRE_TOPSPIN,
132 DESIRE_SMASH,
133 DESIRE_MAX
136 /* wait 2 seconds before we score the game */
137 #define SCORING_DELAY 2000
139 enum {
140 SCORE_UNDECIDED,
141 SCORE_EVENT_NET,
142 SCORE_EVENT_OUT,
143 SCORE_EVENT_GROUND_INVALID,
144 SCORE_EVENT_GROUND_VALID,
145 SCORE_EVENT_OFFSCREEN,
146 SCORE_EVENT_MAX
149 typedef struct {
150 Location* location;
151 Ball ball;
152 Player players[MAXPLAYERS];
153 Uint32 time;
154 bool was_stopped;
155 unsigned int serving_player;
156 const char* status;
157 char game_score_str[50];
158 char sets_score_str[50];
159 unsigned char referee;
160 unsigned int current_set;
161 int winner;
162 bool is_over;
163 image_id displayed_court_type;
164 unsigned int history[3];
165 unsigned int history_size;
166 bool history_is_locked;
167 unsigned char ngram[NGRAM_STEPS][NGRAM_STEPS][NGRAM_STEPS];
168 float ngram_prediction;
169 sound_id play_sound;
170 float joystick_y;
171 float joystick_x;
172 unsigned char joystick_a;
173 unsigned int rain;
174 unsigned int fog;
175 bool night;
176 int wind;
177 unsigned int windtime;
178 Uint32 timelimit;
179 bool text_changed;
180 unsigned char old_referee;
181 int score_event;
182 unsigned int score_time;
183 } GameState;
185 #define PI 3.1415
187 #define GAME_TICKS 15
189 #define GRAVITY -.03
191 #define RACKET_X_MID 15
192 #define RACKET_Y_MID 24
194 #define BALL_X_MID 9
195 #define BALL_Y_MID 9
197 #define GAME_X_MIN 41.0*2
198 #define GAME_X_MAX 270.0*2
199 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
201 #define GAME_Y_MIN 155.0
202 #define GAME_Y_MAX 330.0
203 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
205 /* net size and collision detection */
206 #define NET_X 320
207 #define NET_Y 107
208 #define NET_WIDTH 5
209 #define NET_HEIGHT 273
210 #define NET_TALLNESS 5
211 #define IN_NET_X(x) (x+BALL_X_MID >= NET_X && x-BALL_X_MID <= NET_X+NET_WIDTH)
212 #define IN_NET_Y(y) (y >= NET_Y && y <= NET_Y+NET_HEIGHT)
213 #define NET_COLLISION(x, y, z) (z<=NET_TALLNESS && IN_NET_X(x) && IN_NET_Y(y))
214 #define NET_COLLISION_BALL(b) (NET_COLLISION(b.x, b.y, b.z))
216 /* "in field" coordinates (left and right part of court) */
217 #define FIELD_LEFT_X 119
218 #define FIELD_LEFT_WIDTH 202
219 #define FIELD_LEFT_IS_VALID(x) (x >= FIELD_LEFT_X && x <= FIELD_LEFT_X+FIELD_LEFT_WIDTH)
220 #define FIELD_RIGHT_X 326
221 #define FIELD_RIGHT_WIDTH 202
222 #define FIELD_RIGHT_IS_VALID(x) (x >= FIELD_RIGHT_X && x <= FIELD_RIGHT_X+FIELD_RIGHT_WIDTH)
224 /* coordinates for one-vs-one game */
225 #define FIELD_SINGLE_Y 154
226 #define FIELD_SINGLE_HEIGHT 178
227 #define FIELD_SINGLE_IS_VALID(y) (y >= FIELD_SINGLE_Y && y <= FIELD_SINGLE_Y+FIELD_SINGLE_HEIGHT)
229 /* coordinates for two-vs-two game */
230 #define FIELD_DOUBLE_Y 115
231 #define FIELD_DOUBLE_HEIGHT 256
232 #define FIELD_DOUBLE_IS_VALID(y) (y >= FIELD_DOUBLE_Y && y <= FIELD_DOUBLE_Y+FIELD_DOUBLE_HEIGHT)
234 /* player = player that last hit the ball, */
235 #define GROUND_IS_VALID(player, x, y) (FIELD_SINGLE_IS_VALID(y) && ((player==1)?(FIELD_RIGHT_IS_VALID(x)):(FIELD_LEFT_IS_VALID(x))))
236 #define IS_OUT(x, y) (!(FIELD_SINGLE_IS_VALID(y) && (FIELD_RIGHT_IS_VALID(x) || FIELD_LEFT_IS_VALID(x))))
238 #define PLAYER_Y_MIN 0
239 #define PLAYER_Y_MAX HEIGHT
241 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
242 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
243 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
245 #define PLAYER_AREA_Y RACKET_Y_MID
246 #define PLAYER_AREA_X RACKET_X_MID
247 #define IS_NEAR_Y(py,by) (fabsf((py)-(by))<PLAYER_AREA_Y)
248 #define IS_NEAR_Y_AI(py,by) (fabsf((py)-(by))<PLAYER_AREA_Y/3.5)
249 #define IS_NEAR_X(px,bx) (fabsf((px)-(bx))<PLAYER_AREA_X)
250 #define IS_NEAR_X_AI(px,bx) (fabsf((px)-(bx))<PLAYER_AREA_X*2)
252 #define PLAYER_MOVE_Y 5.0
253 #define PLAYER_ACCEL_DEFAULT 0.2
254 #define PLAYER_ACCEL_INCREASE 1.2
256 #define POWER_UP_FACTOR 1.1
257 #define POWER_DOWN_FACTOR 0.9
258 #define PLAYER_POWER_MAX 100
260 #define MOVE_Y_SEED 3
262 #define PLAYER(s, num) (s->players[num-1])
264 /* Comment out the following #define to enable mouse control */
265 #define ENABLE_MOUSE
267 /* GameState handling*/
268 GameState *gamestate_new();
271 /* Game module functions */
272 void gameloop(GameState*);
273 void render( GameState*);
274 bool step( GameState*);
275 void limit_value( float*, float, float);
276 float get_move_y( GameState*, unsigned char);
277 void input_human(GameState*, int);
278 void input_ai(GameState*, int);
279 void game_setup_serve( GameState*);
280 float ngram_predictor( GameState*);
282 int score_game(GameState*);
283 char* format_sets( GameState*);
284 char* format_game( GameState*);
285 char* format_status( GameState*);
286 int game_get_winner( GameState*);
288 #endif