]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/minigames/minigame/pong.qc
Remove SELFPARAM() from .think and .touch
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigame / pong.qc
index ac96960178ce4ff2e28c507948bbc097b049b400..4bcb8c51446ee77020731131ee95bc2e9bc7ef9e 100644 (file)
+REGISTER_MINIGAME(pong, "Pong");
+
+// minigame flags
+const int PONG_STATUS_WAIT = 0x0010; // waiting for players to join
+const int PONG_STATUS_PLAY = 0x0020; // playing
+
 // send flags
-const int PONG_SF_PLAYERSCORE  = MINIG_SF_CUSTOM;   // sent when reporting scores
-const int PONG_SF_SINGLEPLAYER = MINIG_SF_CUSTOM<<1;// send minigame.pong_ai
+// (minigame_player) sent when reporting scores
+const int PONG_SF_PLAYERSCORE = MINIG_SF_CUSTOM;
+// (pong_ball) sent when changing team
+const int PONG_SF_BALLTEAM = MINIG_SF_CUSTOM;
+
+// keys
+const int PONG_KEY_INCREASE = 0x01; // Move down/right
+const int PONG_KEY_DECREASE = 0x02; // Move up/left
+const int PONG_KEY_BOTH     = 0x03; // Player jamming keys at ramdom
 
 // fields
-.int       pong_ai;    // (minigame) when non-zero, singleplayer vs AI
-.int       pong_score; // (minigame_player) number of goals
-.entity pong_paddles[2];// (minigame) paddles
-.float     pong_length;// (pong_paddle) size (0,1)
+const int PONG_MAX_PLAYERS = 4;
+.int    pong_score;                    // (minigame_player) number of goals
+.int    pong_keys;                     // (client) pressed keys
+.entity pong_paddles[PONG_MAX_PLAYERS];// (minigame) paddles
+.float  pong_length;                   // (pong_paddle/pong_ball) size (0,1)
+.entity pong_ai_paddle;                // (pong_ai) controlled paddle entity
 
 #ifdef SVQC
 
-float autocvar_sv_minigames_pong_paddlesize = 0.3;
-float autocvar_sv_minigames_pong_paddlespeed= 1;
-float autocvar_sv_minigames_pong_ballwait   = 1;
-float autocvar_sv_minigames_pong_ballspeed  = 1;
+float autocvar_sv_minigames_pong_paddle_size;
+float autocvar_sv_minigames_pong_paddle_speed;
+
+float autocvar_sv_minigames_pong_ball_wait;
+float autocvar_sv_minigames_pong_ball_speed;
+float autocvar_sv_minigames_pong_ball_radius;
+float autocvar_sv_minigames_pong_ball_number;
+
+float autocvar_sv_minigames_pong_ai_thinkspeed;
+float autocvar_sv_minigames_pong_ai_tolerance;
 
-void pong_ball_think();
+void pong_ball_think(entity this);
 
-void pong_ball_thinkthrow()
+// Throws a ball in a random direction and sets the think function
+void pong_ball_throw(entity ball)
 {
        float angle;
        do
                angle = random()*M_PI*2;
-       while ( (angle > 0.44*M_PI && angle < 0.55*M_PI) || 
-               (angle > 1.44*M_PI && angle < 1.55*M_PI) );
-       self.velocity_x = cos(angle)*autocvar_sv_minigames_pong_ballspeed;
-       self.velocity_y = sin(angle)*autocvar_sv_minigames_pong_ballspeed;
-       self.SendFlags |= MINIG_SF_UPDATE;
-       self.think = pong_ball_think;
-       self.nextthink = time;
+       while ( fabs(sin(angle)) < 0.17 || fabs(cos(angle)) < 0.17 );
+       ball.velocity_x = cos(angle)*autocvar_sv_minigames_pong_ball_speed;
+       ball.velocity_y = sin(angle)*autocvar_sv_minigames_pong_ball_speed;
+       setthink(ball, pong_ball_think);
+       ball.nextthink = time;
+       ball.team = 0;
+       ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
 }
 
