]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_keepaway.qc
Merge branch 'master' into TimePath/scoreboard_elo
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_keepaway.qc
1 #include "gamemode_keepaway.qh"
2 #ifndef GAMEMODE_KEEPAWAY_H
3 #define GAMEMODE_KEEPAWAY_H
4
5 void ka_Initialize();
6
7 REGISTER_MUTATOR(ka, false)
8 {
9         MUTATOR_ONADD
10         {
11                 if (time > 1) // game loads at time 1
12                         error("This is a game type and it cannot be added at runtime.");
13                 ka_Initialize();
14         }
15
16         MUTATOR_ONROLLBACK_OR_REMOVE
17         {
18                 // we actually cannot roll back ka_Initialize here
19                 // BUT: we don't need to! If this gets called, adding always
20                 // succeeds.
21         }
22
23         MUTATOR_ONREMOVE
24         {
25                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
26                 return -1;
27         }
28
29         return false;
30 }
31
32
33 entity ka_ball;
34
35 void(entity this) havocbot_role_ka_carrier;
36 void(entity this) havocbot_role_ka_collector;
37
38 void ka_DropEvent(entity plyr);
39 #endif
40
41 #ifdef IMPLEMENTATION
42
43 int autocvar_g_keepaway_ballcarrier_effects;
44 float autocvar_g_keepaway_ballcarrier_damage;
45 float autocvar_g_keepaway_ballcarrier_force;
46 float autocvar_g_keepaway_ballcarrier_highspeed;
47 float autocvar_g_keepaway_ballcarrier_selfdamage;
48 float autocvar_g_keepaway_ballcarrier_selfforce;
49 float autocvar_g_keepaway_noncarrier_damage;
50 float autocvar_g_keepaway_noncarrier_force;
51 float autocvar_g_keepaway_noncarrier_selfdamage;
52 float autocvar_g_keepaway_noncarrier_selfforce;
53 bool autocvar_g_keepaway_noncarrier_warn;
54 int autocvar_g_keepaway_score_bckill;
55 int autocvar_g_keepaway_score_killac;
56 int autocvar_g_keepaway_score_timepoints;
57 float autocvar_g_keepaway_score_timeinterval;
58 float autocvar_g_keepawayball_damageforcescale;
59 int autocvar_g_keepawayball_effects;
60 float autocvar_g_keepawayball_respawntime;
61 int autocvar_g_keepawayball_trail_color;
62
63 bool ka_ballcarrier_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
64 {
65         if(view.ballcarried)
66                 if(IS_SPEC(player))
67                         return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
68
69         // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
70
71         return true;
72 }
73
74 void ka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
75 {
76         if(autocvar_sv_eventlog)
77                 GameLogEcho(strcat(":ka:", mode, ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
78 }
79
80 void ka_TouchEvent(entity this, entity toucher);
81 void ka_RespawnBall(entity this);
82 void ka_RespawnBall(entity this) // runs whenever the ball needs to be relocated
83 {
84         if(gameover) { return; }
85         vector oldballorigin = this.origin;
86
87         if(!MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
88         {
89                 entity spot = SelectSpawnPoint(this, true);
90                 setorigin(this, spot.origin);
91                 this.angles = spot.angles;
92         }
93
94         makevectors(this.angles);
95         set_movetype(this, MOVETYPE_BOUNCE);
96         this.velocity = '0 0 200';
97         this.angles = '0 0 0';
98         this.effects = autocvar_g_keepawayball_effects;
99         settouch(this, ka_TouchEvent);
100         setthink(this, ka_RespawnBall);
101         this.nextthink = time + autocvar_g_keepawayball_respawntime;
102
103         Send_Effect(EFFECT_ELECTRO_COMBO, oldballorigin, '0 0 0', 1);
104         Send_Effect(EFFECT_ELECTRO_COMBO, this.origin, '0 0 0', 1);
105
106         WaypointSprite_Spawn(WP_KaBall, 0, 0, this, '0 0 64', NULL, this.team, this, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
107         WaypointSprite_Ping(this.waypointsprite_attachedforcarrier);
108
109         sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
110 }
111
112 void ka_TimeScoring(entity this)
113 {
114         if(this.owner.ballcarried)
115         { // add points for holding the ball after a certain amount of time
116                 if(autocvar_g_keepaway_score_timepoints)
117                         PlayerScore_Add(this.owner, SP_SCORE, autocvar_g_keepaway_score_timepoints);
118
119                 PlayerScore_Add(this.owner, SP_KEEPAWAY_BCTIME, (autocvar_g_keepaway_score_timeinterval / 1)); // interval is divided by 1 so that time always shows "seconds"
120                 this.nextthink = time + autocvar_g_keepaway_score_timeinterval;
121         }
122 }
123
124 void ka_TouchEvent(entity this, entity toucher) // runs any time that the ball comes in contact with something
125 {
126         if(gameover) { return; }
127         if(!this) { return; }
128         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
129         { // The ball fell off the map, respawn it since players can't get to it
130                 ka_RespawnBall(this);
131                 return;
132         }
133         if(IS_DEAD(toucher)) { return; }
134         if(STAT(FROZEN, toucher)) { return; }
135         if (!IS_PLAYER(toucher))
136         {  // The ball just touched an object, most likely the world
137                 Send_Effect(EFFECT_BALL_SPARKS, this.origin, '0 0 0', 1);
138                 sound(this, CH_TRIGGER, SND_KA_TOUCH, VOL_BASE, ATTEN_NORM);
139                 return;
140         }
141         else if(this.wait > time) { return; }
142
143         // attach the ball to the player
144         this.owner = toucher;
145         toucher.ballcarried = this;
146         setattachment(this, toucher, "");
147         setorigin(this, '0 0 0');
148
149         // make the ball invisible/unable to do anything/set up time scoring
150         this.velocity = '0 0 0';
151         set_movetype(this, MOVETYPE_NONE);
152         this.effects |= EF_NODRAW;
153         settouch(this, func_null);
154         setthink(this, ka_TimeScoring);
155         this.nextthink = time + autocvar_g_keepaway_score_timeinterval;
156         this.takedamage = DAMAGE_NO;
157
158         // apply effects to player
159         toucher.glow_color = autocvar_g_keepawayball_trail_color;
160         toucher.glow_trail = true;
161         toucher.effects |= autocvar_g_keepaway_ballcarrier_effects;
162
163         // messages and sounds
164         ka_EventLog("pickup", toucher);
165         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_PICKUP, toucher.netname);
166         Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_CENTER, CENTER_KEEPAWAY_PICKUP, toucher.netname);
167         Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_KEEPAWAY_PICKUP_SELF);
168         sound(this.owner, CH_TRIGGER, SND_KA_PICKEDUP, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
169
170         // scoring
171         PlayerScore_Add(toucher, SP_KEEPAWAY_PICKUPS, 1);
172
173         // waypoints
174         WaypointSprite_AttachCarrier(WP_KaBallCarrier, toucher, RADARICON_FLAGCARRIER);
175         toucher.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
176         WaypointSprite_UpdateRule(toucher.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
177         WaypointSprite_Ping(toucher.waypointsprite_attachedforcarrier);
178         WaypointSprite_Kill(this.waypointsprite_attachedforcarrier);
179 }
180
181 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
182 {
183         entity ball;
184         ball = plyr.ballcarried;
185
186         if(!ball) { return; }
187
188         // reset the ball
189         setattachment(ball, NULL, "");
190         set_movetype(ball, MOVETYPE_BOUNCE);
191         ball.wait = time + 1;
192         settouch(ball, ka_TouchEvent);
193         setthink(ball, ka_RespawnBall);
194         ball.nextthink = time + autocvar_g_keepawayball_respawntime;
195         ball.takedamage = DAMAGE_YES;
196         ball.effects &= ~EF_NODRAW;
197         setorigin(ball, plyr.origin + '0 0 10');
198         ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
199         ball.owner.ballcarried = world; // I hope nothing checks to see if the world has the ball in the rest of my code :P
200         ball.owner = NULL;
201
202         // reset the player effects
203         plyr.glow_trail = false;
204         plyr.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
205
206         // messages and sounds
207         ka_EventLog("dropped", plyr);
208         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_DROPPED, plyr.netname);
209         Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEEPAWAY_DROPPED, plyr.netname);
210         sound(NULL, CH_TRIGGER, SND_KA_DROPPED, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
211
212         // scoring
213         // PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1); Not anymore, this is 100% the same as pickups and is useless.
214
215         // waypoints
216         WaypointSprite_Spawn(WP_KaBall, 0, 0, ball, '0 0 64', NULL, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
217         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
218         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
219         WaypointSprite_Kill(plyr.waypointsprite_attachedforcarrier);
220 }
221
222 /** used to clear the ballcarrier whenever the match switches from warmup to normal */
223 void ka_Reset(entity this)
224 {
225         if((this.owner) && (IS_PLAYER(this.owner)))
226                 ka_DropEvent(this.owner);
227
228         if(time < game_starttime)
229         {
230                 setthink(this, ka_RespawnBall);
231                 settouch(this, func_null);
232                 this.nextthink = game_starttime;
233         }
234         else
235                 ka_RespawnBall(this);
236 }
237
238
239 // ================
240 // Bot player logic
241 // ================
242
243 void havocbot_goalrating_ball(entity this, float ratingscale, vector org)
244 {
245         float t;
246         entity ball_owner;
247         ball_owner = ka_ball.owner;
248
249         if (ball_owner == this)
250                 return;
251
252         // If ball is carried by player then hunt them down.
253         if (ball_owner)
254         {
255                 t = (this.health + this.armorvalue) / (ball_owner.health + ball_owner.armorvalue);
256                 navigation_routerating(this, ball_owner, t * ratingscale, 2000);
257         }
258         else // Ball has been dropped so collect.
259                 navigation_routerating(this, ka_ball, ratingscale, 2000);
260 }
261
262 void havocbot_role_ka_carrier(entity this)
263 {
264         if (IS_DEAD(this))
265                 return;
266
267         if (time > this.bot_strategytime)
268         {
269                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
270
271                 navigation_goalrating_start(this);
272                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
273                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
274                 //havocbot_goalrating_waypoints(1, this.origin, 1000);
275                 navigation_goalrating_end(this);
276         }
277
278         if (!this.ballcarried)
279         {
280                 this.havocbot_role = havocbot_role_ka_collector;
281                 this.bot_strategytime = 0;
282         }
283 }
284
285 void havocbot_role_ka_collector(entity this)
286 {
287         if (IS_DEAD(this))
288                 return;
289
290         if (time > this.bot_strategytime)
291         {
292                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
293
294                 navigation_goalrating_start(this);
295                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
296                 havocbot_goalrating_enemyplayers(this, 1000, this.origin, 10000);
297                 havocbot_goalrating_ball(this, 20000, this.origin);
298                 navigation_goalrating_end(this);
299         }
300
301         if (this.ballcarried)
302         {
303                 this.havocbot_role = havocbot_role_ka_carrier;
304                 this.bot_strategytime = 0;
305         }
306 }
307
308
309 // ==============
310 // Hook Functions
311 // ==============
312
313 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
314 {
315         entity frag_attacker = M_ARGV(1, entity);
316         entity frag_target = M_ARGV(2, entity);
317
318         if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
319         {
320                 if(frag_target.ballcarried) { // add to amount of times killing carrier
321                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
322                         if(autocvar_g_keepaway_score_bckill) // add bckills to the score
323                                 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_bckill);
324                 }
325                 else if(!frag_attacker.ballcarried)
326                         if(autocvar_g_keepaway_noncarrier_warn)
327                                 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
328
329                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
330                         PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
331         }
332
333         if(frag_target.ballcarried) { ka_DropEvent(frag_target); } // a player with the ball has died, drop it
334 }
335
336 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
337 {
338         M_ARGV(2, float) = 0; // no frags counted in keepaway
339         return true; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
340 }
341
342 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
343 {
344         entity player = M_ARGV(0, entity);
345
346         // clear the item used for the ball in keepaway
347         player.items &= ~IT_KEY1;
348
349         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
350         if(player.ballcarried)
351                 player.items |= IT_KEY1;
352 }
353
354 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
355 {
356         entity player = M_ARGV(0, entity);
357
358         if(MUTATOR_RETURNVALUE == 0)
359         if(player.ballcarried)
360         {
361                 ka_DropEvent(player);
362                 return true;
363         }
364 }
365
366 MUTATOR_HOOKFUNCTION(ka, PlayerDamage_Calculate) // for changing damage and force values that are applied to players in g_damage.qc
367 {
368         entity frag_attacker = M_ARGV(1, entity);
369         entity frag_target = M_ARGV(2, entity);
370         float frag_damage = M_ARGV(4, float);
371         vector frag_force = M_ARGV(6, vector);
372
373         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
374         {
375                 if(frag_target == frag_attacker) // damage done to yourself
376                 {
377                         frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
378                         frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
379                 }
380                 else // damage done to noncarriers
381                 {
382                         frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
383                         frag_force *= autocvar_g_keepaway_ballcarrier_force;
384                 }
385         }
386         else if (!frag_target.ballcarried) // if the target is a noncarrier
387         {
388                 if(frag_target == frag_attacker) // damage done to yourself
389                 {
390                         frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
391                         frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
392                 }
393                 else // damage done to other noncarriers
394                 {
395                         frag_damage *= autocvar_g_keepaway_noncarrier_damage;
396                         frag_force *= autocvar_g_keepaway_noncarrier_force;
397                 }
398         }
399
400         M_ARGV(4, float) = frag_damage;
401         M_ARGV(6, vector) = frag_force;
402 }
403
404 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
405 {
406         entity player = M_ARGV(0, entity);
407
408         if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
409 }
410
411 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
412 {
413         entity player = M_ARGV(0, entity);
414
415         if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
416 }
417
418 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
419 {
420         entity player = M_ARGV(0, entity);
421
422         // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
423         // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
424
425         player.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
426
427         if(player.ballcarried)
428                 player.effects |= autocvar_g_keepaway_ballcarrier_effects;
429 }
430
431 .float stat_sv_airspeedlimit_nonqw;
432 .float stat_sv_maxspeed;
433
434 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics)
435 {
436         entity player = M_ARGV(0, entity);
437
438         if(player.ballcarried)
439         {
440                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_keepaway_ballcarrier_highspeed;
441                 player.stat_sv_maxspeed *= autocvar_g_keepaway_ballcarrier_highspeed;
442         }
443 }
444
445 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
446 {
447         entity bot = M_ARGV(0, entity);
448         entity targ = M_ARGV(1, entity);
449
450         // if neither player has ball then don't attack unless the ball is on the ground
451         if(!targ.ballcarried && !bot.ballcarried && ka_ball.owner)
452                 return true;
453 }
454
455 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
456 {
457         entity bot = M_ARGV(0, entity);
458
459         if (bot.ballcarried)
460                 bot.havocbot_role = havocbot_role_ka_carrier;
461         else
462                 bot.havocbot_role = havocbot_role_ka_collector;
463         return true;
464 }
465
466 MUTATOR_HOOKFUNCTION(ka, DropSpecialItems)
467 {
468         entity frag_target = M_ARGV(0, entity);
469         
470         if(frag_target.ballcarried)
471                 ka_DropEvent(frag_target);
472 }
473
474
475 // ==============
476 // Initialization
477 // ==============
478
479 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
480 {
481         entity e = new(keepawayball);
482         e.model = "models/orbs/orbblue.md3";
483         precache_model(e.model);
484         _setmodel(e, e.model);
485         setsize(e, '-16 -16 -20', '16 16 20'); // 20 20 20 was too big, player is only 16 16 24... gotta cheat with the Z (20) axis so that the particle isn't cut off
486         e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
487         e.takedamage = DAMAGE_YES;
488         e.solid = SOLID_TRIGGER;
489         set_movetype(e, MOVETYPE_BOUNCE);
490         e.glow_color = autocvar_g_keepawayball_trail_color;
491         e.glow_trail = true;
492         e.flags = FL_ITEM;
493         e.pushable = true;
494         e.reset = ka_Reset;
495         settouch(e, ka_TouchEvent);
496         e.owner = NULL;
497         ka_ball = e;
498
499         InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
500 }
501
502 void ka_ScoreRules()
503 {
504         ScoreRules_basics(0, SFL_SORT_PRIO_PRIMARY, 0, true); // SFL_SORT_PRIO_PRIMARY
505         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_PICKUPS,             "pickups",              0);
506         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_CARRIERKILLS,   "bckills",           0);
507         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_BCTIME,                  "bctime",           SFL_SORT_PRIO_SECONDARY);
508         ScoreRules_basics_end();
509 }
510
511 void ka_Initialize() // run at the start of a match, initiates game mode
512 {
513         ka_ScoreRules();
514         ka_SpawnBall();
515 }
516
517 #endif