2 REGISTER_MINIGAME(pp, _("Push-Pull"));
4 const int PP_TURN_PLACE = 0x0100; // player has to place a piece on the board
5 const int PP_TURN_WIN = 0x0200; // player has won
6 const int PP_TURN_DRAW = 0x0400; // players have equal scores
7 const int PP_TURN_NEXT = 0x0800; // a player wants to start a new match
8 const int PP_TURN_TYPE = 0x0f00; // turn type mask
10 const int PP_TURN_TEAM1 = 0x0001;
11 const int PP_TURN_TEAM2 = 0x0002;
12 const int PP_TURN_TEAM = 0x000f; // turn team mask
14 const int PP_SPECTATOR_TEAM = 255; // must be above max teams and equal to or below 255
16 const int PP_LET_CNT = 7;
17 const int PP_NUM_CNT = 7;
19 const int PP_TILE_SIZE = 7;
28 .entity pp_curr_piece; // identifier for the current target piece
30 // find tic tac toe piece given its tile name
31 entity pp_find_piece(entity minig, string tile)
34 while ( ( e = findentity(e,owner,minig) ) )
35 if ( e.classname == "minigame_board_piece" && e.netname == tile )
40 // check if the tile name is valid (3x3 grid)
41 bool pp_valid_tile(string tile)
45 int number = minigame_tile_number(tile);
46 int letter = minigame_tile_letter(tile);
47 return 0 <= number && number < PP_NUM_CNT && 0 <= letter && letter < PP_LET_CNT;
50 // Checks if the given piece completes a row
51 bool pp_winning_piece(entity piece)
53 int number = minigame_tile_number(piece.netname);
54 int letter = minigame_tile_letter(piece.netname);
57 if(!pp_valid_tile(minigame_tile_buildname(letter-1,number)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter-1,number)).team == 5)
58 if(!pp_valid_tile(minigame_tile_buildname(letter+1,number)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter+1,number)).team == 5)
59 if(!pp_valid_tile(minigame_tile_buildname(letter,number-1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter,number-1)).team == 5)
60 if(!pp_valid_tile(minigame_tile_buildname(letter,number+1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter,number+1)).team == 5)
61 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)
62 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)
63 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)
64 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)
70 bool pp_valid_move(entity minigame, string pos)
72 if(!pp_valid_tile(pos))
74 if(pp_find_piece(minigame,pos).team == 5)
77 entity current = minigame.pp_curr_piece;
79 return true; // no current piece? allow the move anywhere
81 int number = minigame_tile_number(pos);
82 int letter = minigame_tile_letter(pos);
84 if( (pp_find_piece(minigame,minigame_tile_buildname(letter-1,number)) == current)
85 || (pp_find_piece(minigame,minigame_tile_buildname(letter+1,number)) == current)
86 || (pp_find_piece(minigame,minigame_tile_buildname(letter,number-1)) == current)
87 || (pp_find_piece(minigame,minigame_tile_buildname(letter,number+1)) == current)
88 || (pp_find_piece(minigame,minigame_tile_buildname(letter+1,number+1)) == current)
89 || (pp_find_piece(minigame,minigame_tile_buildname(letter-1,number-1)) == current)
90 || (pp_find_piece(minigame,minigame_tile_buildname(letter+1,number-1)) == current)
91 || (pp_find_piece(minigame,minigame_tile_buildname(letter-1,number+1)) == current)
98 void pp_move(entity minigame, entity player, string pos )
100 if ( minigame.minigame_flags & PP_TURN_PLACE )
101 if ( pos && player.team == (minigame.minigame_flags & PP_TURN_TEAM) )
103 if ( pp_valid_move(minigame,pos))
105 entity existing = pp_find_piece(minigame,pos);
107 if(existing && existing.team != 5)
109 if(existing.team == 1)
110 minigame.pp_team1_score++;
111 if(existing.team == 2)
112 minigame.pp_team2_score++;
115 if(minigame.pp_curr_piece)
117 minigame.pp_curr_piece.cnt = 0;
118 minigame.pp_curr_piece.team = 5;
119 minigame_server_sendflags(minigame.pp_curr_piece,MINIG_SF_ALL);
124 strfree(existing.netname);
128 entity piece = msle_spawn(minigame,new(minigame_board_piece));
130 piece.team = player.team; // temporary
131 piece.netname = strzone(pos);
132 minigame_server_sendflags(piece,MINIG_SF_ALL);
133 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
134 minigame.pp_nexteam = minigame_next_team(player.team,2);
135 minigame.pp_curr_piece = piece;
136 if ( pp_winning_piece(piece) )
138 if(minigame.pp_team1_score == minigame.pp_team2_score)
139 minigame.minigame_flags = PP_TURN_DRAW;
141 minigame.minigame_flags = PP_TURN_WIN | ((minigame.pp_team1_score > minigame.pp_team2_score) ? 1 : 2);
144 minigame.minigame_flags = PP_TURN_PLACE | minigame.pp_nexteam;
149 void pp_setup_pieces(entity minigame)
151 int i, t; // letter, number
152 for(i = 0; i < PP_LET_CNT; ++i)
153 for(t = 0; t < PP_NUM_CNT; ++t)
155 bool t2_true = ((i == 0 || i == 6) && t > 0 && t < 6);
156 bool t1_true = (i > 0 && i < 6 && (t == 0 || t == 6));
158 if(t1_true || t2_true)
160 entity piece = msle_spawn(minigame,new(minigame_board_piece));
161 piece.team = ((t1_true) ? 1 : 2);
162 piece.netname = strzone(minigame_tile_buildname(i,t));
163 minigame_server_sendflags(piece,MINIG_SF_ALL);
164 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
168 minigame.pp_curr_piece = NULL;
171 // request a new match
172 void pp_next_match(entity minigame, entity player)
175 // on multiplayer matches, wait for both players to agree
176 if ( minigame.minigame_flags & (PP_TURN_WIN|PP_TURN_DRAW) )
178 minigame.minigame_flags = PP_TURN_NEXT | player.team;
179 minigame.SendFlags |= MINIG_SF_UPDATE;
181 else if ( (minigame.minigame_flags & PP_TURN_NEXT) &&
182 !( minigame.minigame_flags & player.team ) )
185 minigame.minigame_flags = PP_TURN_PLACE | minigame.pp_nexteam;
186 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
188 while ( ( e = findentity(e,owner,minigame) ) )
189 if ( e.classname == "minigame_board_piece" )
191 minigame.pp_team1_score = 0;
192 minigame.pp_team2_score = 0;
194 pp_setup_pieces(minigame);
201 // required function, handle server side events
202 int pp_server_event(entity minigame, string event, ...)
208 minigame.minigame_flags = (PP_TURN_PLACE | PP_TURN_TEAM1);
209 pp_setup_pieces(minigame);
215 while( (e = findentity(e, owner, minigame)) )
216 if(e.classname == "minigame_board_piece")
225 int pl_num = minigame_count_players(minigame);
227 // Don't allow more than 2 players
228 if(pl_num >= 2) { return PP_SPECTATOR_TEAM; }
230 // Get the right team
231 if(minigame.minigame_players)
232 return minigame_next_team(minigame.minigame_players.team, 2);
239 entity player = ...(0,entity);
240 bool event_blocked = (player.team == PP_SPECTATOR_TEAM);
246 pp_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
251 pp_next_match(minigame,...(0,entity));
259 entity sent = ...(0,entity);
261 if ( sent.classname == "minigame" && (sf & MINIG_SF_UPDATE ) )
263 WriteByte(MSG_ENTITY,sent.pp_team1_score);
264 WriteByte(MSG_ENTITY,sent.pp_team2_score);
266 else if(sent.classname == "minigame_board_piece")
267 WriteByte(MSG_ENTITY,sent.cnt);
278 string pp_curr_pos; // identifier of the tile under the mouse
279 vector pp_boardpos; // HUD board position
280 vector pp_boardsize; // HUD board size
282 // Required function, draw the game board
283 void pp_hud_board(vector pos, vector mySize)
285 minigame_hud_fitsqare(pos, mySize);
287 pp_boardsize = mySize;
289 minigame_hud_simpleboard(pos,mySize,minigame_texture("pp/board"));
291 vector tile_size = minigame_hud_denormalize_size('1 1 0'/PP_TILE_SIZE,pos,mySize);
294 active_minigame.pp_curr_piece = NULL;
296 FOREACH_MINIGAME_ENTITY(e)
297 if(e.classname == "minigame_board_piece")
300 active_minigame.pp_curr_piece = e;
304 FOREACH_MINIGAME_ENTITY(e)
306 if ( e.classname == "minigame_board_piece" )
308 tile_pos = minigame_tile_pos(e.netname,PP_LET_CNT,PP_NUM_CNT);
309 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
311 vector tile_color = '1 1 1';
314 case 1: tile_color = '1 0.3 0.3'; break;
315 case 2: tile_color = '0.3 0.3 1'; break;
316 // 3, 4 coming later?
319 string tile_name = strcat("pp/piece",ftos(e.team));
320 if(e.team == 5) { tile_name = "pp/piece_taken"; }
322 if(e == active_minigame.pp_curr_piece)
324 tile_name = "pp/piece_current";
326 // draw the splat too
327 minigame_drawpic_centered( tile_pos,
328 minigame_texture("pp/piece_taken"),
329 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
332 minigame_drawpic_centered( tile_pos,
333 minigame_texture(tile_name),
334 tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
338 if ( (active_minigame.minigame_flags & PP_TURN_TEAM) == minigame_self.team )
339 if ( pp_valid_move(active_minigame, pp_curr_pos) )
341 tile_pos = minigame_tile_pos(pp_curr_pos,PP_LET_CNT,PP_NUM_CNT);
342 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
343 minigame_drawpic_centered( tile_pos,
344 minigame_texture("pp/piece_current"),
345 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
347 else if(pp_valid_tile(pp_curr_pos))
349 tile_pos = minigame_tile_pos(pp_curr_pos,PP_LET_CNT,PP_NUM_CNT);
350 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
351 minigame_drawpic_centered( tile_pos,
352 minigame_texture("pp/piece_selected"),
353 tile_size, '1 1 1', panel_fg_alpha / 2, DRAWFLAG_NORMAL );
356 if ( active_minigame.minigame_flags & PP_TURN_WIN )
358 vector winfs = hud_fontsize*2;
360 FOREACH_MINIGAME_ENTITY(e)
361 if ( e.classname == "minigame_player" &&
362 e.team == (active_minigame.minigame_flags & PP_TURN_TEAM) )
363 pname = entcs_GetName(e.minigame_playerslot-1);
365 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
367 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
368 sprintf(_("%s^7 won the game!"), pname),
369 winfs, 0, DRAWFLAG_NORMAL, 0.5);
371 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'1 1 1',0.5*panel_fg_alpha,DRAWFLAG_ADDITIVE);
373 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
374 sprintf(_("%s^7 won the game!"), pname),
375 winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
380 // Required function, draw the game status panel
381 void pp_hud_status(vector pos, vector mySize)
385 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
386 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
391 vector player_fontsize = hud_fontsize * 1.75;
392 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
395 vector tile_size = '48 48 0';
397 if(minigame_self.team != PP_SPECTATOR_TEAM)
400 if ( (active_minigame.minigame_flags&PP_TURN_TEAM) == 2 )
401 mypos_y += player_fontsize_y + ts_y;
402 drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5*panel_fg_alpha,DRAWFLAG_ADDITIVE);
403 mypos_y += player_fontsize_y;
404 drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25*panel_fg_alpha,DRAWFLAG_ADDITIVE);
408 FOREACH_MINIGAME_ENTITY(e)
410 if ( e.classname == "minigame_player" && e.team != PP_SPECTATOR_TEAM )
412 vector tile_color = '1 1 1';
415 case 1: tile_color = '1 0.3 0.3'; break;
416 case 2: tile_color = '0.3 0.3 1'; break;
417 // 3, 4 coming later?
422 mypos_y += player_fontsize_y + ts_y;
423 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
424 entcs_GetName(e.minigame_playerslot-1),
425 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
427 mypos_y += player_fontsize_y;
429 minigame_texture(strcat("pp/piece",ftos(e.team))),
430 tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
432 mypos_x += tile_size_x;
434 if(e.team == 1) { myscore = active_minigame.pp_team1_score; }
435 if(e.team == 2) { myscore = active_minigame.pp_team2_score; }
437 drawstring(mypos,ftos(myscore),tile_size,
438 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
443 // Turn a set of flags into a help message
444 string pp_turn_to_string(int turnflags)
446 if(minigame_self.team == PP_SPECTATOR_TEAM)
447 return _("You are spectating");
449 if ( turnflags & PP_TURN_DRAW )
452 if ( turnflags & PP_TURN_WIN )
454 // translator-friendly messages composed of 2 existing messages
455 if ( (turnflags & PP_TURN_TEAM) != minigame_self.team )
456 return strcat(_("You lost the game!"), "\n", _("Select \"^1Next Match^7\" on the menu for a rematch!"));
457 return strcat(_("You win!"), "\n", _("Select \"^1Next Match^7\" on the menu to start a new match!"));
460 if ( turnflags & PP_TURN_NEXT )
462 if ( (turnflags&PP_TURN_TEAM) != minigame_self.team )
463 return _("Select \"^1Next Match^7\" on the menu to start a new match!");
464 return _("Wait for your opponent to confirm the rematch");
467 if ( (turnflags & PP_TURN_TEAM) != minigame_self.team )
468 return _("Wait for your opponent to make their move");
470 if ( turnflags & PP_TURN_PLACE )
471 return _("Click on the game board to place your piece");
476 // Make the correct move
477 void pp_make_move(entity minigame)
479 if ( minigame.minigame_flags == (PP_TURN_PLACE|minigame_self.team) )
481 minigame_cmd("move ",pp_curr_pos);
485 void pp_set_curr_pos(string s)
487 strfree(pp_curr_pos);
493 // Required function, handle client events
494 int pp_client_event(entity minigame, string event, ...)
501 strcpy(minigame.message, pp_turn_to_string(minigame.minigame_flags));
506 strfree(minigame.message);
512 bool event_blocked = ((event == "key_released")
513 || ((minigame.minigame_flags & PP_TURN_TEAM) != minigame_self.team));
514 if (!(minigame.minigame_flags & PP_TURN_WIN) && !(minigame.minigame_flags & PP_TURN_DRAW))
516 switch ( ...(0,int) )
519 case K_KP_RIGHTARROW:
523 pp_set_curr_pos("a3");
525 pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,1,0,PP_LET_CNT,PP_NUM_CNT));
532 pp_set_curr_pos("c3");
534 pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,-1,0,PP_LET_CNT,PP_NUM_CNT));
541 pp_set_curr_pos("a1");
543 pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,0,1,PP_LET_CNT,PP_NUM_CNT));
550 pp_set_curr_pos("a3");
552 pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,0,-1,PP_LET_CNT,PP_NUM_CNT));
559 pp_make_move(minigame);
566 case "mouse_pressed":
568 if(...(0,int) == K_MOUSE1)
570 pp_client_event(minigame, "mouse_moved");
571 pp_make_move(minigame);
579 vector mouse_pos = minigame_hud_normalize(mousepos,pp_boardpos,pp_boardsize);
580 if ( minigame.minigame_flags == (PP_TURN_PLACE|minigame_self.team) )
581 pp_set_curr_pos(minigame_tile_name(mouse_pos,PP_LET_CNT,PP_NUM_CNT));
582 if ( ! pp_valid_tile(pp_curr_pos) )
587 case "network_receive":
589 entity sent = ...(0,entity);
591 if ( sent.classname == "minigame" )
593 if ( sf & MINIG_SF_UPDATE )
595 strcpy(sent.message, pp_turn_to_string(sent.minigame_flags));
596 if ( sent.minigame_flags & minigame_self.team )
598 sent.pp_team1_score = ReadByte();
599 sent.pp_team2_score = ReadByte();
602 else if(sent.classname == "minigame_board_piece")
604 sent.cnt = ReadByte();
606 minigame.pp_curr_piece = sent;
613 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
618 if(...(0,string) == "next")
619 minigame_cmd("next");