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 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 = findentity(e,owner,minig) ) )
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 owner
Definition main.qh:87
entity findentity(entity start,.entity field, entity match)
#define NULL
Definition post.qh:14

References entity(), findentity(), NULL, and owner.

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(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:87
void minigame_server_sendflags(entity ent, int mgflags)
Definition minigames.qc:78
int minigame_next_team(int curr_team, int n_teams)
Definition minigames.qc:65
const int MINIG_SF_UPDATE
Definition minigames.qh:109
const int MINIG_SF_ALL
Definition minigames.qh:112
const int TTT_TURN_PLACE
Definition ttt.qc:4
const int TTT_SF_PLAYERSCORE
Definition ttt.qc:15
bool ttt_winning_piece(entity piece)
Definition ttt.qc:39
const int TTT_LET_CNT
Definition ttt.qc:20
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 = findentity(e,owner,minigame) ) )
128 if ( e.classname == "minigame_board_piece" )
129 delete(e);
130 }
131}
const int TTT_TURN_NEXT
Definition ttt.qc:7

References entity(), findentity(), MINIG_SF_UPDATE, minigame_server_sendflags(), NULL, owner, 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 137 of file ttt.qc.

138{
139 switch(event)
140 {
141 case "start":
142 {
143 minigame.minigame_flags = (TTT_TURN_PLACE | TTT_TURN_TEAM1);
144 return true;
145 }
146 case "end":
147 {
148 entity e = NULL;
149 while( (e = findentity(e, owner, minigame)) )
150 if(e.classname == "minigame_board_piece")
151 {
152 strfree(e.netname);
153 delete(e);
154 }
155 return false;
156 }
157 case "join":
158 {
159 int pl_num = minigame_count_players(minigame);
160
161 // Don't allow joining a single player match
162 if ( (minigame.ttt_ai) && pl_num > 0 )
163 return false;
164
165 // Don't allow more than 2 players
166 if(pl_num >= 2) { return TTT_SPECTATOR_TEAM; }
167
168 // Get the right team
169 if(minigame.minigame_players)
170 return minigame_next_team(minigame.minigame_players.team, 2);
171
172 // Team 1 by default
173 return 1;
174 }
175 case "cmd":
176 {
177 entity player = ...(0,entity);
178 bool event_blocked = (player.team == TTT_SPECTATOR_TEAM);
179 switch(argv(0))
180 {
181 case "move":
182 if(event_blocked)
183 return true;
184 ttt_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
185 return true;
186 case "next":
187 if(event_blocked)
188 return true;
189 ttt_next_match(minigame,...(0,entity));
190 return true;
191 case "singleplayer":
192 if(event_blocked)
193 return true;
194 if ( minigame_count_players(minigame) == 1 )
195 {
196 minigame.ttt_ai = minigame_next_team(minigame.minigame_players.team, 2);
197 minigame.SendFlags = TTT_SF_SINGLEPLAYER;
198 }
199 return true;
200 }
201
202 return false;
203 }
204 case "network_send":
205 {
206 entity sent = ...(0,entity);
207 int sf = ...(1,int);
208 if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
209 {
210 WriteByte(MSG_ENTITY,sent.minigame_flags);
211 }
212 else if ( sent.classname == "minigame" && (sf & TTT_SF_SINGLEPLAYER) )
213 {
214 WriteByte(MSG_ENTITY,sent.ttt_ai);
215 }
216 return false;
217 }
218 }
219
220 return false;
221}
#define int
Definition _all.inc:20
const int MSG_ENTITY
Definition net.qh:115
void WriteByte(float data, float dest, float desto)
string argv(float n)
int minigame_count_players(entity minigame)
Definition minigames.qc:121
string string_null
Definition nil.qh:9
#define strfree(this)
Definition string.qh:59
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(), findentity(), int, minigame_count_players(), minigame_next_team(), MSG_ENTITY, NULL, owner, 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:12
int minigame_tile_number(string id)
Definition minigames.qc:21
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 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(piece.owner,minigame_tile_buildname(0,number)).team == piece.team )
45 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,number)).team == piece.team )
46 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,number)).team == piece.team )
47 return true;
48
49 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,0)).team == piece.team )
50 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,1)).team == piece.team )
51 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,2)).team == piece.team )
52 return true;
53
54 if ( number == letter )
55 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,0)).team == piece.team )
56 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,1)).team == piece.team )
57 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,2)).team == piece.team )
58 return true;
59
60 if ( number == 2-letter )
61 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,2)).team == piece.team )
62 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,1)).team == piece.team )
63 if ( ttt_find_piece(piece.owner,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:34

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().