Voice support, experimental wind, extended referee
[tennix.git] / game.h
blobc5579123df4bc2343c77bbc180308337d23e0d0b
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008 Thomas Perl <thp@perli.net>
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"
31 #define SETS_TO_WIN 3
33 #define NGRAM_STEPS 6
35 typedef unsigned char bool;
36 enum {
37 false,
38 true
41 #ifdef EXTENDED_REFEREE
42 enum {
43 REFEREE_NORMAL,
44 REFEREE_OUT,
45 REFEREE_PLAYER1,
46 REFEREE_PLAYER2
48 #else
49 enum {
50 REFEREE_NORMAL,
51 REFEREE_PLAYER1,
52 REFEREE_OUT,
53 REFEREE_PLAYER2
55 #endif
57 enum {
58 WINNER_NONE,
59 WINNER_PLAYER1,
60 WINNER_PLAYER2
63 typedef struct {
64 float x;
65 float y;
66 float move_x;
67 float move_y;
68 float jump;
69 } Ball;
71 typedef struct {
72 float x;
73 float y;
74 unsigned char state;
75 unsigned int score;
76 bool responsible; /* responsible for the next fault (if any) */
77 unsigned char desire; /* what the player aims to do (0=normal, 1=upper edge, 2=lower edge)*/
78 bool type; /* is this player ai-controlled or human? */
79 float ball_dest; /* prospective y-position of ball */
80 bool state_locked; /* enabled when user keeps pressing the "hit" key */
81 int game; /* score for the current game */
82 int sets[SETS_TO_WIN*2]; /* score for each set */
83 int mouse_x; /* x position of mouse */
84 int mouse_y; /* y position of mouse */
85 float accelerate; /* a value [0..1] how fast the user accelerates */
86 bool mouse_locked; /* on start, ignore unpressed mouse state */
87 } Player;
89 enum {
90 PLAYER_TYPE_HUMAN,
91 PLAYER_TYPE_AI,
94 enum {
95 DESIRE_NORMAL,
96 DESIRE_MAX
99 typedef struct {
100 Ball ball;
101 Ball ground;
102 Player player1;
103 Player player2;
104 float phase;
105 unsigned int time;
106 bool was_stopped;
107 bool player1_serves;
108 char* status;
109 char game_score_str[50];
110 char sets_score_str[50];
111 unsigned char referee;
112 unsigned int current_set;
113 int winner;
114 bool is_over;
115 unsigned int court_type;
116 unsigned int old_court_type;
117 unsigned int history[3];
118 unsigned int history_size;
119 bool history_is_locked;
120 unsigned char ngram[NGRAM_STEPS][NGRAM_STEPS][NGRAM_STEPS];
121 float ngram_prediction;
122 sound_id play_sound;
123 float joystick_y;
124 float joystick_x;
125 unsigned char joystick_a;
126 unsigned int rain;
127 unsigned int fog;
128 bool night;
129 int wind;
130 unsigned int windtime;
131 } GameState;
133 #define PI 3.1415
135 #define GAME_TICKS 15
137 #define GROUND_PHASE 0.4
138 #define PHASE_AMP 3.2
140 #define BALL_JUMP_MIN 4
141 #define BALL_JUMP_MAX 10
143 #define RACKET_X_MID 15
144 #define RACKET_Y_MID 24
146 #define BALL_X_MID 9
147 #define BALL_Y_MID 9
149 #define GAME_X_MIN 41.0*2
150 #define GAME_X_MAX 270.0*2
151 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
153 #define GAME_Y_MIN 155.0
154 #define GAME_Y_MAX 330.0
155 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
157 #define GAME_EDGE_AREA 20.0
158 #define GAME_EDGE_UPPER GAME_Y_MIN+GAME_EDGE_AREA
159 #define GAME_EDGE_LOWER GAME_Y_MAX-GAME_EDGE_AREA
161 #define PLAYER_Y_MIN 0.0
162 #define PLAYER_Y_MAX 1.0*HEIGHT
164 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
165 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
166 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
167 #define IS_OUT_Y(y) (y<GAME_Y_MIN || y>GAME_Y_MAX)
168 #define IS_OUT_X(x) (x<GAME_X_MIN || x>GAME_X_MAX)
170 #define PLAYER_AREA_Y RACKET_Y_MID
171 #define PLAYER_AREA_X RACKET_X_MID
172 #define IS_NEAR_Y(py,by) (fabsf(py-by)<PLAYER_AREA_Y)
173 #define IS_NEAR_Y_AI(py,by) (fabsf(py-by)<PLAYER_AREA_Y/3.5)
174 #define IS_NEAR_X(px,bx) (fabsf(px-bx)<PLAYER_AREA_X)
175 #define IS_NEAR_X_AI(px,bx) (fabsf(px-bx)<PLAYER_AREA_X*2)
177 #define PLAYER_MOVE_Y 5.0
178 #define PLAYER_ACCEL_DEFAULT 0.2
179 #define PLAYER_ACCEL_INCREASE 1.2
180 #define PLAYER_STATE_MAX 7
181 #define PLAYER_POWERSHOT 6.2
183 #define MOVE_Y_SEED 3
185 /* Comment out the following #define to enable mouse control */
186 #define ENABLE_MOUSE
188 /* GameState handling*/
189 GameState *gamestate_new();
192 /* Game module functions */
193 void gameloop(GameState*);
194 void render( GameState*);
195 bool step( GameState*);
196 void limit_value( float*, float, float);
197 float get_phase( GameState*);
198 float get_move_y( GameState*, unsigned char);
199 void input_human( Player*, bool, bool, bool, bool, GameState*);
200 void input_ai( Player*, Ball*, Player*, GameState*);
201 void game_setup_serve( GameState*);
202 float ngram_predictor( GameState*);
204 void score_game( GameState*, bool);
205 char* format_sets( GameState*);
206 char* format_game( GameState*);
207 char* format_status( GameState*);
208 int game_get_winner( GameState*);
210 #endif