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