diff --git a/lib/OSRIC/Character.pm b/lib/OSRIC/Character.pm index 78c01f3..d04713a 100644 --- a/lib/OSRIC/Character.pm +++ b/lib/OSRIC/Character.pm @@ -20,7 +20,8 @@ use OSRIC::Class::Paladin; use OSRIC::Class::Ranger; use OSRIC::Class::Thief; -use OSRIC::Util qw/d/; +use OSRIC::Util qw/d con_mod/; +use POSIX qw/ceil/; use JSON qw/to_json/; # These functions are ordered in this file in the order they are to be @@ -42,7 +43,7 @@ sub new xp => 0, hp => 0, ac => 0, - lvl => 0, + lvl => 1, age => 0, height => 0, weight => 0, @@ -218,6 +219,25 @@ sub generate_age $self->{personal}->{age} /= @{$self->{personal}->{classes}}; } +# Generates the player's HP. +sub generate_hp +{ + my $self = shift; + + # Loop over each class and generate an HP value: + for my $class(@{$self->{personal}->{classes}}) + { + $self->{personal}->{hp} += ("OSRIC::Class::$class"->get_hp + + con_mod($self->{stats}->{con}, $class)); + } + + # Divide by the number of classes: + $self->{personal}->{hp} /= @{$self->{personal}->{classes}}; + + # Round up if needed: + $self->{personal}->{hp} = ceil($self->{personal}->{hp}); +} + # Encodes the character to JSON: sub as_json { diff --git a/lib/OSRIC/Class.pm b/lib/OSRIC/Class.pm index 841fa34..b9aca46 100644 --- a/lib/OSRIC/Class.pm +++ b/lib/OSRIC/Class.pm @@ -9,6 +9,9 @@ my @CLASSES = qw/Assassin Cleric Druid Fighter Illusionist MagicUser Paladin sub max_starting_gold { 0 } sub get_gold { 0 } +# The starting HP of the class: +sub get_hp { 0 } + # Minimum score requirements: sub minimum_scores { diff --git a/lib/OSRIC/Class/Assassin.pm b/lib/OSRIC/Class/Assassin.pm index 6e74832..26eb54c 100644 --- a/lib/OSRIC/Class/Assassin.pm +++ b/lib/OSRIC/Class/Assassin.pm @@ -7,6 +7,9 @@ use OSRIC::Util qw/d/; sub max_starting_gold { 120 } sub get_gold { (d(6, 2) * 10) } # 2d6 * 10 +# The starting HP of the class: +sub get_hp { d(6) } + # Minimum score requirements: sub minimum_scores { diff --git a/lib/OSRIC/Class/Cleric.pm b/lib/OSRIC/Class/Cleric.pm index a66dd8f..95e9447 100644 --- a/lib/OSRIC/Class/Cleric.pm +++ b/lib/OSRIC/Class/Cleric.pm @@ -7,6 +7,9 @@ use OSRIC::Util qw/d/; sub max_starting_gold { 180 } sub get_gold { (d(6, 3) * 10) } # 3d6 * 10 +# The starting HP of the class: +sub get_hp { d(8) } + # Minimum score requirements: sub minimum_scores { diff --git a/lib/OSRIC/Class/Druid.pm b/lib/OSRIC/Class/Druid.pm index 367a200..637fd30 100644 --- a/lib/OSRIC/Class/Druid.pm +++ b/lib/OSRIC/Class/Druid.pm @@ -7,6 +7,9 @@ use OSRIC::Util qw/d/; sub max_starting_gold { 180 } sub get_gold { (d(6, 3) * 10) } # 3d6 * 10 +# The starting HP of the class: +sub get_hp { d(8) } + # Minimum score requirements: sub minimum_scores { diff --git a/lib/OSRIC/Class/Fighter.pm b/lib/OSRIC/Class/Fighter.pm index 7559db7..085e6b2 100644 --- a/lib/OSRIC/Class/Fighter.pm +++ b/lib/OSRIC/Class/Fighter.pm @@ -7,6 +7,9 @@ use OSRIC::Util qw/d/; sub max_starting_gold { 200 } sub get_gold { ((d(6, 3) + 2) * 10) } # (3d6 + 2) * 10 +# The starting HP of the class: +sub get_hp { d(10) } + # Minimum score requirements: sub minimum_scores { diff --git a/lib/OSRIC/Class/Illusionist.pm b/lib/OSRIC/Class/Illusionist.pm index 0322f1d..c4899f5 100644 --- a/lib/OSRIC/Class/Illusionist.pm +++ b/lib/OSRIC/Class/Illusionist.pm @@ -7,6 +7,9 @@ use OSRIC::Util qw/d/; sub max_starting_gold { 80 } sub get_gold { (d(4, 2) * 10) } # 2d4 * 10 +# The starting HP of the class: +sub get_hp { d(4) } + # Minimum score requirements: sub minimum_scores { diff --git a/lib/OSRIC/Class/MagicUser.pm b/lib/OSRIC/Class/MagicUser.pm index ddc2fb6..4aa87fa 100644 --- a/lib/OSRIC/Class/MagicUser.pm +++ b/lib/OSRIC/Class/MagicUser.pm @@ -7,6 +7,9 @@ use OSRIC::Util qw/d/; sub max_starting_gold { 80 } sub get_gold { (d(4, 2) * 10) } # 2d4 * 10 +# The starting HP of the class: +sub get_hp { d(4) } + # Minimum score requirements: sub minimum_scores { diff --git a/lib/OSRIC/Class/Paladin.pm b/lib/OSRIC/Class/Paladin.pm index b66d107..5598471 100644 --- a/lib/OSRIC/Class/Paladin.pm +++ b/lib/OSRIC/Class/Paladin.pm @@ -7,6 +7,9 @@ use OSRIC::Util qw/d/; sub max_starting_gold { 200 } sub get_gold { ((d(6, 3) + 2) * 10) } # (3d6 + 2) * 10 +# The starting HP of the class: +sub get_hp { d(10) } + # Minimum score requirements: sub minimum_scores { diff --git a/lib/OSRIC/Class/Ranger.pm b/lib/OSRIC/Class/Ranger.pm index 42130d2..6809175 100644 --- a/lib/OSRIC/Class/Ranger.pm +++ b/lib/OSRIC/Class/Ranger.pm @@ -7,6 +7,9 @@ use OSRIC::Util qw/d/; sub max_starting_gold { 200 } sub get_gold { ((d(6, 3) + 2) * 10) } # (3d6 + 2) * 10 +# The starting HP of the class: +sub get_hp { d(8) } + # Minimum score requirements: sub minimum_scores { diff --git a/lib/OSRIC/Class/Thief.pm b/lib/OSRIC/Class/Thief.pm index bec0a19..d13d84e 100644 --- a/lib/OSRIC/Class/Thief.pm +++ b/lib/OSRIC/Class/Thief.pm @@ -7,6 +7,9 @@ use OSRIC::Util qw/d/; sub max_starting_gold { 120 } sub get_gold { (d(6, 2) * 10) } # 2d6 * 10 +# The starting HP of the class: +sub get_hp { d(6) } + # Minimum score requirements: sub minimum_scores { diff --git a/lib/OSRIC/Util.pm b/lib/OSRIC/Util.pm index 8a91095..dccb2cb 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/; +our @EXPORT = qw/d con_mod/; # Rolls a dice of the specified number: sub d @@ -16,4 +16,71 @@ sub d return $i; } +# Returns the constitution hp modifier (at level 1): +sub con_mod +{ + my $con = shift; + my $class = shift; + + # Get the constitution modifier: + my $mod; + if($con <= 3) + { + $mod = -2; + } + elsif(($con > 3) && ($con <= 6)) + { + $mod = -1; + } + elsif(($con > 6) && ($con <= 14)) + { + $mod = 0; + } + elsif(($con > 14) && ($con <= 15)) + { + $mod = 1; + } + elsif(($con > 15) && ($con <= 16)) + { + $mod = 2; + } + elsif(($con > 16) && ($con <= 17)) + { + if(($class eq "Fighter") || ($class eq "Paladin") + || ($class eq "Ranger")) + { + $mod = 3; + } + else + { + $mod = 2; + } + } + elsif(($con > 17) && ($con <= 18)) + { + if(($class eq "Fighter") || ($class eq "Paladin") + || ($class eq "Ranger")) + { + $mod = 4; + } + else + { + $mod = 2; + } + } + elsif(($con > 18) && ($con <= 19)) + { + if(($class eq "Fighter") || ($class eq "Paladin") + || ($class eq "Ranger")) + { + $mod = 5; + } + else + { + $mod = 2; + } + } + return $mod; +} + 1;