2 REGISTER_MINIGAME(pong, "Pong");
5 const int PONG_STATUS_WAIT = 0x0010; // waiting for players to join
6 const int PONG_STATUS_PLAY = 0x0020; // playing
9 // (minigame_player) sent when reporting scores
10 const int PONG_SF_PLAYERSCORE = MINIG_SF_CUSTOM;
11 // (pong_ball) sent when changing team
12 const int PONG_SF_BALLTEAM = MINIG_SF_CUSTOM;
15 const int PONG_KEY_INCREASE = 0x01; // Move down/right
16 const int PONG_KEY_DECREASE = 0x02; // Move up/left
17 const int PONG_KEY_BOTH = 0x03; // Player jamming keys at ramdom
20 const int PONG_MAX_PLAYERS = 4;
21 .int pong_score; // (minigame_player) number of goals
22 .int pong_keys; // (client) pressed keys
23 .entity pong_paddles[PONG_MAX_PLAYERS];// (minigame) paddles
24 .float pong_length; // (pong_paddle/pong_ball) size (0,1)
25 .entity pong_ai_paddle; // (pong_ai) controlled paddle entity
29 float autocvar_sv_minigames_pong_paddle_size;
30 float autocvar_sv_minigames_pong_paddle_speed;
32 float autocvar_sv_minigames_pong_ball_wait;
33 float autocvar_sv_minigames_pong_ball_speed;
34 float autocvar_sv_minigames_pong_ball_radius;
35 float autocvar_sv_minigames_pong_ball_number;
37 float autocvar_sv_minigames_pong_ai_thinkspeed;
38 float autocvar_sv_minigames_pong_ai_tolerance;
40 void pong_ball_think(entity this);
42 // Throws a ball in a random direction and sets the think function
43 void pong_ball_throw(entity ball)
47 angle = random()*M_PI*2;
48 while ( fabs(sin(angle)) < 0.17 || fabs(cos(angle)) < 0.17 );
49 ball.velocity_x = cos(angle)*autocvar_sv_minigames_pong_ball_speed;
50 ball.velocity_y = sin(angle)*autocvar_sv_minigames_pong_ball_speed;
51 setthink(ball, pong_ball_think);
52 ball.nextthink = time;
54 ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
57 // Think equivalent of pong_ball_throw, used to delay throws
58 void pong_ball_throwthink(entity this)
60 pong_ball_throw(this);
63 // Moves ball to the center and stops its motion
64 void pong_ball_reset(entity ball)
66 ball.velocity = '0 0 0';
67 ball.origin = '0.5 0.5 0';
68 setthink(ball, SUB_NullThink);
70 ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
71 setthink(ball, pong_ball_throwthink);
72 ball.nextthink = time + autocvar_sv_minigames_pong_ball_wait;
75 // Add the score to the given team in the minigame
76 void pong_add_score(entity minigame, int team_thrower, int team_receiver, int delta)
81 if ( team_thrower == 0 )
82 team_thrower = team_receiver;
84 if ( team_thrower == team_receiver )
87 entity paddle_thrower = minigame.pong_paddles[team_thrower-1];
88 if ( paddle_thrower.realowner.minigame_players )
90 paddle_thrower.realowner.minigame_players.pong_score += delta;
91 paddle_thrower.realowner.minigame_players.SendFlags |= PONG_SF_PLAYERSCORE;
95 // get point in the box nearest to the given one (2D)
96 vector box_nearest(vector box_min, vector box_max, vector p)
98 return vec2( p.x > box_max.x ? box_max.x : ( p.x < box_min.x ? box_min.x : p.x ),
99 p.y > box_max.y ? box_max.y : ( p.y < box_min.y ? box_min.y : p.y ) );
102 void pong_paddle_bounce(entity ball, int pteam)
106 case 1: ball.velocity_x = -fabs(ball.velocity_x); break;
107 case 2: ball.velocity_x = fabs(ball.velocity_x); break;
108 case 3: ball.velocity_y = fabs(ball.velocity_y); break;
109 case 4: ball.velocity_y = -fabs(ball.velocity_y); break;
112 float angle = atan2(ball.velocity_y, ball.velocity_x);
113 angle += ( random() - 0.5 ) * 2 * M_PI/6;
114 float speed = vlen(ball.velocity);
116 ball.velocity_y = speed * sin(angle);
117 ball.velocity_x = speed * cos(angle);
120 // checks if the ball hit the paddle for the given team
121 bool pong_paddle_hit(entity ball, int pteam)
123 entity paddle = ball.owner.pong_paddles[pteam-1];
128 vector near_point = box_nearest(paddle.m_mins+paddle.origin,
129 paddle.m_maxs+paddle.origin, ball.origin);
130 return vdist(near_point - ball.origin, <=, ball.pong_length);
132 return boxesoverlap(paddle.m_mins + paddle.origin, paddle.m_maxs + paddle.origin, ball.m_mins + ball.origin, ball.m_maxs + ball.origin);
136 // Checks for a goal, when that happes adds scores and resets the ball
137 bool pong_goal(entity ball, int pteam)
139 entity paddle = ball.owner.pong_paddles[pteam-1];
143 if ( !pong_paddle_hit(ball, pteam) )
145 pong_add_score(ball.owner ,ball.team, pteam, 1);
146 pong_ball_reset(ball);
153 // Moves the ball around
154 void pong_ball_think(entity this)
156 float think_speed = autocvar_sys_ticrate;
157 this.nextthink = time + think_speed;
159 this.origin_x += this.velocity_x * think_speed;
160 this.origin_y += this.velocity_y * think_speed;
161 this.SendFlags |= MINIG_SF_UPDATE;
164 for ( i = 1; i <= PONG_MAX_PLAYERS; i++ )
165 if ( pong_paddle_hit(this, i) )
167 pong_paddle_bounce(this,i);
169 this.SendFlags |= PONG_SF_BALLTEAM;
173 if ( this.origin_y <= this.pong_length )
175 if ( !pong_goal(this,3) )
177 this.origin_y = this.pong_length;
178 this.velocity_y *= -1;
181 else if ( this.origin_y >= 1-this.pong_length )
183 if ( !pong_goal(this,4) )
185 this.origin_y = 1-this.pong_length;
186 this.velocity_y *= -1;
190 if ( this.origin_x <= this.pong_length )
192 if ( !pong_goal(this,2) )
194 this.origin_x = this.pong_length;
195 this.velocity_x *= -1;
198 else if ( this.origin_x >= 1-this.pong_length )
200 if ( !pong_goal(this,1) )
202 this.origin_x = 1-this.pong_length;
203 this.velocity_x *= -1;
210 void pong_ai_think(entity this)
212 float think_speed = autocvar_sv_minigames_pong_ai_thinkspeed;
213 this.nextthink = time + think_speed;
217 float min_distance = 1;
219 entity mayball = NULL;
220 while ( ( mayball = findentity(mayball,owner,this.owner) ) )
221 if ( mayball.classname == "pong_ball" )
223 distance = vlen(mayball.origin-this.pong_ai_paddle.origin);
224 next_distance = vlen(mayball.origin+mayball.velocity-this.pong_ai_paddle.origin);
225 if ( distance < min_distance && ( distance < 0.5 || next_distance < distance ) )
227 min_distance = distance;
236 if ( this.team <= 2 )
239 target = ball.origin_y + ball.velocity_y*think_speed;
240 my_pos = this.pong_ai_paddle.origin_y;
245 target = ball.origin_x + ball.velocity_x*think_speed;
246 my_pos = this.pong_ai_paddle.origin_x;
249 distance = this.pong_length/2 * autocvar_sv_minigames_pong_ai_tolerance
250 + autocvar_sv_minigames_pong_paddle_speed * think_speed;
252 if (target < my_pos - distance)
253 this.pong_keys = PONG_KEY_DECREASE;
254 else if (target > my_pos + distance)
255 this.pong_keys = PONG_KEY_INCREASE;
260 entity pong_ai_spawn(entity paddle)
262 entity ai = msle_spawn(paddle.owner,"pong_ai");
263 ai.minigame_players = ai;
264 ai.team = paddle.team;
265 setthink(ai, pong_ai_think);
267 ai.pong_ai_paddle = paddle;
269 paddle.realowner = ai;
275 void pong_paddle_think(entity this)
277 float think_speed = autocvar_sys_ticrate;
278 this.nextthink = time + think_speed;
280 if ( this.realowner.minigame_players.pong_keys == PONG_KEY_INCREASE ||
281 this.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
283 float pmovement = autocvar_sv_minigames_pong_paddle_speed * think_speed;
284 float halflen = this.pong_length/2;
286 if ( this.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
290 this.origin_x = bound(halflen, this.origin_x+pmovement, 1-halflen);
292 this.origin_y = bound(halflen, this.origin_y+pmovement, 1-halflen);
294 this.SendFlags |= MINIG_SF_UPDATE;
298 vector pong_team_to_box_halfsize(int nteam, float length, float width)
301 return vec2(length/2, width/2);
302 return vec2(width/2, length/2);
305 vector pong_team_to_paddlepos(int nteam)
309 case 1: return '0.99 0.5 0';
310 case 2: return '0.01 0.5 0';
311 case 3: return '0.5 0.01 0';
312 case 4: return '0.5 0.99 0';
313 default:return '0 0 0';
317 // Spawns a pong paddle
318 // if real_player is NULL, the paddle is controlled by AI
319 entity pong_paddle_spawn(entity minigame, int pl_team, entity real_player)
321 entity paddle = msle_spawn(minigame,"pong_paddle");
322 paddle.pong_length = autocvar_sv_minigames_pong_paddle_size;
323 paddle.origin = pong_team_to_paddlepos(pl_team);
324 setthink(paddle, pong_paddle_think);
325 paddle.nextthink = time;
326 paddle.team = pl_team;
327 paddle.m_mins = pong_team_to_box_halfsize(pl_team,-paddle.pong_length,-1/16);
328 paddle.m_maxs = pong_team_to_box_halfsize(pl_team,paddle.pong_length,1/16);
330 if ( real_player == NULL )
331 pong_ai_spawn(paddle);
333 paddle.realowner = real_player;
335 minigame.pong_paddles[pl_team-1] = paddle;
341 // required function, handle server side events
342 int pong_server_event(entity minigame, string event, ...)
348 minigame.minigame_flags |= PONG_STATUS_WAIT;
353 // Don't allow joining a match that is already running
354 if ( minigame.minigame_flags & PONG_STATUS_PLAY )
357 entity player = ...(0,entity);
359 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
361 if ( minigame.pong_paddles[i] == NULL )
363 pong_paddle_spawn(minigame,i+1,player);
372 entity player = ...(0,entity);
376 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
378 paddle = minigame.pong_paddles[i];
379 if ( paddle != NULL && paddle.realowner == player )
381 ai = pong_ai_spawn(paddle);
382 ai.pong_score = player.minigame_players.pong_score;
391 entity player = ...(0,entity);
395 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
397 minigame.minigame_flags = PONG_STATUS_PLAY |
398 (minigame.minigame_flags & ~PONG_STATUS_WAIT);
399 minigame.SendFlags |= MINIG_SF_UPDATE;
402 for ( int j = 0; j < autocvar_sv_minigames_pong_ball_number; j++ )
404 ball = msle_spawn(minigame,"pong_ball");
405 ball.pong_length = autocvar_sv_minigames_pong_ball_radius;
406 ball.m_mins = vec2(-ball.pong_length, -ball.pong_length);
407 ball.m_maxs = vec2(ball.pong_length, ball.pong_length);
408 pong_ball_reset(ball);
413 player.pong_keys |= PONG_KEY_INCREASE;
416 player.pong_keys |= PONG_KEY_DECREASE;
419 player.pong_keys &= ~PONG_KEY_INCREASE;
422 player.pong_keys &= ~PONG_KEY_DECREASE;
426 player.pong_keys = stoi(argv(1));
430 // keep declaration here, moving it into for() reverses weapon order
431 // potentially compiler bug
433 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
434 for ( j = 0; j < PONG_MAX_PLAYERS; j++ )
435 //for ( int j = 0; j < PONG_MAX_PLAYERS; j++ )
437 if ( minigame.pong_paddles[j] == NULL )
439 pong_paddle_spawn(minigame,j+1,NULL);
443 sprint(player.minigame_players,"Cannot spawn AI\n");
448 if ( minigame.minigame_flags & PONG_STATUS_WAIT )
451 for ( int j = PONG_MAX_PLAYERS-1; j >= 0; j-- )
453 paddle = minigame.pong_paddles[j];
454 if ( paddle != NULL &&
455 paddle.realowner.classname == "pong_ai" )
457 minigame.pong_paddles[j] = NULL;
458 delete(paddle.realowner);
464 sprint(player.minigame_players,"Cannot remove AI\n");
473 entity sent = ...(0,entity);
475 if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
477 WriteLong(MSG_ENTITY,sent.pong_score);
488 void drawrotpic(vector org, float rot, string pic, vector sz, vector hotspot, vector rgb, float a, float f);
490 float pong_team_to_angle(int nteam)
497 case 3: return M_PI*3/2;
498 case 4: return M_PI/2;
502 vector pong_team_to_color(int nteam)
506 case 1: return '1 0 0';
507 case 2: return '0 0 1';
508 case 3: return '1 1 0';
509 case 4: return '1 0 1';
510 default:return '1 1 1';
514 int pong_keys_pressed;
515 int pong_keys_pressed_old;
517 // Required function, draw the game board
518 void pong_hud_board(vector pos, vector mySize)
520 if(pong_keys_pressed != pong_keys_pressed_old)
521 minigame_cmd(strcat("move ", itos(pong_keys_pressed)));
522 pong_keys_pressed_old = pong_keys_pressed;
524 minigame_hud_fitsqare(pos, mySize);
525 minigame_hud_simpleboard(pos,mySize,minigame_texture("pong/board"));
530 FOREACH_MINIGAME_ENTITY(e)
532 if ( e.classname == "pong_ball" )
534 // Note: 4*radius = 2*diameter because the image is large enough to fit the glow around the ball
535 obj_size = minigame_hud_denormalize_size('4 4 0'*e.pong_length,pos,mySize);
536 obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
538 minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball"),
539 obj_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
541 minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball-glow"),
542 obj_size, pong_team_to_color(e.team),
543 panel_fg_alpha, DRAWFLAG_ADDITIVE );
545 else if ( e.classname == "pong_paddle" )
547 obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
548 obj_size = minigame_hud_denormalize_size(eX / 16 + eY*e.pong_length,pos,mySize);
550 drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle-glow"),
551 obj_size, obj_size/2, pong_team_to_color(e.team),
552 panel_fg_alpha, DRAWFLAG_ADDITIVE );
554 drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle"),
555 obj_size, obj_size/2, '1 1 1',
556 panel_fg_alpha, DRAWFLAG_NORMAL );
562 // Required function, draw the game status panel
563 void pong_hud_status(vector pos, vector mySize)
567 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
568 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
569 ts_y += hud_fontsize_y;
573 vector player_fontsize = hud_fontsize * 1.75;
574 ts_y = ( mySize_y - PONG_MAX_PLAYERS*player_fontsize_y ) / PONG_MAX_PLAYERS;
579 FOREACH_MINIGAME_ENTITY(e)
581 if ( e.classname == "minigame_player" || e.classname == "pong_ai" )
584 mypos_y += (e.team-1) * (player_fontsize_y + ts_y);
586 drawfill(mypos, ts, pong_team_to_color(e.team), 0.25, DRAWFLAG_ADDITIVE);
588 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
589 (e.minigame_playerslot ? entcs_GetName(e.minigame_playerslot-1) : _("AI")),
590 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
592 drawstring(mypos+eY*player_fontsize_y,ftos(e.pong_score),'48 48 0',
593 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
595 if ( e == minigame_self )
596 drawborderlines(1, mypos, ts, pong_team_to_color(e.team), 1, DRAWFLAG_NORMAL);
601 // convert minigame flags to a message
602 string pong_message(int mgflags)
604 string rmessage = "";
605 if (mgflags & PONG_STATUS_WAIT)
606 rmessage = _("Press ^1Start Match^7 to start the match with the current players");
610 // Required function, handle client events
611 int pong_client_event(entity minigame, string event, ...)
618 switch ( ...(0,int) )
624 //minigame_cmd("+moved");
625 pong_keys_pressed |= PONG_KEY_DECREASE;
630 case K_KP_RIGHTARROW:
631 //minigame_cmd("+movei");
632 pong_keys_pressed |= PONG_KEY_INCREASE;
637 switch ( ...(0,int) )
643 //minigame_cmd("-moved");
644 pong_keys_pressed &= ~PONG_KEY_DECREASE;
649 case K_KP_RIGHTARROW:
650 //minigame_cmd("-movei");
651 pong_keys_pressed &= ~PONG_KEY_INCREASE;
655 case "network_receive":
657 entity sent = ...(0,entity);
659 if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
661 sent.pong_score = ReadLong();
663 else if ( sent.classname == "minigame" )
665 if ( sf & MINIG_SF_UPDATE )
667 sent.message = pong_message(sent.minigame_flags);
674 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Start Match"),"pong_throw");
675 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Add AI player"),"pong_aimore");
676 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Remove AI player"),"pong_ailess");
681 string cmd = ...(0,string);
682 if( cmd == "pong_throw" && minigame.minigame_flags & PONG_STATUS_WAIT )
684 minigame_cmd("throw");
686 else if ( cmd == "pong_aimore" || cmd == "pong_ailess" )