]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/minigames/minigame/ttt.qc
Minigame code and cfg
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigame / ttt.qc
1 const float TTT_TURN_PLACE = 0x0100; // player has to place a piece on the board
2 const float TTT_TURN_WIN   = 0x0200; // player has won
3 const float TTT_TURN_DRAW  = 0x0400; // no moves are possible
4 const float TTT_TURN_NEXT  = 0x0800; // a player wants to start a new match
5 const float TTT_TURN_TYPE  = 0x0f00; // turn type mask
6
7 const float TTT_TURN_TEAM1 = 0x0001;
8 const float TTT_TURN_TEAM2 = 0x0002;
9 const float TTT_TURN_TEAM  = 0x000f; // turn team mask
10
11 // send flags
12 const float TTT_SF_PLAYERSCORE  = MINIG_SF_CUSTOM;   // send minigame_player scores (won matches)
13 const float TTT_SF_SINGLEPLAYER = MINIG_SF_CUSTOM<<1;// send minigame.ttt_ai
14
15 .float ttt_npieces; // (minigame) number of pieces on the board (simplifies checking a draw)
16 .float ttt_nexteam; // (minigame) next team (used to change the starting team on following matches)
17 .float 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 float ttt_winning_piece(entity piece)
31 {
32         float number = minigame_tile_number(piece.netname);
33         float 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 1;
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 1;
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 1;
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 1;
56         
57         return 0;
58 }
59
60 // check if the tile name is valid (3x3 grid)
61 float ttt_valid_tile(string tile)
62 {
63         if ( !tile )
64                 return 0;
65         float number = minigame_tile_number(tile);
66         float 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 float minigame_event_ttt(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                         float 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,float) == 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                         float sf = ...(1,float);
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 .float ttt_checkwin; // Used to optimize checks to display a win
213
214 // Required function, draw the game board
215 void minigame_hud_board_ttt(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 minigame_hud_status_ttt(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(float 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 float TTT_AI_POSFLAG_A1 = 0x0001;
338 const float TTT_AI_POSFLAG_A2 = 0x0002;
339 const float TTT_AI_POSFLAG_A3 = 0x0004;
340 const float TTT_AI_POSFLAG_B1 = 0x0008;
341 const float TTT_AI_POSFLAG_B2 = 0x0010;
342 const float TTT_AI_POSFLAG_B3 = 0x0020;
343 const float TTT_AI_POSFLAG_C1 = 0x0040;
344 const float TTT_AI_POSFLAG_C2 = 0x0080;
345 const float TTT_AI_POSFLAG_C3 = 0x0100;
346
347 // convert a flag to a position
348 string ttt_ai_piece_flag2pos(float 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 float ttt_ai_checkmask(float piecemask, float checkflags)
379 {
380         return checkflags && (piecemask & checkflags) == checkflags;
381 }
382
383 // get the third flag if the mask matches two of them
384 float ttt_ai_1of3(float piecemask, float flag1, float flag2, float 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 float ttt_ai_random(float piecemask)
403 {
404         if ( !piecemask )
405                 return 0;
406         
407         float i;
408         float f = 1;
409         
410         RandomSelection_Init();
411         
412         for ( i = 0; i < 9; i++ )
413         {
414                 if ( piecemask & f )
415                         RandomSelection_Add(world, f, string_null, 1, 1);
416                 f <<= 1;
417         }
418         
419         dprint(sprintf("TTT AI: selected %x from %x\n",
420                         RandomSelection_chosen_float, piecemask) );
421         return RandomSelection_chosen_float;
422 }
423
424 // Block/complete a 3 i na row
425 float ttt_ai_block3 ( float piecemask, float piecemask_free )
426 {
427         float r = 0;
428         
429         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_A3);
430         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_B3);
431         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_C1,TTT_AI_POSFLAG_C2,TTT_AI_POSFLAG_C3);
432         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B1,TTT_AI_POSFLAG_C1);
433         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A2,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C2);
434         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B3,TTT_AI_POSFLAG_C3);
435         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A1,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C3);
436         r |= ttt_ai_1of3(piecemask,TTT_AI_POSFLAG_A3,TTT_AI_POSFLAG_B2,TTT_AI_POSFLAG_C1);
437         dprint(sprintf("TTT AI: possible 3 in a rows in %x: %x (%x)\n",piecemask,r, r&piecemask_free));
438         r &= piecemask_free;
439         return ttt_ai_random(r);
440 }
441
442 // Simple AI
443 // 1) tries to win the game if possible
444 // 2) tries to block the opponent if they have 2 in a row
445 // 3) places a piece randomly
446 string ttt_ai_choose_simple(float piecemask_self, float piecemask_opponent, float piecemask_free )
447 {
448         float move = 0;
449         
450         dprint("TTT AI: checking winning move\n");
451         if (( move = ttt_ai_block3(piecemask_self,piecemask_free) ))
452                 return ttt_ai_piece_flag2pos(move); // place winning move
453                 
454         dprint("TTT AI: checking opponent's winning move\n");
455         if (( move = ttt_ai_block3(piecemask_opponent,piecemask_free) ))
456                 return ttt_ai_piece_flag2pos(move); // block opponent
457                 
458         dprint("TTT AI: random move\n");
459         return ttt_ai_piece_flag2pos(ttt_ai_random(piecemask_free));
460 }
461
462 // AI move (if it's AI's turn)
463 void ttt_aimove(entity minigame)
464 {
465         if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame.ttt_ai) )
466         {
467                 entity aiplayer = world;
468                 while ( ( aiplayer = findentity(aiplayer,owner,minigame) ) )
469                         if ( aiplayer.classname == "minigame_player" && !aiplayer.minigame_playerslot )
470                                 break;
471                 
472                 /*
473                  * Build bit masks for the board pieces
474                  * .---.---.---.
475                  * | 4 | 32|256| 3
476                  * |---+---+---| 
477                  * | 2 | 16|128| 2
478                  * |---+---+---| 
479                  * | 1 | 8 | 64| 1
480                  * '---'---'---'
481                  *   A   B   C
482                  */
483                 float piecemask_self = 0;
484                 float piecemask_opponent = 0;
485                 float piecemask_free = 0;
486                 float pieceflag = 1;
487                 string pos;
488                 
489                 float i,j;
490                 for ( i = 0; i < 3; i++ )
491                         for ( j = 0; j < 3; j++ )
492                         {
493                                 pos = minigame_tile_buildname(i,j);
494                                 entity piece = ttt_find_piece(minigame,pos);
495                                 if ( piece )
496                                 {
497                                         if ( piece.team == aiplayer.team )
498                                                 piecemask_self |= pieceflag;
499                                         else
500                                                 piecemask_opponent |= pieceflag;
501                                 }
502                                 else
503                                         piecemask_free |= pieceflag;
504                                 pieceflag <<= 1;
505                         }
506                         
507                 // TODO multiple AI difficulties
508                 dprint(sprintf("TTT AI: self: %x opponent: %x free: %x\n",
509                                 piecemask_self, piecemask_opponent, piecemask_free));
510                 pos = ttt_ai_choose_simple(piecemask_self, piecemask_opponent, piecemask_free);
511                 dprint("TTT AI: chosen move: ",pos,"\n\n");
512                 if ( !pos )
513                         dprint("Tic Tac Toe AI has derped!\n");
514                 else
515                         ttt_move(minigame,aiplayer,pos);
516         }
517         minigame.message = ttt_turn_to_string(minigame.minigame_flags);
518 }
519
520 // Make the correct move
521 void ttt_make_move(entity minigame)
522 {
523         if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
524         {
525                 if ( minigame.ttt_ai  )
526                 {
527                         ttt_move(minigame, minigame_self, ttt_curr_pos );
528                         ttt_aimove(minigame);
529                 }
530                 else
531                         minigame_cmd("move ",ttt_curr_pos);
532         }
533 }
534
535 void ttt_set_curr_pos(string s)
536 {
537         if ( ttt_curr_pos )
538                 strunzone(ttt_curr_pos);
539         if ( s )
540                 s = strzone(s);
541         ttt_curr_pos = s;
542 }
543
544 // Required function, handle client events
545 float minigame_event_ttt(entity minigame, string event, ...)
546 {
547         switch(event)
548         {
549                 case "activate":
550                 {
551                         ttt_set_curr_pos("");
552                         minigame.message = ttt_turn_to_string(minigame.minigame_flags);
553                         return false;
554                 }
555                 case "key_pressed":
556                 {
557                         if((minigame.minigame_flags & TTT_TURN_TEAM) == minigame_self.team)
558                         {
559                                 switch ( ...(0,float) )
560                                 {
561                                         case K_RIGHTARROW:
562                                         case K_KP_RIGHTARROW:
563                                                 if ( ! ttt_curr_pos )
564                                                         ttt_set_curr_pos("a3");
565                                                 else
566                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,1,0,3,3));
567                                                 return true;
568                                         case K_LEFTARROW:
569                                         case K_KP_LEFTARROW:
570                                                 if ( ! ttt_curr_pos )
571                                                         ttt_set_curr_pos("c3");
572                                                 else
573                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,-1,0,3,3));
574                                                 return true;
575                                         case K_UPARROW:
576                                         case K_KP_UPARROW:
577                                                 if ( ! ttt_curr_pos )
578                                                         ttt_set_curr_pos("a1");
579                                                 else
580                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,1,3,3));
581                                                 return true;
582                                         case K_DOWNARROW:
583                                         case K_KP_DOWNARROW:
584                                                 if ( ! ttt_curr_pos )
585                                                         ttt_set_curr_pos("a3");
586                                                 else
587                                                         ttt_set_curr_pos(minigame_relative_tile(ttt_curr_pos,0,-1,3,3));
588                                                 return true;
589                                         case K_ENTER:
590                                         case K_KP_ENTER:
591                                         case K_SPACE:
592                                                 ttt_make_move(minigame);
593                                                 return true;
594                                 }
595                         }
596
597                         return false;
598                 }
599                 case "mouse_pressed":
600                 {
601                         if(...(0,float) == K_MOUSE1)
602                         {
603                                 ttt_make_move(minigame);
604                                 return true;
605                         }
606
607                         return false;
608                 }
609                 case "mouse_moved":
610                 {
611                         vector mouse_pos = minigame_hud_normalize(mousepos,ttt_boardpos,ttt_boardsize);
612                         if ( minigame.minigame_flags == (TTT_TURN_PLACE|minigame_self.team) )
613                                 ttt_set_curr_pos(minigame_tile_name(mouse_pos,3,3));
614                         if ( ! ttt_valid_tile(ttt_curr_pos) )
615                                 ttt_set_curr_pos("");
616
617                         return true;
618                 }
619                 case "network_receive":
620                 {
621                         entity sent = ...(0,entity);
622                         float sf = ...(1,float);
623                         if ( sent.classname == "minigame" )
624                         {
625                                 if ( sf & MINIG_SF_UPDATE )
626                                 {
627                                         sent.message = ttt_turn_to_string(sent.minigame_flags);
628                                         if ( sent.minigame_flags & minigame_self.team )
629                                                 minigame_prompt();
630                                 }
631                                 
632                                 if ( (sf & TTT_SF_SINGLEPLAYER) )
633                                 {
634                                         float ai = ReadByte();
635                                         float spawnai = ai && !sent.ttt_ai;
636                                         sent.ttt_ai = ai;
637                                         
638                                         if ( spawnai )
639                                         {
640                                                 entity aiplayer = spawn();
641                                                 aiplayer.classname = "minigame_player";
642                                                 aiplayer.owner = minigame;
643                                                 aiplayer.team = ai;
644                                                 aiplayer.minigame_playerslot = 0;
645                                                 aiplayer.minigame_autoclean = 1;
646                                                 ttt_aimove(minigame);
647                                         }
648                                         
649                                 }
650                         }
651                         else if ( sent.classname == "minigame_player" && (sf & TTT_SF_PLAYERSCORE ) )
652                         {
653                                 sent.minigame_flags = ReadByte();
654                         }
655
656                         return false;
657                 }
658                 case "menu_show":
659                 {
660                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
661                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Single Player"),"singleplayer");
662                         return false;
663                 }
664                 case "menu_click":
665                 {
666                         if(...(0,string) == "next")
667                         {
668                                 if ( minigame.ttt_ai )
669                                 {
670                                         ttt_next_match(minigame,minigame_self);
671                                         ttt_aimove(minigame);
672                                 }
673                                 else
674                                         minigame_cmd("next");
675                         }
676                         else if ( ...(0,string) == "singleplayer" && !minigame.ttt_ai )
677                         {
678                                 if ( minigame_count_players(minigame) == 1 )
679                                         minigame_cmd("singleplayer");
680                         }
681                         return false;
682                 }
683         }
684
685         return false;
686 }
687
688 #endif