1 #include "gamemode_keepaway.qh"
6 // ===========================================================
7 // Keepaway game mode coding, written by Samual and Diabolik
8 // Last updated: September, 2012
9 // ===========================================================
11 float ka_ballcarrier_waypointsprite_visible_for_player(entity e) // runs on waypoints which are attached to ballcarriers, updates once per frame
15 return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
17 // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
22 void ka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
24 if(autocvar_sv_eventlog)
25 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.think = ka_RespawnBall;
46 self.nextthink = time + autocvar_g_keepawayball_respawntime;
48 pointparticles(particleeffectnum("electro_combo"), oldballorigin, '0 0 0', 1);
49 pointparticles(particleeffectnum("electro_combo"), self.origin, '0 0 0', 1);
51 WaypointSprite_Spawn("ka-ball", 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER, '0 1 1');
52 WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);
54 sound(self, CH_TRIGGER, "keepaway/respawn.wav", VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
59 if(self.owner.ballcarried)
60 { // add points for holding the ball after a certain amount of time
61 if(autocvar_g_keepaway_score_timepoints)
62 PlayerScore_Add(self.owner, SP_SCORE, autocvar_g_keepaway_score_timepoints);
64 PlayerScore_Add(self.owner, SP_KEEPAWAY_BCTIME, (autocvar_g_keepaway_score_timeinterval / 1)); // interval is divided by 1 so that time always shows "seconds"
65 self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
69 void ka_TouchEvent() // runs any time that the ball comes in contact with something
71 if(gameover) { return; }
73 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
74 { // The ball fell off the map, respawn it since players can't get to it
78 if(other.deadflag != DEAD_NO) { return; }
79 if(other.frozen) { return; }
80 if (!IS_PLAYER(other))
81 { // The ball just touched an object, most likely the world
82 pointparticles(particleeffectnum("kaball_sparks"), self.origin, '0 0 0', 1);
83 sound(self, CH_TRIGGER, "keepaway/touch.wav", VOL_BASE, ATTEN_NORM);
86 else if(self.wait > time) { return; }
88 // attach the ball to the player
90 other.ballcarried = self;
91 setattachment(self, other, "");
92 setorigin(self, '0 0 0');
94 // make the ball invisible/unable to do anything/set up time scoring
95 self.velocity = '0 0 0';
96 self.movetype = MOVETYPE_NONE;
97 self.effects |= EF_NODRAW;
98 self.touch = func_null;
99 self.think = ka_TimeScoring;
100 self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
101 self.takedamage = DAMAGE_NO;
103 // apply effects to player
104 other.glow_color = autocvar_g_keepawayball_trail_color;
105 other.glow_trail = true;
106 other.effects |= autocvar_g_keepaway_ballcarrier_effects;
108 // messages and sounds
109 ka_EventLog("pickup", other);
110 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_KEEPAWAY_PICKUP, other.netname);
111 Send_Notification(NOTIF_ALL_EXCEPT, other, MSG_CENTER, CENTER_KEEPAWAY_PICKUP, other.netname);
112 Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_KEEPAWAY_PICKUP_SELF);
113 sound(self.owner, CH_TRIGGER, "keepaway/pickedup.wav", VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
116 PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
119 WaypointSprite_AttachCarrier("ka-ballcarrier", other, RADARICON_FLAGCARRIER, '1 0 0');
120 other.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
121 WaypointSprite_UpdateRule(other.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
122 WaypointSprite_Ping(other.waypointsprite_attachedforcarrier);
123 WaypointSprite_Kill(self.waypointsprite_attachedforcarrier);
126 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
129 ball = plyr.ballcarried;
131 if(!ball) { return; }
134 setattachment(ball, world, "");
135 ball.movetype = MOVETYPE_BOUNCE;
136 ball.wait = time + 1;
137 ball.touch = ka_TouchEvent;
138 ball.think = ka_RespawnBall;
139 ball.nextthink = time + autocvar_g_keepawayball_respawntime;
140 ball.takedamage = DAMAGE_YES;
141 ball.effects &= ~EF_NODRAW;
142 setorigin(ball, plyr.origin + '0 0 10');
143 ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
144 ball.owner.ballcarried = world; // I hope nothing checks to see if the world has the ball in the rest of my code :P
147 // reset the player effects
148 plyr.glow_trail = false;
149 plyr.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
151 // messages and sounds
152 ka_EventLog("dropped", plyr);
153 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_KEEPAWAY_DROPPED, plyr.netname);
154 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_KEEPAWAY_DROPPED, plyr.netname);
155 sound(other, CH_TRIGGER, "keepaway/dropped.wav", VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
158 // PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1); Not anymore, this is 100% the same as pickups and is useless.
161 WaypointSprite_Spawn("ka-ball", 0, 0, ball, '0 0 64', world, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER, '0 1 1');
162 WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
163 WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
164 WaypointSprite_Kill(plyr.waypointsprite_attachedforcarrier);
167 void ka_Reset() // used to clear the ballcarrier whenever the match switches from warmup to normal
169 if((self.owner) && (IS_PLAYER(self.owner)))
170 ka_DropEvent(self.owner);
180 void havocbot_goalrating_ball(float ratingscale, vector org)
184 ball_owner = ka_ball.owner;
186 if (ball_owner == self)
189 // If ball is carried by player then hunt them down.
192 t = (self.health + self.armorvalue) / (ball_owner.health + ball_owner.armorvalue);
193 navigation_routerating(ball_owner, t * ratingscale, 2000);
196 // Ball has been dropped so collect.
197 navigation_routerating(ka_ball, ratingscale, 2000);
200 void havocbot_role_ka_carrier()
202 if (self.deadflag != DEAD_NO)
205 if (time > self.bot_strategytime)
207 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
209 navigation_goalrating_start();
210 havocbot_goalrating_items(10000, self.origin, 10000);
211 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
212 //havocbot_goalrating_waypoints(1, self.origin, 1000);
213 navigation_goalrating_end();
216 if (!self.ballcarried)
218 self.havocbot_role = havocbot_role_ka_collector;
219 self.bot_strategytime = 0;
223 void havocbot_role_ka_collector()
225 if (self.deadflag != DEAD_NO)
228 if (time > self.bot_strategytime)
230 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
232 navigation_goalrating_start();
233 havocbot_goalrating_items(10000, self.origin, 10000);
234 havocbot_goalrating_enemyplayers(1000, self.origin, 10000);
235 havocbot_goalrating_ball(20000, self.origin);
236 navigation_goalrating_end();
239 if (self.ballcarried)
241 self.havocbot_role = havocbot_role_ka_carrier;
242 self.bot_strategytime = 0;
251 MUTATOR_HOOKFUNCTION(ka_Scoring)
253 if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
255 if(frag_target.ballcarried) { // add to amount of times killing carrier
256 PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
257 if(autocvar_g_keepaway_score_bckill) // add bckills to the score
258 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_bckill);
260 else if(!frag_attacker.ballcarried)
261 if(autocvar_g_keepaway_noncarrier_warn)
262 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
264 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
265 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
268 if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
272 MUTATOR_HOOKFUNCTION(ka_GiveFragsForKill)
274 frag_score = 0; // no frags counted in keepaway
275 return 1; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
278 MUTATOR_HOOKFUNCTION(ka_PlayerPreThink)
280 // clear the item used for the ball in keepaway
281 self.items &= ~IT_KEY1;
283 // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
285 self.items |= IT_KEY1;
290 MUTATOR_HOOKFUNCTION(ka_PlayerUseKey)
292 if(MUTATOR_RETURNVALUE == 0)
301 MUTATOR_HOOKFUNCTION(ka_PlayerDamage) // for changing damage and force values that are applied to players in g_damage.qc
303 if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
305 if(frag_target == frag_attacker) // damage done to yourself
307 frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
308 frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
310 else // damage done to noncarriers
312 frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
313 frag_force *= autocvar_g_keepaway_ballcarrier_force;
316 else if (!frag_target.ballcarried) // if the target is a noncarrier
318 if(frag_target == frag_attacker) // damage done to yourself
320 frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
321 frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
323 else // damage done to other noncarriers
325 frag_damage *= autocvar_g_keepaway_noncarrier_damage;
326 frag_force *= autocvar_g_keepaway_noncarrier_force;
332 MUTATOR_HOOKFUNCTION(ka_RemovePlayer)
334 if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
338 MUTATOR_HOOKFUNCTION(ka_PlayerPowerups)
340 // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
341 // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
343 self.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
346 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
351 MUTATOR_HOOKFUNCTION(ka_BotRoles)
353 if (self.ballcarried)
354 self.havocbot_role = havocbot_role_ka_carrier;
356 self.havocbot_role = havocbot_role_ka_collector;
365 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
367 if(!g_keepaway) { return; }
371 e.model = "models/orbs/orbblue.md3";
372 precache_model(e.model);
373 setmodel(e, e.model);
374 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
375 e.classname = "keepawayball";
376 e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
377 e.takedamage = DAMAGE_YES;
378 e.solid = SOLID_TRIGGER;
379 e.movetype = MOVETYPE_BOUNCE;
380 e.glow_color = autocvar_g_keepawayball_trail_color;
384 e.touch = ka_TouchEvent;
388 InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
393 ScoreRules_basics(0, SFL_SORT_PRIO_PRIMARY, 0, true); // SFL_SORT_PRIO_PRIMARY
394 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_PICKUPS, "pickups", 0);
395 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_CARRIERKILLS, "bckills", 0);
396 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_BCTIME, "bctime", SFL_SORT_PRIO_SECONDARY);
397 ScoreRules_basics_end();
400 void ka_Initialize() // run at the start of a match, initiates game mode
405 precache_sound("keepaway/pickedup.wav");
406 precache_sound("keepaway/dropped.wav");
407 precache_sound("keepaway/respawn.wav");
408 precache_sound("keepaway/touch.wav");
415 MUTATOR_DEFINITION(gamemode_keepaway)
417 MUTATOR_HOOK(MakePlayerObserver, ka_RemovePlayer, CBC_ORDER_ANY);
418 MUTATOR_HOOK(ClientDisconnect, ka_RemovePlayer, CBC_ORDER_ANY);
419 MUTATOR_HOOK(PlayerDies, ka_Scoring, CBC_ORDER_ANY);
420 MUTATOR_HOOK(GiveFragsForKill, ka_GiveFragsForKill, CBC_ORDER_ANY);
421 MUTATOR_HOOK(PlayerPreThink, ka_PlayerPreThink, CBC_ORDER_ANY);
422 MUTATOR_HOOK(PlayerDamage_Calculate, ka_PlayerDamage, CBC_ORDER_ANY);
423 MUTATOR_HOOK(PlayerPowerups, ka_PlayerPowerups, CBC_ORDER_ANY);
424 MUTATOR_HOOK(PlayerUseKey, ka_PlayerUseKey, CBC_ORDER_ANY);
425 MUTATOR_HOOK(HavocBot_ChooseRole, ka_BotRoles, CBC_ORDER_ANY);
429 if(time > 1) // game loads at time 1
430 error("This is a game type and it cannot be added at runtime.");
434 MUTATOR_ONROLLBACK_OR_REMOVE
436 // we actually cannot roll back ka_Initialize here
437 // BUT: we don't need to! If this gets called, adding always
443 print("This is a game type and it cannot be removed at runtime.");