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