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