]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/minigames/minigame/pong.qc
Merge branch 'terencehill/minigame_flood_control' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigame / pong.qc
index 55d41617da9c387a0146cb01c53a190751295089..b6c0988c7d5682c9871dc60dd25c47ead69fd9e6 100644 (file)
@@ -1,5 +1,5 @@
 #include "pong.qh"
-REGISTER_MINIGAME(pong, "Pong");
+REGISTER_MINIGAME(pong, _("Pong"));
 
 // minigame flags
 const int PONG_STATUS_WAIT = 0x0010; // waiting for players to join
@@ -18,6 +18,7 @@ const int PONG_KEY_BOTH     = 0x03; // Player jamming keys at ramdom
 
 // fields
 const int PONG_MAX_PLAYERS = 4;
+const int PONG_SPECTATOR_TEAM = 255; // must be above max teams and equal to or below 255
 .int    pong_score;                    // (minigame_player) number of goals
 .int    pong_keys;                     // (client) pressed keys
 .entity pong_paddles[PONG_MAX_PLAYERS];// (minigame) paddles
@@ -123,9 +124,14 @@ bool pong_paddle_hit(entity ball, int pteam)
        entity paddle = ball.owner.pong_paddles[pteam-1];
        if (!paddle)
                return false;
-       vector near_point = box_nearest(paddle.mins+paddle.origin,
-                                                                       paddle.maxs+paddle.origin, ball.origin);
+
+#if 1
+       vector near_point = box_nearest(paddle.m_mins+paddle.origin,
+                                                                       paddle.m_maxs+paddle.origin, ball.origin);
        return vdist(near_point - ball.origin, <=, ball.pong_length);
+#else
+       return boxesoverlap(paddle.m_mins + paddle.origin, paddle.m_maxs + paddle.origin, ball.m_mins + ball.origin, ball.m_maxs + ball.origin);
+#endif
 }
 
 // Checks for a goal, when that happes adds scores and resets the ball
@@ -156,7 +162,7 @@ void pong_ball_think(entity this)
        this.SendFlags |= MINIG_SF_UPDATE;
 
        int i;
