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