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