-void pong_reset_ball(entity ball)
+// Think equivalent of pong_ball_throw, used to delay throws
+void pong_ball_throwthink(entity this)
+{
+       pong_ball_throw(self);
+}
+
+// Moves ball to the center and stops its motion
+void pong_ball_reset(entity ball)
 {
        ball.velocity = '0 0 0';
        ball.origin = '0.5 0.5 0';
-       ball.SendFlags |= MINIG_SF_UPDATE;
-       ball.think = pong_ball_thinkthrow;
-       ball.nextthink = time + autocvar_sv_minigames_pong_ballwait;
+       setthink(ball, SUB_NullThink);
+       ball.team = 0;
+       ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
+       setthink(ball, pong_ball_throwthink);
+       ball.nextthink = time + autocvar_sv_minigames_pong_ball_wait;
 }
 
-void pong_add_score(entity minigame, int pteam)
+// Add the score to the given team in the minigame
+void pong_add_score(entity minigame, int team_thrower, int team_receiver, int delta)
 {
        if ( !minigame )
                return;
-       entity paddle = minigame.pong_paddles[pteam-1];
-       if ( paddle.realowner )
+
+       if ( team_thrower == 0 )
+               team_thrower = team_receiver;
+
+       if ( team_thrower == team_receiver )
+               delta *= -1;
+
+       entity paddle_thrower = minigame.pong_paddles[team_thrower-1];
+       if ( paddle_thrower.realowner.minigame_players )
+       {
+               paddle_thrower.realowner.minigame_players.pong_score += delta;
+               paddle_thrower.realowner.minigame_players.SendFlags |= PONG_SF_PLAYERSCORE;
+       }
+}
+
+// get point in the box nearest to the given one (2D)
+vector box_nearest(vector box_min, vector box_max, vector p)
+{
+       return eX * ( p_x > box_max_x  ? box_max_x  : ( p_x < box_min_x ? box_min_x : p_x ) )
+               + eY * ( p_y > box_max_y  ? box_max_y  : ( p_y < box_min_y ? box_min_y : p_y ) );
+}
+
+void pong_paddle_bounce(entity ball, int pteam)
+{
+       switch(pteam)
        {
-               paddle.realowner.pong_score++;
-               paddle.realowner.SendFlags |= PONG_SF_PLAYERSCORE;
+               case 1: ball.velocity_x = -fabs(ball.velocity_x); break;
+               case 2: ball.velocity_x = fabs(ball.velocity_x); break;
+               case 3: ball.velocity_y = fabs(ball.velocity_y); break;
+               case 4: ball.velocity_y = -fabs(ball.velocity_y); break;
        }
+
+       float angle = atan2(ball.velocity_y, ball.velocity_x);
+       angle += ( random() - 0.5 ) * 2 * M_PI/6;
+       float speed = vlen(ball.velocity);
+
+       ball.velocity_y = speed * sin(angle);
+       ball.velocity_x = speed * cos(angle);
+}
+
+// checks if the ball hit the paddle for the given team
+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);
+       return vdist(near_point - ball.origin, <=, ball.pong_length);
 }
 
+// Checks for a goal, when that happes adds scores and resets the ball
 bool pong_goal(entity ball, int pteam)
 {
        entity paddle = ball.owner.pong_paddles[pteam-1];
        if (!paddle)
                return false;
-       if ( ball.origin_y < paddle.origin_y-paddle.pong_length/2 ||
-               ball.origin_y > paddle.origin_y+paddle.pong_length/2 )
+
+       if ( !pong_paddle_hit(ball, pteam) )
        {
-               pong_add_score(ball.owner,minigame_next_team(pteam, 2));
-               pong_reset_ball(ball);
+               pong_add_score(ball.owner ,ball.team, pteam, 1);
+               pong_ball_reset(ball);
                return true;
        }
+
        return false;
 }
 
