Merge branch 'master' into terencehill/hud_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigame / pong.qc
1 #include "pong.qh"
2 REGISTER_MINIGAME(pong, "Pong");
3
4 // minigame flags
5 const int PONG_STATUS_WAIT = 0x0010; // waiting for players to join
6 const int PONG_STATUS_PLAY = 0x0020; // playing
7
8 // send flags
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;
13
14 // keys
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
18
19 // fields
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
26
27 #ifdef SVQC
28
29 float autocvar_sv_minigames_pong_paddle_size;
30 float autocvar_sv_minigames_pong_paddle_speed;
31
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;
36
37 float autocvar_sv_minigames_pong_ai_thinkspeed;
38 float autocvar_sv_minigames_pong_ai_tolerance;
39
40 void pong_ball_think(entity this);
41
42 // Throws a ball in a random direction and sets the think function
43 void pong_ball_throw(entity ball)
44 {
45         float angle;
46         do
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;
53         ball.team = 0;
54         ball.SendFlags |= MINIG_SF_UPDATE|PONG_SF_BALLTEAM;
55 }
56
57 // Think equivalent of pong_ball_throw, used to delay throws
58 void pong_ball_throwthink(entity this)
59 {
60         pong_ball_throw(this);
61 }
62
63 // Moves ball to the center and stops its motion
64 void pong_ball_reset(entity ball)
65 {
66         ball.velocity = '0 0 0';
67         ball.origin = '0.5 0.5 0';
68         setthink(ball, SUB_NullThink);
69         ball.team = 0;
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;
73 }
74
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)
77 {
78         if ( !minigame )
79                 return;
80
81         if ( team_thrower == 0 )
82                 team_thrower = team_receiver;
83
84         if ( team_thrower == team_receiver )
85                 delta *= -1;
86
87         entity paddle_thrower = minigame.pong_paddles[team_thrower-1];
88         if ( paddle_thrower.realowner.minigame_players )
89         {
90                 paddle_thrower.realowner.minigame_players.pong_score += delta;
91                 paddle_thrower.realowner.minigame_players.SendFlags |= PONG_SF_PLAYERSCORE;
92         }
93 }
94
95 // get point in the box nearest to the given one (2D)
96 vector box_nearest(vector box_min, vector box_max, vector p)
97 {
98         return eX * ( p_x > box_max_x  ? box_max_x  : ( p_x < box_min_x ? box_min_x : p_x ) )
99                 + eY * ( p_y > box_max_y  ? box_max_y  : ( p_y < box_min_y ? box_min_y : p_y ) );
100 }
101
102 void pong_paddle_bounce(entity ball, int pteam)
103 {
104         switch(pteam)
105         {
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;
110         }
111
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);
115
116         ball.velocity_y = speed * sin(angle);
117         ball.velocity_x = speed * cos(angle);
118 }
119
120 // checks if the ball hit the paddle for the given team
121 bool pong_paddle_hit(entity ball, int pteam)
122 {
123         entity paddle = ball.owner.pong_paddles[pteam-1];
124         if (!paddle)
125                 return false;
126         vector near_point = box_nearest(paddle.mins+paddle.origin,
127                                                                         paddle.maxs+paddle.origin, ball.origin);
128         return vdist(near_point - ball.origin, <=, ball.pong_length);
129 }
130
131 // Checks for a goal, when that happes adds scores and resets the ball
132 bool pong_goal(entity ball, int pteam)
133 {
134         entity paddle = ball.owner.pong_paddles[pteam-1];
135         if (!paddle)
136                 return false;
137
138         if ( !pong_paddle_hit(ball, pteam) )
139         {
140                 pong_add_score(ball.owner ,ball.team, pteam, 1);
141                 pong_ball_reset(ball);
142                 return true;
143         }
144
145         return false;
146 }
147
148 // Moves the ball around
149 void pong_ball_think(entity this)
150 {
151         float think_speed = autocvar_sys_ticrate;
152         this.nextthink = time + think_speed;
153
154         this.origin_x += this.velocity_x * think_speed;
155         this.origin_y += this.velocity_y * think_speed;
156         this.SendFlags |= MINIG_SF_UPDATE;
157
158         int i;
159         for ( i = 1; i <= PONG_MAX_PLAYERS; i++ )
160                 if ( pong_paddle_hit(this, i) )
161                 {
162                         pong_paddle_bounce(this,i);
163                         this.team = i;
164                         this.SendFlags |= PONG_SF_BALLTEAM;
165                         return;
166                 }
167
168         if ( this.origin_y <= this.pong_length )
169         {
170                 if ( !pong_goal(this,3) )
171                 {
172                         this.origin_y = this.pong_length;
173                         this.velocity_y *= -1;
174                 }
175         }
176         else if ( this.origin_y >= 1-this.pong_length )
177         {
178                 if ( !pong_goal(this,4) )
179                 {
180                         this.origin_y = 1-this.pong_length;
181                         this.velocity_y *= -1;
182                 }
183         }
184
185         if ( this.origin_x <= this.pong_length )
186         {
187                 if ( !pong_goal(this,2) )
188                 {
189                          this.origin_x = this.pong_length;
190                          this.velocity_x *= -1;
191                 }
192         }
193         else if ( this.origin_x >= 1-this.pong_length )
194         {
195                 if ( !pong_goal(this,1) )
196                 {
197                          this.origin_x = 1-this.pong_length;
198                          this.velocity_x *= -1;
199                 }
200         }
201
202 }
203
204 // AI action
205 void pong_ai_think(entity this)
206 {
207         float think_speed = autocvar_sv_minigames_pong_ai_thinkspeed;
208         this.nextthink = time + think_speed;
209
210         float distance;
211         float next_distance;
212         float min_distance = 1;
213         entity ball = NULL;
214         entity mayball = NULL;
215         while ( ( mayball = findentity(mayball,owner,this.owner) ) )
216                 if ( mayball.classname == "pong_ball" )
217                 {
218                         distance = vlen(mayball.origin-this.pong_ai_paddle.origin);
219                         next_distance = vlen(mayball.origin+mayball.velocity-this.pong_ai_paddle.origin);
220                         if (  distance < min_distance && ( distance < 0.5 || next_distance < distance ) )
221                         {
222                                 min_distance = distance;
223                                 ball = mayball;
224                         }
225                 }
226
227         float target = 0.5;
228         float my_pos;
229
230
231         if ( this.team <= 2 )
232         {
233                 if ( ball )
234                         target = ball.origin_y + ball.velocity_y*think_speed;
235                 my_pos = this.pong_ai_paddle.origin_y;
236         }
237         else
238         {
239                 if ( ball )
240                         target = ball.origin_x + ball.velocity_x*think_speed;
241                 my_pos = this.pong_ai_paddle.origin_x;
242         }
243
244         distance = this.pong_length/2 * autocvar_sv_minigames_pong_ai_tolerance
245                 + autocvar_sv_minigames_pong_paddle_speed * think_speed;
246
247         if (target < my_pos - distance)
248                 this.pong_keys = PONG_KEY_DECREASE;
249         else if (target > my_pos + distance)
250                 this.pong_keys = PONG_KEY_INCREASE;
251         else
252                 this.pong_keys = 0;
253 }
254
255 entity pong_ai_spawn(entity paddle)
256 {
257         entity ai = msle_spawn(paddle.owner,"pong_ai");
258         ai.minigame_players = ai;
259         ai.team = paddle.team;
260         setthink(ai, pong_ai_think);
261         ai.nextthink = time;
262         ai.pong_ai_paddle = paddle;
263
264         paddle.realowner = ai;
265
266         return ai;
267 }
268
269 // Moves the paddle
270 void pong_paddle_think(entity this)
271 {
272         float think_speed = autocvar_sys_ticrate;
273         this.nextthink = time + think_speed;
274
275         if ( this.realowner.minigame_players.pong_keys == PONG_KEY_INCREASE ||
276                  this.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
277         {
278                 float movement = autocvar_sv_minigames_pong_paddle_speed * think_speed;
279                 float halflen = this.pong_length/2;
280
281                 if ( this.realowner.minigame_players.pong_keys == PONG_KEY_DECREASE )
282                         movement *= -1;
283
284                 if ( this.team > 2 )
285                         this.origin_x = bound(halflen, this.origin_x+movement, 1-halflen);
286                 else
287                         this.origin_y = bound(halflen, this.origin_y+movement, 1-halflen);
288
289                 this.SendFlags |= MINIG_SF_UPDATE;
290         }
291 }
292
293 vector pong_team_to_box_halfsize(int nteam, float length, float width)
294 {
295         if ( nteam > 2 )
296                 return eY*width/2 + eX*length/2;
297         return eX*width/2 + eY*length/2;
298 }
299
300 vector pong_team_to_paddlepos(int nteam)
301 {
302         switch(nteam)
303         {
304                 case 1: return '0.99 0.5 0';
305                 case 2: return '0.01 0.5 0';
306                 case 3: return '0.5 0.01 0';
307                 case 4: return '0.5 0.99 0';
308                 default:return '0 0 0';
309         }
310 }
311
312 // Spawns a pong paddle
313 // if real_player is NULL, the paddle is controlled by AI
314 entity pong_paddle_spawn(entity minigame, int pl_team, entity real_player)
315 {
316         entity paddle = msle_spawn(minigame,"pong_paddle");
317         paddle.pong_length = autocvar_sv_minigames_pong_paddle_size;
318         paddle.origin = pong_team_to_paddlepos(pl_team);
319         setthink(paddle, pong_paddle_think);
320         paddle.nextthink = time;
321         paddle.team = pl_team;
322         paddle.mins = pong_team_to_box_halfsize(pl_team,-paddle.pong_length,-1/16);
323         paddle.maxs = pong_team_to_box_halfsize(pl_team,paddle.pong_length,1/16);
324
325         if ( real_player == NULL )
326                 pong_ai_spawn(paddle);
327         else
328                 paddle.realowner = real_player;
329
330         minigame.pong_paddles[pl_team-1] = paddle;
331
332         return paddle;
333
334 }
335
336 // required function, handle server side events
337 int pong_server_event(entity minigame, string event, ...)
338 {
339         switch (event)
340         {
341                 case "start":
342                 {
343                         minigame.minigame_flags |= PONG_STATUS_WAIT;
344                         return true;
345                 }
346                 case "join":
347                 {
348                         // Don't allow joining a match that is already running
349                         if ( minigame.minigame_flags & PONG_STATUS_PLAY )
350                                 return false;
351
352                         entity player = ...(0,entity);
353                         int i;
354                         for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
355                         {
356                                 if ( minigame.pong_paddles[i] == NULL )
357                                 {
358                                         pong_paddle_spawn(minigame,i+1,player);
359                                         return i+1;
360                                 }
361                         }
362
363                         return false;
364                 }
365                 case "part":
366                 {
367                         entity player = ...(0,entity);
368                         entity paddle;
369                         entity ai;
370                         int i;
371                         for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
372                         {
373                                 paddle = minigame.pong_paddles[i];
374                                 if ( paddle != NULL && paddle.realowner == player )
375                                 {
376                                         ai = pong_ai_spawn(paddle);
377                                         ai.pong_score = player.minigame_players.pong_score;
378                                         break;
379                                 }
380
381                         }
382                         return false;
383                 }
384                 case "cmd":
385                 {
386                         entity player = ...(0,entity);
387                         switch(argv(0))
388                         {
389                                 case "throw":
390                                         if ( minigame.minigame_flags & PONG_STATUS_WAIT )
391                                         {
392                                                 minigame.minigame_flags = PONG_STATUS_PLAY |
393                                                         (minigame.minigame_flags & ~PONG_STATUS_WAIT);
394                                                 minigame.SendFlags |= MINIG_SF_UPDATE;
395
396                                                 int i;
397                                                 entity ball;
398                                                 for ( i = 0; i < autocvar_sv_minigames_pong_ball_number; i++ )
399                                                 {
400                                                         ball = msle_spawn(minigame,"pong_ball");
401                                                         ball.pong_length = autocvar_sv_minigames_pong_ball_radius;
402                                                         pong_ball_reset(ball);
403                                                 }
404                                         }
405                                         return true;
406                                 case "+movei":
407                                         player.pong_keys |= PONG_KEY_INCREASE;
408                                         return true;
409                                 case "+moved":
410                                         player.pong_keys |= PONG_KEY_DECREASE;
411                                         return true;
412                                 case "-movei":
413                                         player.pong_keys &= ~PONG_KEY_INCREASE;
414                                         return true;
415                                 case "-moved":
416                                         player.pong_keys &= ~PONG_KEY_DECREASE;
417                                         return true;
418                                 case "pong_aimore":
419                                 {
420                                         int i;
421                                         if ( minigame.minigame_flags & PONG_STATUS_WAIT )
422                                                 for ( i = 0; i < PONG_MAX_PLAYERS; i++ )
423                                                 {
424                                                         if ( minigame.pong_paddles[i] == NULL )
425                                                         {
426                                                                 pong_paddle_spawn(minigame,i+1,NULL);
427                                                                 return true;
428                                                         }
429                                                 }
430                                         sprint(player.minigame_players,"Cannot spawn AI\n");
431                                         return true;
432                                 }
433                                 case "pong_ailess":
434                                 {
435                                         if ( minigame.minigame_flags & PONG_STATUS_WAIT )
436                                         {
437                                                 entity paddle;
438                                                 int i;
439                                                 for ( i = PONG_MAX_PLAYERS-1; i >= 0; i-- )
440                                                 {
441                                                         paddle = minigame.pong_paddles[i];
442                                                         if ( paddle != NULL &&
443                                                                 paddle.realowner.classname == "pong_ai" )
444                                                         {
445                                                                 minigame.pong_paddles[i] = NULL;
446                                                                 delete(paddle.realowner);
447                                                                 delete(paddle);
448                                                                 return true;
449                                                         }
450                                                 }
451                                         }
452                                         sprint(player.minigame_players,"Cannot remove AI\n");
453                                         return true;
454                                 }
455
456                         }
457                         return false;
458                 }
459                 case "network_send":
460                 {
461                         entity sent = ...(0,entity);
462                         int sf = ...(1,int);
463                         if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
464                         {
465                                 WriteLong(MSG_ENTITY,sent.pong_score);
466                         }
467                         return false;
468                 }
469         }
470         return false;
471 }
472
473
474 #elif defined(CSQC)
475
476 void drawrotpic(vector org, float rot, string pic, vector sz, vector hotspot, vector rgb, float a, float f);
477
478 float pong_team_to_angle(int nteam)
479 {
480         switch(nteam)
481         {
482                 default:
483                 case 1: return 0;
484                 case 2: return M_PI;
485                 case 3: return M_PI*3/2;
486                 case 4: return M_PI/2;
487         }
488 }
489
490 vector pong_team_to_color(int nteam)
491 {
492         switch(nteam)
493         {
494                 case 1: return '1 0 0';
495                 case 2: return '0 0 1';
496                 case 3: return '1 1 0';
497                 case 4: return '1 0 1';
498                 default:return '1 1 1';
499         }
500 }
501
502 // Required function, draw the game board
503 void pong_hud_board(vector pos, vector mySize)
504 {
505         minigame_hud_fitsqare(pos, mySize);
506         minigame_hud_simpleboard(pos,mySize,minigame_texture("pong/board"));
507
508         entity e;
509         vector obj_pos;
510         vector obj_size;
511         FOREACH_MINIGAME_ENTITY(e)
512         {
513                 if ( e.classname == "pong_ball" )
514                 {
515                         // Note: 4*radius = 2*diameter because the image is large enough to fit the glow around the ball
516                         obj_size =  minigame_hud_denormalize_size('4 4 0'*e.pong_length,pos,mySize);
517                         obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
518
519                         minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball"),
520                                         obj_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
521
522                         minigame_drawpic_centered( obj_pos, minigame_texture("pong/ball-glow"),
523                                         obj_size, pong_team_to_color(e.team),
524                                         panel_fg_alpha, DRAWFLAG_ADDITIVE );
525                 }
526                 else if ( e.classname == "pong_paddle" )
527                 {
528                         obj_pos = minigame_hud_denormalize(e.origin,pos,mySize);
529                         obj_size = minigame_hud_denormalize_size(eX / 16 + eY*e.pong_length,pos,mySize);
530
531                         drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle-glow"),
532                                 obj_size, obj_size/2, pong_team_to_color(e.team),
533                                 panel_fg_alpha, DRAWFLAG_ADDITIVE );
534
535                         drawrotpic(obj_pos, pong_team_to_angle(e.team), minigame_texture("pong/paddle"),
536                                 obj_size, obj_size/2, '1 1 1',
537                                 panel_fg_alpha, DRAWFLAG_NORMAL );
538
539                 }
540         }
541 }
542
543 // Required function, draw the game status panel
544 void pong_hud_status(vector pos, vector mySize)
545 {
546         HUD_Panel_DrawBg();
547         vector ts;
548         ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
549                 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
550         ts_y += hud_fontsize_y;
551         pos_y += ts_y;
552         mySize_y -= ts_y;
553
554         vector player_fontsize = hud_fontsize * 1.75;
555         ts_y = ( mySize_y - PONG_MAX_PLAYERS*player_fontsize_y ) / PONG_MAX_PLAYERS;
556         ts_x = mySize_x;
557         vector mypos;
558
559         entity e;
560         FOREACH_MINIGAME_ENTITY(e)
561         {
562                 if ( e.classname == "minigame_player" || e.classname == "pong_ai" )
563                 {
564                         mypos = pos;
565                         mypos_y  += (e.team-1) * (player_fontsize_y + ts_y);
566
567                         drawfill(mypos, ts, pong_team_to_color(e.team), 0.25, DRAWFLAG_ADDITIVE);
568
569                         minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
570                                 (e.minigame_playerslot ? entcs_GetName(e.minigame_playerslot-1) : _("AI")),
571                                 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
572
573                         drawstring(mypos+eY*player_fontsize_y,ftos(e.pong_score),'48 48 0',
574                                            '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
575
576                         if ( e == minigame_self )
577                                 drawborderlines(1, mypos, ts, pong_team_to_color(e.team), 1, DRAWFLAG_NORMAL);
578                 }
579         }
580 }
581
582 // convert minigame flags to a message
583 string pong_message(int mgflags)
584 {
585         string rmessage = "";
586         if (mgflags & PONG_STATUS_WAIT)
587                 rmessage = _("Press ^1Start Match^7 to start the match with the current players");
588         return rmessage;
589 }
590
591 // Required function, handle client events
592 int pong_client_event(entity minigame, string event, ...)
593 {
594         switch(event)
595         {
596                 case "activate":
597                         return false;
598                 case "key_pressed":
599                         switch ( ...(0,int) )
600                         {
601                                 case K_UPARROW:
602                                 case K_KP_UPARROW:
603                                 case K_LEFTARROW:
604                                 case K_KP_LEFTARROW:
605                                         minigame_cmd("+moved");
606                                         return true;
607                                 case K_DOWNARROW:
608                                 case K_KP_DOWNARROW:
609                                 case K_RIGHTARROW:
610                                 case K_KP_RIGHTARROW:
611                                         minigame_cmd("+movei");
612                                         return true;
613                         }
614                         return false;
615                 case "key_released":
616                         switch ( ...(0,int) )
617                         {
618                                 case K_UPARROW:
619                                 case K_KP_UPARROW:
620                                 case K_LEFTARROW:
621                                 case K_KP_LEFTARROW:
622                                         minigame_cmd("-moved");
623                                         return true;
624                                 case K_DOWNARROW:
625                                 case K_KP_DOWNARROW:
626                                 case K_RIGHTARROW:
627                                 case K_KP_RIGHTARROW:
628                                         minigame_cmd("-movei");
629                                         return true;
630                         }
631                         return false;
632                 case "network_receive":
633                 {
634                         entity sent = ...(0,entity);
635                         int sf = ...(1,int);
636                         if ( sent.classname == "minigame_player" && (sf & PONG_SF_PLAYERSCORE ) )
637                         {
638                                 sent.pong_score = ReadLong();
639                         }
640                         else if ( sent.classname == "minigame" )
641                         {
642                                 if ( sf & MINIG_SF_UPDATE )
643                                 {
644                                         sent.message = pong_message(sent.minigame_flags);
645                                 }
646                         }
647                         return false;
648                 }
649                 case "menu_show":
650                 {
651                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Start Match"),"pong_throw");
652                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Add AI player"),"pong_aimore");
653                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Remove AI player"),"pong_ailess");
654                         return false;
655                 }
656                 case "menu_click":
657                 {
658                         string cmd = ...(0,string);
659                         if( cmd == "pong_throw" && minigame.minigame_flags & PONG_STATUS_WAIT )
660                         {
661                                 minigame_cmd("throw");
662                         }
663                         else if ( cmd == "pong_aimore" || cmd == "pong_ailess" )
664                         {
665                                 minigame_cmd(cmd);
666                         }
667                         return false;
668                 }
669         }
670
671         return false;
672 }
673 #endif