1 const int PP_TURN_PLACE = 0x0100; // player has to place a piece on the board
2 const int PP_TURN_WIN = 0x0200; // player has won
3 const int PP_TURN_DRAW = 0x0400; // players have equal scores
4 const int PP_TURN_NEXT = 0x0800; // a player wants to start a new match
5 const int PP_TURN_TYPE = 0x0f00; // turn type mask
7 const int PP_TURN_TEAM1 = 0x0001;
8 const int PP_TURN_TEAM2 = 0x0002;
9 const int PP_TURN_TEAM = 0x000f; // turn team mask
11 const int PP_BLOCKED_TEAM = 5; // there won't ever be a 5th team, so we can abuse this
13 const int PP_LET_CNT = 7;
14 const int PP_NUM_CNT = 7;
16 const int PP_TILE_SIZE = 7;
23 .entity pp_curr_piece; // identifier for the current target piece
25 // find tic tac toe piece given its tile name
26 entity pp_find_piece(entity minig, string tile)
29 while ( ( e = findentity(e,owner,minig) ) )
30 if ( e.classname == "minigame_board_piece" && e.netname == tile )
35 // check if the tile name is valid (3x3 grid)
36 bool pp_valid_tile(string tile)
40 int number = minigame_tile_number(tile);
41 int letter = minigame_tile_letter(tile);
42 return 0 <= number && number < PP_NUM_CNT && 0 <= letter && letter < PP_LET_CNT;
45 // Checks if the given piece completes a row
46 bool pp_winning_piece(entity piece)
48 int number = minigame_tile_number(piece.netname);
49 int letter = minigame_tile_letter(piece.netname);
52 if(!pp_valid_tile(minigame_tile_buildname(letter-1,number)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter-1,number)).team == 5)
53 if(!pp_valid_tile(minigame_tile_buildname(letter+1,number)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter+1,number)).team == 5)
54 if(!pp_valid_tile(minigame_tile_buildname(letter,number-1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter,number-1)).team == 5)
55 if(!pp_valid_tile(minigame_tile_buildname(letter,number+1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter,number+1)).team == 5)
56 if(!pp_valid_tile(minigame_tile_buildname(letter+1,number+1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter+1,number+1)).team == 5)
57 if(!pp_valid_tile(minigame_tile_buildname(letter-1,number-1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter-1,number-1)).team == 5)
58 if(!pp_valid_tile(minigame_tile_buildname(letter+1,number-1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter+1,number-1)).team == 5)
59 if(!pp_valid_tile(minigame_tile_buildname(letter-1,number+1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter-1,number+1)).team == 5)
65 bool pp_valid_move(entity minigame, string pos)
67 if(!pp_valid_tile(pos))
69 if(pp_find_piece(minigame,pos).team == 5)
72 entity current = minigame.pp_curr_piece;
74 return true; // no current piece? allow the move anywhere
76 int number = minigame_tile_number(pos);
77 int letter = minigame_tile_letter(pos);
79 if( (pp_find_piece(minigame,minigame_tile_buildname(letter-1,number)) == current)
80 || (pp_find_piece(minigame,minigame_tile_buildname(letter+1,number)) == current)
81 || (pp_find_piece(minigame,minigame_tile_buildname(letter,number-1)) == current)
82 || (pp_find_piece(minigame,minigame_tile_buildname(letter,number+1)) == current)
83 || (pp_find_piece(minigame,minigame_tile_buildname(letter+1,number+1)) == current)
84 || (pp_find_piece(minigame,minigame_tile_buildname(letter-1,number-1)) == current)
85 || (pp_find_piece(minigame,minigame_tile_buildname(letter+1,number-1)) == current)
86 || (pp_find_piece(minigame,minigame_tile_buildname(letter-1,number+1)) == current)
93 void pp_move(entity minigame, entity player, string pos )
95 if ( minigame.minigame_flags & PP_TURN_PLACE )
96 if ( pos && player.team == (minigame.minigame_flags & PP_TURN_TEAM) )
98 if ( pp_valid_move(minigame,pos))
100 entity existing = pp_find_piece(minigame,pos);
102 if(existing && existing.team != 5)
104 if(existing.team == 1)
105 minigame.pp_team1_score++;
106 if(existing.team == 2)
107 minigame.pp_team2_score++;
110 if(minigame.pp_curr_piece)
112 minigame.pp_curr_piece.cnt = 0;
113 minigame.pp_curr_piece.team = 5;
114 minigame_server_sendflags(minigame.pp_curr_piece,MINIG_SF_ALL);
119 if(existing.netname) { strunzone(existing.netname); }
123 entity piece = msle_spawn(minigame,"minigame_board_piece");
125 piece.team = player.team; // temporary
126 piece.netname = strzone(pos);
127 minigame_server_sendflags(piece,MINIG_SF_ALL);
128 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
129 minigame.pp_nexteam = minigame_next_team(player.team,2);
130 minigame.pp_curr_piece = piece;
131 if ( pp_winning_piece(piece) )
133 if(minigame.pp_team1_score == minigame.pp_team2_score)
134 minigame.minigame_flags = PP_TURN_DRAW;
136 minigame.minigame_flags = PP_TURN_WIN | ((minigame.pp_team1_score > minigame.pp_team2_score) ? 1 : 2);
139 minigame.minigame_flags = PP_TURN_PLACE | minigame.pp_nexteam;
144 void pp_setup_pieces(entity minigame)
146 int i, t; // letter, number
147 for(i = 0; i < PP_LET_CNT; ++i)
148 for(t = 0; t < PP_NUM_CNT; ++t)
150 bool t2_true = ((i == 0 || i == 6) && t > 0 && t < 6);
151 bool t1_true = (i > 0 && i < 6 && (t == 0 || t == 6));
153 if(t1_true || t2_true)
155 entity piece = msle_spawn(minigame,"minigame_board_piece");
156 piece.team = ((t1_true) ? 1 : 2);
157 piece.netname = strzone(minigame_tile_buildname(i,t));
158 minigame_server_sendflags(piece,MINIG_SF_ALL);
159 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
163 minigame.pp_curr_piece = world;
166 // request a new match
167 void pp_next_match(entity minigame, entity player)
170 // on multiplayer matches, wait for both players to agree
171 if ( minigame.minigame_flags & (PP_TURN_WIN|PP_TURN_DRAW) )
173 minigame.minigame_flags = PP_TURN_NEXT | player.team;
174 minigame.SendFlags |= MINIG_SF_UPDATE;
176 else if ( (minigame.minigame_flags & PP_TURN_NEXT) &&
177 !( minigame.minigame_flags & player.team ) )
180 minigame.minigame_flags = PP_TURN_PLACE | minigame.pp_nexteam;
181 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
183 while ( ( e = findentity(e,owner,minigame) ) )
184 if ( e.classname == "minigame_board_piece" )
186 minigame.pp_team1_score = 0;
187 minigame.pp_team2_score = 0;
189 pp_setup_pieces(minigame);
196 // required function, handle server side events
197 int pp_server_event(entity minigame, string event, ...)
203 minigame.minigame_flags = (PP_TURN_PLACE | PP_TURN_TEAM1);
204 pp_setup_pieces(minigame);
210 while( (e = findentity(e, owner, minigame)) )
211 if(e.classname == "minigame_board_piece")
213 if(e.netname) { strunzone(e.netname); }
220 int pl_num = minigame_count_players(minigame);
222 // Don't allow more than 2 players
223 if(pl_num >= 2) { return false; }
225 // Get the right team
226 if(minigame.minigame_players)
227 return minigame_next_team(minigame.minigame_players.team, 2);
237 pp_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
240 pp_next_match(minigame,...(0,entity));
248 entity sent = ...(0,entity);
250 if ( sent.classname == "minigame" && (sf & MINIG_SF_UPDATE ) )
252 WriteByte(MSG_ENTITY,sent.pp_team1_score);
253 WriteByte(MSG_ENTITY,sent.pp_team2_score);
255 else if(sent.classname == "minigame_board_piece")
256 WriteByte(MSG_ENTITY,sent.cnt);
267 string pp_curr_pos; // identifier of the tile under the mouse
268 vector pp_boardpos; // HUD board position
269 vector pp_boardsize;// HUD board size
270 .int pp_checkwin; // Used to optimize checks to display a win
272 // Required function, draw the game board
273 void pp_hud_board(vector pos, vector mySize)
275 minigame_hud_fitsqare(pos, mySize);
277 pp_boardsize = mySize;
279 minigame_hud_simpleboard(pos,mySize,minigame_texture("pp/board"));
281 vector tile_size = minigame_hud_denormalize_size('1 1 0'/PP_TILE_SIZE,pos,mySize);
284 active_minigame.pp_curr_piece = world;
286 FOREACH_MINIGAME_ENTITY(e)
287 if(e.classname == "minigame_board_piece")
290 active_minigame.pp_curr_piece = e;
294 FOREACH_MINIGAME_ENTITY(e)
296 if ( e.classname == "minigame_board_piece" )
298 tile_pos = minigame_tile_pos(e.netname,PP_LET_CNT,PP_NUM_CNT);
299 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
301 vector tile_color = '1 1 1';
304 case 1: tile_color = '1 0.3 0.3'; break;
305 case 2: tile_color = '0.3 0.3 1'; break;
306 // 3, 4 coming later?
309 string tile_name = strcat("pp/piece",ftos(e.team));
310 if(e.team == 5) { tile_name = "pp/piece_taken"; }
312 if(e == active_minigame.pp_curr_piece)
314 tile_name = "pp/piece_current";
316 // draw the splat too
317 minigame_drawpic_centered( tile_pos,
318 minigame_texture("pp/piece_taken"),
319 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
322 minigame_drawpic_centered( tile_pos,
323 minigame_texture(tile_name),
324 tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
328 if ( (active_minigame.minigame_flags & PP_TURN_TEAM) == minigame_self.team )
329 if ( pp_valid_move(active_minigame, pp_curr_pos) )
331 tile_pos = minigame_tile_pos(pp_curr_pos,PP_LET_CNT,PP_NUM_CNT);
332 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
333 minigame_drawpic_centered( tile_pos,
334 minigame_texture("pp/piece_current"),
335 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
337 else if(pp_valid_tile(pp_curr_pos))
339 tile_pos = minigame_tile_pos(pp_curr_pos,PP_LET_CNT,PP_NUM_CNT);
340 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
341 minigame_drawpic_centered( tile_pos,
342 minigame_texture("pp/piece_selected"),
343 tile_size, '1 1 1', panel_fg_alpha / 2, DRAWFLAG_NORMAL );
346 if ( active_minigame.minigame_flags & PP_TURN_WIN )
348 vector winfs = hud_fontsize*2;
349 string playername = "";
350 FOREACH_MINIGAME_ENTITY(e)
351 if ( e.classname == "minigame_player" &&
352 e.team == (active_minigame.minigame_flags & PP_TURN_TEAM) )
353 playername = GetPlayerName(e.minigame_playerslot-1);
355 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
357 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
358 sprintf("%s^7 won the game!",playername),
359 winfs, 0, DRAWFLAG_NORMAL, 0.5);
361 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
363 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
364 sprintf("%s^7 won the game!",playername),
365 winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
370 // Required function, draw the game status panel
371 void pp_hud_status(vector pos, vector mySize)
375 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
376 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
381 vector player_fontsize = hud_fontsize * 1.75;
382 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
385 vector tile_size = '48 48 0';
388 if ( (active_minigame.minigame_flags&PP_TURN_TEAM) == 2 )
389 mypos_y += player_fontsize_y + ts_y;
390 drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
391 mypos_y += player_fontsize_y;
392 drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25,DRAWFLAG_ADDITIVE);
395 FOREACH_MINIGAME_ENTITY(e)
397 if ( e.classname == "minigame_player" )
399 vector tile_color = '1 1 1';
402 case 1: tile_color = '1 0.3 0.3'; break;
403 case 2: tile_color = '0.3 0.3 1'; break;
404 // 3, 4 coming later?
409 mypos_y += player_fontsize_y + ts_y;
410 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
411 GetPlayerName(e.minigame_playerslot-1),
412 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
414 mypos_y += player_fontsize_y;
416 minigame_texture(strcat("pp/piece",ftos(e.team))),
417 tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
419 mypos_x += tile_size_x;
421 if(e.team == 1) { myscore = active_minigame.pp_team1_score; }
422 if(e.team == 2) { myscore = active_minigame.pp_team2_score; }
424 drawstring(mypos,ftos(myscore),tile_size,
425 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
430 // Turn a set of flags into a help message
431 string pp_turn_to_string(int turnflags)
433 if ( turnflags & PP_TURN_DRAW )
436 if ( turnflags & PP_TURN_WIN )
438 if ( (turnflags&PP_TURN_TEAM) != minigame_self.team )
439 return _("You lost the game!\nSelect \"^1Next Match^7\" on the menu for a rematch!");
440 return _("You win!\nSelect \"^1Next Match^7\" on the menu to start a new match!");
443 if ( turnflags & PP_TURN_NEXT )
445 if ( (turnflags&PP_TURN_TEAM) != minigame_self.team )
446 return _("Select \"^1Next Match^7\" on the menu to start a new match!");
447 return _("Wait for your opponent to confirm the rematch");
450 if ( (turnflags & PP_TURN_TEAM) != minigame_self.team )
451 return _("Wait for your opponent to make their move");
453 if ( turnflags & PP_TURN_PLACE )
454 return _("Click on the game board to place your piece");
459 // Make the correct move
460 void pp_make_move(entity minigame)
462 if ( minigame.minigame_flags == (PP_TURN_PLACE|minigame_self.team) )
464 minigame_cmd("move ",pp_curr_pos);
468 void pp_set_curr_pos(string s)
471 strunzone(pp_curr_pos);
477 // Required function, handle client events
478 int pp_client_event(entity minigame, string event, ...)
485 minigame.message = pp_turn_to_string(minigame.minigame_flags);
490 if((minigame.minigame_flags & PP_TURN_TEAM) == minigame_self.team)
492 switch ( ...(0,int) )
495 case K_KP_RIGHTARROW:
497 pp_set_curr_pos("a3");
499 pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,1,0,PP_LET_CNT,PP_NUM_CNT));
504 pp_set_curr_pos("c3");
506 pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,-1,0,PP_LET_CNT,PP_NUM_CNT));
511 pp_set_curr_pos("a1");
513 pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,0,1,PP_LET_CNT,PP_NUM_CNT));
518 pp_set_curr_pos("a3");
520 pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,0,-1,PP_LET_CNT,PP_NUM_CNT));
525 pp_make_move(minigame);
532 case "mouse_pressed":
534 if(...(0,int) == K_MOUSE1)
536 pp_make_move(minigame);
544 vector mouse_pos = minigame_hud_normalize(mousepos,pp_boardpos,pp_boardsize);
545 if ( minigame.minigame_flags == (PP_TURN_PLACE|minigame_self.team) )
546 pp_set_curr_pos(minigame_tile_name(mouse_pos,PP_LET_CNT,PP_NUM_CNT));
547 if ( ! pp_valid_tile(pp_curr_pos) )
552 case "network_receive":
554 entity sent = ...(0,entity);
556 if ( sent.classname == "minigame" )
558 if ( sf & MINIG_SF_UPDATE )
560 sent.message = pp_turn_to_string(sent.minigame_flags);
561 if ( sent.minigame_flags & minigame_self.team )
563 sent.pp_team1_score = ReadByte();
564 sent.pp_team2_score = ReadByte();
567 else if(sent.classname == "minigame_board_piece")
569 sent.cnt = ReadByte();
571 minigame.pp_curr_piece = sent;
578 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
583 if(...(0,string) == "next")
584 minigame_cmd("next");