1 REGISTER_MINIGAME(pong, "Pong");
4 const int PONG_STATUS_WAIT = 0x0010; // waiting for players to join
5 const int PONG_STATUS_PLAY = 0x0020; // playing
8 // (minigame_player) sent when reporting scores
9 const int PONG_SF_PLAYERSCORE = MINIG_SF_CUSTOM;
10 // (pong_ball) sent when changing team
11 const int PONG_SF_BALLTEAM = MINIG_SF_CUSTOM;
14 const int PONG_KEY_INCREASE = 0x01; // Move down/right
15 const int PONG_KEY_DECREASE = 0x02; // Move up/left
16 const int PONG_KEY_BOTH = 0x03; // Player jamming keys at ramdom
19 const int PONG_MAX_PLAYERS = 4;
20 .int pong_score; // (minigame_player) number of goals
21 .int pong_keys; // (client) pressed keys
22 .entity pong_paddles[PONG_MAX_PLAYERS];// (minigame) paddles
23 .float pong_length; // (pong_paddle/pong_ball) size (0,1)
24 .entity pong_ai_paddle; // (pong_ai) controlled paddle entity
28 float autocvar_sv_minigames_pong_paddle_size;
29 float autocvar_sv_minigames_pong_paddle_speed;
31 float autocvar_sv_minigames_pong_ball_wait;
32 float autocvar_sv_minigames_pong_ball_speed;
33 float autocvar_sv_minigames_pong_ball_radius;
34 float autocvar_sv_minigames_pong_ball_number;
36 float autocvar_sv_minigames_pong_ai_thinkspeed;
37 float autocvar_sv_minigames_pong_ai_tolerance;
39 void pong_ball_think();
41 // Throws a ball in a random direction and sets the think function
42 void pong_ball_throw(entity ball)
46 angle = random()*M_PI*2;
47 while ( fabs(sin(angle)) < 0.17 || fabs(cos(angle)) < 0.17 );
48 ball.velocity_x = cos(angle)*autocvar_sv_minigames_pong_ball_speed;
49 ball.velocity_y = sin(angle)*autocvar_sv_minigames_pong_ball_speed;
50 ball.think = pong_ball_think;
51 ball.nextthink = time;
53 ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
56 // Think equivalent of pong_ball_throw, used to delay throws
57 void pong_ball_throwthink()
59 pong_ball_throw(self);
62 // Moves ball to the center and stops its motion
63 void pong_ball_reset(entity ball)
65 ball.velocity = '0 0 0';
66 ball.origin = '0.5 0.5 0';
67 ball.think = SUB_NullThink;
69 ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
70 ball.think = pong_ball_throwthink;
71 ball.nextthink = time + autocvar_sv_minigames_pong_ball_wait;
74 // Add the score to the given team in the minigame
75 void pong_add_score(entity minigame, int team_thrower, int team_receiver, int delta)
80 if ( team_thrower == 0 )
81 team_thrower = team_receiver;
83 if ( team_thrower == team_receiver )
86 entity paddle_thrower = minigame.pong_paddles[team_thrower-1];
87 if ( paddle_thrower.realowner.minigame_players )
89 paddle_thrower.realowner.minigame_players.pong_score += delta;
90 paddle_thrower.realowner.minigame_players.SendFlags |= PONG_SF_PLAYERSCORE;
94 // get point in the box nearest to the given one (2D)
95 vector box_nearest(vector box_min, vector box_max, vector p)
97 return eX * ( p_x > box_max_x ? box_max_x : ( p_x < box_min_x ? box_min_x : p_x ) )
98 + eY * ( p_y > box_max_y ? box_max_y : ( p_y < box_min_y ? box_min_y : p_y ) );
101 void pong_paddle_bounce(entity ball, int pteam)
105 case 1: ball.velocity_x = -fabs(ball.velocity_x); break;
106 case 2: ball.velocity_x = fabs(ball.velocity_x); break;
107 case 3: ball.velocity_y = fabs(ball.velocity_y); break;
108 case 4: ball.velocity_y = -fabs(ball.velocity_y); break;
111 float angle = atan2(ball.velocity_y, ball.velocity_x);
112 angle += ( random() - 0.5 ) * 2 * M_PI/6;
113 float speed = vlen(ball.velocity);
115 ball.velocity_y = speed * sin(angle);
116 ball.velocity_x = speed * cos(angle);
119 // checks if the ball hit the paddle for the given team
120 bool pong_paddle_hit(entity ball, int pteam)
122 entity paddle = ball.owner.pong_paddles[pteam-1];
125 vector near_point = box_nearest(paddle.mins+paddle.origin,
126 paddle.maxs+paddle.origin, ball.origin);
127 return vlen(near_point-ball.origin) <= ball.pong_length ;
130 // Checks for a goal, when that happes adds scores and resets the ball
131 bool pong_goal(entity ball, int pteam)
133 entity paddle = ball.owner.pong_paddles[pteam-1];
137 if ( !pong_paddle_hit(ball, pteam) )
139 pong_add_score(ball.owner ,ball.team, pteam, 1);
140 pong_ball_reset(ball);
147 // Moves the ball around
148 void pong_ball_think()
150 float think_speed = autocvar_sys_ticrate;
151 self.nextthink = time + think_speed;
153 self.origin_x += self.velocity_x * think_speed;
154 self.origin_y += self.velocity_y * think_speed;
155 self.SendFlags |= MINIG_SF_UPDATE;
158 for ( i = 1; i <= PONG_MAX_PLAYERS; i++ )
159 if ( pong_paddle_hit(self, i) )
161 pong_paddle_bounce(self,i);
163 self.SendFlags |= PONG_SF_BALLTEAM;
167 if ( self.origin_y <= self.pong_length )
169 if ( !pong_goal(self,3) )
171 self.origin_y = self.pong_length;
172 self.velocity_y *= -1;
175 else if ( self.origin_y >= 1-self.pong_length )
177 if ( !pong_goal(self,4) )
179 self.origin_y = 1-self.pong_length;
180 self.velocity_y *= -1;
184 if ( self.origin_x <= self.pong_length )
186 if ( !pong_goal(self,2) )
188 self.origin_x = self.pong_length;
189 self.velocity_x *= -1;
192 else if ( self.origin_x >= 1-self.pong_length )
194 if ( !pong_goal(self,1) )
196 self.origin_x = 1-self.pong_length;
197 self.velocity_x *= -1;
206 float think_speed = autocvar_sv_minigames_pong_ai_thinkspeed;
207 self.nextthink = time + think_speed;
211 float min_distance = 1;
213 entity mayball = world;
214 while ( ( mayball = findentity(mayball,owner,self.owner) ) )
215 if ( mayball.classname == "pong_ball" )
217 distance = vlen(mayball.origin-self.pong_ai_paddle.origin);
218 next_distance = vlen(mayball.origin+mayball.velocity-self.pong_ai_paddle.origin);
219 if ( distance < min_distance && ( distance < 0.5 || next_distance < distance ) )
221 min_distance = distance;
230 if ( self.team <= 2 )
233 target = ball.origin_y + ball.velocity_y*think_speed;
234 self_pos = self.pong_ai_paddle.origin_y;
239 target = ball.origin_x + ball.velocity_x*think_speed;
240 self_pos = self.pong_ai_paddle.origin_x;
243 distance = self.pong_length/2 * autocvar_sv_minigames_pong_ai_tolerance
244 + autocvar_sv_minigames_pong_paddle_speed * think_speed;
246 if (target < self_pos - distance)
247 self.pong_keys = PONG_KEY_DECREASE;
248 else if (target > self_pos + distance)
249 self.pong_keys = PONG_KEY_INCREASE;
254 entity pong_ai_spawn(entity paddle)
256 entity ai = msle_spawn(paddle.owner,"pong_ai");
257 ai.minigame_players = ai;
258 ai.team = paddle.team;
259 ai.think = pong_ai_think;
261 ai.pong_ai_paddle = paddle;
263 paddle.realowner = ai;
269 void pong_paddle_think()
271 float think_speed = autocvar_sys_ticrate;
272 self.nextthink = time + think_speed;
274 if ( self.realowner.minigame_players.pong_keys == PONG_KEY_INCREASE ||
275 self.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
277 float movement = autocvar_sv_minigames_pong_paddle_speed * think_speed;
278 float halflen = self.pong_length/2;
280 if ( self.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
284 self.origin_x = bound(halflen, self.origin_x+movement, 1-halflen);
286 self.origin_y = bound(halflen, self.origin_y+movement, 1-halflen);
288 self.SendFlags |= MINIG_SF_UPDATE;
292 vector pong_team_to_box_halfsize(int nteam, float length, float width)
295 return eY*width/2 + eX*length/2;
296 return eX*width/2 + eY*length/2;
299 vector pong_team_to_paddlepos(int nteam)
303 case 1: return '0.99 0.5 0';
304 case 2: return '0.01 0.5 0';
305 case 3: return '0.5 0.01 0';
306 case 4: return '0.5 0.99 0';
307 default:return '0 0 0';
311 // Spawns a pong paddle
312 // if real_player is world, the paddle is controlled by AI
313 entity pong_paddle_spawn(entity minigame, int pl_team, entity real_player)
315 entity paddle = msle_spawn(minigame,"pong_paddle");
316 paddle.pong_length = autocvar_sv_minigames_pong_paddle_size;
317 paddle.origin = pong_team_to_paddlepos(pl_team);
318 paddle.think = pong_paddle_think;
319 paddle.nextthink = time;
320 paddle.team = pl_team;
321 paddle.mins = pong_team_to_box_halfsize(pl_team,-paddle.pong_length,-1/16);
322 paddle.maxs = pong_team_to_box_halfsize(pl_team,paddle.pong_length,1/16);
324 if ( real_player == world )
325 pong_ai_spawn(paddle);
327 paddle.realowner = real_player;
329 minigame.pong_paddles[pl_team-1] = paddle;
335 // required function, handle server side events
336 int pong_server_event(entity minigame, string event, ...)
342 minigame.minigame_flags |= PONG_STATUS_WAIT;
347 // Don't allow joining a match that is already running
348 if ( minigame.minigame_flags & PONG_STATUS_PLAY )
351 entity player = ...(0,entity);
353 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
355 if ( minigame.pong_paddles[i] == world )
357 pong_paddle_spawn(minigame,i+1,player);
366 entity player = ...(0,entity);
370 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
372 paddle = minigame.pong_paddles[i];
373 if ( paddle != world && paddle.realowner == player )
375 ai = pong_ai_spawn(paddle);
376 ai.pong_score = player.minigame_players.pong_score;
385 entity player = ...(0,entity);
389 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
391 minigame.minigame_flags = PONG_STATUS_PLAY |
392 (minigame.minigame_flags & ~PONG_STATUS_WAIT);
393 minigame.SendFlags |= MINIG_SF_UPDATE;
397 for ( i = 0; i < autocvar_sv_minigames_pong_ball_number; i++ )
399 ball = msle_spawn(minigame,"pong_ball");
400 ball.pong_length = autocvar_sv_minigames_pong_ball_radius;
401 pong_ball_reset(ball);
406 player.pong_keys |= PONG_KEY_INCREASE;
409 player.pong_keys |= PONG_KEY_DECREASE;
412 player.pong_keys &= ~PONG_KEY_INCREASE;
415 player.pong_keys &= ~PONG_KEY_DECREASE;
420 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
421 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
423 if ( minigame.pong_paddles[i] == world )
425 pong_paddle_spawn(minigame,i+1,world);
429 sprint(player.minigame_players,"Cannot spawn AI\n");
434 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
438 for ( i = PONG_MAX_PLAYERS-1; i >= 0; i-- )
440 paddle = minigame.pong_paddles[i];
441 if ( paddle != world &&
442 paddle.realowner.classname == "pong_ai" )
444 minigame.pong_paddles[i] = world;
445 remove(paddle.realowner);
451 sprint(player.minigame_players,"Cannot remove AI\n");
460 entity sent = ...(0,entity);
462 if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
464 WriteLong(MSG_ENTITY,sent.pong_score);
475 void drawrotpic(vector org, float rot, string pic, vector sz, vector hotspot, vector rgb, float a, float f);
477 float pong_team_to_angle(int nteam)
484 case 3: return M_PI*3/2;
485 case 4: return M_PI/2;
489 vector pong_team_to_color(int nteam)
493 case 1: return '1 0 0';
494 case 2: return '0 0 1';
495 case 3: return '1 1 0';
496 case 4: return '1 0 1';
497 default:return '1 1 1';
501 // Required function, draw the game board
502 void pong_hud_board(vector pos, vector mySize)
504 minigame_hud_fitsqare(pos, mySize);
505 minigame_hud_simpleboard(pos,mySize,minigame_texture("pong/board"));
510 FOREACH_MINIGAME_ENTITY(e)
512 if ( e.classname == "pong_ball" )
514 // Note: 4*radius = 2*diameter because the image is large enough to fit the glow around the ball
515 obj_size = minigame_hud_denormalize_size('4 4 0'*e.pong_length,pos,mySize);
516 obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
518 minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball"),
519 obj_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
521 minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball-glow"),
522 obj_size, pong_team_to_color(e.team),
523 panel_fg_alpha, DRAWFLAG_ADDITIVE );
525 else if ( e.classname == "pong_paddle" )
527 obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
528 obj_size = minigame_hud_denormalize_size(eX / 16 + eY*e.pong_length,pos,mySize);
530 drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle-glow"),
531 obj_size, obj_size/2, pong_team_to_color(e.team),
532 panel_fg_alpha, DRAWFLAG_ADDITIVE );
534 drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle"),
535 obj_size, obj_size/2, '1 1 1',
536 panel_fg_alpha, DRAWFLAG_NORMAL );
542 // Required function, draw the game status panel
543 void pong_hud_status(vector pos, vector mySize)
547 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
548 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
549 ts_y += hud_fontsize_y;
553 vector player_fontsize = hud_fontsize * 1.75;
554 ts_y = ( mySize_y - PONG_MAX_PLAYERS*player_fontsize_y ) / PONG_MAX_PLAYERS;
559 FOREACH_MINIGAME_ENTITY(e)
561 if ( e.classname == "minigame_player" || e.classname == "pong_ai" )
564 mypos_y += (e.team-1) * (player_fontsize_y + ts_y);
566 drawfill(mypos, ts, pong_team_to_color(e.team), 0.25, DRAWFLAG_ADDITIVE);
568 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
569 (e.minigame_playerslot ? GetPlayerName(e.minigame_playerslot-1) : _("AI")),
570 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
572 drawstring(mypos+eY*player_fontsize_y,ftos(e.pong_score),'48 48 0',
573 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
575 if ( e == minigame_self )
576 drawborderlines(1, mypos, ts, pong_team_to_color(e.team), 1, DRAWFLAG_NORMAL);
581 // convert minigame flags to a message
582 string pong_message(int mgflags)
584 string rmessage = "";
585 if (mgflags & PONG_STATUS_WAIT)
586 rmessage = _("Press ^1Start Match^7 to start the match with the current players");
590 // Required function, handle client events
591 int pong_client_event(entity minigame, string event, ...)
598 switch ( ...(0,int) )
604 minigame_cmd("+moved");
609 case K_KP_RIGHTARROW:
610 minigame_cmd("+movei");
615 switch ( ...(0,int) )
621 minigame_cmd("-moved");
626 case K_KP_RIGHTARROW:
627 minigame_cmd("-movei");
631 case "network_receive":
633 entity sent = ...(0,entity);
635 if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
637 sent.pong_score = ReadLong();
639 else if ( sent.classname == "minigame" )
641 if ( sf & MINIG_SF_UPDATE )
643 sent.message = pong_message(sent.minigame_flags);
650 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Start Match"),"pong_throw");
651 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Add AI player"),"pong_aimore");
652 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Remove AI player"),"pong_ailess");
657 string cmd = ...(0,string);
658 if( cmd == "pong_throw" && minigame.minigame_flags & PONG_STATUS_WAIT )
660 minigame_cmd("throw");
662 else if ( cmd == "pong_aimore" || cmd == "pong_ailess" )