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