]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/minigames/minigame/pp.qc
Merge branch 'master' into terencehill/min_spec_time
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigame / pp.qc
index b81ce7a9cccd562ff0f034dce669e2034bd70613..0b1d74344e93f09fbe00877ecc2c8e6818c328ed 100644 (file)
@@ -1,3 +1,6 @@
+#include "pp.qh"
+REGISTER_MINIGAME(pp, "Push-Pull");
+
 const int PP_TURN_PLACE = 0x0100; // player has to place a piece on the board
 const int PP_TURN_WIN   = 0x0200; // player has won
 const int PP_TURN_DRAW  = 0x0400; // players have equal scores
@@ -10,6 +13,13 @@ const int PP_TURN_TEAM  = 0x000f; // turn team mask
 
 const int PP_BLOCKED_TEAM = 5; // there won't ever be a 5th team, so we can abuse this
 
+const int PP_LET_CNT = 7;
+const int PP_NUM_CNT = 7;
+
+const int PP_TILE_SIZE = 7;
+
+.int cnt;
+
 .int pp_team1_score;
 .int pp_team2_score;
 
@@ -20,11 +30,11 @@ const int PP_BLOCKED_TEAM = 5; // there won't ever be a 5th team, so we can abus
 // find tic tac toe piece given its tile name
 entity pp_find_piece(entity minig, string tile)
 {
-       entity e = world;
+       entity e = NULL;
        while ( ( e = findentity(e,owner,minig) ) )
                if ( e.classname == "minigame_board_piece" && e.netname == tile )
                        return e;
-       return world;
+       return NULL;
 }
 
 // check if the tile name is valid (3x3 grid)
@@ -34,7 +44,7 @@ bool pp_valid_tile(string tile)
                return 0;
        int number = minigame_tile_number(tile);
        int letter = minigame_tile_letter(tile);
-       return 0 <= number && number < 7 && 0 <= letter && letter < 7;
+       return 0 <= number && number < PP_NUM_CNT && 0 <= letter && letter < PP_LET_CNT;
 }
 
 // Checks if the given piece completes a row
@@ -53,7 +63,7 @@ bool pp_winning_piece(entity piece)
        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)
        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)
                return true;
-       
+
        return false;
 }
 
@@ -111,8 +121,8 @@ void pp_move(entity minigame, entity player, string pos )
 
                        if(existing)
                        {
-                               if(existing.netname) { strunzone(existing.netname); }
-                               remove(existing);
+                               strfree(existing.netname);
+                               delete(existing);
                        }
 
                        entity piece = msle_spawn(minigame,"minigame_board_piece");
@@ -139,8 +149,8 @@ void pp_move(entity minigame, entity player, string pos )
 void pp_setup_pieces(entity minigame)
 {
        int i, t; // letter, number
-       for(i = 0; i < 7; ++i)
-       for(t = 0; t < 7; ++t)
+       for(i = 0; i < PP_LET_CNT; ++i)
+       for(t = 0; t < PP_NUM_CNT; ++t)
        {
                bool t2_true = ((i == 0 || i == 6) && t > 0 && t < 6);
                bool t1_true = (i > 0 && i < 6 && (t == 0 || t == 6));
@@ -155,7 +165,7 @@ void pp_setup_pieces(entity minigame)
                }
        }
 
-       minigame.pp_curr_piece = world;
+       minigame.pp_curr_piece = NULL;
 }
 
 // request a new match
@@ -174,10 +184,10 @@ void pp_next_match(entity minigame, entity player)
        {
                minigame.minigame_flags = PP_TURN_PLACE | minigame.pp_nexteam;
                minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
-               entity e = world;
+               entity e = NULL;
                while ( ( e = findentity(e,owner,minigame) ) )
                        if ( e.classname == "minigame_board_piece" )
-                               remove(e);
+                               delete(e);
                minigame.pp_team1_score = 0;
                minigame.pp_team2_score = 0;
 
@@ -201,12 +211,12 @@ int pp_server_event(entity minigame, string event, ...)
                }
                case "end":
                {
-                       entity e = world;
+                       entity e = NULL;
                        while( (e = findentity(e, owner, minigame)) )
                        if(e.classname == "minigame_board_piece")
                        {
-                               if(e.netname) { strunzone(e.netname); }
-                               remove(e);
+                               strfree(e.netname);
+                               delete(e);
                        }
                        return false;
                }
@@ -228,8 +238,8 @@ int pp_server_event(entity minigame, string event, ...)
                {
                        switch(argv(0))
                        {
-                               case "move": 
-                                       pp_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null ); 
+                               case "move":
+                                       pp_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
                                        return true;
                                case "next":
                                        pp_next_match(minigame,...(0,entity));
@@ -252,7 +262,7 @@ int pp_server_event(entity minigame, string event, ...)
                        return false;
                }
        }
-       
+
        return false;
 }
 
