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