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