1 #include "sv_keepaway.qh"
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>
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;
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
36 return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
38 // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
43 void ka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
45 if(autocvar_sv_eventlog)
46 GameLogEcho(strcat(":ka:", mode, ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
49 void ka_RespawnBall(entity this) // runs whenever the ball needs to be relocated
51 if(game_stopped) return;
52 vector oldballorigin = this.origin;
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);
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);
66 Send_Effect(EFFECT_ELECTRO_COMBO, oldballorigin, '0 0 0', 1);
67 Send_Effect(EFFECT_ELECTRO_COMBO, this.origin, '0 0 0', 1);
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);
72 sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
75 .float timepoints_counter;
76 MUTATOR_HOOKFUNCTION(ka, reset_map_global)
80 it.timepoints_counter = 0;
85 void ka_TimeScoring(entity this)
87 if(this.owner.ballcarried)
88 { // add points for holding the ball after a certain amount of time
90 if(autocvar_g_keepaway_score_timepoints)
91 timescore = autocvar_g_keepaway_score_timepoints / max(0.001, autocvar_g_keepaway_score_timeinterval);
94 GameRules_scoring_add_float2int(this.owner, SCORE, timescore, timepoints_counter, 1);
96 GameRules_scoring_add(this.owner, KEEPAWAY_BCTIME, 1);
97 this.nextthink = time + 1;
101 void ka_DamageEvent(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
103 if(ITEM_DAMAGE_NEEDKILL(deathtype))
104 ka_RespawnBall(this);
107 void ka_TouchEvent(entity this, entity toucher) // runs any time that the ball comes in contact with something
109 if (!this || game_stopped)
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);
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);
126 else if(this.wait > time) { return; }
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');
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);
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;
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)
161 GameRules_scoring_add(toucher, KEEPAWAY_PICKUPS, 1);
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);
171 void ka_PlayerReset(entity player)
173 player.ballcarried = NULL;
174 GameRules_scoring_vip(player, false);
175 WaypointSprite_Kill(player.waypointsprite_attachedforcarrier);
177 // reset the player effects
178 player.glow_trail = false;
179 player.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
182 void ka_DropEvent(entity player) // runs any time that a player is supposed to lose the ball
184 entity ball = player.ballcarried;
186 if(!ball) { return; }
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();
203 navigation_dynamicgoal_set(ball, player);
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)
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);
216 ka_PlayerReset(player);
221 MODEL(KA_BALL, "models/orbs/orbblue.md3");
223 void ka_RemoveBalls()
225 IL_EACH(g_kaballs, true,
227 if (it.owner) // it was attached
228 ka_PlayerReset(it.owner);
230 WaypointSprite_DetachCarrier(it);
238 do // never allow less than 1 ball to spawn
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;
255 settouch(e, ka_TouchEvent);
257 IL_PUSH(g_kaballs, e);
258 navigation_dynamicgoal_init(e, false);
264 while (i < KA_BALL_COUNT);
267 void ka_Handler_CheckBall(entity this)
269 if(time < game_starttime)
271 if (!IL_EMPTY(g_kaballs))
276 if (IL_EMPTY(g_kaballs))
277 ka_SpawnBalls(); // ;)
280 this.nextthink = time;
288 void havocbot_goalrating_ball(entity this, float ratingscale, vector org)
290 entity ball = NULL, ball_carried = NULL;
292 // stops at last ball, prefers ball without carrier
293 IL_EACH(g_kaballs, it.owner != this,
296 ball_carried = it.owner;
302 navigation_routerating(this, ball, ratingscale, 2000);
303 else if(ball_carried)
304 navigation_routerating(this, ball_carried, ratingscale, 2000);
307 void havocbot_role_ka_carrier(entity this)
312 if (navigation_goalrating_timeout(this))
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);
320 navigation_goalrating_timeout_set(this);
323 if (!this.ballcarried)
325 this.havocbot_role = havocbot_role_ka_collector;
326 navigation_goalrating_timeout_expire(this, 2);
330 void havocbot_role_ka_collector(entity this)
335 if (navigation_goalrating_timeout(this))
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);
343 navigation_goalrating_timeout_set(this);
346 if (this.ballcarried)
348 this.havocbot_role = havocbot_role_ka_carrier;
349 navigation_goalrating_timeout_expire(this, 2);
358 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
360 entity frag_attacker = M_ARGV(1, entity);
361 entity frag_target = M_ARGV(2, entity);
363 if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
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);
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);
374 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
375 GameRules_scoring_add(frag_attacker, SCORE, autocvar_g_keepaway_score_killac);
378 if(frag_target.ballcarried) { ka_DropEvent(frag_target); } // a player with the ball has died, drop it
381 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
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.
387 MUTATOR_HOOKFUNCTION(ka, Scores_CountFragsRemaining)
389 // announce remaining frags, but only when timed scoring is off
390 return !autocvar_g_keepaway_score_timepoints;
393 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
395 entity player = M_ARGV(0, entity);
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);
401 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
403 entity player = M_ARGV(0, entity);
405 if(MUTATOR_RETURNVALUE == 0)
406 if(player.ballcarried)
408 ka_DropEvent(player);
413 MUTATOR_HOOKFUNCTION(ka, Damage_Calculate) // for changing damage and force values that are applied to players
415 entity frag_attacker = M_ARGV(1, entity);
416 entity frag_target = M_ARGV(2, entity);
418 // as a gamemode rule, only apply scaling to player versus player combat
419 if(!IS_PLAYER(frag_attacker) || !IS_PLAYER(frag_target))
422 if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
424 if(frag_target == frag_attacker) // damage done to yourself
426 M_ARGV(4, float) *= autocvar_g_keepaway_ballcarrier_selfdamage;
427 M_ARGV(6, vector) *= autocvar_g_keepaway_ballcarrier_selfforce;
429 else // damage done to other ballcarriers
431 M_ARGV(4, float) *= autocvar_g_keepaway_ballcarrier_damage;
432 M_ARGV(6, vector) *= autocvar_g_keepaway_ballcarrier_force;
435 else // if the attacker is a noncarrier
437 if(frag_target == frag_attacker) // damage done to yourself
439 M_ARGV(4, float) *= autocvar_g_keepaway_noncarrier_selfdamage;
440 M_ARGV(6, vector) *= autocvar_g_keepaway_noncarrier_selfforce;
442 else // damage done to other noncarriers
444 M_ARGV(4, float) *= autocvar_g_keepaway_noncarrier_damage;
445 M_ARGV(6, vector) *= autocvar_g_keepaway_noncarrier_force;
450 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
452 entity player = M_ARGV(0, entity);
454 if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
457 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
459 entity player = M_ARGV(0, entity);
461 if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
464 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
466 entity player = M_ARGV(0, entity);
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()
471 player.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
473 if(player.ballcarried)
474 player.effects |= autocvar_g_keepaway_ballcarrier_effects;
478 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics_UpdateStats)
480 entity player = M_ARGV(0, entity);
481 // these automatically reset, no need to worry
483 if(player.ballcarried)
484 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_keepaway_ballcarrier_highspeed;
487 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
489 entity bot = M_ARGV(0, entity);
490 entity targ = M_ARGV(1, entity);
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,
496 have_held_ball = true;
499 if(!targ.ballcarried && !bot.ballcarried && have_held_ball)
503 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
505 entity bot = M_ARGV(0, entity);
508 bot.havocbot_role = havocbot_role_ka_carrier;
510 bot.havocbot_role = havocbot_role_ka_collector;
514 MUTATOR_HOOKFUNCTION(ka, DropSpecialItems)
516 entity frag_target = M_ARGV(0, entity);
518 if(frag_target.ballcarried)
519 ka_DropEvent(frag_target);