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