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