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