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