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 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();
204 navigation_dynamicgoal_set(ball, player);
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)
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);
217 ka_PlayerReset(player);
222 MODEL(KA_BALL, "models/orbs/orbblue.md3");
224 void ka_RemoveBalls()
226 IL_EACH(g_kaballs, true,
228 if (it.owner) // it was attached
229 ka_PlayerReset(it.owner);
231 WaypointSprite_DetachCarrier(it);
239 do // never allow less than 1 ball to spawn
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;
259 settouch(e, ka_TouchEvent);
261 IL_PUSH(g_kaballs, e);
262 navigation_dynamicgoal_init(e, false);
268 while (i < KA_BALL_COUNT);
271 void ka_Handler_CheckBall(entity this)
273 if(time < game_starttime)
275 if (!IL_EMPTY(g_kaballs))
280 if (IL_EMPTY(g_kaballs))
281 ka_SpawnBalls(); // ;)
284 this.nextthink = time;
292 void havocbot_goalrating_ball(entity this, float ratingscale, vector org)
294 entity ball = NULL, ball_carried = NULL;
296 // stops at last ball, prefers ball without carrier
297 IL_EACH(g_kaballs, it.owner != this,
300 ball_carried = it.owner;
306 navigation_routerating(this, ball, ratingscale, 2000);
307 else if(ball_carried)
308 navigation_routerating(this, ball_carried, ratingscale, 2000);
311 void havocbot_role_ka_carrier(entity this)
316 if (navigation_goalrating_timeout(this))
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);
324 navigation_goalrating_timeout_set(this);
327 if (!this.ballcarried)
329 this.havocbot_role = havocbot_role_ka_collector;
330 navigation_goalrating_timeout_expire(this, 2);
334 void havocbot_role_ka_collector(entity this)
339 if (navigation_goalrating_timeout(this))
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);
347 navigation_goalrating_timeout_set(this);
350 if (this.ballcarried)
352 this.havocbot_role = havocbot_role_ka_carrier;
353 navigation_goalrating_timeout_expire(this, 2);
362 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
364 entity frag_attacker = M_ARGV(1, entity);
365 entity frag_target = M_ARGV(2, entity);
367 if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
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);
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);
378 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
379 GameRules_scoring_add(frag_attacker, SCORE, autocvar_g_keepaway_score_killac);
382 if(frag_target.ballcarried) { ka_DropEvent(frag_target); } // a player with the ball has died, drop it
385 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
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.
391 MUTATOR_HOOKFUNCTION(ka, Scores_CountFragsRemaining)
393 // announce remaining frags, but only when timed scoring is off
394 return !autocvar_g_keepaway_score_timepoints;
397 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
399 entity player = M_ARGV(0, entity);
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);
405 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
407 entity player = M_ARGV(0, entity);
409 if(MUTATOR_RETURNVALUE == 0)
410 if(player.ballcarried)
412 ka_DropEvent(player);
417 MUTATOR_HOOKFUNCTION(ka, Damage_Calculate) // for changing damage and force values that are applied to players
419 entity frag_attacker = M_ARGV(1, entity);
420 entity frag_target = M_ARGV(2, entity);
422 // as a gamemode rule, only apply scaling to player versus player combat
423 if(!IS_PLAYER(frag_attacker) || !IS_PLAYER(frag_target))
426 if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
428 if(frag_target == frag_attacker) // damage done to yourself
430 M_ARGV(4, float) *= autocvar_g_keepaway_ballcarrier_selfdamage;
431 M_ARGV(6, vector) *= autocvar_g_keepaway_ballcarrier_selfforce;
433 else // damage done to other ballcarriers
435 M_ARGV(4, float) *= autocvar_g_keepaway_ballcarrier_damage;
436 M_ARGV(6, vector) *= autocvar_g_keepaway_ballcarrier_force;
439 else // if the attacker is a noncarrier
441 if(frag_target == frag_attacker) // damage done to yourself
443 M_ARGV(4, float) *= autocvar_g_keepaway_noncarrier_selfdamage;
444 M_ARGV(6, vector) *= autocvar_g_keepaway_noncarrier_selfforce;
446 else // damage done to other noncarriers
448 M_ARGV(4, float) *= autocvar_g_keepaway_noncarrier_damage;
449 M_ARGV(6, vector) *= autocvar_g_keepaway_noncarrier_force;
454 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
456 entity player = M_ARGV(0, entity);
458 if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
461 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
463 entity player = M_ARGV(0, entity);
465 if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
468 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
470 entity player = M_ARGV(0, entity);
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()
475 player.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
477 if(player.ballcarried)
478 player.effects |= autocvar_g_keepaway_ballcarrier_effects;
482 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics_UpdateStats)
484 entity player = M_ARGV(0, entity);
485 // these automatically reset, no need to worry
487 if(player.ballcarried)
488 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_keepaway_ballcarrier_highspeed;
491 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
493 entity bot = M_ARGV(0, entity);
494 entity targ = M_ARGV(1, entity);
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,
500 have_held_ball = true;
503 if(!targ.ballcarried && !bot.ballcarried && have_held_ball)
507 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
509 entity bot = M_ARGV(0, entity);
512 bot.havocbot_role = havocbot_role_ka_carrier;
514 bot.havocbot_role = havocbot_role_ka_collector;
518 MUTATOR_HOOKFUNCTION(ka, DropSpecialItems)
520 entity frag_target = M_ARGV(0, entity);
522 if(frag_target.ballcarried)
523 ka_DropEvent(frag_target);