]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/keepaway/sv_keepaway.qc
Transifex autosync
[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         nudgeoutofsolid(ball); // a ball has a horizontally bigger bbox than a player
202         ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
203         ball.owner = NULL;
204         navigation_dynamicgoal_set(ball, player);
205
206         // messages and sounds
207         ka_EventLog("dropped", player);
208         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_DROPPED, player.netname);
209         Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEEPAWAY_DROPPED, player.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         // waypoints
213         WaypointSprite_Spawn(WP_KaBall, 0, 0, ball, '0 0 64', NULL, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
214         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
215         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
216
217         ka_PlayerReset(player);
218 }
219
220 .bool pushable;
221
222 MODEL(KA_BALL, "models/orbs/orbblue.md3");
223
224 void ka_RemoveBalls()
225 {
226         IL_EACH(g_kaballs, true,
227         {
228                 if (it.owner) // it was attached
229                         ka_PlayerReset(it.owner);
230                 else
231                         WaypointSprite_DetachCarrier(it);
232                 delete(it);
233         });
234 }
235
236 void ka_SpawnBalls()
237 {
238         int i = 0;
239         do // never allow less than 1 ball to spawn
240         {
241                 entity e = new(keepawayball);
242                 setmodel(e, MDL_KA_BALL);
243                 e.solid = SOLID_TRIGGER; // before setsize to ensure area grid linking
244                 // 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
245                 // bones_was_here: that was WITH sv_legacy_bbox_expand 1 and FL_ITEM (mins -= '15 15 1'; maxs += '15 15 1')
246                 // it's round so should have a symmetrical bbox, same height as pickup items so it can't be jumped over in any physics
247                 setsize(e, '-24 -24 -24', '24 24 24');
248                 e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
249                 e.takedamage = DAMAGE_YES;
250                 e.event_damage = ka_DamageEvent;
251                 e.damagedbycontents = true;
252                 IL_PUSH(g_damagedbycontents, e);
253                 set_movetype(e, MOVETYPE_BOUNCE);
254                 e.glow_color = autocvar_g_keepawayball_trail_color;
255                 e.glow_trail = true;
256                 e.flags = FL_ITEM;
257                 IL_PUSH(g_items, e);
258                 e.pushable = true;
259                 settouch(e, ka_TouchEvent);
260                 e.owner = NULL;
261                 IL_PUSH(g_kaballs, e);
262                 navigation_dynamicgoal_init(e, false);
263
264                 ka_RespawnBall(e);
265
266                 ++i;
267         }
268         while (i < KA_BALL_COUNT);
269 }
270
271 void ka_Handler_CheckBall(entity this)
272 {
273         if(time < game_starttime)
274         {
275                 if (!IL_EMPTY(g_kaballs))
276                         ka_RemoveBalls();
277         }
278         else
279         {
280                 if (IL_EMPTY(g_kaballs))
281                         ka_SpawnBalls(); // ;)
282         }
283
284         this.nextthink = time;
285 }
286
287
288 // ================
289 // Bot player logic
290 // ================
291
292 void havocbot_goalrating_ball(entity this, float ratingscale, vector org)
293 {
294         entity ball = NULL, ball_carried = NULL;
295
296         // stops at last ball, prefers ball without carrier
297         IL_EACH(g_kaballs, it.owner != this,
298         {
299                 if(it.owner)
300                         ball_carried = it.owner;
301                 else
302                         ball = it;
303         });
304
305         if(ball)
306                 navigation_routerating(this, ball, ratingscale, 2000);
307         else if(ball_carried)
308                 navigation_routerating(this, ball_carried, ratingscale, 2000);
309 }
310
311 void havocbot_role_ka_carrier(entity this)
312 {
313         if (IS_DEAD(this))
314                 return;
315
316         if (navigation_goalrating_timeout(this))
317         {
318                 navigation_goalrating_start(this);
319                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
320                 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
321                 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
322                 navigation_goalrating_end(this);
323
324                 navigation_goalrating_timeout_set(this);
325         }
326
327         if (!this.ballcarried)
328         {
329                 this.havocbot_role = havocbot_role_ka_collector;
330                 navigation_goalrating_timeout_expire(this, 2);
331         }
332 }
333
334 void havocbot_role_ka_collector(entity this)
335 {
336         if (IS_DEAD(this))
337                 return;
338
339         if (navigation_goalrating_timeout(this))
340         {
341                 navigation_goalrating_start(this);
342                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
343                 havocbot_goalrating_enemyplayers(this, 500, this.origin, 10000);
344                 havocbot_goalrating_ball(this, 8000, this.origin);
345                 navigation_goalrating_end(this);
346
347                 navigation_goalrating_timeout_set(this);
348         }
349
350         if (this.ballcarried)
351         {
352                 this.havocbot_role = havocbot_role_ka_carrier;
353                 navigation_goalrating_timeout_expire(this, 2);
354         }
355 }
356
357
358 // ==============
359 // Hook Functions
360 // ==============
361
362 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
363 {
364         entity frag_attacker = M_ARGV(1, entity);
365         entity frag_target = M_ARGV(2, entity);
366
367         if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
368         {
369                 if(frag_target.ballcarried) { // add to amount of times killing carrier
370                         GameRules_scoring_add(frag_attacker, KEEPAWAY_CARRIERKILLS, 1);
371                         if(autocvar_g_keepaway_score_bckill) // add bckills to the score
372                                 GameRules_scoring_add(frag_attacker, SCORE, autocvar_g_keepaway_score_bckill);
373                 }
374                 else if(!frag_attacker.ballcarried)
375                         if(autocvar_g_keepaway_noncarrier_warn)
376                                 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
377
378                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
379                         GameRules_scoring_add(frag_attacker, SCORE, autocvar_g_keepaway_score_killac);
380         }
381
382         if(frag_target.ballcarried) { ka_DropEvent(frag_target); } // a player with the ball has died, drop it
383 }
384
385 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
386 {
387         M_ARGV(2, float) = 0; // no frags counted in keepaway
388         return true; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
389 }
390
391 MUTATOR_HOOKFUNCTION(ka, Scores_CountFragsRemaining)
392 {
393         // announce remaining frags, but only when timed scoring is off
394         return !autocvar_g_keepaway_score_timepoints;
395 }
396
397 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
398 {
399         entity player = M_ARGV(0, entity);
400
401         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
402         STAT(OBJECTIVE_STATUS, player) = BITSET(STAT(OBJECTIVE_STATUS, player), KA_CARRYING, player.ballcarried != NULL);
403 }
404
405 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
406 {
407         entity player = M_ARGV(0, entity);
408
409         if(MUTATOR_RETURNVALUE == 0)
410         if(player.ballcarried)
411         {
412                 ka_DropEvent(player);
413                 return true;
414         }
415 }
416
417 MUTATOR_HOOKFUNCTION(ka, Damage_Calculate) // for changing damage and force values that are applied to players
418 {
419         entity frag_attacker = M_ARGV(1, entity);
420         entity frag_target = M_ARGV(2, entity);
421
422         // as a gamemode rule, only apply scaling to player versus player combat
423         if(!IS_PLAYER(frag_attacker) || !IS_PLAYER(frag_target))
424                 return;
425
426         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
427         {
428                 if(frag_target == frag_attacker) // damage done to yourself
429                 {
430                         M_ARGV(4, float) *= autocvar_g_keepaway_ballcarrier_selfdamage;
431                         M_ARGV(6, vector) *= autocvar_g_keepaway_ballcarrier_selfforce;
432                 }
433                 else // damage done to other ballcarriers
434                 {
435                         M_ARGV(4, float) *= autocvar_g_keepaway_ballcarrier_damage;
436                         M_ARGV(6, vector) *= autocvar_g_keepaway_ballcarrier_force;
437                 }
438         }
439         else // if the attacker is a noncarrier
440         {
441                 if(frag_target == frag_attacker) // damage done to yourself
442                 {
443                         M_ARGV(4, float) *= autocvar_g_keepaway_noncarrier_selfdamage;
444                         M_ARGV(6, vector) *= autocvar_g_keepaway_noncarrier_selfforce;
445                 }
446                 else // damage done to other noncarriers
447                 {
448                         M_ARGV(4, float) *= autocvar_g_keepaway_noncarrier_damage;
449                         M_ARGV(6, vector) *= autocvar_g_keepaway_noncarrier_force;
450                 }
451         }
452 }
453
454 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
455 {
456         entity player = M_ARGV(0, entity);
457
458         if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
459 }
460
461 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
462 {
463         entity player = M_ARGV(0, entity);
464
465         if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
466 }
467
468 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
469 {
470         entity player = M_ARGV(0, entity);
471
472         // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
473         // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
474
475         player.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
476
477         if(player.ballcarried)
478                 player.effects |= autocvar_g_keepaway_ballcarrier_effects;
479 }
480
481
482 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics_UpdateStats)
483 {
484         entity player = M_ARGV(0, entity);
485         // these automatically reset, no need to worry
486
487         if(player.ballcarried)
488                 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_keepaway_ballcarrier_highspeed;
489 }
490
491 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
492 {
493         entity bot = M_ARGV(0, entity);
494         entity targ = M_ARGV(1, entity);
495
496         // if neither player has ball then don't attack unless the ball is on the ground
497         bool have_held_ball = false;
498         IL_EACH(g_kaballs, it.owner,
499         {
500                 have_held_ball = true;
501                 break;
502         });
503         if(!targ.ballcarried && !bot.ballcarried && have_held_ball)
504                 return true;
505 }
506
507 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
508 {
509         entity bot = M_ARGV(0, entity);
510
511         if (bot.ballcarried)
512                 bot.havocbot_role = havocbot_role_ka_carrier;
513         else
514                 bot.havocbot_role = havocbot_role_ka_collector;
515         return true;
516 }
517
518 MUTATOR_HOOKFUNCTION(ka, DropSpecialItems)
519 {
520         entity frag_target = M_ARGV(0, entity);
521
522         if(frag_target.ballcarried)
523                 ka_DropEvent(frag_target);
524 }