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