From b5db153e032edb0145079b62c9e28ce7a7a35da7 Mon Sep 17 00:00:00 2001 From: Alex Kerr Date: Tue, 24 Feb 2015 03:18:56 +0000 Subject: [PATCH] Added alignment support --- lib/OSRIC/Character.pm | 38 ++++++++++++++++++++++++++++++++++ lib/OSRIC/Class.pm | 3 +++ lib/OSRIC/Class/Assassin.pm | 9 +++++++- lib/OSRIC/Class/Cleric.pm | 9 +++++++- lib/OSRIC/Class/Druid.pm | 9 +++++++- lib/OSRIC/Class/Fighter.pm | 9 +++++++- lib/OSRIC/Class/Illusionist.pm | 9 +++++++- lib/OSRIC/Class/MagicUser.pm | 9 +++++++- lib/OSRIC/Class/Paladin.pm | 9 +++++++- lib/OSRIC/Class/Ranger.pm | 9 +++++++- lib/OSRIC/Class/Thief.pm | 9 +++++++- lib/OSRIC/Util.pm | 19 ++++++++++++++++- 12 files changed, 131 insertions(+), 10 deletions(-) diff --git a/lib/OSRIC/Character.pm b/lib/OSRIC/Character.pm index d04713a..ce756f6 100644 --- a/lib/OSRIC/Character.pm +++ b/lib/OSRIC/Character.pm @@ -23,6 +23,7 @@ use OSRIC::Class::Thief; use OSRIC::Util qw/d con_mod/; use POSIX qw/ceil/; use JSON qw/to_json/; +use List::Compare qw/new get_intersection/; # These functions are ordered in this file in the order they are to be # called in: @@ -238,6 +239,43 @@ sub generate_hp $self->{personal}->{hp} = ceil($self->{personal}->{hp}); } +# Gets all of the player's available alignments: +sub get_available_alignments +{ + my $self = shift; + + # Store all the returned alignment options: + my @alignments; + + # Loop over the player's classes: + my $classes = $self->{personal}->{classes}; + for my $class(@{$classes}) + { + push @alignments, "OSRIC::Class::$class"->get_alignments; + } + + # Return the intersection of all the arrays obtained: + if(@alignments > 1) + { + my $lc = List::Compare->new({ + lists => \@alignments, + unsorted => 1, + }); + return $lc->get_intersection; + } + else + { + return @{$alignments[0]}; + } +} + +# Sets the player's alignment: +sub set_alignment +{ + my $self = shift; + $self->{personal}->{alignment} = shift; +} + # Encodes the character to JSON: sub as_json { diff --git a/lib/OSRIC/Class.pm b/lib/OSRIC/Class.pm index b9aca46..a7af25d 100644 --- a/lib/OSRIC/Class.pm +++ b/lib/OSRIC/Class.pm @@ -25,4 +25,7 @@ sub minimum_scores } } +# The allowed alignments +sub get_alignments { } + 1; diff --git a/lib/OSRIC/Class/Assassin.pm b/lib/OSRIC/Class/Assassin.pm index 26eb54c..0eef044 100644 --- a/lib/OSRIC/Class/Assassin.pm +++ b/lib/OSRIC/Class/Assassin.pm @@ -1,6 +1,6 @@ package OSRIC::Class::Assassin; use parent qw(OSRIC::Class); -use OSRIC::Util qw/d/; +use OSRIC::Util qw/d alignments/; # A sub to get the maximum amount of starting gold (for sorting) and one to get # an actual amount of starting gold: @@ -23,4 +23,11 @@ sub minimum_scores } } +# The allowed alignments +sub get_alignments +{ + my @a = grep { $_ =~ /^evil/i } alignments; + return \@a; +} + 1; diff --git a/lib/OSRIC/Class/Cleric.pm b/lib/OSRIC/Class/Cleric.pm index 95e9447..eadd2e1 100644 --- a/lib/OSRIC/Class/Cleric.pm +++ b/lib/OSRIC/Class/Cleric.pm @@ -1,6 +1,6 @@ package OSRIC::Class::Cleric; use parent qw(OSRIC::Class); -use OSRIC::Util qw/d/; +use OSRIC::Util qw/d alignments/; # A sub to get the maximum amount of starting gold (for sorting) and one to get # an actual amount of starting gold: @@ -23,4 +23,11 @@ sub minimum_scores } } +# The allowed alignments +sub get_alignments +{ + my @a = alignments; + return \@a; +} + 1; diff --git a/lib/OSRIC/Class/Druid.pm b/lib/OSRIC/Class/Druid.pm index 637fd30..04e5b61 100644 --- a/lib/OSRIC/Class/Druid.pm +++ b/lib/OSRIC/Class/Druid.pm @@ -1,6 +1,6 @@ package OSRIC::Class::Druid; use parent qw(OSRIC::Class); -use OSRIC::Util qw/d/; +use OSRIC::Util qw/d alignments/; # A sub to get the maximum amount of starting gold (for sorting) and one to get # an actual amount of starting gold: @@ -23,4 +23,11 @@ sub minimum_scores } } +# The allowed alignments +sub get_alignments +{ + my @a = grep { $_ =~ /^neutral$/i } alignments; + return \@a; +} + 1; diff --git a/lib/OSRIC/Class/Fighter.pm b/lib/OSRIC/Class/Fighter.pm index 085e6b2..a4424ff 100644 --- a/lib/OSRIC/Class/Fighter.pm +++ b/lib/OSRIC/Class/Fighter.pm @@ -1,6 +1,6 @@ package OSRIC::Class::Fighter; use parent qw(OSRIC::Class); -use OSRIC::Util qw/d/; +use OSRIC::Util qw/d alignments/; # A sub to get the maximum amount of starting gold (for sorting) and one to get # an actual amount of starting gold: @@ -23,4 +23,11 @@ sub minimum_scores } } +# The allowed alignments +sub get_alignments +{ + my @a = alignments; + return \@a; +} + 1; diff --git a/lib/OSRIC/Class/Illusionist.pm b/lib/OSRIC/Class/Illusionist.pm index c4899f5..e12c4e5 100644 --- a/lib/OSRIC/Class/Illusionist.pm +++ b/lib/OSRIC/Class/Illusionist.pm @@ -1,6 +1,6 @@ package OSRIC::Class::Illusionist; use parent qw(OSRIC::Class); -use OSRIC::Util qw/d/; +use OSRIC::Util qw/d alignments/; # A sub to get the maximum amount of starting gold (for sorting) and one to get # an actual amount of starting gold: @@ -23,4 +23,11 @@ sub minimum_scores } } +# The allowed alignments +sub get_alignments +{ + my @a = alignments; + return \@a; +} + 1; diff --git a/lib/OSRIC/Class/MagicUser.pm b/lib/OSRIC/Class/MagicUser.pm index 4aa87fa..07f6d53 100644 --- a/lib/OSRIC/Class/MagicUser.pm +++ b/lib/OSRIC/Class/MagicUser.pm @@ -1,6 +1,6 @@ package OSRIC::Class::MagicUser; use parent qw(OSRIC::Class); -use OSRIC::Util qw/d/; +use OSRIC::Util qw/d alignments/; # A sub to get the maximum amount of starting gold (for sorting) and one to get # an actual amount of starting gold: @@ -23,4 +23,11 @@ sub minimum_scores } } +# The allowed alignments +sub get_alignments +{ + my @a = alignments; + return \@a; +} + 1; diff --git a/lib/OSRIC/Class/Paladin.pm b/lib/OSRIC/Class/Paladin.pm index 5598471..f888eeb 100644 --- a/lib/OSRIC/Class/Paladin.pm +++ b/lib/OSRIC/Class/Paladin.pm @@ -1,6 +1,6 @@ package OSRIC::Class::Paladin; use parent qw(OSRIC::Class); -use OSRIC::Util qw/d/; +use OSRIC::Util qw/d alignments/; # A sub to get the maximum amount of starting gold (for sorting) and one to get # an actual amount of starting gold: @@ -23,4 +23,11 @@ sub minimum_scores } } +# The allowed alignments +sub get_alignments +{ + my @a = grep { $_ =~ /^lawful good$/i } alignments; + return \@a; +} + 1; diff --git a/lib/OSRIC/Class/Ranger.pm b/lib/OSRIC/Class/Ranger.pm index 6809175..ac87bac 100644 --- a/lib/OSRIC/Class/Ranger.pm +++ b/lib/OSRIC/Class/Ranger.pm @@ -1,6 +1,6 @@ package OSRIC::Class::Ranger; use parent qw(OSRIC::Class); -use OSRIC::Util qw/d/; +use OSRIC::Util qw/d alignments/; # A sub to get the maximum amount of starting gold (for sorting) and one to get # an actual amount of starting gold: @@ -23,4 +23,11 @@ sub minimum_scores } } +# The allowed alignments +sub get_alignments +{ + my @a = grep { $_ =~ /^good/i } alignments; + return \@a; +} + 1; diff --git a/lib/OSRIC/Class/Thief.pm b/lib/OSRIC/Class/Thief.pm index d13d84e..e79c612 100644 --- a/lib/OSRIC/Class/Thief.pm +++ b/lib/OSRIC/Class/Thief.pm @@ -1,6 +1,6 @@ package OSRIC::Class::Thief; use parent qw(OSRIC::Class); -use OSRIC::Util qw/d/; +use OSRIC::Util qw/d alignments/; # A sub to get the maximum amount of starting gold (for sorting) and one to get # an actual amount of starting gold: @@ -23,4 +23,11 @@ sub minimum_scores } } +# The allowed alignments +sub get_alignments +{ + my @a = grep { $_ =~ /(?:neutral|evil)$/i } alignments; + return \@a; +} + 1; diff --git a/lib/OSRIC/Util.pm b/lib/OSRIC/Util.pm index dccb2cb..3cc8c6a 100644 --- a/lib/OSRIC/Util.pm +++ b/lib/OSRIC/Util.pm @@ -1,6 +1,6 @@ package OSRIC::Util; use Exporter qw/import/; -our @EXPORT = qw/d con_mod/; +our @EXPORT = qw/d con_mod alignments/; # Rolls a dice of the specified number: sub d @@ -83,4 +83,21 @@ sub con_mod return $mod; } +# Generates the alignments because I'm too lazy to write them out: +sub alignments +{ + my @a = qw/Lawful Neutral Chaotic/; + my @b = qw/Good Neutral Evil/; + my @alignments; + for my $a(@a) + { + for my $b(@b) + { + unless($a eq $b) { push @alignments, ($a . " " . $b); } + else { push @alignments, $a; } + } + } + return @alignments; +} + 1;