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_LET_CNT = 3;
19 const int TTT_NUM_CNT = 3;
20 const int TTT_TILE_SIZE = 3;
22 .int ttt_npieces; // (minigame) number of pieces on the board (simplifies checking a draw)
23 .int ttt_nexteam; // (minigame) next team (used to change the starting team on following matches)
24 .int ttt_ai; // (minigame) when non-zero, singleplayer vs AI
26 // find tic tac toe piece given its tile name
27 entity ttt_find_piece(entity minig, string tile)
30 while ( ( e = findentity(e,owner,minig) ) )
31 if ( e.classname == "minigame_board_piece" && e.netname == tile )
36 // Checks if the given piece completes a row
37 bool ttt_winning_piece(entity piece)
39 int number = minigame_tile_number(piece.netname);
40 int letter = minigame_tile_letter(piece.netname);
42 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,number)).team == piece.team )
43 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,number)).team == piece.team )
44 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,number)).team == piece.team )
47 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,0)).team == piece.team )
48 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,1)).team == piece.team )
49 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,2)).team == piece.team )
52 if ( number == letter )
53 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,0)).team == piece.team )
54 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,1)).team == piece.team )
55 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,2)).team == piece.team )
58 if ( number == 2-letter )
59 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,2)).team == piece.team )
60 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,1)).team == piece.team )
61 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,0)).team == piece.team )
67 // check if the tile name is valid (3x3 grid)
68 bool ttt_valid_tile(string tile)
72 int number = minigame_tile_number(tile);
73 int letter = minigame_tile_letter(tile);
74 return 0 <= number && number < TTT_NUM_CNT && 0 <= letter && letter < TTT_LET_CNT;
78 void ttt_move(entity minigame, entity player, string pos )
80 if ( minigame.minigame_flags & TTT_TURN_PLACE )
81 if ( pos && player.team == (minigame.minigame_flags & TTT_TURN_TEAM) )
83 if ( ttt_valid_tile(pos) )
84 if ( !ttt_find_piece(minigame,pos) )
86 entity piece = msle_spawn(minigame,"minigame_board_piece");
87 piece.team = player.team;
88 piece.netname = strzone(pos);
89 minigame_server_sendflags(piece,MINIG_SF_ALL);
90 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
91 minigame.ttt_npieces++;
92 minigame.ttt_nexteam = minigame_next_team(player.team,2);
93 if ( ttt_winning_piece(piece) )
95 player.minigame_flags++;
96 minigame_server_sendflags(player, TTT_SF_PLAYERSCORE);
97 minigame.minigame_flags = TTT_TURN_WIN | player.team;
99 else if ( minigame.ttt_npieces >= (TTT_LET_CNT * TTT_NUM_CNT) )
100 minigame.minigame_flags = TTT_TURN_DRAW;
102 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
107 // request a new match
108 void ttt_next_match(entity minigame, entity player)
111 // on multiplayer matches, wait for both players to agree
112 if ( minigame.minigame_flags & (TTT_TURN_WIN|TTT_TURN_DRAW) )
114 minigame.minigame_flags = TTT_TURN_NEXT | player.team;
115 minigame.SendFlags |= MINIG_SF_UPDATE;
117 else if ( (minigame.minigame_flags & TTT_TURN_NEXT) &&
118 !( minigame.minigame_flags & player.team ) )
121 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
122 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
123 minigame.ttt_npieces = 0;
125 while ( ( e = findentity(e,owner,minigame) ) )
126 if ( e.classname == "minigame_board_piece" )
134 // required function, handle server side events
135 int ttt_server_event(entity minigame, string event, ...)
141 minigame.minigame_flags = (TTT_TURN_PLACE | TTT_TURN_TEAM1);
147 while( (e = findentity(e, owner, minigame)) )
148 if(e.classname == "minigame_board_piece")
150 if(e.netname) { strunzone(e.netname); }
157 int pl_num = minigame_count_players(minigame);
159 // Don't allow joining a single player match
160 if ( (minigame.ttt_ai) && pl_num > 0 )
163 // Don't allow more than 2 players
164 if(pl_num >= 2) { return false; }
166 // Get the right team
167 if(minigame.minigame_players)
168 return minigame_next_team(minigame.minigame_players.team, 2);
178 ttt_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
181 ttt_next_match(minigame,...(0,entity));
184 if ( minigame_count_players(minigame) == 1 )
186 minigame.ttt_ai = minigame_next_team(minigame.minigame_players.team, 2);
187 minigame.SendFlags = TTT_SF_SINGLEPLAYER;
196 entity sent = ...(0,entity);
198 if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
200 WriteByte(MSG_ENTITY,sent.minigame_flags);
202 else if ( sent.classname == "minigame" && (sf & TTT_SF_SINGLEPLAYER) )
204 WriteByte(MSG_ENTITY,sent.ttt_ai);
216 string ttt_curr_pos; // identifier of the tile under the mouse
217 vector ttt_boardpos; // HUD board position
218 vector ttt_boardsize;// HUD board size
219 .int ttt_checkwin; // Used to optimize checks to display a win
221 // Required function, draw the game board
222 void ttt_hud_board(vector pos, vector mySize)
224 minigame_hud_fitsqare(pos, mySize);
226 ttt_boardsize = mySize;
228 minigame_hud_simpleboard(pos,mySize,minigame_texture("ttt/board"));
230 vector tile_size = minigame_hud_denormalize_size('1 1 0'/TTT_TILE_SIZE,pos,mySize);
233 if ( (active_minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team )
234 if ( ttt_valid_tile(ttt_curr_pos) )
236 tile_pos = minigame_tile_pos(ttt_curr_pos,TTT_LET_CNT,TTT_NUM_CNT);
237 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
238 minigame_drawpic_centered( tile_pos,
239 minigame_texture(strcat("ttt/piece",ftos(minigame_self.team))),
240 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
244 FOREACH_MINIGAME_ENTITY(e)
246 if ( e.classname == "minigame_board_piece" )
248 tile_pos = minigame_tile_pos(e.netname,TTT_LET_CNT,TTT_NUM_CNT);
249 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
251 if ( active_minigame.minigame_flags & TTT_TURN_WIN )
252 if ( !e.ttt_checkwin )
253 e.ttt_checkwin = ttt_winning_piece(e) ? 1 : -1;
255 float icon_color = 1;
256 if ( e.ttt_checkwin == -1 )
258 else if ( e.ttt_checkwin == 1 )
261 minigame_drawpic_centered( tile_pos, minigame_texture("ttt/winglow"),
262 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE );
265 minigame_drawpic_centered( tile_pos,
266 minigame_texture(strcat("ttt/piece",ftos(e.team))),
267 tile_size, '1 1 1'*icon_color, panel_fg_alpha, DRAWFLAG_NORMAL );
273 // Required function, draw the game status panel
274 void ttt_hud_status(vector pos, vector mySize)
278 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
279 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
284 vector player_fontsize = hud_fontsize * 1.75;
285 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
288 vector tile_size = '48 48 0';
291 FOREACH_MINIGAME_ENTITY(e)
293 if ( e.classname == "minigame_player" )
297 mypos_y += player_fontsize_y + ts_y;
298 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
299 (e.minigame_playerslot ? entcs_GetName(e.minigame_playerslot-1) : _("AI")),
300 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
302 mypos_y += player_fontsize_y;
304 minigame_texture(strcat("ttt/piece",ftos(e.team))),
305 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
307 mypos_x += tile_size_x;
309 drawstring(mypos,ftos(e.minigame_flags),tile_size,
310 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
315 // Turn a set of flags into a help message
316 string ttt_turn_to_string(int turnflags)
318 if ( turnflags & TTT_TURN_DRAW )
321 if ( turnflags & TTT_TURN_WIN )
323 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
324 return _("You lost the game!\nSelect \"^1Next Match^7\" on the menu for a rematch!");
325 return _("You win!\nSelect \"^1Next Match^7\" on the menu to start a new match!");
328 if ( turnflags & TTT_TURN_NEXT )
330 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
331 return _("Select \"^1Next Match^7\" on the menu to start a new match!");
332 return _("Wait for your opponent to confirm the rematch");
335 if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
336 return _("Wait for your opponent to make their move");
338 if ( turnflags & TTT_TURN_PLACE )
339 return _("Click on the game board to place your piece");
344 const int TTT_AI_POSFLAG_A1 = 0x0001;
345 const int TTT_AI_POSFLAG_A2 = 0x0002;
346 const int TTT_AI_POSFLAG_A3 = 0x0004;
347 const int TTT_AI_POSFLAG_B1 = 0x0008;
348 const int TTT_AI_POSFLAG_B2 = 0x0010;
349 const int TTT_AI_POSFLAG_B3 = 0x0020;
350 const int TTT_AI_POSFLAG_C1 = 0x0040;
351 const int TTT_AI_POSFLAG_C2 = 0x0080;
352 const int TTT_AI_POSFLAG_C3 = 0x0100;
354 // convert a flag to a position
355 string ttt_ai_piece_flag2pos(int pieceflag)
359 case TTT_AI_POSFLAG_A1:
361 case TTT_AI_POSFLAG_A2:
363 case TTT_AI_POSFLAG_A3:
366 case TTT_AI_POSFLAG_B1:
368 case TTT_AI_POSFLAG_B2:
370 case TTT_AI_POSFLAG_B3:
373 case TTT_AI_POSFLAG_C1:
375 case TTT_AI_POSFLAG_C2:
377 case TTT_AI_POSFLAG_C3:
385 bool ttt_ai_checkmask(int piecemask, int checkflags)
387 return checkflags && (piecemask & checkflags) == checkflags;
390 // get the third flag if the mask matches two of them
391 int ttt_ai_1of3(int piecemask, int flag1, int flag2, int flag3)
393 if ( ttt_ai_checkmask(piecemask,flag1|flag2|flag3) )
396 if ( ttt_ai_checkmask(piecemask,flag1|flag2) )
399 if ( ttt_ai_checkmask(piecemask,flag3|flag2) )
402 if ( ttt_ai_checkmask(piecemask,flag3|flag1) )
408 // Select a random flag in the mask
409 int ttt_ai_random(int piecemask)
416 RandomSelection_Init();
418 for ( int i = 0; i < 9; i++ )
421 RandomSelection_AddFloat(f, 1, 1);
425 LOG_TRACE(sprintf("TTT AI: selected %x from %x",
426 RandomSelection_chosen_float, piecemask) );
427 return RandomSelection_chosen_float;
430 // Block/complete a 3 i na row
431 int ttt_ai_block3 ( int piecemask, int piecemask_free )
435 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_A3);
436 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_B3);
437 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_C1,TTT_AI_POSFLAG_C2,TTT_AI_POSFLAG_C3);
438 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_C1);
439 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C2);
440 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B3,TTT_AI_POSFLAG_C3);
441 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C3);
442 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C1);
443 LOG_TRACE(sprintf("TTT AI: possible 3 in a rows in %x: %x (%x)",piecemask,r, r&piecemask_free));
445 return ttt_ai_random(r);
449 // 1) tries to win the game if possible
450 // 2) tries to block the opponent if they have 2 in a row
451 // 3) places a piece randomly
452 string ttt_ai_choose_simple(int piecemask_self, int piecemask_opponent, int piecemask_free )
456 LOG_TRACE("TTT AI: checking winning move");
457 if (( move = ttt_ai_block3(piecemask_self,piecemask_free) ))
458 return ttt_ai_piece_flag2pos(move); // place winning move
460 LOG_TRACE("TTT AI: checking opponent's winning move");
461 if (( move = ttt_ai_block3(piecemask_opponent,piecemask_free) ))
462 return ttt_ai_piece_flag2pos(move); // block opponent
464 LOG_TRACE("TTT AI: random move");
465 return ttt_ai_piece_flag2pos(ttt_ai_random(piecemask_free));
468 // AI move (if it's AI's turn)
469 void ttt_aimove(entity minigame)
471 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame.ttt_ai) )
473 entity aiplayer = NULL;
474 while ( ( aiplayer = findentity(aiplayer,owner,minigame) ) )
475 if ( aiplayer.classname == "minigame_player" && !aiplayer.minigame_playerslot )
479 * Build bit masks for the board pieces
489 int piecemask_self = 0;
490 int piecemask_opponent = 0;
491 int piecemask_free = 0;
494 for ( int i = 0; i < 3; i++ )
496 for ( int j = 0; j < 3; j++ )
498 pos = minigame_tile_buildname(i,j);
499 entity piece = ttt_find_piece(minigame,pos);
502 if ( piece.team == aiplayer.team )
503 piecemask_self |= pieceflag;
505 piecemask_opponent |= pieceflag;
508 piecemask_free |= pieceflag;
513 // TODO multiple AI difficulties
514 LOG_TRACE(sprintf("TTT AI: self: %x opponent: %x free: %x",
515 piecemask_self, piecemask_opponent, piecemask_free));
516 pos = ttt_ai_choose_simple(piecemask_self, piecemask_opponent, piecemask_free);
517 LOG_TRACE("TTT AI: chosen move: ", pos);
519 LOG_TRACE("Tic Tac Toe AI has derped!");
521 ttt_move(minigame,aiplayer,pos);
523 minigame.message = ttt_turn_to_string(minigame.minigame_flags);
526 // Make the correct move
527 void ttt_make_move(entity minigame)
529 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
531 if ( minigame.ttt_ai )
533 ttt_move(minigame, minigame_self, ttt_curr_pos );
534 ttt_aimove(minigame);
537 minigame_cmd("move ",ttt_curr_pos);
541 void ttt_set_curr_pos(string s)
544 strunzone(ttt_curr_pos);
550 // Required function, handle client events
551 int ttt_client_event(entity minigame, string event, ...)
557 ttt_set_curr_pos("");
558 minigame.message = ttt_turn_to_string(minigame.minigame_flags);
563 if((minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team)
565 switch ( ...(0,int) )
568 case K_KP_RIGHTARROW:
569 if ( ! ttt_curr_pos )
570 ttt_set_curr_pos("a3");
572 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,1,0,TTT_LET_CNT,TTT_NUM_CNT));
576 if ( ! ttt_curr_pos )
577 ttt_set_curr_pos("c3");
579 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,-1,0,TTT_LET_CNT,TTT_NUM_CNT));
583 if ( ! ttt_curr_pos )
584 ttt_set_curr_pos("a1");
586 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,1,TTT_LET_CNT,TTT_NUM_CNT));
590 if ( ! ttt_curr_pos )
591 ttt_set_curr_pos("a3");
593 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,-1,TTT_LET_CNT,TTT_NUM_CNT));
598 ttt_make_move(minigame);
605 case "mouse_pressed":
607 if(...(0,int) == K_MOUSE1)
609 ttt_make_move(minigame);
617 vector mouse_pos = minigame_hud_normalize(mousepos,ttt_boardpos,ttt_boardsize);
618 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
619 ttt_set_curr_pos(minigame_tile_name(mouse_pos,TTT_LET_CNT,TTT_NUM_CNT));
620 if ( ! ttt_valid_tile(ttt_curr_pos) )
621 ttt_set_curr_pos("");
625 case "network_receive":
627 entity sent = ...(0,entity);
629 if ( sent.classname == "minigame" )
631 if ( sf & MINIG_SF_UPDATE )
633 sent.message = ttt_turn_to_string(sent.minigame_flags);
634 if ( sent.minigame_flags & minigame_self.team )
638 if ( (sf & TTT_SF_SINGLEPLAYER) )
641 bool spawnai = ai && !sent.ttt_ai;
646 entity aiplayer = new(minigame_player);
647 aiplayer.owner = minigame;
649 aiplayer.minigame_playerslot = 0;
650 aiplayer.minigame_autoclean = 1;
651 ttt_aimove(minigame);
656 else if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
658 sent.minigame_flags = ReadByte();
665 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
666 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Single Player"),"singleplayer");
671 if(...(0,string) == "next")
673 if ( minigame.ttt_ai )
675 ttt_next_match(minigame,minigame_self);
676 ttt_aimove(minigame);
679 minigame_cmd("next");
681 else if ( ...(0,string) == "singleplayer" && !minigame.ttt_ai )
683 if ( minigame_count_players(minigame) == 1 )
684 minigame_cmd("singleplayer");