]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/minigames/minigame/ttt.qc
Merge branch 'master' into terencehill/bot_waypoints
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigame / ttt.qc
1 #include "ttt.qh"
2 REGISTER_MINIGAME(ttt, _("Tic Tac Toe"));
3
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
9
10 const int TTT_TURN_TEAM1 = 0x0001;
11 const int TTT_TURN_TEAM2 = 0x0002;
12 const int TTT_TURN_TEAM  = 0x000f; // turn team mask
13
14 // send flags
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
17
18 const int TTT_LET_CNT = 3;
19 const int TTT_NUM_CNT = 3;
20 const int TTT_TILE_SIZE = 3;
21
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
25
26 // find tic tac toe piece given its tile name
27 entity ttt_find_piece(entity minig, string tile)
28 {
29         entity e = NULL;
30         while ( ( e = findentity(e,owner,minig) ) )
31                 if ( e.classname == "minigame_board_piece" && e.netname == tile )
32                         return e;
33         return NULL;
34 }
35
36 // Checks if the given piece completes a row
37 bool ttt_winning_piece(entity piece)
38 {
39         int number = minigame_tile_number(piece.netname);
40         int letter = minigame_tile_letter(piece.netname);
41
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 )
45                 return true;
46
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 )
50                 return true;
51
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 )
56                 return true;
57
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 )
62                 return true;
63
64         return false;
65 }
66
67 // check if the tile name is valid (3x3 grid)
68 bool ttt_valid_tile(string tile)
69 {
70         if ( !tile )
71                 return 0;
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;
75 }
76
77 // make a move
78 void ttt_move(entity minigame, entity player, string pos )
79 {
80         if ( minigame.minigame_flags & TTT_TURN_PLACE )
81         if ( pos && player.team == (minigame.minigame_flags & TTT_TURN_TEAM) )
82         {
83                 if ( ttt_valid_tile(pos) )
84                 if ( !ttt_find_piece(minigame,pos) )
85                 {
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) )
94                         {
95                                 player.minigame_flags++;
96                                 minigame_server_sendflags(player, TTT_SF_PLAYERSCORE);
97                                 minigame.minigame_flags = TTT_TURN_WIN | player.team;
98                         }
99                         else if ( minigame.ttt_npieces >= (TTT_LET_CNT * TTT_NUM_CNT) )
100                                 minigame.minigame_flags = TTT_TURN_DRAW;
101                         else
102                                 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
103                 }
104         }
105 }
106
107 // request a new match
108 void ttt_next_match(entity minigame, entity player)
109 {
110 #ifdef SVQC
111         // on multiplayer matches, wait for both players to agree
112         if ( minigame.minigame_flags & (TTT_TURN_WIN|TTT_TURN_DRAW) )
113         {
114                 minigame.minigame_flags = TTT_TURN_NEXT | player.team;
115                 minigame.SendFlags |= MINIG_SF_UPDATE;
116         }
117         else if ( (minigame.minigame_flags & TTT_TURN_NEXT) &&
118                         !( minigame.minigame_flags & player.team ) )
119 #endif
120         {
121                 minigame.minigame_flags = TTT_TURN_PLACE | minigame.ttt_nexteam;
122                 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
123                 minigame.ttt_npieces = 0;
124                 entity e = NULL;
125                 while ( ( e = findentity(e,owner,minigame) ) )
126                         if ( e.classname == "minigame_board_piece" )
127                                 delete(e);
128         }
129 }
130
131 #ifdef SVQC
132
133
134 // required function, handle server side events
135 int ttt_server_event(entity minigame, string event, ...)
136 {
137         switch(event)
138         {
139                 case "start":
140                 {
141                         minigame.minigame_flags = (TTT_TURN_PLACE | TTT_TURN_TEAM1);
142                         return true;
143                 }
144                 case "end":
145                 {
146                         entity e = NULL;
147                         while( (e = findentity(e, owner, minigame)) )
148                         if(e.classname == "minigame_board_piece")
149                         {
150                                 strfree(e.netname);
151                                 delete(e);
152                         }
153                         return false;
154                 }
155                 case "join":
156                 {
157                         int pl_num = minigame_count_players(minigame);
158
159                         // Don't allow joining a single player match
160                         if ( (minigame.ttt_ai) && pl_num > 0 )
161                                 return false;
162
163                         // Don't allow more than 2 players
164                         if(pl_num >= 2) { return false; }
165
166                         // Get the right team
167                         if(minigame.minigame_players)
168                                 return minigame_next_team(minigame.minigame_players.team, 2);
169
170                         // Team 1 by default
171                         return 1;
172                 }
173                 case "cmd":
174                 {
175                         switch(argv(0))
176                         {
177                                 case "move":
178                                         ttt_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
179                                         return true;
180                                 case "next":
181                                         ttt_next_match(minigame,...(0,entity));
182                                         return true;
183                                 case "singleplayer":
184                                         if ( minigame_count_players(minigame) == 1 )
185                                         {
186                                                 minigame.ttt_ai = minigame_next_team(minigame.minigame_players.team, 2);
187                                                 minigame.SendFlags = TTT_SF_SINGLEPLAYER;
188                                         }
189                                         return true;
190                         }
191
192                         return false;
193                 }
194                 case "network_send":
195                 {
196                         entity sent = ...(0,entity);
197                         int sf = ...(1,int);
198                         if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
199                         {
200                                 WriteByte(MSG_ENTITY,sent.minigame_flags);
201                         }
202                         else if ( sent.classname == "minigame" && (sf & TTT_SF_SINGLEPLAYER) )
203                         {
204                                 WriteByte(MSG_ENTITY,sent.ttt_ai);
205                         }
206                         return false;
207                 }
208         }
209
210         return false;
211 }
212
213
214 #elif defined(CSQC)
215
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
220
221 // Required function, draw the game board
222 void ttt_hud_board(vector pos, vector mySize)
223 {
224         minigame_hud_fitsqare(pos, mySize);
225         ttt_boardpos = pos;
226         ttt_boardsize = mySize;
227
228         minigame_hud_simpleboard(pos,mySize,minigame_texture("ttt/board"));
229
230         vector tile_size = minigame_hud_denormalize_size('1 1 0'/TTT_TILE_SIZE,pos,mySize);
231         vector tile_pos;
232
233         if ( (active_minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team )
234         if ( ttt_valid_tile(ttt_curr_pos) )
235         {
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 );
241         }
242
243         entity e;
244         FOREACH_MINIGAME_ENTITY(e)
245         {
246                 if ( e.classname == "minigame_board_piece" )
247                 {
248                         tile_pos = minigame_tile_pos(e.netname,TTT_LET_CNT,TTT_NUM_CNT);
249                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
250
251                         if ( active_minigame.minigame_flags & TTT_TURN_WIN )
252                         if ( !e.ttt_checkwin )
253                                 e.ttt_checkwin = ttt_winning_piece(e) ? 1 : -1;
254
255                         float icon_color = 1;
256                         if ( e.ttt_checkwin == -1 )
257                                 icon_color = 0.4;
258                         else if ( e.ttt_checkwin == 1 )
259                         {
260                                 icon_color = 2;
261                                 minigame_drawpic_centered( tile_pos, minigame_texture("ttt/winglow"),
262                                                 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE );
263                         }
264
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 );
268                 }
269         }
270 }
271
272
273 // Required function, draw the game status panel
274 void ttt_hud_status(vector pos, vector mySize)
275 {
276         HUD_Panel_DrawBg();
277         vector ts;
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);
280
281         pos_y += ts_y;
282         mySize_y -= ts_y;
283
284         vector player_fontsize = hud_fontsize * 1.75;
285         ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
286         ts_x = mySize_x;
287         vector mypos;
288         vector tile_size = '48 48 0';
289
290         entity e;
291         FOREACH_MINIGAME_ENTITY(e)
292         {
293                 if ( e.classname == "minigame_player" )
294                 {
295                         mypos = pos;
296                         if ( e.team == 2 )
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);
301
302                         mypos_y += player_fontsize_y;
303                         drawpic( mypos,
304                                         minigame_texture(strcat("ttt/piece",ftos(e.team))),
305                                         tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
306
307                         mypos_x += tile_size_x;
308
309                         drawstring(mypos,ftos(e.minigame_flags),tile_size,
310                                            '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
311                 }
312         }
313 }
314
315 // Turn a set of flags into a help message
316 string ttt_turn_to_string(int turnflags)
317 {
318         if ( turnflags & TTT_TURN_DRAW )
319                 return _("Draw");
320
321         if ( turnflags & TTT_TURN_WIN )
322         {
323                 // translator-friendly messages composed of 2 existing messages
324                 // TODO: proper "you win" banner instead of hijacking the help message
325                 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
326                         strcat(_("You lost the game!"), "\n", _("Select \"^1Next Match^7\" on the menu for a rematch!"));
327                 return strcat(_("You win!"), "\n", _("Select \"^1Next Match^7\" on the menu to start a new match!"));
328         }
329
330         if ( turnflags & TTT_TURN_NEXT )
331         {
332                 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
333                         return _("Select \"^1Next Match^7\" on the menu to start a new match!");
334                 return _("Wait for your opponent to confirm the rematch");
335         }
336
337         if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
338                 return _("Wait for your opponent to make their move");
339
340         if ( turnflags & TTT_TURN_PLACE )
341                 return _("Click on the game board to place your piece");
342
343         return "";
344 }
345
346 const int TTT_AI_POSFLAG_A1 = 0x0001;
347 const int TTT_AI_POSFLAG_A2 = 0x0002;
348 const int TTT_AI_POSFLAG_A3 = 0x0004;
349 const int TTT_AI_POSFLAG_B1 = 0x0008;
350 const int TTT_AI_POSFLAG_B2 = 0x0010;
351 const int TTT_AI_POSFLAG_B3 = 0x0020;
352 const int TTT_AI_POSFLAG_C1 = 0x0040;
353 const int TTT_AI_POSFLAG_C2 = 0x0080;
354 const int TTT_AI_POSFLAG_C3 = 0x0100;
355
356 // convert a flag to a position
357 string ttt_ai_piece_flag2pos(int pieceflag)
358 {
359         switch(pieceflag)
360         {
361                 case TTT_AI_POSFLAG_A1:
362                         return "a1";
363                 case TTT_AI_POSFLAG_A2:
364                         return "a2";
365                 case TTT_AI_POSFLAG_A3:
366                         return "a3";
367
368                 case TTT_AI_POSFLAG_B1:
369                         return "b1";
370                 case TTT_AI_POSFLAG_B2:
371                         return "b2";
372                 case TTT_AI_POSFLAG_B3:
373                         return "b3";
374
375                 case TTT_AI_POSFLAG_C1:
376                         return "c1";
377                 case TTT_AI_POSFLAG_C2:
378                         return "c2";
379                 case TTT_AI_POSFLAG_C3:
380                         return "c3";
381
382                 default:
383                         return string_null;
384         }
385 }
386
387 bool ttt_ai_checkmask(int piecemask, int checkflags)
388 {
389         return checkflags && (piecemask & checkflags) == checkflags;
390 }
391
392 // get the third flag if the mask matches two of them
393 int ttt_ai_1of3(int piecemask, int flag1, int flag2, int flag3)
394 {
395         if ( ttt_ai_checkmask(piecemask,flag1|flag2|flag3) )
396                 return 0;
397
398         if ( ttt_ai_checkmask(piecemask,flag1|flag2) )
399                 return flag3;
400
401         if ( ttt_ai_checkmask(piecemask,flag3|flag2) )
402                 return flag1;
403
404         if ( ttt_ai_checkmask(piecemask,flag3|flag1) )
405                 return flag2;
406
407         return 0;
408 }
409
410 // Select a random flag in the mask
411 int ttt_ai_random(int piecemask)
412 {
413         if ( !piecemask )
414                 return 0;
415
416         int f = 1;
417
418         RandomSelection_Init();
419
420         for ( int i = 0; i < 9; i++ )
421         {
422                 if ( piecemask & f )
423                         RandomSelection_AddFloat(f, 1, 1);
424                 f <<= 1;
425         }
426
427         LOG_TRACE(sprintf("TTT AI: selected %x from %x",
428                         RandomSelection_chosen_float, piecemask) );
429         return RandomSelection_chosen_float;
430 }
431
432 // Block/complete a 3 i na row
433 int ttt_ai_block3 ( int piecemask, int piecemask_free )
434 {
435         int r = 0;
436
437         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_A3);
438         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_B3);
439         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_C1,TTT_AI_POSFLAG_C2,TTT_AI_POSFLAG_C3);
440         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_C1);
441         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C2);
442         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B3,TTT_AI_POSFLAG_C3);
443         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C3);
444         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C1);
445         LOG_TRACE(sprintf("TTT AI: possible 3 in a rows in %x: %x (%x)",piecemask,r, r&piecemask_free));
446         r &= piecemask_free;
447         return ttt_ai_random(r);
448 }
449
450 // Simple AI
451 // 1) tries to win the game if possible
452 // 2) tries to block the opponent if they have 2 in a row
453 // 3) places a piece randomly
454 string ttt_ai_choose_simple(int piecemask_self, int piecemask_opponent, int piecemask_free )
455 {
456         int move = 0;
457
458         LOG_TRACE("TTT AI: checking winning move");
459         if (( move = ttt_ai_block3(piecemask_self,piecemask_free) ))
460                 return ttt_ai_piece_flag2pos(move); // place winning move
461
462         LOG_TRACE("TTT AI: checking opponent's winning move");
463         if (( move = ttt_ai_block3(piecemask_opponent,piecemask_free) ))
464                 return ttt_ai_piece_flag2pos(move); // block opponent
465
466         LOG_TRACE("TTT AI: random move");
467         return ttt_ai_piece_flag2pos(ttt_ai_random(piecemask_free));
468 }
469
470 // AI move (if it's AI's turn)
471 void ttt_aimove(entity minigame)
472 {
473         if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame.ttt_ai) )
474         {
475                 entity aiplayer = NULL;
476                 while ( ( aiplayer = findentity(aiplayer,owner,minigame) ) )
477                         if ( aiplayer.classname == "minigame_player" && !aiplayer.minigame_playerslot )
478                                 break;
479
480                 /*
481                  * Build bit masks for the board pieces
482                  * .---.---.---.
483                  * | 4 | 32|256| 3
484                  * |---+---+---|
485                  * | 2 | 16|128| 2
486                  * |---+---+---|
487                  * | 1 | 8 | 64| 1
488                  * '---'---'---'
489                  *   A   B   C
490                  */
491                 int piecemask_self = 0;
492                 int piecemask_opponent = 0;
493                 int piecemask_free = 0;
494                 int pieceflag = 1;
495                 string pos;
496                 for ( int i = 0; i < 3; i++ )
497                 {
498                         for ( int j = 0; j < 3; j++ )
499                         {
500                                 pos = minigame_tile_buildname(i,j);
501                                 entity piece = ttt_find_piece(minigame,pos);
502                                 if ( piece )
503                                 {
504                                         if ( piece.team == aiplayer.team )
505                                                 piecemask_self |= pieceflag;
506                                         else
507                                                 piecemask_opponent |= pieceflag;
508                                 }
509                                 else
510                                         piecemask_free |= pieceflag;
511                                 pieceflag <<= 1;
512                         }
513                 }
514
515                 // TODO multiple AI difficulties
516                 LOG_TRACE(sprintf("TTT AI: self: %x opponent: %x free: %x",
517                                 piecemask_self, piecemask_opponent, piecemask_free));
518                 pos = ttt_ai_choose_simple(piecemask_self, piecemask_opponent, piecemask_free);
519                 LOG_TRACE("TTT AI: chosen move: ", pos);
520                 if ( !pos )
521                         LOG_TRACE("Tic Tac Toe AI has derped!");
522                 else
523                         ttt_move(minigame,aiplayer,pos);
524         }
525         strcpy(minigame.message, ttt_turn_to_string(minigame.minigame_flags));
526 }
527
528 // Make the correct move
529 void ttt_make_move(entity minigame)
530 {
531         if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
532         {
533                 if ( minigame.ttt_ai  )
534                 {
535                         ttt_move(minigame, minigame_self, ttt_curr_pos );
536                         ttt_aimove(minigame);
537                 }
538                 else
539                         minigame_cmd("move ",ttt_curr_pos);
540         }
541 }
542
543 void ttt_set_curr_pos(string s)
544 {
545         strfree(ttt_curr_pos);
546         if ( s )
547                 s = strzone(s);
548         ttt_curr_pos = s;
549 }
550
551 // Required function, handle client events
552 int ttt_client_event(entity minigame, string event, ...)
553 {
554         switch(event)
555         {
556                 case "activate":
557                 {
558                         ttt_set_curr_pos("");
559                         strcpy(minigame.message, ttt_turn_to_string(minigame.minigame_flags));
560                         return false;
561                 }
562                 case "deactivate":
563                 {
564                         strfree(minigame.message);
565                         return false;
566                 }
567                 case "key_pressed":
568                 {
569                         if((minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team)
570                         {
571                                 switch ( ...(0,int) )
572                                 {
573                                         case K_RIGHTARROW:
574                                         case K_KP_RIGHTARROW:
575                                                 if ( ! ttt_curr_pos )
576                                                         ttt_set_curr_pos("a3");
577                                                 else
578                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,1,0,TTT_LET_CNT,TTT_NUM_CNT));
579                                                 return true;
580                                         case K_LEFTARROW:
581                                         case K_KP_LEFTARROW:
582                                                 if ( ! ttt_curr_pos )
583                                                         ttt_set_curr_pos("c3");
584                                                 else
585                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,-1,0,TTT_LET_CNT,TTT_NUM_CNT));
586                                                 return true;
587                                         case K_UPARROW:
588                                         case K_KP_UPARROW:
589                                                 if ( ! ttt_curr_pos )
590                                                         ttt_set_curr_pos("a1");
591                                                 else
592                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,1,TTT_LET_CNT,TTT_NUM_CNT));
593                                                 return true;
594                                         case K_DOWNARROW:
595                                         case K_KP_DOWNARROW:
596                                                 if ( ! ttt_curr_pos )
597                                                         ttt_set_curr_pos("a3");
598                                                 else
599                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,-1,TTT_LET_CNT,TTT_NUM_CNT));
600                                                 return true;
601                                         case K_ENTER:
602                                         case K_KP_ENTER:
603                                         case K_SPACE:
604                                                 ttt_make_move(minigame);
605                                                 return true;
606                                 }
607                         }
608
609                         return false;
610                 }
611                 case "mouse_pressed":
612                 {
613                         if(...(0,int) == K_MOUSE1)
614                         {
615                                 ttt_make_move(minigame);
616                                 return true;
617                         }
618
619                         return false;
620                 }
621                 case "mouse_moved":
622                 {
623                         vector mouse_pos = minigame_hud_normalize(mousepos,ttt_boardpos,ttt_boardsize);
624                         if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
625                                 ttt_set_curr_pos(minigame_tile_name(mouse_pos,TTT_LET_CNT,TTT_NUM_CNT));
626                         if ( ! ttt_valid_tile(ttt_curr_pos) )
627                                 ttt_set_curr_pos("");
628
629                         return true;
630                 }
631                 case "network_receive":
632                 {
633                         entity sent = ...(0,entity);
634                         int sf = ...(1,int);
635                         if ( sent.classname == "minigame" )
636                         {
637                                 if ( sf & MINIG_SF_UPDATE )
638                                 {
639                                         strcpy(sent.message, ttt_turn_to_string(sent.minigame_flags));
640                                         if ( sent.minigame_flags & minigame_self.team )
641                                                 minigame_prompt();
642                                 }
643
644                                 if ( (sf & TTT_SF_SINGLEPLAYER) )
645                                 {
646                                         int ai = ReadByte();
647                                         bool spawnai = ai && !sent.ttt_ai;
648                                         sent.ttt_ai = ai;
649
650                                         if ( spawnai )
651                                         {
652                                                 entity aiplayer = new(minigame_player);
653                                                 aiplayer.owner = minigame;
654                                                 aiplayer.team = ai;
655                                                 aiplayer.minigame_playerslot = 0;
656                                                 aiplayer.minigame_autoclean = 1;
657                                                 ttt_aimove(minigame);
658                                         }
659
660                                 }
661                         }
662                         else if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
663                         {
664                                 sent.minigame_flags = ReadByte();
665                         }
666
667                         return false;
668                 }
669                 case "menu_show":
670                 {
671                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
672                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Single Player"),"singleplayer");
673                         return false;
674                 }
675                 case "menu_click":
676                 {
677                         if(...(0,string) == "next")
678                         {
679                                 if ( minigame.ttt_ai )
680                                 {
681                                         ttt_next_match(minigame,minigame_self);
682                                         ttt_aimove(minigame);
683                                 }
684                                 else
685                                         minigame_cmd("next");
686                         }
687                         else if ( ...(0,string) == "singleplayer" && !minigame.ttt_ai )
688                         {
689                                 if ( minigame_count_players(minigame) == 1 )
690                                         minigame_cmd("singleplayer");
691                         }
692                         return false;
693                 }
694         }
695
696         return false;
697 }
698
699 #endif