-void pong_ball_think()
+// Moves the ball around
+void pong_ball_think(entity this)
 {
        float think_speed = autocvar_sys_ticrate;
-       self.nextthink = time + think_speed;
-       
-       self.origin_x += self.velocity_x * think_speed;
-       self.origin_y += self.velocity_y * think_speed;
-       if ( self.origin_y <= 0 )
+       this.nextthink = time + think_speed;
+
+       this.origin_x += this.velocity_x * think_speed;
+       this.origin_y += this.velocity_y * think_speed;
+       this.SendFlags |= MINIG_SF_UPDATE;
+
+       int i;
+       for ( i = 1; i <= PONG_MAX_PLAYERS; i++ )
+               if ( pong_paddle_hit(this, i) )
+               {
+                       pong_paddle_bounce(this,i);
+                       this.team = i;
+                       this.SendFlags |= PONG_SF_BALLTEAM;
+                       return;
+               }
+
+       if ( this.origin_y <= this.pong_length )
        {
-               self.origin_y = 0;
-               self.velocity_y *= -1;
+               if ( !pong_goal(this,3) )
+               {
+                       this.origin_y = this.pong_length;
+                       this.velocity_y *= -1;
+               }
        }
-       else if ( self.origin_y >= 1 )
+       else if ( this.origin_y >= 1-this.pong_length )
        {
-               self.origin_y = 1;
-               self.velocity_y *= -1;
+               if ( !pong_goal(this,4) )
+               {
+                       this.origin_y = 1-this.pong_length;
+                       this.velocity_y *= -1;
+               }
        }
-       
-       if ( self.origin_x <= 0 )
+
+       if ( this.origin_x <= this.pong_length )
        {
-               if ( !pong_goal(self,2) )
+               if ( !pong_goal(this,2) )
                {
-                        self.origin_x = 0;
-                        self.velocity_x *= -1;
+                        this.origin_x = this.pong_length;
+                        this.velocity_x *= -1;
                }
        }
-       else if ( self.origin_x >= 1 )
+       else if ( this.origin_x >= 1-this.pong_length )
        {
-               if ( !pong_goal(self,1) )
+               if ( !pong_goal(this,1) )
                {
-                        self.origin_x = 1;
-                        self.velocity_x *= -1;
+                        this.origin_x = 1-this.pong_length;
+                        this.velocity_x *= -1;
                }
        }
-       
-       self.SendFlags |= MINIG_SF_UPDATE;
+
+}
+
+// AI action
+void pong_ai_think(entity this)
+{
+       float think_speed = autocvar_sv_minigames_pong_ai_thinkspeed;
+       this.nextthink = time + think_speed;
+
+       float distance;
+       float next_distance;
+       float min_distance = 1;
+       entity ball = world;
+       entity mayball = world;
+       while ( ( mayball = findentity(mayball,owner,this.owner) ) )
+               if ( mayball.classname == "pong_ball" )
+               {
+                       distance = vlen(mayball.origin-this.pong_ai_paddle.origin);
+                       next_distance = vlen(mayball.origin+mayball.velocity-this.pong_ai_paddle.origin);
+                       if (  distance < min_distance && ( distance < 0.5 || next_distance < distance ) )
+                       {
+                               min_distance = distance;
+                               ball = mayball;
+                       }
+               }
+
+       float target = 0.5;
+       float my_pos;
+
+
+       if ( this.team <= 2 )
+       {
+               if ( ball )
+                       target = ball.origin_y + ball.velocity_y*think_speed;
+               my_pos = this.pong_ai_paddle.origin_y;
+       }
+       else
+       {
+               if ( ball )
+                       target = ball.origin_x + ball.velocity_x*think_speed;
+               my_pos = this.pong_ai_paddle.origin_x;
+       }
+
+       distance = this.pong_length/2 * autocvar_sv_minigames_pong_ai_tolerance
+               + autocvar_sv_minigames_pong_paddle_speed * think_speed;
+
+       if (target < my_pos - distance)
+               this.pong_keys = PONG_KEY_DECREASE;
+       else if (target > my_pos + distance)
+               this.pong_keys = PONG_KEY_INCREASE;
+       else
+               this.pong_keys = 0;
 }
 
