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_TouchEvent(entity this, entity toucher);
50 void ka_RespawnBall(entity this) // runs whenever the ball needs to be relocated
52 if(game_stopped) return;
53 vector oldballorigin = this.origin;
55 if(!MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
57 entity spot = SelectSpawnPoint(this, true);
58 setorigin(this, spot.origin);
59 this.angles = spot.angles;
62 makevectors(this.angles);
63 set_movetype(this, MOVETYPE_BOUNCE);
64 this.velocity = '0 0 200';
65 this.angles = '0 0 0';
66 this.effects = autocvar_g_keepawayball_effects;
67 settouch(this, ka_TouchEvent);
68 setthink(this, ka_RespawnBall);
69 this.nextthink = time + autocvar_g_keepawayball_respawntime;
70 navigation_dynamicgoal_set(this, NULL);
72 Send_Effect(EFFECT_ELECTRO_COMBO, oldballorigin, '0 0 0', 1);
73 Send_Effect(EFFECT_ELECTRO_COMBO, this.origin, '0 0 0', 1);
75 WaypointSprite_Spawn(WP_KaBall, 0, 0, this, '0 0 64', NULL, this.team, this, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
76 WaypointSprite_Ping(this.waypointsprite_attachedforcarrier);
78 sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
81 void ka_TimeScoring(entity this)
83 if(this.owner.ballcarried)
84 { // add points for holding the ball after a certain amount of time
85 if(autocvar_g_keepaway_score_timepoints)
86 GameRules_scoring_add(this.owner, SCORE, autocvar_g_keepaway_score_timepoints);
88 GameRules_scoring_add(this.owner, KEEPAWAY_BCTIME, (autocvar_g_keepaway_score_timeinterval / 1)); // interval is divided by 1 so that time always shows "seconds"
89 this.nextthink = time + autocvar_g_keepaway_score_timeinterval;
93 void ka_TouchEvent(entity this, entity toucher) // runs any time that the ball comes in contact with something
95 if (!this || game_stopped)
98 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
99 { // The ball fell off the map, respawn it since players can't get to it
100 ka_RespawnBall(this);
103 if(toucher.ballcarried) { return; }
104 if(IS_DEAD(toucher)) { return; }
105 if(STAT(FROZEN, toucher)) { return; }
106 if (!IS_PLAYER(toucher))
107 { // The ball just touched an object, most likely the world
108 Send_Effect(EFFECT_BALL_SPARKS, this.origin, '0 0 0', 1);
109 sound(this, CH_TRIGGER, SND_KA_TOUCH, VOL_BASE, ATTEN_NORM);
112 else if(this.wait > time) { return; }
114 // attach the ball to the player
115 this.owner = toucher;
116 toucher.ballcarried = this;
117 GameRules_scoring_vip(toucher, true);
118 setattachment(this, toucher, "");
119 setorigin(this, '0 0 0');
121 // make the ball invisible/unable to do anything/set up time scoring
122 this.velocity = '0 0 0';
123 set_movetype(this, MOVETYPE_NONE);
124 this.effects |= EF_NODRAW;
125 settouch(this, func_null);
126 setthink(this, ka_TimeScoring);
127 this.nextthink = time + autocvar_g_keepaway_score_timeinterval;
128 this.takedamage = DAMAGE_NO;
129 navigation_dynamicgoal_unset(this);
131 // apply effects to player
132 toucher.glow_color = autocvar_g_keepawayball_trail_color;
133 toucher.glow_trail = true;
134 toucher.effects |= autocvar_g_keepaway_ballcarrier_effects;
136 // messages and sounds
137 ka_EventLog("pickup", toucher);
138 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_PICKUP, toucher.netname);
139 Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_CENTER, CENTER_KEEPAWAY_PICKUP, toucher.netname);
140 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_KEEPAWAY_PICKUP_SELF);
141 sound(this.owner, CH_TRIGGER, SND_KA_PICKEDUP, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
144 GameRules_scoring_add(toucher, KEEPAWAY_PICKUPS, 1);
147 WaypointSprite_AttachCarrier(WP_KaBallCarrier, toucher, RADARICON_FLAGCARRIER);
148 toucher.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
149 WaypointSprite_UpdateRule(toucher.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
150 WaypointSprite_Ping(toucher.waypointsprite_attachedforcarrier);
151 WaypointSprite_Kill(this.waypointsprite_attachedforcarrier);
154 void ka_PlayerReset(entity player)
156 player.ballcarried = NULL;
157 GameRules_scoring_vip(player, false);
158 WaypointSprite_Kill(player.waypointsprite_attachedforcarrier);
160 // reset the player effects
161 player.glow_trail = false;
162 player.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
165 void ka_DropEvent(entity player) // runs any time that a player is supposed to lose the ball
167 entity ball = player.ballcarried;
169 if(!ball) { return; }
172 setattachment(ball, NULL, "");
173 set_movetype(ball, MOVETYPE_BOUNCE);
174 ball.wait = time + 1;
175 settouch(ball, ka_TouchEvent);
176 setthink(ball, ka_RespawnBall);
177 ball.nextthink = time + autocvar_g_keepawayball_respawntime;
178 ball.takedamage = DAMAGE_YES;
179 ball.effects &= ~EF_NODRAW;
180 setorigin(ball, player.origin + '0 0 10');
181 ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
183 navigation_dynamicgoal_set(ball, player);
185 // messages and sounds
186 ka_EventLog("dropped", player);
187 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_DROPPED, player.netname);
188 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEEPAWAY_DROPPED, player.netname);
189 sound(NULL, CH_TRIGGER, SND_KA_DROPPED, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
192 WaypointSprite_Spawn(WP_KaBall, 0, 0, ball, '0 0 64', NULL, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
193 WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
194 WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
196 ka_PlayerReset(player);
201 MODEL(KA_BALL, "models/orbs/orbblue.md3");
203 void ka_RemoveBall(entity ball)
205 entity player = ball.owner;
206 if (player) // it was attached
207 ka_PlayerReset(player);
209 WaypointSprite_DetachCarrier(ball);
213 void ka_RemoveBalls()
215 IL_EACH(g_kaballs, true,
223 entity e = new(keepawayball);
224 setmodel(e, MDL_KA_BALL);
225 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
226 e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
227 e.takedamage = DAMAGE_YES;
228 e.solid = SOLID_TRIGGER;
229 set_movetype(e, MOVETYPE_BOUNCE);
230 e.glow_color = autocvar_g_keepawayball_trail_color;
235 settouch(e, ka_TouchEvent);
237 IL_PUSH(g_kaballs, e);
238 navigation_dynamicgoal_init(e, false);
240 InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
243 void ka_SpawnBalls(int ballcount)
245 int realballcount = max(1, ballcount); // never allow less than 1 ball to spawn
246 for(int j = 0; j < realballcount; ++j)
252 void ka_Handler_CheckBall(entity this)
254 if(time < game_starttime)
256 if (!IL_EMPTY(g_kaballs))
261 if (IL_EMPTY(g_kaballs))
262 ka_SpawnBalls(KA_BALL_COUNT); // ;)
265 this.nextthink = time;
268 void ka_Initialize() // run at the start of a match, initiates game mode
270 g_kaballs = IL_NEW();
271 ka_Handler = new_pure(ka_Handler);
272 setthink(ka_Handler, ka_Handler_CheckBall);
273 ka_Handler.nextthink = time;
281 void havocbot_goalrating_ball(entity this, float ratingscale, vector org)
283 entity ball = NULL, ball_carried = NULL;
285 // stops at last ball, prefers ball without carrier
286 IL_EACH(g_kaballs, it.owner != this,
289 ball_carried = it.owner;
295 navigation_routerating(this, ball, ratingscale, 2000);
296 else if(ball_carried)
297 navigation_routerating(this, ball_carried, ratingscale, 2000);
300 void havocbot_role_ka_carrier(entity this)
305 if (navigation_goalrating_timeout(this))
307 navigation_goalrating_start(this);
308 havocbot_goalrating_items(this, 10000, this.origin, 10000);
309 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
310 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
311 navigation_goalrating_end(this);
313 navigation_goalrating_timeout_set(this);
316 if (!this.ballcarried)
318 this.havocbot_role = havocbot_role_ka_collector;
319 navigation_goalrating_timeout_expire(this, 2);
323 void havocbot_role_ka_collector(entity this)
328 if (navigation_goalrating_timeout(this))
330 navigation_goalrating_start(this);
331 havocbot_goalrating_items(this, 10000, this.origin, 10000);
332 havocbot_goalrating_enemyplayers(this, 500, this.origin, 10000);
333 havocbot_goalrating_ball(this, 8000, this.origin);
334 navigation_goalrating_end(this);
336 navigation_goalrating_timeout_set(this);
339 if (this.ballcarried)
341 this.havocbot_role = havocbot_role_ka_carrier;
342 navigation_goalrating_timeout_expire(this, 2);
351 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
353 entity frag_attacker = M_ARGV(1, entity);
354 entity frag_target = M_ARGV(2, entity);
356 if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
358 if(frag_target.ballcarried) { // add to amount of times killing carrier
359 GameRules_scoring_add(frag_attacker, KEEPAWAY_CARRIERKILLS, 1);
360 if(autocvar_g_keepaway_score_bckill) // add bckills to the score
361 GameRules_scoring_add(frag_attacker, SCORE, autocvar_g_keepaway_score_bckill);
363 else if(!frag_attacker.ballcarried)
364 if(autocvar_g_keepaway_noncarrier_warn)
365 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
367 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
368 GameRules_scoring_add(frag_attacker, SCORE, autocvar_g_keepaway_score_killac);
371 if(frag_target.ballcarried) { ka_DropEvent(frag_target); } // a player with the ball has died, drop it
374 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
376 M_ARGV(2, float) = 0; // no frags counted in keepaway
377 return true; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
380 MUTATOR_HOOKFUNCTION(ka, Scores_CountFragsRemaining)
382 // announce remaining frags, but only when timed scoring is off
383 return !autocvar_g_keepaway_score_timepoints;
386 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
388 entity player = M_ARGV(0, entity);
390 // clear the item used for the ball in keepaway
391 player.items &= ~IT_KEY1;
393 // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
394 if(player.ballcarried)
395 player.items |= IT_KEY1;
398 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
400 entity player = M_ARGV(0, entity);
402 if(MUTATOR_RETURNVALUE == 0)
403 if(player.ballcarried)
405 ka_DropEvent(player);
410 MUTATOR_HOOKFUNCTION(ka, Damage_Calculate) // for changing damage and force values that are applied to players in damage.qc
412 entity frag_attacker = M_ARGV(1, entity);
413 entity frag_target = M_ARGV(2, entity);
414 float frag_damage = M_ARGV(4, float);
415 vector frag_force = M_ARGV(6, vector);
417 if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
419 if(frag_target == frag_attacker) // damage done to yourself
421 frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
422 frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
424 else // damage done to noncarriers
426 frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
427 frag_force *= autocvar_g_keepaway_ballcarrier_force;
430 else if (IS_PLAYER(frag_attacker) && !frag_target.ballcarried) // if the target is a noncarrier
432 if(frag_target == frag_attacker) // damage done to yourself
434 frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
435 frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
437 else // damage done to other noncarriers
439 frag_damage *= autocvar_g_keepaway_noncarrier_damage;
440 frag_force *= autocvar_g_keepaway_noncarrier_force;
444 M_ARGV(4, float) = frag_damage;
445 M_ARGV(6, vector) = frag_force;
448 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
450 entity player = M_ARGV(0, entity);
452 if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
455 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
457 entity player = M_ARGV(0, entity);
459 if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
462 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
464 entity player = M_ARGV(0, entity);
466 // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
467 // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
469 player.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
471 if(player.ballcarried)
472 player.effects |= autocvar_g_keepaway_ballcarrier_effects;
476 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics_UpdateStats)
478 entity player = M_ARGV(0, entity);
479 // these automatically reset, no need to worry
481 if(player.ballcarried)
482 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_keepaway_ballcarrier_highspeed;
485 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
487 entity bot = M_ARGV(0, entity);
488 entity targ = M_ARGV(1, entity);
490 // if neither player has ball then don't attack unless the ball is on the ground
491 bool have_held_ball = false;
492 IL_EACH(g_kaballs, it.owner,
494 have_held_ball = true;
497 if(!targ.ballcarried && !bot.ballcarried && have_held_ball)
501 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
503 entity bot = M_ARGV(0, entity);
506 bot.havocbot_role = havocbot_role_ka_carrier;
508 bot.havocbot_role = havocbot_role_ka_collector;
512 MUTATOR_HOOKFUNCTION(ka, DropSpecialItems)
514 entity frag_target = M_ARGV(0, entity);
516 if(frag_target.ballcarried)
517 ka_DropEvent(frag_target);