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