-void pong_paddle_think()
+entity pong_ai_spawn(entity paddle)
+{
+       entity ai = msle_spawn(paddle.owner,"pong_ai");
+       ai.minigame_players = ai;
+       ai.team = paddle.team;
+       setthink(ai, pong_ai_think);
+       ai.nextthink = time;
+       ai.pong_ai_paddle = paddle;
+
+       paddle.realowner = ai;
+
+       return ai;
+}
+
+// Moves the paddle
+void pong_paddle_think(entity this)
 {
        float think_speed = autocvar_sys_ticrate;
-       self.nextthink = time + think_speed;
-       
-       if ( self.realowner.movement.x > 0 && self.origin_y > self.pong_length/2 )
+       this.nextthink = time + think_speed;
+
+       if ( this.realowner.minigame_players.pong_keys == PONG_KEY_INCREASE ||
+                this.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
        {
-               self.origin_y -= autocvar_sv_minigames_pong_paddlespeed * think_speed;
-               if ( self.origin_y < 0 )
-                       self.origin_y = 0;
-               self.SendFlags |= MINIG_SF_UPDATE;
+               float movement = 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;
+
+               if ( this.team > 2 )
+                       this.origin_x = bound(halflen, this.origin_x+movement, 1-halflen);
+               else
+                       this.origin_y = bound(halflen, this.origin_y+movement, 1-halflen);
+
+               this.SendFlags |= MINIG_SF_UPDATE;
        }
-       else if ( self.realowner.movement.x < 0 && self.origin_y < 1-self.pong_length/2 )
+}
+
+vector pong_team_to_box_halfsize(int nteam, float length, float width)
+{
+       if ( nteam > 2 )
+               return eY*width/2 + eX*length/2;
+       return eX*width/2 + eY*length/2;
+}
+
+vector pong_team_to_paddlepos(int nteam)
+{
+       switch(nteam)
        {
-               self.origin_y += autocvar_sv_minigames_pong_paddlespeed * think_speed;
-               if ( self.origin_y > 1 )
-                       self.origin_y = 1;
-               self.SendFlags |= MINIG_SF_UPDATE;
+               case 1: return '0.99 0.5 0';
+               case 2: return '0.01 0.5 0';
+               case 3: return '0.5 0.01 0';
+               case 4: return '0.5 0.99 0';
+               default:return '0 0 0';
        }
-               
+}
+
+// Spawns a pong paddle
+// if real_player is world, 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");
+       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);
+
+       if ( real_player == world )
+               pong_ai_spawn(paddle);
+       else
+               paddle.realowner = real_player;
+
+       minigame.pong_paddles[pl_team-1] = paddle;
+
+       return paddle;
+
 }
 
 // required function, handle server side events
