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