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