Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
ttt.qc
Go to the documentation of this file.
1#include "ttt.qh"
2REGISTER_MINIGAME(ttt, _("Tic Tac Toe"));
3
4const int TTT_TURN_PLACE = 0x0100; // player has to place a piece on the board
5const int TTT_TURN_WIN = 0x0200; // player has won
6const int TTT_TURN_DRAW = 0x0400; // no moves are possible
7const int TTT_TURN_NEXT = 0x0800; // a player wants to start a new match
8const int TTT_TURN_TYPE = 0x0f00; // turn type mask
9
10const int TTT_TURN_TEAM1 = 0x0001;
11const int TTT_TURN_TEAM2 = 0x0002;
12const int TTT_TURN_TEAM = 0x000f; // turn team mask
13
14// send flags
15const int TTT_SF_PLAYERSCORE = MINIG_SF_CUSTOM; // send minigame_player scores (won matches)
16const int TTT_SF_SINGLEPLAYER = MINIG_SF_CUSTOM<<1;// send minigame.ttt_ai
17
18const int TTT_SPECTATOR_TEAM = 255; // must be above max teams and equal to or below 255
19
20const int TTT_LET_CNT = 3;
21const int TTT_NUM_CNT = 3;
22const int TTT_TILE_SIZE = 3;
23
24.int ttt_npieces; // (minigame) number of pieces on the board (simplifies checking a draw)
25.int ttt_nexteam; // (minigame) next team (used to change the starting team on following matches)
26.int ttt_ai; // (minigame) when non-zero, singleplayer vs AI
27
28// find tic tac toe piece given its tile name
29entity ttt_find_piece(entity minig, string tile)
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}
37
38// Checks if the given piece completes a row
39bool ttt_winning_piece(entity minigame, entity piece)
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}
68
69// check if the tile name is valid (3x3 grid)
70bool ttt_valid_tile(string tile)
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}
78
79// make a move
80void ttt_move(entity minigame, entity player, string pos )
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}
108
109// request a new match
110void ttt_next_match(entity minigame, entity player)
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}
138
139#ifdef SVQC
140
141
142// required function, handle server side events
143int ttt_server_event(entity minigame, string event, ...)
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}
228
229
230#elif defined(CSQC)
231
232string ttt_curr_pos; // identifier of the tile under the mouse
233vector ttt_boardpos; // HUD board position
234vector ttt_boardsize;// HUD board size
235.int ttt_checkwin; // Used to optimize checks to display a win
236
237// Required function, draw the game board
238void ttt_hud_board(vector pos, vector mySize)
239{
240 minigame_hud_fitsqare(pos, mySize);
241 ttt_boardpos = pos;
242 ttt_boardsize = mySize;
243
244 minigame_hud_simpleboard(pos,mySize,minigame_texture("ttt/board"));
245
246 vector tile_size = minigame_hud_denormalize_size('1 1 0'/TTT_TILE_SIZE,pos,mySize);
247 vector tile_pos;
248
249 if ( (active_minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team )
250 if ( ttt_valid_tile(ttt_curr_pos) )
251 {
252 tile_pos = minigame_tile_pos(ttt_curr_pos,TTT_LET_CNT,TTT_NUM_CNT);
253 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
255 minigame_texture(strcat("ttt/piece",ftos(minigame_self.team))),
256 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
257 }
258
259 entity e;
261 {
262 if ( e.classname == "minigame_board_piece" )
263 {
264 tile_pos = minigame_tile_pos(e.netname,TTT_LET_CNT,TTT_NUM_CNT);
265 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
266
267 if ( active_minigame.minigame_flags & TTT_TURN_WIN )
268 if ( !e.ttt_checkwin )
269 e.ttt_checkwin = ttt_winning_piece(active_minigame, e) ? 1 : -1;
270
271 float icon_color = 1;
272 if ( e.ttt_checkwin == -1 )
273 icon_color = 0.4;
274 else if ( e.ttt_checkwin == 1 )
275 {
276 icon_color = 2;
277 minigame_drawpic_centered( tile_pos, minigame_texture("ttt/winglow"),
278 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE );
279 }
280
282 minigame_texture(strcat("ttt/piece",ftos(e.team))),
283 tile_size, '1 1 1'*icon_color, panel_fg_alpha, DRAWFLAG_NORMAL );
284 }
285 }
286
287 minigame_show_allspecs(ttt_boardpos, ttt_boardsize);
288}
289
290
291// Required function, draw the game status panel
292void ttt_hud_status(vector pos, vector mySize)
293{
295 vector ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
296 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
297
298 pos_y += ts_y;
299 mySize_y -= ts_y;
300
301 vector player_fontsize = hud_fontsize * 1.75;
302 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
303 ts_x = mySize_x;
304 vector mypos;
305 vector tile_size = '48 48 0';
306
307 entity e;
309 {
310 if ( e.classname == "minigame_player" && e.team != TTT_SPECTATOR_TEAM )
311 {
312 mypos = pos;
313 if ( e.team == 2 )
314 mypos_y += player_fontsize_y + ts_y;
316 (e.minigame_playerslot ? entcs_GetName(e.minigame_playerslot-1) : _("AI")),
317 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
318
319 mypos_y += player_fontsize_y;
320 drawpic( mypos,
321 minigame_texture(strcat("ttt/piece",ftos(e.team))),
322 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
323
324 mypos_x += tile_size_x;
325
326 drawstring(mypos,ftos(e.minigame_flags),tile_size,
327 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
328 }
329 }
330}
331
332// Turn a set of flags into a help message
333string ttt_turn_to_string(int turnflags)
334{
336 return _("You are spectating");
337
338 if ( turnflags & TTT_TURN_DRAW )
339 return _("Draw");
340
341 if ( turnflags & TTT_TURN_WIN )
342 {
343 // translator-friendly messages composed of 2 existing messages
344 // TODO: proper "you win" banner instead of hijacking the help message
345 if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
346 return strcat(_("You lost the game!"), "\n", _("Select \"^1Next Match^7\" on the menu for a rematch!"));
347 return strcat(_("You win!"), "\n", _("Select \"^1Next Match^7\" on the menu to start a new match!"));
348 }
349
350 if ( turnflags & TTT_TURN_NEXT )
351 {
352 if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
353 return _("Select \"^1Next Match^7\" on the menu to start a new match!");
354 return _("Wait for your opponent to confirm the rematch");
355 }
356
357 if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
358 return _("Wait for your opponent to make their move");
359
360 if ( turnflags & TTT_TURN_PLACE )
361 return _("Click on the game board to place your piece");
362
363 return "";
364}
365
366const int TTT_AI_POSFLAG_A1 = 0x0001;
367const int TTT_AI_POSFLAG_A2 = 0x0002;
368const int TTT_AI_POSFLAG_A3 = 0x0004;
369const int TTT_AI_POSFLAG_B1 = 0x0008;
370const int TTT_AI_POSFLAG_B2 = 0x0010;
371const int TTT_AI_POSFLAG_B3 = 0x0020;
372const int TTT_AI_POSFLAG_C1 = 0x0040;
373const int TTT_AI_POSFLAG_C2 = 0x0080;
374const int TTT_AI_POSFLAG_C3 = 0x0100;
375
376// convert a flag to a position
377string ttt_ai_piece_flag2pos(int pieceflag)
378{
379 switch(pieceflag)
380 {
381 case TTT_AI_POSFLAG_A1:
382 return "a1";
383 case TTT_AI_POSFLAG_A2:
384 return "a2";
385 case TTT_AI_POSFLAG_A3:
386 return "a3";
387
388 case TTT_AI_POSFLAG_B1:
389 return "b1";
390 case TTT_AI_POSFLAG_B2:
391 return "b2";
392 case TTT_AI_POSFLAG_B3:
393 return "b3";
394
395 case TTT_AI_POSFLAG_C1:
396 return "c1";
397 case TTT_AI_POSFLAG_C2:
398 return "c2";
399 case TTT_AI_POSFLAG_C3:
400 return "c3";
401
402 default:
403 return string_null;
404 }
405}
406
407bool ttt_ai_checkmask(int piecemask, int checkflags)
408{
409 return checkflags && (piecemask & checkflags) == checkflags;
410}
411
412// get the third flag if the mask matches two of them
413int ttt_ai_1of3(int piecemask, int flag1, int flag2, int flag3)
414{
415 if ( ttt_ai_checkmask(piecemask,flag1|flag2|flag3) )
416 return 0;
417
418 if ( ttt_ai_checkmask(piecemask,flag1|flag2) )
419 return flag3;
420
421 if ( ttt_ai_checkmask(piecemask,flag3|flag2) )
422 return flag1;
423
424 if ( ttt_ai_checkmask(piecemask,flag3|flag1) )
425 return flag2;
426
427 return 0;
428}
429
430// Select a random flag in the mask
431int ttt_ai_random(int piecemask)
432{
433 if ( !piecemask )
434 return 0;
435
436 int f = 1;
437
439
440 for ( int i = 0; i < 9; ++i )
441 {
442 if ( piecemask & f )
444 f <<= 1;
445 }
446
447 LOG_TRACE(sprintf("TTT AI: selected %x from %x",
448 RandomSelection_chosen_float, piecemask) );
450}
451
452// Block/complete a 3 i na row
453int ttt_ai_block3 ( int piecemask, int piecemask_free )
454{
455 int r = 0;
456
457 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_A3);
458 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_B3);
459 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_C1,TTT_AI_POSFLAG_C2,TTT_AI_POSFLAG_C3);
460 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_C1);
461 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C2);
462 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B3,TTT_AI_POSFLAG_C3);
463 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C3);
464 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C1);
465 LOG_TRACE(sprintf("TTT AI: possible 3 in a rows in %x: %x (%x)",piecemask,r, r&piecemask_free));
466 r &= piecemask_free;
467 return ttt_ai_random(r);
468}
469
470// Simple AI
471// 1) tries to win the game if possible
472// 2) tries to block the opponent if they have 2 in a row
473// 3) places a piece randomly
474string ttt_ai_choose_simple(int piecemask_self, int piecemask_opponent, int piecemask_free )
475{
476 int move = 0;
477
478 LOG_TRACE("TTT AI: checking winning move");
479 if (( move = ttt_ai_block3(piecemask_self,piecemask_free) ))
480 return ttt_ai_piece_flag2pos(move); // place winning move
481
482 LOG_TRACE("TTT AI: checking opponent's winning move");
483 if (( move = ttt_ai_block3(piecemask_opponent,piecemask_free) ))
484 return ttt_ai_piece_flag2pos(move); // block opponent
485
486 LOG_TRACE("TTT AI: random move");
487 return ttt_ai_piece_flag2pos(ttt_ai_random(piecemask_free));
488}
489
490// AI move (if it's AI's turn)
491void ttt_aimove(entity minigame)
492{
493 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame.ttt_ai) )
494 {
495 entity aiplayer = NULL;
496 while ( ( aiplayer = findfloat(aiplayer,minigame_id,minigame.minigame_id) ) )
497 if ( aiplayer.classname == "minigame_player" && !aiplayer.minigame_playerslot )
498 break;
499
500 /*
501 * Build bit masks for the board pieces
502 * .---.---.---.
503 * | 4 | 32|256| 3
504 * |---+---+---|
505 * | 2 | 16|128| 2
506 * |---+---+---|
507 * | 1 | 8 | 64| 1
508 * '---'---'---'
509 * A B C
510 */
511 int piecemask_self = 0;
512 int piecemask_opponent = 0;
513 int piecemask_free = 0;
514 int pieceflag = 1;
515 string pos;
516 for ( int i = 0; i < 3; ++i )
517 {
518 for ( int j = 0; j < 3; ++j )
519 {
520 pos = minigame_tile_buildname(i,j);
521 entity piece = ttt_find_piece(minigame,pos);
522 if ( piece )
523 {
524 if ( piece.team == aiplayer.team )
525 piecemask_self |= pieceflag;
526 else
527 piecemask_opponent |= pieceflag;
528 }
529 else
530 piecemask_free |= pieceflag;
531 pieceflag <<= 1;
532 }
533 }
534
535 // TODO multiple AI difficulties
536 LOG_TRACE(sprintf("TTT AI: self: %x opponent: %x free: %x",
537 piecemask_self, piecemask_opponent, piecemask_free));
538 pos = ttt_ai_choose_simple(piecemask_self, piecemask_opponent, piecemask_free);
539 LOG_TRACE("TTT AI: chosen move: ", pos);
540 if ( !pos )
541 LOG_TRACE("Tic Tac Toe AI has derped!");
542 else
543 ttt_move(minigame,aiplayer,pos);
544 }
545 strcpy(minigame.message, ttt_turn_to_string(minigame.minigame_flags));
546}
547
548// Make the correct move
549void ttt_make_move(entity minigame)
550{
551 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
552 {
553 if ( minigame.ttt_ai )
554 {
555 ttt_move(minigame, minigame_self, ttt_curr_pos );
556 ttt_aimove(minigame);
557 }
558 else
559 minigame_cmd("move ",ttt_curr_pos);
560 }
561}
562
563void ttt_set_curr_pos(string s)
564{
565 strfree(ttt_curr_pos);
566 if ( s )
567 s = strzone(s);
568 ttt_curr_pos = s;
569}
570
571// Required function, handle client events
572int ttt_client_event(entity minigame, string event, ...)
573{
574 switch(event)
575 {
576 case "activate":
577 {
578 ttt_set_curr_pos("");
579 strcpy(minigame.message, ttt_turn_to_string(minigame.minigame_flags));
580 return false;
581 }
582 case "deactivate":
583 {
584 strfree(minigame.message);
585 return false;
586 }
587 case "key_pressed":
588 case "key_released":
589 {
590 bool event_blocked = ((event == "key_released")
591 || ((minigame.minigame_flags & TTT_TURN_TEAM) != minigame_self.team));
592 if (!(minigame.minigame_flags & (TTT_TURN_WIN | TTT_TURN_DRAW)))
593 {
594 switch ( ...(0,int) )
595 {
596 case K_RIGHTARROW:
597 case K_KP_RIGHTARROW:
598 if (event_blocked)
599 return true;
600 if ( ! ttt_curr_pos )
601 ttt_set_curr_pos("a3");
602 else
603 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,1,0,TTT_LET_CNT,TTT_NUM_CNT));
604 return true;
605 case K_LEFTARROW:
606 case K_KP_LEFTARROW:
607 if (event_blocked)
608 return true;
609 if ( ! ttt_curr_pos )
610 ttt_set_curr_pos("c3");
611 else
612 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,-1,0,TTT_LET_CNT,TTT_NUM_CNT));
613 return true;
614 case K_UPARROW:
615 case K_KP_UPARROW:
616 if (event_blocked)
617 return true;
618 if ( ! ttt_curr_pos )
619 ttt_set_curr_pos("a1");
620 else
621 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,1,TTT_LET_CNT,TTT_NUM_CNT));
622 return true;
623 case K_DOWNARROW:
624 case K_KP_DOWNARROW:
625 if (event_blocked)
626 return true;
627 if ( ! ttt_curr_pos )
628 ttt_set_curr_pos("a3");
629 else
630 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,-1,TTT_LET_CNT,TTT_NUM_CNT));
631 return true;
632 case K_ENTER:
633 case K_KP_ENTER:
634 case K_SPACE:
635 if (event_blocked)
636 return true;
637 ttt_make_move(minigame);
638 return true;
639 }
640 }
641
642 return false;
643 }
644 case "mouse_pressed":
645 {
646 if(...(0,int) == K_MOUSE1)
647 {
648 ttt_client_event(minigame, "mouse_moved");
649 ttt_make_move(minigame);
650 return true;
651 }
652
653 return false;
654 }
655 case "mouse_moved":
656 {
657 vector mouse_pos = minigame_hud_normalize(mousepos,ttt_boardpos,ttt_boardsize);
658 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
659 ttt_set_curr_pos(minigame_tile_name(mouse_pos,TTT_LET_CNT,TTT_NUM_CNT));
660 if ( ! ttt_valid_tile(ttt_curr_pos) )
661 ttt_set_curr_pos("");
662
663 return true;
664 }
665 case "network_receive":
666 {
667 entity sent = ...(0,entity);
668 int sf = ...(1,int);
669 if ( sent.classname == "minigame" )
670 {
671 if ( sf & MINIG_SF_UPDATE )
672 {
673 strcpy(sent.message, ttt_turn_to_string(sent.minigame_flags));
674 if ( sent.minigame_flags & minigame_self.team )
676 }
677
678 if ( (sf & TTT_SF_SINGLEPLAYER) )
679 {
680 int ai = ReadByte();
681 bool spawnai = ai && !sent.ttt_ai;
682 sent.ttt_ai = ai;
683
684 if ( spawnai )
685 {
686 entity aiplayer = new(minigame_player);
687 aiplayer.owner = minigame;
688 aiplayer.minigame_id = minigame.minigame_id;
689 aiplayer.descriptor = minigame.descriptor;
690 aiplayer.team = ai;
691 aiplayer.minigame_playerslot = 0;
692 aiplayer.minigame_autoclean = 1;
693 ttt_aimove(minigame);
694 }
695
696 }
697 }
698 else if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
699 {
700 sent.minigame_flags = ReadByte();
701 }
702
703 return false;
704 }
705 case "menu_show":
706 {
707 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
708 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Single Player"),"singleplayer");
709 return false;
710 }
711 case "menu_click":
712 {
713 if(...(0,string) == "next")
714 {
715 if ( minigame.ttt_ai )
716 {
718 ttt_aimove(minigame);
719 }
720 else
721 minigame_cmd("next");
722 }
723 else if ( ...(0,string) == "singleplayer" && !minigame.ttt_ai )
724 {
725 if ( minigame_count_players(minigame) == 1 )
726 minigame_cmd("singleplayer");
727 }
728 return false;
729 }
730 }
731
732 return false;
733}
734
735#endif
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
void minigame_drawcolorcodedstring_trunc(float maxwidth, vector pos, string text, vector fontsize, float theAlpha, int drawflags)
void minigame_show_allspecs(vector boardpos, vector boardsize)
void minigame_prompt()
vector minigame_drawstring_wrapped(float maxwidth, vector pos, string text, vector fontsize, vector color, float theAlpha, int drawflags, float align)
void minigame_hud_simpleboard(vector pos, vector mySize, string board_texture)
string minigame_texture(string name)
vector minigame_hud_denormalize_size(vector v, vector pos, vector mySize)
vector minigame_hud_denormalize(vector v, vector pos, vector mySize)
vector minigame_hud_normalize(vector v, vector pos, vector mySize)
void minigame_drawpic_centered(vector pos, string texture, vector sz, vector color, float thealpha, int drawflags)
#define REGISTER_MINIGAME(name, nicename)
entity minigame_self
entity active_minigame
#define minigame_hud_fitsqare(pos, mySize)
#define minigame_cmd(...)
#define FOREACH_MINIGAME_ENTITY(entityvar)
void HUD_MinigameMenu_CustomEntry(entity parent, string menumessage, string event_arg)
#define drawstring(position, text, scale, rgb, alpha, flag)
Definition draw.qh:27
#define drawpic(position, pic, size, rgb, alpha, flag)
Definition draw.qh:21
vector hud_fontsize
Definition main.qh:77
int team
Definition main.qh:188
const float DRAWFLAG_NORMAL
const float DRAWFLAG_ADDITIVE
WriteByte(chan, ent.angles.y/DEC_FACTOR)
string entcs_GetName(int i)
Definition ent_cs.qh:148
float panel_fg_alpha
Definition hud.qh:169
#define HUD_Panel_DrawBg()
Definition hud.qh:55
vector mousepos
Definition hud.qh:103
float K_KP_RIGHTARROW
Definition keycodes.qc:60
float K_UPARROW
Definition keycodes.qc:15
float K_DOWNARROW
Definition keycodes.qc:16
float K_MOUSE1
Definition keycodes.qc:129
float K_RIGHTARROW
Definition keycodes.qc:18
float K_KP_UPARROW
Definition keycodes.qc:64
float K_SPACE
Definition keycodes.qc:10
float K_KP_LEFTARROW
Definition keycodes.qc:57
float K_ENTER
Definition keycodes.qc:8
float K_KP_DOWNARROW
Definition keycodes.qc:53
float K_LEFTARROW
Definition keycodes.qc:17
float K_KP_ENTER
Definition keycodes.qc:74
#define int
Definition _all.inc:20
const int MSG_ENTITY
Definition net.qh:156
int ReadByte()
#define LOG_TRACE(...)
Definition log.qh:74
string ftos(float f)
string strzone(string s)
string argv(float n)
entity findfloat(entity start,.float field, float match)
entity msle_spawn(entity minigame_session, entity e)
Definition minigames.qc:96
string minigame_relative_tile(string start_id, int dx, int dy, int rows, int columns)
Definition minigames.qc:49
void minigame_server_sendflags(entity ent, int mgflags)
Definition minigames.qc:87
int minigame_count_players(entity minigame)
Definition minigames.qc:132
int minigame_next_team(int curr_team, int n_teams)
Definition minigames.qc:74
int minigame_tile_letter(string id)
Definition minigames.qc:21
string minigame_tile_name(vector pos, int rows, int columns)
Definition minigames.qc:63
vector minigame_tile_pos(string id, int rows, int columns)
Definition minigames.qc:36
string minigame_tile_buildname(int letter, int number)
Definition minigames.qc:43
int minigame_tile_number(string id)
Definition minigames.qc:30
int minigame_id
Unique ID for the minigame session, associated with all objects currently the result of etof(minigame...
Definition minigames.qh:49
const int MINIG_SF_UPDATE
Definition minigames.qh:113
const int MINIG_SF_ALL
Definition minigames.qh:116
const int MINIG_SF_CUSTOM
Definition minigames.qh:114
string string_null
Definition nil.qh:9
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
#define NULL
Definition post.qh:14
ERASEABLE void RandomSelection_Init()
Definition random.qc:4
float RandomSelection_chosen_float
Definition random.qh:6
#define RandomSelection_AddFloat(f, weight, priority)
Definition random.qh:15
vector
Definition self.qh:96
int int number
Definition impulse.qc:89
#define strfree(this)
Definition string.qh:57
#define strcpy(this, s)
Definition string.qh:51
const int TTT_TURN_TEAM2
Definition ttt.qc:11
const int TTT_SF_SINGLEPLAYER
Definition ttt.qc:16
const int TTT_TURN_TYPE
Definition ttt.qc:8
const int TTT_TURN_PLACE
Definition ttt.qc:4
const int TTT_SF_PLAYERSCORE
Definition ttt.qc:15
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_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
int ttt_server_event(entity minigame, string event,...)
Definition ttt.qc:143
int ttt_nexteam
Definition ttt.qc:25
const int TTT_SPECTATOR_TEAM
Definition ttt.qc:18
int ttt_npieces
Definition ttt.qc:24
const int TTT_TURN_TEAM
Definition ttt.qc:12
const int TTT_NUM_CNT
Definition ttt.qc:21
const int TTT_TURN_NEXT
Definition ttt.qc:7
entity ttt_find_piece(entity minig, string tile)
Definition ttt.qc:29
int ttt_ai
Definition ttt.qc:26
bool ttt_valid_tile(string tile)
Definition ttt.qc:70
void ttt_next_match(entity minigame, entity player)
Definition ttt.qc:110
const int TTT_TURN_WIN
Definition ttt.qc:5
const int TTT_TILE_SIZE
Definition ttt.qc:22