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