]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/buffs/sv_buffs.qc
Resolve conflicts 1: Merge commit 'c58baab5' into bones_was_here/q3compat
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / buffs / sv_buffs.qc
1 #include "sv_buffs.qh"
2
3 #include <common/mapobjects/target/music.qh>
4 #include <common/mutators/mutator/instagib/_mod.qh>
5 #include <common/gamemodes/_mod.qh>
6 #include <server/items/items.qh>
7
8 bool buffs_BuffModel_Customize(entity this, entity client)
9 {
10         entity player = WaypointSprite_getviewentity(client);
11         entity myowner = this.owner;
12
13         if(myowner.alpha <= 0.5 && DIFF_TEAM(player, myowner) && myowner.alpha != 0)
14                 return false;
15
16         if(MUTATOR_CALLHOOK(BuffModel_Customize, this, player))
17                 return false;
18
19         if(player == myowner)
20         {
21                 // somewhat hide the model, but keep the glow
22                 this.effects = 0;
23                 this.alpha = -1;
24         }
25         else
26         {
27                 this.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
28                 this.alpha = 1;
29         }
30         return true;
31 }
32
33 void buffs_BuffModel_Spawn(entity player)
34 {
35         player.buff_model = new(buff_model);
36         setmodel(player.buff_model, MDL_BUFF);
37         setsize(player.buff_model, '0 0 -40', '0 0 40');
38         setattachment(player.buff_model, player, "");
39         setorigin(player.buff_model, '0 0 1' * (player.buff_model.maxs.z * 1));
40         player.buff_model.owner = player;
41         player.buff_model.scale = 0.7;
42         player.buff_model.pflags = PFLAGS_FULLDYNAMIC;
43         player.buff_model.light_lev = 200;
44         setcefc(player.buff_model, buffs_BuffModel_Customize);
45 }
46
47 void buffs_BuffModel_Remove(entity player)
48 {
49         if(player.buff_model)
50                 delete(player.buff_model);
51         player.buff_model = NULL;
52 }
53
54 vector buff_GlowColor(entity buff)
55 {
56         //if(buff.team_forced) { return Team_ColorRGB(buff.team_forced); }
57         return buff.m_color;
58 }
59
60 void buff_Effect(entity player, string eff)
61 {
62         if(!autocvar_g_buffs_effects) { return; }
63
64         if(time >= player.buff_effect_delay)
65         {
66                 Send_Effect_(eff, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
67                 player.buff_effect_delay = time + 0.05; // prevent spam
68         }
69 }
70
71 // buff item
72 bool buff_Waypoint_visible_for_player(entity this, entity player, entity view)
73 {
74         if(!this.owner.buff_active && !this.owner.buff_activetime || !this.owner.buffdef)
75                 return false;
76
77         entity heldbuff = buff_FirstFromFlags(view); // TODO: cache this information so it isn't performing a loop every frame
78         if (heldbuff) 
79         {
80                 return CS_CVAR(view).cvar_cl_buffs_autoreplace == false || heldbuff != this.owner.buffdef;
81         }
82
83         return WaypointSprite_visible_for_player(this, player, view);
84 }
85
86 void buff_Waypoint_Spawn(entity e)
87 {
88         if(autocvar_g_buffs_waypoint_distance <= 0) return;
89
90         entity buff = e.buffdef;
91         entity wp = WaypointSprite_Spawn(WP_Buff, 0, autocvar_g_buffs_waypoint_distance, e, '0 0 1' * e.maxs.z, NULL, e.team_forced, e, buff_waypoint, true, RADARICON_Buff);
92         wp.wp_extra = buff.m_id;
93         WaypointSprite_UpdateTeamRadar(e.buff_waypoint, RADARICON_Buff, e.glowmod);
94         e.buff_waypoint.waypointsprite_visible_for_player = buff_Waypoint_visible_for_player;
95 }
96
97 void buff_SetCooldown(entity this, float cd)
98 {
99         cd = max(0, cd);
100
101         if(!this.buff_waypoint)
102                 buff_Waypoint_Spawn(this);
103
104         if(this.buff_waypoint)
105                 WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + cd);
106
107         this.buff_activetime = cd;
108         this.buff_active = !cd;
109 }
110
111 void buff_Respawn(entity this)
112 {
113         if(game_stopped) return;
114
115         vector oldbufforigin = this.origin;
116         this.velocity = '0 0 200';
117
118         if(!MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY,
119                 ((autocvar_g_buffs_random_location_attempts > 0) ? autocvar_g_buffs_random_location_attempts : 10), 1024, 256))
120         {
121                 entity spot = SelectSpawnPoint(this, true);
122                 setorigin(this, spot.origin);
123                 this.velocity = ((randomvec() * 100) + '0 0 200');
124                 this.angles = spot.angles;
125         }
126
127         tracebox(this.origin, this.mins * 1.5, this.maxs * 1.5, this.origin, MOVE_NOMONSTERS, this);
128
129         setorigin(this, trace_endpos); // attempt to unstick
130
131         set_movetype(this, MOVETYPE_TOSS);
132
133         makevectors(this.angles);
134         this.angles = '0 0 0';
135         if(autocvar_g_buffs_random_lifetime > 0)
136                 this.lifetime = time + autocvar_g_buffs_random_lifetime;
137
138         Send_Effect(EFFECT_ELECTRO_COMBO, oldbufforigin + ((this.mins + this.maxs) * 0.5), '0 0 0', 1);
139         Send_Effect(EFFECT_ELECTRO_COMBO, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
140
141         WaypointSprite_Ping(this.buff_waypoint);
142
143         sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
144 }
145
146 void buff_Touch(entity this, entity toucher)
147 {
148         if(game_stopped) return;
149
150         if(ITEM_TOUCH_NEEDKILL())
151         {
152                 buff_Respawn(this);
153                 return;
154         }
155
156         if(!this.buff_active)
157                 return;
158
159         if(MUTATOR_CALLHOOK(BuffTouch, this, toucher))
160                 return;
161         toucher = M_ARGV(1, entity);
162
163         if(!IS_PLAYER(toucher))
164                 return; // incase mutator changed toucher
165
166         if((this.team_forced && toucher.team != this.team_forced)
167         || (STAT(FROZEN, toucher))
168         || (toucher.vehicle)
169         || (!this.buffdef) // TODO: error out or maybe reset type if this occurs?
170         || (time < PS(toucher).buff_shield)
171         )
172         {
173                 // can't touch this
174                 return;
175         }
176
177         entity heldbuff = buff_FirstFromFlags(toucher);
178         entity thebuff = this.buffdef;
179
180         if (heldbuff)
181         {
182                 if (CS_CVAR(toucher).cvar_cl_buffs_autoreplace && heldbuff != thebuff)
183                 {
184                         // TODO: lost-gained notification for this case
185                         int buffid = heldbuff.m_id;
186                         Send_Notification(NOTIF_ONE, toucher, MSG_INFO, INFO_ITEM_BUFF_LOST, toucher.netname, buffid);
187                         if(!IS_INDEPENDENT_PLAYER(toucher))
188                                 Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_INFO, INFO_ITEM_BUFF_LOST, toucher.netname, buffid);
189
190                         //sound(toucher, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
191                 }
192                 else { return; } // do nothing
193         }
194
195         this.owner = toucher;
196         this.buff_active = false;
197         this.lifetime = 0;
198         Send_Notification(NOTIF_ONE, toucher, MSG_MULTI, ITEM_BUFF_GOT, thebuff.m_id);
199         if(!IS_INDEPENDENT_PLAYER(toucher))
200                 Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_INFO, INFO_ITEM_BUFF, toucher.netname, thebuff.m_id);
201
202         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
203         sound(toucher, CH_TRIGGER, SND_SHIELD_RESPAWN, VOL_BASE, ATTN_NORM);
204         float oldtime = StatusEffects_gettime(thebuff, toucher);
205         float bufftime = ((this.buffs_finished) ? this.buffs_finished : thebuff.m_time(thebuff));
206
207         buff_RemoveAll(toucher, STATUSEFFECT_REMOVE_NORMAL); // remove previous buffs so that a new one may be added
208         if(bufftime)
209                 StatusEffects_apply(thebuff, toucher, min(time + bufftime, max(oldtime, time) + bufftime), 0);
210         else
211                 StatusEffects_apply(thebuff, toucher, time + 999, 0); // HACK: zero timer means "infinite"!
212
213         STAT(LAST_PICKUP, toucher) = time;
214 }
215
216 float buff_Available(entity buff)
217 {
218         if (!buff)
219                 return false;
220         if (buff == BUFF_AMMO && ((start_items & IT_UNLIMITED_AMMO) || cvar("g_melee_only")))
221                 return false;
222         if (buff == BUFF_VAMPIRE && cvar("g_vampire"))
223                 return false;
224         return cvar(strcat("g_buffs_", buff.netname));
225 }
226
227 .int buff_seencount;
228
229 void buff_NewType(entity ent)
230 {
231         RandomSelection_Init();
232         FOREACH(StatusEffect, it.instanceOfBuff && buff_Available(it),
233         {
234                 // if it's already been chosen, give it a lower priority
235                 float myseencount = (it.buff_seencount > 0) ? it.buff_seencount : 1; // no division by zero please!
236                 RandomSelection_AddEnt(it, max(0.2, 1 / myseencount), 1);
237         });
238         entity newbuff = RandomSelection_chosen_ent;
239         if(!newbuff)
240                 return;
241         newbuff.buff_seencount += 1; // lower chances of seeing this buff again soon
242         ent.buffdef = newbuff;
243 }
244
245 void buff_RemoveAll(entity actor, int removal_type)
246 {
247         if(!actor.statuseffects)
248                 return;
249         FOREACH(StatusEffect, it.instanceOfBuff,
250         {
251                 it.m_remove(it, actor, removal_type);
252         });
253 }
254
255 entity buff_FirstFromFlags(entity actor)
256 {
257         if(!actor.statuseffects)
258                 return NULL;
259         FOREACH(StatusEffect, it.instanceOfBuff && it.m_active(it, actor), { return it; });
260         return NULL;
261 }
262
263 void buff_Think(entity this)
264 {
265         if(this.buff_waypoint && autocvar_g_buffs_waypoint_distance <= 0)
266                 WaypointSprite_Kill(this.buff_waypoint);
267
268         if(this.buffdef != this.oldbuffs)
269         {
270                 entity buff = this.buffdef;
271                 this.color = buff.m_color;
272                 this.glowmod = buff_GlowColor(buff);
273                 this.skin = buff.m_skin;
274
275                 setmodel(this, MDL_BUFF);
276                 setsize(this, BUFF_MIN, BUFF_MAX);
277
278                 if(this.buff_waypoint)
279                 {
280                         //WaypointSprite_Disown(this.buff_waypoint, 1);
281                         WaypointSprite_Kill(this.buff_waypoint);
282                         buff_Waypoint_Spawn(this);
283                         if(this.buff_activetime)
284                                 WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + this.buff_activetime - frametime);
285                 }
286
287                 this.oldbuffs = this.buffdef;
288         }
289
290         if(!game_stopped)
291         if((round_handler_IsActive() && round_handler_IsRoundStarted()) || time >= game_starttime)
292         if(!this.buff_activetime_updated)
293         {
294                 buff_SetCooldown(this, this.buff_activetime);
295                 this.buff_activetime_updated = true;
296         }
297
298         if(!this.buff_active && !this.buff_activetime)
299         if(!this.owner || STAT(FROZEN, this.owner) || IS_DEAD(this.owner) || !this.owner.iscreature || this.owner.vehicle
300                 || this.pickup_anyway > 0 || (this.pickup_anyway >= 0 && autocvar_g_buffs_pickup_anyway) || this.buffdef != buff_FirstFromFlags(this.owner))
301         {
302                 buff_SetCooldown(this, autocvar_g_buffs_cooldown_respawn + frametime);
303                 this.owner = NULL;
304                 if(autocvar_g_buffs_randomize && (!teamplay || autocvar_g_buffs_randomize_teamplay))
305                         buff_NewType(this);
306
307                 if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
308                         buff_Respawn(this);
309         }
310
311         if(this.buff_activetime)
312         if(!game_stopped)
313         if((round_handler_IsActive() && round_handler_IsRoundStarted()) || time >= game_starttime)
314         {
315                 this.buff_activetime = max(0, this.buff_activetime - frametime);
316
317                 if(!this.buff_activetime)
318                 {
319                         this.buff_active = true;
320                         sound(this, CH_TRIGGER, SND_STRENGTH_RESPAWN, VOL_BASE, ATTN_NORM);
321                         Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
322                 }
323         }
324
325         if(this.buff_active)
326         {
327                 if(this.team_forced && !this.buff_waypoint)
328                         buff_Waypoint_Spawn(this);
329
330                 if(this.lifetime && time >= this.lifetime)
331                         buff_Respawn(this);
332         }
333
334         this.nextthink = time;
335         //this.angles_y = time * 110.1;
336 }
337
338 void buff_Waypoint_Reset(entity this)
339 {
340         WaypointSprite_Kill(this.buff_waypoint);
341
342         if(this.buff_activetime) { buff_Waypoint_Spawn(this); }
343 }
344
345 void buff_Reset(entity this)
346 {
347         if(autocvar_g_buffs_randomize && (!teamplay || autocvar_g_buffs_randomize_teamplay))
348                 buff_NewType(this);
349         this.owner = NULL;
350         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate);
351         buff_Waypoint_Reset(this);
352         this.buff_activetime_updated = false;
353
354         if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
355                 buff_Respawn(this);
356 }
357
358 bool buff_Customize(entity this, entity client)
359 {
360         entity player = WaypointSprite_getviewentity(client);
361         if((!this.buff_active || !this.buffdef) || (this.team_forced && player.team != this.team_forced))
362         {
363                 this.alpha = 0.3;
364                 if(this.effects & EF_FULLBRIGHT) { this.effects &= ~(EF_FULLBRIGHT); }
365                 this.pflags = 0;
366         }
367         else
368         {
369                 this.alpha = 1;
370                 if(!(this.effects & EF_FULLBRIGHT)) { this.effects |= EF_FULLBRIGHT; }
371                 this.light_lev = 220 + 36 * sin(time);
372                 this.pflags = PFLAGS_FULLDYNAMIC;
373         }
374         return true;
375 }
376
377 void buff_Delete(entity this)
378 {
379         WaypointSprite_Kill(this.buff_waypoint);
380         delete_fn(this);
381 }
382
383 void buff_Init(entity this)
384 {
385         if(!cvar("g_buffs")) { delete(this); return; }
386
387         entity buff = this.buffdef;
388
389         if(!buff || !buff_Available(buff))
390                 buff_NewType(this);
391
392         this.classname = "item_buff";
393         this.solid = SOLID_TRIGGER;
394         this.flags = FL_ITEM;
395         this.bot_pickup = true;
396         this.bot_pickupevalfunc = generic_pickupevalfunc;
397         this.bot_pickupbasevalue = 1000;
398         IL_PUSH(g_items, this);
399         setthink(this, buff_Think);
400         settouch(this, buff_Touch);
401         setmodel(this, MDL_BUFF);
402         setsize(this, BUFF_MIN, BUFF_MAX);
403         this.reset = buff_Reset;
404         this.nextthink = time + 0.1;
405         this.gravity = 1;
406         set_movetype(this, MOVETYPE_TOSS);
407         this.scale = 1;
408         this.skin = buff.m_skin;
409         this.effects = EF_FULLBRIGHT | EF_STARDUST | EF_NOSHADOW;
410         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
411         setcefc(this, buff_Customize);
412         //this.gravity = 100;
413         this.color = buff.m_color;
414         this.glowmod = buff_GlowColor(this);
415         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate + max(0, game_starttime - time));
416         this.buff_active = !this.buff_activetime;
417         this.pflags = PFLAGS_FULLDYNAMIC;
418         this.dtor = buff_Delete;
419
420         if(!this.buffs_finished)
421                 this.buffs_finished = this.count; // legacy support
422
423         if(this.spawnflags & 1)
424                 this.noalign = true;
425
426         if(this.noalign)
427                 set_movetype(this, MOVETYPE_NONE); // reset by random location
428
429         if(cvar("g_buffs_random_location") || (this.spawnflags & 64))
430                 buff_Respawn(this);
431 }
432
433 void buff_Init_Compat(entity ent, entity replacement)
434 {
435         if (teamplay)
436         {
437                 if (ent.spawnflags & 2)
438                         ent.team_forced = NUM_TEAM_1;
439                 else if (ent.spawnflags & 4)
440                         ent.team_forced = NUM_TEAM_2;
441         }
442
443         ent.buffdef = replacement;
444
445         buff_Init(ent);
446 }
447
448 void buff_SpawnReplacement(entity ent, entity old)
449 {
450         setorigin(ent, old.origin);
451         ent.angles = old.angles;
452         ent.noalign = Item_ShouldKeepPosition(old);
453
454         buff_Init(ent);
455 }
456
457 void buff_Vengeance_DelayedDamage(entity this)
458 {
459         if(this.enemy)
460                 Damage(this.enemy, this.owner, this.owner, this.dmg, DEATH_BUFF.m_id, DMG_NOWEP, this.enemy.origin, '0 0 0');
461
462         delete(this);
463         return;
464 }
465
466 float buff_Inferno_CalculateTime(float damg, float offset_x, float offset_y, float intersect_x, float intersect_y, float base)
467 {
468         return offset_y + (intersect_y - offset_y) * logn(((damg - offset_x) * ((base - 1) / intersect_x)) + 1, base);
469 }
470
471 // mutator hooks
472 MUTATOR_HOOKFUNCTION(buffs, Damage_Calculate)
473 {
474         entity frag_attacker = M_ARGV(1, entity);
475         entity frag_target = M_ARGV(2, entity);
476         float frag_deathtype = M_ARGV(3, float);
477         float frag_damage = M_ARGV(4, float);
478         vector frag_force = M_ARGV(6, vector);
479
480         if(frag_deathtype == DEATH_BUFF.m_id) { return; }
481
482         if(StatusEffects_active(BUFF_RESISTANCE, frag_target))
483         {
484                 float reduced = frag_damage * autocvar_g_buffs_resistance_blockpercent;
485                 frag_damage = bound(0, frag_damage - reduced, frag_damage);
486         }
487
488         if(StatusEffects_active(BUFF_SPEED, frag_target))
489         if(frag_target != frag_attacker)
490                 frag_damage *= autocvar_g_buffs_speed_damage_take;
491
492         if(StatusEffects_active(BUFF_MEDIC, frag_target))
493         if((GetResource(frag_target, RES_HEALTH) - frag_damage) <= 0)
494         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
495         if(frag_attacker)
496         if(random() <= autocvar_g_buffs_medic_survive_chance)
497                 frag_damage = max(5, GetResource(frag_target, RES_HEALTH) - autocvar_g_buffs_medic_survive_health);
498
499         if(StatusEffects_active(BUFF_JUMP, frag_target))
500         if(frag_deathtype == DEATH_FALL.m_id)
501                 frag_damage = 0;
502
503         if(StatusEffects_active(BUFF_VENGEANCE, frag_target))
504         if(frag_attacker)
505         if(frag_attacker != frag_target)
506         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
507         {
508                 entity dmgent = new_pure(dmgent);
509
510                 dmgent.dmg = frag_damage * autocvar_g_buffs_vengeance_damage_multiplier;
511                 dmgent.enemy = frag_attacker;
512                 dmgent.owner = frag_target;
513                 setthink(dmgent, buff_Vengeance_DelayedDamage);
514                 dmgent.nextthink = time + 0.1;
515         }
516
517         if(StatusEffects_active(BUFF_BASH, frag_target))
518         if(frag_attacker != frag_target)
519                 frag_force = '0 0 0';
520
521         if(StatusEffects_active(BUFF_BASH, frag_attacker))
522         if(frag_force)
523         {
524                 if(frag_attacker == frag_target)
525                         frag_force *= autocvar_g_buffs_bash_force_self;
526                 else
527                         frag_force *= autocvar_g_buffs_bash_force;
528         }
529
530         if(StatusEffects_active(BUFF_DISABILITY, frag_attacker))
531         if(frag_target != frag_attacker)
532                 frag_target.buff_disability_time = time + autocvar_g_buffs_disability_slowtime;
533
534         if(StatusEffects_active(BUFF_INFERNO, frag_target))
535         {
536                 if(frag_deathtype == DEATH_FIRE.m_id)
537                         frag_damage = 0;
538                 if(frag_deathtype == DEATH_LAVA.m_id)
539                         frag_damage *= 0.5; // TODO: cvarize?
540         }
541
542         if(StatusEffects_active(BUFF_LUCK, frag_attacker))
543         if(frag_attacker != frag_target)
544         if(autocvar_g_buffs_luck_damagemultiplier > 0)
545         if(random() <= autocvar_g_buffs_luck_chance)
546                 frag_damage *= autocvar_g_buffs_luck_damagemultiplier;
547
548         if(StatusEffects_active(BUFF_INFERNO, frag_attacker))
549         if(frag_target != frag_attacker) {
550                 float btime = buff_Inferno_CalculateTime(
551                         frag_damage,
552                         0,
553                         autocvar_g_buffs_inferno_burntime_min_time,
554                         autocvar_g_buffs_inferno_burntime_target_damage,
555                         autocvar_g_buffs_inferno_burntime_target_time,
556                         autocvar_g_buffs_inferno_burntime_factor
557                 );
558                 Fire_AddDamage(frag_target, frag_attacker, (frag_damage * autocvar_g_buffs_inferno_damagemultiplier), btime, DEATH_BUFF.m_id);
559         }
560
561         M_ARGV(4, float) = frag_damage;
562         M_ARGV(6, vector) = frag_force;
563 }
564
565 MUTATOR_HOOKFUNCTION(buffs, PlayerDamage_SplitHealthArmor)
566 {
567         entity frag_attacker = M_ARGV(1, entity);
568         entity frag_target = M_ARGV(2, entity);
569         if(!StatusEffects_active(BUFF_VAMPIRE, frag_attacker))
570                 return;
571         float health_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
572
573         if(time >= frag_target.spawnshieldtime &&
574                 frag_target != frag_attacker &&
575                 IS_PLAYER(frag_attacker) &&
576                 !IS_DEAD(frag_target) && !STAT(FROZEN, frag_target))
577         {
578                 GiveResource(frag_attacker, RES_HEALTH,
579                         autocvar_g_buffs_vampire_damage_steal * health_take);
580         }
581 }
582
583 MUTATOR_HOOKFUNCTION(buffs, PlayerSpawn)
584 {
585         entity player = M_ARGV(0, entity);
586
587         buffs_BuffModel_Remove(player);
588         player.oldbuffs = NULL;
589         // reset timers here to prevent them continuing after re-spawn
590         player.buff_disability_time = 0;
591         player.buff_disability_effect_time = 0;
592 }
593
594 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics_UpdateStats)
595 {
596         entity player = M_ARGV(0, entity);
597         // these automatically reset, no need to worry
598
599         if(StatusEffects_active(BUFF_SPEED, player))
600                 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_buffs_speed_speed;
601
602         if(time < player.buff_disability_time)
603                 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_buffs_disability_speed;
604 }
605
606 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics)
607 {
608         entity player = M_ARGV(0, entity);
609         // these automatically reset, no need to worry
610
611         if(StatusEffects_active(BUFF_JUMP, player))
612                 STAT(MOVEVARS_JUMPVELOCITY, player) = autocvar_g_buffs_jump_height;
613 }
614
615 MUTATOR_HOOKFUNCTION(buffs, MonsterMove)
616 {
617         entity mon = M_ARGV(0, entity);
618
619         if(time < mon.buff_disability_time)
620         {
621                 M_ARGV(1, float) *= autocvar_g_buffs_disability_speed; // run speed
622                 M_ARGV(2, float) *= autocvar_g_buffs_disability_speed; // walk speed
623         }
624 }
625
626 MUTATOR_HOOKFUNCTION(buffs, PlayerDies)
627 {
628         entity frag_target = M_ARGV(2, entity);
629
630         entity heldbuff = buff_FirstFromFlags(frag_target);
631         if(heldbuff)
632         {
633                 int buffid = heldbuff.m_id;
634                 if(!IS_INDEPENDENT_PLAYER(frag_target))
635                         Send_Notification(NOTIF_ALL_EXCEPT, frag_target, MSG_INFO, INFO_ITEM_BUFF_LOST, frag_target.netname, buffid);
636
637                 buffs_BuffModel_Remove(frag_target);
638         }
639 }
640
641 MUTATOR_HOOKFUNCTION(buffs, PlayerUseKey, CBC_ORDER_FIRST)
642 {
643         if(MUTATOR_RETURNVALUE || game_stopped || !autocvar_g_buffs_drop) return;
644
645         entity player = M_ARGV(0, entity);
646
647         entity heldbuff = buff_FirstFromFlags(player);
648         if(heldbuff)
649         {
650                 int buffid = heldbuff.m_id;
651                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid);
652                 if(!IS_INDEPENDENT_PLAYER(player))
653                         Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
654
655                 buff_RemoveAll(player, STATUSEFFECT_REMOVE_NORMAL);
656                 PS(player).buff_shield = time + max(0, autocvar_g_buffs_pickup_delay);
657                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
658                 return true;
659         }
660 }
661
662 MUTATOR_HOOKFUNCTION(buffs, ForbidThrowCurrentWeapon)
663 {
664         if(MUTATOR_RETURNVALUE || game_stopped) return;
665         entity player = M_ARGV(0, entity);
666
667         if(StatusEffects_active(BUFF_SWAPPER, player))
668         {
669                 float best_distance = autocvar_g_buffs_swapper_range;
670                 entity closest = NULL;
671                 FOREACH_CLIENT(IS_PLAYER(it), {
672                         if(!IS_DEAD(it) && !STAT(FROZEN, it) && !it.vehicle)
673                         if(DIFF_TEAM(it, player))
674                         {
675                                 float test = vlen2(player.origin - it.origin);
676                                 if(test <= best_distance * best_distance)
677                                 {
678                                         best_distance = sqrt(test);
679                                         closest = it;
680                                 }
681                         }
682                 });
683
684                 if(closest)
685                 {
686                         vector my_org, my_vel, my_ang, their_org, their_vel, their_ang;
687
688                         my_org = player.origin;
689                         my_vel = player.velocity;
690                         my_ang = player.angles;
691                         their_org = closest.origin;
692                         their_vel = closest.velocity;
693                         their_ang = closest.angles;
694
695                         Drop_Special_Items(closest);
696
697                         MUTATOR_CALLHOOK(PortalTeleport, player); // initiate flag dropper
698
699                         setorigin(player, their_org);
700                         setorigin(closest, my_org);
701
702                         closest.velocity = my_vel;
703                         closest.angles = my_ang;
704                         if (IS_BOT_CLIENT(closest))
705                         {
706                                 closest.v_angle = closest.angles;
707                                 bot_aim_reset(closest);
708                         }
709                         closest.fixangle = true;
710                         closest.oldorigin = my_org;
711                         closest.oldvelocity = my_vel;
712                         player.velocity = their_vel;
713                         player.angles = their_ang;
714                         if (IS_BOT_CLIENT(player))
715                         {
716                                 player.v_angle = player.angles;
717                                 bot_aim_reset(player);
718                         }
719                         player.fixangle = true;
720                         player.oldorigin = their_org;
721                         player.oldvelocity = their_vel;
722
723                         // set pusher so player gets the kill if they fall into void
724                         closest.pusher = player;
725                         closest.pushltime = time + autocvar_g_maxpushtime;
726                         closest.istypefrag = PHYS_INPUT_BUTTON_CHAT(closest);
727
728                         Send_Effect(EFFECT_ELECTRO_COMBO, their_org, '0 0 0', 1);
729                         Send_Effect(EFFECT_ELECTRO_COMBO, my_org, '0 0 0', 1);
730
731                         sound(player, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
732                         sound(closest, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
733
734                         // TODO: add a counter to handle how many times one can teleport, and a delay to prevent spam
735                         buff_RemoveAll(player, STATUSEFFECT_REMOVE_NORMAL);
736                         return true;
737                 }
738         }
739 }
740
741 bool buffs_RemovePlayer(entity player)
742 {
743         buffs_BuffModel_Remove(player);
744
745         // also reset timers here to prevent them continuing after spectating
746         player.buff_disability_time = 0;
747         player.buff_disability_effect_time = 0;
748
749         return false;
750 }
751 MUTATOR_HOOKFUNCTION(buffs, MakePlayerObserver) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
752 MUTATOR_HOOKFUNCTION(buffs, ClientDisconnect) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
753
754 MUTATOR_HOOKFUNCTION(buffs, CustomizeWaypoint)
755 {
756         entity wp = M_ARGV(0, entity);
757         entity player = M_ARGV(1, entity);
758
759         entity e = WaypointSprite_getviewentity(player);
760
761         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
762         // but only apply this to real players, not to spectators
763         if((wp.owner.flags & FL_CLIENT) && (e == player) && StatusEffects_active(BUFF_INVISIBLE, wp.owner))
764         if(DIFF_TEAM(wp.owner, e))
765                 return true;
766 }
767
768 MUTATOR_HOOKFUNCTION(buffs, FilterItem)
769 {
770         if(autocvar_g_buffs < 0)
771                 return false; // no auto replacing of entities in this mode
772
773         entity item = M_ARGV(0, entity);
774
775         if(autocvar_g_buffs_replace_powerups)
776         {
777                 switch(item.classname)
778                 {
779                         case "item_strength":
780                         case "item_shield":
781                         {
782                                 entity e = spawn();
783                                 buff_SpawnReplacement(e, item);
784                                 return true;
785                         }
786                 }
787         }
788
789         return false;
790 }
791
792 MUTATOR_HOOKFUNCTION(buffs, WeaponRateFactor)
793 {
794         entity player = M_ARGV(1, entity);
795
796         if(StatusEffects_active(BUFF_SPEED, player))
797                 M_ARGV(0, float) *= autocvar_g_buffs_speed_rate;
798
799         if(time < player.buff_disability_time)
800                 M_ARGV(0, float) *= autocvar_g_buffs_disability_rate;
801 }
802
803 MUTATOR_HOOKFUNCTION(buffs, WeaponSpeedFactor)
804 {
805         entity player = M_ARGV(1, entity);
806
807         if(StatusEffects_active(BUFF_SPEED, player))
808                 M_ARGV(0, float) *= autocvar_g_buffs_speed_weaponspeed;
809
810         if(time < player.buff_disability_time)
811                 M_ARGV(0, float) *= autocvar_g_buffs_disability_weaponspeed;
812 }
813
814 .bool buff_flight_crouchheld;
815
816 MUTATOR_HOOKFUNCTION(buffs, PlayerPreThink)
817 {
818         entity player = M_ARGV(0, entity);
819
820         if(game_stopped || IS_DEAD(player) || !IS_PLAYER(player)) return;
821
822         if(StatusEffects_active(BUFF_FLIGHT, player))
823         {
824                 if(!PHYS_INPUT_BUTTON_CROUCH(player))
825                         player.buff_flight_crouchheld = false;
826                 else if(!player.buff_flight_crouchheld)
827                 {
828                         player.buff_flight_crouchheld = true;
829                         player.gravity *= -1;
830                 }
831         }
832
833         if(time < player.buff_disability_time)
834         if(time >= player.buff_disability_effect_time)
835         {
836                 Send_Effect(EFFECT_SMOKING, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
837                 player.buff_disability_effect_time = time + 0.5;
838         }
839
840         // handle buff lost status
841         // 1: notify everyone else
842         // 2: notify carrier as well
843         int buff_lost = 0;
844
845         entity heldbuff = buff_FirstFromFlags(player);
846         float bufftime = StatusEffects_gettime(heldbuff, player);
847         if(heldbuff && bufftime && time >= bufftime)
848                 buff_lost = 2;
849
850         if(STAT(FROZEN, player)) { buff_lost = 1; }
851
852         if(buff_lost && heldbuff)
853         {
854                 int buffid = heldbuff.m_id;
855                 if(buff_lost == 2)
856                 {
857                         Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid); // TODO: special timeout message?
858                         sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
859                 }
860                 else if(!IS_INDEPENDENT_PLAYER(player))
861                         Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
862                 buff_RemoveAll(player, STATUSEFFECT_REMOVE_TIMEOUT); // TODO: remove only the currently active buff?
863                 heldbuff = NULL;
864                 PS(player).buff_shield = time + max(0, autocvar_g_buffs_pickup_delay); // always put in a delay, even if small
865         }
866
867         if(StatusEffects_active(BUFF_MAGNET, player))
868         {
869                 vector pickup_size;
870                 IL_EACH(g_items, it.itemdef,
871                 {
872                         if(it.buffdef)
873                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_buff;
874                         else
875                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_item;
876
877                         if(boxesoverlap(player.absmin - pickup_size, player.absmax + pickup_size, it.absmin, it.absmax))
878                         {
879                                 if(gettouch(it))
880                                         gettouch(it)(it, player);
881                         }
882                 });
883         }
884
885         if(StatusEffects_active(BUFF_AMMO, player))
886         {
887                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
888                 {
889                         .entity weaponentity = weaponentities[slot];
890                         if(player.(weaponentity).clip_size)
891                                 player.(weaponentity).clip_load = player.(weaponentity).(weapon_load[player.(weaponentity).m_switchweapon.m_id]) = player.(weaponentity).clip_size;
892                 }
893         }
894
895         if(!player.vehicle && StatusEffects_active(BUFF_INVISIBLE, player) && player.oldbuffs == BUFF_INVISIBLE)
896                 player.alpha = ((autocvar_g_buffs_invisible_alpha) ? autocvar_g_buffs_invisible_alpha : -1); // powerups reset alpha, so we must enforce this (TODO)
897
898 #define BUFF_ONADD(b) if ( (heldbuff == (b)) && (player.oldbuffs != (b)))
899 #define BUFF_ONREM(b) if ( (heldbuff != (b)) && (player.oldbuffs == (b)))
900
901         if(heldbuff != player.oldbuffs)
902         {
903                 bufftime = heldbuff ? heldbuff.m_time(heldbuff) : 0;
904                 if(StatusEffects_gettime(heldbuff, player) <= time) // if the player still has a buff countdown, don't reset it!
905                 {
906                         player.statuseffects.statuseffect_time[heldbuff.m_id] = (bufftime) ? time + bufftime : 0;
907                         StatusEffects_update(player);
908                 }
909
910                 BUFF_ONADD(BUFF_AMMO)
911                 {
912                         player.buff_ammo_prev_infitems = (player.items & IT_UNLIMITED_AMMO);
913                         player.items |= IT_UNLIMITED_AMMO;
914
915                         if(StatusEffects_active(BUFF_AMMO, player))
916                         {
917                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
918                                 {
919                                         .entity weaponentity = weaponentities[slot];
920                                         if(player.(weaponentity).clip_load)
921                                                 player.(weaponentity).buff_ammo_prev_clipload = player.(weaponentity).clip_load;
922                                         if(player.(weaponentity).clip_size)
923                                                 player.(weaponentity).clip_load = player.(weaponentity).(weapon_load[player.(weaponentity).m_switchweapon.m_id]) = player.(weaponentity).clip_size;
924                                 }
925                         }
926                 }
927
928                 BUFF_ONREM(BUFF_AMMO)
929                 {
930                         if(player.buff_ammo_prev_infitems)
931                                 player.items |= IT_UNLIMITED_AMMO;
932                         else
933                                 player.items &= ~IT_UNLIMITED_AMMO;
934
935                         if(StatusEffects_active(BUFF_AMMO, player))
936                         {
937                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
938                                 {
939                                         .entity weaponentity = weaponentities[slot];
940                                         if(player.(weaponentity).buff_ammo_prev_clipload)
941                                                 player.(weaponentity).clip_load = player.(weaponentity).buff_ammo_prev_clipload;
942                                 }
943                         }
944                 }
945
946                 BUFF_ONADD(BUFF_INVISIBLE)
947                 {
948                         if(StatusEffects_active(STATUSEFFECT_Strength, player) && MUTATOR_IS_ENABLED(mutator_instagib))
949                                 player.buff_invisible_prev_alpha = default_player_alpha; // we don't want to save the powerup's alpha, as player may lose the powerup while holding the buff
950                         else
951                                 player.buff_invisible_prev_alpha = player.alpha;
952                         if(!player.vehicle)
953                                 player.alpha = autocvar_g_buffs_invisible_alpha;
954                 }
955
956                 BUFF_ONREM(BUFF_INVISIBLE)
957                 {
958                         if(!player.vehicle)
959                         {
960                                 if(StatusEffects_active(STATUSEFFECT_Strength, player) && MUTATOR_IS_ENABLED(mutator_instagib))
961                                         player.alpha = autocvar_g_instagib_invis_alpha;
962                                 else
963                                         player.alpha = player.buff_invisible_prev_alpha;
964                         }
965                 }
966
967                 BUFF_ONADD(BUFF_FLIGHT)
968                 {
969                         player.buff_flight_oldgravity = player.gravity;
970                         if(!player.gravity)
971                                 player.gravity = 1;
972                 }
973
974                 BUFF_ONREM(BUFF_FLIGHT)
975                         player.gravity = ((player.trigger_gravity_check) ? player.trigger_gravity_check.enemy.gravity : player.buff_flight_oldgravity);
976
977                 player.oldbuffs = heldbuff;
978                 if(heldbuff)
979                 {
980                         if(!player.buff_model)
981                                 buffs_BuffModel_Spawn(player);
982
983                         player.buff_model.color = heldbuff.m_color;
984                         player.buff_model.glowmod = buff_GlowColor(heldbuff);
985                         player.buff_model.skin = heldbuff.m_skin;
986
987                         player.effects |= EF_NOSHADOW;
988                 }
989                 else
990                 {
991                         buffs_BuffModel_Remove(player);
992
993                         player.effects &= ~(EF_NOSHADOW);
994                 }
995         }
996
997         if(player.buff_model)
998         {
999                 player.buff_model.effects = player.effects;
1000                 player.buff_model.effects |= EF_LOWPRECISION;
1001                 player.buff_model.effects = player.buff_model.effects & EFMASK_CHEAP; // eat performance
1002
1003                 player.buff_model.alpha = player.alpha;
1004         }
1005
1006 #undef BUFF_ONADD
1007 #undef BUFF_ONREM
1008 }
1009
1010 MUTATOR_HOOKFUNCTION(buffs, PlayerRegen)
1011 {
1012         entity player = M_ARGV(0, entity);
1013
1014         if(StatusEffects_active(BUFF_MEDIC, player))
1015         {
1016                 M_ARGV(2, float) = autocvar_g_buffs_medic_rot; // rot_mod
1017                 M_ARGV(4, float) = M_ARGV(1, float) = autocvar_g_buffs_medic_max; // limit_mod = max_mod
1018                 M_ARGV(2, float) = autocvar_g_buffs_medic_regen; // regen_mod
1019         }
1020
1021         if(StatusEffects_active(BUFF_SPEED, player))
1022                 M_ARGV(2, float) = autocvar_g_buffs_speed_regen; // regen_mod
1023 }
1024
1025 REPLICATE(cvar_cl_buffs_autoreplace, bool, "cl_buffs_autoreplace");
1026
1027 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsString)
1028 {
1029         if(autocvar_g_buffs > 0) // only report as a mutator if they're enabled
1030                 M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Buffs");
1031 }
1032
1033 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsPrettyString)
1034 {
1035         if(autocvar_g_buffs > 0)
1036                 M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Buffs");
1037 }
1038
1039 void buffs_DelayedInit(entity this)
1040 {
1041         if(autocvar_g_buffs_spawn_count > 0)
1042         if(find(NULL, classname, "item_buff") == NULL)
1043         {
1044                 float i;
1045                 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
1046                 {
1047                         entity e = spawn();
1048                         e.spawnflags |= 64; // always randomize
1049                         e.velocity = randomvec() * 250; // this gets reset anyway if random location works
1050                         buff_Init(e);
1051                 }
1052         }
1053 }
1054
1055 void buffs_Initialize()
1056 {
1057         // if buffs are above 0, allow random spawning
1058         if(autocvar_g_buffs > 0 && autocvar_g_buffs_spawn_count > 0)
1059                 InitializeEntity(NULL, buffs_DelayedInit, INITPRIO_FINDTARGET);
1060 }