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