View Single Post
  #2  
Old 12-09-2010, 12:40 PM
Kayen
Developer
 
Join Date: Mar 2009
Location: -
Posts: 228
Default

Trev did a rewrite, either version works the same but this one I think is
a bit more efficient.


Code:
#Usage: plugin::ModSpecialAttack("Abilities", Remove?=0);
# Add or remove one or more special attacks to an NPC.
# Abilities is a special ATK value ie "A" or "AB" or "ABH" ect
# Remove is an optional field that defaults to 0 (add).  (1 = remove, 0 = add)
# Example1: plugin::ModSpecialAttack("ABH");
# If NPC as special ATK values set as "Qf" already, and you want to make it immune, this
# plugin will add ABH making your final value "QfABH"
# Returns the current special attack values ie would return "QfABH";
# Example 2: plugin::ModSpecialAttack("ABH", 1);
# Removes "ABH" from the NPC's special attacks, and using the example above, it would become "Qf" again.

sub ModSpecialAttack {
	my $npc = plugin::val('npc');
	my @ModAbilities = split(//, $_[0]);
	my $Remove = $_[1];
	
	# Build the array of the NPC's current ability settings
	my @CurAbilityArray = split(//, $CurAbilities);
	my @AbilityList = ("A","B","H","S","E","R","r","T","Q","U","M","C","N","I","D","f","O","W","g","d");
	foreach $Ability (@AbilityList)
	{
		my $HasAbility = $npc->HasNPCSpecialAtk($Ability);
		if ($HasAbility)
		{

			push (@CurAbilityArray, $Ability);
		}
	}

	my @FinalAbilityArray = ();

	if ($Remove)
	{
		# Removing Abilities
		
		# Check each of the currently set abilities
		foreach $CurAbility(@CurAbilityArray)
		{
			# Rebuild the array 1 ability at a time
			push (@FinalAbilityArray, $CurAbility);
			
			# Check each of the abilities to be removed against the current ones
			foreach $NewAbility (@ModAbilities)
			{
				if ($CurAbility eq $NewAbility)
				{
					# Ability found, so remove it from the array
					pop(@FinalAbilityArray);
				}
			}
		}
	}
	else
	{
		# Adding Abilities
		
		# Check each of the abilities to be added
		foreach $NewAbility (@ModAbilities)
		{
			my $AbilityFound = 0;
			# Check each of the currently set abilities against the ones to be added
			foreach $CurAbility (@CurAbilityArray)
			{
				# If the ability to be added already exists, mark it as found
				if ($CurAbility eq $NewAbility)
				{
					$AbilityFound = 1;
				}
			}
			
			# If the ability to be added was not found, add it to the array
			if (!$AbilityFound)
			{
				push (@CurAbilityArray, $NewAbility);
			}
		}
		
		# Set the final array to equal the current one with the new additions
		@FinalAbilityArray = @CurAbilityArray;
	}
	
	# Rebuild the final array into a string so it can be used in the command
	my $FinalAbilityList = "";
	foreach $FinalAbility (@FinalAbilityArray)
	{
		$FinalAbilityList = join('', $FinalAbilityList, $FinalAbility);
	}
	
	$npc->NPCSpecialAttacks($FinalAbilityList, 0);
	return $FinalAbilityList;
	
}

Last edited by trevius; 12-10-2010 at 12:54 AM..
Reply With Quote