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