-       for ( i = 1; i <= PONG_MAX_PLAYERS; i++ )
+       for ( i = 1; i <= PONG_MAX_PLAYERS; ++i )
                if ( pong_paddle_hit(this, i) )
                {
                        pong_paddle_bounce(this,i);
@@ -254,7 +260,7 @@ void pong_ai_think(entity this)
 
 entity pong_ai_spawn(entity paddle)
 {
-       entity ai = msle_spawn(paddle.owner,"pong_ai");
+       entity ai = msle_spawn(paddle.owner,new(pong_ai));
        ai.minigame_players = ai;
        ai.team = paddle.team;
        setthink(ai, pong_ai_think);
@@ -275,16 +281,16 @@ void pong_paddle_think(entity this)
        if ( this.realowner.minigame_players.pong_keys == PONG_KEY_INCREASE ||
                 this.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
        {
-               float movement = autocvar_sv_minigames_pong_paddle_speed * think_speed;
+               float pmovement = autocvar_sv_minigames_pong_paddle_speed * think_speed;
                float halflen = this.pong_length/2;
 
                if ( this.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
-                       movement *= -1;
+                       pmovement *= -1;
 
                if ( this.team > 2 )
-                       this.origin_x = bound(halflen, this.origin_x+movement, 1-halflen);
+                       this.origin_x = bound(halflen, this.origin_x+pmovement, 1-halflen);
                else
-                       this.origin_y = bound(halflen, this.origin_y+movement, 1-halflen);
+                       this.origin_y = bound(halflen, this.origin_y+pmovement, 1-halflen);
 
                this.SendFlags |= MINIG_SF_UPDATE;
        }
@@ -313,14 +319,14 @@ vector pong_team_to_paddlepos(int nteam)
 // if real_player is NULL, the paddle is controlled by AI
 entity pong_paddle_spawn(entity minigame, int pl_team, entity real_player)
 {
-       entity paddle = msle_spawn(minigame,"pong_paddle");
+       entity paddle = msle_spawn(minigame,new(pong_paddle));
        paddle.pong_length = autocvar_sv_minigames_pong_paddle_size;
        paddle.origin = pong_team_to_paddlepos(pl_team);
        setthink(paddle, pong_paddle_think);
        paddle.nextthink = time;
        paddle.team = pl_team;
-       paddle.mins = pong_team_to_box_halfsize(pl_team,-paddle.pong_length,-1/16);
-       paddle.maxs = pong_team_to_box_halfsize(pl_team,paddle.pong_length,1/16);
+       paddle.m_mins = pong_team_to_box_halfsize(pl_team,-paddle.pong_length,-1/16);
+       paddle.m_maxs = pong_team_to_box_halfsize(pl_team,paddle.pong_length,1/16);
 
        if ( real_player == NULL )
                pong_ai_spawn(paddle);
@@ -347,11 +353,11 @@ int pong_server_event(entity minigame, string event, ...)
                {
                        // Don't allow joining a match that is already running
                        if ( minigame.minigame_flags & PONG_STATUS_PLAY )
-                               return false;
+                               return PONG_SPECTATOR_TEAM;
 
                        entity player = ...(0,entity);
                        int i;
-                       for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
+                       for ( i = 0; i < PONG_MAX_PLAYERS; ++i )
                        {
                                if ( minigame.pong_paddles[i] == NULL )
                                {
@@ -360,7 +366,7 @@ int pong_server_event(entity minigame, string event, ...)
                                }
                        }
 
-                       return false;
+                       return PONG_SPECTATOR_TEAM;
                }
                case "part":
                {
@@ -368,7 +374,7 @@ int pong_server_event(entity minigame, string event, ...)
                        entity paddle;
                        entity ai;
                        int i;
-                       for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
+                       for ( i = 0; i < PONG_MAX_PLAYERS; ++i )
                        {
                                paddle = minigame.pong_paddles[i];
                                if ( paddle != NULL && paddle.realowner == player )
@@ -384,46 +390,69 @@ int pong_server_event(entity minigame, string event, ...)
                case "cmd":
                {
                        entity player = ...(0,entity);
+                       bool event_blocked = (player.team == PONG_SPECTATOR_TEAM);
                        switch(argv(0))
                        {
                                case "throw":
+                                       if(event_blocked)
+                                               return true;
                                        if ( minigame.minigame_flags & PONG_STATUS_WAIT )
                                        {
                                                minigame.minigame_flags = PONG_STATUS_PLAY |
                                                        (minigame.minigame_flags & ~PONG_STATUS_WAIT);
                                                minigame.SendFlags |= MINIG_SF_UPDATE;
 
-                                               int i;
                                                entity ball;
-                                               for ( i = 0; i < autocvar_sv_minigames_pong_ball_number; i++ )
+                                               for ( int j = 0; j < autocvar_sv_minigames_pong_ball_number; ++j )
                                                {
-                                                       ball = msle_spawn(minigame,"pong_ball");
+                                                       ball = msle_spawn(minigame,new(pong_ball));
                                                        ball.pong_length = autocvar_sv_minigames_pong_ball_radius;
+                                                       ball.m_mins = vec2(-ball.pong_length, -ball.pong_length);
+                                                       ball.m_maxs = vec2(ball.pong_length, ball.pong_length);
                                                        pong_ball_reset(ball);
                                                }
                                        }
                                        return true;
                                case "+movei":
+                                       if(event_blocked)
+                                               return true;
                                        player.pong_keys |= PONG_KEY_INCREASE;
                                        return true;
                                case "+moved":
+                                       if(event_blocked)
+                                               return true;
                                        player.pong_keys |= PONG_KEY_DECREASE;
                                        return true;
                                case "-movei":
+                                       if(event_blocked)
+                                               return true;
                                        player.pong_keys &= ~PONG_KEY_INCREASE;
                                        return true;
                                case "-moved":
+                                       if(event_blocked)
+                                               return true;
                                        player.pong_keys &= ~PONG_KEY_DECREASE;
                                        return true;
+                               case "move":
+                                       if(event_blocked)
+                                               return true;
+                                       if(argv(1))
+                                               player.pong_keys = stoi(argv(1));
+                                       return true;
                                case "pong_aimore":
                                {
-                                       int i;
+                                       if(event_blocked)
+                                               return true;
+                                       // keep declaration here, moving it into for() reverses weapon order
+                                       // potentially compiler bug
+                                       int j;
                                        if ( minigame.minigame_flags & PONG_STATUS_WAIT )
-                                               for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
+                                               for ( j = 0; j < PONG_MAX_PLAYERS; ++j )
+                                               //for ( int j = 0; j < PONG_MAX_PLAYERS; ++j )
                                                {
-                                                       if ( minigame.pong_paddles[i] == NULL )
+                                                       if ( minigame.pong_paddles[j] == NULL )
                                                        {
-                                                               pong_paddle_spawn(minigame,i+1,NULL);
+                                                               pong_paddle_spawn(minigame,j+1,NULL);
                                                                return true;
                                                        }
                                                }
@@ -432,17 +461,18 @@ int pong_server_event(entity minigame, string event, ...)
                                }
                                case "pong_ailess":
                                {
+                                       if(event_blocked)
+                                               return true;
                                        if ( minigame.minigame_flags & PONG_STATUS_WAIT )
                                        {
                                                entity paddle;
-                                               int i;
-                                               for ( i = PONG_MAX_PLAYERS-1; i >= 0; i-- )
+                                               for ( int j = PONG_MAX_PLAYERS-1; j >= 0; --j )
                                                {
-                                                       paddle = minigame.pong_paddles[i];
+                                                       paddle = minigame.pong_paddles[j];
                                                        if ( paddle != NULL &&
                                                                paddle.realowner.classname == "pong_ai" )
                                                        {
-                                                               minigame.pong_paddles[i] = NULL;
+                                                               minigame.pong_paddles[j] = NULL;
                                                                delete(paddle.realowner);
                                                                delete(paddle);
                                                                return true;
@@ -499,9 +529,16 @@ vector pong_team_to_color(int nteam)
        }
 }
 
+int pong_keys_pressed;
+int pong_keys_pressed_old;
+
 // Required function, draw the game board
 void pong_hud_board(vector pos, vector mySize)
 {
+       if(pong_keys_pressed != pong_keys_pressed_old)
+               minigame_cmd(strcat("move ", itos(pong_keys_pressed)));
+       pong_keys_pressed_old = pong_keys_pressed;
+
        minigame_hud_fitsqare(pos, mySize);
        minigame_hud_simpleboard(pos,mySize,minigame_texture("pong/board"));
 
@@ -538,6 +575,8 @@ void pong_hud_board(vector pos, vector mySize)
 
                }
        }
+
+       minigame_show_allspecs(pos, mySize);
 }
 
 // Required function, draw the game status panel
@@ -559,12 +598,12 @@ void pong_hud_status(vector pos, vector mySize)
        entity e;
        FOREACH_MINIGAME_ENTITY(e)
        {
-               if ( e.classname == "minigame_player" || e.classname == "pong_ai" )
+               if ( (e.classname == "minigame_player" || e.classname == "pong_ai") && e.team != PONG_SPECTATOR_TEAM )
                {
                        mypos = pos;
                        mypos_y  += (e.team-1) * (player_fontsize_y + ts_y);
 
-                       drawfill(mypos, ts, pong_team_to_color(e.team), 0.25, DRAWFLAG_ADDITIVE);
+                       drawfill(mypos, ts, pong_team_to_color(e.team), 0.25 * panel_fg_alpha, DRAWFLAG_ADDITIVE);
 
                        minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
                                (e.minigame_playerslot ? entcs_GetName(e.minigame_playerslot-1) : _("AI")),
@@ -583,7 +622,9 @@ void pong_hud_status(vector pos, vector mySize)
 string pong_message(int mgflags)
 {
        string rmessage = "";
-       if (mgflags & PONG_STATUS_WAIT)
+       if(minigame_self.team == PONG_SPECTATOR_TEAM)
+               rmessage = _("You are spectating");
+       else if (mgflags & PONG_STATUS_WAIT)
                rmessage = _("Press ^1Start Match^7 to start the match with the current players");
        return rmessage;
 }
@@ -595,39 +636,47 @@ int pong_client_event(entity minigame, string event, ...)
        {
                case "activate":
                        return false;
-               case "key_pressed":
-                       switch ( ...(0,int) )
-                       {
-                               case K_UPARROW:
-                               case K_KP_UPARROW:
-                               case K_LEFTARROW:
-                               case K_KP_LEFTARROW:
-                                       minigame_cmd("+moved");
-                                       return true;
-                               case K_DOWNARROW:
-                               case K_KP_DOWNARROW:
-                               case K_RIGHTARROW:
-                               case K_KP_RIGHTARROW:
-                                       minigame_cmd("+movei");
-                                       return true;
-                       }
+               case "deactivate":
+               {
+                       strfree(minigame.message);
                        return false;
+               }
+               case "key_pressed":
                case "key_released":
-                       switch ( ...(0,int) )
-                       {
-                               case K_UPARROW:
-                               case K_KP_UPARROW:
-                               case K_LEFTARROW:
-                               case K_KP_LEFTARROW:
-                                       minigame_cmd("-moved");
-                                       return true;
-                               case K_DOWNARROW:
-                               case K_KP_DOWNARROW:
-                               case K_RIGHTARROW:
-                               case K_KP_RIGHTARROW:
-                                       minigame_cmd("-movei");
-                                       return true;
-                       }
+                       if ((minigame.minigame_flags & PONG_STATUS_PLAY) && minigame_self.team != PONG_SPECTATOR_TEAM)
+                               switch ( ...(0,int) )
+                               {
+                                       case K_UPARROW:
+                                       case K_KP_UPARROW:
+                                       case K_LEFTARROW:
+                                       case K_KP_LEFTARROW:
+                                               if (event == "key_pressed")
+                                               {
+                                                       //minigame_cmd("+moved");
+                                                       pong_keys_pressed |= PONG_KEY_DECREASE;
+                                               }
+                                               else
+                                               {
+                                                       //minigame_cmd("-moved");
+                                                       pong_keys_pressed &= ~PONG_KEY_DECREASE;
+                                               }
+                                               return true;
+                                       case K_DOWNARROW:
+                                       case K_KP_DOWNARROW:
+                                       case K_RIGHTARROW:
+                                       case K_KP_RIGHTARROW:
+                                               if (event == "key_pressed")
+                                               {
+                                                       //minigame_cmd("+movei");
+                                                       pong_keys_pressed |= PONG_KEY_INCREASE;
+                                               }
+                                               else
+                                               {
+                                                       //minigame_cmd("-movei");
+                                                       pong_keys_pressed &= ~PONG_KEY_INCREASE;
+                                               }
+                                               return true;
+                               }
                        return false;
                case "network_receive":
                {
@@ -641,7 +690,7 @@ int pong_client_event(entity minigame, string event, ...)
                        {
                                if ( sf & MINIG_SF_UPDATE )
                                {
-                                       sent.message = pong_message(sent.minigame_flags);
+                                       strcpy(sent.message, pong_message(sent.minigame_flags));
                                }
                        }
                        return false;
@@ -656,7 +705,7 @@ int pong_client_event(entity minigame, string event, ...)
                case "menu_click":
                {
                        string cmd = ...(0,string);
-                       if( cmd == "pong_throw" && minigame.minigame_flags & PONG_STATUS_WAIT )
+                       if( cmd == "pong_throw" && ( minigame.minigame_flags & PONG_STATUS_WAIT ) )
                        {
                                minigame_cmd("throw");
                        }