Port archiving tool to C++
[tennix.git] / game.h
blob249d6e13cfadf610619a98315ac381d79dca065a
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"
34 #define SETS_TO_WIN 3
36 #define MAXPLAYERS 2
38 #ifdef DELUXE_EDITION
39 # define BALL_STATES 16
40 #else
41 # define BALL_STATES 4
42 #endif
44 typedef unsigned char referee_t;
45 enum {
46 REFEREE_NORMAL,
47 REFEREE_OUT,
48 REFEREE_PLAYER1,
49 REFEREE_PLAYER2,
50 REFEREE_COUNT
53 typedef unsigned char winner_t;
54 enum {
55 WINNER_NONE,
56 WINNER_PLAYER1,
57 WINNER_PLAYER2
60 typedef unsigned char soundevent_t;
61 enum {
62 SOUND_EVENT_NONE = 0<<0,
63 SOUND_EVENT_GROUND = 1<<0,
64 SOUND_EVENT_OUT = 1<<1,
65 SOUND_EVENT_APPLAUSE = 1<<2,
66 SOUND_EVENT_RACKET = 1<<3
68 #define SOUND_EVENT_COUNT 4
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 bool night;
83 unsigned int rain;
84 unsigned int fog;
85 } Location;
87 #define BALL_RESTITUTION .6
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 bool ground_hit;
97 char last_hit_by;
98 bool inhibit_gravity;
99 } Ball;
101 typedef struct {
102 InputDevice* input;
103 char input_device_index;
104 float x;
105 float y;
106 float power;
107 bool use_power;
108 unsigned char score;
109 unsigned char desire;
110 bool type; /* is this player ai-controlled or human? */
111 char game; /* score for the current game */
112 unsigned char sets[SETS_TO_WIN*2]; /* score for each set */
113 float accelerate; /* a value [0..1] how fast the user accelerates */
114 } Player;
116 enum {
117 PLAYER_TYPE_HUMAN,
118 PLAYER_TYPE_AI
121 enum {
122 DESIRE_NORMAL,
123 DESIRE_TOPSPIN,
124 DESIRE_SMASH,
125 DESIRE_MAX
128 /* wait 2 seconds before we score the game */
129 #define SCORING_DELAY 1000
131 typedef unsigned char scoreevent_t;
132 enum {
133 SCORE_UNDECIDED,
134 SCORE_EVENT_NET,
135 SCORE_EVENT_OUT,
136 SCORE_EVENT_GROUND_INVALID,
137 SCORE_EVENT_GROUND_VALID,
138 SCORE_EVENT_OFFSCREEN,
139 SCORE_EVENT_MAX
142 typedef unsigned char eventcounter_t;
143 enum {
144 EVENTCOUNTER_RENDERSTATE_START = 0,
145 EVENTCOUNTER_GAMESTATE_START = 1
148 typedef unsigned char statusmessage_t;
149 enum {
150 STATUSMSG_NONE,
151 STATUSMSG_WELCOME,
152 STATUSMSG_DEFAULT,
153 STATUSMSG_NET,
154 STATUSMSG_OUT,
155 STATUSMSG_FAULT,
156 STATUSMSG_P1SCORES,
157 STATUSMSG_P2SCORES,
158 STATUSMSG_VOLLEY,
159 STATUSMSG_DIDNTCATCH
162 typedef struct {
163 const Location* location;
164 char current_location; /* index of loc. in global location table */
165 Ball ball;
166 Player players[MAXPLAYERS];
167 unsigned char serving_player;
168 referee_t referee;
169 unsigned char current_set;
170 winner_t winner;
171 soundevent_t sound_events;
172 scoreevent_t score_event;
173 unsigned char score_time;
174 eventcounter_t ec_game;
175 eventcounter_t ec_sets;
176 statusmessage_t status_message;
177 } GameState;
179 typedef struct {
180 soundevent_t sound_events;
181 referee_t referee;
182 eventcounter_t ec_game;
183 eventcounter_t ec_sets;
184 statusmessage_t status_message;
185 const char* text_status;
186 const char* text_game;
187 const char* text_sets;
188 } RenderState;
190 #define PI 3.1415
192 #define GAME_TICKS 15
194 #define GRAVITY -.03
196 #define RACKET_X_MID 15
197 #define RACKET_Y_MID 24
199 #define BALL_X_MID 9
200 #define BALL_Y_MID 9
202 #define GAME_X_MIN 41.0*2
203 #define GAME_X_MAX 270.0*2
204 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
206 #define GAME_Y_MIN 155.0
207 #define GAME_Y_MAX 330.0
208 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
210 /* net size and collision detection */
211 #define NET_X 320
212 #define NET_Y 107
213 #define NET_WIDTH 5
214 #define NET_HEIGHT 273
215 #define NET_TALLNESS 5
216 #define IN_NET_X(x) (x+BALL_X_MID >= NET_X && x-BALL_X_MID <= NET_X+NET_WIDTH)
217 #define IN_NET_Y(y) (y >= NET_Y && y <= NET_Y+NET_HEIGHT)
218 #define NET_COLLISION(x, y, z) (z<=NET_TALLNESS && IN_NET_X(x) && IN_NET_Y(y))
219 #define NET_COLLISION_BALL(b) (NET_COLLISION(b.x, b.y, b.z))
221 /* "in field" coordinates (left and right part of court) */
222 #define FIELD_LEFT_X 119
223 #define FIELD_LEFT_WIDTH 202
224 #define FIELD_LEFT_IS_VALID(x) (x >= FIELD_LEFT_X && x <= FIELD_LEFT_X+FIELD_LEFT_WIDTH)
225 #define FIELD_RIGHT_X 326
226 #define FIELD_RIGHT_WIDTH 202
227 #define FIELD_RIGHT_IS_VALID(x) (x >= FIELD_RIGHT_X && x <= FIELD_RIGHT_X+FIELD_RIGHT_WIDTH)
229 /* coordinates for one-vs-one game */
230 #define FIELD_SINGLE_Y 154
231 #define FIELD_SINGLE_HEIGHT 178
232 #define FIELD_SINGLE_IS_VALID(y) (y >= FIELD_SINGLE_Y && y <= FIELD_SINGLE_Y+FIELD_SINGLE_HEIGHT)
234 /* coordinates for two-vs-two game */
235 #define FIELD_DOUBLE_Y 115
236 #define FIELD_DOUBLE_HEIGHT 256
237 #define FIELD_DOUBLE_IS_VALID(y) (y >= FIELD_DOUBLE_Y && y <= FIELD_DOUBLE_Y+FIELD_DOUBLE_HEIGHT)
239 /* player = player that last hit the ball, */
240 #define GROUND_IS_VALID(player, x, y) (FIELD_SINGLE_IS_VALID(y) && ((player==1)?(FIELD_RIGHT_IS_VALID(x)):(FIELD_LEFT_IS_VALID(x))))
241 #define IS_OUT(x, y) (!(FIELD_SINGLE_IS_VALID(y) && (FIELD_RIGHT_IS_VALID(x) || FIELD_LEFT_IS_VALID(x))))
243 #define PLAYER_Y_MIN 0
244 #define PLAYER_Y_MAX HEIGHT
246 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
247 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
248 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
250 #define PLAYER_AREA_Y RACKET_Y_MID
251 #define PLAYER_AREA_X RACKET_X_MID
252 #define IS_NEAR_Y(py,by) (fabsf((py)-(by))<(float)PLAYER_AREA_Y)
253 #define IS_NEAR_Y_AI(py,by) (fabsf((py)-(by))<(float)PLAYER_AREA_Y/3.5)
254 #define IS_NEAR_X(px,bx) (fabsf((px)-(bx))<(float)PLAYER_AREA_X)
255 #define IS_NEAR_X_AI(px,bx) (fabsf((px)-(bx))<(float)PLAYER_AREA_X*2.)
257 #define PLAYER_MOVE_Y 5.0
258 #define PLAYER_ACCEL_DEFAULT 0.2
259 #define PLAYER_ACCEL_INCREASE 1.2
261 #define POWER_UP_FACTOR 1.1
262 #define POWER_DOWN_FACTOR 0.9
263 #define PLAYER_POWER_MAX 100
265 #define PLAYER(s, num) (s->players[num-1])
267 /* Comment out the following #define to enable mouse control */
268 #define ENABLE_MOUSE
270 /* GameState handling*/
271 GameState *gamestate_new();
273 int gamestate_save(GameState* s, const char* filename);
274 GameState* gamestate_load(const char* filename);
276 /* Game module functions */
277 void gameloop(GameState*, TennixNet*);
278 void step(GameState*);
279 bool handle_input(GameState*, TennixNet*);
280 void render(const GameState*, RenderState*);
281 float get_move_y( GameState*, unsigned char);
282 void input_human(GameState*, int);
283 void input_ai(GameState*, int);
284 void game_setup_serve( GameState*);
286 int score_game(GameState*);
287 winner_t game_get_winner(const GameState*);
289 /* Helper functions for the renderer */
290 const char* format_sets(const GameState*);
291 const char* format_game(const GameState*);
292 const char* format_status(const GameState*);
294 #endif