]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/keepaway/sv_keepaway.qc
Merge remote-tracking branch 'origin/master' into pending-release
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / keepaway / sv_keepaway.qc
1 #include "sv_keepaway.qh"
2
3 #include <common/effects/all.qh>
4 #include <server/client.qh>
5 #include <server/gamelog.qh>
6 #include <server/damage.qh>
7 #include <server/items/items.qh>
8 #include <server/world.qh>
9
10 .entity ballcarried;
11
12 int autocvar_g_keepaway_ballcarrier_effects;
13 float autocvar_g_keepaway_ballcarrier_damage;
14 float autocvar_g_keepaway_ballcarrier_force;
15 float autocvar_g_keepaway_ballcarrier_highspeed;
16 float autocvar_g_keepaway_ballcarrier_selfdamage;
17 float autocvar_g_keepaway_ballcarrier_selfforce;
18 float autocvar_g_keepaway_noncarrier_damage;
19 float autocvar_g_keepaway_noncarrier_force;
20 float autocvar_g_keepaway_noncarrier_selfdamage;
21 float autocvar_g_keepaway_noncarrier_selfforce;
22 bool autocvar_g_keepaway_noncarrier_warn;
23 int autocvar_g_keepaway_score_bckill;
24 int autocvar_g_keepaway_score_killac;
25 int autocvar_g_keepaway_score_timepoints;
26 float autocvar_g_keepaway_score_timeinterval;
27 float autocvar_g_keepawayball_damageforcescale;
28 int autocvar_g_keepawayball_effects;
29 float autocvar_g_keepawayball_respawntime;
30 int autocvar_g_keepawayball_trail_color;
31
32 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
33 {
34         if(view.ballcarried)
35                 if(IS_SPEC(player))
36                         return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
37
38         // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
39
40         return true;
41 }
42
43 void ka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
44 {
45         if(autocvar_sv_eventlog)
46                 GameLogEcho(strcat(":ka:", mode, ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
47 }
48
49 void ka_RespawnBall(entity this) // runs whenever the ball needs to be relocated
50 {
51         if(game_stopped) return;
52         vector oldballorigin = this.origin;
53
54         if(!MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
55                 setorigin(this, SelectSpawnPoint(this, true).origin);
56
57         set_movetype(this, MOVETYPE_BOUNCE);
58         this.velocity = '0 0 200';
59         this.angles = '0 0 0';
60         this.effects = autocvar_g_keepawayball_effects;
61         settouch(this, ka_TouchEvent);
62         setthink(this, ka_RespawnBall);
63         this.nextthink = time + autocvar_g_keepawayball_respawntime;
64         navigation_dynamicgoal_set(this, NULL);
65
66         Send_Effect(EFFECT_ELECTRO_COMBO, oldballorigin, '0 0 0', 1);
67         Send_Effect(EFFECT_ELECTRO_COMBO, this.origin, '0 0 0', 1);
68
69         WaypointSprite_Spawn(WP_KaBall, 0, 0, this, '0 0 64', NULL, this.team, this, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
70         WaypointSprite_Ping(this.waypointsprite_attachedforcarrier);
71
72         sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
73 }
74
75 .float timepoints_counter;
76 MUTATOR_HOOKFUNCTION(ka, reset_map_global)
77 {
78         FOREACH_CLIENT(true,
79         {
80                 it.timepoints_counter = 0;
81         });
82         return true;
83 }
84
85 void ka_TimeScoring(entity this)
86 {
87         if(this.owner.ballcarried)
88         { // add points for holding the ball after a certain amount of time
89                 float timescore = 0;
90                 if(autocvar_g_keepaway_score_timepoints)
91                         timescore = autocvar_g_keepaway_score_timepoints / max(0.001, autocvar_g_keepaway_score_timeinterval);
92
93                 if (timescore)
94                         GameRules_scoring_add_float2int(this.owner, SCORE, timescore, timepoints_counter, 1);
95
96                 GameRules_scoring_add(this.owner, KEEPAWAY_BCTIME, 1);
97                 this.nextthink = time + 1;
98         }
99 }
100
101 void ka_DamageEvent(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
102 {
103         if(ITEM_DAMAGE_NEEDKILL(deathtype))
104                 ka_RespawnBall(this);
105 }
106
107 void ka_TouchEvent(entity this, entity toucher) // runs any time that the ball comes in contact with something
108 {
109         if (!this || game_stopped)
110                 return;
111
112         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
113         { // The ball fell off the map, respawn it since players can't get to it
114                 ka_RespawnBall(this);
115                 return;
116         }
117         if(toucher.ballcarried) { return; }
118         if(IS_DEAD(toucher)) { return; }
119         if(STAT(FROZEN, toucher)) { return; }
120         if (!IS_PLAYER(toucher))
121         {  // The ball just touched an object, most likely the world
122                 Send_Effect(EFFECT_BALL_SPARKS, this.origin, '0 0 0', 1);
123                 sound(this, CH_TRIGGER, SND_KA_TOUCH, VOL_BASE, ATTEN_NORM);
124                 return;
125         }
126         else if(this.wait > time) { return; }
127
128         // attach the ball to the player
129         this.owner = toucher;
130         toucher.ballcarried = this;
131         GameRules_scoring_vip(toucher, true);
132         setattachment(this, toucher, "");
133         setorigin(this, '0 0 0');
134
135         // make the ball invisible/unable to do anything/set up time scoring
136         this.velocity = '0 0 0';
137         set_movetype(this, MOVETYPE_NONE);
138         this.effects |= EF_NODRAW;
139         settouch(this, func_null);
140         setthink(this, ka_TimeScoring);
141         this.nextthink = time + 1;
142         this.takedamage = DAMAGE_NO;
143         this.event_damage = func_null;
144         this.damagedbycontents = false;
145         IL_REMOVE(g_damagedbycontents, this);
146         navigation_dynamicgoal_unset(this);
147
148         // apply effects to player
149         toucher.glow_color = autocvar_g_keepawayball_trail_color;
150         toucher.glow_trail = true;
151         toucher.effects |= autocvar_g_keepaway_ballcarrier_effects;
152
153         // messages and sounds
154         ka_EventLog("pickup", toucher);
155         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_PICKUP, toucher.netname);
156         Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_CENTER, CENTER_KEEPAWAY_PICKUP, toucher.netname);
157         Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_KEEPAWAY_PICKUP_SELF);
158         sound(this.owner, CH_TRIGGER, SND_KA_PICKEDUP, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
159
160         // scoring
161         GameRules_scoring_add(toucher, KEEPAWAY_PICKUPS, 1);
162
163         // waypoints
164         WaypointSprite_AttachCarrier(WP_KaBallCarrier, toucher, RADARICON_FLAGCARRIER);
165         toucher.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
166         WaypointSprite_UpdateRule(toucher.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
167         WaypointSprite_Ping(toucher.waypointsprite_attachedforcarrier);
168         WaypointSprite_Kill(this.waypointsprite_attachedforcarrier);
169 }
170
171 void ka_PlayerReset(entity player)
172 {
173         player.ballcarried = NULL;
174         GameRules_scoring_vip(player, false);
175         WaypointSprite_Kill(player.waypointsprite_attachedforcarrier);
176
177         // reset the player effects
178         player.glow_trail = false;
179         player.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
180 }
181
182 void ka_DropEvent(entity player) // runs any time that a player is supposed to lose the ball
183 {
184         entity ball = player.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.event_damage = ka_DamageEvent;
197         ball.damagedbycontents = true;
198         IL_PUSH(g_damagedbycontents, ball);
199         ball.effects &= ~EF_NODRAW;
200         setorigin(ball, player.origin + '0 0 10');
201         ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
202         ball.owner = NULL;
203         navigation_dynamicgoal_set(ball, player);
204
205         // messages and sounds
206         ka_EventLog("dropped", player);
207         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_DROPPED, player.netname);
208         Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEEPAWAY_DROPPED, player.netname);
209         sound(NULL, CH_TRIGGER, SND_KA_DROPPED, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
210
211         // waypoints
212         WaypointSprite_Spawn(WP_KaBall, 0, 0, ball, '0 0 64', NULL, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
213         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
214         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
215
216         ka_PlayerReset(player);
217 }
218
219 .bool pushable;
220
221 MODEL(KA_BALL, "models/orbs/orbblue.md3");
222
223 void ka_RemoveBalls()
224 {
225         IL_EACH(g_kaballs, true,
226         {
227                 if (it.owner) // it was attached
228                         ka_PlayerReset(it.owner);
229                 else
230                         WaypointSprite_DetachCarrier(it);
231                 delete(it);
232         });
233 }
234
235 void ka_SpawnBalls()
236 {
237         int i = 0;
238         do // never allow less than 1 ball to spawn
239         {
240                 entity e = new(keepawayball);
241                 setmodel(e, MDL_KA_BALL);
242                 e.solid = SOLID_TRIGGER; // before setsize to ensure area grid linking
243                 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
244                 e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
245                 e.takedamage = DAMAGE_YES;
246                 e.event_damage = ka_DamageEvent;
247                 e.damagedbycontents = true;
248                 IL_PUSH(g_damagedbycontents, e);
249                 set_movetype(e, MOVETYPE_BOUNCE);
250                 e.glow_color = autocvar_g_keepawayball_trail_color;
251                 e.glow_trail = true;
252                 e.flags = FL_ITEM;
253                 IL_PUSH(g_items, e);
254                 e.pushable = true;
255                 settouch(e, ka_TouchEvent);
256                 e.owner = NULL;
257                 IL_PUSH(g_kaballs, e);
258                 navigation_dynamicgoal_init(e, false);
259
260                 ka_RespawnBall(e);
261
262                 ++i;
263         }
264         while (i < KA_BALL_COUNT);
265 }
266
267 void ka_Handler_CheckBall(entity this)
268 {
269         if(time < game_starttime)
270         {
271                 if (!IL_EMPTY(g_kaballs))
272                         ka_RemoveBalls();
273         }
274         else
275         {
276                 if (IL_EMPTY(g_kaballs))
277                         ka_SpawnBalls(); // ;)
278         }
279
280         this.nextthink = time;
281 }
282
283
284 // ================
285 // Bot player logic
286 // ================
287
288 void havocbot_goalrating_ball(entity this, float ratingscale, vector org)
289 {
290         entity ball = NULL, ball_carried = NULL;
291
292         // stops at last ball, prefers ball without carrier
293         IL_EACH(g_kaballs, it.owner != this,
294         {
295                 if(it.owner)
296                         ball_carried = it.owner;
297                 else
298                         ball = it;
299         });
300
301         if(ball)
302                 navigation_routerating(this, ball, ratingscale, 2000);
303         else if(ball_carried)
304                 navigation_routerating(this, ball_carried, ratingscale, 2000);
305 }
306
307 void havocbot_role_ka_carrier(entity this)
308 {
309         if (IS_DEAD(this))
310                 return;
311
312         if (navigation_goalrating_timeout(this))
313         {
314                 navigation_goalrating_start(this);
315                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
316                 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
317                 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
318                 navigation_goalrating_end(this);
319
320                 navigation_goalrating_timeout_set(this);
321         }
322
323         if (!this.ballcarried)
324         {
325                 this.havocbot_role = havocbot_role_ka_collector;
326                 navigation_goalrating_timeout_expire(this, 2);
327         }
328 }
329
330 void havocbot_role_ka_collector(entity this)
331 {
332         if (IS_DEAD(this))
333                 return;
334
335         if (navigation_goalrating_timeout(this))
336         {
337                 navigation_goalrating_start(this);
338                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
339                 havocbot_goalrating_enemyplayers(this, 500, this.origin, 10000);
340                 havocbot_goalrating_ball(this, 8000, this.origin);
341                 navigation_goalrating_end(this);
342
343                 navigation_goalrating_timeout_set(this);
344         }
345
346         if (this.ballcarried)
347         {
348                 this.havocbot_role = havocbot_role_ka_carrier;
349                 navigation_goalrating_timeout_expire(this, 2);
350         }
351 }
352
353
354 // ==============
355 // Hook Functions
356 // ==============
357
358 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
359 {
360         entity frag_attacker = M_ARGV(1, entity);
361         entity frag_target = M_ARGV(2, entity);
362
363         if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
364         {
365                 if(frag_target.ballcarried) { // add to amount of times killing carrier
366                         GameRules_scoring_add(frag_attacker, KEEPAWAY_CARRIERKILLS, 1);
367                         if(autocvar_g_keepaway_score_bckill) // add bckills to the score
368                                 GameRules_scoring_add(frag_attacker, SCORE, autocvar_g_keepaway_score_bckill);
369                 }
370                 else if(!frag_attacker.ballcarried)
371                         if(autocvar_g_keepaway_noncarrier_warn)
372                                 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
373
374                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
375                         GameRules_scoring_add(frag_attacker, SCORE, autocvar_g_keepaway_score_killac);
376         }
377
378         if(frag_target.ballcarried) { ka_DropEvent(frag_target); } // a player with the ball has died, drop it
379 }
380
381 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
382 {
383         M_ARGV(2, float) = 0; // no frags counted in keepaway
384         return true; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
385 }
386
387 MUTATOR_HOOKFUNCTION(ka, Scores_CountFragsRemaining)
388 {
389         // announce remaining frags, but only when timed scoring is off
390         return !autocvar_g_keepaway_score_timepoints;
391 }
392
393 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
394 {
395         entity player = M_ARGV(0, entity);
396
397         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
398         STAT(OBJECTIVE_STATUS, player) = BITSET(STAT(OBJECTIVE_STATUS, player), KA_CARRYING, player.ballcarried != NULL);
399 }
400
401 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
402 {
403         entity player = M_ARGV(0, entity);
404
405         if(MUTATOR_RETURNVALUE == 0)
406         if(player.ballcarried)
407         {
408                 ka_DropEvent(player);
409                 return true;
410         }
411 }
412
413 MUTATOR_HOOKFUNCTION(ka, Damage_Calculate) // for changing damage and force values that are applied to players
414 {
415         entity frag_attacker = M_ARGV(1, entity);
416         entity frag_target = M_ARGV(2, entity);
417
418         // as a gamemode rule, only apply scaling to player versus player combat
419         if(!IS_PLAYER(frag_attacker) || !IS_PLAYER(frag_target))
420                 return;
421
422         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
423         {
424                 if(frag_target == frag_attacker) // damage done to yourself
425                 {
426                         M_ARGV(4, float) *= autocvar_g_keepaway_ballcarrier_selfdamage;
427                         M_ARGV(6, vector) *= autocvar_g_keepaway_ballcarrier_selfforce;
428                 }
429                 else // damage done to other ballcarriers
430                 {
431                         M_ARGV(4, float) *= autocvar_g_keepaway_ballcarrier_damage;
432                         M_ARGV(6, vector) *= autocvar_g_keepaway_ballcarrier_force;
433                 }
434         }
435         else // if the attacker is a noncarrier
436         {
437                 if(frag_target == frag_attacker) // damage done to yourself
438                 {
439                         M_ARGV(4, float) *= autocvar_g_keepaway_noncarrier_selfdamage;
440                         M_ARGV(6, vector) *= autocvar_g_keepaway_noncarrier_selfforce;
441                 }
442                 else // damage done to other noncarriers
443                 {
444                         M_ARGV(4, float) *= autocvar_g_keepaway_noncarrier_damage;
445                         M_ARGV(6, vector) *= autocvar_g_keepaway_noncarrier_force;
446                 }
447         }
448 }
449
450 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
451 {
452         entity player = M_ARGV(0, entity);
453
454         if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
455 }
456
457 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
458 {
459         entity player = M_ARGV(0, entity);
460
461         if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
462 }
463
464 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
465 {
466         entity player = M_ARGV(0, entity);
467
468         // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
469         // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
470
471         player.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
472
473         if(player.ballcarried)
474                 player.effects |= autocvar_g_keepaway_ballcarrier_effects;
475 }
476
477
478 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics_UpdateStats)
479 {
480         entity player = M_ARGV(0, entity);
481         // these automatically reset, no need to worry
482
483         if(player.ballcarried)
484                 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_keepaway_ballcarrier_highspeed;
485 }
486
487 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
488 {
489         entity bot = M_ARGV(0, entity);
490         entity targ = M_ARGV(1, entity);
491
492         // if neither player has ball then don't attack unless the ball is on the ground
493         bool have_held_ball = false;
494         IL_EACH(g_kaballs, it.owner,
495         {
496                 have_held_ball = true;
497                 break;
498         });
499         if(!targ.ballcarried && !bot.ballcarried && have_held_ball)
500                 return true;
501 }
502
503 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
504 {
505         entity bot = M_ARGV(0, entity);
506
507         if (bot.ballcarried)
508                 bot.havocbot_role = havocbot_role_ka_carrier;
509         else
510                 bot.havocbot_role = havocbot_role_ka_collector;
511         return true;
512 }
513
514 MUTATOR_HOOKFUNCTION(ka, DropSpecialItems)
515 {
516         entity frag_target = M_ARGV(0, entity);
517
518         if(frag_target.ballcarried)
519                 ka_DropEvent(frag_target);
520 }