1 #include "sv_keepaway.qh"
3 #include <common/effects/all.qh>
7 int autocvar_g_keepaway_ballcarrier_effects;
8 float autocvar_g_keepaway_ballcarrier_damage;
9 float autocvar_g_keepaway_ballcarrier_force;
10 float autocvar_g_keepaway_ballcarrier_highspeed;
11 float autocvar_g_keepaway_ballcarrier_selfdamage;
12 float autocvar_g_keepaway_ballcarrier_selfforce;
13 float autocvar_g_keepaway_noncarrier_damage;
14 float autocvar_g_keepaway_noncarrier_force;
15 float autocvar_g_keepaway_noncarrier_selfdamage;
16 float autocvar_g_keepaway_noncarrier_selfforce;
17 bool autocvar_g_keepaway_noncarrier_warn;
18 int autocvar_g_keepaway_score_bckill;
19 int autocvar_g_keepaway_score_killac;
20 int autocvar_g_keepaway_score_timepoints;
21 float autocvar_g_keepaway_score_timeinterval;
22 float autocvar_g_keepawayball_damageforcescale;
23 int autocvar_g_keepawayball_effects;
24 float autocvar_g_keepawayball_respawntime;
25 int autocvar_g_keepawayball_trail_color;
27 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
31 return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
33 // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
38 void ka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
40 if(autocvar_sv_eventlog)
41 GameLogEcho(strcat(":ka:", mode, ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
44 void ka_TouchEvent(entity this, entity toucher);
45 void ka_RespawnBall(entity this) // runs whenever the ball needs to be relocated
47 if(game_stopped) return;
48 vector oldballorigin = this.origin;
50 if(!MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
52 entity spot = SelectSpawnPoint(this, true);
53 setorigin(this, spot.origin);
54 this.angles = spot.angles;
57 makevectors(this.angles);
58 set_movetype(this, MOVETYPE_BOUNCE);
59 this.velocity = '0 0 200';
60 this.angles = '0 0 0';
61 this.effects = autocvar_g_keepawayball_effects;
62 settouch(this, ka_TouchEvent);
63 setthink(this, ka_RespawnBall);
64 this.nextthink = time + autocvar_g_keepawayball_respawntime;
65 navigation_dynamicgoal_set(this, NULL);
67 Send_Effect(EFFECT_ELECTRO_COMBO, oldballorigin, '0 0 0', 1);
68 Send_Effect(EFFECT_ELECTRO_COMBO, this.origin, '0 0 0', 1);
70 WaypointSprite_Spawn(WP_KaBall, 0, 0, this, '0 0 64', NULL, this.team, this, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
71 WaypointSprite_Ping(this.waypointsprite_attachedforcarrier);
73 sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
76 void ka_TimeScoring(entity this)
78 if(this.owner.ballcarried)
79 { // add points for holding the ball after a certain amount of time
80 if(autocvar_g_keepaway_score_timepoints)
81 GameRules_scoring_add(this.owner, SCORE, autocvar_g_keepaway_score_timepoints);
83 GameRules_scoring_add(this.owner, KEEPAWAY_BCTIME, (autocvar_g_keepaway_score_timeinterval / 1)); // interval is divided by 1 so that time always shows "seconds"
84 this.nextthink = time + autocvar_g_keepaway_score_timeinterval;
88 void ka_TouchEvent(entity this, entity toucher) // runs any time that the ball comes in contact with something
90 if (!this || game_stopped)
93 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
94 { // The ball fell off the map, respawn it since players can't get to it
98 if(IS_DEAD(toucher)) { return; }
99 if(STAT(FROZEN, toucher)) { return; }
100 if (!IS_PLAYER(toucher))
101 { // The ball just touched an object, most likely the world
102 Send_Effect(EFFECT_BALL_SPARKS, this.origin, '0 0 0', 1);
103 sound(this, CH_TRIGGER, SND_KA_TOUCH, VOL_BASE, ATTEN_NORM);
106 else if(this.wait > time) { return; }
108 // attach the ball to the player
109 this.owner = toucher;
110 toucher.ballcarried = this;
111 GameRules_scoring_vip(toucher, true);
112 setattachment(this, toucher, "");
113 setorigin(this, '0 0 0');
115 // make the ball invisible/unable to do anything/set up time scoring
116 this.velocity = '0 0 0';
117 set_movetype(this, MOVETYPE_NONE);
118 this.effects |= EF_NODRAW;
119 settouch(this, func_null);
120 setthink(this, ka_TimeScoring);
121 this.nextthink = time + autocvar_g_keepaway_score_timeinterval;
122 this.takedamage = DAMAGE_NO;
123 navigation_dynamicgoal_unset(this);
125 // apply effects to player
126 toucher.glow_color = autocvar_g_keepawayball_trail_color;
127 toucher.glow_trail = true;
128 toucher.effects |= autocvar_g_keepaway_ballcarrier_effects;
130 // messages and sounds
131 ka_EventLog("pickup", toucher);
132 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_PICKUP, toucher.netname);
133 Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_CENTER, CENTER_KEEPAWAY_PICKUP, toucher.netname);
134 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_KEEPAWAY_PICKUP_SELF);
135 sound(this.owner, CH_TRIGGER, SND_KA_PICKEDUP, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
138 GameRules_scoring_add(toucher, KEEPAWAY_PICKUPS, 1);
141 WaypointSprite_AttachCarrier(WP_KaBallCarrier, toucher, RADARICON_FLAGCARRIER);
142 toucher.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
143 WaypointSprite_UpdateRule(toucher.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
144 WaypointSprite_Ping(toucher.waypointsprite_attachedforcarrier);
145 WaypointSprite_Kill(this.waypointsprite_attachedforcarrier);
148 void ka_PlayerReset(entity plyr)
150 plyr.ballcarried = NULL;
151 GameRules_scoring_vip(plyr, false);
152 WaypointSprite_Kill(plyr.waypointsprite_attachedforcarrier);
154 // reset the player effects
155 plyr.glow_trail = false;
156 plyr.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
159 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
162 ball = plyr.ballcarried;
164 if(!ball) { return; }
167 setattachment(ball, NULL, "");
168 set_movetype(ball, MOVETYPE_BOUNCE);
169 ball.wait = time + 1;
170 settouch(ball, ka_TouchEvent);
171 setthink(ball, ka_RespawnBall);
172 ball.nextthink = time + autocvar_g_keepawayball_respawntime;
173 ball.takedamage = DAMAGE_YES;
174 ball.effects &= ~EF_NODRAW;
175 setorigin(ball, plyr.origin + '0 0 10');
176 ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
178 navigation_dynamicgoal_set(ball, plyr);
180 // messages and sounds
181 ka_EventLog("dropped", plyr);
182 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_DROPPED, plyr.netname);
183 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEEPAWAY_DROPPED, plyr.netname);
184 sound(NULL, CH_TRIGGER, SND_KA_DROPPED, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
187 WaypointSprite_Spawn(WP_KaBall, 0, 0, ball, '0 0 64', NULL, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
188 WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
189 WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
191 ka_PlayerReset(plyr);
196 MODEL(KA_BALL, "models/orbs/orbblue.md3");
200 entity plyr = ka_ball.owner;
201 if (plyr) // it was attached
202 ka_PlayerReset(plyr);
204 WaypointSprite_DetachCarrier(ka_ball);
211 entity e = new(keepawayball);
212 setmodel(e, MDL_KA_BALL);
213 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
214 e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
215 e.takedamage = DAMAGE_YES;
216 e.solid = SOLID_TRIGGER;
217 set_movetype(e, MOVETYPE_BOUNCE);
218 e.glow_color = autocvar_g_keepawayball_trail_color;
223 settouch(e, ka_TouchEvent);
226 navigation_dynamicgoal_init(ka_ball, false);
228 InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
231 void ka_Handler_CheckBall(entity this)
233 if(time < game_starttime)
244 this.nextthink = time;
247 void ka_Initialize() // run at the start of a match, initiates game mode
249 ka_Handler = new(ka_Handler);
250 setthink(ka_Handler, ka_Handler_CheckBall);
251 ka_Handler.nextthink = time;
259 void havocbot_goalrating_ball(entity this, float ratingscale, vector org)
262 ball_owner = ka_ball.owner;
264 if (ball_owner == this)
268 navigation_routerating(this, ball_owner, ratingscale, 2000);
270 navigation_routerating(this, ka_ball, ratingscale, 2000);
273 void havocbot_role_ka_carrier(entity this)
278 if (navigation_goalrating_timeout(this))
280 navigation_goalrating_start(this);
281 havocbot_goalrating_items(this, 10000, this.origin, 10000);
282 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
283 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
284 navigation_goalrating_end(this);
286 navigation_goalrating_timeout_set(this);
289 if (!this.ballcarried)
291 this.havocbot_role = havocbot_role_ka_collector;
292 navigation_goalrating_timeout_expire(this, 2);
296 void havocbot_role_ka_collector(entity this)
301 if (navigation_goalrating_timeout(this))
303 navigation_goalrating_start(this);
304 havocbot_goalrating_items(this, 10000, this.origin, 10000);
305 havocbot_goalrating_enemyplayers(this, 500, this.origin, 10000);
306 havocbot_goalrating_ball(this, 8000, this.origin);
307 navigation_goalrating_end(this);
309 navigation_goalrating_timeout_set(this);
312 if (this.ballcarried)
314 this.havocbot_role = havocbot_role_ka_carrier;
315 navigation_goalrating_timeout_expire(this, 2);
324 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
326 entity frag_attacker = M_ARGV(1, entity);
327 entity frag_target = M_ARGV(2, entity);
329 if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
331 if(frag_target.ballcarried) { // add to amount of times killing carrier
332 GameRules_scoring_add(frag_attacker, KEEPAWAY_CARRIERKILLS, 1);
333 if(autocvar_g_keepaway_score_bckill) // add bckills to the score
334 GameRules_scoring_add(frag_attacker, SCORE, autocvar_g_keepaway_score_bckill);
336 else if(!frag_attacker.ballcarried)
337 if(autocvar_g_keepaway_noncarrier_warn)
338 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
340 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
341 GameRules_scoring_add(frag_attacker, SCORE, autocvar_g_keepaway_score_killac);
344 if(frag_target.ballcarried) { ka_DropEvent(frag_target); } // a player with the ball has died, drop it
347 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
349 M_ARGV(2, float) = 0; // no frags counted in keepaway
350 return true; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
353 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
355 entity player = M_ARGV(0, entity);
357 // clear the item used for the ball in keepaway
358 player.items &= ~IT_KEY1;
360 // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
361 if(player.ballcarried)
362 player.items |= IT_KEY1;
365 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
367 entity player = M_ARGV(0, entity);
369 if(MUTATOR_RETURNVALUE == 0)
370 if(player.ballcarried)
372 ka_DropEvent(player);
377 MUTATOR_HOOKFUNCTION(ka, Damage_Calculate) // for changing damage and force values that are applied to players in g_damage.qc
379 entity frag_attacker = M_ARGV(1, entity);
380 entity frag_target = M_ARGV(2, entity);
381 float frag_damage = M_ARGV(4, float);
382 vector frag_force = M_ARGV(6, vector);
384 if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
386 if(frag_target == frag_attacker) // damage done to yourself
388 frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
389 frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
391 else // damage done to noncarriers
393 frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
394 frag_force *= autocvar_g_keepaway_ballcarrier_force;
397 else if (!frag_target.ballcarried) // if the target is a noncarrier
399 if(frag_target == frag_attacker) // damage done to yourself
401 frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
402 frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
404 else // damage done to other noncarriers
406 frag_damage *= autocvar_g_keepaway_noncarrier_damage;
407 frag_force *= autocvar_g_keepaway_noncarrier_force;
411 M_ARGV(4, float) = frag_damage;
412 M_ARGV(6, vector) = frag_force;
415 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
417 entity player = M_ARGV(0, entity);
419 if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
422 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
424 entity player = M_ARGV(0, entity);
426 if(player.ballcarried) { ka_DropEvent(player); } // a player with the ball has left the match, drop it
429 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
431 entity player = M_ARGV(0, entity);
433 // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
434 // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
436 player.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
438 if(player.ballcarried)
439 player.effects |= autocvar_g_keepaway_ballcarrier_effects;
443 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics_UpdateStats)
445 entity player = M_ARGV(0, entity);
446 // these automatically reset, no need to worry
448 if(player.ballcarried)
449 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_keepaway_ballcarrier_highspeed;
452 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
454 entity bot = M_ARGV(0, entity);
455 entity targ = M_ARGV(1, entity);
457 // if neither player has ball then don't attack unless the ball is on the ground
458 if(!targ.ballcarried && !bot.ballcarried && ka_ball.owner)
462 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
464 entity bot = M_ARGV(0, entity);
467 bot.havocbot_role = havocbot_role_ka_carrier;
469 bot.havocbot_role = havocbot_role_ka_collector;
473 MUTATOR_HOOKFUNCTION(ka, DropSpecialItems)
475 entity frag_target = M_ARGV(0, entity);
477 if(frag_target.ballcarried)
478 ka_DropEvent(frag_target);