L4D2 Vscript Helpers

From Valve Developer Community
Revision as of 11:46, 19 March 2022 by Eyal282 (talk | contribs)
Jump to navigation Jump to search

Sometimes an idea for a function is not immediately available in the List of L4D2 Script Functions

This function library was built to help you get more functions for your code.

/*
Eyal282's helper scripts:
*/

/**
 * Helper function to prevent witches from instakilling survivors. This is done by blocking the damage via AllowTakeDamage and manually setting the health to 0.
 */
function AllowTakeDamage(damageTable)
{
	local attacker = damageTable.Attacker;
	local victim = damageTable.Victim;

	if (attacker != null && victim != null && attacker.GetClassname() == "witch")
	{
		if(victim != null && victim.GetClassname() == "player" && victim.IsSurvivor() && !victim.IsIncapacitated())
		{
			DoEntFire( "!self", "SetHealth", "0", 0, victim, victim );
			damageTable.DamageDone = 0;
			return false;
		}
	}
}


/**
 * Helper function to equip adrenaline and pipe bomb found in safe room to avoid wasting time, happens on grabbing any item, or when pressing E on safe room door.
 * If someone has a tactic to make this run when the player spawns, please alter it.
 */
function OnGameEvent_player_use( params )
{

	local autoEquipItems =
	{
		weapon_adrenaline = "Adrenaline"
		weapon_pipe_bomb = "Pipe Bomb"
	}

	local player = GetPlayerFromUserID( params["userid"] );
	
	if ( player )
	{
		if(player.IsSurvivor() && IsEntityInStartSafeRoom(player))
		{
			foreach(key, val in autoEquipItems)
			{
				if(DoesPlayerHaveWeapon(player, key))
				{
					continue;
				}
				else
				{
					local weapon = null;
					while ( weapon = Entities.FindByClassname( weapon, key + "_spawn" ) )
					{
						if ( weapon.IsValid() )
						{
							if( IsEntityInStartSafeRoom(weapon) )
							{
								DoEntFire( "!self", "Use", "", 0, player, weapon );
								
								ClientPrint(player, 5, "Equipped " + val + " from safe room.");
								
								break;
							}
						}
					}
				
				}
			}
		}
	}
}
/**
 * Checks whether or not an entity is in the starting safe room
 * 
 * @param entity     Entity to check
 * @return           true if entity is in start safe room, false otherwise.
 * @notes			 This works for the safe area on the first chapter of a campaign.
 */
function IsEntityInStartSafeRoom(entity)
{
	local origin = entity.GetOrigin();
	
	local navArea = NavMesh.GetNearestNavArea(origin, 2048, true, true);

	if(navArea != null)
	{
		// https://developer.valvesoftware.com/wiki/List_of_L4D_Series_Nav_Mesh_Attributes
		if(navArea.HasSpawnAttributes(2048) && GetFlowPercentForPosition(origin, false) <= 50.0)
		{
			return true;
		}

	}

	return false;
}

/**
 * Checks whether or not an entity is in the ending safe room
 * 
 * @param entity     Entity to check
 * @return           true if entity is in end safe room, false otherwise.
 * @notes			 It is unknown whether or not this works for rescue vehicles.
 */
function IsEntityInEndSafeRoom(entity)
{
	local origin = entity.GetOrigin();
	
	local navArea = NavMesh.GetNearestNavArea(origin, 2048, true, true);

	if(navArea != null)
	{
		// https://developer.valvesoftware.com/wiki/List_of_L4D_Series_Nav_Mesh_Attributes
		if(navArea.HasSpawnAttributes(2048) && GetFlowPercentForPosition(origin, false) >= 50.0)
		{
			return true;
		}

	}

	return false;
}


/**
 * Checks whether or not an player owns a weapon by its classname
 * 
 * @param player     player to check
 * @param classname  classname string to check
 * @return           true if player has the weapon in inventory, false otherwise
 */
function DoesPlayerHaveWeapon(player, classname)
{
	local ent = null;

	while ( ent = Entities.FindByClassname( ent, classname ) )
	{
		if(ent.GetOwnerEntity() == player)
		{
			return true;
		}
	}

	return false;
}

/*
Anonymous's helper scripts:
*/

function IsMissionFinale()
{
	if(IsMissionFinalMap())
	{
		return true;
	}

	return false;

}