1 #include "gamemode_keepaway.qh"
5 // ===========================================================
6 // Keepaway game mode coding, written by Samual and Diabolik
7 // Last updated: September, 2012
8 // ===========================================================
10 float ka_ballcarrier_waypointsprite_visible_for_player(entity e) // runs on waypoints which are attached to ballcarriers, updates once per frame
14 return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
16 // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
21 void ka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
23 if(autocvar_sv_eventlog)
24 GameLogEcho(strcat(":ka:", mode, ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
28 void ka_RespawnBall() // runs whenever the ball needs to be relocated
30 if(gameover) { return; }
31 vector oldballorigin = self.origin;
33 if(!MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
35 entity spot = SelectSpawnPoint(true);
36 setorigin(self, spot.origin);
37 self.angles = spot.angles;
40 makevectors(self.angles);
41 self.movetype = MOVETYPE_BOUNCE;
42 self.velocity = '0 0 200';
43 self.angles = '0 0 0';
44 self.effects = autocvar_g_keepawayball_effects;
45 self.touch = ka_TouchEvent;
46 self.think = ka_RespawnBall;
47 self.nextthink = time + autocvar_g_keepawayball_respawntime;
49 Send_Effect(EFFECT_ELECTRO_COMBO, oldballorigin, '0 0 0', 1);
50 Send_Effect(EFFECT_ELECTRO_COMBO, self.origin, '0 0 0', 1);
52 WaypointSprite_Spawn(WP_KaBall, 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
53 WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);
55 sound(self, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
60 if(self.owner.ballcarried)
61 { // add points for holding the ball after a certain amount of time
62 if(autocvar_g_keepaway_score_timepoints)
63 PlayerScore_Add(self.owner, SP_SCORE, autocvar_g_keepaway_score_timepoints);
65 PlayerScore_Add(self.owner, SP_KEEPAWAY_BCTIME, (autocvar_g_keepaway_score_timeinterval / 1)); // interval is divided by 1 so that time always shows "seconds"
66 self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
70 void ka_TouchEvent() // runs any time that the ball comes in contact with something
72 if(gameover) { return; }
74 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
75 { // The ball fell off the map, respawn it since players can't get to it
79 if(other.deadflag != DEAD_NO) { return; }
80 if(other.frozen) { return; }
81 if (!IS_PLAYER(other))
82 { // The ball just touched an object, most likely the world
83 Send_Effect(EFFECT_BALL_SPARKS, self.origin, '0 0 0', 1);
84 sound(self, CH_TRIGGER, SND_KA_TOUCH, VOL_BASE, ATTEN_NORM);
87 else if(self.wait > time) { return; }
89 // attach the ball to the player
91 other.ballcarried = self;
92 setattachment(self, other, "");
93 setorigin(self, '0 0 0');
95 // make the ball invisible/unable to do anything/set up time scoring
96 self.velocity = '0 0 0';
97 self.movetype = MOVETYPE_NONE;
98 self.effects |= EF_NODRAW;
99 self.touch = func_null;
100 self.think = ka_TimeScoring;
101 self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
102 self.takedamage = DAMAGE_NO;
104 // apply effects to player
105 other.glow_color = autocvar_g_keepawayball_trail_color;
106 other.glow_trail = true;
107 other.effects |= autocvar_g_keepaway_ballcarrier_effects;
109 // messages and sounds
110 ka_EventLog("pickup", other);
111 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_KEEPAWAY_PICKUP, other.netname);
112 Send_Notification(NOTIF_ALL_EXCEPT, other, MSG_CENTER, CENTER_KEEPAWAY_PICKUP, other.netname);
113 Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_KEEPAWAY_PICKUP_SELF);
114 sound(self.owner, CH_TRIGGER, SND_KA_PICKEDUP, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
117 PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
120 WaypointSprite_AttachCarrier(WP_KaBallCarrier, other, RADARICON_FLAGCARRIER);
121 other.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
122 WaypointSprite_UpdateRule(other.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
123 WaypointSprite_Ping(other.waypointsprite_attachedforcarrier);
124 WaypointSprite_Kill(self.waypointsprite_attachedforcarrier);
127 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
130 ball = plyr.ballcarried;
132 if(!ball) { return; }
135 setattachment(ball, world, "");
136 ball.movetype = MOVETYPE_BOUNCE;
137 ball.wait = time + 1;
138 ball.touch = ka_TouchEvent;
139 ball.think = ka_RespawnBall;
140 ball.nextthink = time + autocvar_g_keepawayball_respawntime;
141 ball.takedamage = DAMAGE_YES;
142 ball.effects &= ~EF_NODRAW;
143 setorigin(ball, plyr.origin + '0 0 10');
144 ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
145 ball.owner.ballcarried = world; // I hope nothing checks to see if the world has the ball in the rest of my code :P
148 // reset the player effects
149 plyr.glow_trail = false;
150 plyr.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
152 // messages and sounds
153 ka_EventLog("dropped", plyr);
154 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_KEEPAWAY_DROPPED, plyr.netname);
155 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_KEEPAWAY_DROPPED, plyr.netname);
156 sound(other, CH_TRIGGER, SND_KA_DROPPED, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
159 // PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1); Not anymore, this is 100% the same as pickups and is useless.
162 WaypointSprite_Spawn(WP_KaBall, 0, 0, ball, '0 0 64', world, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
163 WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
164 WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
165 WaypointSprite_Kill(plyr.waypointsprite_attachedforcarrier);
168 void ka_Reset() // used to clear the ballcarrier whenever the match switches from warmup to normal
170 if((self.owner) && (IS_PLAYER(self.owner)))
171 ka_DropEvent(self.owner);
173 if(time < game_starttime)
175 self.think = ka_RespawnBall;
176 self.touch = func_null;
177 self.nextthink = game_starttime;
188 void havocbot_goalrating_ball(float ratingscale, vector org)
192 ball_owner = ka_ball.owner;
194 if (ball_owner == self)
197 // If ball is carried by player then hunt them down.
200 t = (self.health + self.armorvalue) / (ball_owner.health + ball_owner.armorvalue);
201 navigation_routerating(ball_owner, t * ratingscale, 2000);
203 else // Ball has been dropped so collect.
204 navigation_routerating(ka_ball, ratingscale, 2000);
207 void havocbot_role_ka_carrier()
209 if (self.deadflag != DEAD_NO)
212 if (time > self.bot_strategytime)
214 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
216 navigation_goalrating_start();
217 havocbot_goalrating_items(10000, self.origin, 10000);
218 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
219 //havocbot_goalrating_waypoints(1, self.origin, 1000);
220 navigation_goalrating_end();
223 if (!self.ballcarried)
225 self.havocbot_role = havocbot_role_ka_collector;
226 self.bot_strategytime = 0;
230 void havocbot_role_ka_collector()
232 if (self.deadflag != DEAD_NO)
235 if (time > self.bot_strategytime)
237 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
239 navigation_goalrating_start();
240 havocbot_goalrating_items(10000, self.origin, 10000);
241 havocbot_goalrating_enemyplayers(1000, self.origin, 10000);
242 havocbot_goalrating_ball(20000, self.origin);
243 navigation_goalrating_end();
246 if (self.ballcarried)
248 self.havocbot_role = havocbot_role_ka_carrier;
249 self.bot_strategytime = 0;
258 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
260 if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
262 if(frag_target.ballcarried) { // add to amount of times killing carrier
263 PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
264 if(autocvar_g_keepaway_score_bckill) // add bckills to the score
265 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_bckill);
267 else if(!frag_attacker.ballcarried)
268 if(autocvar_g_keepaway_noncarrier_warn)
269 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
271 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
272 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
275 if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
279 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
281 frag_score = 0; // no frags counted in keepaway
282 return 1; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
285 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
287 // clear the item used for the ball in keepaway
288 self.items &= ~IT_KEY1;
290 // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
292 self.items |= IT_KEY1;
297 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
299 if(MUTATOR_RETURNVALUE == 0)
308 MUTATOR_HOOKFUNCTION(ka, PlayerDamage_Calculate) // for changing damage and force values that are applied to players in g_damage.qc
310 if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
312 if(frag_target == frag_attacker) // damage done to yourself
314 frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
315 frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
317 else // damage done to noncarriers
319 frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
320 frag_force *= autocvar_g_keepaway_ballcarrier_force;
323 else if (!frag_target.ballcarried) // if the target is a noncarrier
325 if(frag_target == frag_attacker) // damage done to yourself
327 frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
328 frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
330 else // damage done to other noncarriers
332 frag_damage *= autocvar_g_keepaway_noncarrier_damage;
333 frag_force *= autocvar_g_keepaway_noncarrier_force;
339 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
341 if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
345 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
347 if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
351 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
353 // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
354 // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
356 self.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
359 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
364 .float stat_sv_airspeedlimit_nonqw;
365 .float stat_sv_maxspeed;
367 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics)
371 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_keepaway_ballcarrier_highspeed;
372 self.stat_sv_maxspeed *= autocvar_g_keepaway_ballcarrier_highspeed;
377 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
379 // if neither player has ball then don't attack unless the ball is on the ground
380 if(!checkentity.ballcarried && !self.ballcarried && ka_ball.owner)
385 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
387 if (self.ballcarried)
388 self.havocbot_role = havocbot_role_ka_carrier;
390 self.havocbot_role = havocbot_role_ka_collector;
399 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
401 if(!g_keepaway) { return; }
405 e.model = "models/orbs/orbblue.md3";
406 precache_model(e.model);
407 _setmodel(e, e.model);
408 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
409 e.classname = "keepawayball";
410 e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
411 e.takedamage = DAMAGE_YES;
412 e.solid = SOLID_TRIGGER;
413 e.movetype = MOVETYPE_BOUNCE;
414 e.glow_color = autocvar_g_keepawayball_trail_color;
418 e.touch = ka_TouchEvent;
422 InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
427 ScoreRules_basics(0, SFL_SORT_PRIO_PRIMARY, 0, true); // SFL_SORT_PRIO_PRIMARY
428 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_PICKUPS, "pickups", 0);
429 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_CARRIERKILLS, "bckills", 0);
430 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_BCTIME, "bctime", SFL_SORT_PRIO_SECONDARY);
431 ScoreRules_basics_end();
434 void ka_Initialize() // run at the start of a match, initiates game mode
444 REGISTER_MUTATOR(ka, g_keepaway)
448 if(time > 1) // game loads at time 1
449 error("This is a game type and it cannot be added at runtime.");
453 MUTATOR_ONROLLBACK_OR_REMOVE
455 // we actually cannot roll back ka_Initialize here
456 // BUT: we don't need to! If this gets called, adding always
462 LOG_INFO("This is a game type and it cannot be removed at runtime.");