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);
195 else // Ball has been dropped so collect.
196 navigation_routerating(ka_ball, ratingscale, 2000);
199 void havocbot_role_ka_carrier()
201 if (self.deadflag != DEAD_NO)
204 if (time > self.bot_strategytime)
206 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
208 navigation_goalrating_start();
209 havocbot_goalrating_items(10000, self.origin, 10000);
210 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
211 //havocbot_goalrating_waypoints(1, self.origin, 1000);
212 navigation_goalrating_end();
215 if (!self.ballcarried)
217 self.havocbot_role = havocbot_role_ka_collector;
218 self.bot_strategytime = 0;
222 void havocbot_role_ka_collector()
224 if (self.deadflag != DEAD_NO)
227 if (time > self.bot_strategytime)
229 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
231 navigation_goalrating_start();
232 havocbot_goalrating_items(10000, self.origin, 10000);
233 havocbot_goalrating_enemyplayers(1000, self.origin, 10000);
234 havocbot_goalrating_ball(20000, self.origin);
235 navigation_goalrating_end();
238 if (self.ballcarried)
240 self.havocbot_role = havocbot_role_ka_carrier;
241 self.bot_strategytime = 0;
250 MUTATOR_HOOKFUNCTION(ka_Scoring)
252 if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
254 if(frag_target.ballcarried) { // add to amount of times killing carrier
255 PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
256 if(autocvar_g_keepaway_score_bckill) // add bckills to the score
257 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_bckill);
259 else if(!frag_attacker.ballcarried)
260 if(autocvar_g_keepaway_noncarrier_warn)
261 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
263 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
264 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
267 if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
271 MUTATOR_HOOKFUNCTION(ka_GiveFragsForKill)
273 frag_score = 0; // no frags counted in keepaway
274 return 1; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
277 MUTATOR_HOOKFUNCTION(ka_PlayerPreThink)
279 // clear the item used for the ball in keepaway
280 self.items &= ~IT_KEY1;
282 // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
284 self.items |= IT_KEY1;
289 MUTATOR_HOOKFUNCTION(ka_PlayerUseKey)
291 if(MUTATOR_RETURNVALUE == 0)
300 MUTATOR_HOOKFUNCTION(ka_PlayerDamage) // for changing damage and force values that are applied to players in g_damage.qc
302 if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
304 if(frag_target == frag_attacker) // damage done to yourself
306 frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
307 frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
309 else // damage done to noncarriers
311 frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
312 frag_force *= autocvar_g_keepaway_ballcarrier_force;
315 else if (!frag_target.ballcarried) // if the target is a noncarrier
317 if(frag_target == frag_attacker) // damage done to yourself
319 frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
320 frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
322 else // damage done to other noncarriers
324 frag_damage *= autocvar_g_keepaway_noncarrier_damage;
325 frag_force *= autocvar_g_keepaway_noncarrier_force;
331 MUTATOR_HOOKFUNCTION(ka_RemovePlayer)
333 if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
337 MUTATOR_HOOKFUNCTION(ka_PlayerPowerups)
339 // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
340 // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
342 self.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
345 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
350 MUTATOR_HOOKFUNCTION(ka_BotRoles)
352 if (self.ballcarried)
353 self.havocbot_role = havocbot_role_ka_carrier;
355 self.havocbot_role = havocbot_role_ka_collector;
364 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
366 if(!g_keepaway) { return; }
370 e.model = "models/orbs/orbblue.md3";
371 precache_model(e.model);
372 setmodel(e, e.model);
373 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
374 e.classname = "keepawayball";
375 e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
376 e.takedamage = DAMAGE_YES;
377 e.solid = SOLID_TRIGGER;
378 e.movetype = MOVETYPE_BOUNCE;
379 e.glow_color = autocvar_g_keepawayball_trail_color;
383 e.touch = ka_TouchEvent;
387 InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
392 ScoreRules_basics(0, SFL_SORT_PRIO_PRIMARY, 0, true); // SFL_SORT_PRIO_PRIMARY
393 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_PICKUPS, "pickups", 0);
394 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_CARRIERKILLS, "bckills", 0);
395 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_BCTIME, "bctime", SFL_SORT_PRIO_SECONDARY);
396 ScoreRules_basics_end();
399 void ka_Initialize() // run at the start of a match, initiates game mode
404 precache_sound("keepaway/pickedup.wav");
405 precache_sound("keepaway/dropped.wav");
406 precache_sound("keepaway/respawn.wav");
407 precache_sound("keepaway/touch.wav");
414 MUTATOR_DEFINITION(gamemode_keepaway)
416 MUTATOR_HOOK(MakePlayerObserver, ka_RemovePlayer, CBC_ORDER_ANY);
417 MUTATOR_HOOK(ClientDisconnect, ka_RemovePlayer, CBC_ORDER_ANY);
418 MUTATOR_HOOK(PlayerDies, ka_Scoring, CBC_ORDER_ANY);
419 MUTATOR_HOOK(GiveFragsForKill, ka_GiveFragsForKill, CBC_ORDER_ANY);
420 MUTATOR_HOOK(PlayerPreThink, ka_PlayerPreThink, CBC_ORDER_ANY);
421 MUTATOR_HOOK(PlayerDamage_Calculate, ka_PlayerDamage, CBC_ORDER_ANY);
422 MUTATOR_HOOK(PlayerPowerups, ka_PlayerPowerups, CBC_ORDER_ANY);
423 MUTATOR_HOOK(PlayerUseKey, ka_PlayerUseKey, CBC_ORDER_ANY);
424 MUTATOR_HOOK(HavocBot_ChooseRole, ka_BotRoles, CBC_ORDER_ANY);
428 if(time > 1) // game loads at time 1
429 error("This is a game type and it cannot be added at runtime.");
433 MUTATOR_ONROLLBACK_OR_REMOVE
435 // we actually cannot roll back ka_Initialize here
436 // BUT: we don't need to! If this gets called, adding always
442 print("This is a game type and it cannot be removed at runtime.");