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