Split input handling + stepping; type changes in GameState
[tennix.git] / game.h
blob4983431287e6d1683af43420904f6fd7083b1d92
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 typedef unsigned char winner_t;
59 enum {
60 WINNER_NONE,
61 WINNER_PLAYER1,
62 WINNER_PLAYER2
65 typedef unsigned char soundevent_t;
66 enum {
67 SOUND_EVENT_NONE = 0<<0,
68 SOUND_EVENT_GROUND = 1<<0,
69 SOUND_EVENT_OUT = 1<<1,
70 SOUND_EVENT_APPLAUSE = 1<<2,
71 SOUND_EVENT_RACKET = 1<<3
73 #define SOUND_EVENT_COUNT 4
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 bool night;
88 unsigned int rain;
89 unsigned int fog;
90 } Location;
92 typedef struct {
93 float x;
94 float y;
95 float z;
96 float move_x;
97 float move_y;
98 float move_z;
99 float restitution;
100 bool ground_hit;
101 int last_hit_by;
102 bool inhibit_gravity;
103 } Ball;
105 typedef struct {
106 InputDevice* input;
107 int input_device_index;
108 float x;
109 float y;
110 float power;
111 float power_up_factor;
112 float power_down_factor;
113 bool use_power;
114 unsigned int score;
115 unsigned char desire;
116 bool type; /* is this player ai-controlled or human? */
117 int game; /* score for the current game */
118 int sets[SETS_TO_WIN*2]; /* score for each set */
119 float accelerate; /* a value [0..1] how fast the user accelerates */
120 } Player;
122 enum {
123 PLAYER_TYPE_HUMAN,
124 PLAYER_TYPE_AI
127 enum {
128 DESIRE_NORMAL,
129 DESIRE_TOPSPIN,
130 DESIRE_SMASH,
131 DESIRE_MAX
134 /* wait 2 seconds before we score the game */
135 #define SCORING_DELAY 1000
137 typedef unsigned char scoreevent_t;
138 enum {
139 SCORE_UNDECIDED,
140 SCORE_EVENT_NET,
141 SCORE_EVENT_OUT,
142 SCORE_EVENT_GROUND_INVALID,
143 SCORE_EVENT_GROUND_VALID,
144 SCORE_EVENT_OFFSCREEN,
145 SCORE_EVENT_MAX
148 typedef unsigned char eventcounter_t;
149 enum {
150 EVENTCOUNTER_RENDERSTATE_START = 0,
151 EVENTCOUNTER_GAMESTATE_START = 1
154 typedef unsigned char statusmessage_t;
155 enum {
156 STATUSMSG_NONE,
157 STATUSMSG_WELCOME,
158 STATUSMSG_DEFAULT,
159 STATUSMSG_NET,
160 STATUSMSG_OUT,
161 STATUSMSG_FAULT,
162 STATUSMSG_P1SCORES,
163 STATUSMSG_P2SCORES,
164 STATUSMSG_VOLLEY,
165 STATUSMSG_DIDNTCATCH
168 typedef struct {
169 const Location* location;
170 char current_location; /* index of loc. in global location table */
171 Ball ball;
172 Player players[MAXPLAYERS];
173 unsigned char serving_player;
174 referee_t referee;
175 unsigned char current_set;
176 winner_t winner;
177 soundevent_t sound_events;
178 scoreevent_t score_event;
179 unsigned char score_time;
180 eventcounter_t ec_game;
181 eventcounter_t ec_sets;
182 statusmessage_t status_message;
183 } GameState;
185 typedef struct {
186 soundevent_t sound_events;
187 referee_t referee;
188 eventcounter_t ec_game;
189 eventcounter_t ec_sets;
190 statusmessage_t status_message;
191 const char* text_status;
192 const char* text_game;
193 const char* text_sets;
194 } RenderState;
196 #define PI 3.1415
198 #define GAME_TICKS 15
200 #define GRAVITY -.03
202 #define RACKET_X_MID 15
203 #define RACKET_Y_MID 24
205 #define BALL_X_MID 9
206 #define BALL_Y_MID 9
208 #define GAME_X_MIN 41.0*2
209 #define GAME_X_MAX 270.0*2
210 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
212 #define GAME_Y_MIN 155.0
213 #define GAME_Y_MAX 330.0
214 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
216 /* net size and collision detection */
217 #define NET_X 320
218 #define NET_Y 107
219 #define NET_WIDTH 5
220 #define NET_HEIGHT 273
221 #define NET_TALLNESS 5
222 #define IN_NET_X(x) (x+BALL_X_MID >= NET_X && x-BALL_X_MID <= NET_X+NET_WIDTH)
223 #define IN_NET_Y(y) (y >= NET_Y && y <= NET_Y+NET_HEIGHT)
224 #define NET_COLLISION(x, y, z) (z<=NET_TALLNESS && IN_NET_X(x) && IN_NET_Y(y))
225 #define NET_COLLISION_BALL(b) (NET_COLLISION(b.x, b.y, b.z))
227 /* "in field" coordinates (left and right part of court) */
228 #define FIELD_LEFT_X 119
229 #define FIELD_LEFT_WIDTH 202
230 #define FIELD_LEFT_IS_VALID(x) (x >= FIELD_LEFT_X && x <= FIELD_LEFT_X+FIELD_LEFT_WIDTH)
231 #define FIELD_RIGHT_X 326
232 #define FIELD_RIGHT_WIDTH 202
233 #define FIELD_RIGHT_IS_VALID(x) (x >= FIELD_RIGHT_X && x <= FIELD_RIGHT_X+FIELD_RIGHT_WIDTH)
235 /* coordinates for one-vs-one game */
236 #define FIELD_SINGLE_Y 154
237 #define FIELD_SINGLE_HEIGHT 178
238 #define FIELD_SINGLE_IS_VALID(y) (y >= FIELD_SINGLE_Y && y <= FIELD_SINGLE_Y+FIELD_SINGLE_HEIGHT)
240 /* coordinates for two-vs-two game */
241 #define FIELD_DOUBLE_Y 115
242 #define FIELD_DOUBLE_HEIGHT 256
243 #define FIELD_DOUBLE_IS_VALID(y) (y >= FIELD_DOUBLE_Y && y <= FIELD_DOUBLE_Y+FIELD_DOUBLE_HEIGHT)
245 /* player = player that last hit the ball, */
246 #define GROUND_IS_VALID(player, x, y) (FIELD_SINGLE_IS_VALID(y) && ((player==1)?(FIELD_RIGHT_IS_VALID(x)):(FIELD_LEFT_IS_VALID(x))))
247 #define IS_OUT(x, y) (!(FIELD_SINGLE_IS_VALID(y) && (FIELD_RIGHT_IS_VALID(x) || FIELD_LEFT_IS_VALID(x))))
249 #define PLAYER_Y_MIN 0
250 #define PLAYER_Y_MAX HEIGHT
252 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
253 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
254 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
256 #define PLAYER_AREA_Y RACKET_Y_MID
257 #define PLAYER_AREA_X RACKET_X_MID
258 #define IS_NEAR_Y(py,by) (fabsf((py)-(by))<(float)PLAYER_AREA_Y)
259 #define IS_NEAR_Y_AI(py,by) (fabsf((py)-(by))<(float)PLAYER_AREA_Y/3.5)
260 #define IS_NEAR_X(px,bx) (fabsf((px)-(bx))<(float)PLAYER_AREA_X)
261 #define IS_NEAR_X_AI(px,bx) (fabsf((px)-(bx))<(float)PLAYER_AREA_X*2.)
263 #define PLAYER_MOVE_Y 5.0
264 #define PLAYER_ACCEL_DEFAULT 0.2
265 #define PLAYER_ACCEL_INCREASE 1.2
267 #define POWER_UP_FACTOR 1.1
268 #define POWER_DOWN_FACTOR 0.9
269 #define PLAYER_POWER_MAX 100
271 #define PLAYER(s, num) (s->players[num-1])
273 /* Comment out the following #define to enable mouse control */
274 #define ENABLE_MOUSE
276 /* GameState handling*/
277 GameState *gamestate_new();
279 int gamestate_save(GameState* s, const char* filename);
280 GameState* gamestate_load(const char* filename);
282 /* Game module functions */
283 void gameloop(GameState*);
284 void step(GameState*);
285 bool handle_input(GameState*);
286 void render(const GameState*, RenderState*);
287 float get_move_y( GameState*, unsigned char);
288 void input_human(GameState*, int);
289 void input_ai(GameState*, int);
290 void game_setup_serve( GameState*);
292 int score_game(GameState*);
293 winner_t game_get_winner(const GameState*);
295 /* Helper functions for the renderer */
296 const char* format_sets(const GameState*);
297 const char* format_game(const GameState*);
298 const char* format_status(const GameState*);
300 #endif