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 .int ttt_npieces; // (minigame) number of pieces on the board (simplifies checking a draw)
16 .int ttt_nexteam; // (minigame) next team (used to change the starting team on following matches)
17 .int ttt_ai; // (minigame) when non-zero, singleplayer vs AI
19 // find tic tac toe piece given its tile name
20 entity ttt_find_piece(entity minig, string tile)
23 while ( ( e = findentity(e,owner,minig) ) )
24 if ( e.classname == "minigame_board_piece" && e.netname == tile )
29 // Checks if the given piece completes a row
30 bool ttt_winning_piece(entity piece)
32 int number = minigame_tile_number(piece.netname);
33 int letter = minigame_tile_letter(piece.netname);
35 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,number)).team == piece.team )
36 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,number)).team == piece.team )
37 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,number)).team == piece.team )
40 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,0)).team == piece.team )
41 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,1)).team == piece.team )
42 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(letter,2)).team == piece.team )
45 if ( number == letter )
46 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,0)).team == piece.team )
47 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(1,1)).team == piece.team )
48 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(2,2)).team == piece.team )
51 if ( number == 2-letter )
52 if ( ttt_find_piece(piece.owner,minigame_tile_buildname(0,2)).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,0)).team == piece.team )
60 // check if the tile name is valid (3x3 grid)
61 bool ttt_valid_tile(string tile)
65 int number = minigame_tile_number(tile);
66 int letter = minigame_tile_letter(tile);
67 return 0 <= number && number < 3 && 0 <= letter && letter < 3;
71 void ttt_move(entity minigame, entity player, string pos )
73 if ( minigame.minigame_flags & TTT_TURN_PLACE )
74 if ( pos && player.team == (minigame.minigame_flags & TTT_TURN_TEAM) )
76 if ( ttt_valid_tile(pos) )
77 if ( !ttt_find_piece(minigame,pos) )
79 entity piece = msle_spawn(minigame,"minigame_board_piece");
80 piece.team = player.team;
81 piece.netname = strzone(pos);
82 minigame_server_sendflags(piece,MINIG_SF_ALL);
83 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
84 minigame.ttt_npieces++;
85 minigame.ttt_nexteam = minigame_next_team(player.team,2);
86 if ( ttt_winning_piece(piece) )
88 player.minigame_flags++;
89 minigame_server_sendflags(player, TTT_SF_PLAYERSCORE);
90 minigame.minigame_flags = TTT_TURN_WIN | player.team;
92 else if ( minigame.ttt_npieces >= 9 )
93 minigame.minigame_flags = TTT_TURN_DRAW;
95 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
100 // request a new match
101 void ttt_next_match(entity minigame, entity player)
104 // on multiplayer matches, wait for both players to agree
105 if ( minigame.minigame_flags & (TTT_TURN_WIN|TTT_TURN_DRAW) )
107 minigame.minigame_flags = TTT_TURN_NEXT | player.team;
108 minigame.SendFlags |= MINIG_SF_UPDATE;
110 else if ( (minigame.minigame_flags & TTT_TURN_NEXT) &&
111 !( minigame.minigame_flags & player.team ) )
114 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
115 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
116 minigame.ttt_npieces = 0;
118 while ( ( e = findentity(e,owner,minigame) ) )
119 if ( e.classname == "minigame_board_piece" )
127 // required function, handle server side events
128 int ttt_server_event(entity minigame, string event, ...)
134 minigame.minigame_flags = (TTT_TURN_PLACE | TTT_TURN_TEAM1);
140 while( (e = findentity(e, owner, minigame)) )
141 if(e.classname == "minigame_board_piece")
143 if(e.netname) { strunzone(e.netname); }
150 int pl_num = minigame_count_players(minigame);
152 // Don't allow joining a single player match
153 if ( (minigame.ttt_ai) && pl_num > 0 )
156 // Don't allow more than 2 players
157 if(pl_num >= 2) { return false; }
159 // Get the right team
160 if(minigame.minigame_players)
161 return minigame_next_team(minigame.minigame_players.team, 2);
171 ttt_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
174 ttt_next_match(minigame,...(0,entity));
177 if ( minigame_count_players(minigame) == 1 )
179 minigame.ttt_ai = minigame_next_team(minigame.minigame_players.team, 2);
180 minigame.SendFlags = TTT_SF_SINGLEPLAYER;
189 entity sent = ...(0,entity);
191 if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
193 WriteByte(MSG_ENTITY,sent.minigame_flags);
195 else if ( sent.classname == "minigame" && (sf & TTT_SF_SINGLEPLAYER) )
197 WriteByte(MSG_ENTITY,sent.ttt_ai);
209 string ttt_curr_pos; // identifier of the tile under the mouse
210 vector ttt_boardpos; // HUD board position
211 vector ttt_boardsize;// HUD board size
212 .int ttt_checkwin; // Used to optimize checks to display a win
214 // Required function, draw the game board
215 void ttt_hud_board(vector pos, vector mySize)
217 minigame_hud_fitsqare(pos, mySize);
219 ttt_boardsize = mySize;
221 minigame_hud_simpleboard(pos,mySize,minigame_texture("ttt/board"));
223 vector tile_size = minigame_hud_denormalize_size('1 1 0'/3,pos,mySize);
226 if ( (active_minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team )
227 if ( ttt_valid_tile(ttt_curr_pos) )
229 tile_pos = minigame_tile_pos(ttt_curr_pos,3,3);
230 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
231 minigame_drawpic_centered( tile_pos,
232 minigame_texture(strcat("ttt/piece",ftos(minigame_self.team))),
233 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
237 FOREACH_MINIGAME_ENTITY(e)
239 if ( e.classname == "minigame_board_piece" )
241 tile_pos = minigame_tile_pos(e.netname,3,3);
242 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
244 if ( active_minigame.minigame_flags & TTT_TURN_WIN )
245 if ( !e.ttt_checkwin )
246 e.ttt_checkwin = ttt_winning_piece(e) ? 1 : -1;
248 float icon_color = 1;
249 if ( e.ttt_checkwin == -1 )
251 else if ( e.ttt_checkwin == 1 )
254 minigame_drawpic_centered( tile_pos, minigame_texture("ttt/winglow"),
255 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE );
258 minigame_drawpic_centered( tile_pos,
259 minigame_texture(strcat("ttt/piece",ftos(e.team))),
260 tile_size, '1 1 1'*icon_color, panel_fg_alpha, DRAWFLAG_NORMAL );
266 // Required function, draw the game status panel
267 void ttt_hud_status(vector pos, vector mySize)
271 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
272 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
277 vector player_fontsize = hud_fontsize * 1.75;
278 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
281 vector tile_size = '48 48 0';
284 FOREACH_MINIGAME_ENTITY(e)
286 if ( e.classname == "minigame_player" )
290 mypos_y += player_fontsize_y + ts_y;
291 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
292 (e.minigame_playerslot ? GetPlayerName(e.minigame_playerslot-1) : _("AI")),
293 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
295 mypos_y += player_fontsize_y;
297 minigame_texture(strcat("ttt/piece",ftos(e.team))),
298 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
300 mypos_x += tile_size_x;
302 drawstring(mypos,ftos(e.minigame_flags),tile_size,
303 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
308 // Turn a set of flags into a help message
309 string ttt_turn_to_string(int turnflags)
311 if ( turnflags & TTT_TURN_DRAW )
314 if ( turnflags & TTT_TURN_WIN )
316 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
317 return _("You lost the game!\nSelect \"^1Next Match^7\" on the menu for a rematch!");
318 return _("You win!\nSelect \"^1Next Match^7\" on the menu to start a new match!");
321 if ( turnflags & TTT_TURN_NEXT )
323 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
324 return _("Select \"^1Next Match^7\" on the menu to start a new match!");
325 return _("Wait for your opponent to confirm the rematch");
328 if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
329 return _("Wait for your opponent to make their move");
331 if ( turnflags & TTT_TURN_PLACE )
332 return _("Click on the game board to place your piece");
337 const int TTT_AI_POSFLAG_A1 = 0x0001;
338 const int TTT_AI_POSFLAG_A2 = 0x0002;
339 const int TTT_AI_POSFLAG_A3 = 0x0004;
340 const int TTT_AI_POSFLAG_B1 = 0x0008;
341 const int TTT_AI_POSFLAG_B2 = 0x0010;
342 const int TTT_AI_POSFLAG_B3 = 0x0020;
343 const int TTT_AI_POSFLAG_C1 = 0x0040;
344 const int TTT_AI_POSFLAG_C2 = 0x0080;
345 const int TTT_AI_POSFLAG_C3 = 0x0100;
347 // convert a flag to a position
348 string ttt_ai_piece_flag2pos(int pieceflag)
352 case TTT_AI_POSFLAG_A1:
354 case TTT_AI_POSFLAG_A2:
356 case TTT_AI_POSFLAG_A3:
359 case TTT_AI_POSFLAG_B1:
361 case TTT_AI_POSFLAG_B2:
363 case TTT_AI_POSFLAG_B3:
366 case TTT_AI_POSFLAG_C1:
368 case TTT_AI_POSFLAG_C2:
370 case TTT_AI_POSFLAG_C3:
378 bool ttt_ai_checkmask(int piecemask, int checkflags)
380 return checkflags && (piecemask & checkflags) == checkflags;
383 // get the third flag if the mask matches two of them
384 int ttt_ai_1of3(int piecemask, int flag1, int flag2, int flag3)
386 if ( ttt_ai_checkmask(piecemask,flag1|flag2|flag3) )
389 if ( ttt_ai_checkmask(piecemask,flag1|flag2) )
392 if ( ttt_ai_checkmask(piecemask,flag3|flag2) )
395 if ( ttt_ai_checkmask(piecemask,flag3|flag1) )
401 // Select a random flag in the mask
402 int ttt_ai_random(int piecemask)
409 RandomSelection_Init();
411 for ( int i = 0; i < 9; i++ )
414 RandomSelection_Add(world, f, string_null, 1, 1);
418 dprint(sprintf("TTT AI: selected %x from %x\n",
419 RandomSelection_chosen_float, piecemask) );
420 return RandomSelection_chosen_float;
423 // Block/complete a 3 i na row
424 int ttt_ai_block3 ( int piecemask, int piecemask_free )
428 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_A3);
429 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_B3);
430 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_C1,TTT_AI_POSFLAG_C2,TTT_AI_POSFLAG_C3);
431 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_C1);
432 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C2);
433 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B3,TTT_AI_POSFLAG_C3);
434 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C3);
435 r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C1);
436 dprint(sprintf("TTT AI: possible 3 in a rows in %x: %x (%x)\n",piecemask,r, r&piecemask_free));
438 return ttt_ai_random(r);
442 // 1) tries to win the game if possible
443 // 2) tries to block the opponent if they have 2 in a row
444 // 3) places a piece randomly
445 string ttt_ai_choose_simple(int piecemask_self, int piecemask_opponent, int piecemask_free )
449 dprint("TTT AI: checking winning move\n");
450 if (( move = ttt_ai_block3(piecemask_self,piecemask_free) ))
451 return ttt_ai_piece_flag2pos(move); // place winning move
453 dprint("TTT AI: checking opponent's winning move\n");
454 if (( move = ttt_ai_block3(piecemask_opponent,piecemask_free) ))
455 return ttt_ai_piece_flag2pos(move); // block opponent
457 dprint("TTT AI: random move\n");
458 return ttt_ai_piece_flag2pos(ttt_ai_random(piecemask_free));
461 // AI move (if it's AI's turn)
462 void ttt_aimove(entity minigame)
464 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame.ttt_ai) )
466 entity aiplayer = world;
467 while ( ( aiplayer = findentity(aiplayer,owner,minigame) ) )
468 if ( aiplayer.classname == "minigame_player" && !aiplayer.minigame_playerslot )
472 * Build bit masks for the board pieces
482 int piecemask_self = 0;
483 int piecemask_opponent = 0;
484 int piecemask_free = 0;
487 for ( int i = 0; i < 3; i++ )
489 for ( int j = 0; j < 3; j++ )
491 pos = minigame_tile_buildname(i,j);
492 entity piece = ttt_find_piece(minigame,pos);
495 if ( piece.team == aiplayer.team )
496 piecemask_self |= pieceflag;
498 piecemask_opponent |= pieceflag;
501 piecemask_free |= pieceflag;
506 // TODO multiple AI difficulties
507 dprint(sprintf("TTT AI: self: %x opponent: %x free: %x\n",
508 piecemask_self, piecemask_opponent, piecemask_free));
509 pos = ttt_ai_choose_simple(piecemask_self, piecemask_opponent, piecemask_free);
510 dprint("TTT AI: chosen move: ",pos,"\n\n");
512 dprint("Tic Tac Toe AI has derped!\n");
514 ttt_move(minigame,aiplayer,pos);
516 minigame.message = ttt_turn_to_string(minigame.minigame_flags);
519 // Make the correct move
520 void ttt_make_move(entity minigame)
522 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
524 if ( minigame.ttt_ai )
526 ttt_move(minigame, minigame_self, ttt_curr_pos );
527 ttt_aimove(minigame);
530 minigame_cmd("move ",ttt_curr_pos);
534 void ttt_set_curr_pos(string s)
537 strunzone(ttt_curr_pos);
543 // Required function, handle client events
544 int ttt_client_event(entity minigame, string event, ...)
550 ttt_set_curr_pos("");
551 minigame.message = ttt_turn_to_string(minigame.minigame_flags);
556 if((minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team)
558 switch ( ...(0,int) )
561 case K_KP_RIGHTARROW:
562 if ( ! ttt_curr_pos )
563 ttt_set_curr_pos("a3");
565 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,1,0,3,3));
569 if ( ! ttt_curr_pos )
570 ttt_set_curr_pos("c3");
572 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,-1,0,3,3));
576 if ( ! ttt_curr_pos )
577 ttt_set_curr_pos("a1");
579 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,1,3,3));
583 if ( ! ttt_curr_pos )
584 ttt_set_curr_pos("a3");
586 ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,-1,3,3));
591 ttt_make_move(minigame);
598 case "mouse_pressed":
600 if(...(0,int) == K_MOUSE1)
602 ttt_make_move(minigame);
610 vector mouse_pos = minigame_hud_normalize(mousepos,ttt_boardpos,ttt_boardsize);
611 if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
612 ttt_set_curr_pos(minigame_tile_name(mouse_pos,3,3));
613 if ( ! ttt_valid_tile(ttt_curr_pos) )
614 ttt_set_curr_pos("");
618 case "network_receive":
620 entity sent = ...(0,entity);
622 if ( sent.classname == "minigame" )
624 if ( sf & MINIG_SF_UPDATE )
626 sent.message = ttt_turn_to_string(sent.minigame_flags);
627 if ( sent.minigame_flags & minigame_self.team )
631 if ( (sf & TTT_SF_SINGLEPLAYER) )
634 bool spawnai = ai && !sent.ttt_ai;
639 entity aiplayer = spawn();
640 aiplayer.classname = "minigame_player";
641 aiplayer.owner = minigame;
643 aiplayer.minigame_playerslot = 0;
644 aiplayer.minigame_autoclean = 1;
645 ttt_aimove(minigame);
650 else if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
652 sent.minigame_flags = ReadByte();
659 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
660 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Single Player"),"singleplayer");
665 if(...(0,string) == "next")
667 if ( minigame.ttt_ai )
669 ttt_next_match(minigame,minigame_self);
670 ttt_aimove(minigame);
673 minigame_cmd("next");
675 else if ( ...(0,string) == "singleplayer" && !minigame.ttt_ai )
677 if ( minigame_count_players(minigame) == 1 )
678 minigame_cmd("singleplayer");