]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_keepaway.qc
1a91658a66f64ba47a269ce14115383df8e9f8b7
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_keepaway.qc
1 #include "gamemode_keepaway.qh"
2 #include "../_all.qh"
3
4 #include "gamemode.qh"
5
6 // ===========================================================
7 //  Keepaway game mode coding, written by Samual and Diabolik
8 //  Last updated: September, 2012
9 // ===========================================================
10
11 float ka_ballcarrier_waypointsprite_visible_for_player(entity e) // runs on waypoints which are attached to ballcarriers, updates once per frame
12 {
13         if(e.ballcarried)
14                 if(IS_SPEC(other))
15                         return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
16
17         // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
18
19         return true;
20 }
21
22 void ka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
23 {
24         if(autocvar_sv_eventlog)
25                 GameLogEcho(strcat(":ka:", mode, ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
26 }
27
28 void ka_RespawnBall() // runs whenever the ball needs to be relocated
29 {
30         if(gameover) { return; }
31         vector oldballorigin = self.origin;
32
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))
34         {
35                 entity spot = SelectSpawnPoint(true);
36                 setorigin(self, spot.origin);
37                 self.angles = spot.angles;
38         }
39
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;
47
48         pointparticles(particleeffectnum("electro_combo"), oldballorigin, '0 0 0', 1);
49         pointparticles(particleeffectnum("electro_combo"), self.origin, '0 0 0', 1);
50
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);
53
54         sound(self, CH_TRIGGER, "keepaway/respawn.wav", VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
55 }
56
57 void ka_TimeScoring()
58 {
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);
63
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;
66         }
67 }
68
69 void ka_TouchEvent() // runs any time that the ball comes in contact with something
70 {
71         if(gameover) { return; }
72         if(!self) { return; }
73         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
74         { // The ball fell off the map, respawn it since players can't get to it
75                 ka_RespawnBall();
76                 return;
77         }
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);
84                 return;
85         }
86         else if(self.wait > time) { return; }
87
88         // attach the ball to the player
89         self.owner = other;
90         other.ballcarried = self;
91         setattachment(self, other, "");
92         setorigin(self, '0 0 0');
93
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;
102
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;
107
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)
114
115         // scoring
116         PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
117
118         // waypoints
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);
124 }
125
126 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
127 {
128         entity ball;
129         ball = plyr.ballcarried;
130
131         if(!ball) { return; }
132
133         // reset the ball
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
145         ball.owner = world;
146
147         // reset the player effects
148         plyr.glow_trail = false;
149         plyr.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
150
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)
156
157         // scoring
158         // PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1); Not anymore, this is 100% the same as pickups and is useless.
159
160         // waypoints
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);
165 }
166
167 void ka_Reset() // used to clear the ballcarrier whenever the match switches from warmup to normal
168 {
169         if((self.owner) && (IS_PLAYER(self.owner)))
170                 ka_DropEvent(self.owner);
171
172         ka_RespawnBall();
173 }
174
175
176 // ================
177 // Bot player logic
178 // ================
179
180 void havocbot_goalrating_ball(float ratingscale, vector org)
181 {
182         float t;
183         entity ball_owner;
184         ball_owner = ka_ball.owner;
185
186         if (ball_owner == self)
187                 return;
188
189         // If ball is carried by player then hunt them down.
190         if (ball_owner)
191         {
192                 t = (self.health + self.armorvalue) / (ball_owner.health + ball_owner.armorvalue);
193                 navigation_routerating(ball_owner, t * ratingscale, 2000);
194         }
195         else // Ball has been dropped so collect.
196                 navigation_routerating(ka_ball, ratingscale, 2000);
197 }
198
199 void havocbot_role_ka_carrier()
200 {
201         if (self.deadflag != DEAD_NO)
202                 return;
203
204         if (time > self.bot_strategytime)
205         {
206                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
207
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();
213         }
214
215         if (!self.ballcarried)
216         {
217                 self.havocbot_role = havocbot_role_ka_collector;
218                 self.bot_strategytime = 0;
219         }
220 }
221
222 void havocbot_role_ka_collector()
223 {
224         if (self.deadflag != DEAD_NO)
225                 return;
226
227         if (time > self.bot_strategytime)
228         {
229                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
230
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();
236         }
237
238         if (self.ballcarried)
239         {
240                 self.havocbot_role = havocbot_role_ka_carrier;
241                 self.bot_strategytime = 0;
242         }
243 }
244
245
246 // ==============
247 // Hook Functions
248 // ==============
249
250 MUTATOR_HOOKFUNCTION(ka_Scoring)
251 {
252         if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
253         {
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);
258                 }
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);
262
263                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
264                         PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
265         }
266
267         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
268         return 0;
269 }
270
271 MUTATOR_HOOKFUNCTION(ka_GiveFragsForKill)
272 {
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.
275 }
276
277 MUTATOR_HOOKFUNCTION(ka_PlayerPreThink)
278 {
279         // clear the item used for the ball in keepaway
280         self.items &= ~IT_KEY1;
281
282         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
283         if(self.ballcarried)
284                 self.items |= IT_KEY1;
285
286         return 0;
287 }
288
289 MUTATOR_HOOKFUNCTION(ka_PlayerUseKey)
290 {
291         if(MUTATOR_RETURNVALUE == 0)
292         if(self.ballcarried)
293         {
294                 ka_DropEvent(self);
295                 return 1;
296         }
297         return 0;
298 }
299
300 MUTATOR_HOOKFUNCTION(ka_PlayerDamage) // for changing damage and force values that are applied to players in g_damage.qc
301 {
302         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
303         {
304                 if(frag_target == frag_attacker) // damage done to yourself
305                 {
306                         frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
307                         frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
308                 }
309                 else // damage done to noncarriers
310                 {
311                         frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
312                         frag_force *= autocvar_g_keepaway_ballcarrier_force;
313                 }
314         }
315         else if (!frag_target.ballcarried) // if the target is a noncarrier
316         {
317                 if(frag_target == frag_attacker) // damage done to yourself
318                 {
319                         frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
320                         frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
321                 }
322                 else // damage done to other noncarriers
323                 {
324                         frag_damage *= autocvar_g_keepaway_noncarrier_damage;
325                         frag_force *= autocvar_g_keepaway_noncarrier_force;
326                 }
327         }
328         return 0;
329 }
330
331 MUTATOR_HOOKFUNCTION(ka_RemovePlayer)
332 {
333         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
334         return 0;
335 }
336
337 MUTATOR_HOOKFUNCTION(ka_PlayerPowerups)
338 {
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()
341
342         self.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
343
344         if(self.ballcarried)
345                 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
346
347         return 0;
348 }
349
350 MUTATOR_HOOKFUNCTION(ka_BotRoles)
351 {
352         if (self.ballcarried)
353                 self.havocbot_role = havocbot_role_ka_carrier;
354         else
355                 self.havocbot_role = havocbot_role_ka_collector;
356         return true;
357 }
358
359
360 // ==============
361 // Initialization
362 // ==============
363
364 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
365 {
366         if(!g_keepaway) { return; }
367
368         entity e;
369         e = spawn();
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;
380         e.glow_trail = true;
381         e.flags = FL_ITEM;
382         e.reset = ka_Reset;
383         e.touch = ka_TouchEvent;
384         e.owner = world;
385         ka_ball = e;
386
387         InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
388 }
389
390 void ka_ScoreRules()
391 {
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();
397 }
398
399 void ka_Initialize() // run at the start of a match, initiates game mode
400 {
401         if(!g_keepaway)
402                 return;
403
404         precache_sound("keepaway/pickedup.wav");
405         precache_sound("keepaway/dropped.wav");
406         precache_sound("keepaway/respawn.wav");
407         precache_sound("keepaway/touch.wav");
408
409         ka_ScoreRules();
410         ka_SpawnBall();
411 }
412
413
414 MUTATOR_DEFINITION(gamemode_keepaway)
415 {
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);
425
426         MUTATOR_ONADD
427         {
428                 if(time > 1) // game loads at time 1
429                         error("This is a game type and it cannot be added at runtime.");
430                 ka_Initialize();
431         }
432
433         MUTATOR_ONROLLBACK_OR_REMOVE
434         {
435                 // we actually cannot roll back ka_Initialize here
436                 // BUT: we don't need to! If this gets called, adding always
437                 // succeeds.
438         }
439
440         MUTATOR_ONREMOVE
441         {
442                 print("This is a game type and it cannot be removed at runtime.");
443                 return -1;
444         }
445
446         return 0;
447 }