PDA

View Full Version : plugin::ModSpecialAttack


Kayen
12-09-2010, 01:59 AM
Instead of having to redefine the entire special attack string every time
you want to make a change this will allow on the fly changes without worrying
about having to account for what the npc's current settings are.

#Usage: plugin::ModSpecialAttack("$Mod", "Ability");
#Add or remove one or more special attacks to an NPC.
#Where "$Mod" is either "Add" or "Remove";
#Where "Ability" is a special ATK value ie "A" or "AB" or "ABH" ect
#Example: ModSpecialAttack("Add", "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";
#Kayen [GM Storm Haven] 12/09/10

sub ModSpecialAttack {

sub remove_duplicates {
my($array, $new_array)=@_;
my %entries=();
foreach (@$array) {
next if exists $entries{$_};
$entries{$_}=1}
foreach (@$array) {
if($entries{$_}){
push @$new_array,$_;
$entries{$_}=0;
}}}


my $npc = plugin::val('npc');
my $Mod = $_[0];
my $Ability = $_[1];

my $SATK_A = $npc->HasNPCSpecialAtk("A"); if ($SATK_A) { $SATK_A = "A"; }
my $SATK_B = $npc->HasNPCSpecialAtk("B"); if ($SATK_B) { $SATK_B = "B"; }
my $SATK_H = $npc->HasNPCSpecialAtk("H"); if ($SATK_H) { $SATK_H = "H"; }

my $SATK_S = $npc->HasNPCSpecialAtk("S"); if ($SATK_S) { $SATK_S = "S"; }
my $SATK_E = $npc->HasNPCSpecialAtk("E"); if ($SATK_E) { $SATK_E = "E"; }
my $SATK_R = $npc->HasNPCSpecialAtk("R"); if ($SATK_R) { $SATK_R = "R"; }
my $SATK_r = $npc->HasNPCSpecialAtk("r"); if ($SATK_r) { $SATK_r = "r"; }
my $SATK_T = $npc->HasNPCSpecialAtk("T"); if ($SATK_T) { $SATK_T = "T"; }
my $SATK_Q = $npc->HasNPCSpecialAtk("Q"); if ($SATK_Q) { $SATK_Q = "Q"; }
my $SATK_U = $npc->HasNPCSpecialAtk("U"); if ($SATK_U) { $SATK_U = "U"; }
my $SATK_M = $npc->HasNPCSpecialAtk("M"); if ($SATK_M) { $SATK_M = "M"; }
my $SATK_C = $npc->HasNPCSpecialAtk("C"); if ($SATK_C) { $SATK_C = "C"; }
my $SATK_N = $npc->HasNPCSpecialAtk("N"); if ($SATK_N) { $SATK_N = "N"; }
my $SATK_I = $npc->HasNPCSpecialAtk("I"); if ($SATK_I) { $SATK_I = "I"; }
my $SATK_D = $npc->HasNPCSpecialAtk("D"); if ($SATK_D) { $SATK_D = "D"; }
my $SATK_f = $npc->HasNPCSpecialAtk("f"); if ($SATK_f) { $SATK_f = "f"; }
my $SATK_O = $npc->HasNPCSpecialAtk("O"); if ($SATK_O) { $SATK_O = "O"; }
my $SATK_W = $npc->HasNPCSpecialAtk("W"); if ($SATK_W) { $SATK_W = "W"; }
my $SATK_g = $npc->HasNPCSpecialAtk("g"); if ($SATK_g) { $SATK_g = "g"; } #Must be close to land spells
my $SATK_d = $npc->HasNPCSpecialAtk("d"); if ($SATK_d) { $SATK_d = "d"; } #Immune to FD

if ($Mod eq "Remove") {
@charas = split(//, $Ability);
foreach $letter (@charas) {

if ($letter eq "A") { undef $SATK_A; }
if ($letter eq "B") { undef $SATK_B; }
if ($letter eq "H") { undef $SATK_H; }

if ($letter eq "S") { undef $SATK_S; }
if ($letter eq "E") { undef $SATK_E; }
if ($letter eq "R") { undef $SATK_R; }
if ($letter eq "r") { undef $SATK_r; }
if ($letter eq "T") { undef $SATK_T; }
if ($letter eq "Q") { undef $SATK_Q; }
if ($letter eq "U") { undef $SATK_U; }
if ($letter eq "M") { undef $SATK_M; }
if ($letter eq "C") { undef $SATK_C; }
if ($letter eq "N") { undef $SATK_N; }
if ($letter eq "I") { undef $SATK_I; }
if ($letter eq "D") { undef $SATK_D; }
if ($letter eq "f") { undef $SATK_f; }
if ($letter eq "O") { undef $SATK_O; }
if ($letter eq "W") { undef $SATK_W; }
if ($letter eq "g") { undef $SATK_g; } #Must be close to land spells
if ($letter eq "d") { undef $SATK_d; } #Immune to FD
}
}

my $SpecialATK_Var = "$SATK_A$SATK_B$SATK_H$SATK_S$SATK_E$SATK_R$SATK_r$ SATK_T$SATK_Q$SATK_U$SATK_M$SATK_C$SATK_N$SATK_I$S ATK_D$SATK_f$SATK_O$SATK_W$SATK_g$SATK_d";
my $Final_Value;

if ($Mod eq "Add") {
my $SpecialATK_Var_New = "$SpecialATK_Var$Ability";
@charas = split(//, $SpecialATK_Var_New);
my $array=[@charas];
my $new_array=[];
remove_duplicates($array, $new_array);
$npc->NPCSpecialAttacks(@$new_array, 0);
$Final_Value = @$new_array;
#plugin::Debug("Special Abilities: ADD [@$new_array]");
}

if ($Mod eq "Remove") {
$npc->NPCSpecialAttacks($SpecialATK_Var, 0);
$Final_Value = $SpecialATK_Var;
#plugin::Debug("Special Abilities: Removed [$SpecialATK_Var]");
}



return $Final_Value;

}

Kayen
12-09-2010, 12:40 PM
Trev did a rewrite, either version works the same but this one I think is
a bit more efficient.


#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;

}

Akkadius
12-10-2010, 02:12 AM
Added, I am on vacation to Alaska sorry about the late add.

Great addition, this is another great example of what plugins can do for the community.

I will get to making a compilation of all the plugins in a stickied cheat sheet soon.