]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_keepaway.qc
Merge branch 'TimePath/entitydebugger' into 'master'
[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(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);
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 void ka_Reset() // used to clear the ballcarrier whenever the match switches from warmup to normal
225 {SELFPARAM();
226         if((self.owner) && (IS_PLAYER(self.owner)))
227                 ka_DropEvent(self.owner);
228
229         if(time < game_starttime)
230         {
231                 self.think = ka_RespawnBall;
232                 self.touch = func_null;
233                 self.nextthink = game_starttime;
234         }
235         else
236                 ka_RespawnBall();
237 }
238
239
240 // ================
241 // Bot player logic
242 // ================
243
244 void havocbot_goalrating_ball(float ratingscale, vector org)
245 {SELFPARAM();
246         float t;
247         entity ball_owner;
248         ball_owner = ka_ball.owner;
249
250         if (ball_owner == self)
251                 return;
252
253         // If ball is carried by player then hunt them down.
254         if (ball_owner)
255         {
256                 t = (self.health + self.armorvalue) / (ball_owner.health + ball_owner.armorvalue);
257                 navigation_routerating(ball_owner, t * ratingscale, 2000);
258         }
259         else // Ball has been dropped so collect.
260                 navigation_routerating(ka_ball, ratingscale, 2000);
261 }
262
263 void havocbot_role_ka_carrier()
264 {SELFPARAM();
265         if (self.deadflag != DEAD_NO)
266                 return;
267
268         if (time > self.bot_strategytime)
269         {
270                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
271
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();
277         }
278
279         if (!self.ballcarried)
280         {
281                 self.havocbot_role = havocbot_role_ka_collector;
282                 self.bot_strategytime = 0;
283         }
284 }
285
286 void havocbot_role_ka_collector()
287 {SELFPARAM();
288         if (self.deadflag != DEAD_NO)
289                 return;
290
291         if (time > self.bot_strategytime)
292         {
293                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
294
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();
300         }
301
302         if (self.ballcarried)
303         {
304                 self.havocbot_role = havocbot_role_ka_carrier;
305                 self.bot_strategytime = 0;
306         }
307 }
308
309
310 // ==============
311 // Hook Functions
312 // ==============
313
314 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
315 {SELFPARAM();
316         if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
317         {
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);
322                 }
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);
326
327                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
328                         PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
329         }
330
331         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
332         return 0;
333 }
334
335 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
336 {
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.
339 }
340
341 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
342 {SELFPARAM();
343         // clear the item used for the ball in keepaway
344         self.items &= ~IT_KEY1;
345
346         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
347         if(self.ballcarried)
348                 self.items |= IT_KEY1;
349
350         return 0;
351 }
352
353 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
354 {SELFPARAM();
355         if(MUTATOR_RETURNVALUE == 0)
356         if(self.ballcarried)
357         {
358                 ka_DropEvent(self);
359                 return 1;
360         }
361         return 0;
362 }
363
364 MUTATOR_HOOKFUNCTION(ka, PlayerDamage_Calculate) // for changing damage and force values that are applied to players in g_damage.qc
365 {
366         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
367         {
368                 if(frag_target == frag_attacker) // damage done to yourself
369                 {
370                         frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
371                         frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
372                 }
373                 else // damage done to noncarriers
374                 {
375                         frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
376                         frag_force *= autocvar_g_keepaway_ballcarrier_force;
377                 }
378         }
379         else if (!frag_target.ballcarried) // if the target is a noncarrier
380         {
381                 if(frag_target == frag_attacker) // damage done to yourself
382                 {
383                         frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
384                         frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
385                 }
386                 else // damage done to other noncarriers
387                 {
388                         frag_damage *= autocvar_g_keepaway_noncarrier_damage;
389                         frag_force *= autocvar_g_keepaway_noncarrier_force;
390                 }
391         }
392         return 0;
393 }
394
395 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
396 {SELFPARAM();
397         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
398         return 0;
399 }
400
401 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
402 {SELFPARAM();
403         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
404         return 0;
405 }
406
407 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
408 {SELFPARAM();
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()
411
412         self.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
413
414         if(self.ballcarried)
415                 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
416
417         return 0;
418 }
419
420 .float stat_sv_airspeedlimit_nonqw;
421 .float stat_sv_maxspeed;
422
423 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics)
424 {SELFPARAM();
425         if(self.ballcarried)
426         {
427                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_keepaway_ballcarrier_highspeed;
428                 self.stat_sv_maxspeed *= autocvar_g_keepaway_ballcarrier_highspeed;
429         }
430         return false;
431 }
432
433 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
434 {SELFPARAM();
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)
437                 return true;
438         return false;
439 }
440
441 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
442 {SELFPARAM();
443         if (self.ballcarried)
444                 self.havocbot_role = havocbot_role_ka_carrier;
445         else
446                 self.havocbot_role = havocbot_role_ka_collector;
447         return true;
448 }
449
450 MUTATOR_HOOKFUNCTION(ka, DropSpecialItems)
451 {
452         if(frag_target.ballcarried)
453                 ka_DropEvent(frag_target);
454
455         return false;
456 }
457
458
459 // ==============
460 // Initialization
461 // ==============
462
463 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
464 {
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;
475         e.glow_trail = true;
476         e.flags = FL_ITEM;
477         e.reset = ka_Reset;
478         e.touch = ka_TouchEvent;
479         e.owner = world;
480         ka_ball = e;
481
482         InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
483 }
484
485 void ka_ScoreRules()
486 {
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();
492 }
493
494 void ka_Initialize() // run at the start of a match, initiates game mode
495 {
496         ka_ScoreRules();
497         ka_SpawnBall();
498 }
499
500 #endif