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