using something very close to this to verify that the npc can equip the item in question before the call to $npc->AddItem(). read: no more augfists, yay!
it can be modified for use as an additional validation step while picking the random item (filtering out items that are not usable by an participant in the encounter, for example).
this was fun! (no sarcasm)
Code:
use 5.012;
no strict 'vars';
use constant {
MIN_ITEMID => 1001,
MAX_ITEMID => 132475,
};
# class patterns used to build regular expressions for bit values
my $war = '1|war(?:rior)?';
my $clr = '2|cl(?:eric|r)';
my $pal = '3|pal(?:adin)';
my $rng = '4|r(?:anger|ng)';
my $shd = '5|sh(?:adowknight|d)';
my $dru = '6|dru(?:id)?';
my $mnk = '7|m(?:o)?nk';
my $brd = '8|b(?:a)?rd';
my $rog = '9|rog(?:ue)?';
my $shm = '10|sh(?:aman|m)';
my $nec = '11|nec(?:romancer)?';
my $wiz = '12|wiz(?:ard)?';
my $mag = '13|mag(?:ician)?';
my $enc = '14|enc(?:hanter)?';
my $bst = '15|b(?:eastlord|st)';
my $ber = '16|ber(?:serker)?';
# race patterns used to build regular expressions for bit values
my $hum = '1|hum(?:an)?';
my $bar = '2|bar(?:barian)?';
my $eru = '3|eru(?:dite)?';
my $elf = '4|(?:wood )?elf';
my $hie = '5|hi(?:gh elf|e)';
my $def = '6|d(?:ark elf|ef)';
my $hel = '7|h(?:alf-elf|el)';
my $dwf = '8|dw(?:ar)?f';
my $trl = '9|tr(?:ol)?l';
my $ogr = '10|ogr(?:e)?';
my $hlf = '11|h(?:alfling|lf)';
my $gnm = '12|gn(?:ome|m)';
my $iks = '128|iks(?:ar)?';
my $vah = '130|vah(?: shir)?';
my $frg = '330|fr(?:oglok|g)';
my $drk = '522|dr(?:akkin|k)';
# get bit value for any playable race/class you pass it
# works with numeric values, abbreviations, and full spellings
sub GetBitFor {
my $bit = {
1 => qr/^$war|$hum$/i,
2 => qr/^$clr|$bar$/i,
4 => qr/^$pal|$eru$/i,
8 => qr/^$rng|$elf$/i,
16 => qr/^$shd|$hie$/i,
32 => qr/^$dru|$def$/i,
64 => qr/^$mnk|$hel$/i,
128 => qr/^$brd|$dwf$/i,
256 => qr/^$rog|$trl$/i,
512 => qr/^$shm|$ogr$/i,
1024 => qr/^$nec|$hlf$/i,
2048 => qr/^$wiz|$gnm$/i,
4096 => qr/^$mag|$iks$/i,
8192 => qr/^$enc|$vah$/i,
16384 => qr/^$bst|$frg$/i,
32768 => qr/^$ber|$drk$/i,
};
my $for = shift;
while ( my ( $key, $val ) = each %$bit ) {
return $key if $for ~~ $val;
}
return 0;
}
sub CanEquip {
# itemid to check
my $itemid = shift;
# coderef for $npc->GetItemStat()
my $GISref = sub { $npc->GetItemStat(@_) };
# list of tests we want to do, in order of importance
# 1. is itemid within valid range?
# 2. is itemid tied to a valid item (some id number gaps exist)?
# 3. is itemid able to be equipped at all?
# 4. is itemid not an aug?
# 5. is itemid not deity specific?
# 6. is itemid's required level less than or equal to my level?
# 7. is itemid's class restriction compatible with my class?
# 8. is itemid's race restriction compatible with my class?
my $test = [
sub { shift ~~ [ MIN_ITEMID .. MAX_ITEMID ] },
sub { quest::varlink(shift) !~ /INVALID/ },
sub { $GISref->( shift, "slots" ) },
sub { !$GISref->( shift, "augtype" ) },
sub { !$GISref->( shift, "deity" ) },
sub { $GISref->( shift, "reqlevel" ) <= $mlevel },
sub { $GISref->( shift, "classes" ) & GetBitFor( $npc->GetClass() ) },
sub { $GISref->( shift, "races" ) & GetBitFor( $npc->GetRace() ) },
];
# run through tests, stopping at first failure
foreach (@$test) {
return 0 unless $_->($itemid);
}
# passed all tests
return 1;
}