-int minigame_event_pong(entity minigame, string event, ...)
+int pong_server_event(entity minigame, string event, ...)
 {
        switch (event)
        {
                case "start":
                {
-                       entity ball = msle_spawn(minigame,"pong_ball");
-                       pong_reset_ball(ball);
+                       minigame.minigame_flags |= PONG_STATUS_WAIT;
                        return true;
                }
-               case "end":
-                       // nothing to do
-                       return false;
                case "join":
                {
-                       int pl_num = minigame_count_players(minigame);
-                       
-                       // Don't allow joining a single player match
-                       if ( (minigame.pong_ai) && pl_num > 0 )
+                       // Don't allow joining a match that is already running
+                       if ( minigame.minigame_flags & PONG_STATUS_PLAY )
                                return false;
 
-                       // Don't allow more than 2 players
-                       if(pl_num >= 2) { return false; }
-
-                       int pl_team = 1;
-                       // Get the right team
-                       if(minigame.minigame_players)
-                               pl_team = minigame_next_team(minigame.minigame_players.team, 2);
-                       
-                       
                        entity player = ...(0,entity);
-                       entity paddle = msle_spawn(minigame,"pong_paddle");// Note puddle isn't a typo
-                       paddle.pong_length = autocvar_sv_minigames_pong_paddlesize;
-                       paddle.origin_y = 0.5;
-                       paddle.origin_x = pl_team == 1 ? 0.99 : 0.01;
-                       paddle.think = pong_paddle_think;
-                       paddle.nextthink = time;
-                       paddle.team = pl_team;
-                       paddle.realowner = player;
-                       minigame.pong_paddles[pl_team-1] = paddle;
-
-                       // Team 1 by default
-                       return pl_team;
+                       int i;
+                       for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
+                       {
+                               if ( minigame.pong_paddles[i] == world )
+                               {
+                                       pong_paddle_spawn(minigame,i+1,player);
+                                       return i+1;
+                               }
+                       }
+
+                       return false;
                }
                case "part":
-                       // todo
+               {
+                       entity player = ...(0,entity);
+                       entity paddle;
+                       entity ai;
+                       int i;
+                       for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
+                       {
+                               paddle = minigame.pong_paddles[i];
+                               if ( paddle != world && paddle.realowner == player )
+                               {
+                                       ai = pong_ai_spawn(paddle);
+                                       ai.pong_score = player.minigame_players.pong_score;
+                                       break;
+                               }
+
+                       }
                        return false;
+               }
                case "cmd":
-                       // nothing to do
+               {
+                       entity player = ...(0,entity);
+                       switch(argv(0))
+                       {
+                               case "throw":
+                                       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++ )
+                                               {
+                                                       ball = msle_spawn(minigame,"pong_ball");
+                                                       ball.pong_length = autocvar_sv_minigames_pong_ball_radius;
+                                                       pong_ball_reset(ball);
+                                               }
+                                       }
+                                       return true;
+                               case "+movei":
+                                       player.pong_keys |= PONG_KEY_INCREASE;
+                                       return true;
+                               case "+moved":
+                                       player.pong_keys |= PONG_KEY_DECREASE;
+                                       return true;
+                               case "-movei":
+                                       player.pong_keys &= ~PONG_KEY_INCREASE;
+                                       return true;
+                               case "-moved":
+                                       player.pong_keys &= ~PONG_KEY_DECREASE;
+                                       return true;
+                               case "pong_aimore":
+                               {
+                                       int i;
+                                       if ( minigame.minigame_flags & PONG_STATUS_WAIT )
+                                               for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
+                                               {
+                                                       if ( minigame.pong_paddles[i] == world )
+                                                       {
+                                                               pong_paddle_spawn(minigame,i+1,world);
+                                                               return true;
+                                                       }
+                                               }
+                                       sprint(player.minigame_players,"Cannot spawn AI\n");
+                                       return true;
+                               }
+                               case "pong_ailess":
+                               {
+                                       if ( minigame.minigame_flags & PONG_STATUS_WAIT )
+                                       {
+                                               entity paddle;
+                                               int i;
+                                               for ( i = PONG_MAX_PLAYERS-1; i >= 0; i-- )
+                                               {
+                                                       paddle = minigame.pong_paddles[i];
+                                                       if ( paddle != world &&
+                                                               paddle.realowner.classname == "pong_ai" )
+                                                       {
+                                                               minigame.pong_paddles[i] = world;
+                                                               remove(paddle.realowner);
+                                                               remove(paddle);
+                                                               return true;
+                                                       }
+                                               }
+                                       }
+                                       sprint(player.minigame_players,"Cannot remove AI\n");
+                                       return true;
+                               }
+
+                       }
                        return false;
+               }
                case "network_send":
                {
                        entity sent = ...(0,entity);
                        int sf = ...(1,int);
                        if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
                        {
-                               WriteByte(MSG_ENTITY,sent.pong_score);
-                       }
-                       else if ( sent.classname == "minigame" && (sf & PONG_SF_SINGLEPLAYER) )
-                       {
-                               WriteByte(MSG_ENTITY,sent.pong_ai);
+                               WriteLong(MSG_ENTITY,sent.pong_score);
                        }
                        return false;
                }
