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