Computer AI: History-based positioning
[tennix.git] / game.h
blob489d118126edba8cec193f0808ab5ed6d474ecd0
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 #define NGRAM_STEPS 6
34 typedef unsigned char bool;
35 enum {
36 false,
37 true
40 enum {
41 REFEREE_NORMAL,
42 REFEREE_PLAYER1,
43 REFEREE_OUT,
44 REFEREE_PLAYER2
47 enum {
48 WINNER_NONE,
49 WINNER_PLAYER1,
50 WINNER_PLAYER2
53 typedef struct {
54 float x;
55 float y;
56 float move_x;
57 float move_y;
58 float jump;
59 } Ball;
61 typedef struct {
62 float x;
63 float y;
64 unsigned char state;
65 unsigned int score;
66 bool responsible; /* responsible for the next fault (if any) */
67 unsigned char desire; /* what the player aims to do (0=normal, 1=upper edge, 2=lower edge)*/
68 bool type; /* is this player ai-controlled or human? */
69 float ball_dest; /* prospective y-position of ball */
70 bool state_locked; /* enabled when user keeps pressing the "hit" key */
71 int game; /* score for the current game */
72 int sets[SETS_TO_WIN*2]; /* score for each set */
73 } Player;
75 enum {
76 PLAYER_TYPE_HUMAN,
77 PLAYER_TYPE_AI,
80 enum {
81 DESIRE_NORMAL,
82 DESIRE_MAX
85 typedef struct {
86 Ball ball;
87 Ball ground;
88 Player player1;
89 Player player2;
90 float phase;
91 unsigned int time;
92 bool was_stopped;
93 bool player1_serves;
94 char* status;
95 char game_score_str[50];
96 char sets_score_str[50];
97 unsigned char referee;
98 unsigned int current_set;
99 int winner;
100 bool is_over;
101 unsigned int court_type;
102 unsigned int history[3];
103 unsigned int history_size;
104 bool history_is_locked;
105 unsigned char ngram[NGRAM_STEPS][NGRAM_STEPS][NGRAM_STEPS];
106 float ngram_prediction;
107 } GameState;
109 #define PI 3.1415
111 #define GAME_TICKS 15
113 #define GROUND_PHASE 0.4
114 #define PHASE_AMP 3.2
116 #define BALL_JUMP_MIN 4
117 #define BALL_JUMP_MAX 10
118 #define BALL_DEFAULT_SPEED 3.0
120 #define RACKET_X_MID 15
121 #define RACKET_Y_MID 24
123 #define BALL_X_MID 9
124 #define BALL_Y_MID 9
126 #define GAME_X_MIN 41.0*2
127 #define GAME_X_MAX 270.0*2
128 #define GAME_X_MID ((GAME_X_MIN+GAME_X_MAX)/2)
130 #define GAME_Y_MIN 155.0
131 #define GAME_Y_MAX 330.0
132 #define GAME_Y_MID ((GAME_Y_MIN+GAME_Y_MAX)/2)
134 #define GAME_EDGE_AREA 20.0
135 #define GAME_EDGE_UPPER GAME_Y_MIN+GAME_EDGE_AREA
136 #define GAME_EDGE_LOWER GAME_Y_MAX-GAME_EDGE_AREA
138 #define PLAYER_Y_MIN 0.0
139 #define PLAYER_Y_MAX 1.0*HEIGHT
141 #define IS_OFFSCREEN_Y(y) (((y)<0-BALL_Y_MID*2) || ((y)>HEIGHT+BALL_Y_MID*2))
142 #define IS_OFFSCREEN_X(x) (((x)<0-BALL_X_MID*2) || ((x)>WIDTH+BALL_X_MID*2))
143 #define IS_OFFSCREEN(x,y) ((IS_OFFSCREEN_X(x)) || (IS_OFFSCREEN_Y(y)))
144 #define IS_OUT_Y(y) (y<GAME_Y_MIN || y>GAME_Y_MAX)
145 #define IS_OUT_X(x) (x<GAME_X_MIN || x>GAME_X_MAX)
147 #define PLAYER_AREA_Y RACKET_Y_MID
148 #define PLAYER_AREA_X RACKET_X_MID
149 #define IS_NEAR_Y(py,by) (fabsf(py-by)<PLAYER_AREA_Y)
150 #define IS_NEAR_Y_AI(py,by) (fabsf(py-by)<PLAYER_AREA_Y/3.5)
151 #define IS_NEAR_X(px,bx) (fabsf(px-bx)<PLAYER_AREA_X)
152 #define IS_NEAR_X_AI(px,bx) (fabsf(px-bx)<PLAYER_AREA_X*2)
154 #define PLAYER_STATE_MAX 7
155 #define PLAYER_POWERSHOT 6.2
157 #define MOVE_Y_SEED 3
159 void game( bool);
160 void render( GameState*);
161 bool step( GameState*);
162 void limit_value( float*, float, float);
163 float get_phase( GameState*);
164 float get_move_y( GameState*, unsigned char);
165 void input_human( Player*, bool, bool, bool);
166 void input_ai( Player*, Ball*, Player*, GameState*);
167 void game_setup_serve( GameState*);
168 float ngram_predictor( GameState*);
170 void score_game( GameState*, bool);
171 char* format_sets( GameState*);
172 char* format_game( GameState*);
173 char* format_status( GameState*);
174 int game_get_winner( GameState*);
176 #endif