@@ -199,76 +472,123 @@ int minigame_event_pong(entity minigame, string event, ...)
 
 #elif defined(CSQC)
 
+void drawrotpic(vector org, float rot, string pic, vector sz, vector hotspot, vector rgb, float a, float f);
+
+float pong_team_to_angle(int nteam)
+{
+       switch(nteam)
+       {
+               default:
+               case 1: return 0;
+               case 2: return M_PI;
+               case 3: return M_PI*3/2;
+               case 4: return M_PI/2;
+       }
+}
+
+vector pong_team_to_color(int nteam)
+{
+       switch(nteam)
+       {
+               case 1: return '1 0 0';
+               case 2: return '0 0 1';
+               case 3: return '1 1 0';
+               case 4: return '1 0 1';
+               default:return '1 1 1';
+       }
+}
 
 // Required function, draw the game board
-void minigame_hud_board_pong(vector pos, vector mySize)
+void pong_hud_board(vector pos, vector mySize)
 {
        minigame_hud_fitsqare(pos, mySize);
        minigame_hud_simpleboard(pos,mySize,minigame_texture("pong/board"));
-       
+
        entity e;
        vector obj_pos;
-       vector ball_size = minigame_hud_denormalize_size('1 1 0' / 16,pos,mySize);
-       vector paddle_size;
+       vector obj_size;
        FOREACH_MINIGAME_ENTITY(e)
        {
                if ( e.classname == "pong_ball" )
                {
+                       // Note: 4*radius = 2*diameter because the image is large enough to fit the glow around the ball
+                       obj_size =  minigame_hud_denormalize_size('4 4 0'*e.pong_length,pos,mySize);
                        obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
+
                        minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball"),
-                                       ball_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
+                                       obj_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
+
+                       minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball-glow"),
+                                       obj_size, pong_team_to_color(e.team),
+                                       panel_fg_alpha, DRAWFLAG_ADDITIVE );
                }
                else if ( e.classname == "pong_paddle" )
                {
                        obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
-                       paddle_size = minigame_hud_denormalize_size(eX / 32 + eY*e.pong_length,pos,mySize);
-                       minigame_drawpic_centered( obj_pos, minigame_texture("pong/paddle"),
-                                       paddle_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
+                       obj_size = minigame_hud_denormalize_size(eX / 16 + eY*e.pong_length,pos,mySize);
+
+                       drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle-glow"),
+                               obj_size, obj_size/2, pong_team_to_color(e.team),
+                               panel_fg_alpha, DRAWFLAG_ADDITIVE );
+
+                       drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle"),
+                               obj_size, obj_size/2, '1 1 1',
+                               panel_fg_alpha, DRAWFLAG_NORMAL );
+
                }
        }
 }
 
-
 // Required function, draw the game status panel
-void minigame_hud_status_pong(vector pos, vector mySize)
+void pong_hud_status(vector pos, vector mySize)
 {
        HUD_Panel_DrawBg(1);
        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);
-       
+       ts_y += hud_fontsize_y;
        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_y = ( mySize_y - PONG_MAX_PLAYERS*player_fontsize_y ) / PONG_MAX_PLAYERS;
        ts_x = mySize_x;
        vector mypos;
