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