1 #include "sv_instagib.qh"
3 #include <server/client.qh>
4 #include <common/items/_mod.qh>
5 #include <common/mutators/mutator/powerups/_mod.qh>
6 #include <common/mutators/mutator/status_effects/_mod.qh>
7 #include "../random_items/sv_random_items.qh"
9 bool autocvar_g_instagib_damagedbycontents = true;
10 bool autocvar_g_instagib_blaster_keepdamage = false;
11 bool autocvar_g_instagib_blaster_keepforce = false;
12 bool autocvar_g_instagib_mirrordamage;
13 bool autocvar_g_instagib_friendlypush = true;
14 //int autocvar_g_instagib_ammo_drop;
15 bool autocvar_g_instagib_ammo_convert_cells;
16 bool autocvar_g_instagib_ammo_convert_rockets;
17 bool autocvar_g_instagib_ammo_convert_shells;
18 bool autocvar_g_instagib_ammo_convert_bullets;
20 void instagib_invisibility(entity this)
22 this.invisibility_finished = autocvar_g_instagib_invisibility_time;
23 StartItem(this, ITEM_Invisibility);
26 void instagib_extralife(entity this)
28 StartItem(this, ITEM_ExtraLife);
31 void instagib_speed(entity this)
33 this.speed_finished = autocvar_g_instagib_speed_time;
34 StartItem(this, ITEM_Speed);
37 /// \brief Returns a random classname of the instagib item.
38 /// \param[in] prefix Prefix of the cvars that hold probabilities.
39 /// \return Random classname of the instagib item.
40 string RandomItems_GetRandomInstagibItemClassName(string prefix)
42 RandomSelection_Init();
43 IL_EACH(g_instagib_items, Item_IsDefinitionAllowed(it),
45 string cvar_name = sprintf("g_%s_%s_probability", prefix,
46 it.m_canonical_spawnfunc);
47 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
49 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
52 RandomSelection_AddString(it.m_canonical_spawnfunc, cvar(cvar_name), 1);
54 return RandomSelection_chosen_string;
57 .float instagib_nextthink;
58 .float instagib_needammo;
59 void instagib_stop_countdown(entity e)
61 if (!e.instagib_needammo)
63 Kill_Notification(NOTIF_ONE_ONLY, e, MSG_CENTER, CPID_INSTAGIB_FINDAMMO);
64 e.instagib_needammo = false;
67 void instagib_countdown(entity this)
69 float hp = GetResource(this, RES_HEALTH);
71 float dmg = (hp <= 10) ? 5 : 10;
72 Damage(this, this, this, dmg, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
74 entity annce = (hp <= 5) ? ANNCE_INSTAGIB_TERMINATED : Announcer_PickNumber(CNT_NORMAL, ceil(hp / 10));
75 Send_Notification(NOTIF_ONE, this, MSG_ANNCE, annce);
80 Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_INSTAGIB_FINDAMMO);
82 Send_Notification(NOTIF_ONE_ONLY, this, MSG_MULTI, MULTI_INSTAGIB_FINDAMMO);
86 void instagib_ammocheck(entity this)
88 if(time < this.instagib_nextthink)
91 return; // not a player
93 if(IS_DEAD(this) || game_stopped)
94 instagib_stop_countdown(this);
95 else if (GetResource(this, RES_CELLS) > 0 || (this.items & IT_UNLIMITED_AMMO) || (this.flags & FL_GODMODE))
96 instagib_stop_countdown(this);
97 else if(autocvar_g_rm && autocvar_g_rm_laser)
99 if(!this.instagib_needammo)
101 Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_INSTAGIB_DOWNGRADE);
102 this.instagib_needammo = true;
107 this.instagib_needammo = true;
108 instagib_countdown(this);
110 this.instagib_nextthink = time + 1;
113 MUTATOR_HOOKFUNCTION(mutator_instagib, MatchEnd)
115 FOREACH_CLIENT(IS_PLAYER(it), { instagib_stop_countdown(it); });
118 MUTATOR_HOOKFUNCTION(mutator_instagib, RandomItems_GetRandomItemClassName)
120 M_ARGV(1, string) = RandomItems_GetRandomInstagibItemClassName(
125 MUTATOR_HOOKFUNCTION(mutator_instagib, MonsterDropItem)
127 entity item = M_ARGV(1, entity);
129 item.monster_loot = ITEM_VaporizerCells;
132 MUTATOR_HOOKFUNCTION(mutator_instagib, MonsterSpawn)
134 entity mon = M_ARGV(0, entity);
136 // always refill ammo
137 if(mon.monsterdef == MON_MAGE)
141 MUTATOR_HOOKFUNCTION(mutator_instagib, MakePlayerObserver)
143 entity player = M_ARGV(0, entity);
145 instagib_stop_countdown(player);
148 MUTATOR_HOOKFUNCTION(mutator_instagib, ForbidRandomStartWeapons)
153 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerSpawn)
155 entity player = M_ARGV(0, entity);
157 player.effects |= EF_FULLBRIGHT;
160 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerPreThink)
162 entity player = M_ARGV(0, entity);
164 instagib_ammocheck(player);
167 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerRegen)
169 // no regeneration in instagib
173 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerDamage_SplitHealthArmor)
175 M_ARGV(4, float) = M_ARGV(7, float); // take = damage
176 M_ARGV(5, float) = 0; // save
179 MUTATOR_HOOKFUNCTION(mutator_instagib, ForbidThrowCurrentWeapon)
181 // weapon dropping on death handled by FilterItem
185 MUTATOR_HOOKFUNCTION(mutator_instagib, Damage_Calculate)
187 entity frag_attacker = M_ARGV(1, entity);
188 entity frag_target = M_ARGV(2, entity);
189 float frag_deathtype = M_ARGV(3, float);
190 float frag_damage = M_ARGV(4, float);
191 float frag_mirrordamage = M_ARGV(5, float);
192 vector frag_force = M_ARGV(6, vector);
194 if(autocvar_g_friendlyfire == 0 && SAME_TEAM(frag_target, frag_attacker) && IS_PLAYER(frag_target) && IS_PLAYER(frag_attacker))
197 if(IS_PLAYER(frag_target))
199 if(frag_deathtype == DEATH_FALL.m_id)
200 frag_damage = 0; // never count fall damage
202 if(!autocvar_g_instagib_damagedbycontents)
203 switch(DEATH_ENT(frag_deathtype))
212 if(IS_PLAYER(frag_attacker))
213 if(DEATH_ISWEAPON(frag_deathtype, WEP_VAPORIZER))
215 if(!autocvar_g_instagib_friendlypush && SAME_TEAM(frag_target, frag_attacker))
216 frag_force = '0 0 0';
218 float armor = GetResource(frag_target, RES_ARMOR);
222 SetResource(frag_target, RES_ARMOR, armor);
224 frag_target.hitsound_damage_dealt += 1;
225 frag_attacker.hitsound_damage_dealt += 1;
226 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_INSTAGIB_LIVES_REMAINING, armor);
230 if(IS_PLAYER(frag_attacker) && DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
232 if(frag_deathtype & HITTYPE_SECONDARY)
234 if(!autocvar_g_instagib_blaster_keepdamage || frag_attacker == frag_target)
237 if(!autocvar_g_instagib_mirrordamage)
238 frag_mirrordamage = 0; // never do mirror damage on enemies
241 if(frag_target != frag_attacker)
243 if(!autocvar_g_instagib_blaster_keepforce)
244 frag_force = '0 0 0';
250 if(!autocvar_g_instagib_mirrordamage) // only apply the taking lives hack if we don't want to support real damage mirroring
251 if(IS_PLAYER(frag_attacker))
252 if(frag_mirrordamage > 0)
254 // just lose extra LIVES, don't kill the player for mirror damage
255 float armor = GetResource(frag_attacker, RES_ARMOR);
259 SetResource(frag_attacker, RES_ARMOR, armor);
260 Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_INSTAGIB_LIVES_REMAINING, armor);
261 frag_attacker.hitsound_damage_dealt += frag_mirrordamage;
263 frag_mirrordamage = 0;
266 if(frag_target.alpha && frag_target.alpha < 1)
267 if(IS_PLAYER(frag_target))
270 M_ARGV(4, float) = frag_damage;
271 M_ARGV(5, float) = frag_mirrordamage;
272 M_ARGV(6, vector) = frag_force;
275 MUTATOR_HOOKFUNCTION(mutator_instagib, SetStartItems, CBC_ORDER_LAST)
277 start_health = warmup_start_health = 100;
278 start_armorvalue = warmup_start_armorvalue = 0;
280 start_ammo_shells = warmup_start_ammo_shells = 0;
281 start_ammo_nails = warmup_start_ammo_nails = 0;
282 start_ammo_cells = warmup_start_ammo_cells = cvar("g_instagib_ammo_start");
283 start_ammo_plasma = warmup_start_ammo_plasma = 0;
284 start_ammo_rockets = warmup_start_ammo_rockets = 0;
285 //start_ammo_fuel = warmup_start_ammo_fuel = 0;
287 start_weapons = warmup_start_weapons = WEPSET(VAPORIZER);
288 start_items |= IT_UNLIMITED_SUPERWEAPONS;
291 MUTATOR_HOOKFUNCTION(mutator_instagib, SetWeaponArena)
293 // turn weapon arena off
294 M_ARGV(0, string) = "off";
297 void replace_with_insta_cells(entity item)
299 entity e = new(item_vaporizer_cells);
300 setorigin(e, item.origin);
301 e.noalign = Item_ShouldKeepPosition(item);
304 e.spawnfunc_checked = true;
305 spawnfunc_item_vaporizer_cells(e);
308 MUTATOR_HOOKFUNCTION(mutator_instagib, FilterItem)
310 entity item = M_ARGV(0, entity);
312 if(item.classname == "item_cells")
314 if(autocvar_g_instagib_ammo_convert_cells)
316 replace_with_insta_cells(item);
320 else if(item.classname == "item_rockets")
322 if(autocvar_g_instagib_ammo_convert_rockets)
324 replace_with_insta_cells(item);
328 else if(item.classname == "item_shells")
330 if(autocvar_g_instagib_ammo_convert_shells)
332 replace_with_insta_cells(item);
336 else if(item.classname == "item_bullets")
338 if(autocvar_g_instagib_ammo_convert_bullets)
340 replace_with_insta_cells(item);
345 if(item.weapon == WEP_VAPORIZER.m_id && Item_IsLoot(item))
347 SetResource(item, RES_CELLS, autocvar_g_instagib_ammo_drop);
351 if(item.weapon == WEP_DEVASTATOR.m_id || item.weapon == WEP_VORTEX.m_id)
353 replace_with_insta_cells(item);
357 if(item.itemdef.instanceOfPowerup)
360 float cells = GetResource(item, RES_CELLS);
361 if(cells > autocvar_g_instagib_ammo_drop && item.classname != "item_vaporizer_cells")
362 SetResource(item, RES_CELLS, autocvar_g_instagib_ammo_drop);
364 if(cells && !item.weapon)
370 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerDies)
372 float frag_deathtype = M_ARGV(3, float);
374 if(DEATH_ISWEAPON(frag_deathtype, WEP_VAPORIZER))
375 M_ARGV(4, float) = 1000; // always gib if it was a vaporizer death
378 MUTATOR_HOOKFUNCTION(mutator_instagib, ItemTouch)
380 if(MUTATOR_RETURNVALUE) return false;
382 entity item = M_ARGV(0, entity);
383 entity toucher = M_ARGV(1, entity);
385 if(GetResource(item, RES_CELLS))
387 // play some cool sounds ;)
388 float hp = GetResource(toucher, RES_HEALTH);
389 if (IS_CLIENT(toucher))
392 Send_Notification(NOTIF_ONE, toucher, MSG_ANNCE, ANNCE_INSTAGIB_LASTSECOND);
394 Send_Notification(NOTIF_ONE, toucher, MSG_ANNCE, ANNCE_INSTAGIB_NARROWLY);
398 SetResource(toucher, RES_HEALTH, 100);
400 return MUT_ITEMTOUCH_CONTINUE;
403 if(item.itemdef == ITEM_ExtraLife)
405 GiveResource(toucher, RES_ARMOR, autocvar_g_instagib_extralives);
406 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_instagib_extralives);
407 return MUT_ITEMTOUCH_PICKUP;
410 return MUT_ITEMTOUCH_CONTINUE;
413 MUTATOR_HOOKFUNCTION(mutator_instagib, OnEntityPreSpawn)
415 if (MUTATOR_RETURNVALUE) return false;
416 if (!autocvar_g_powerups) { return; }
417 entity ent = M_ARGV(0, entity);
418 // Can't use .itemdef here
419 if (!(ent.classname == "item_strength" || ent.classname == "item_shield" || ent.classname == "item_invincible" || ent.classname == "item_health_mega"))
427 e.classname = "item_invisibility";
428 setthink(e, instagib_invisibility);
432 e.classname = "item_extralife";
433 setthink(e, instagib_extralife);
437 e.classname = "item_speed";
438 setthink(e, instagib_speed);
441 e.nextthink = time + 0.1;
442 e.spawnflags = ent.spawnflags;
443 e.noalign = ent.noalign;
444 setorigin(e, ent.origin);
449 MUTATOR_HOOKFUNCTION(mutator_instagib, BuildMutatorsString)
451 M_ARGV(0, string) = strcat(M_ARGV(0, string), ":instagib");
454 MUTATOR_HOOKFUNCTION(mutator_instagib, BuildMutatorsPrettyString)
456 M_ARGV(0, string) = strcat(M_ARGV(0, string), ", InstaGib");
459 MUTATOR_HOOKFUNCTION(mutator_instagib, SetModname)
461 M_ARGV(0, string) = "InstaGib";