]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_keepaway.qc
Merge remote-tracking branch 'origin/master' into terencehill/menu_remove_tab_title
[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
196         // Ball has been dropped so collect.
197         navigation_routerating(ka_ball, ratingscale, 2000);
198 }
199
200 void havocbot_role_ka_carrier()
201 {
202         if (self.deadflag != DEAD_NO)
203                 return;
204
205         if (time > self.bot_strategytime)
206         {
207                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
208
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();
214         }
215
216         if (!self.ballcarried)
217         {
218                 self.havocbot_role = havocbot_role_ka_collector;
219                 self.bot_strategytime = 0;
220         }
221 }
222
223 void havocbot_role_ka_collector()
224 {
225         if (self.deadflag != DEAD_NO)
226                 return;
227
228         if (time > self.bot_strategytime)
229         {
230                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
231
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();
237         }
238
239         if (self.ballcarried)
240         {
241                 self.havocbot_role = havocbot_role_ka_carrier;
242                 self.bot_strategytime = 0;
243         }
244 }
245
246
247 // ==============
248 // Hook Functions
249 // ==============
250
251 MUTATOR_HOOKFUNCTION(ka_Scoring)
252 {
253         if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
254         {
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);
259                 }
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);
263
264                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
265                         PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
266         }
267
268         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
269         return 0;
270 }
271
272 MUTATOR_HOOKFUNCTION(ka_GiveFragsForKill)
273 {
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.
276 }
277
278 MUTATOR_HOOKFUNCTION(ka_PlayerPreThink)
279 {
280         // clear the item used for the ball in keepaway
281         self.items &= ~IT_KEY1;
282
283         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
284         if(self.ballcarried)
285                 self.items |= IT_KEY1;
286
287         return 0;
288 }
289
290 MUTATOR_HOOKFUNCTION(ka_PlayerUseKey)
291 {
292         if(MUTATOR_RETURNVALUE == 0)
293         if(self.ballcarried)
294         {
295                 ka_DropEvent(self);
296                 return 1;
297         }
298         return 0;
299 }
300
301 MUTATOR_HOOKFUNCTION(ka_PlayerDamage) // for changing damage and force values that are applied to players in g_damage.qc
302 {
303         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
304         {
305                 if(frag_target == frag_attacker) // damage done to yourself
306                 {
307                         frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
308                         frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
309                 }
310                 else // damage done to noncarriers
311                 {
312                         frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
313                         frag_force *= autocvar_g_keepaway_ballcarrier_force;
314                 }
315         }
316         else if (!frag_target.ballcarried) // if the target is a noncarrier
317         {
318                 if(frag_target == frag_attacker) // damage done to yourself
319                 {
320                         frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
321                         frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
322                 }
323                 else // damage done to other noncarriers
324                 {
325                         frag_damage *= autocvar_g_keepaway_noncarrier_damage;
326                         frag_force *= autocvar_g_keepaway_noncarrier_force;
327                 }
328         }
329         return 0;
330 }
331
332 MUTATOR_HOOKFUNCTION(ka_RemovePlayer)
333 {
334         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
335         return 0;
336 }
337
338 MUTATOR_HOOKFUNCTION(ka_PlayerPowerups)
339 {
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()
342
343         self.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
344
345         if(self.ballcarried)
346                 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
347
348         return 0;
349 }
350
351 MUTATOR_HOOKFUNCTION(ka_BotRoles)
352 {
353         if (self.ballcarried)
354                 self.havocbot_role = havocbot_role_ka_carrier;
355         else
356                 self.havocbot_role = havocbot_role_ka_collector;
357         return true;
358 }
359
360
361 // ==============
362 // Initialization
363 // ==============
364
365 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
366 {
367         if(!g_keepaway) { return; }
368
369         entity e;
370         e = spawn();
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;
381         e.glow_trail = true;
382         e.flags = FL_ITEM;
383         e.reset = ka_Reset;
384         e.touch = ka_TouchEvent;
385         e.owner = world;
386         ka_ball = e;
387
388         InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
389 }
390
391 void ka_ScoreRules()
392 {
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();
398 }
399
400 void ka_Initialize() // run at the start of a match, initiates game mode
401 {
402         if(!g_keepaway)
403                 return;
404
405         precache_sound("keepaway/pickedup.wav");
406         precache_sound("keepaway/dropped.wav");
407         precache_sound("keepaway/respawn.wav");
408         precache_sound("keepaway/touch.wav");
409
410         ka_ScoreRules();
411         ka_SpawnBall();
412 }
413
414
415 MUTATOR_DEFINITION(gamemode_keepaway)
416 {
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);
426
427         MUTATOR_ONADD
428         {
429                 if(time > 1) // game loads at time 1
430                         error("This is a game type and it cannot be added at runtime.");
431                 ka_Initialize();
432         }
433
434         MUTATOR_ONROLLBACK_OR_REMOVE
435         {
436                 // we actually cannot roll back ka_Initialize here
437                 // BUT: we don't need to! If this gets called, adding always
438                 // succeeds.
439         }
440
441         MUTATOR_ONREMOVE
442         {
443                 print("This is a game type and it cannot be removed at runtime.");
444                 return -1;
445         }
446
447         return 0;
448 }