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