Games-Axmud
view release on metacpan or search on metacpan
lib/Games/Axmud/Obj/Dict.pm view on Meta::CPAN
# recommend that if a relative direction is added to $self->relativeDirHash or
# ->relativeAbbrevHash, the opposite relative direction is added too, but there's no way
# to guarantee the user won't miss one out
for (my $key = 0; $key < 8; $key++) {
my ($oppKey, $dir, $oppDir);
# (Keys in the hash are in the range 0-7, so the opposite of 0 is 4, the opposite of 7
# is 3, etc)
$oppKey = $key + 4;
if ($oppKey > 7) {
$oppKey -= 8;
}
foreach my $iv ('relativeDirHash', 'relativeAbbrevHash') {
my ($value, $oppValue);
$value = $self->ivShow($iv, $key);
$oppValue = $self->ivShow($iv, $oppKey);
if (defined $value && $value ne '' && defined $oppValue && $oppValue ne '') {
$combOppHash{$value} = $oppValue;
}
}
}
# A hash of customised directions, in the form
# ->combRevDirHash{custom_direction} = standard_direction (for primary directions)
# ->combRevDirHash{recognised_direction} = recognised_direction (for secondary directions)
@typeList = ('primaryDir', 'primaryAbbrev', 'secondaryDir', 'secondaryAbbrev');
foreach my $type (@typeList) {
my $hashName = $type . 'Hash';
my %thisHash = $self->$hashName;
foreach my $key (keys %thisHash) {
my $value = $thisHash{$key};
# ->secondaryAbbrevHash can have 'undef' values in its key-value pairs, so don't add
# those to the combined hash
# If the customised direction has already been added, don't replace it
if ($value && (! exists $combRevHash{$value})) {
$combRevHash{$value} = $key;
}
}
}
# Combined hashes complete. Update IVs
$self->{'combDirHash'} = \%combHash;
$self->{'combOppDirHash'} = \%combOppHash;
$self->{'combRevDirHash'} = \%combRevHash;
return 1;
}
sub updateCombNounHash {
# Called by GA::Cmd::AddWord->do or DeleteWord->do
# Updates the hash $self->combNounHash after one of the main noun hashes is modified
# (this method is hopefully quicker than calling $self->createCombHashes every time a
# single key-value pair is changed)
#
# Expected arguments
# $nounType - Which type of noun has been changed ('pluralNoun', 'sentient', 'creature',
# 'portable', 'decoration', 'race', 'guild', 'weapon', 'armour',
# 'garment', 'pseudoNoun')
# $addFlag - TRUE if the key-value pair is to be added to the combined hash; FALSE if
# it is to be removed
# $key - ($addFlag = TRUE) The new key; ($addFlag = FALSE) the key to be removed
#
# Optional arguments
# $value - ($addFlag = TRUE) The new value ($addFlag = FALSE) 'undef'
#
# Return values
# 'undef' on improper arguments or if the combined hash isn't modified
# 1 otherwise
my ($self, $nounType, $addFlag, $key, $value, $check) = @_;
# Local variables
my (
$newPriority, $oldPriority,
%priorityHash,
);
# Check for improper arguments
if (! defined $nounType || ! defined $addFlag || ! defined $key || defined $check) {
return $axmud::CLIENT->writeImproper($self->_objClass . '->updateCombNounHash', @_);
}
# Create a hash of priorities, so we can quickly work out what to do with the new key-value
# pair
%priorityHash = (
'pluralNoun' => 0,
'sentient' => 1,
'creature' => 2,
'portable' => 3,
'decoration' => 4,
'race' => 5,
'guild' => 6,
'weapon' => 7,
'armour' => 8,
'garment' => 9,
'pseudoNoun' => 10,
);
# Check that $nounType is valid
if (! exists $priorityHash{$nounType} ) {
# Treat this as an improper argument
return $axmud::CLIENT->writeImproper($self->_objClass . '->updateCombNounHash', @_);
}
# If the key-value pair is to be added to the combined hash...
if ($addFlag) {
lib/Games/Axmud/Obj/Dict.pm view on Meta::CPAN
if ($oldPriority < $newPriority) {
# New key-value pair takes precedence
$self->ivAdd('combNounHash', $key, $value);
return 1;
} else {
# Old key-value pair takes precedence; don't change the combined hash
return undef;
}
}
# If the key-value pair is to be removed from the combined hash, need to check whether a
# noun from a higher-priority hash should replace it
#
# If the key-value pair isn't in the combined hash, no changes to make
} elsif (! $self->ivExists('combNounHash', $key)) {
return undef;
} else {
# Remember the priority of the dictionary hash which contains the key-value pair to be
# deleted
$value = $self->ivShow('combNounHash', $key);
$newPriority = $priorityHash{$value};
# Delete the key
$self->ivDelete('combNounHash', $key);
# Check the lower-priority dictionary hashes to see if any of them use the same key
# Reinstate the first key-value pair found
if ($newPriority == 10) { # Axmud dictionaries use 11 noun hashes
# No dictionary hashes with lower priority to check
return 1;
} else {
for (my $count = 1; $count <= 10; $count++) {
my $hashName = $nounType . 'Hash';
if ($newPriority <= $count && $self->ivExists($hashName, $key)) {
# The same key was found in this dictionary hash. Reinstate it into the
# combined hash
$value = $self->ivShow($hashName, $key);
$self->ivAdd('combNounHash', $key, $value);
return 1;
}
}
# None of the lower-priority dictionary hashes use the same key
return undef;
}
}
}
sub updateCombAdjHash {
# Called by GA::Cmd::AddWord->do or DeleteWord->do
# Updates the hash $self->combinedAdjHash after one of the main adjective hashes is modified
# (this method is hopefully quicker than calling $self->createCombHashes every time a
# single key-value pair is changed)
#
# Expected arguments
# $adjType - Which type of adjective has been changed ('declinedAdj',
# 'adj', 'pseudoAdj')
# $addFlag - TRUE if the key-value pair is to be added to the combined hash; FALSE if
# it is to be removed
# $key - ($addFlag = TRUE) The new key; ($addFlag = FALSE) the key to be removed
#
# Optional arguments
# $value - ($addFlag = TRUE) The new value ($addFlag = FALSE) 'undef'
#
# Return values
# 'undef' on improper arguments or if the combined hash isn't modified
# 1 otherwise
my ($self, $adjType, $addFlag, $key, $value, $check) = @_;
# Local variables
my (
$newPriority, $oldPriority,
%priorityHash,
);
# Check for improper arguments
if (! defined $adjType || ! defined $addFlag || ! defined $key || defined $check) {
return $axmud::CLIENT->writeImproper($self->_objClass . '->updateCombAdjHash', @_);
}
# Create a hash of priorities, so we can quickly work out what to do with the new key-value
# pair
%priorityHash = (
'declinedAdj' => 0,
'adj' => 1,
'pseudoAdj' => 2,
);
# Check that $$adjType is valid
if (! exists $priorityHash{$adjType} ) {
# Treat this as an improper argument
return $axmud::CLIENT->writeImproper($self->_objClass . '->updateCombAdjHash', @_);
}
# If the key-value pair is to be added to the combined hash...
if ($addFlag) {
# If it doesn't already exist in the hash, simply add it
if (! $self->ivExists('combAdjHash', $key)) {
$self->ivAdd('combAdjHash', $key, $value);
return 1;
# Otherwise work out which key-value pair has priority
} else {
lib/Games/Axmud/Obj/Dict.pm view on Meta::CPAN
if ($oldPriority < $newPriority) {
# New key-value pair takes precedence
$self->ivAdd('combAdjHash', $key, $value);
return 1;
} else {
# Old key-value pair takes precedence; don't change the combined hash
return undef;
}
}
# If the key-value pair is to be removed from the combined hash, need to check whether an
# adjective from a higher-priority hash should replace it
#
# If the key-value pair isn't in the combined hash, no changes to make
} elsif (! $self->ivExists('combAdjHash', $key)) {
return undef;
} else {
# Remember the priority of the dictionary hash which contains the key-value pair to be
# deleted
$value = $self->ivShow('combAdjHash', $key);
$newPriority = $priorityHash{$value};
# Delete the key
$self->ivDelete('combAdjHash', $key);
# Check the lower-priority dictionary hashes to see if any of them use the same key
# Reinstate the first key-value pair found
if ($newPriority == 2) { # Axmud dictionaries use 3 adjective hashes
# No dictionary hashes with lower priority to check
return 1;
} else {
for (my $count = 1; $count <= 2; $count++) {
my $hashName = $adjType . 'Hash';
if ($newPriority <= $count && $self->ivExists($hashName, $key)) {
# The same key was found in this dictionary hash. Reinstate it into the
# combined hash
$value = $self->ivShow($hashName, $key);
$self->ivAdd('combAdjHash', $key, $value);
return 1;
}
}
# None of the lower-priority dictionary hashes use the same key
return undef;
}
}
}
sub updateOppDirHash {
# Can be called by anything (for example, called by GA::WizWin::Locator->saveChanges)
# When $self->primaryDirHash and/or $self->primaryAbbrevHash are modified, this function can
# be called to update the contents of $self->primaryOppHash and ->primaryOppAbbrevHash
#
# Expected arguments
# (none besides $self)
#
# Return values
# 'undef' on improper arguments
# 1 otherwise
my ($self, $check) = @_;
# Local variables
my (%dirHash, %abbrevHash, %oppHash, %oppAbbrevHash);
# Check for improper arguments
if (defined $check) {
return $axmud::CLIENT->writeImproper($self->_objClass . '->updateOppDirHash', @_);
}
# Import IVs that we won't change (for simplicity)
%dirHash = $self->primaryDirHash;
%abbrevHash = $self->primaryAbbrevHash;
# Use them to update these IVs
foreach my $key ($axmud::CLIENT->constPrimaryDirList) {
$oppHash{$key} = $dirHash{$axmud::CLIENT->ivShow('constOppDirHash', $key)};
$oppAbbrevHash{$key} = $abbrevHash{$axmud::CLIENT->ivShow('constOppDirHash', $key)};
}
$self->ivPoke('primaryOppHash', %oppHash);
$self->ivPoke('primaryOppAbbrevHash', %oppAbbrevHash);
$self->createCombDirHash();
return 1;
}
sub convertToPlural {
# Called by anything
# Converts any singular noun to its plural form, as best as it can
#
# Expected arguments
# $word - The word to convert
#
# Return values
# The plural form, if a plural form can be found
# Otherwise returns $word unaltered (even on improper arguments)
my ($self, $word, $check) = @_;
# Local variables
my @endingList;
# Check for improper arguments
( run in 3.374 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )