3 #include <common/effects/all.qh>
7 int autocvar_g_tka_ballcarrier_effects;
8 float autocvar_g_tka_ballcarrier_damage;
9 float autocvar_g_tka_ballcarrier_force;
10 float autocvar_g_tka_ballcarrier_highspeed;
11 float autocvar_g_tka_ballcarrier_selfdamage;
12 float autocvar_g_tka_ballcarrier_selfforce;
13 float autocvar_g_tka_noncarrier_damage;
14 float autocvar_g_tka_noncarrier_force;
15 float autocvar_g_tka_noncarrier_selfdamage;
16 float autocvar_g_tka_noncarrier_selfforce;
17 bool autocvar_g_tka_noncarrier_warn;
18 int autocvar_g_tka_score_bckill;
19 int autocvar_g_tka_score_killac;
20 bool autocvar_g_tka_score_team;
21 int autocvar_g_tka_score_timepoints;
22 float autocvar_g_tka_score_timeinterval;
23 float autocvar_g_tkaball_damageforcescale;
24 int autocvar_g_tkaball_effects;
25 float autocvar_g_tkaball_respawntime;
26 int autocvar_g_tkaball_trail_color;
28 bool tka_ballcarrier_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
32 return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
34 // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
39 void tka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
41 if(autocvar_sv_eventlog)
42 GameLogEcho(strcat(":tka:", mode, ((actor != NULL) ? (strcat(":", ftos(actor.team), ":", ftos(actor.playerid))) : "")));
45 void tka_TouchEvent(entity this, entity toucher);
46 void tka_RespawnBall(entity this) // runs whenever the ball needs to be relocated
48 if(game_stopped) return;
49 vector oldballorigin = this.origin;
51 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 setorigin(this, SelectSpawnPoint(this, true).origin);
54 set_movetype(this, MOVETYPE_BOUNCE);
55 this.velocity = '0 0 200';
56 this.angles = '0 0 0';
57 this.effects = autocvar_g_tkaball_effects;
58 settouch(this, tka_TouchEvent);
59 setthink(this, tka_RespawnBall);
60 this.nextthink = time + autocvar_g_tkaball_respawntime;
61 navigation_dynamicgoal_set(this, NULL);
63 Send_Effect(EFFECT_ELECTRO_COMBO, oldballorigin, '0 0 0', 1);
64 Send_Effect(EFFECT_ELECTRO_COMBO, this.origin, '0 0 0', 1);
66 WaypointSprite_Spawn(WP_KaBall, 0, 0, this, '0 0 64', NULL, this.team, this, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
67 WaypointSprite_Ping(this.waypointsprite_attachedforcarrier);
69 sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
72 void tka_TimeScoring(entity this)
74 if(this.owner.ballcarried)
75 { // add points for holding the ball after a certain amount of time
76 if(autocvar_g_tka_score_timepoints)
77 GameRules_scoring_add_team(this.owner, SCORE, autocvar_g_tka_score_timepoints);
79 GameRules_scoring_add(this.owner, TKA_BCTIME, (autocvar_g_tka_score_timeinterval / 1)); // interval is divided by 1 so that time always shows "seconds"
80 this.nextthink = time + autocvar_g_tka_score_timeinterval;
84 void tka_TouchEvent(entity this, entity toucher) // runs any time that the ball comes in contact with something
86 if (!this || game_stopped)
89 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
90 { // The ball fell off the map, respawn it since players can't get to it
91 tka_RespawnBall(this);
94 if(toucher.ballcarried) { return; }
95 if(IS_DEAD(toucher)) { return; }
96 if(STAT(FROZEN, toucher)) { return; }
97 if (!IS_PLAYER(toucher))
98 { // The ball just touched an object, most likely the world
99 Send_Effect(EFFECT_BALL_SPARKS, this.origin, '0 0 0', 1);
100 sound(this, CH_TRIGGER, SND_KA_TOUCH, VOL_BASE, ATTEN_NORM);
103 else if(this.wait > time) { return; }
105 // attach the ball to the player
106 this.owner = toucher;
107 toucher.ballcarried = this;
108 GameRules_scoring_vip(toucher, true);
109 setattachment(this, toucher, "");
110 setorigin(this, '0 0 0');
112 // make the ball invisible/unable to do anything/set up time scoring
113 this.velocity = '0 0 0';
114 set_movetype(this, MOVETYPE_NONE);
115 this.effects |= EF_NODRAW;
116 settouch(this, func_null);
117 setthink(this, tka_TimeScoring);
118 this.nextthink = time + autocvar_g_tka_score_timeinterval;
119 this.takedamage = DAMAGE_NO;
120 navigation_dynamicgoal_unset(this);
122 // apply effects to player
123 toucher.glow_color = autocvar_g_tkaball_trail_color;
124 toucher.glow_trail = true;
125 toucher.effects |= autocvar_g_tka_ballcarrier_effects;
127 // messages and sounds
128 tka_EventLog("pickup", toucher);
129 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_PICKUP, toucher.netname);
130 Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_CENTER, CENTER_KEEPAWAY_PICKUP, toucher.netname);
131 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_KEEPAWAY_PICKUP_SELF);
132 sound(this.owner, CH_TRIGGER, SND_KA_PICKEDUP, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
135 GameRules_scoring_add(toucher, TKA_PICKUPS, 1);
138 WaypointSprite_AttachCarrier(WP_Null, toucher, RADARICON_FLAGCARRIER);
139 toucher.waypointsprite_attachedforcarrier.colormod = colormapPaletteColor(toucher.team - 1, 0);
140 toucher.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = tka_ballcarrier_waypointsprite_visible_for_player;
141 WaypointSprite_UpdateRule(toucher.waypointsprite_attachedforcarrier, toucher.team, SPRITERULE_TEAMPLAY);
142 if(toucher.team == NUM_TEAM_1)
143 WaypointSprite_UpdateSprites(toucher.waypointsprite_attachedforcarrier, WP_TkaBallCarrierRed, WP_KaBallCarrier, WP_TkaBallCarrierRed);
144 else if(toucher.team == NUM_TEAM_2)
145 WaypointSprite_UpdateSprites(toucher.waypointsprite_attachedforcarrier, WP_TkaBallCarrierBlue, WP_KaBallCarrier, WP_TkaBallCarrierBlue);
146 else if(toucher.team == NUM_TEAM_3)
147 WaypointSprite_UpdateSprites(toucher.waypointsprite_attachedforcarrier, WP_TkaBallCarrierYellow, WP_KaBallCarrier, WP_TkaBallCarrierYellow);
148 else if(toucher.team == NUM_TEAM_4)
149 WaypointSprite_UpdateSprites(toucher.waypointsprite_attachedforcarrier, WP_TkaBallCarrierPink, WP_KaBallCarrier, WP_TkaBallCarrierPink);
150 WaypointSprite_Ping(toucher.waypointsprite_attachedforcarrier);
151 WaypointSprite_Kill(this.waypointsprite_attachedforcarrier);
154 void tka_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_tka_ballcarrier_effects;
165 void tka_DropEvent(entity player) // runs any time that a player is supposed to lose the ball
168 ball = player.ballcarried;
170 if(!ball) { return; }
173 setattachment(ball, NULL, "");
174 set_movetype(ball, MOVETYPE_BOUNCE);
175 ball.wait = time + 1;
176 settouch(ball, tka_TouchEvent);
177 setthink(ball, tka_RespawnBall);
178 ball.nextthink = time + autocvar_g_tkaball_respawntime;
179 ball.takedamage = DAMAGE_YES;
180 ball.effects &= ~EF_NODRAW;
181 setorigin(ball, player.origin + '0 0 10');
182 nudgeoutofsolid(ball); // a ball has a horizontally bigger bbox than a player
183 ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
185 navigation_dynamicgoal_set(ball, player);
187 // messages and sounds
188 tka_EventLog("dropped", player);
189 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_KEEPAWAY_DROPPED, player.netname);
190 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_KEEPAWAY_DROPPED, player.netname);
191 sound(NULL, CH_TRIGGER, SND_KA_DROPPED, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
194 WaypointSprite_Spawn(WP_KaBall, 0, 0, ball, '0 0 64', NULL, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
195 WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
196 WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
198 tka_PlayerReset(player);
203 MODEL(TKA_BALL, "models/orbs/orbblue.md3");
205 void tka_RemoveBalls()
207 IL_EACH(g_tkaballs, true,
209 if (it.owner) // it was attached
210 tka_PlayerReset(it.owner);
212 WaypointSprite_DetachCarrier(it);
217 void tka_SpawnBalls()
220 do // never allow less than 1 ball to spawn
222 entity e = new(keepawayball);
223 setmodel(e, MDL_TKA_BALL);
224 // 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
225 // bones_was_here: that was WITH sv_legacy_bbox_expand 1 and FL_ITEM (mins -= '15 15 1'; maxs += '15 15 1')
226 // it's round so should have a symmetrical bbox, same height as pickup items so it can't be jumped over in any physics
227 setsize(e, '-24 -24 -24', '24 24 24');
228 e.damageforcescale = autocvar_g_tkaball_damageforcescale;
229 e.takedamage = DAMAGE_YES;
230 e.solid = SOLID_TRIGGER;
231 set_movetype(e, MOVETYPE_BOUNCE);
232 e.glow_color = autocvar_g_tkaball_trail_color;
237 settouch(e, tka_TouchEvent);
239 IL_PUSH(g_tkaballs, e);
240 navigation_dynamicgoal_init(e, false);
246 while (i < TKA_BALL_COUNT);
249 void tka_Handler_CheckBall(entity this)
251 if(time < game_starttime)
253 if (!IL_EMPTY(g_tkaballs))
258 if (IL_EMPTY(g_tkaballs))
262 this.nextthink = time;
270 void havocbot_goalrating_tkaball(entity this, float ratingscale, vector org)
272 entity ball = NULL, ball_carried = NULL;
274 // stops at last ball, prefers ball without carrier
275 IL_EACH(g_tkaballs, it.owner != this && DIFF_TEAM(ball.owner, this),
278 ball_carried = it.owner;
284 navigation_routerating(this, ball, ratingscale, 2000);
285 else if(ball_carried)
286 navigation_routerating(this, ball_carried, ratingscale, 2000);
289 void havocbot_role_tka_carrier(entity this)
294 if (navigation_goalrating_timeout(this))
296 navigation_goalrating_start(this);
297 havocbot_goalrating_items(this, 10000, this.origin, 10000);
298 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
299 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
300 navigation_goalrating_end(this);
302 navigation_goalrating_timeout_set(this);
305 if (!this.ballcarried)
307 this.havocbot_role = havocbot_role_tka_collector;
308 navigation_goalrating_timeout_expire(this, 2);
312 void havocbot_role_tka_collector(entity this)
317 if (navigation_goalrating_timeout(this))
319 navigation_goalrating_start(this);
320 havocbot_goalrating_items(this, 10000, this.origin, 10000);
321 havocbot_goalrating_enemyplayers(this, 500, this.origin, 10000);
322 havocbot_goalrating_tkaball(this, 8000, this.origin);
323 navigation_goalrating_end(this);
325 navigation_goalrating_timeout_set(this);
328 if (this.ballcarried)
330 this.havocbot_role = havocbot_role_tka_carrier;
331 navigation_goalrating_timeout_expire(this, 2);
340 MUTATOR_HOOKFUNCTION(tka, PlayerDies)
342 entity frag_attacker = M_ARGV(1, entity);
343 entity frag_target = M_ARGV(2, entity);
345 if(frag_attacker != frag_target && IS_PLAYER(frag_attacker) && DIFF_TEAM(frag_attacker, frag_target))
347 bool team_has_ball = false;
348 IL_EACH(g_tkaballs, it.owner != frag_attacker && SAME_TEAM(it.owner, frag_attacker),
350 team_has_ball = true;
353 if(frag_target.ballcarried) { // add to amount of times killing carrier
354 GameRules_scoring_add(frag_attacker, TKA_CARRIERKILLS, 1);
355 if(autocvar_g_tka_score_bckill) // add bckills to the score
356 GameRules_scoring_add_team(frag_attacker, SCORE, autocvar_g_tka_score_bckill);
358 else if(!frag_attacker.ballcarried && !(autocvar_g_tka_score_team && team_has_ball))
360 if(autocvar_g_tka_noncarrier_warn)
361 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
364 if(frag_attacker.ballcarried || (autocvar_g_tka_score_team && team_has_ball)) // add to amount of kills while ballcarrier (or if team scoring is enabled)
365 GameRules_scoring_add_team(frag_attacker, SCORE, autocvar_g_tka_score_killac);
368 if(frag_target.ballcarried) { tka_DropEvent(frag_target); } // a player with the ball has died, drop it
371 MUTATOR_HOOKFUNCTION(tka, GiveFragsForKill)
373 M_ARGV(2, float) = 0; // no frags counted in keepaway
374 return true; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
377 MUTATOR_HOOKFUNCTION(tka, Scores_CountFragsRemaining)
379 // announce remaining frags, but only when timed scoring is off
380 return !autocvar_g_tka_score_timepoints;
383 MUTATOR_HOOKFUNCTION(tka, PlayerPreThink)
385 entity player = M_ARGV(0, entity);
387 // clear the item used for the ball in keepaway
388 STAT(TKA_BALLSTATUS, player) = 0;
390 // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
391 if(player.ballcarried)
392 STAT(TKA_BALLSTATUS, player) |= TKA_BALL_CARRYING;
394 IL_EACH(g_tkaballs, true,
397 STAT(TKA_BALLSTATUS, player) |= TKA_BALL_DROPPED;
400 // TODO: teamless carrier?
401 switch(it.owner.team)
403 case NUM_TEAM_1: STAT(TKA_BALLSTATUS, player) |= TKA_BALL_TAKEN_RED; break;
404 case NUM_TEAM_2: STAT(TKA_BALLSTATUS, player) |= TKA_BALL_TAKEN_BLUE; break;
405 case NUM_TEAM_3: STAT(TKA_BALLSTATUS, player) |= TKA_BALL_TAKEN_YELLOW; break;
406 case NUM_TEAM_4: STAT(TKA_BALLSTATUS, player) |= TKA_BALL_TAKEN_PINK; break;
412 MUTATOR_HOOKFUNCTION(tka, PlayerUseKey)
414 entity player = M_ARGV(0, entity);
416 if(MUTATOR_RETURNVALUE == 0)
417 if(player.ballcarried)
419 tka_DropEvent(player);
424 MUTATOR_HOOKFUNCTION(tka, Damage_Calculate) // for changing damage and force values that are applied to players
426 entity frag_attacker = M_ARGV(1, entity);
427 entity frag_target = M_ARGV(2, entity);
428 float frag_damage = M_ARGV(4, float);
429 vector frag_force = M_ARGV(6, vector);
431 // as a gamemode rule, only apply scaling to player versus player combat
432 if(!IS_PLAYER(frag_attacker) || !IS_PLAYER(frag_target))
435 if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
437 if(frag_target == frag_attacker) // damage done to yourself
439 frag_damage *= autocvar_g_tka_ballcarrier_selfdamage;
440 frag_force *= autocvar_g_tka_ballcarrier_selfforce;
442 else // damage done to other ballcarriers
444 frag_damage *= autocvar_g_tka_ballcarrier_damage;
445 frag_force *= autocvar_g_tka_ballcarrier_force;
448 else // if the attacker is a noncarrier
450 if(frag_target == frag_attacker) // damage done to yourself
452 frag_damage *= autocvar_g_tka_noncarrier_selfdamage;
453 frag_force *= autocvar_g_tka_noncarrier_selfforce;
455 else // damage done to other noncarriers
457 frag_damage *= autocvar_g_tka_noncarrier_damage;
458 frag_force *= autocvar_g_tka_noncarrier_force;
462 M_ARGV(4, float) = frag_damage;
463 M_ARGV(6, vector) = frag_force;
466 MUTATOR_HOOKFUNCTION(tka, ClientDisconnect)
468 entity player = M_ARGV(0, entity);
470 if(player.ballcarried) { tka_DropEvent(player); } // a player with the ball has left the match, drop it
473 MUTATOR_HOOKFUNCTION(tka, MakePlayerObserver)
475 entity player = M_ARGV(0, entity);
477 if(player.ballcarried) { tka_DropEvent(player); } // a player with the ball has left the match, drop it
480 MUTATOR_HOOKFUNCTION(tka, PlayerPowerups)
482 entity player = M_ARGV(0, entity);
484 // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
485 // So bare with me until I can fix a certain bug with tka_ballcarrier_waypointsprite_visible_for_player()
487 player.effects &= ~autocvar_g_tka_ballcarrier_effects;
489 if(player.ballcarried)
490 player.effects |= autocvar_g_tka_ballcarrier_effects;
494 MUTATOR_HOOKFUNCTION(tka, PlayerPhysics_UpdateStats)
496 entity player = M_ARGV(0, entity);
497 // these automatically reset, no need to worry
499 if(player.ballcarried)
500 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_tka_ballcarrier_highspeed;
503 MUTATOR_HOOKFUNCTION(tka, BotShouldAttack)
505 entity bot = M_ARGV(0, entity);
506 entity targ = M_ARGV(1, entity);
508 // if neither player has ball then don't attack unless the ball is on the ground
509 bool have_held_ball = false, team_has_ball = false;
510 IL_EACH(g_tkaballs, it.owner,
512 have_held_ball = true;
513 if(SAME_TEAM(bot, it.owner))
514 team_has_ball = true;
517 if(!targ.ballcarried && !bot.ballcarried && have_held_ball && !(autocvar_g_tka_score_team && team_has_ball))
521 MUTATOR_HOOKFUNCTION(tka, HavocBot_ChooseRole)
523 entity bot = M_ARGV(0, entity);
526 bot.havocbot_role = havocbot_role_tka_carrier;
528 bot.havocbot_role = havocbot_role_tka_collector;
532 MUTATOR_HOOKFUNCTION(tka, DropSpecialItems)
534 entity frag_target = M_ARGV(0, entity);
536 if(frag_target.ballcarried)
537 tka_DropEvent(frag_target);
540 MUTATOR_HOOKFUNCTION(tka, SpectateCopy)
542 entity spectatee = M_ARGV(0, entity);
543 entity client = M_ARGV(1, entity);
545 STAT(TKA_BALLSTATUS, client) = STAT(TKA_BALLSTATUS, spectatee);
548 MUTATOR_HOOKFUNCTION(tka, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
550 M_ARGV(0, float) = tka_teams;
554 void tka_Initialize()
556 tka_teams = autocvar_g_tka_teams_override;
558 tka_teams = cvar("g_tka_teams"); // read the cvar directly as it gets written earlier in the same frame
559 tka_teams = BITS(bound(2, tka_teams, 4));
560 GameRules_scoring(tka_teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, {
561 field(SP_TKA_PICKUPS, "pickups", 0);
562 field(SP_TKA_CARRIERKILLS, "bckills", 0);
563 field(SP_TKA_BCTIME, "bctime", SFL_SORT_PRIO_SECONDARY);
566 g_tkaballs = IL_NEW();
567 entity tka_Handler = new_pure(tka_Handler);
568 setthink(tka_Handler, tka_Handler_CheckBall);
569 InitializeEntity(tka_Handler, tka_Handler_CheckBall, INITPRIO_GAMETYPE);