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