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