Tennix 0.4.1
[tennix.git] / game.h
blobed3105847241b7a2857382af31379bd6bd91a2b8
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007 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"
30 #define SETS_TO_WIN 3
32 typedef unsigned char bool;
33 enum {
34 false,
35 true
38 enum {
39 REFEREE_NORMAL,
40 REFEREE_PLAYER1,
41 REFEREE_OUT,
42 REFEREE_PLAYER2
45 enum {
46 WINNER_NONE,
47 WINNER_PLAYER1,
48 WINNER_PLAYER2
51 typedef struct {
52 float x;
53 float y;
54 float move_x;
55 float move_y;
56 float jump;
57 } Ball;
59 typedef struct {
60 float x;
61 float y;
62 unsigned char state;
63 unsigned int score;
64 bool responsible; /* responsible for the next fault (if any) */
65 unsigned char desire; /* what the player aims to do (0=normal, 1=upper edge, 2=lower edge)*/
66 bool type; /* is this player ai-controlled or human? */
67 float ball_dest; /* prospective y-position of ball */
68 bool state_locked; /* enabled when user keeps pressing the "hit" key */
69 int game; /* score for the current game */
70 int sets[SETS_TO_WIN*2]; /* score for each set */
71 } Player;
73 enum {
74 PLAYER_TYPE_HUMAN,
75 PLAYER_TYPE_AI,
78 enum {
79 DESIRE_NORMAL,
80 DESIRE_MAX
83 typedef struct {
84 Ball ball;
85 Ball ground;
86 Player player1;
87 Player player2;
88 float phase;
89 unsigned int time;
90 bool was_stopped;
91 bool player1_serves;
92 char* status;
93 char game_score_str[50];
94 char sets_score_str[50];
95 unsigned char referee;
96 unsigned int current_set;
97 int winner;
98 bool is_over;
99 unsigned int court_type;
100 } GameState;
102 #define PI 3.1415
104 #define GAME_TICKS 15
106 #define GROUND_PHASE 0.4
107 #define PHASE_AMP 3.2
109 #define BALL_JUMP_MIN 4
110 #define BALL_JUMP_MAX 10
111 #define BALL_DEFAULT_SPEED 3.0
113 #define RACKET_X_MID 15
114 #define RACKET_Y_MID 24
116 #define BALL_X_MID 9
117 #define BALL_Y_MID 9
119 #define GAME_X_MIN 41.0*2
120 #define GAME_X_MAX 270.0*2
121 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
123 #define GAME_Y_MIN 155.0
124 #define GAME_Y_MAX 330.0
125 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
127 #define GAME_EDGE_AREA 20.0
128 #define GAME_EDGE_UPPER GAME_Y_MIN+GAME_EDGE_AREA
129 #define GAME_EDGE_LOWER GAME_Y_MAX-GAME_EDGE_AREA
131 #define PLAYER_Y_MIN 0.0
132 #define PLAYER_Y_MAX 1.0*HEIGHT
134 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
135 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
136 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
137 #define IS_OUT_Y(y) (y<GAME_Y_MIN || y>GAME_Y_MAX)
138 #define IS_OUT_X(x) (x<GAME_X_MIN || x>GAME_X_MAX)
140 #define PLAYER_AREA_Y RACKET_Y_MID
141 #define PLAYER_AREA_X RACKET_X_MID
142 #define IS_NEAR_Y(py,by) (fabsf(py-by)<PLAYER_AREA_Y)
143 #define IS_NEAR_Y_AI(py,by) (fabsf(py-by)<PLAYER_AREA_Y/2.5)
144 #define IS_NEAR_X(px,bx) (fabsf(px-bx)<PLAYER_AREA_X)
145 #define IS_NEAR_X_AI(px,bx) (fabsf(px-bx)<PLAYER_AREA_X*2)
147 #define PLAYER_STATE_MAX 7
148 #define PLAYER_POWERSHOT 6.2
150 #define MOVE_Y_SEED 3
152 void game( bool);
153 void render( GameState*);
154 bool step( GameState*);
155 void limit_value( float*, float, float);
156 float get_phase( GameState*);
157 float get_move_y( GameState*, unsigned char);
158 void input_human( Player*, bool, bool, bool);
159 void input_ai( Player*, Ball*, Player*);
160 void game_setup_serve( GameState*);
162 void score_game( GameState*, bool);
163 char* format_sets( GameState*);
164 char* format_game( GameState*);
165 char* format_status( GameState*);
166 int game_get_winner( GameState*);
168 #endif