]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/minigames/minigame/ttt.qc
Merge branch 'master' into Mario/entcs
[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                                 if(e.netname) { strunzone(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(1);
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                 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
324                         return _("You lost the game!\nSelect \"^1Next Match^7\" on the menu for a rematch!");
325                 return _("You win!\nSelect \"^1Next Match^7\" on the menu to start a new match!");
326         }
327
328         if ( turnflags & TTT_TURN_NEXT )
329         {
330                 if ( (turnflags&TTT_TURN_TEAM) != minigame_self.team )
331                         return _("Select \"^1Next Match^7\" on the menu to start a new match!");
332                 return _("Wait for your opponent to confirm the rematch");
333         }
334
335         if ( (turnflags & TTT_TURN_TEAM) != minigame_self.team )
336                 return _("Wait for your opponent to make their move");
337
338         if ( turnflags & TTT_TURN_PLACE )
339                 return _("Click on the game board to place your piece");
340
341         return "";
342 }
343
344 const int TTT_AI_POSFLAG_A1 = 0x0001;
345 const int TTT_AI_POSFLAG_A2 = 0x0002;
346 const int TTT_AI_POSFLAG_A3 = 0x0004;
347 const int TTT_AI_POSFLAG_B1 = 0x0008;
348 const int TTT_AI_POSFLAG_B2 = 0x0010;
349 const int TTT_AI_POSFLAG_B3 = 0x0020;
350 const int TTT_AI_POSFLAG_C1 = 0x0040;
351 const int TTT_AI_POSFLAG_C2 = 0x0080;
352 const int TTT_AI_POSFLAG_C3 = 0x0100;
353
354 // convert a flag to a position
355 string ttt_ai_piece_flag2pos(int pieceflag)
356 {
357         switch(pieceflag)
358         {
359                 case TTT_AI_POSFLAG_A1:
360                         return "a1";
361                 case TTT_AI_POSFLAG_A2:
362                         return "a2";
363                 case TTT_AI_POSFLAG_A3:
364                         return "a3";
365
366                 case TTT_AI_POSFLAG_B1:
367                         return "b1";
368                 case TTT_AI_POSFLAG_B2:
369                         return "b2";
370                 case TTT_AI_POSFLAG_B3:
371                         return "b3";
372
373                 case TTT_AI_POSFLAG_C1:
374                         return "c1";
375                 case TTT_AI_POSFLAG_C2:
376                         return "c2";
377                 case TTT_AI_POSFLAG_C3:
378                         return "c3";
379
380                 default:
381                         return string_null;
382         }
383 }
384
385 bool ttt_ai_checkmask(int piecemask, int checkflags)
386 {
387         return checkflags && (piecemask & checkflags) == checkflags;
388 }
389
390 // get the third flag if the mask matches two of them
391 int ttt_ai_1of3(int piecemask, int flag1, int flag2, int flag3)
392 {
393         if ( ttt_ai_checkmask(piecemask,flag1|flag2|flag3) )
394                 return 0;
395
396         if ( ttt_ai_checkmask(piecemask,flag1|flag2) )
397                 return flag3;
398
399         if ( ttt_ai_checkmask(piecemask,flag3|flag2) )
400                 return flag1;
401
402         if ( ttt_ai_checkmask(piecemask,flag3|flag1) )
403                 return flag2;
404
405         return 0;
406 }
407
408 // Select a random flag in the mask
409 int ttt_ai_random(int piecemask)
410 {
411         if ( !piecemask )
412                 return 0;
413
414         int f = 1;
415
416         RandomSelection_Init();
417
418         for ( int i = 0; i < 9; i++ )
419         {
420                 if ( piecemask & f )
421                         RandomSelection_Add(NULL, f, string_null, 1, 1);
422                 f <<= 1;
423         }
424
425         LOG_TRACE(sprintf("TTT AI: selected %x from %x",
426                         RandomSelection_chosen_float, piecemask) );
427         return RandomSelection_chosen_float;
428 }
429
430 // Block/complete a 3 i na row
431 int ttt_ai_block3 ( int piecemask, int piecemask_free )
432 {
433         int r = 0;
434
435         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_A3);
436         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_B3);
437         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_C1,TTT_AI_POSFLAG_C2,TTT_AI_POSFLAG_C3);
438         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_C1);
439         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C2);
440         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B3,TTT_AI_POSFLAG_C3);
441         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C3);
442         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C1);
443         LOG_TRACE(sprintf("TTT AI: possible 3 in a rows in %x: %x (%x)",piecemask,r, r&piecemask_free));
444         r &= piecemask_free;
445         return ttt_ai_random(r);
446 }
447
448 // Simple AI
449 // 1) tries to win the game if possible
450 // 2) tries to block the opponent if they have 2 in a row
451 // 3) places a piece randomly
452 string ttt_ai_choose_simple(int piecemask_self, int piecemask_opponent, int piecemask_free )
453 {
454         int move = 0;
455
456         LOG_TRACE("TTT AI: checking winning move");
457         if (( move = ttt_ai_block3(piecemask_self,piecemask_free) ))
458                 return ttt_ai_piece_flag2pos(move); // place winning move
459
460         LOG_TRACE("TTT AI: checking opponent's winning move");
461         if (( move = ttt_ai_block3(piecemask_opponent,piecemask_free) ))
462                 return ttt_ai_piece_flag2pos(move); // block opponent
463
464         LOG_TRACE("TTT AI: random move");
465         return ttt_ai_piece_flag2pos(ttt_ai_random(piecemask_free));
466 }
467
468 // AI move (if it's AI's turn)
469 void ttt_aimove(entity minigame)
470 {
471         if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame.ttt_ai) )
472         {
473                 entity aiplayer = NULL;
474                 while ( ( aiplayer = findentity(aiplayer,owner,minigame) ) )
475                         if ( aiplayer.classname == "minigame_player" && !aiplayer.minigame_playerslot )
476                                 break;
477
478                 /*
479                  * Build bit masks for the board pieces
480                  * .---.---.---.
481                  * | 4 | 32|256| 3
482                  * |---+---+---|
483                  * | 2 | 16|128| 2
484                  * |---+---+---|
485                  * | 1 | 8 | 64| 1
486                  * '---'---'---'
487                  *   A   B   C
488                  */
489                 int piecemask_self = 0;
490                 int piecemask_opponent = 0;
491                 int piecemask_free = 0;
492                 int pieceflag = 1;
493                 string pos;
494                 for ( int i = 0; i < 3; i++ )
495                 {
496                         for ( int j = 0; j < 3; j++ )
497                         {
498                                 pos = minigame_tile_buildname(i,j);
499                                 entity piece = ttt_find_piece(minigame,pos);
500                                 if ( piece )
501                                 {
502                                         if ( piece.team == aiplayer.team )
503                                                 piecemask_self |= pieceflag;
504                                         else
505                                                 piecemask_opponent |= pieceflag;
506                                 }
507                                 else
508                                         piecemask_free |= pieceflag;
509                                 pieceflag <<= 1;
510                         }
511                 }
512
513                 // TODO multiple AI difficulties
514                 LOG_TRACE(sprintf("TTT AI: self: %x opponent: %x free: %x",
515                                 piecemask_self, piecemask_opponent, piecemask_free));
516                 pos = ttt_ai_choose_simple(piecemask_self, piecemask_opponent, piecemask_free);
517                 LOG_TRACE("TTT AI: chosen move: ", pos);
518                 if ( !pos )
519                         LOG_TRACE("Tic Tac Toe AI has derped!");
520                 else
521                         ttt_move(minigame,aiplayer,pos);
522         }
523         minigame.message = ttt_turn_to_string(minigame.minigame_flags);
524 }
525
526 // Make the correct move
527 void ttt_make_move(entity minigame)
528 {
529         if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
530         {
531                 if ( minigame.ttt_ai  )
532                 {
533                         ttt_move(minigame, minigame_self, ttt_curr_pos );
534                         ttt_aimove(minigame);
535                 }
536                 else
537                         minigame_cmd("move ",ttt_curr_pos);
538         }
539 }
540
541 void ttt_set_curr_pos(string s)
542 {
543         if ( ttt_curr_pos )
544                 strunzone(ttt_curr_pos);
545         if ( s )
546                 s = strzone(s);
547         ttt_curr_pos = s;
548 }
549
550 // Required function, handle client events
551 int ttt_client_event(entity minigame, string event, ...)
552 {
553         switch(event)
554         {
555                 case "activate":
556                 {
557                         ttt_set_curr_pos("");
558                         minigame.message = ttt_turn_to_string(minigame.minigame_flags);
559                         return false;
560                 }
561                 case "key_pressed":
562                 {
563                         if((minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team)
564                         {
565                                 switch ( ...(0,int) )
566                                 {
567                                         case K_RIGHTARROW:
568                                         case K_KP_RIGHTARROW:
569                                                 if ( ! ttt_curr_pos )
570                                                         ttt_set_curr_pos("a3");
571                                                 else
572                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,1,0,TTT_LET_CNT,TTT_NUM_CNT));
573                                                 return true;
574                                         case K_LEFTARROW:
575                                         case K_KP_LEFTARROW:
576                                                 if ( ! ttt_curr_pos )
577                                                         ttt_set_curr_pos("c3");
578                                                 else
579                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,-1,0,TTT_LET_CNT,TTT_NUM_CNT));
580                                                 return true;
581                                         case K_UPARROW:
582                                         case K_KP_UPARROW:
583                                                 if ( ! ttt_curr_pos )
584                                                         ttt_set_curr_pos("a1");
585                                                 else
586                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,1,TTT_LET_CNT,TTT_NUM_CNT));
587                                                 return true;
588                                         case K_DOWNARROW:
589                                         case K_KP_DOWNARROW:
590                                                 if ( ! ttt_curr_pos )
591                                                         ttt_set_curr_pos("a3");
592                                                 else
593                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,-1,TTT_LET_CNT,TTT_NUM_CNT));
594                                                 return true;
595                                         case K_ENTER:
596                                         case K_KP_ENTER:
597                                         case K_SPACE:
598                                                 ttt_make_move(minigame);
599                                                 return true;
600                                 }
601                         }
602
603                         return false;
604                 }
605                 case "mouse_pressed":
606                 {
607                         if(...(0,int) == K_MOUSE1)
608                         {
609                                 ttt_make_move(minigame);
610                                 return true;
611                         }
612
613                         return false;
614                 }
615                 case "mouse_moved":
616                 {
617                         vector mouse_pos = minigame_hud_normalize(mousepos,ttt_boardpos,ttt_boardsize);
618                         if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
619                                 ttt_set_curr_pos(minigame_tile_name(mouse_pos,TTT_LET_CNT,TTT_NUM_CNT));
620                         if ( ! ttt_valid_tile(ttt_curr_pos) )
621                                 ttt_set_curr_pos("");
622
623                         return true;
624                 }
625                 case "network_receive":
626                 {
627                         entity sent = ...(0,entity);
628                         int sf = ...(1,int);
629                         if ( sent.classname == "minigame" )
630                         {
631                                 if ( sf & MINIG_SF_UPDATE )
632                                 {
633                                         sent.message = ttt_turn_to_string(sent.minigame_flags);
634                                         if ( sent.minigame_flags & minigame_self.team )
635                                                 minigame_prompt();
636                                 }
637
638                                 if ( (sf & TTT_SF_SINGLEPLAYER) )
639                                 {
640                                         int ai = ReadByte();
641                                         bool spawnai = ai && !sent.ttt_ai;
642                                         sent.ttt_ai = ai;
643
644                                         if ( spawnai )
645                                         {
646                                                 entity aiplayer = new(minigame_player);
647                                                 aiplayer.owner = minigame;
648                                                 aiplayer.team = ai;
649                                                 aiplayer.minigame_playerslot = 0;
650                                                 aiplayer.minigame_autoclean = 1;
651                                                 ttt_aimove(minigame);
652                                         }
653
654                                 }
655                         }
656                         else if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
657                         {
658                                 sent.minigame_flags = ReadByte();
659                         }
660
661                         return false;
662                 }
663                 case "menu_show":
664                 {
665                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
666                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Single Player"),"singleplayer");
667                         return false;
668                 }
669                 case "menu_click":
670                 {
671                         if(...(0,string) == "next")
672                         {
673                                 if ( minigame.ttt_ai )
674                                 {
675                                         ttt_next_match(minigame,minigame_self);
676                                         ttt_aimove(minigame);
677                                 }
678                                 else
679                                         minigame_cmd("next");
680                         }
681                         else if ( ...(0,string) == "singleplayer" && !minigame.ttt_ai )
682                         {
683                                 if ( minigame_count_players(minigame) == 1 )
684                                         minigame_cmd("singleplayer");
685                         }
686                         return false;
687                 }
688         }
689
690         return false;
691 }
692
693 #endif