1 #ifndef GAMEMODE_KEEPAWAY_H
2 #define GAMEMODE_KEEPAWAY_H
6 REGISTER_MUTATOR(ka, false)
10 if (time > 1) // game loads at time 1
11 error("This is a game type and it cannot be added at runtime.");
15 MUTATOR_ONROLLBACK_OR_REMOVE
17 // we actually cannot roll back ka_Initialize here
18 // BUT: we don't need to! If this gets called, adding always
24 LOG_INFO("This is a game type and it cannot be removed at runtime.");
34 const float SP_KEEPAWAY_PICKUPS = 4;
35 const float SP_KEEPAWAY_CARRIERKILLS = 5;
36 const float SP_KEEPAWAY_BCTIME = 6;
38 void() havocbot_role_ka_carrier;
39 void() havocbot_role_ka_collector;
41 void ka_DropEvent(entity plyr);
46 int autocvar_g_keepaway_ballcarrier_effects;
47 float autocvar_g_keepaway_ballcarrier_damage;
48 float autocvar_g_keepaway_ballcarrier_force;
49 float autocvar_g_keepaway_ballcarrier_highspeed;
50 float autocvar_g_keepaway_ballcarrier_selfdamage;
51 float autocvar_g_keepaway_ballcarrier_selfforce;
52 float autocvar_g_keepaway_noncarrier_damage;
53 float autocvar_g_keepaway_noncarrier_force;
54 float autocvar_g_keepaway_noncarrier_selfdamage;
55 float autocvar_g_keepaway_noncarrier_selfforce;
56 bool autocvar_g_keepaway_noncarrier_warn;
57 int autocvar_g_keepaway_score_bckill;
58 int autocvar_g_keepaway_score_killac;
59 int autocvar_g_keepaway_score_timepoints;
60 float autocvar_g_keepaway_score_timeinterval;
61 float autocvar_g_keepawayball_damageforcescale;
62 int autocvar_g_keepawayball_effects;
63 float autocvar_g_keepawayball_respawntime;
64 int autocvar_g_keepawayball_trail_color;
66 float ka_ballcarrier_waypointsprite_visible_for_player(entity e) // runs on waypoints which are attached to ballcarriers, updates once per frame
70 return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
72 // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
77 void ka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
79 if(autocvar_sv_eventlog)
80 GameLogEcho(strcat(":ka:", mode, ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
84 void ka_RespawnBall() // runs whenever the ball needs to be relocated
86 if(gameover) { return; }
87 vector oldballorigin = self.origin;
89 if(!MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
91 entity spot = SelectSpawnPoint(true);
92 setorigin(self, spot.origin);
93 self.angles = spot.angles;
96 makevectors(self.angles);
97 self.movetype = MOVETYPE_BOUNCE;
98 self.velocity = '0 0 200';
99 self.angles = '0 0 0';
100 self.effects = autocvar_g_keepawayball_effects;
101 self.touch = ka_TouchEvent;
102 self.think = ka_RespawnBall;
103 self.nextthink = time + autocvar_g_keepawayball_respawntime;
105 Send_Effect(EFFECT_ELECTRO_COMBO, oldballorigin, '0 0 0', 1);
106 Send_Effect(EFFECT_ELECTRO_COMBO, self.origin, '0 0 0', 1);
108 WaypointSprite_Spawn(WP_KaBall, 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
109 WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);
111 sound(self, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
114 void ka_TimeScoring()
116 if(self.owner.ballcarried)
117 { // add points for holding the ball after a certain amount of time
118 if(autocvar_g_keepaway_score_timepoints)
119 PlayerScore_Add(self.owner, SP_SCORE, autocvar_g_keepaway_score_timepoints);
121 PlayerScore_Add(self.owner, SP_KEEPAWAY_BCTIME, (autocvar_g_keepaway_score_timeinterval / 1)); // interval is divided by 1 so that time always shows "seconds"
122 self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
126 void ka_TouchEvent() // runs any time that the ball comes in contact with something
128 if(gameover) { return; }
129 if(!self) { return; }
130 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
131 { // The ball fell off the map, respawn it since players can't get to it
135 if(other.deadflag != DEAD_NO) { return; }
136 if(other.frozen) { return; }
137 if (!IS_PLAYER(other))
138 { // The ball just touched an object, most likely the world
139 Send_Effect(EFFECT_BALL_SPARKS, self.origin, '0 0 0', 1);
140 sound(self, CH_TRIGGER, SND_KA_TOUCH, VOL_BASE, ATTEN_NORM);
143 else if(self.wait > time) { return; }
145 // attach the ball to the player
147 other.ballcarried = self;
148 setattachment(self, other, "");
149 setorigin(self, '0 0 0');
151 // make the ball invisible/unable to do anything/set up time scoring
152 self.velocity = '0 0 0';
153 self.movetype = MOVETYPE_NONE;
154 self.effects |= EF_NODRAW;
155 self.touch = func_null;
156 self.think = ka_TimeScoring;
157 self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
158 self.takedamage = DAMAGE_NO;
160 // apply effects to player
161 other.glow_color = autocvar_g_keepawayball_trail_color;
162 other.glow_trail = true;
163 other.effects |= autocvar_g_keepaway_ballcarrier_effects;
165 // messages and sounds
166 ka_EventLog("pickup", other);
167 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_KEEPAWAY_PICKUP, other.netname);
168 Send_Notification(NOTIF_ALL_EXCEPT, other, MSG_CENTER, CENTER_KEEPAWAY_PICKUP, other.netname);
169 Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_KEEPAWAY_PICKUP_SELF);
170 sound(self.owner, CH_TRIGGER, SND_KA_PICKEDUP, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
173 PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
176 WaypointSprite_AttachCarrier(WP_KaBallCarrier, other, RADARICON_FLAGCARRIER);
177 other.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
178 WaypointSprite_UpdateRule(other.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
179 WaypointSprite_Ping(other.waypointsprite_attachedforcarrier);
180 WaypointSprite_Kill(self.waypointsprite_attachedforcarrier);
183 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
186 ball = plyr.ballcarried;
188 if(!ball) { return; }
191 setattachment(ball, world, "");
192 ball.movetype = MOVETYPE_BOUNCE;
193 ball.wait = time + 1;
194 ball.touch = ka_TouchEvent;
195 ball.think = ka_RespawnBall;
196 ball.nextthink = time + autocvar_g_keepawayball_respawntime;
197 ball.takedamage = DAMAGE_YES;
198 ball.effects &= ~EF_NODRAW;
199 setorigin(ball, plyr.origin + '0 0 10');
200 ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
201 ball.owner.ballcarried = world; // I hope nothing checks to see if the world has the ball in the rest of my code :P
204 // reset the player effects
205 plyr.glow_trail = false;
206 plyr.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
208 // messages and sounds
209 ka_EventLog("dropped", plyr);
210 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_KEEPAWAY_DROPPED, plyr.netname);
211 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_KEEPAWAY_DROPPED, plyr.netname);
212 sound(other, CH_TRIGGER, SND_KA_DROPPED, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
215 // PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1); Not anymore, this is 100% the same as pickups and is useless.
218 WaypointSprite_Spawn(WP_KaBall, 0, 0, ball, '0 0 64', world, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
219 WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
220 WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
221 WaypointSprite_Kill(plyr.waypointsprite_attachedforcarrier);
224 void ka_Reset() // used to clear the ballcarrier whenever the match switches from warmup to normal
226 if((self.owner) && (IS_PLAYER(self.owner)))
227 ka_DropEvent(self.owner);
229 if(time < game_starttime)
231 self.think = ka_RespawnBall;
232 self.touch = func_null;
233 self.nextthink = game_starttime;
244 void havocbot_goalrating_ball(float ratingscale, vector org)
248 ball_owner = ka_ball.owner;
250 if (ball_owner == self)
253 // If ball is carried by player then hunt them down.
256 t = (self.health + self.armorvalue) / (ball_owner.health + ball_owner.armorvalue);
257 navigation_routerating(ball_owner, t * ratingscale, 2000);
259 else // Ball has been dropped so collect.
260 navigation_routerating(ka_ball, ratingscale, 2000);
263 void havocbot_role_ka_carrier()
265 if (self.deadflag != DEAD_NO)
268 if (time > self.bot_strategytime)
270 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
272 navigation_goalrating_start();
273 havocbot_goalrating_items(10000, self.origin, 10000);
274 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
275 //havocbot_goalrating_waypoints(1, self.origin, 1000);
276 navigation_goalrating_end();
279 if (!self.ballcarried)
281 self.havocbot_role = havocbot_role_ka_collector;
282 self.bot_strategytime = 0;
286 void havocbot_role_ka_collector()
288 if (self.deadflag != DEAD_NO)
291 if (time > self.bot_strategytime)
293 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
295 navigation_goalrating_start();
296 havocbot_goalrating_items(10000, self.origin, 10000);
297 havocbot_goalrating_enemyplayers(1000, self.origin, 10000);
298 havocbot_goalrating_ball(20000, self.origin);
299 navigation_goalrating_end();
302 if (self.ballcarried)
304 self.havocbot_role = havocbot_role_ka_carrier;
305 self.bot_strategytime = 0;
314 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
316 if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
318 if(frag_target.ballcarried) { // add to amount of times killing carrier
319 PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
320 if(autocvar_g_keepaway_score_bckill) // add bckills to the score
321 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_bckill);
323 else if(!frag_attacker.ballcarried)
324 if(autocvar_g_keepaway_noncarrier_warn)
325 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
327 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
328 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
331 if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
335 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
337 frag_score = 0; // no frags counted in keepaway
338 return 1; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
341 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
343 // clear the item used for the ball in keepaway
344 self.items &= ~IT_KEY1;
346 // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
348 self.items |= IT_KEY1;
353 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
355 if(MUTATOR_RETURNVALUE == 0)
364 MUTATOR_HOOKFUNCTION(ka, PlayerDamage_Calculate) // for changing damage and force values that are applied to players in g_damage.qc
366 if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
368 if(frag_target == frag_attacker) // damage done to yourself
370 frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
371 frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
373 else // damage done to noncarriers
375 frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
376 frag_force *= autocvar_g_keepaway_ballcarrier_force;
379 else if (!frag_target.ballcarried) // if the target is a noncarrier
381 if(frag_target == frag_attacker) // damage done to yourself
383 frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
384 frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
386 else // damage done to other noncarriers
388 frag_damage *= autocvar_g_keepaway_noncarrier_damage;
389 frag_force *= autocvar_g_keepaway_noncarrier_force;
395 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
397 if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
401 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
403 if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
407 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
409 // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
410 // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
412 self.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
415 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
420 .float stat_sv_airspeedlimit_nonqw;
421 .float stat_sv_maxspeed;
423 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics)
427 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_keepaway_ballcarrier_highspeed;
428 self.stat_sv_maxspeed *= autocvar_g_keepaway_ballcarrier_highspeed;
433 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
435 // if neither player has ball then don't attack unless the ball is on the ground
436 if(!checkentity.ballcarried && !self.ballcarried && ka_ball.owner)
441 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
443 if (self.ballcarried)
444 self.havocbot_role = havocbot_role_ka_carrier;
446 self.havocbot_role = havocbot_role_ka_collector;
450 MUTATOR_HOOKFUNCTION(ka, DropSpecialItems)
452 if(frag_target.ballcarried)
453 ka_DropEvent(frag_target);
463 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
465 entity e = new(keepawayball);
466 e.model = "models/orbs/orbblue.md3";
467 precache_model(e.model);
468 _setmodel(e, e.model);
469 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
470 e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
471 e.takedamage = DAMAGE_YES;
472 e.solid = SOLID_TRIGGER;
473 e.movetype = MOVETYPE_BOUNCE;
474 e.glow_color = autocvar_g_keepawayball_trail_color;
478 e.touch = ka_TouchEvent;
482 InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
487 ScoreRules_basics(0, SFL_SORT_PRIO_PRIMARY, 0, true); // SFL_SORT_PRIO_PRIMARY
488 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_PICKUPS, "pickups", 0);
489 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_CARRIERKILLS, "bckills", 0);
490 ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_BCTIME, "bctime", SFL_SORT_PRIO_SECONDARY);
491 ScoreRules_basics_end();
494 void ka_Initialize() // run at the start of a match, initiates game mode