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