Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
ttt.qc File Reference
#include "ttt.qh"
Include dependency graph for ttt.qc:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

 REGISTER_MINIGAME (ttt, _("Tic Tac Toe"))
entity ttt_find_piece (entity minig, string tile)
void ttt_move (entity minigame, entity player, string pos)
void ttt_next_match (entity minigame, entity player)
int ttt_server_event (entity minigame, string event,...)
bool ttt_valid_tile (string tile)
bool ttt_winning_piece (entity minigame, entity piece)

Variables

int ttt_ai
const int TTT_LET_CNT = 3
int ttt_nexteam
int ttt_npieces
const int TTT_NUM_CNT = 3
const int TTT_SF_PLAYERSCORE = MINIG_SF_CUSTOM
const int TTT_SF_SINGLEPLAYER = MINIG_SF_CUSTOM<<1
const int TTT_SPECTATOR_TEAM = 255
const int TTT_TILE_SIZE = 3
const int TTT_TURN_DRAW = 0x0400
const int TTT_TURN_NEXT = 0x0800
const int TTT_TURN_PLACE = 0x0100
const int TTT_TURN_TEAM = 0x000f
const int TTT_TURN_TEAM1 = 0x0001
const int TTT_TURN_TEAM2 = 0x0002
const int TTT_TURN_TYPE = 0x0f00
const int TTT_TURN_WIN = 0x0200

Function Documentation

◆ REGISTER_MINIGAME()

REGISTER_MINIGAME ( ttt ,
_("Tic Tac Toe")  )

◆ ttt_find_piece()

entity ttt_find_piece ( entity minig,
string tile )

Definition at line 29 of file ttt.qc.

