View Single Post
  #1  
Old 01-29-2013, 02:09 PM
c0ncrete's Avatar
c0ncrete
Dragon
 
Join Date: Dec 2009
Posts: 719
Default get playable race/class equipment bit

Code:
# returns bit value for playable races and classes 
# usage: plugin::GetEquipmentBitForRaceOrClass($roc)
sub GetEquipmentBitForRaceOrClass {

    # bit values with matching regex for race/class
    # matches number value, full spelling, or abbreviation
    my $bit = {
        1     => qr/^1|war(?:rior)?|hum(?:an)?$/i,
        2     => qr/^2|cl(?:eric|r)|bar(?:barian)?$/i,
        4     => qr/^3|pal(?:adin)|eru(?:dite)?$/i,
        8     => qr/^4|r(?:anger|ng)|(?:wood )?elf$/i,
        16    => qr/^5|sh(?:adowknight|d)|hi(?:gh elf|e)$/i,
        32    => qr/^6|dru(?:id)?|d(?:ark elf|ef)$/i,
        64    => qr/^7|m(?:o)?nk|h(?:alf-elf|el)$/i,
        128   => qr/^8|b(?:a)?rd|dw(?:ar)?f$/i,
        256   => qr/^9|rog(?:ue)?|tr(?:ol)?l$/i,
        512   => qr/^10|sh(?:aman|m)|ogr(?:e)?$/i,
        1024  => qr/^11|nec(?:romancer)?|h(?:alfling|lf)$/i,
        2048  => qr/^12|wiz(?:ard)?|gn(?:ome|m)$/i,
        4096  => qr/^1(?:3|28)|mag(?:ician)?|iks(?:ar)?$/i,
        8192  => qr/^1(?:4|30)|enc(?:hanter)?|vah(?: shir)?$/i,
        16384 => qr/^15|330|b(?:eastlord|st)|fr(?:oglok|g)$/i,
        32768 => qr/^16|522|ber(?:serker)?|dr(?:akkin|k)$/i,
    };

    # iterate over key/val pairs
    # return key if argument passed matches value
    my $for = shift;
    while ( my ( $key, $val ) = each %$bit ) {
        return $key if $for =~ $val;
    }

    return 0;
}
patterns tested with the following:
Code:
sub BitTest {

    my $bit = {
        1     => qr/^1|war(?:rior)?|hum(?:an)?$/i,
        2     => qr/^2|cl(?:eric|r)|bar(?:barian)?$/i,
        4     => qr/^3|pal(?:adin)|eru(?:dite)?$/i,
        8     => qr/^4|r(?:anger|ng)|(?:wood )?elf$/i,
        16    => qr/^5|sh(?:adowknight|d)|hi(?:gh elf|e)$/i,
        32    => qr/^6|dru(?:id)?|d(?:ark elf|ef)$/i,
        64    => qr/^7|m(?:o)?nk|h(?:alf-elf|el)$/i,
        128   => qr/^8|b(?:a)?rd|dw(?:ar)?f$/i,
        256   => qr/^9|rog(?:ue)?|tr(?:ol)?l$/i,
        512   => qr/^10|sh(?:aman|m)|ogr(?:e)?$/i,
        1024  => qr/^11|nec(?:romancer)?|h(?:alfling|lf)$/i,
        2048  => qr/^12|wiz(?:ard)?|gn(?:ome|m)$/i,
        4096  => qr/^1(?:3|28)|mag(?:ician)?|iks(?:ar)?$/i,
        8192  => qr/^1(?:4|30)|enc(?:hanter)?|vah(?: shir)?$/i,
        16384 => qr/^15|330|b(?:eastlord|st)|fr(?:oglok|g)$/i,
        32768 => qr/^16|522|ber(?:serker)?|dr(?:akkin|k)$/i,
    };

    my $test = {
        1     => [ 1,  'war', 'warrior',      'hum',       'human' ],
        2     => [ 2,  'clr', 'cleric',       'bar',       'barbarian' ],
        3     => [ 3,  'pal', 'paladin',      'eru',       'erudite' ],
        4     => [ 4,  'rng', 'ranger',       'elf',       'wood elf' ],
        8     => [ 5,  'shd', 'shadowknight', 'hie',       'high elf' ],
        16    => [ 6,  'dru', 'druid',        'def',       'dark elf' ],
        32    => [ 7,  'mnk', 'monk',         'hel',       'half-elf' ],
        64    => [ 8,  'brd', 'bard',         'dwf',       'dwarf' ],
        128   => [ 9,  'rog', 'rogue',        'trl',       'troll' ],
        256   => [ 10, 'shm', 'shaman',       'ogr',       'ogre' ],
        512   => [ 11, 'nec', 'necromancer',  'hlf',       'halfling' ],
        1024  => [ 12, 'wiz', 'wizard',       'gnm',       'gnome' ],
        2048  => [ 13, 128,   'mag',          'magician',  'iks', 'iksar' ],
        4096  => [ 14, 130,   'enc',          'enchanter', 'vah', 'vah shir' ],
        16384 => [ 15, 330,   'bst',          'beastlord', 'frg', 'froglok' ],
        32768 => [ 16, 522,   'ber',          'berserker', 'drk', 'drakkin' ],
    };

    $txt = "passed %d tests for %s\n";
    while ( my ( $cat, $itm ) = each %$test ) {
        $result = grep { $_ =~ $bit->{$key} } @$itm;
        print sprintf $txt, $result, $cat;
    }
}

BitTest();
resulting in:
Code:
passed 5 tests for 32
passed 5 tests for 1024
passed 6 tests for 32768
passed 6 tests for 16384
passed 5 tests for 2
passed 5 tests for 1
passed 6 tests for 4096
passed 5 tests for 16
passed 5 tests for 128
passed 5 tests for 512
passed 5 tests for 3
passed 5 tests for 64
passed 6 tests for 2048
passed 5 tests for 8
passed 5 tests for 4
passed 5 tests for 256
2048, 4096, 16384, and 32768 are the only ones that had 6 tests to pass since the numeric values for the associated race and class were not identical.
__________________
I muck about @ The Forge.
say(rand 99>49?'try '.('0x'.join '',map{unpack 'H*',chr rand 256}1..2):'incoherent nonsense')while our $Noport=1;
Reply With Quote