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;
32 float autocvar_sv_minigames_pong_ball_number;
34 float autocvar_sv_minigames_pong_ai_thinkspeed;
35 float autocvar_sv_minigames_pong_ai_tolerance;
37 void pong_ball_think();
39 // Throws a ball in a random direction and sets the think function
40 void pong_ball_throw(entity ball)
44 angle = random()*M_PI*2;
45 while ( fabs(sin(angle)) < 0.17 || fabs(cos(angle)) < 0.17 );
46 ball.velocity_x = cos(angle)*autocvar_sv_minigames_pong_ball_speed;
47 ball.velocity_y = sin(angle)*autocvar_sv_minigames_pong_ball_speed;
48 ball.think = pong_ball_think;
49 ball.nextthink = time;
51 ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
54 // Think equivalent of pong_ball_throw, used to delay throws
55 void pong_ball_throwthink()
57 pong_ball_throw(self);
60 // Moves ball to the center and stops its motion
61 void pong_ball_reset(entity ball)
63 ball.velocity = '0 0 0';
64 ball.origin = '0.5 0.5 0';
65 ball.think = SUB_NullThink;
67 ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
68 ball.think = pong_ball_throwthink;
69 ball.nextthink = time + autocvar_sv_minigames_pong_ball_wait;
72 // Add the score to the given team in the minigame
73 void pong_add_score(entity minigame, int team_thrower, int team_receiver, int delta)
78 if ( team_thrower == 0 )
79 team_thrower = team_receiver;
81 if ( team_thrower == team_receiver )
84 entity paddle_thrower = minigame.pong_paddles[team_thrower-1];
85 if ( paddle_thrower.realowner.minigame_players )
87 paddle_thrower.realowner.minigame_players.pong_score += delta;
88 paddle_thrower.realowner.minigame_players.SendFlags |= PONG_SF_PLAYERSCORE;
92 // get point in the box nearest to the given one (2D)
93 vector box_nearest(vector box_min, vector box_max, vector p)
95 return eX * ( p_x > box_max_x ? box_max_x : ( p_x < box_min_x ? box_min_x : p_x ) )
96 + eY * ( p_y > box_max_y ? box_max_y : ( p_y < box_min_y ? box_min_y : p_y ) );
99 void pong_paddle_bounce(entity ball, int pteam)
103 case 1: ball.velocity_x = -fabs(ball.velocity_x); break;
104 case 2: ball.velocity_x = fabs(ball.velocity_x); break;
105 case 3: ball.velocity_y = fabs(ball.velocity_y); break;
106 case 4: ball.velocity_y = -fabs(ball.velocity_y); break;
109 float angle = atan2(ball.velocity_y, ball.velocity_x);
110 angle += ( random() - 0.5 ) * 2 * M_PI/6;
111 float speed = vlen(ball.velocity);
113 ball.velocity_y = speed * sin(angle);
114 ball.velocity_x = speed * cos(angle);
117 // checks if the ball hit the paddle for the given team
118 bool pong_paddle_hit(entity ball, int pteam)
120 entity paddle = ball.owner.pong_paddles[pteam-1];
123 vector near_point = box_nearest(paddle.mins+paddle.origin,
124 paddle.maxs+paddle.origin, ball.origin);
125 return vlen(near_point-ball.origin) <= ball.pong_length ;
128 // Checks for a goal, when that happes adds scores and resets the ball
129 bool pong_goal(entity ball, int pteam)
131 entity paddle = ball.owner.pong_paddles[pteam-1];
135 if ( !pong_paddle_hit(ball, pteam) )
137 pong_add_score(ball.owner ,ball.team, pteam, 1);
138 pong_ball_reset(ball);
145 // Moves the ball around
146 void pong_ball_think()
148 float think_speed = autocvar_sys_ticrate;
149 self.nextthink = time + think_speed;
151 self.origin_x += self.velocity_x * think_speed;
152 self.origin_y += self.velocity_y * think_speed;
153 self.SendFlags |= MINIG_SF_UPDATE;
156 for ( i = 1; i <= PONG_MAX_PLAYERS; i++ )
157 if ( pong_paddle_hit(self, i) )
159 pong_paddle_bounce(self,i);
161 self.SendFlags |= PONG_SF_BALLTEAM;
165 if ( self.origin_y <= self.pong_length )
167 if ( !pong_goal(self,3) )
169 self.origin_y = self.pong_length;
170 self.velocity_y *= -1;
173 else if ( self.origin_y >= 1-self.pong_length )
175 if ( !pong_goal(self,4) )
177 self.origin_y = 1-self.pong_length;
178 self.velocity_y *= -1;
182 if ( self.origin_x <= self.pong_length )
184 if ( !pong_goal(self,2) )
186 self.origin_x = self.pong_length;
187 self.velocity_x *= -1;
190 else if ( self.origin_x >= 1-self.pong_length )
192 if ( !pong_goal(self,1) )
194 self.origin_x = 1-self.pong_length;
195 self.velocity_x *= -1;
204 float think_speed = autocvar_sv_minigames_pong_ai_thinkspeed;
205 self.nextthink = time + think_speed;
209 float min_distance = 1;
211 entity mayball = world;
212 while ( ( mayball = findentity(mayball,owner,self.owner) ) )
213 if ( mayball.classname == "pong_ball" )
215 distance = vlen(mayball.origin-self.pong_ai_paddle.origin);
216 next_distance = vlen(mayball.origin+mayball.velocity-self.pong_ai_paddle.origin);
217 if ( distance < min_distance && ( distance < 0.5 || next_distance < distance ) )
219 min_distance = distance;
228 if ( self.team <= 2 )
231 target = ball.origin_y + ball.velocity_y*think_speed;
232 self_pos = self.pong_ai_paddle.origin_y;
237 target = ball.origin_x + ball.velocity_x*think_speed;
238 self_pos = self.pong_ai_paddle.origin_x;
241 distance = self.pong_length/2 * autocvar_sv_minigames_pong_ai_tolerance
242 + autocvar_sv_minigames_pong_paddle_speed * think_speed;
244 if (target < self_pos - distance)
245 self.pong_keys = PONG_KEY_DECREASE;
246 else if (target > self_pos + distance)
247 self.pong_keys = PONG_KEY_INCREASE;
252 entity pong_ai_spawn(entity paddle)
254 entity ai = msle_spawn(paddle.owner,"pong_ai");
255 ai.minigame_players = ai;
256 ai.team = paddle.team;
257 ai.think = pong_ai_think;
259 ai.pong_ai_paddle = paddle;
261 paddle.realowner = ai;
267 void pong_paddle_think()
269 float think_speed = autocvar_sys_ticrate;
270 self.nextthink = time + think_speed;
272 if ( self.realowner.minigame_players.pong_keys == PONG_KEY_INCREASE ||
273 self.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
275 float movement = autocvar_sv_minigames_pong_paddle_speed * think_speed;
276 float halflen = self.pong_length/2;
278 if ( self.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
282 self.origin_x = bound(halflen, self.origin_x+movement, 1-halflen);
284 self.origin_y = bound(halflen, self.origin_y+movement, 1-halflen);
286 self.SendFlags |= MINIG_SF_UPDATE;
290 vector pong_team_to_box_halfsize(int nteam, float length, float width)
293 return eY*width/2 + eX*length/2;
294 return eX*width/2 + eY*length/2;
297 vector pong_team_to_paddlepos(int nteam)
301 case 1: return '0.99 0.5 0';
302 case 2: return '0.01 0.5 0';
303 case 3: return '0.5 0.01 0';
304 case 4: return '0.5 0.99 0';
305 default:return '0 0 0';
309 // Spawns a pong paddle
310 // if real_player is world, the paddle is controlled by AI
311 entity pong_paddle_spawn(entity minigame, int pl_team, entity real_player)
313 entity paddle = msle_spawn(minigame,"pong_paddle");
314 paddle.pong_length = autocvar_sv_minigames_pong_paddle_size;
315 paddle.origin = pong_team_to_paddlepos(pl_team);
316 paddle.think = pong_paddle_think;
317 paddle.nextthink = time;
318 paddle.team = pl_team;
319 paddle.mins = pong_team_to_box_halfsize(pl_team,-paddle.pong_length,-1/16);
320 paddle.maxs = pong_team_to_box_halfsize(pl_team,paddle.pong_length,1/16);
322 if ( real_player == world )
323 pong_ai_spawn(paddle);
325 paddle.realowner = real_player;
327 minigame.pong_paddles[pl_team-1] = paddle;
333 // required function, handle server side events
334 int pong_server_event(entity minigame, string event, ...)
340 minigame.minigame_flags |= PONG_STATUS_WAIT;
345 // Don't allow joining a match that is already running
346 if ( minigame.minigame_flags & PONG_STATUS_PLAY )
349 entity player = ...(0,entity);
351 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
353 if ( minigame.pong_paddles[i] == world )
355 pong_paddle_spawn(minigame,i+1,player);
364 entity player = ...(0,entity);
367 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
369 paddle = minigame.pong_paddles[i];
370 if ( paddle != world && paddle.realowner == player )
372 pong_ai_spawn(paddle);
381 entity player = ...(0,entity);
385 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
387 minigame.minigame_flags = PONG_STATUS_PLAY |
388 (minigame.minigame_flags & ~PONG_STATUS_WAIT);
389 minigame.SendFlags |= MINIG_SF_UPDATE;
393 for ( i = 0; i < autocvar_sv_minigames_pong_ball_number; i++ )
395 ball = msle_spawn(minigame,"pong_ball");
396 ball.pong_length = autocvar_sv_minigames_pong_ball_radius;
397 pong_ball_reset(ball);
402 player.pong_keys |= PONG_KEY_INCREASE;
405 player.pong_keys |= PONG_KEY_DECREASE;
408 player.pong_keys &= ~PONG_KEY_INCREASE;
411 player.pong_keys &= ~PONG_KEY_DECREASE;
416 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
417 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
419 if ( minigame.pong_paddles[i] == world )
421 pong_paddle_spawn(minigame,i+1,world);
425 sprint(player.minigame_players,"Cannot spawn AI\n");
430 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
434 for ( i = PONG_MAX_PLAYERS-1; i >= 0; i-- )
436 paddle = minigame.pong_paddles[i];
437 if ( paddle != world &&
438 paddle.realowner.classname == "pong_ai" )
440 minigame.pong_paddles[i] = world;
441 remove(paddle.realowner);
447 sprint(player.minigame_players,"Cannot remove AI\n");
456 entity sent = ...(0,entity);
458 if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
460 WriteLong(MSG_ENTITY,sent.pong_score);
471 #include "waypointsprites.qh" // drawrotpic
473 float pong_team_to_angle(int nteam)
480 case 3: return M_PI*3/2;
481 case 4: return M_PI/2;
485 vector pong_team_to_color(int nteam)
489 case 1: return '1 0 0';
490 case 2: return '0 0 1';
491 case 3: return '1 1 0';
492 case 4: return '1 0 1';
493 default:return '1 1 1';
497 // Required function, draw the game board
498 void pong_hud_board(vector pos, vector mySize)
500 minigame_hud_fitsqare(pos, mySize);
501 minigame_hud_simpleboard(pos,mySize,minigame_texture("pong/board"));
506 FOREACH_MINIGAME_ENTITY(e)
508 if ( e.classname == "pong_ball" )
510 // Note: 4*radius = 2*diameter because the image is large enough to fit the glow around the ball
511 obj_size = minigame_hud_denormalize_size('4 4 0'*e.pong_length,pos,mySize);
512 obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
514 minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball"),
515 obj_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
517 minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball-glow"),
518 obj_size, pong_team_to_color(e.team),
519 panel_fg_alpha, DRAWFLAG_ADDITIVE );
521 else if ( e.classname == "pong_paddle" )
523 obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
524 obj_size = minigame_hud_denormalize_size(eX / 16 + eY*e.pong_length,pos,mySize);
526 drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle-glow"),
527 obj_size, obj_size/2, pong_team_to_color(e.team),
528 panel_fg_alpha, DRAWFLAG_ADDITIVE );
530 drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle"),
531 obj_size, obj_size/2, '1 1 1',
532 panel_fg_alpha, DRAWFLAG_NORMAL );
538 // Required function, draw the game status panel
539 void pong_hud_status(vector pos, vector mySize)
543 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
544 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
545 ts_y += hud_fontsize_y;
549 vector player_fontsize = hud_fontsize * 1.75;
550 ts_y = ( mySize_y - PONG_MAX_PLAYERS*player_fontsize_y ) / PONG_MAX_PLAYERS;
555 FOREACH_MINIGAME_ENTITY(e)
557 if ( e.classname == "minigame_player" || e.classname == "pong_ai" )
560 mypos_y += (e.team-1) * (player_fontsize_y + ts_y);
562 drawfill(mypos, ts, pong_team_to_color(e.team), 0.25, DRAWFLAG_ADDITIVE);
564 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
565 (e.minigame_playerslot ? GetPlayerName(e.minigame_playerslot-1) : _("AI")),
566 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
568 drawstring(mypos+eY*player_fontsize_y,ftos(e.pong_score),'48 48 0',
569 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
571 if ( e == minigame_self )
572 drawborderlines(1, mypos, ts, pong_team_to_color(e.team), 1, DRAWFLAG_NORMAL);
577 // convert minigame flags to a message
578 string pong_message(int mgflags)
580 string rmessage = "";
581 if (mgflags & PONG_STATUS_WAIT)
582 rmessage = _("Press ^1Start Match^7 to start the match with the current players");
586 // Required function, handle client events
587 int pong_client_event(entity minigame, string event, ...)
594 switch ( ...(0,int) )
600 minigame_cmd("+moved");
605 case K_KP_RIGHTARROW:
606 minigame_cmd("+movei");
611 switch ( ...(0,int) )
617 minigame_cmd("-moved");
622 case K_KP_RIGHTARROW:
623 minigame_cmd("-movei");
627 case "network_receive":
629 entity sent = ...(0,entity);
631 if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
633 sent.pong_score = ReadLong();
635 else if ( sent.classname == "minigame" )
637 if ( sf & MINIG_SF_UPDATE )
639 sent.message = pong_message(sent.minigame_flags);
646 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Start Match"),"pong_throw");
647 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Add AI player"),"pong_aimore");
648 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Remove AI player"),"pong_ailess");
653 string cmd = ...(0,string);
654 if( cmd == "pong_throw" && minigame.minigame_flags & PONG_STATUS_WAIT )
656 minigame_cmd("throw");
658 else if ( cmd == "pong_aimore" || cmd == "pong_ailess" )