2 const int PONG_STATUS_WAIT = 0x0010; // waiting for players to join
3 const int PONG_STATUS_PLAY = 0x0020; // playing
6 // (minigame_player) sent when reporting scores
7 const int PONG_SF_PLAYERSCORE = MINIG_SF_CUSTOM;
8 // (pong_ball) sent when changing team
9 const int PONG_SF_BALLTEAM = MINIG_SF_CUSTOM;
12 const int PONG_KEY_INCREASE = 0x01; // Move down/right
13 const int PONG_KEY_DECREASE = 0x02; // Move up/left
14 const int PONG_KEY_BOTH = 0x03; // Player jamming keys at ramdom
17 const int PONG_MAX_PLAYERS = 4;
18 .int pong_score; // (minigame_player) number of goals
19 .int pong_keys; // (client) pressed keys
20 .entity pong_paddles[PONG_MAX_PLAYERS];// (minigame) paddles
21 .float pong_length; // (pong_paddle/pong_ball) size (0,1)
22 .entity pong_ai_paddle; // (pong_ai) controlled paddle entity
26 float autocvar_sv_minigames_pong_paddle_size;
27 float autocvar_sv_minigames_pong_paddle_speed;
29 float autocvar_sv_minigames_pong_ball_wait;
30 float autocvar_sv_minigames_pong_ball_speed;
31 float autocvar_sv_minigames_pong_ball_radius;
33 float autocvar_sv_minigames_pong_ai_thinkspeed;
34 float autocvar_sv_minigames_pong_ai_tolerance;
36 void pong_ball_think();
38 // Throws a ball in a random direction and sets the think function
39 void pong_ball_throw(entity ball)
43 angle = random()*M_PI*2;
44 while ( fabs(sin(angle)) < 0.17 || fabs(cos(angle)) < 0.17 );
45 ball.velocity_x = cos(angle)*autocvar_sv_minigames_pong_ball_speed;
46 ball.velocity_y = sin(angle)*autocvar_sv_minigames_pong_ball_speed;
47 ball.think = pong_ball_think;
48 ball.nextthink = time;
50 ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
53 // Think equivalent of pong_ball_throw, used to delay throws
54 void pong_ball_throwthink()
56 pong_ball_throw(self);
59 // Moves ball to the center and stops its motion
60 void pong_ball_reset(entity ball)
62 ball.velocity = '0 0 0';
63 ball.origin = '0.5 0.5 0';
64 ball.think = SUB_NullThink;
66 ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
67 ball.think = pong_ball_throwthink;
68 ball.nextthink = time + autocvar_sv_minigames_pong_ball_wait;
71 // Add the score to the given team in the minigame
72 void pong_add_score(entity minigame, int team_thrower, int team_receiver, int delta)
77 if ( team_thrower == 0 )
78 team_thrower = team_receiver;
80 if ( team_thrower == team_receiver )
83 entity paddle_thrower = minigame.pong_paddles[team_thrower-1];
84 if ( paddle_thrower.realowner.minigame_players )
86 paddle_thrower.realowner.minigame_players.pong_score += delta;
87 paddle_thrower.realowner.minigame_players.SendFlags |= PONG_SF_PLAYERSCORE;
91 // get point in the box nearest to the given one (2D)
92 vector box_nearest(vector box_min, vector box_max, vector p)
94 return eX * ( p_x > box_max_x ? box_max_x : ( p_x < box_min_x ? box_min_x : p_x ) )
95 + eY * ( p_y > box_max_y ? box_max_y : ( p_y < box_min_y ? box_min_y : p_y ) );
98 void pong_paddle_bounce(entity ball, int pteam)
102 case 1: ball.velocity_x = -fabs(ball.velocity_x); break;
103 case 2: ball.velocity_x = fabs(ball.velocity_x); break;
104 case 3: ball.velocity_y = fabs(ball.velocity_y); break;
105 case 4: ball.velocity_y = -fabs(ball.velocity_y); break;
108 float angle = atan2(ball.velocity_y, ball.velocity_x);
109 angle += ( random() - 0.5 ) * 2 * M_PI/6;
110 float speed = vlen(ball.velocity);
112 ball.velocity_y = speed * sin(angle);
113 ball.velocity_x = speed * cos(angle);
116 // checks if the ball hit the paddle for the given team
117 bool pong_paddle_hit(entity ball, int pteam)
119 entity paddle = ball.owner.pong_paddles[pteam-1];
122 vector near_point = box_nearest(paddle.mins+paddle.origin,
123 paddle.maxs+paddle.origin, ball.origin);
124 return vlen(near_point-ball.origin) <= ball.pong_length ;
127 // Checks for a goal, when that happes adds scores and resets the ball
128 bool pong_goal(entity ball, int pteam)
130 entity paddle = ball.owner.pong_paddles[pteam-1];
134 if ( !pong_paddle_hit(ball, pteam) )
136 pong_add_score(ball.owner ,ball.team, pteam, 1);
137 pong_ball_reset(ball);
144 // Moves the ball around
145 void pong_ball_think()
147 float think_speed = autocvar_sys_ticrate;
148 self.nextthink = time + think_speed;
150 self.origin_x += self.velocity_x * think_speed;
151 self.origin_y += self.velocity_y * think_speed;
152 self.SendFlags |= MINIG_SF_UPDATE;
155 for ( i = 1; i <= PONG_MAX_PLAYERS; i++ )
156 if ( pong_paddle_hit(self, i) )
158 pong_paddle_bounce(self,i);
160 self.SendFlags |= PONG_SF_BALLTEAM;
164 if ( self.origin_y <= self.pong_length )
166 if ( !pong_goal(self,3) )
168 self.origin_y = self.pong_length;
169 self.velocity_y *= -1;
172 else if ( self.origin_y >= 1-self.pong_length )
174 if ( !pong_goal(self,4) )
176 self.origin_y = 1-self.pong_length;
177 self.velocity_y *= -1;
181 if ( self.origin_x <= self.pong_length )
183 if ( !pong_goal(self,2) )
185 self.origin_x = self.pong_length;
186 self.velocity_x *= -1;
189 else if ( self.origin_x >= 1-self.pong_length )
191 if ( !pong_goal(self,1) )
193 self.origin_x = 1-self.pong_length;
194 self.velocity_x *= -1;
203 float think_speed = autocvar_sv_minigames_pong_ai_thinkspeed;
204 self.nextthink = time + think_speed;
206 float distance = self.pong_length/2 * autocvar_sv_minigames_pong_ai_tolerance;
207 distance += autocvar_sv_minigames_pong_paddle_speed * think_speed;
213 while ( ( ball = findentity(ball,owner,self.owner) ) )
214 if ( ball.classname == "pong_ball" )
216 if ( self.team <= 2 )
218 target = ball.origin_y + ball.velocity_y*think_speed;
219 if ( ( self.team == 1 && ball.origin_x < 0.5 && ball.velocity_x < 0 ) ||
220 ( self.team == 2 && ball.origin_x > 0.5 && ball.velocity_x > 0 ) )
222 self_pos = self.pong_ai_paddle.origin_y;
226 target = ball.origin_x + ball.velocity_x*think_speed;
227 if ( ( self.team == 4 && ball.origin_y < 0.5 && ball.velocity_y < 0 ) ||
228 ( self.team == 3 && ball.origin_y > 0.5 && ball.velocity_y > 0 ) )
230 self_pos = self.pong_ai_paddle.origin_x;
233 if (target < self_pos - distance)
234 self.pong_keys = PONG_KEY_DECREASE;
235 else if (target > self_pos + distance)
236 self.pong_keys = PONG_KEY_INCREASE;
238 break; // TODO support multiple balls?
242 entity pong_ai_spawn(entity paddle)
244 entity ai = msle_spawn(paddle.owner,"pong_ai");
245 ai.minigame_players = ai;
246 ai.team = paddle.team;
247 ai.think = pong_ai_think;
249 ai.pong_ai_paddle = paddle;
251 paddle.realowner = ai;
257 void pong_paddle_think()
259 float think_speed = autocvar_sys_ticrate;
260 self.nextthink = time + think_speed;
262 if ( self.realowner.minigame_players.pong_keys == PONG_KEY_INCREASE ||
263 self.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
265 float movement = autocvar_sv_minigames_pong_paddle_speed * think_speed;
266 float halflen = self.pong_length/2;
268 if ( self.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
272 self.origin_x = bound(halflen, self.origin_x+movement, 1-halflen);
274 self.origin_y = bound(halflen, self.origin_y+movement, 1-halflen);
276 self.SendFlags |= MINIG_SF_UPDATE;
280 vector pong_team_to_box_halfsize(int nteam, float length, float width)
283 return eY*width/2 + eX*length/2;
284 return eX*width/2 + eY*length/2;
287 vector pong_team_to_paddlepos(int nteam)
291 case 1: return '0.99 0.5 0';
292 case 2: return '0.01 0.5 0';
293 case 3: return '0.5 0.01 0';
294 case 4: return '0.5 0.99 0';
295 default:return '0 0 0';
299 // Spawns a pong paddle
300 // if real_player is world, the paddle is controlled by AI
301 entity pong_paddle_spawn(entity minigame, int pl_team, entity real_player)
303 entity paddle = msle_spawn(minigame,"pong_paddle");
304 paddle.pong_length = autocvar_sv_minigames_pong_paddle_size;
305 paddle.origin = pong_team_to_paddlepos(pl_team);
306 paddle.think = pong_paddle_think;
307 paddle.nextthink = time;
308 paddle.team = pl_team;
309 paddle.mins = pong_team_to_box_halfsize(pl_team,-paddle.pong_length,-1/16);
310 paddle.maxs = pong_team_to_box_halfsize(pl_team,paddle.pong_length,1/16);
312 if ( real_player == world )
313 pong_ai_spawn(paddle);
315 paddle.realowner = real_player;
317 minigame.pong_paddles[pl_team-1] = paddle;
323 // required function, handle server side events
324 int pong_server_event(entity minigame, string event, ...)
330 minigame.minigame_flags |= PONG_STATUS_WAIT;
335 // Don't allow joining a match that is already running
336 if ( minigame.minigame_flags & PONG_STATUS_PLAY )
339 entity player = ...(0,entity);
341 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
343 if ( minigame.pong_paddles[i] == world )
345 pong_paddle_spawn(minigame,i+1,player);
354 entity player = ...(0,entity);
357 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
359 paddle = minigame.pong_paddles[i];
360 if ( paddle != world && paddle.realowner == player )
362 pong_ai_spawn(paddle);
371 entity player = ...(0,entity);
375 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
377 minigame.minigame_flags = PONG_STATUS_PLAY |
378 (minigame.minigame_flags & ~PONG_STATUS_WAIT);
379 minigame.SendFlags |= MINIG_SF_UPDATE;
381 entity ball = msle_spawn(minigame,"pong_ball");
382 ball.pong_length = autocvar_sv_minigames_pong_ball_radius;
383 pong_ball_reset(ball);
387 player.pong_keys |= PONG_KEY_INCREASE;
390 player.pong_keys |= PONG_KEY_DECREASE;
393 player.pong_keys &= ~PONG_KEY_INCREASE;
396 player.pong_keys &= ~PONG_KEY_DECREASE;
401 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
402 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
404 if ( minigame.pong_paddles[i] == world )
406 pong_paddle_spawn(minigame,i+1,world);
410 sprint(player.minigame_players,"Cannot spawn AI\n");
415 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
419 for ( i = PONG_MAX_PLAYERS-1; i >= 0; i-- )
421 paddle = minigame.pong_paddles[i];
422 if ( paddle != world &&
423 paddle.realowner.classname == "pong_ai" )
425 minigame.pong_paddles[i] = world;
426 remove(paddle.realowner);
432 sprint(player.minigame_players,"Cannot remove AI\n");
441 entity sent = ...(0,entity);
443 if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
445 WriteLong(MSG_ENTITY,sent.pong_score);
456 #include "waypointsprites.qh" // drawrotpic
458 float pong_team_to_angle(int nteam)
465 case 3: return M_PI*3/2;
466 case 4: return M_PI/2;
470 vector pong_team_to_color(int nteam)
474 case 1: return '1 0 0';
475 case 2: return '0 0 1';
476 case 3: return '1 1 0';
477 case 4: return '1 0 1';
478 default:return '1 1 1';
482 // Required function, draw the game board
483 void pong_hud_board(vector pos, vector mySize)
485 minigame_hud_fitsqare(pos, mySize);
486 minigame_hud_simpleboard(pos,mySize,minigame_texture("pong/board"));
491 FOREACH_MINIGAME_ENTITY(e)
493 if ( e.classname == "pong_ball" )
495 // Note: 4*radius = 2*diameter because the image is large enough to fit the glow around the ball
496 obj_size = minigame_hud_denormalize_size('4 4 0'*e.pong_length,pos,mySize);
497 obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
499 minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball"),
500 obj_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
502 minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball-glow"),
503 obj_size, pong_team_to_color(e.team),
504 panel_fg_alpha, DRAWFLAG_ADDITIVE );
506 else if ( e.classname == "pong_paddle" )
508 obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
509 obj_size = minigame_hud_denormalize_size(eX / 16 + eY*e.pong_length,pos,mySize);
511 drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle-glow"),
512 obj_size, obj_size/2, pong_team_to_color(e.team),
513 panel_fg_alpha, DRAWFLAG_ADDITIVE );
515 drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle"),
516 obj_size, obj_size/2, '1 1 1',
517 panel_fg_alpha, DRAWFLAG_NORMAL );
523 // Required function, draw the game status panel
524 void pong_hud_status(vector pos, vector mySize)
528 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
529 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
530 ts_y += hud_fontsize_y;
534 vector player_fontsize = hud_fontsize * 1.75;
535 ts_y = ( mySize_y - PONG_MAX_PLAYERS*player_fontsize_y ) / PONG_MAX_PLAYERS;
540 FOREACH_MINIGAME_ENTITY(e)
542 if ( e.classname == "minigame_player" || e.classname == "pong_ai" )
545 mypos_y += (e.team-1) * (player_fontsize_y + ts_y);
547 drawfill(mypos, ts, pong_team_to_color(e.team), 0.25, DRAWFLAG_ADDITIVE);
549 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
550 (e.minigame_playerslot ? GetPlayerName(e.minigame_playerslot-1) : _("AI")),
551 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
553 drawstring(mypos+eY*player_fontsize_y,ftos(e.pong_score),'48 48 0',
554 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
556 if ( e == minigame_self )
557 drawborderlines(1, mypos, ts, pong_team_to_color(e.team), 1, DRAWFLAG_NORMAL);
562 // convert minigame flags to a message
563 string pong_message(int mgflags)
565 string rmessage = "";
566 if (mgflags & PONG_STATUS_WAIT)
567 rmessage = _("Press ^1Start Match^7 to start the match with the current players");
571 // Required function, handle client events
572 int pong_client_event(entity minigame, string event, ...)
579 switch ( ...(0,int) )
585 minigame_cmd("+moved");
590 case K_KP_RIGHTARROW:
591 minigame_cmd("+movei");
596 switch ( ...(0,int) )
602 minigame_cmd("-moved");
607 case K_KP_RIGHTARROW:
608 minigame_cmd("-movei");
612 case "network_receive":
614 entity sent = ...(0,entity);
616 if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
618 sent.pong_score = ReadLong();
620 else if ( sent.classname == "minigame" )
622 if ( sf & MINIG_SF_UPDATE )
624 sent.message = pong_message(sent.minigame_flags);
631 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Start Match"),"pong_throw");
632 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Add AI player"),"pong_aimore");
633 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Remove AI player"),"pong_ailess");
638 string cmd = ...(0,string);
639 if( cmd == "pong_throw" && minigame.minigame_flags & PONG_STATUS_WAIT )
641 minigame_cmd("throw");
643 else if ( cmd == "pong_aimore" || cmd == "pong_ailess" )