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