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