1 const int TTT_TURN_PLACE = 0x0100; // player has to place a piece on the board
2 const int TTT_TURN_WIN = 0x0200; // player has won
3 const int TTT_TURN_DRAW = 0x0400; // no moves are possible
4 const int TTT_TURN_NEXT = 0x0800; // a player wants to start a new match
5 const int TTT_TURN_TYPE = 0x0f00; // turn type mask
7 const int TTT_TURN_TEAM1 = 0x0001;
8 const int TTT_TURN_TEAM2 = 0x0002;
9 const int TTT_TURN_TEAM = 0x000f; // turn team mask
12 const int TTT_SF_PLAYERSCORE = MINIG_SF_CUSTOM; // send minigame_player scores (won matches)
13 const int TTT_SF_SINGLEPLAYER = MINIG_SF_CUSTOM<<1;// send minigame.ttt_ai
15 const int TTT_LET_CNT = 3;
16 const int TTT_NUM_CNT = 3;
17 const int TTT_TILE_SIZE = 3;
19 .int ttt_npieces; // (minigame) number of pieces on the board (simplifies checking a draw)
20 .int ttt_nexteam; // (minigame) next team (used to change the starting team on following matches)
21 .int ttt_ai; // (minigame) when non-zero, singleplayer vs AI
23 // find tic tac toe piece given its tile name
24 entity ttt_find_piece(entity minig, string tile)
27 while ( ( e = findentity(e,owner,minig) ) )
28 if ( e.classname == "minigame_board_piece" && e.netname == tile )
33 // Checks if the given piece completes a row
34 bool ttt_winning_piece(entity piece)
36 int number = minigame_tile_number(piece.netname);
37 int letter = minigame_tile_letter(piece.netname);
39 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,number)).team == piece.team )
40 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,number)).team == piece.team )
41 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,number)).team == piece.team )
44 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,0)).team == piece.team )
45 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,1)).team == piece.team )
46 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,2)).team == piece.team )
49 if ( number == letter )
50 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,0)).team == piece.team )
51 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,1)).team == piece.team )
52 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,2)).team == piece.team )
55 if ( number == 2-letter )
56 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,2)).team == piece.team )
57 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,1)).team == piece.team )
58 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,0)).team == piece.team )
64 // check if the tile name is valid (3x3 grid)
65 bool ttt_valid_tile(string tile)
69 int number = minigame_tile_number(tile);
70 int letter = minigame_tile_letter(tile);
71 return 0 <= number && number < TTT_NUM_CNT && 0 <= letter && letter < TTT_LET_CNT;
75 void ttt_move(entity minigame, entity player, string pos )
77 if ( minigame.minigame_flags & TTT_TURN_PLACE )
78 if ( pos && player.team == (minigame.minigame_flags & TTT_TURN_TEAM) )
80 if ( ttt_valid_tile(pos) )
81 if ( !ttt_find_piece(minigame,pos) )
83 entity piece = msle_spawn(minigame,"minigame_board_piece");
84 piece.team = player.team;
85 piece.netname = strzone(pos);
86 minigame_server_sendflags(piece,MINIG_SF_ALL);
87 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
88 minigame.ttt_npieces++;
89 minigame.ttt_nexteam = minigame_next_team(player.team,2);
90 if ( ttt_winning_piece(piece) )
92 player.minigame_flags++;
93 minigame_server_sendflags(player, TTT_SF_PLAYERSCORE);
94 minigame.minigame_flags = TTT_TURN_WIN | player.team;
96 else if ( minigame.ttt_npieces >= (TTT_LET_CNT * TTT_NUM_CNT) )
97 minigame.minigame_flags = TTT_TURN_DRAW;
99 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
104 // request a new match
105 void ttt_next_match(entity minigame, entity player)
108 // on multiplayer matches, wait for both players to agree
109 if ( minigame.minigame_flags & (TTT_TURN_WIN|TTT_TURN_DRAW) )
111 minigame.minigame_flags = TTT_TURN_NEXT | player.team;
112 minigame.SendFlags |= MINIG_SF_UPDATE;
114 else if ( (minigame.minigame_flags & TTT_TURN_NEXT) &&
115 !( minigame.minigame_flags & player.team ) )
118 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
119 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
120 minigame.ttt_npieces = 0;
122 while ( ( e = findentity(e,owner,minigame) ) )
123 if ( e.classname == "minigame_board_piece" )
131 // required function, handle server side events
132 int ttt_server_event(entity minigame, string event, ...)
138 minigame.minigame_flags = (TTT_TURN_PLACE | TTT_TURN_TEAM1);
144 while( (e = findentity(e, owner, minigame)) )
145 if(e.classname == "minigame_board_piece")
147 if(e.netname) { strunzone(e.netname); }
154 int pl_num = minigame_count_players(minigame);
156 // Don't allow joining a single player match
157 if ( (minigame.ttt_ai) && pl_num > 0 )
160 // Don't allow more than 2 players
161 if(pl_num >= 2) { return false; }
163 // Get the right team
164 if(minigame.minigame_players)
165 return minigame_next_team(minigame.minigame_players.team, 2);
175 ttt_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
178 ttt_next_match(minigame,...(0,entity));
181 if ( minigame_count_players(minigame) == 1 )
183 minigame.ttt_ai = minigame_next_team(minigame.minigame_players.team, 2);
184 minigame.SendFlags = TTT_SF_SINGLEPLAYER;
193 entity sent = ...(0,entity);
195 if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
197 WriteByte(MSG_ENTITY,sent.minigame_flags);
199 else if ( sent.classname == "minigame" && (sf & TTT_SF_SINGLEPLAYER) )
201 WriteByte(MSG_ENTITY,sent.ttt_ai);
213 string ttt_curr_pos; // identifier of the tile under the mouse
214 vector ttt_boardpos; // HUD board position
215 vector ttt_boardsize;// HUD board size
216 .int ttt_checkwin; // Used to optimize checks to display a win
218 // Required function, draw the game board
219 void ttt_hud_board(vector pos, vector mySize)
221 minigame_hud_fitsqare(pos, mySize);
223 ttt_boardsize = mySize;
225 minigame_hud_simpleboard(pos,mySize,minigame_texture("ttt/board"));
227 vector tile_size = minigame_hud_denormalize_size('1 1 0'/TTT_TILE_SIZE,pos,mySize);
230 if ( (active_minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team )
231 if ( ttt_valid_tile(ttt_curr_pos) )
233 tile_pos = minigame_tile_pos(ttt_curr_pos,TTT_LET_CNT,TTT_NUM_CNT);
234 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
235 minigame_drawpic_centered( tile_pos,
236 minigame_texture(strcat("ttt/piece",ftos(minigame_self.team))),
237 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
241 FOREACH_MINIGAME_ENTITY(e)
243 if ( e.classname == "minigame_board_piece" )
245 tile_pos = minigame_tile_pos(e.netname,TTT_LET_CNT,TTT_NUM_CNT);
246 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
248 if ( active_minigame.minigame_flags & TTT_TURN_WIN )
249 if ( !e.ttt_checkwin )
250 e.ttt_checkwin = ttt_winning_piece(e) ? 1 : -1;
252 float icon_color = 1;
253 if ( e.ttt_checkwin == -1 )
255 else if ( e.ttt_checkwin == 1 )
258 minigame_drawpic_centered( tile_pos, minigame_texture("ttt/winglow"),
259 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE );
262 minigame_drawpic_centered( tile_pos,
263 minigame_texture(strcat("ttt/piece",ftos(e.team))),
264 tile_size, '1 1 1'*icon_color, panel_fg_alpha, DRAWFLAG_NORMAL );
270 // Required function, draw the game status panel
271 void ttt_hud_status(vector pos, vector mySize)
275 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
276 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
281 vector player_fontsize = hud_fontsize * 1.75;
282 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
285 vector tile_size = '48 48 0';
288 FOREACH_MINIGAME_ENTITY(e)
290 if ( e.classname == "minigame_player" )
294 mypos_y += player_fontsize_y + ts_y;
295 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
296 (e.minigame_playerslot ? GetPlayerName(e.minigame_playerslot-1) : _("AI")),
297 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
299 mypos_y += player_fontsize_y;
301 minigame_texture(strcat("ttt/piece",ftos(e.team))),
302 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
304 mypos_x += tile_size_x;
306 drawstring(mypos,ftos(e.minigame_flags),tile_size,
307 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
312 // Turn a set of flags into a help message
313 string ttt_turn_to_string(int turnflags)
315 if ( turnflags & TTT_TURN_DRAW )
318 if ( turnflags & TTT_TURN_WIN )
320 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
321 return _("You lost the game!\nSelect \"^1Next Match^7\" on the menu for a rematch!");
322 return _("You win!\nSelect \"^1Next Match^7\" on the menu to start a new match!");
325 if ( turnflags & TTT_TURN_NEXT )
327 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
328 return _("Select \"^1Next Match^7\" on the menu to start a new match!");
329 return _("Wait for your opponent to confirm the rematch");
332 if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
333 return _("Wait for your opponent to make their move");
335 if ( turnflags & TTT_TURN_PLACE )
336 return _("Click on the game board to place your piece");
341 const int TTT_AI_POSFLAG_A1 = 0x0001;
342 const int TTT_AI_POSFLAG_A2 = 0x0002;
343 const int TTT_AI_POSFLAG_A3 = 0x0004;
344 const int TTT_AI_POSFLAG_B1 = 0x0008;
345 const int TTT_AI_POSFLAG_B2 = 0x0010;
346 const int TTT_AI_POSFLAG_B3 = 0x0020;
347 const int TTT_AI_POSFLAG_C1 = 0x0040;
348 const int TTT_AI_POSFLAG_C2 = 0x0080;
349 const int TTT_AI_POSFLAG_C3 = 0x0100;
351 // convert a flag to a position
352 string ttt_ai_piece_flag2pos(int pieceflag)
356 case TTT_AI_POSFLAG_A1:
358 case TTT_AI_POSFLAG_A2:
360 case TTT_AI_POSFLAG_A3:
363 case TTT_AI_POSFLAG_B1:
365 case TTT_AI_POSFLAG_B2:
367 case TTT_AI_POSFLAG_B3:
370 case TTT_AI_POSFLAG_C1:
372 case TTT_AI_POSFLAG_C2:
374 case TTT_AI_POSFLAG_C3:
382 bool ttt_ai_checkmask(int piecemask, int checkflags)
384 return checkflags && (piecemask & checkflags) == checkflags;
387 // get the third flag if the mask matches two of them
388 int ttt_ai_1of3(int piecemask, int flag1, int flag2, int flag3)
390 if ( ttt_ai_checkmask(piecemask,flag1|flag2|flag3) )
393 if ( ttt_ai_checkmask(piecemask,flag1|flag2) )
396 if ( ttt_ai_checkmask(piecemask,flag3|flag2) )
399 if ( ttt_ai_checkmask(piecemask,flag3|flag1) )
405 // Select a random flag in the mask
406 int ttt_ai_random(int piecemask)
413 RandomSelection_Init();
415 for ( int i = 0; i < 9; i++ )
418 RandomSelection_Add(world, f, string_null, 1, 1);
422 LOG_TRACE(sprintf("TTT AI: selected %x from %x\n",
423 RandomSelection_chosen_float, piecemask) );
424 return RandomSelection_chosen_float;
427 // Block/complete a 3 i na row
428 int ttt_ai_block3 ( int piecemask, int piecemask_free )
432 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_A3);
433 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_B3);
434 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_C1,TTT_AI_POSFLAG_C2,TTT_AI_POSFLAG_C3);
435 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_C1);
436 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C2);
437 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B3,TTT_AI_POSFLAG_C3);
438 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C3);
439 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C1);
440 LOG_TRACE(sprintf("TTT AI: possible 3 in a rows in %x: %x (%x)\n",piecemask,r, r&piecemask_free));
442 return ttt_ai_random(r);
446 // 1) tries to win the game if possible
447 // 2) tries to block the opponent if they have 2 in a row
448 // 3) places a piece randomly
449 string ttt_ai_choose_simple(int piecemask_self, int piecemask_opponent, int piecemask_free )
453 LOG_TRACE("TTT AI: checking winning move\n");
454 if (( move = ttt_ai_block3(piecemask_self,piecemask_free) ))
455 return ttt_ai_piece_flag2pos(move); // place winning move
457 LOG_TRACE("TTT AI: checking opponent's winning move\n");
458 if (( move = ttt_ai_block3(piecemask_opponent,piecemask_free) ))
459 return ttt_ai_piece_flag2pos(move); // block opponent
461 LOG_TRACE("TTT AI: random move\n");
462 return ttt_ai_piece_flag2pos(ttt_ai_random(piecemask_free));
465 // AI move (if it's AI's turn)
466 void ttt_aimove(entity minigame)
468 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame.ttt_ai) )
470 entity aiplayer = world;
471 while ( ( aiplayer = findentity(aiplayer,owner,minigame) ) )
472 if ( aiplayer.classname == "minigame_player" && !aiplayer.minigame_playerslot )
476 * Build bit masks for the board pieces
486 int piecemask_self = 0;
487 int piecemask_opponent = 0;
488 int piecemask_free = 0;
491 for ( int i = 0; i < 3; i++ )
493 for ( int j = 0; j < 3; j++ )
495 pos = minigame_tile_buildname(i,j);
496 entity piece = ttt_find_piece(minigame,pos);
499 if ( piece.team == aiplayer.team )
500 piecemask_self |= pieceflag;
502 piecemask_opponent |= pieceflag;
505 piecemask_free |= pieceflag;
510 // TODO multiple AI difficulties
511 LOG_TRACE(sprintf("TTT AI: self: %x opponent: %x free: %x\n",
512 piecemask_self, piecemask_opponent, piecemask_free));
513 pos = ttt_ai_choose_simple(piecemask_self, piecemask_opponent, piecemask_free);
514 LOG_TRACE("TTT AI: chosen move: ",pos,"\n\n");
516 LOG_TRACE("Tic Tac Toe AI has derped!\n");
518 ttt_move(minigame,aiplayer,pos);
520 minigame.message = ttt_turn_to_string(minigame.minigame_flags);
523 // Make the correct move
524 void ttt_make_move(entity minigame)
526 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
528 if ( minigame.ttt_ai )
530 ttt_move(minigame, minigame_self, ttt_curr_pos );
531 ttt_aimove(minigame);
534 minigame_cmd("move ",ttt_curr_pos);
538 void ttt_set_curr_pos(string s)
541 strunzone(ttt_curr_pos);
547 // Required function, handle client events
548 int ttt_client_event(entity minigame, string event, ...)
554 ttt_set_curr_pos("");
555 minigame.message = ttt_turn_to_string(minigame.minigame_flags);
560 if((minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team)
562 switch ( ...(0,int) )
565 case K_KP_RIGHTARROW:
566 if ( ! ttt_curr_pos )
567 ttt_set_curr_pos("a3");
569 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,1,0,TTT_LET_CNT,TTT_NUM_CNT));
573 if ( ! ttt_curr_pos )
574 ttt_set_curr_pos("c3");
576 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,-1,0,TTT_LET_CNT,TTT_NUM_CNT));
580 if ( ! ttt_curr_pos )
581 ttt_set_curr_pos("a1");
583 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,1,TTT_LET_CNT,TTT_NUM_CNT));
587 if ( ! ttt_curr_pos )
588 ttt_set_curr_pos("a3");
590 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,-1,TTT_LET_CNT,TTT_NUM_CNT));
595 ttt_make_move(minigame);
602 case "mouse_pressed":
604 if(...(0,int) == K_MOUSE1)
606 ttt_make_move(minigame);
614 vector mouse_pos = minigame_hud_normalize(mousepos,ttt_boardpos,ttt_boardsize);
615 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
616 ttt_set_curr_pos(minigame_tile_name(mouse_pos,TTT_LET_CNT,TTT_NUM_CNT));
617 if ( ! ttt_valid_tile(ttt_curr_pos) )
618 ttt_set_curr_pos("");
622 case "network_receive":
624 entity sent = ...(0,entity);
626 if ( sent.classname == "minigame" )
628 if ( sf & MINIG_SF_UPDATE )
630 sent.message = ttt_turn_to_string(sent.minigame_flags);
631 if ( sent.minigame_flags & minigame_self.team )
635 if ( (sf & TTT_SF_SINGLEPLAYER) )
638 bool spawnai = ai && !sent.ttt_ai;
643 entity aiplayer = spawn();
644 aiplayer.classname = "minigame_player";
645 aiplayer.owner = minigame;
647 aiplayer.minigame_playerslot = 0;
648 aiplayer.minigame_autoclean = 1;
649 ttt_aimove(minigame);
654 else if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
656 sent.minigame_flags = ReadByte();
663 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
664 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Single Player"),"singleplayer");
669 if(...(0,string) == "next")
671 if ( minigame.ttt_ai )
673 ttt_next_match(minigame,minigame_self);
674 ttt_aimove(minigame);
677 minigame_cmd("next");
679 else if ( ...(0,string) == "singleplayer" && !minigame.ttt_ai )
681 if ( minigame_count_players(minigame) == 1 )
682 minigame_cmd("singleplayer");