@@ -261,8 +271,7 @@ int pp_server_event(entity minigame, string event, ...)
 
 string pp_curr_pos; // identifier of the tile under the mouse
 vector pp_boardpos; // HUD board position
-vector pp_boardsize;// HUD board size
-.int pp_checkwin; // Used to optimize checks to display a win
+vector pp_boardsize; // HUD board size
 
 // Required function, draw the game board
 void pp_hud_board(vector pos, vector mySize)
@@ -270,13 +279,13 @@ void pp_hud_board(vector pos, vector mySize)
        minigame_hud_fitsqare(pos, mySize);
        pp_boardpos = pos;
        pp_boardsize = mySize;
-       
+
        minigame_hud_simpleboard(pos,mySize,minigame_texture("pp/board"));
 
-       vector tile_size = minigame_hud_denormalize_size('1 1 0'/7,pos,mySize);
+       vector tile_size = minigame_hud_denormalize_size('1 1 0'/PP_TILE_SIZE,pos,mySize);
        vector tile_pos;
 
-       active_minigame.pp_curr_piece = world;
+       active_minigame.pp_curr_piece = NULL;
        entity e;
        FOREACH_MINIGAME_ENTITY(e)
        if(e.classname == "minigame_board_piece")
@@ -290,7 +299,7 @@ void pp_hud_board(vector pos, vector mySize)
        {
                if ( e.classname == "minigame_board_piece" )
                {
-                       tile_pos = minigame_tile_pos(e.netname,7,7);
+                       tile_pos = minigame_tile_pos(e.netname,PP_LET_CNT,PP_NUM_CNT);
                        tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
 
                        vector tile_color = '1 1 1';
@@ -300,7 +309,7 @@ void pp_hud_board(vector pos, vector mySize)
                                case 2: tile_color = '0.3 0.3 1'; break;
                                // 3, 4 coming later?
                        }
-                       
+
                        string tile_name = strcat("pp/piece",ftos(e.team));
                        if(e.team == 5) { tile_name = "pp/piece_taken"; }
 
@@ -309,12 +318,12 @@ void pp_hud_board(vector pos, vector mySize)
                                tile_name = "pp/piece_current";
 
                                // draw the splat too
-                               minigame_drawpic_centered( tile_pos,  
+                               minigame_drawpic_centered( tile_pos,
                                                minigame_texture("pp/piece_taken"),
                                                tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
                        }
 
-                       minigame_drawpic_centered( tile_pos,  
+                       minigame_drawpic_centered( tile_pos,
                                        minigame_texture(tile_name),
                                        tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
                }
@@ -323,17 +332,17 @@ void pp_hud_board(vector pos, vector mySize)
        if ( (active_minigame.minigame_flags & PP_TURN_TEAM) == minigame_self.team )
        if ( pp_valid_move(active_minigame, pp_curr_pos) )
        {
-               tile_pos = minigame_tile_pos(pp_curr_pos,7,7);
+               tile_pos = minigame_tile_pos(pp_curr_pos,PP_LET_CNT,PP_NUM_CNT);
                tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
-               minigame_drawpic_centered( tile_pos,  
+               minigame_drawpic_centered( tile_pos,
                                minigame_texture("pp/piece_current"),
                                tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
        }
        else if(pp_valid_tile(pp_curr_pos))
        {
-               tile_pos = minigame_tile_pos(pp_curr_pos,7,7);
+               tile_pos = minigame_tile_pos(pp_curr_pos,PP_LET_CNT,PP_NUM_CNT);
                tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
-               minigame_drawpic_centered( tile_pos,  
+               minigame_drawpic_centered( tile_pos,
                                minigame_texture("pp/piece_selected"),
                                tile_size, '1 1 1', panel_fg_alpha / 2, DRAWFLAG_NORMAL );
        }
@@ -341,22 +350,22 @@ void pp_hud_board(vector pos, vector mySize)
        if ( active_minigame.minigame_flags & PP_TURN_WIN )
        {
                vector winfs = hud_fontsize*2;
-               string playername = "";
+               string pname = "";
                FOREACH_MINIGAME_ENTITY(e)
-                       if ( e.classname == "minigame_player" && 
+                       if ( e.classname == "minigame_player" &&
                                        e.team == (active_minigame.minigame_flags & PP_TURN_TEAM) )
-                               playername = GetPlayerName(e.minigame_playerslot-1);
-               
+                               pname = entcs_GetName(e.minigame_playerslot-1);
+
                vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
                vector win_sz;
                win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
-                       sprintf("%s^7 won the game!",playername), 
+                       sprintf("%s^7 won the game!",pname),
                        winfs, 0, DRAWFLAG_NORMAL, 0.5);
-               
+
                drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
-               
+
                minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
-                       sprintf("%s^7 won the game!",playername), 
+                       sprintf("%s^7 won the game!",pname),
                        winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
        }
 }
@@ -365,14 +374,14 @@ void pp_hud_board(vector pos, vector mySize)
 // Required function, draw the game status panel
 void pp_hud_status(vector pos, vector mySize)
 {
-       HUD_Panel_DrawBg(1);
+       HUD_Panel_DrawBg();
        vector ts;
        ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
                hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
-       
+
        pos_y += ts_y;
        mySize_y -= ts_y;
-       
+
        vector player_fontsize = hud_fontsize * 1.75;
        ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
        ts_x = mySize_x;
@@ -403,19 +412,19 @@ void pp_hud_status(vector pos, vector mySize)
                        if ( e.team == 2 )
                                mypos_y  += player_fontsize_y + ts_y;
                        minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
-                               GetPlayerName(e.minigame_playerslot-1),
+                               entcs_GetName(e.minigame_playerslot-1),
                                player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
-                       
+
                        mypos_y += player_fontsize_y;
-                       drawpic( mypos,  
+                       drawpic( mypos,
                                        minigame_texture(strcat("pp/piece",ftos(e.team))),
                                        tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
-                       
+
                        mypos_x += tile_size_x;
                        int myscore = 0;
                        if(e.team == 1) { myscore = active_minigame.pp_team1_score; }
                        if(e.team == 2) { myscore = active_minigame.pp_team2_score; }
-                       
+
                        drawstring(mypos,ftos(myscore),tile_size,
                                           '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
                }
@@ -427,27 +436,27 @@ string pp_turn_to_string(int turnflags)
 {
        if ( turnflags & PP_TURN_DRAW )
                return _("Draw");
-       
+
        if ( turnflags & PP_TURN_WIN )
        {
                if ( (turnflags&PP_TURN_TEAM) != minigame_self.team )
                        return _("You lost the game!\nSelect \"^1Next Match^7\" on the menu for a rematch!");
                return _("You win!\nSelect \"^1Next Match^7\" on the menu to start a new match!");
        }
-       
+
        if ( turnflags & PP_TURN_NEXT )
        {
                if ( (turnflags&PP_TURN_TEAM) != minigame_self.team )
                        return _("Select \"^1Next Match^7\" on the menu to start a new match!");
                return _("Wait for your opponent to confirm the rematch");
        }
-       
+
        if ( (turnflags & PP_TURN_TEAM) != minigame_self.team )
                return _("Wait for your opponent to make their move");
-       
+
        if ( turnflags & PP_TURN_PLACE )
                return _("Click on the game board to place your piece");
-       
+
        return "";
 }
 
@@ -462,8 +471,7 @@ void pp_make_move(entity minigame)
 
 void pp_set_curr_pos(string s)
 {
-       if ( pp_curr_pos )
-               strunzone(pp_curr_pos);
+       strfree(pp_curr_pos);
        if ( s )
                s = strzone(s);
        pp_curr_pos = s;
@@ -491,28 +499,28 @@ int pp_client_event(entity minigame, string event, ...)
                                                if ( ! pp_curr_pos )
                                                        pp_set_curr_pos("a3");
                                                else
-                                                       pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,1,0,7,7));
+                                                       pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,1,0,PP_LET_CNT,PP_NUM_CNT));
                                                return true;
                                        case K_LEFTARROW:
                                        case K_KP_LEFTARROW:
                                                if ( ! pp_curr_pos )
                                                        pp_set_curr_pos("c3");
                                                else
-                                                       pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,-1,0,7,7));
+                                                       pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,-1,0,PP_LET_CNT,PP_NUM_CNT));
                                                return true;
                                        case K_UPARROW:
                                        case K_KP_UPARROW:
                                                if ( ! pp_curr_pos )
                                                        pp_set_curr_pos("a1");
                                                else
-                                                       pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,0,1,7,7));
+                                                       pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,0,1,PP_LET_CNT,PP_NUM_CNT));
                                                return true;
                                        case K_DOWNARROW:
                                        case K_KP_DOWNARROW:
                                                if ( ! pp_curr_pos )
                                                        pp_set_curr_pos("a3");
                                                else
-                                                       pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,0,-1,7,7));
+                                                       pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,0,-1,PP_LET_CNT,PP_NUM_CNT));
                                                return true;
                                        case K_ENTER:
                                        case K_KP_ENTER:
@@ -538,7 +546,7 @@ int pp_client_event(entity minigame, string event, ...)
                {
                        vector mouse_pos = minigame_hud_normalize(mousepos,pp_boardpos,pp_boardsize);
                        if ( minigame.minigame_flags == (PP_TURN_PLACE|minigame_self.team) )
-                               pp_set_curr_pos(minigame_tile_name(mouse_pos,7,7));
+                               pp_set_curr_pos(minigame_tile_name(mouse_pos,PP_LET_CNT,PP_NUM_CNT));
                        if ( ! pp_valid_tile(pp_curr_pos) )
                                pp_set_curr_pos("");
 
@@ -584,4 +592,4 @@ int pp_client_event(entity minigame, string event, ...)
        return false;
 }
 
-#endif
\ No newline at end of file
+#endif