30{
31 entity e = NULL;
32 while ( ( e = findfloat(e,minigame_id,minig.minigame_id) ) )
33 if ( e.classname == "minigame_board_piece" && e.netname == tile )
34 return e;
35 return NULL;
36}
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
entity findfloat(entity start,.float field, float match)
int minigame_id
Unique ID for the minigame session, associated with all objects currently the result of etof(minigame...
Definition minigames.qh:49
#define NULL
Definition post.qh:14

References entity(), findfloat(), minigame_id, and NULL.

Referenced by ttt_move(), and ttt_winning_piece().

◆ ttt_move()

void ttt_move ( entity minigame,
entity player,
string pos )

Definition at line 80 of file ttt.qc.

81{
82 if ( minigame.minigame_flags & TTT_TURN_PLACE )
83 if ( pos && player.team == (minigame.minigame_flags & TTT_TURN_TEAM) )
84 {
85 if ( ttt_valid_tile(pos) )
86 if ( !ttt_find_piece(minigame,pos) )
87 {
88 entity piece = msle_spawn(minigame,new(minigame_board_piece));
89 piece.team = player.team;
90 piece.netname = strzone(pos);
93 ++minigame.ttt_npieces;
94 minigame.ttt_nexteam = minigame_next_team(player.team,2);
95 if ( ttt_winning_piece(minigame, piece) )
96 {
97 ++player.minigame_flags;
99 minigame.minigame_flags = TTT_TURN_WIN | player.team;
100 }
101 else if ( minigame.ttt_npieces >= (TTT_LET_CNT * TTT_NUM_CNT) )
102 minigame.minigame_flags = TTT_TURN_DRAW;
103 else
104 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
105 }
106 }
107}
string strzone(string s)
entity msle_spawn(entity minigame_session, entity e)
Definition minigames.qc:96
void minigame_server_sendflags(entity ent, int mgflags)
Definition minigames.qc:87
int minigame_next_team(int curr_team, int n_teams)
Definition minigames.qc:74
const int MINIG_SF_UPDATE
Definition minigames.qh:113
const int MINIG_SF_ALL
Definition minigames.qh:116
const int TTT_TURN_PLACE
Definition ttt.qc:4
const int TTT_SF_PLAYERSCORE
Definition ttt.qc:15
const int TTT_LET_CNT
Definition ttt.qc:20
bool ttt_winning_piece(entity minigame, entity piece)
Definition ttt.qc:39
const int TTT_TURN_DRAW
Definition ttt.qc:6
const int TTT_TURN_TEAM
Definition ttt.qc:12
const int TTT_NUM_CNT
Definition ttt.qc:21
entity ttt_find_piece(entity minig, string tile)
Definition ttt.qc:29
bool ttt_valid_tile(string tile)
Definition ttt.qc:70
const int TTT_TURN_WIN
Definition ttt.qc:5

References entity(), MINIG_SF_ALL, MINIG_SF_UPDATE, minigame_next_team(), minigame_server_sendflags(), msle_spawn(), strzone(), ttt_find_piece(), TTT_LET_CNT, TTT_NUM_CNT, TTT_SF_PLAYERSCORE, TTT_TURN_DRAW, TTT_TURN_PLACE, TTT_TURN_TEAM, TTT_TURN_WIN, ttt_valid_tile(), and ttt_winning_piece().

Referenced by ttt_server_event().

◆ ttt_next_match()

void ttt_next_match ( entity minigame,
entity player )

Definition at line 110 of file ttt.qc.

111{
112#ifdef SVQC
113 // on multiplayer matches, wait for both players to agree
114 if ( minigame.minigame_flags & (TTT_TURN_WIN|TTT_TURN_DRAW) )
115 {
116 minigame.minigame_flags = TTT_TURN_NEXT | player.team;
117 minigame.SendFlags |= MINIG_SF_UPDATE;
118 }
119 else if ( (minigame.minigame_flags & TTT_TURN_NEXT) &&
120 !( minigame.minigame_flags & player.team ) )
121#endif
122 {
123 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
125 minigame.ttt_npieces = 0;
126 entity e = NULL;
127 while ( ( e = findfloat(e,minigame_id,minigame.minigame_id) ) )
128 if ( e.classname == "minigame_board_piece" )
129 {
130 // since this is done on both sides, we don't want to kill networked pieces early
131 // but we don't want their slots taking up ai positions, so clear their location
132 strfree(e.netname);
133 if (e.minigame_autoclean)
134 delete(e);
135 }
136 }
137}
#define strfree(this)
Definition string.qh:57
const int TTT_TURN_NEXT
Definition ttt.qc:7

References entity(), findfloat(), MINIG_SF_UPDATE, minigame_id, minigame_server_sendflags(), NULL, strfree, TTT_TURN_DRAW, TTT_TURN_NEXT, TTT_TURN_PLACE, and TTT_TURN_WIN.

Referenced by ttt_server_event().

◆ ttt_server_event()

int ttt_server_event ( entity minigame,
string event,
... )

Definition at line 143 of file ttt.qc.

144{
145 switch(event)
146 {
147 case "start":
148 {
149 minigame.minigame_flags = (TTT_TURN_PLACE | TTT_TURN_TEAM1);
150 return true;
151 }
152 case "end":
153 {
154 entity e = NULL;
155 while( (e = findfloat(e, minigame_id, minigame.minigame_id)) )
156 if(e.classname == "minigame_board_piece")
157 {
158 strfree(e.netname);
159 delete(e);
160 }
161 return false;
162 }
163 case "join":
164 {
165 int pl_num = minigame_count_players(minigame);
166
167 // Don't allow joining a single player match
168 if ( (minigame.ttt_ai) && pl_num > 0 )
169 return false;
170
171 // Don't allow more than 2 players
172 if(pl_num >= 2) { return TTT_SPECTATOR_TEAM; }
173
174 // Get the right team
175 if(minigame.minigame_players)
176 return minigame_next_team(minigame.minigame_players.team, 2);
177
178 // Team 1 by default
179 return 1;
180 }
181 case "cmd":
182 {
183 entity player = ...(0,entity);
184 bool event_blocked = (player.team == TTT_SPECTATOR_TEAM);
185 switch(argv(0))
186 {
187 case "move":
188 if(event_blocked)
189 return true;
190 ttt_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
191 return true;
192 case "next":
193 if(event_blocked)
194 return true;
195 ttt_next_match(minigame,...(0,entity));
196 return true;
197 case "singleplayer":
198 if(event_blocked)
199 return true;
200 if ( minigame_count_players(minigame) == 1 )
201 {
202 minigame.ttt_ai = minigame_next_team(minigame.minigame_players.team, 2);
203 minigame.SendFlags = TTT_SF_SINGLEPLAYER;
204 }
205 return true;
206 }
207
208 return false;
209 }
210 case "network_send":
211 {
212 entity sent = ...(0,entity);
213 int sf = ...(1,int);
214 if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
215 {
216 WriteByte(MSG_ENTITY,sent.minigame_flags);
217 }
218 else if ( sent.classname == "minigame" && (sf & TTT_SF_SINGLEPLAYER) )
219 {
220 WriteByte(MSG_ENTITY,sent.ttt_ai);
221 }
222 return false;
223 }
224 }
225
226 return false;
227}
WriteByte(chan, ent.angles.y/DEC_FACTOR)
#define int
Definition _all.inc:20
const int MSG_ENTITY
Definition net.qh:156
string argv(float n)
int minigame_count_players(entity minigame)
Definition minigames.qc:132
string string_null
Definition nil.qh:9
const int TTT_SF_SINGLEPLAYER
Definition ttt.qc:16
const int TTT_TURN_TEAM1
Definition ttt.qc:10
void ttt_move(entity minigame, entity player, string pos)
Definition ttt.qc:80
const int TTT_SPECTATOR_TEAM
Definition ttt.qc:18
void ttt_next_match(entity minigame, entity player)
Definition ttt.qc:110

References argv(), entity(), findfloat(), int, minigame_count_players(), minigame_id, minigame_next_team(), MSG_ENTITY, NULL, strfree, string_null, ttt_move(), ttt_next_match(), TTT_SF_PLAYERSCORE, TTT_SF_SINGLEPLAYER, TTT_SPECTATOR_TEAM, TTT_TURN_PLACE, TTT_TURN_TEAM1, and WriteByte().

◆ ttt_valid_tile()

bool ttt_valid_tile ( string tile)

Definition at line 70 of file ttt.qc.

71{
72 if ( !tile )
73 return 0;
74 int number = minigame_tile_number(tile);
75 int letter = minigame_tile_letter(tile);
76 return 0 <= number && number < TTT_NUM_CNT && 0 <= letter && letter < TTT_LET_CNT;
77}
int minigame_tile_letter(string id)
Definition minigames.qc:21
int minigame_tile_number(string id)
Definition minigames.qc:30
int int number
Definition impulse.qc:89

References minigame_tile_letter(), minigame_tile_number(), number, TTT_LET_CNT, and TTT_NUM_CNT.

Referenced by ttt_move().

◆ ttt_winning_piece()

bool ttt_winning_piece ( entity minigame,
entity piece )

Definition at line 39 of file ttt.qc.

40{
41 int number = minigame_tile_number(piece.netname);
42 int letter = minigame_tile_letter(piece.netname);
43
44 if ( ttt_find_piece(minigame,minigame_tile_buildname(0,number)).team == piece.team )
45 if ( ttt_find_piece(minigame,minigame_tile_buildname(1,number)).team == piece.team )
46 if ( ttt_find_piece(minigame,minigame_tile_buildname(2,number)).team == piece.team )
47 return true;
48
49 if ( ttt_find_piece(minigame,minigame_tile_buildname(letter,0)).team == piece.team )
50 if ( ttt_find_piece(minigame,minigame_tile_buildname(letter,1)).team == piece.team )
51 if ( ttt_find_piece(minigame,minigame_tile_buildname(letter,2)).team == piece.team )
52 return true;
53
54 if ( number == letter )
55 if ( ttt_find_piece(minigame,minigame_tile_buildname(0,0)).team == piece.team )
56 if ( ttt_find_piece(minigame,minigame_tile_buildname(1,1)).team == piece.team )
57 if ( ttt_find_piece(minigame,minigame_tile_buildname(2,2)).team == piece.team )
58 return true;
59
60 if ( number == 2-letter )
61 if ( ttt_find_piece(minigame,minigame_tile_buildname(0,2)).team == piece.team )
62 if ( ttt_find_piece(minigame,minigame_tile_buildname(1,1)).team == piece.team )
63 if ( ttt_find_piece(minigame,minigame_tile_buildname(2,0)).team == piece.team )
64 return true;
65
66 return false;
67}
int team
Definition main.qh:188
string minigame_tile_buildname(int letter, int number)
Definition minigames.qc:43

References entity(), minigame_tile_buildname(), minigame_tile_letter(), minigame_tile_number(), number, team, and ttt_find_piece().

Referenced by ttt_move().

Variable Documentation

◆ ttt_ai

int ttt_ai

Definition at line 26 of file ttt.qc.

◆ TTT_LET_CNT

const int TTT_LET_CNT = 3

Definition at line 20 of file ttt.qc.

Referenced by ttt_move(), and ttt_valid_tile().

◆ ttt_nexteam

int ttt_nexteam

Definition at line 25 of file ttt.qc.

◆ ttt_npieces

int ttt_npieces

Definition at line 24 of file ttt.qc.

◆ TTT_NUM_CNT

const int TTT_NUM_CNT = 3

Definition at line 21 of file ttt.qc.

Referenced by ttt_move(), and ttt_valid_tile().

◆ TTT_SF_PLAYERSCORE

const int TTT_SF_PLAYERSCORE = MINIG_SF_CUSTOM

Definition at line 15 of file ttt.qc.

Referenced by ttt_move(), and ttt_server_event().

◆ TTT_SF_SINGLEPLAYER

const int TTT_SF_SINGLEPLAYER = MINIG_SF_CUSTOM<<1

Definition at line 16 of file ttt.qc.

Referenced by ttt_server_event().

◆ TTT_SPECTATOR_TEAM

const int TTT_SPECTATOR_TEAM = 255

Definition at line 18 of file ttt.qc.

Referenced by ttt_server_event().

◆ TTT_TILE_SIZE

const int TTT_TILE_SIZE = 3

Definition at line 22 of file ttt.qc.

◆ TTT_TURN_DRAW

const int TTT_TURN_DRAW = 0x0400

Definition at line 6 of file ttt.qc.

Referenced by ttt_move(), and ttt_next_match().

◆ TTT_TURN_NEXT

const int TTT_TURN_NEXT = 0x0800

Definition at line 7 of file ttt.qc.

Referenced by ttt_next_match().

◆ TTT_TURN_PLACE

const int TTT_TURN_PLACE = 0x0100

Definition at line 4 of file ttt.qc.

Referenced by ttt_move(), ttt_next_match(), and ttt_server_event().

◆ TTT_TURN_TEAM

const int TTT_TURN_TEAM = 0x000f

Definition at line 12 of file ttt.qc.

Referenced by ttt_move().

◆ TTT_TURN_TEAM1

const int TTT_TURN_TEAM1 = 0x0001

Definition at line 10 of file ttt.qc.

Referenced by ttt_server_event().

◆ TTT_TURN_TEAM2

const int TTT_TURN_TEAM2 = 0x0002

Definition at line 11 of file ttt.qc.

◆ TTT_TURN_TYPE

const int TTT_TURN_TYPE = 0x0f00

Definition at line 8 of file ttt.qc.

◆ TTT_TURN_WIN

const int TTT_TURN_WIN = 0x0200

Definition at line 5 of file ttt.qc.

Referenced by ttt_move(), and ttt_next_match().