-       vector tile_size = '48 48 0';
 
        entity e;
        FOREACH_MINIGAME_ENTITY(e)
        {
-               if ( e.classname == "minigame_player" )
+               if ( e.classname == "minigame_player" || e.classname == "pong_ai" )
                {
                        mypos = pos;
-                       if ( e.team == 2 )
-                               mypos_y  += player_fontsize_y + ts_y;
+                       mypos_y  += (e.team-1) * (player_fontsize_y + ts_y);
+
+                       drawfill(mypos, ts, pong_team_to_color(e.team), 0.25, DRAWFLAG_ADDITIVE);
+
                        minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
-                               (e.minigame_playerslot ? GetPlayerName(e.minigame_playerslot-1) : _("AI")),
+                               (e.minigame_playerslot ? entcs_GetName(e.minigame_playerslot-1) : _("AI")),
                                player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
-                       
-                       mypos_y += player_fontsize_y;
-                       
-                       drawstring(mypos,ftos(e.pong_score),tile_size,
+
+                       drawstring(mypos+eY*player_fontsize_y,ftos(e.pong_score),'48 48 0',
                                           '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
+
+                       if ( e == minigame_self )
+                               drawborderlines(1, mypos, ts, pong_team_to_color(e.team), 1, DRAWFLAG_NORMAL);
                }
        }
 }
 
+// convert minigame flags to a message
+string pong_message(int mgflags)
+{
+       string rmessage = "";
+       if (mgflags & PONG_STATUS_WAIT)
+               rmessage = _("Press ^1Start Match^7 to start the match with the current players");
+       return rmessage;
+}
 
 // Required function, handle client events
-int minigame_event_pong(entity minigame, string event, ...)
+int pong_client_event(entity minigame, string event, ...)
 {
        switch(event)
        {
@@ -279,9 +599,32 @@ int minigame_event_pong(entity minigame, string event, ...)
                        {
                                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;
+                       }
+                       return false;
+               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;
                        }
                        return false;
@@ -291,38 +634,34 @@ int minigame_event_pong(entity minigame, string event, ...)
                        int sf = ...(1,int);
                        if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
                        {
-                               sent.pong_score = ReadByte();
+                               sent.pong_score = ReadLong();
                        }
-                       else if ( sent.classname == "minigame" && (sf & PONG_SF_SINGLEPLAYER) )
+                       else if ( sent.classname == "minigame" )
                        {
-                               int ai = ReadByte();
-                               bool spawnai = ai && !sent.pong_ai;
-                               sent.pong_ai = ai;
-                               
-                               if ( spawnai )
+                               if ( sf & MINIG_SF_UPDATE )
                                {
-                                       entity aiplayer = spawn();
-                                       aiplayer.classname = "minigame_player";
-                                       aiplayer.owner = minigame;
-                                       aiplayer.team = ai;
-                                       aiplayer.minigame_playerslot = 0;
-                                       aiplayer.minigame_autoclean = 1;
-                                       // todo aiplayer.think
+                                       sent.message = pong_message(sent.minigame_flags);
                                }
                        }
                        return false;
                }
                case "menu_show":
                {
-                       HUD_MinigameMenu_CustomEntry(...(0,entity),_("Single Player"),"singleplayer");
+                       HUD_MinigameMenu_CustomEntry(...(0,entity),_("Start Match"),"pong_throw");
+                       HUD_MinigameMenu_CustomEntry(...(0,entity),_("Add AI player"),"pong_aimore");
+                       HUD_MinigameMenu_CustomEntry(...(0,entity),_("Remove AI player"),"pong_ailess");
                        return false;
                }
                case "menu_click":
                {
-                       if ( ...(0,string) == "singleplayer" && !minigame.pong_ai )
+                       string cmd = ...(0,string);
+                       if( cmd == "pong_throw" && minigame.minigame_flags & PONG_STATUS_WAIT )
+                       {
+                               minigame_cmd("throw");
+                       }
+                       else if ( cmd == "pong_aimore" || cmd == "pong_ailess" )
                        {
-                               if ( minigame_count_players(minigame) == 1 )
-                                       minigame_cmd("singleplayer");
+                               minigame_cmd(cmd);
                        }
                        return false;
                }
@@ -330,4 +669,4 @@ int minigame_event_pong(entity minigame, string event, ...)
 
        return false;
 }
-#endif
\ No newline at end of file
+#endif