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