2 REGISTER_MINIGAME(ttt, _("Tic Tac Toe"));
4 const int TTT_TURN_PLACE = 0x0100; // player has to place a piece on the board
5 const int TTT_TURN_WIN = 0x0200; // player has won
6 const int TTT_TURN_DRAW = 0x0400; // no moves are possible
7 const int TTT_TURN_NEXT = 0x0800; // a player wants to start a new match
8 const int TTT_TURN_TYPE = 0x0f00; // turn type mask
10 const int TTT_TURN_TEAM1 = 0x0001;
11 const int TTT_TURN_TEAM2 = 0x0002;
12 const int TTT_TURN_TEAM = 0x000f; // turn team mask
15 const int TTT_SF_PLAYERSCORE = MINIG_SF_CUSTOM; // send minigame_player scores (won matches)
16 const int TTT_SF_SINGLEPLAYER = MINIG_SF_CUSTOM<<1;// send minigame.ttt_ai
18 const int TTT_SPECTATOR_TEAM = 255; // must be above max teams and equal to or below 255
20 const int TTT_LET_CNT = 3;
21 const int TTT_NUM_CNT = 3;
22 const int TTT_TILE_SIZE = 3;
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
28 // find tic tac toe piece given its tile name
29 entity ttt_find_piece(entity minig, string tile)
32 while ( ( e = findentity(e,owner,minig) ) )
33 if ( e.classname == "minigame_board_piece" && e.netname == tile )
38 // Checks if the given piece completes a row
39 bool ttt_winning_piece(entity piece)
41 int number = minigame_tile_number(piece.netname);
42 int letter = minigame_tile_letter(piece.netname);
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 )
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 )
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 )
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 )
69 // check if the tile name is valid (3x3 grid)
70 bool ttt_valid_tile(string tile)
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;
80 void ttt_move(entity minigame, entity player, string pos )
82 if ( minigame.minigame_flags & TTT_TURN_PLACE )
83 if ( pos && player.team == (minigame.minigame_flags & TTT_TURN_TEAM) )
85 if ( ttt_valid_tile(pos) )
86 if ( !ttt_find_piece(minigame,pos) )
88 entity piece = msle_spawn(minigame,new(minigame_board_piece));
89 piece.team = player.team;
90 piece.netname = strzone(pos);
91 minigame_server_sendflags(piece,MINIG_SF_ALL);
92 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
93 minigame.ttt_npieces++;
94 minigame.ttt_nexteam = minigame_next_team(player.team,2);
95 if ( ttt_winning_piece(piece) )
97 player.minigame_flags++;
98 minigame_server_sendflags(player, TTT_SF_PLAYERSCORE);
99 minigame.minigame_flags = TTT_TURN_WIN | player.team;
101 else if ( minigame.ttt_npieces >= (TTT_LET_CNT * TTT_NUM_CNT) )
102 minigame.minigame_flags = TTT_TURN_DRAW;
104 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
109 // request a new match
110 void ttt_next_match(entity minigame, entity player)
113 // on multiplayer matches, wait for both players to agree
114 if ( minigame.minigame_flags & (TTT_TURN_WIN|TTT_TURN_DRAW) )
116 minigame.minigame_flags = TTT_TURN_NEXT | player.team;
117 minigame.SendFlags |= MINIG_SF_UPDATE;
119 else if ( (minigame.minigame_flags & TTT_TURN_NEXT) &&
120 !( minigame.minigame_flags & player.team ) )
123 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
124 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
125 minigame.ttt_npieces = 0;
127 while ( ( e = findentity(e,owner,minigame) ) )
128 if ( e.classname == "minigame_board_piece" )
136 // required function, handle server side events
137 int ttt_server_event(entity minigame, string event, ...)
143 minigame.minigame_flags = (TTT_TURN_PLACE | TTT_TURN_TEAM1);
149 while( (e = findentity(e, owner, minigame)) )
150 if(e.classname == "minigame_board_piece")
159 int pl_num = minigame_count_players(minigame);
161 // Don't allow joining a single player match
162 if ( (minigame.ttt_ai) && pl_num > 0 )
165 // Don't allow more than 2 players
166 if(pl_num >= 2) { return TTT_SPECTATOR_TEAM; }
168 // Get the right team
169 if(minigame.minigame_players)
170 return minigame_next_team(minigame.minigame_players.team, 2);
177 entity player = ...(0,entity);
178 bool event_blocked = (player.team == TTT_SPECTATOR_TEAM);
184 ttt_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
189 ttt_next_match(minigame,...(0,entity));
194 if ( minigame_count_players(minigame) == 1 )
196 minigame.ttt_ai = minigame_next_team(minigame.minigame_players.team, 2);
197 minigame.SendFlags = TTT_SF_SINGLEPLAYER;
206 entity sent = ...(0,entity);
208 if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
210 WriteByte(MSG_ENTITY,sent.minigame_flags);
212 else if ( sent.classname == "minigame" && (sf & TTT_SF_SINGLEPLAYER) )
214 WriteByte(MSG_ENTITY,sent.ttt_ai);
226 string ttt_curr_pos; // identifier of the tile under the mouse
227 vector ttt_boardpos; // HUD board position
228 vector ttt_boardsize;// HUD board size
229 .int ttt_checkwin; // Used to optimize checks to display a win
231 // Required function, draw the game board
232 void ttt_hud_board(vector pos, vector mySize)
234 minigame_hud_fitsqare(pos, mySize);
236 ttt_boardsize = mySize;
238 minigame_hud_simpleboard(pos,mySize,minigame_texture("ttt/board"));
240 vector tile_size = minigame_hud_denormalize_size('1 1 0'/TTT_TILE_SIZE,pos,mySize);
243 if ( (active_minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team )
244 if ( ttt_valid_tile(ttt_curr_pos) )
246 tile_pos = minigame_tile_pos(ttt_curr_pos,TTT_LET_CNT,TTT_NUM_CNT);
247 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
248 minigame_drawpic_centered( tile_pos,
249 minigame_texture(strcat("ttt/piece",ftos(minigame_self.team))),
250 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
254 FOREACH_MINIGAME_ENTITY(e)
256 if ( e.classname == "minigame_board_piece" )
258 tile_pos = minigame_tile_pos(e.netname,TTT_LET_CNT,TTT_NUM_CNT);
259 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
261 if ( active_minigame.minigame_flags & TTT_TURN_WIN )
262 if ( !e.ttt_checkwin )
263 e.ttt_checkwin = ttt_winning_piece(e) ? 1 : -1;
265 float icon_color = 1;
266 if ( e.ttt_checkwin == -1 )
268 else if ( e.ttt_checkwin == 1 )
271 minigame_drawpic_centered( tile_pos, minigame_texture("ttt/winglow"),
272 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE );
275 minigame_drawpic_centered( tile_pos,
276 minigame_texture(strcat("ttt/piece",ftos(e.team))),
277 tile_size, '1 1 1'*icon_color, panel_fg_alpha, DRAWFLAG_NORMAL );
281 minigame_show_allspecs(ttt_boardpos, ttt_boardsize);
285 // Required function, draw the game status panel
286 void ttt_hud_status(vector pos, vector mySize)
290 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
291 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
296 vector player_fontsize = hud_fontsize * 1.75;
297 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
300 vector tile_size = '48 48 0';
303 FOREACH_MINIGAME_ENTITY(e)
305 if ( e.classname == "minigame_player" && e.team != TTT_SPECTATOR_TEAM )
309 mypos_y += player_fontsize_y + ts_y;
310 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
311 (e.minigame_playerslot ? entcs_GetName(e.minigame_playerslot-1) : _("AI")),
312 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
314 mypos_y += player_fontsize_y;
316 minigame_texture(strcat("ttt/piece",ftos(e.team))),
317 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
319 mypos_x += tile_size_x;
321 drawstring(mypos,ftos(e.minigame_flags),tile_size,
322 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
327 // Turn a set of flags into a help message
328 string ttt_turn_to_string(int turnflags)
330 if(minigame_self.team == TTT_SPECTATOR_TEAM)
331 return _("You are spectating");
333 if ( turnflags & TTT_TURN_DRAW )
336 if ( turnflags & TTT_TURN_WIN )
338 // translator-friendly messages composed of 2 existing messages
339 // TODO: proper "you win" banner instead of hijacking the help message
340 if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
341 return strcat(_("You lost the game!"), "\n", _("Select \"^1Next Match^7\" on the menu for a rematch!"));
342 return strcat(_("You win!"), "\n", _("Select \"^1Next Match^7\" on the menu to start a new match!"));
345 if ( turnflags & TTT_TURN_NEXT )
347 if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
348 return _("Select \"^1Next Match^7\" on the menu to start a new match!");
349 return _("Wait for your opponent to confirm the rematch");
352 if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
353 return _("Wait for your opponent to make their move");
355 if ( turnflags & TTT_TURN_PLACE )
356 return _("Click on the game board to place your piece");
361 const int TTT_AI_POSFLAG_A1 = 0x0001;
362 const int TTT_AI_POSFLAG_A2 = 0x0002;
363 const int TTT_AI_POSFLAG_A3 = 0x0004;
364 const int TTT_AI_POSFLAG_B1 = 0x0008;
365 const int TTT_AI_POSFLAG_B2 = 0x0010;
366 const int TTT_AI_POSFLAG_B3 = 0x0020;
367 const int TTT_AI_POSFLAG_C1 = 0x0040;
368 const int TTT_AI_POSFLAG_C2 = 0x0080;
369 const int TTT_AI_POSFLAG_C3 = 0x0100;
371 // convert a flag to a position
372 string ttt_ai_piece_flag2pos(int pieceflag)
376 case TTT_AI_POSFLAG_A1:
378 case TTT_AI_POSFLAG_A2:
380 case TTT_AI_POSFLAG_A3:
383 case TTT_AI_POSFLAG_B1:
385 case TTT_AI_POSFLAG_B2:
387 case TTT_AI_POSFLAG_B3:
390 case TTT_AI_POSFLAG_C1:
392 case TTT_AI_POSFLAG_C2:
394 case TTT_AI_POSFLAG_C3:
402 bool ttt_ai_checkmask(int piecemask, int checkflags)
404 return checkflags && (piecemask & checkflags) == checkflags;
407 // get the third flag if the mask matches two of them
408 int ttt_ai_1of3(int piecemask, int flag1, int flag2, int flag3)
410 if ( ttt_ai_checkmask(piecemask,flag1|flag2|flag3) )
413 if ( ttt_ai_checkmask(piecemask,flag1|flag2) )
416 if ( ttt_ai_checkmask(piecemask,flag3|flag2) )
419 if ( ttt_ai_checkmask(piecemask,flag3|flag1) )
425 // Select a random flag in the mask
426 int ttt_ai_random(int piecemask)
433 RandomSelection_Init();
435 for ( int i = 0; i < 9; ++i )
438 RandomSelection_AddFloat(f, 1, 1);
442 LOG_TRACE(sprintf("TTT AI: selected %x from %x",
443 RandomSelection_chosen_float, piecemask) );
444 return RandomSelection_chosen_float;
447 // Block/complete a 3 i na row
448 int ttt_ai_block3 ( int piecemask, int piecemask_free )
452 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_A3);
453 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_B3);
454 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_C1,TTT_AI_POSFLAG_C2,TTT_AI_POSFLAG_C3);
455 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_C1);
456 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C2);
457 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B3,TTT_AI_POSFLAG_C3);
458 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C3);
459 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C1);
460 LOG_TRACE(sprintf("TTT AI: possible 3 in a rows in %x: %x (%x)",piecemask,r, r&piecemask_free));
462 return ttt_ai_random(r);
466 // 1) tries to win the game if possible
467 // 2) tries to block the opponent if they have 2 in a row
468 // 3) places a piece randomly
469 string ttt_ai_choose_simple(int piecemask_self, int piecemask_opponent, int piecemask_free )
473 LOG_TRACE("TTT AI: checking winning move");
474 if (( move = ttt_ai_block3(piecemask_self,piecemask_free) ))
475 return ttt_ai_piece_flag2pos(move); // place winning move
477 LOG_TRACE("TTT AI: checking opponent's winning move");
478 if (( move = ttt_ai_block3(piecemask_opponent,piecemask_free) ))
479 return ttt_ai_piece_flag2pos(move); // block opponent
481 LOG_TRACE("TTT AI: random move");
482 return ttt_ai_piece_flag2pos(ttt_ai_random(piecemask_free));
485 // AI move (if it's AI's turn)
486 void ttt_aimove(entity minigame)
488 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame.ttt_ai) )
490 entity aiplayer = NULL;
491 while ( ( aiplayer = findentity(aiplayer,owner,minigame) ) )
492 if ( aiplayer.classname == "minigame_player" && !aiplayer.minigame_playerslot )
496 * Build bit masks for the board pieces
506 int piecemask_self = 0;
507 int piecemask_opponent = 0;
508 int piecemask_free = 0;
511 for ( int i = 0; i < 3; ++i )
513 for ( int j = 0; j < 3; ++j )
515 pos = minigame_tile_buildname(i,j);
516 entity piece = ttt_find_piece(minigame,pos);
519 if ( piece.team == aiplayer.team )
520 piecemask_self |= pieceflag;
522 piecemask_opponent |= pieceflag;
525 piecemask_free |= pieceflag;
530 // TODO multiple AI difficulties
531 LOG_TRACE(sprintf("TTT AI: self: %x opponent: %x free: %x",
532 piecemask_self, piecemask_opponent, piecemask_free));
533 pos = ttt_ai_choose_simple(piecemask_self, piecemask_opponent, piecemask_free);
534 LOG_TRACE("TTT AI: chosen move: ", pos);
536 LOG_TRACE("Tic Tac Toe AI has derped!");
538 ttt_move(minigame,aiplayer,pos);
540 strcpy(minigame.message, ttt_turn_to_string(minigame.minigame_flags));
543 // Make the correct move
544 void ttt_make_move(entity minigame)
546 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
548 if ( minigame.ttt_ai )
550 ttt_move(minigame, minigame_self, ttt_curr_pos );
551 ttt_aimove(minigame);
554 minigame_cmd("move ",ttt_curr_pos);
558 void ttt_set_curr_pos(string s)
560 strfree(ttt_curr_pos);
566 // Required function, handle client events
567 int ttt_client_event(entity minigame, string event, ...)
573 ttt_set_curr_pos("");
574 strcpy(minigame.message, ttt_turn_to_string(minigame.minigame_flags));
579 strfree(minigame.message);
585 bool event_blocked = ((event == "key_released")
586 || ((minigame.minigame_flags & TTT_TURN_TEAM) != minigame_self.team));
587 if (!(minigame.minigame_flags & TTT_TURN_WIN) && !(minigame.minigame_flags & TTT_TURN_DRAW))
589 switch ( ...(0,int) )
592 case K_KP_RIGHTARROW:
595 if ( ! ttt_curr_pos )
596 ttt_set_curr_pos("a3");
598 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,1,0,TTT_LET_CNT,TTT_NUM_CNT));
604 if ( ! ttt_curr_pos )
605 ttt_set_curr_pos("c3");
607 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,-1,0,TTT_LET_CNT,TTT_NUM_CNT));
613 if ( ! ttt_curr_pos )
614 ttt_set_curr_pos("a1");
616 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,1,TTT_LET_CNT,TTT_NUM_CNT));
622 if ( ! ttt_curr_pos )
623 ttt_set_curr_pos("a3");
625 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,-1,TTT_LET_CNT,TTT_NUM_CNT));
632 ttt_make_move(minigame);
639 case "mouse_pressed":
641 if(...(0,int) == K_MOUSE1)
643 ttt_client_event(minigame, "mouse_moved");
644 ttt_make_move(minigame);
652 vector mouse_pos = minigame_hud_normalize(mousepos,ttt_boardpos,ttt_boardsize);
653 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
654 ttt_set_curr_pos(minigame_tile_name(mouse_pos,TTT_LET_CNT,TTT_NUM_CNT));
655 if ( ! ttt_valid_tile(ttt_curr_pos) )
656 ttt_set_curr_pos("");
660 case "network_receive":
662 entity sent = ...(0,entity);
664 if ( sent.classname == "minigame" )
666 if ( sf & MINIG_SF_UPDATE )
668 strcpy(sent.message, ttt_turn_to_string(sent.minigame_flags));
669 if ( sent.minigame_flags & minigame_self.team )
673 if ( (sf & TTT_SF_SINGLEPLAYER) )
676 bool spawnai = ai && !sent.ttt_ai;
681 entity aiplayer = new(minigame_player);
682 aiplayer.owner = minigame;
684 aiplayer.minigame_playerslot = 0;
685 aiplayer.minigame_autoclean = 1;
686 ttt_aimove(minigame);
691 else if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
693 sent.minigame_flags = ReadByte();
700 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
701 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Single Player"),"singleplayer");
706 if(...(0,string) == "next")
708 if ( minigame.ttt_ai )
710 ttt_next_match(minigame,minigame_self);
711 ttt_aimove(minigame);
714 minigame_cmd("next");
716 else if ( ...(0,string) == "singleplayer" && !minigame.ttt_ai )
718 if ( minigame_count_players(minigame) == 1 )
719 minigame_cmd("singleplayer");