1 REGISTER_MINIGAME(ttt, "Tic Tac Toe");
3 const int TTT_TURN_PLACE = 0x0100; // player has to place a piece on the board
4 const int TTT_TURN_WIN = 0x0200; // player has won
5 const int TTT_TURN_DRAW = 0x0400; // no moves are possible
6 const int TTT_TURN_NEXT = 0x0800; // a player wants to start a new match
7 const int TTT_TURN_TYPE = 0x0f00; // turn type mask
9 const int TTT_TURN_TEAM1 = 0x0001;
10 const int TTT_TURN_TEAM2 = 0x0002;
11 const int TTT_TURN_TEAM = 0x000f; // turn team mask
14 const int TTT_SF_PLAYERSCORE = MINIG_SF_CUSTOM; // send minigame_player scores (won matches)
15 const int TTT_SF_SINGLEPLAYER = MINIG_SF_CUSTOM<<1;// send minigame.ttt_ai
17 const int TTT_LET_CNT = 3;
18 const int TTT_NUM_CNT = 3;
19 const int TTT_TILE_SIZE = 3;
21 .int ttt_npieces; // (minigame) number of pieces on the board (simplifies checking a draw)
22 .int ttt_nexteam; // (minigame) next team (used to change the starting team on following matches)
23 .int ttt_ai; // (minigame) when non-zero, singleplayer vs AI
25 // find tic tac toe piece given its tile name
26 entity ttt_find_piece(entity minig, string tile)
29 while ( ( e = findentity(e,owner,minig) ) )
30 if ( e.classname == "minigame_board_piece" && e.netname == tile )
35 // Checks if the given piece completes a row
36 bool ttt_winning_piece(entity piece)
38 int number = minigame_tile_number(piece.netname);
39 int letter = minigame_tile_letter(piece.netname);
41 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,number)).team == piece.team )
42 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,number)).team == piece.team )
43 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,number)).team == piece.team )
46 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,0)).team == piece.team )
47 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,1)).team == piece.team )
48 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,2)).team == piece.team )
51 if ( number == letter )
52 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,0)).team == piece.team )
53 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,1)).team == piece.team )
54 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,2)).team == piece.team )
57 if ( number == 2-letter )
58 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,2)).team == piece.team )
59 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,1)).team == piece.team )
60 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,0)).team == piece.team )
66 // check if the tile name is valid (3x3 grid)
67 bool ttt_valid_tile(string tile)
71 int number = minigame_tile_number(tile);
72 int letter = minigame_tile_letter(tile);
73 return 0 <= number && number < TTT_NUM_CNT && 0 <= letter && letter < TTT_LET_CNT;
77 void ttt_move(entity minigame, entity player, string pos )
79 if ( minigame.minigame_flags & TTT_TURN_PLACE )
80 if ( pos && player.team == (minigame.minigame_flags & TTT_TURN_TEAM) )
82 if ( ttt_valid_tile(pos) )
83 if ( !ttt_find_piece(minigame,pos) )
85 entity piece = msle_spawn(minigame,"minigame_board_piece");
86 piece.team = player.team;
87 piece.netname = strzone(pos);
88 minigame_server_sendflags(piece,MINIG_SF_ALL);
89 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
90 minigame.ttt_npieces++;
91 minigame.ttt_nexteam = minigame_next_team(player.team,2);
92 if ( ttt_winning_piece(piece) )
94 player.minigame_flags++;
95 minigame_server_sendflags(player, TTT_SF_PLAYERSCORE);
96 minigame.minigame_flags = TTT_TURN_WIN | player.team;
98 else if ( minigame.ttt_npieces >= (TTT_LET_CNT * TTT_NUM_CNT) )
99 minigame.minigame_flags = TTT_TURN_DRAW;
101 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
106 // request a new match
107 void ttt_next_match(entity minigame, entity player)
110 // on multiplayer matches, wait for both players to agree
111 if ( minigame.minigame_flags & (TTT_TURN_WIN|TTT_TURN_DRAW) )
113 minigame.minigame_flags = TTT_TURN_NEXT | player.team;
114 minigame.SendFlags |= MINIG_SF_UPDATE;
116 else if ( (minigame.minigame_flags & TTT_TURN_NEXT) &&
117 !( minigame.minigame_flags & player.team ) )
120 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
121 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
122 minigame.ttt_npieces = 0;
124 while ( ( e = findentity(e,owner,minigame) ) )
125 if ( e.classname == "minigame_board_piece" )
133 // required function, handle server side events
134 int ttt_server_event(entity minigame, string event, ...)
140 minigame.minigame_flags = (TTT_TURN_PLACE | TTT_TURN_TEAM1);
146 while( (e = findentity(e, owner, minigame)) )
147 if(e.classname == "minigame_board_piece")
149 if(e.netname) { strunzone(e.netname); }
156 int pl_num = minigame_count_players(minigame);
158 // Don't allow joining a single player match
159 if ( (minigame.ttt_ai) && pl_num > 0 )
162 // Don't allow more than 2 players
163 if(pl_num >= 2) { return false; }
165 // Get the right team
166 if(minigame.minigame_players)
167 return minigame_next_team(minigame.minigame_players.team, 2);
177 ttt_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
180 ttt_next_match(minigame,...(0,entity));
183 if ( minigame_count_players(minigame) == 1 )
185 minigame.ttt_ai = minigame_next_team(minigame.minigame_players.team, 2);
186 minigame.SendFlags = TTT_SF_SINGLEPLAYER;
195 entity sent = ...(0,entity);
197 if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
199 WriteByte(MSG_ENTITY,sent.minigame_flags);
201 else if ( sent.classname == "minigame" && (sf & TTT_SF_SINGLEPLAYER) )
203 WriteByte(MSG_ENTITY,sent.ttt_ai);
215 string ttt_curr_pos; // identifier of the tile under the mouse
216 vector ttt_boardpos; // HUD board position
217 vector ttt_boardsize;// HUD board size
218 .int ttt_checkwin; // Used to optimize checks to display a win
220 // Required function, draw the game board
221 void ttt_hud_board(vector pos, vector mySize)
223 minigame_hud_fitsqare(pos, mySize);
225 ttt_boardsize = mySize;
227 minigame_hud_simpleboard(pos,mySize,minigame_texture("ttt/board"));
229 vector tile_size = minigame_hud_denormalize_size('1 1 0'/TTT_TILE_SIZE,pos,mySize);
232 if ( (active_minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team )
233 if ( ttt_valid_tile(ttt_curr_pos) )
235 tile_pos = minigame_tile_pos(ttt_curr_pos,TTT_LET_CNT,TTT_NUM_CNT);
236 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
237 minigame_drawpic_centered( tile_pos,
238 minigame_texture(strcat("ttt/piece",ftos(minigame_self.team))),
239 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
243 FOREACH_MINIGAME_ENTITY(e)
245 if ( e.classname == "minigame_board_piece" )
247 tile_pos = minigame_tile_pos(e.netname,TTT_LET_CNT,TTT_NUM_CNT);
248 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
250 if ( active_minigame.minigame_flags & TTT_TURN_WIN )
251 if ( !e.ttt_checkwin )
252 e.ttt_checkwin = ttt_winning_piece(e) ? 1 : -1;
254 float icon_color = 1;
255 if ( e.ttt_checkwin == -1 )
257 else if ( e.ttt_checkwin == 1 )
260 minigame_drawpic_centered( tile_pos, minigame_texture("ttt/winglow"),
261 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE );
264 minigame_drawpic_centered( tile_pos,
265 minigame_texture(strcat("ttt/piece",ftos(e.team))),
266 tile_size, '1 1 1'*icon_color, panel_fg_alpha, DRAWFLAG_NORMAL );
272 // Required function, draw the game status panel
273 void ttt_hud_status(vector pos, vector mySize)
277 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
278 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
283 vector player_fontsize = hud_fontsize * 1.75;
284 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
287 vector tile_size = '48 48 0';
290 FOREACH_MINIGAME_ENTITY(e)
292 if ( e.classname == "minigame_player" )
296 mypos_y += player_fontsize_y + ts_y;
297 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
298 (e.minigame_playerslot ? GetPlayerName(e.minigame_playerslot-1) : _("AI")),
299 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
301 mypos_y += player_fontsize_y;
303 minigame_texture(strcat("ttt/piece",ftos(e.team))),
304 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
306 mypos_x += tile_size_x;
308 drawstring(mypos,ftos(e.minigame_flags),tile_size,
309 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
314 // Turn a set of flags into a help message
315 string ttt_turn_to_string(int turnflags)
317 if ( turnflags & TTT_TURN_DRAW )
320 if ( turnflags & TTT_TURN_WIN )
322 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
323 return _("You lost the game!\nSelect \"^1Next Match^7\" on the menu for a rematch!");
324 return _("You win!\nSelect \"^1Next Match^7\" on the menu to start a new match!");
327 if ( turnflags & TTT_TURN_NEXT )
329 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
330 return _("Select \"^1Next Match^7\" on the menu to start a new match!");
331 return _("Wait for your opponent to confirm the rematch");
334 if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
335 return _("Wait for your opponent to make their move");
337 if ( turnflags & TTT_TURN_PLACE )
338 return _("Click on the game board to place your piece");
343 const int TTT_AI_POSFLAG_A1 = 0x0001;
344 const int TTT_AI_POSFLAG_A2 = 0x0002;
345 const int TTT_AI_POSFLAG_A3 = 0x0004;
346 const int TTT_AI_POSFLAG_B1 = 0x0008;
347 const int TTT_AI_POSFLAG_B2 = 0x0010;
348 const int TTT_AI_POSFLAG_B3 = 0x0020;
349 const int TTT_AI_POSFLAG_C1 = 0x0040;
350 const int TTT_AI_POSFLAG_C2 = 0x0080;
351 const int TTT_AI_POSFLAG_C3 = 0x0100;
353 // convert a flag to a position
354 string ttt_ai_piece_flag2pos(int pieceflag)
358 case TTT_AI_POSFLAG_A1:
360 case TTT_AI_POSFLAG_A2:
362 case TTT_AI_POSFLAG_A3:
365 case TTT_AI_POSFLAG_B1:
367 case TTT_AI_POSFLAG_B2:
369 case TTT_AI_POSFLAG_B3:
372 case TTT_AI_POSFLAG_C1:
374 case TTT_AI_POSFLAG_C2:
376 case TTT_AI_POSFLAG_C3:
384 bool ttt_ai_checkmask(int piecemask, int checkflags)
386 return checkflags && (piecemask & checkflags) == checkflags;
389 // get the third flag if the mask matches two of them
390 int ttt_ai_1of3(int piecemask, int flag1, int flag2, int flag3)
392 if ( ttt_ai_checkmask(piecemask,flag1|flag2|flag3) )
395 if ( ttt_ai_checkmask(piecemask,flag1|flag2) )
398 if ( ttt_ai_checkmask(piecemask,flag3|flag2) )
401 if ( ttt_ai_checkmask(piecemask,flag3|flag1) )
407 // Select a random flag in the mask
408 int ttt_ai_random(int piecemask)
415 RandomSelection_Init();
417 for ( int i = 0; i < 9; i++ )
420 RandomSelection_Add(world, f, string_null, 1, 1);
424 LOG_TRACE(sprintf("TTT AI: selected %x from %x\n",
425 RandomSelection_chosen_float, piecemask) );
426 return RandomSelection_chosen_float;
429 // Block/complete a 3 i na row
430 int ttt_ai_block3 ( int piecemask, int piecemask_free )
434 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_A3);
435 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_B3);
436 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_C1,TTT_AI_POSFLAG_C2,TTT_AI_POSFLAG_C3);
437 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_C1);
438 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C2);
439 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B3,TTT_AI_POSFLAG_C3);
440 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C3);
441 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C1);
442 LOG_TRACE(sprintf("TTT AI: possible 3 in a rows in %x: %x (%x)\n",piecemask,r, r&piecemask_free));
444 return ttt_ai_random(r);
448 // 1) tries to win the game if possible
449 // 2) tries to block the opponent if they have 2 in a row
450 // 3) places a piece randomly
451 string ttt_ai_choose_simple(int piecemask_self, int piecemask_opponent, int piecemask_free )
455 LOG_TRACE("TTT AI: checking winning move\n");
456 if (( move = ttt_ai_block3(piecemask_self,piecemask_free) ))
457 return ttt_ai_piece_flag2pos(move); // place winning move
459 LOG_TRACE("TTT AI: checking opponent's winning move\n");
460 if (( move = ttt_ai_block3(piecemask_opponent,piecemask_free) ))
461 return ttt_ai_piece_flag2pos(move); // block opponent
463 LOG_TRACE("TTT AI: random move\n");
464 return ttt_ai_piece_flag2pos(ttt_ai_random(piecemask_free));
467 // AI move (if it's AI's turn)
468 void ttt_aimove(entity minigame)
470 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame.ttt_ai) )
472 entity aiplayer = world;
473 while ( ( aiplayer = findentity(aiplayer,owner,minigame) ) )
474 if ( aiplayer.classname == "minigame_player" && !aiplayer.minigame_playerslot )
478 * Build bit masks for the board pieces
488 int piecemask_self = 0;
489 int piecemask_opponent = 0;
490 int piecemask_free = 0;
493 for ( int i = 0; i < 3; i++ )
495 for ( int j = 0; j < 3; j++ )
497 pos = minigame_tile_buildname(i,j);
498 entity piece = ttt_find_piece(minigame,pos);
501 if ( piece.team == aiplayer.team )
502 piecemask_self |= pieceflag;
504 piecemask_opponent |= pieceflag;
507 piecemask_free |= pieceflag;
512 // TODO multiple AI difficulties
513 LOG_TRACE(sprintf("TTT AI: self: %x opponent: %x free: %x\n",
514 piecemask_self, piecemask_opponent, piecemask_free));
515 pos = ttt_ai_choose_simple(piecemask_self, piecemask_opponent, piecemask_free);
516 LOG_TRACE("TTT AI: chosen move: ",pos,"\n\n");
518 LOG_TRACE("Tic Tac Toe AI has derped!\n");
520 ttt_move(minigame,aiplayer,pos);
522 minigame.message = ttt_turn_to_string(minigame.minigame_flags);
525 // Make the correct move
526 void ttt_make_move(entity minigame)
528 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
530 if ( minigame.ttt_ai )
532 ttt_move(minigame, minigame_self, ttt_curr_pos );
533 ttt_aimove(minigame);
536 minigame_cmd("move ",ttt_curr_pos);
540 void ttt_set_curr_pos(string s)
543 strunzone(ttt_curr_pos);
549 // Required function, handle client events
550 int ttt_client_event(entity minigame, string event, ...)
556 ttt_set_curr_pos("");
557 minigame.message = ttt_turn_to_string(minigame.minigame_flags);
562 if((minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team)
564 switch ( ...(0,int) )
567 case K_KP_RIGHTARROW:
568 if ( ! ttt_curr_pos )
569 ttt_set_curr_pos("a3");
571 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,1,0,TTT_LET_CNT,TTT_NUM_CNT));
575 if ( ! ttt_curr_pos )
576 ttt_set_curr_pos("c3");
578 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,-1,0,TTT_LET_CNT,TTT_NUM_CNT));
582 if ( ! ttt_curr_pos )
583 ttt_set_curr_pos("a1");
585 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,1,TTT_LET_CNT,TTT_NUM_CNT));
589 if ( ! ttt_curr_pos )
590 ttt_set_curr_pos("a3");
592 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,-1,TTT_LET_CNT,TTT_NUM_CNT));
597 ttt_make_move(minigame);
604 case "mouse_pressed":
606 if(...(0,int) == K_MOUSE1)
608 ttt_make_move(minigame);
616 vector mouse_pos = minigame_hud_normalize(mousepos,ttt_boardpos,ttt_boardsize);
617 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
618 ttt_set_curr_pos(minigame_tile_name(mouse_pos,TTT_LET_CNT,TTT_NUM_CNT));
619 if ( ! ttt_valid_tile(ttt_curr_pos) )
620 ttt_set_curr_pos("");
624 case "network_receive":
626 entity sent = ...(0,entity);
628 if ( sent.classname == "minigame" )
630 if ( sf & MINIG_SF_UPDATE )
632 sent.message = ttt_turn_to_string(sent.minigame_flags);
633 if ( sent.minigame_flags & minigame_self.team )
637 if ( (sf & TTT_SF_SINGLEPLAYER) )
640 bool spawnai = ai && !sent.ttt_ai;
645 entity aiplayer = new(minigame_player);
646 aiplayer.owner = minigame;
648 aiplayer.minigame_playerslot = 0;
649 aiplayer.minigame_autoclean = 1;
650 ttt_aimove(minigame);
655 else if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
657 sent.minigame_flags = ReadByte();
664 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
665 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Single Player"),"singleplayer");
670 if(...(0,string) == "next")
672 if ( minigame.ttt_ai )
674 ttt_next_match(minigame,minigame_self);
675 ttt_aimove(minigame);
678 minigame_cmd("next");
680 else if ( ...(0,string) == "singleplayer" && !minigame.ttt_ai )
682 if ( minigame_count_players(minigame) == 1 )
683 minigame_cmd("singleplayer");