]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_keepaway.qc
Merge branch 'master' into morphed/hagar
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_keepaway.qc
1 #ifndef GAMEMODE_KEEPAWAY_H
2 #define GAMEMODE_KEEPAWAY_H
3
4 void ka_Initialize();
5
6 REGISTER_MUTATOR(ka, false)
7 {
8         MUTATOR_ONADD
9         {
10                 if (time > 1) // game loads at time 1
11                         error("This is a game type and it cannot be added at runtime.");
12                 ka_Initialize();
13         }
14
15         MUTATOR_ONROLLBACK_OR_REMOVE
16         {
17                 // we actually cannot roll back ka_Initialize here
18                 // BUT: we don't need to! If this gets called, adding always
19                 // succeeds.
20         }
21
22         MUTATOR_ONREMOVE
23         {
24                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
25                 return -1;
26         }
27
28         return 0;
29 }
30
31
32 entity ka_ball;
33
34 const float SP_KEEPAWAY_PICKUPS = 4;
35 const float SP_KEEPAWAY_CARRIERKILLS = 5;
36 const float SP_KEEPAWAY_BCTIME = 6;
37
38 void() havocbot_role_ka_carrier;
39 void() havocbot_role_ka_collector;
40
41 void ka_DropEvent(entity plyr);
42 #endif
43
44 #ifdef IMPLEMENTATION
45
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;
65
66 float ka_ballcarrier_waypointsprite_visible_for_player(entity e) // runs on waypoints which are attached to ballcarriers, updates once per frame
67 {
68         if(e.ballcarried)
69                 if(IS_SPEC(other))
70                         return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
71
72         // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
73
74         return true;
75 }
76
77 void ka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
78 {
79         if(autocvar_sv_eventlog)
80                 GameLogEcho(strcat(":ka:", mode, ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
81 }
82
83 void ka_TouchEvent();
84 void ka_RespawnBall() // runs whenever the ball needs to be relocated
85 {SELFPARAM();
86         if(gameover) { return; }
87         vector oldballorigin = self.origin;
88
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))
90         {
91                 entity spot = SelectSpawnPoint(true);
92                 setorigin(self, spot.origin);
93                 self.angles = spot.angles;
94         }
95
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;
104
105         Send_Effect(EFFECT_ELECTRO_COMBO, oldballorigin, '0 0 0', 1);
106         Send_Effect(EFFECT_ELECTRO_COMBO, self.origin, '0 0 0', 1);
107
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);
110
111         sound(self, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
112 }
113
114 void ka_TimeScoring()
115 {SELFPARAM();
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);
120
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;
123         }
124 }
125
126 void ka_TouchEvent() // runs any time that the ball comes in contact with something
127 {SELFPARAM();
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
132                 ka_RespawnBall();
133                 return;
134         }
135         if(IS_DEAD(other)) { return; }
136         if(STAT(FROZEN, other)) { 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);
141                 return;
142         }
143         else if(self.wait > time) { return; }
144
145         // attach the ball to the player
146         self.owner = other;
147         other.ballcarried = self;
148         setattachment(self, other, "");
149         setorigin(self, '0 0 0');
150
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;
159
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;
164
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)
171
172         // scoring
173         PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
174
175         // waypoints
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);
181 }
182
183 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
184 {
185         entity ball;
186         ball = plyr.ballcarried;
187
188         if(!ball) { return; }
189
190         // reset the ball
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
202         ball.owner = world;
203
204         // reset the player effects
205         plyr.glow_trail = false;
206         plyr.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
207
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)
213
214         // scoring
215         // PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1); Not anymore, this is 100% the same as pickups and is useless.
216
217         // waypoints
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);
222 }
223
224 /** used to clear the ballcarrier whenever the match switches from warmup to normal */
225 void ka_Reset(entity this)
226 {
227         if((this.owner) && (IS_PLAYER(this.owner)))
228                 ka_DropEvent(this.owner);
229
230         if(time < game_starttime)
231         {
232                 this.think = ka_RespawnBall;
233                 this.touch = func_null;
234                 this.nextthink = game_starttime;
235         }
236         else
237                 ka_RespawnBall();
238 }
239
240
241 // ================
242 // Bot player logic
243 // ================
244
245 void havocbot_goalrating_ball(float ratingscale, vector org)
246 {SELFPARAM();
247         float t;
248         entity ball_owner;
249         ball_owner = ka_ball.owner;
250
251         if (ball_owner == self)
252                 return;
253
254         // If ball is carried by player then hunt them down.
255         if (ball_owner)
256         {
257                 t = (self.health + self.armorvalue) / (ball_owner.health + ball_owner.armorvalue);
258                 navigation_routerating(ball_owner, t * ratingscale, 2000);
259         }
260         else // Ball has been dropped so collect.
261                 navigation_routerating(ka_ball, ratingscale, 2000);
262 }
263
264 void havocbot_role_ka_carrier()
265 {SELFPARAM();
266         if (IS_DEAD(self))
267                 return;
268
269         if (time > self.bot_strategytime)
270         {
271                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
272
273                 navigation_goalrating_start();
274                 havocbot_goalrating_items(10000, self.origin, 10000);
275                 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
276                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
277                 navigation_goalrating_end();
278         }
279
280         if (!self.ballcarried)
281         {
282                 self.havocbot_role = havocbot_role_ka_collector;
283                 self.bot_strategytime = 0;
284         }
285 }
286
287 void havocbot_role_ka_collector()
288 {SELFPARAM();
289         if (IS_DEAD(self))
290                 return;
291
292         if (time > self.bot_strategytime)
293         {
294                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
295
296                 navigation_goalrating_start();
297                 havocbot_goalrating_items(10000, self.origin, 10000);
298                 havocbot_goalrating_enemyplayers(1000, self.origin, 10000);
299                 havocbot_goalrating_ball(20000, self.origin);
300                 navigation_goalrating_end();
301         }
302
303         if (self.ballcarried)
304         {
305                 self.havocbot_role = havocbot_role_ka_carrier;
306                 self.bot_strategytime = 0;
307         }
308 }
309
310
311 // ==============
312 // Hook Functions
313 // ==============
314
315 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
316 {
317         if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
318         {
319                 if(frag_target.ballcarried) { // add to amount of times killing carrier
320                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
321                         if(autocvar_g_keepaway_score_bckill) // add bckills to the score
322                                 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_bckill);
323                 }
324                 else if(!frag_attacker.ballcarried)
325                         if(autocvar_g_keepaway_noncarrier_warn)
326                                 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
327
328                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
329                         PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
330         }
331
332         if(frag_target.ballcarried) { ka_DropEvent(frag_target); } // a player with the ball has died, drop it
333         return 0;
334 }
335
336 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
337 {
338         frag_score = 0; // no frags counted in keepaway
339         return 1; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
340 }
341
342 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
343 {SELFPARAM();
344         // clear the item used for the ball in keepaway
345         self.items &= ~IT_KEY1;
346
347         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
348         if(self.ballcarried)
349                 self.items |= IT_KEY1;
350
351         return 0;
352 }
353
354 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
355 {SELFPARAM();
356         if(MUTATOR_RETURNVALUE == 0)
357         if(self.ballcarried)
358         {
359                 ka_DropEvent(self);
360                 return 1;
361         }
362         return 0;
363 }
364
365 MUTATOR_HOOKFUNCTION(ka, PlayerDamage_Calculate) // for changing damage and force values that are applied to players in g_damage.qc
366 {
367         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
368         {
369                 if(frag_target == frag_attacker) // damage done to yourself
370                 {
371                         frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
372                         frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
373                 }
374                 else // damage done to noncarriers
375                 {
376                         frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
377                         frag_force *= autocvar_g_keepaway_ballcarrier_force;
378                 }
379         }
380         else if (!frag_target.ballcarried) // if the target is a noncarrier
381         {
382                 if(frag_target == frag_attacker) // damage done to yourself
383                 {
384                         frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
385                         frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
386                 }
387                 else // damage done to other noncarriers
388                 {
389                         frag_damage *= autocvar_g_keepaway_noncarrier_damage;
390                         frag_force *= autocvar_g_keepaway_noncarrier_force;
391                 }
392         }
393         return 0;
394 }
395
396 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
397 {SELFPARAM();
398         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
399         return 0;
400 }
401
402 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
403 {SELFPARAM();
404         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
405         return 0;
406 }
407
408 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
409 {SELFPARAM();
410         // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
411         // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
412
413         self.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
414
415         if(self.ballcarried)
416                 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
417
418         return 0;
419 }
420
421 .float stat_sv_airspeedlimit_nonqw;
422 .float stat_sv_maxspeed;
423
424 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics)
425 {SELFPARAM();
426         if(self.ballcarried)
427         {
428                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_keepaway_ballcarrier_highspeed;
429                 self.stat_sv_maxspeed *= autocvar_g_keepaway_ballcarrier_highspeed;
430         }
431         return false;
432 }
433
434 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
435 {SELFPARAM();
436         // if neither player has ball then don't attack unless the ball is on the ground
437         if(!checkentity.ballcarried && !self.ballcarried && ka_ball.owner)
438                 return true;
439         return false;
440 }
441
442 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
443 {SELFPARAM();
444         if (self.ballcarried)
445                 self.havocbot_role = havocbot_role_ka_carrier;
446         else
447                 self.havocbot_role = havocbot_role_ka_collector;
448         return true;
449 }
450
451 MUTATOR_HOOKFUNCTION(ka, DropSpecialItems)
452 {
453         if(frag_target.ballcarried)
454                 ka_DropEvent(frag_target);
455
456         return false;
457 }
458
459
460 // ==============
461 // Initialization
462 // ==============
463
464 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
465 {
466         entity e = new(keepawayball);
467         e.model = "models/orbs/orbblue.md3";
468         precache_model(e.model);
469         _setmodel(e, e.model);
470         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
471         e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
472         e.takedamage = DAMAGE_YES;
473         e.solid = SOLID_TRIGGER;
474         e.movetype = MOVETYPE_BOUNCE;
475         e.glow_color = autocvar_g_keepawayball_trail_color;
476         e.glow_trail = true;
477         e.flags = FL_ITEM;
478         e.reset = ka_Reset;
479         e.touch = ka_TouchEvent;
480         e.owner = world;
481         ka_ball = e;
482
483         InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
484 }
485
486 void ka_ScoreRules()
487 {
488         ScoreRules_basics(0, SFL_SORT_PRIO_PRIMARY, 0, true); // SFL_SORT_PRIO_PRIMARY
489         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_PICKUPS,                     "pickups",              0);
490         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_CARRIERKILLS,        "bckills",              0);
491         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_BCTIME,                      "bctime",               SFL_SORT_PRIO_SECONDARY);
492         ScoreRules_basics_end();
493 }
494
495 void ka_Initialize() // run at the start of a match, initiates game mode
496 {
497         ka_ScoreRules();
498         ka_SpawnBall();
499 }
500
501 #endif