]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/instagib/sv_instagib.qc
Added MUTATOR_IS_ENABLED macro.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / instagib / sv_instagib.qc
1 #include "sv_instagib.qh"
2
3 bool autocvar_g_instagib_damagedbycontents = true;
4 bool autocvar_g_instagib_blaster_keepdamage = false;
5 bool autocvar_g_instagib_blaster_keepforce = false;
6 bool autocvar_g_instagib_mirrordamage;
7 bool autocvar_g_instagib_friendlypush = true;
8 //int autocvar_g_instagib_ammo_drop;
9 bool autocvar_g_instagib_ammo_convert_cells;
10 bool autocvar_g_instagib_ammo_convert_rockets;
11 bool autocvar_g_instagib_ammo_convert_shells;
12 bool autocvar_g_instagib_ammo_convert_bullets;
13 int autocvar_g_instagib_extralives;
14 float autocvar_g_instagib_speed_highspeed;
15
16 #include <server/client.qh>
17
18 #include <common/items/_mod.qh>
19
20 void instagib_invisibility(entity this)
21 {
22         this.strength_finished = autocvar_g_instagib_invisibility_time;
23         StartItem(this, ITEM_Invisibility);
24 }
25
26 void instagib_extralife(entity this)
27 {
28         StartItem(this, ITEM_ExtraLife);
29 }
30
31 void instagib_speed(entity this)
32 {
33         this.invincible_finished = autocvar_g_instagib_speed_time;
34         StartItem(this, ITEM_Speed);
35 }
36
37 .float instagib_nextthink;
38 .float instagib_needammo;
39 void instagib_stop_countdown(entity e)
40 {
41         if (!e.instagib_needammo)
42                 return;
43         Kill_Notification(NOTIF_ONE_ONLY, e, MSG_CENTER, CPID_INSTAGIB_FINDAMMO);
44         e.instagib_needammo = false;
45 }
46 void instagib_ammocheck(entity this)
47 {
48         if(time < this.instagib_nextthink)
49                 return;
50         if(!IS_PLAYER(this))
51                 return; // not a player
52
53         if(IS_DEAD(this) || game_stopped)
54                 instagib_stop_countdown(this);
55         else if (GetResourceAmount(this, RESOURCE_CELLS) > 0 || (this.items & IT_UNLIMITED_WEAPON_AMMO) || (this.flags & FL_GODMODE))
56                 instagib_stop_countdown(this);
57         else if(autocvar_g_rm && autocvar_g_rm_laser)
58         {
59                 if(!this.instagib_needammo)
60                 {
61                         Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_INSTAGIB_DOWNGRADE);
62                         this.instagib_needammo = true;
63                 }
64         }
65         else
66         {
67                 float hp = GetResourceAmount(this, RESOURCE_HEALTH);
68                 this.instagib_needammo = true;
69                 if (hp <= 5)
70                 {
71                         Damage(this, this, this, 5, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
72                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_INSTAGIB_TERMINATED);
73                 }
74                 else if (hp <= 10)
75                 {
76                         Damage(this, this, this, 5, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
77                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_1);
78                 }
79                 else if (hp <= 20)
80                 {
81                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
82                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_2);
83                 }
84                 else if (hp <= 30)
85                 {
86                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
87                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_3);
88                 }
89                 else if (hp <= 40)
90                 {
91                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
92                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_4);
93                 }
94                 else if (hp <= 50)
95                 {
96                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
97                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_5);
98                 }
99                 else if (hp <= 60)
100                 {
101                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
102                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_6);
103                 }
104                 else if (hp <= 70)
105                 {
106                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
107                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_7);
108                 }
109                 else if (hp <= 80)
110                 {
111                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
112                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_8);
113                 }
114                 else if (hp <= 90)
115                 {
116                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_INSTAGIB_FINDAMMO);
117                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
118                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, ANNCE_NUM_9);
119                 }
120                 else
121                 {
122                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_MULTI, MULTI_INSTAGIB_FINDAMMO);
123                         Damage(this, this, this, 10, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
124                 }
125         }
126         this.instagib_nextthink = time + 1;
127 }
128
129 MUTATOR_HOOKFUNCTION(mutator_instagib, MatchEnd)
130 {
131         FOREACH_CLIENT(IS_PLAYER(it), { instagib_stop_countdown(it); });
132 }
133
134 MUTATOR_HOOKFUNCTION(mutator_instagib, MonsterDropItem)
135 {
136         entity item = M_ARGV(1, entity);
137
138         item.monster_loot = ITEM_VaporizerCells;
139 }
140
141 MUTATOR_HOOKFUNCTION(mutator_instagib, MonsterSpawn)
142 {
143         entity mon = M_ARGV(0, entity);
144
145         // always refill ammo
146         if(mon.monsterid == MON_MAGE.monsterid)
147                 mon.skin = 1;
148 }
149
150 MUTATOR_HOOKFUNCTION(mutator_instagib, BotShouldAttack)
151 {
152         entity targ = M_ARGV(1, entity);
153
154         if (targ.items & ITEM_Invisibility.m_itemid)
155                 return true;
156 }
157
158 MUTATOR_HOOKFUNCTION(mutator_instagib, MakePlayerObserver)
159 {
160         entity player = M_ARGV(0, entity);
161
162         instagib_stop_countdown(player);
163 }
164
165 MUTATOR_HOOKFUNCTION(mutator_instagib, ForbidRandomStartWeapons)
166 {
167         return true;
168 }
169
170 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerSpawn)
171 {
172         entity player = M_ARGV(0, entity);
173
174         player.effects |= EF_FULLBRIGHT;
175 }
176
177 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerPreThink)
178 {
179         entity player = M_ARGV(0, entity);
180
181         instagib_ammocheck(player);
182 }
183
184 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerRegen)
185 {
186         // no regeneration in instagib
187         return true;
188 }
189
190 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerPowerups)
191 {
192         entity player = M_ARGV(0, entity);
193
194         if (!(player.effects & EF_FULLBRIGHT))
195                 player.effects |= EF_FULLBRIGHT;
196
197         if (player.items & ITEM_Invisibility.m_itemid)
198         {
199                 play_countdown(player, player.strength_finished, SND_POWEROFF);
200                 if (time > player.strength_finished)
201                 {
202                         player.alpha = default_player_alpha;
203                         player.exteriorweaponentity.alpha = default_weapon_alpha;
204                         player.items &= ~ITEM_Invisibility.m_itemid;
205                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_POWERDOWN_INVISIBILITY);
206                 }
207         }
208         else
209         {
210                 if (time < player.strength_finished)
211                 {
212                         player.alpha = autocvar_g_instagib_invis_alpha;
213                         player.exteriorweaponentity.alpha = autocvar_g_instagib_invis_alpha;
214                         player.items |= ITEM_Invisibility.m_itemid;
215                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_POWERUP_INVISIBILITY, player.netname);
216                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_POWERUP_INVISIBILITY);
217                 }
218         }
219
220         if (player.items & ITEM_Speed.m_itemid)
221         {
222                 play_countdown(player, player.invincible_finished, SND_POWEROFF);
223                 if (time > player.invincible_finished)
224                 {
225                         player.items &= ~ITEM_Speed.m_itemid;
226                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_POWERDOWN_SPEED);
227                 }
228         }
229         else
230         {
231                 if (time < player.invincible_finished)
232                 {
233                         player.items |= ITEM_Speed.m_itemid;
234                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_POWERUP_SPEED, player.netname);
235                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_POWERUP_SPEED);
236                 }
237         }
238 }
239
240 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerPhysics_UpdateStats)
241 {
242         entity player = M_ARGV(0, entity);
243         // these automatically reset, no need to worry
244
245         if(player.items & ITEM_Speed.m_itemid)
246                 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_instagib_speed_highspeed;
247 }
248
249 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerDamage_SplitHealthArmor)
250 {
251         M_ARGV(4, float) = M_ARGV(7, float); // take = damage
252         M_ARGV(5, float) = 0; // save
253 }
254
255 MUTATOR_HOOKFUNCTION(mutator_instagib, ForbidThrowCurrentWeapon)
256 {
257         // weapon dropping on death handled by FilterItem
258         return true;
259 }
260
261 MUTATOR_HOOKFUNCTION(mutator_instagib, Damage_Calculate)
262 {
263         entity frag_attacker = M_ARGV(1, entity);
264         entity frag_target = M_ARGV(2, entity);
265         float frag_deathtype = M_ARGV(3, float);
266         float frag_damage = M_ARGV(4, float);
267         float frag_mirrordamage = M_ARGV(5, float);
268         vector frag_force = M_ARGV(6, vector);
269
270         if(autocvar_g_friendlyfire == 0 && SAME_TEAM(frag_target, frag_attacker) && IS_PLAYER(frag_target) && IS_PLAYER(frag_attacker))
271                 frag_damage = 0;
272
273         if(IS_PLAYER(frag_target))
274         {
275                 if(frag_deathtype == DEATH_FALL.m_id)
276                         frag_damage = 0; // never count fall damage
277
278                 if(!autocvar_g_instagib_damagedbycontents)
279                 switch(DEATH_ENT(frag_deathtype))
280                 {
281                         case DEATH_DROWN:
282                         case DEATH_SLIME:
283                         case DEATH_LAVA:
284                                 frag_damage = 0;
285                                 break;
286                 }
287
288                 if(IS_PLAYER(frag_attacker))
289                 if(DEATH_ISWEAPON(frag_deathtype, WEP_VAPORIZER))
290                 {
291                         if(!autocvar_g_instagib_friendlypush && SAME_TEAM(frag_target, frag_attacker))
292                                 frag_force = '0 0 0';
293
294                         float armor = GetResourceAmount(frag_target, RESOURCE_ARMOR);
295                         if(armor)
296                         {
297                                 armor -= 1;
298                                 SetResourceAmount(frag_target, RESOURCE_ARMOR, armor);
299                                 frag_damage = 0;
300                                 frag_target.damage_dealt += 1;
301                                 frag_attacker.damage_dealt += 1;
302                                 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_INSTAGIB_LIVES_REMAINING, armor);
303                         }
304                 }
305
306                 if(IS_PLAYER(frag_attacker) && DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
307                 {
308                         if(frag_deathtype & HITTYPE_SECONDARY)
309                         {
310                                 if(!autocvar_g_instagib_blaster_keepdamage || frag_attacker == frag_target)
311                                 {
312                                         frag_damage = 0;
313                                         if(!autocvar_g_instagib_mirrordamage)
314                                                 frag_mirrordamage = 0; // never do mirror damage on enemies
315                                 }
316
317                                 if(frag_target != frag_attacker)
318                                 {
319                                         if(frag_damage <= 0 && GetResourceAmount(frag_target, RESOURCE_HEALTH) > 0) { Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_SECONDARY_NODAMAGE); }
320                                         if(!autocvar_g_instagib_blaster_keepforce)
321                                                 frag_force = '0 0 0';
322                                 }
323                         }
324                 }
325         }
326
327         if(!autocvar_g_instagib_mirrordamage) // only apply the taking lives hack if we don't want to support real damage mirroring
328         if(IS_PLAYER(frag_attacker))
329         if(frag_mirrordamage > 0)
330         {
331                 // just lose extra LIVES, don't kill the player for mirror damage
332                 float armor = GetResourceAmount(frag_attacker, RESOURCE_ARMOR);
333                 if(armor > 0)
334                 {
335                         armor -= 1;
336                         SetResourceAmount(frag_attacker, RESOURCE_ARMOR, armor);
337                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_INSTAGIB_LIVES_REMAINING, armor);
338                         frag_attacker.damage_dealt += frag_mirrordamage;
339                 }
340                 frag_mirrordamage = 0;
341         }
342
343         if(frag_target.alpha && frag_target.alpha < 1)
344         if(IS_PLAYER(frag_target))
345                 yoda = 1;
346
347         M_ARGV(4, float) = frag_damage;
348         M_ARGV(5, float) = frag_mirrordamage;
349         M_ARGV(6, vector) = frag_force;
350 }
351
352 MUTATOR_HOOKFUNCTION(mutator_instagib, SetStartItems)
353 {
354         start_health       = warmup_start_health       = 100;
355         start_armorvalue   = warmup_start_armorvalue   = 0;
356
357         start_ammo_shells  = warmup_start_ammo_shells  = 0;
358         start_ammo_nails   = warmup_start_ammo_nails   = 0;
359         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_instagib_ammo_start");
360         start_ammo_plasma  = warmup_start_ammo_plasma  = 0;
361         start_ammo_rockets = warmup_start_ammo_rockets = 0;
362         //start_ammo_fuel    = warmup_start_ammo_fuel    = 0;
363
364         start_weapons = warmup_start_weapons = WEPSET(VAPORIZER);
365         start_items |= IT_UNLIMITED_SUPERWEAPONS;
366 }
367
368 MUTATOR_HOOKFUNCTION(mutator_instagib, SetWeaponArena)
369 {
370         // turn weapon arena off
371         M_ARGV(0, string) = "off";
372 }
373
374 void replace_with_insta_cells(entity item)
375 {
376         entity e = new(item_vaporizer_cells);
377         setorigin(e, item.origin);
378         e.noalign = Item_ShouldKeepPosition(item);
379         e.cnt = item.cnt;
380         e.team = item.team;
381         e.spawnfunc_checked = true;
382         spawnfunc_item_vaporizer_cells(e);
383 }
384
385 MUTATOR_HOOKFUNCTION(mutator_instagib, FilterItem)
386 {
387         entity item = M_ARGV(0, entity);
388
389         if(item.classname == "item_cells")
390         {
391                 if(autocvar_g_instagib_ammo_convert_cells)
392                 {
393                         replace_with_insta_cells(item);
394                 }
395                 return true;
396         }
397         else if(item.classname == "item_rockets")
398         {
399                 if(autocvar_g_instagib_ammo_convert_rockets)
400                 {
401                         replace_with_insta_cells(item);
402                 }
403                 return true;
404         }
405         else if(item.classname == "item_shells")
406         {
407                 if(autocvar_g_instagib_ammo_convert_shells)
408                 {
409                         replace_with_insta_cells(item);
410                 }
411                 return true;
412         }
413         else if(item.classname == "item_bullets")
414         {
415                 if(autocvar_g_instagib_ammo_convert_bullets)
416                 {
417                         replace_with_insta_cells(item);
418                 }
419                 return true;
420         }
421
422         if(item.weapon == WEP_VAPORIZER.m_id && Item_IsLoot(item))
423         {
424                 SetResourceAmount(item, RESOURCE_CELLS, autocvar_g_instagib_ammo_drop);
425                 return false;
426         }
427
428         if(item.weapon == WEP_DEVASTATOR.m_id || item.weapon == WEP_VORTEX.m_id)
429         {
430                 replace_with_insta_cells(item);
431                 return true;
432         }
433
434         if(item.flags & FL_POWERUP)
435                 return false;
436
437         float cells = GetResourceAmount(item, RESOURCE_CELLS);
438         if(cells > autocvar_g_instagib_ammo_drop && item.classname != "item_vaporizer_cells")
439                 SetResourceAmount(item, RESOURCE_CELLS, autocvar_g_instagib_ammo_drop);
440
441         if(cells && !item.weapon)
442                 return false;
443
444         return true;
445 }
446
447 MUTATOR_HOOKFUNCTION(mutator_instagib, CustomizeWaypoint)
448 {
449         entity wp = M_ARGV(0, entity);
450         entity player = M_ARGV(1, entity);
451
452         entity e = WaypointSprite_getviewentity(player);
453
454         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
455         // but only apply this to real players, not to spectators
456         if((wp.owner.flags & FL_CLIENT) && (wp.owner.items & ITEM_Invisibility.m_itemid) && (e == player))
457         if(DIFF_TEAM(wp.owner, e))
458                 return true;
459 }
460
461 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerDies)
462 {
463         float frag_deathtype = M_ARGV(3, float);
464
465         if(DEATH_ISWEAPON(frag_deathtype, WEP_VAPORIZER))
466                 M_ARGV(4, float) = 1000; // always gib if it was a vaporizer death
467 }
468
469 MUTATOR_HOOKFUNCTION(mutator_instagib, ItemTouch)
470 {
471         entity item = M_ARGV(0, entity);
472         entity toucher = M_ARGV(1, entity);
473
474         if(GetResourceAmount(item, RESOURCE_CELLS))
475         {
476                 // play some cool sounds ;)
477                 float hp = GetResourceAmount(toucher, RESOURCE_HEALTH);
478                 if (IS_CLIENT(toucher))
479                 {
480                         if(hp <= 5)
481                                 Send_Notification(NOTIF_ONE, toucher, MSG_ANNCE, ANNCE_INSTAGIB_LASTSECOND);
482                         else if(hp < 50)
483                                 Send_Notification(NOTIF_ONE, toucher, MSG_ANNCE, ANNCE_INSTAGIB_NARROWLY);
484                 }
485
486                 if(hp < 100)
487                         SetResourceAmount(toucher, RESOURCE_HEALTH, 100);
488
489                 return MUT_ITEMTOUCH_CONTINUE;
490         }
491
492         if(item.itemdef == ITEM_ExtraLife)
493         {
494                 GiveResource(toucher, RESOURCE_ARMOR, autocvar_g_instagib_extralives);
495                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
496                 return MUT_ITEMTOUCH_PICKUP;
497         }
498
499         return MUT_ITEMTOUCH_CONTINUE;
500 }
501
502 MUTATOR_HOOKFUNCTION(mutator_instagib, OnEntityPreSpawn)
503 {
504         if (!autocvar_g_powerups) { return; }
505         entity ent = M_ARGV(0, entity);
506         // Can't use .itemdef here
507         if (!(ent.classname == "item_strength" || ent.classname == "item_shield" || ent.classname == "item_health_mega"))
508                 return;
509
510         entity e = spawn();
511
512         float r = random();
513         if (r < 0.3)
514         {
515                 e.classname = "item_invisibility";
516                 setthink(e, instagib_invisibility);
517         }
518         else if (r < 0.6)
519         {
520                 e.classname = "item_extralife";
521                 setthink(e, instagib_extralife);
522         }
523         else
524         {
525                 e.classname = "item_speed";
526                 setthink(e, instagib_speed);
527         }
528
529         e.nextthink = time + 0.1;
530         e.spawnflags = ent.spawnflags;
531         e.noalign = ent.noalign;
532         setorigin(e, ent.origin);
533
534         return true;
535 }
536
537 MUTATOR_HOOKFUNCTION(mutator_instagib, BuildMutatorsString)
538 {
539         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":instagib");
540 }
541
542 MUTATOR_HOOKFUNCTION(mutator_instagib, BuildMutatorsPrettyString)
543 {
544         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", instagib");
545 }
546
547 MUTATOR_HOOKFUNCTION(mutator_instagib, SetModname)
548 {
549         M_ARGV(0, string) = "InstaGib";
550         return true;
551 }