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