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