1 #include "sv_instagib.qh"
3 #include <server/client.qh>
4 #include <common/items/_mod.qh>
5 #include "../random_items/sv_random_items.qh"
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;
19 void instagib_invisibility(entity this)
21 this.strength_finished = autocvar_g_instagib_invisibility_time;
22 StartItem(this, ITEM_Invisibility);
25 void instagib_extralife(entity this)
27 StartItem(this, ITEM_ExtraLife);
30 void instagib_speed(entity this)
32 this.invincible_finished = autocvar_g_instagib_speed_time;
33 StartItem(this, ITEM_Speed);
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)
41 RandomSelection_Init();
42 IL_EACH(g_instagib_items, Item_IsDefinitionAllowed(it),
44 string cvar_name = sprintf("g_%s_%s_probability", prefix,
45 it.m_canonical_spawnfunc);
46 if (!(cvar_type(cvar_name) & CVAR_TYPEFLAG_EXISTS))
48 LOG_WARNF("Random items: cvar %s doesn't exist.", cvar_name);
51 RandomSelection_AddString(it.m_canonical_spawnfunc, cvar(cvar_name), 1);
53 return RandomSelection_chosen_string;
56 .float instagib_nextthink;
57 .float instagib_needammo;
58 void instagib_stop_countdown(entity e)
60 if (!e.instagib_needammo)
62 Kill_Notification(NOTIF_ONE_ONLY, e, MSG_CENTER, CPID_INSTAGIB_FINDAMMO);
63 e.instagib_needammo = false;
66 void instagib_countdown(entity this)
68 float hp = GetResource(this, RES_HEALTH);
70 float dmg = (hp <= 10) ? 5 : 10;
71 Damage(this, this, this, dmg, DEATH_NOAMMO.m_id, DMG_NOWEP, this.origin, '0 0 0');
73 entity annce = (hp <= 5) ? ANNCE_INSTAGIB_TERMINATED : Announcer_PickNumber(CNT_NORMAL, ceil(hp / 10));
74 Send_Notification(NOTIF_ONE, this, MSG_ANNCE, annce);
79 Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_INSTAGIB_FINDAMMO);
81 Send_Notification(NOTIF_ONE_ONLY, this, MSG_MULTI, MULTI_INSTAGIB_FINDAMMO);
85 void instagib_ammocheck(entity this)
87 if(time < this.instagib_nextthink)
90 return; // not a player
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)
98 if(!this.instagib_needammo)
100 Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_INSTAGIB_DOWNGRADE);
101 this.instagib_needammo = true;
106 this.instagib_needammo = true;
107 instagib_countdown(this);
109 this.instagib_nextthink = time + 1;
112 MUTATOR_HOOKFUNCTION(mutator_instagib, MatchEnd)
114 FOREACH_CLIENT(IS_PLAYER(it), { instagib_stop_countdown(it); });
117 MUTATOR_HOOKFUNCTION(mutator_instagib, RandomItems_GetRandomItemClassName)
119 M_ARGV(1, string) = RandomItems_GetRandomInstagibItemClassName(
124 MUTATOR_HOOKFUNCTION(mutator_instagib, MonsterDropItem)
126 entity item = M_ARGV(1, entity);
128 item.monster_loot = ITEM_VaporizerCells;
131 MUTATOR_HOOKFUNCTION(mutator_instagib, MonsterSpawn)
133 entity mon = M_ARGV(0, entity);
135 // always refill ammo
136 if(mon.monsterdef == MON_MAGE)
140 MUTATOR_HOOKFUNCTION(mutator_instagib, BotShouldAttack)
142 entity targ = M_ARGV(1, entity);
144 if (targ.items & ITEM_Invisibility.m_itemid)
148 MUTATOR_HOOKFUNCTION(mutator_instagib, MakePlayerObserver)
150 entity player = M_ARGV(0, entity);
152 instagib_stop_countdown(player);
155 MUTATOR_HOOKFUNCTION(mutator_instagib, ForbidRandomStartWeapons)
160 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerSpawn)
162 entity player = M_ARGV(0, entity);
164 player.effects |= EF_FULLBRIGHT;
167 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerPreThink)
169 entity player = M_ARGV(0, entity);
171 instagib_ammocheck(player);
174 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerRegen)
176 // no regeneration in instagib
180 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerPowerups)
182 entity player = M_ARGV(0, entity);
184 if (!(player.effects & EF_FULLBRIGHT))
185 player.effects |= EF_FULLBRIGHT;
187 if (player.items & ITEM_Invisibility.m_itemid)
189 play_countdown(player, STAT(STRENGTH_FINISHED, player), SND_POWEROFF);
190 if (time > STAT(STRENGTH_FINISHED, player))
192 if(!player.vehicle) // already reset upon exit
194 player.alpha = default_player_alpha;
195 player.exteriorweaponentity.alpha = default_weapon_alpha;
197 player.items &= ~ITEM_Invisibility.m_itemid;
198 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_POWERDOWN_INVISIBILITY);
203 if (time < STAT(STRENGTH_FINISHED, player))
205 if(!player.vehicle) // incase the player is given powerups while inside a vehicle
207 player.alpha = autocvar_g_instagib_invis_alpha;
208 player.exteriorweaponentity.alpha = autocvar_g_instagib_invis_alpha;
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);
216 if (player.items & ITEM_Speed.m_itemid)
218 play_countdown(player, STAT(INVINCIBLE_FINISHED, player), SND_POWEROFF);
219 if (time > STAT(INVINCIBLE_FINISHED, player))
221 player.items &= ~ITEM_Speed.m_itemid;
222 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_POWERDOWN_SPEED);
227 if (time < STAT(INVINCIBLE_FINISHED, player))
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);
236 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerPhysics_UpdateStats)
238 entity player = M_ARGV(0, entity);
239 // these automatically reset, no need to worry
241 if(player.items & ITEM_Speed.m_itemid)
242 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_instagib_speed_highspeed;
245 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerDamage_SplitHealthArmor)
247 M_ARGV(4, float) = M_ARGV(7, float); // take = damage
248 M_ARGV(5, float) = 0; // save
251 MUTATOR_HOOKFUNCTION(mutator_instagib, ForbidThrowCurrentWeapon)
253 // weapon dropping on death handled by FilterItem
257 MUTATOR_HOOKFUNCTION(mutator_instagib, Damage_Calculate)
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);
266 if(autocvar_g_friendlyfire == 0 && SAME_TEAM(frag_target, frag_attacker) && IS_PLAYER(frag_target) && IS_PLAYER(frag_attacker))
269 if(IS_PLAYER(frag_target))
271 if(frag_deathtype == DEATH_FALL.m_id)
272 frag_damage = 0; // never count fall damage
274 if(!autocvar_g_instagib_damagedbycontents)
275 switch(DEATH_ENT(frag_deathtype))
284 if(IS_PLAYER(frag_attacker))
285 if(DEATH_ISWEAPON(frag_deathtype, WEP_VAPORIZER))
287 if(!autocvar_g_instagib_friendlypush && SAME_TEAM(frag_target, frag_attacker))
288 frag_force = '0 0 0';
290 float armor = GetResource(frag_target, RES_ARMOR);
294 SetResource(frag_target, RES_ARMOR, armor);
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);
302 if(IS_PLAYER(frag_attacker) && DEATH_ISWEAPON(frag_deathtype, WEP_BLASTER))
304 if(frag_deathtype & HITTYPE_SECONDARY)
306 if(!autocvar_g_instagib_blaster_keepdamage || frag_attacker == frag_target)
309 if(!autocvar_g_instagib_mirrordamage)
310 frag_mirrordamage = 0; // never do mirror damage on enemies
313 if(frag_target != frag_attacker)
315 if(!autocvar_g_instagib_blaster_keepforce)
316 frag_force = '0 0 0';
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)
326 // just lose extra LIVES, don't kill the player for mirror damage
327 float armor = GetResource(frag_attacker, RES_ARMOR);
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;
335 frag_mirrordamage = 0;
338 if(frag_target.alpha && frag_target.alpha < 1)
339 if(IS_PLAYER(frag_target))
342 M_ARGV(4, float) = frag_damage;
343 M_ARGV(5, float) = frag_mirrordamage;
344 M_ARGV(6, vector) = frag_force;
347 MUTATOR_HOOKFUNCTION(mutator_instagib, SetStartItems, CBC_ORDER_LAST)
349 start_health = warmup_start_health = 100;
350 start_armorvalue = warmup_start_armorvalue = 0;
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;
359 start_weapons = warmup_start_weapons = WEPSET(VAPORIZER);
360 start_items |= IT_UNLIMITED_SUPERWEAPONS;
363 MUTATOR_HOOKFUNCTION(mutator_instagib, SetWeaponArena)
365 // turn weapon arena off
366 M_ARGV(0, string) = "off";
369 void replace_with_insta_cells(entity item)
371 entity e = new(item_vaporizer_cells);
372 setorigin(e, item.origin);
373 e.noalign = Item_ShouldKeepPosition(item);
376 e.spawnfunc_checked = true;
377 spawnfunc_item_vaporizer_cells(e);
380 MUTATOR_HOOKFUNCTION(mutator_instagib, FilterItem)
382 entity item = M_ARGV(0, entity);
384 if(item.classname == "item_cells")
386 if(autocvar_g_instagib_ammo_convert_cells)
388 replace_with_insta_cells(item);
392 else if(item.classname == "item_rockets")
394 if(autocvar_g_instagib_ammo_convert_rockets)
396 replace_with_insta_cells(item);
400 else if(item.classname == "item_shells")
402 if(autocvar_g_instagib_ammo_convert_shells)
404 replace_with_insta_cells(item);
408 else if(item.classname == "item_bullets")
410 if(autocvar_g_instagib_ammo_convert_bullets)
412 replace_with_insta_cells(item);
417 if(item.weapon == WEP_VAPORIZER.m_id && Item_IsLoot(item))
419 SetResource(item, RES_CELLS, autocvar_g_instagib_ammo_drop);
423 if(item.weapon == WEP_DEVASTATOR.m_id || item.weapon == WEP_VORTEX.m_id)
425 replace_with_insta_cells(item);
429 if(item.flags & FL_POWERUP)
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);
436 if(cells && !item.weapon)
442 MUTATOR_HOOKFUNCTION(mutator_instagib, CustomizeWaypoint)
444 entity wp = M_ARGV(0, entity);
445 entity player = M_ARGV(1, entity);
447 entity e = WaypointSprite_getviewentity(player);
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))
456 MUTATOR_HOOKFUNCTION(mutator_instagib, PlayerDies)
458 float frag_deathtype = M_ARGV(3, float);
460 if(DEATH_ISWEAPON(frag_deathtype, WEP_VAPORIZER))
461 M_ARGV(4, float) = 1000; // always gib if it was a vaporizer death
464 MUTATOR_HOOKFUNCTION(mutator_instagib, ItemTouch)
466 if(MUTATOR_RETURNVALUE) return false;
468 entity item = M_ARGV(0, entity);
469 entity toucher = M_ARGV(1, entity);
471 if(GetResource(item, RES_CELLS))
473 // play some cool sounds ;)
474 float hp = GetResource(toucher, RES_HEALTH);
475 if (IS_CLIENT(toucher))
478 Send_Notification(NOTIF_ONE, toucher, MSG_ANNCE, ANNCE_INSTAGIB_LASTSECOND);
480 Send_Notification(NOTIF_ONE, toucher, MSG_ANNCE, ANNCE_INSTAGIB_NARROWLY);
484 SetResource(toucher, RES_HEALTH, 100);
486 return MUT_ITEMTOUCH_CONTINUE;
489 if(item.itemdef == ITEM_ExtraLife)
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;
496 return MUT_ITEMTOUCH_CONTINUE;
499 MUTATOR_HOOKFUNCTION(mutator_instagib, OnEntityPreSpawn)
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"))
513 e.classname = "item_invisibility";
514 setthink(e, instagib_invisibility);
518 e.classname = "item_extralife";
519 setthink(e, instagib_extralife);
523 e.classname = "item_speed";
524 setthink(e, instagib_speed);
527 e.nextthink = time + 0.1;
528 e.spawnflags = ent.spawnflags;
529 e.noalign = ent.noalign;
530 setorigin(e, ent.origin);
535 MUTATOR_HOOKFUNCTION(mutator_instagib, BuildMutatorsString)
537 M_ARGV(0, string) = strcat(M_ARGV(0, string), ":instagib");
540 MUTATOR_HOOKFUNCTION(mutator_instagib, BuildMutatorsPrettyString)
542 M_ARGV(0, string) = strcat(M_ARGV(0, string), ", instagib");
545 MUTATOR_HOOKFUNCTION(mutator_instagib, SetModname)
547 M_ARGV(0, string) = "InstaGib";