=head1 NAME Jeweler =head1 DESCRIPTION The Jeweler skill helper module. =cut package Jeweler; use strict; use YAML; =over 4 =item @RESISTS List of all resistancies that can occur on rings and amulets. =cut our $CFG; sub read_config { my ($filename) = @_; unless (-e $filename) { warn "$filename doesn't exists! no config for jeweler skill loaded!\n"; $CFG = {}; return } $CFG = YAML::LoadFile $filename; } sub getcfg { my ($sect, $key) = @_; my $cfg = $CFG->{$sect}->{$key} or die "Couldn't find $sect/$key in configuration!"; $cfg } # makes a template arch (for example to get the value) sub get_arch { my ($outarch) = @_; unless ($CFG->{arch}->{$outarch}) { $CFG->{arch}->{$outarch} = cf::object::new $outarch; unless ($CFG->{arch}->{$outarch}) { warn "ERROR: Couldn't make $outarch in conversion for $outarch!"; return; } } $CFG->{arch}->{$outarch} } our @RESISTS = ( cf::ATNR_PHYSICAL, cf::ATNR_MAGIC, cf::ATNR_FIRE, cf::ATNR_ELECTRICITY, cf::ATNR_COLD, cf::ATNR_CONFUSION, cf::ATNR_ACID, cf::ATNR_DRAIN, cf::ATNR_GHOSTHIT, cf::ATNR_POISON, cf::ATNR_SLOW, cf::ATNR_PARALYZE, cf::ATNR_TURN_UNDEAD, cf::ATNR_FEAR, cf::ATNR_DEPLETE, cf::ATNR_DEATH, cf::ATNR_HOLYWORD, cf::ATNR_LIFE_STEALING, cf::ATNR_BLIND, cf::ATNR_DISEASE, ); =item @EFFECT_RESISTS List of all effect resistancies that occur on rings and amulets. The difference is made because effect resistancies are less effective at lower levels. =back =cut our @EFFECT_RESISTS = ( cf::ATNR_CONFUSION, cf::ATNR_DRAIN, cf::ATNR_POISON, cf::ATNR_SLOW, cf::ATNR_PARALYZE, cf::ATNR_TURN_UNDEAD, cf::ATNR_FEAR, cf::ATNR_DEPLETE, cf::ATNR_DEATH, cf::ATNR_BLIND, cf::ATNR_DISEASE, ); our %RESMAP = ( cf::ATNR_PHYSICAL => "PHYSICAL", cf::ATNR_MAGIC => "MAGIC", cf::ATNR_FIRE => "FIRE", cf::ATNR_ELECTRICITY => "ELECTRICITY", cf::ATNR_COLD => "COLD", cf::ATNR_CONFUSION => "CONFUSION", cf::ATNR_ACID => "ACID", cf::ATNR_DRAIN => "DRAIN", cf::ATNR_GHOSTHIT => "GHOSTHIT", cf::ATNR_POISON => "POISON", cf::ATNR_SLOW => "SLOW", cf::ATNR_PARALYZE => "PARALYZE", cf::ATNR_TURN_UNDEAD => "TURN_UNDEAD", cf::ATNR_FEAR => "FEAR", cf::ATNR_DEPLETE => "DEPLETE", cf::ATNR_DEATH => "DEATH", cf::ATNR_HOLYWORD => "HOLYWORD", cf::ATNR_LIFE_STEALING => "LIFE_STEALING", cf::ATNR_BLIND => "BLIND", cf::ATNR_DISEASE => "DISEASE", ); our %LVL_DIFF_CHANCES = ( +5 => 100, +4 => 95, +3 => 85, +2 => 75, +1 => 65, 0 => 50, -1 => 45, -2 => 35, -3 => 25, -4 => 10, -5 => 0 ); our %LVL_DIFF_MSG = ( -5 => '%s is way above your skill', -4 => 'The chance to make %s is very low', -3 => 'You hava a slight chance to make %s', -2 => 'There is a low chance you finish %s', -1 => 'You could make %s with a chance of nearly 50:50', 0 => 'The chances to fininsh %s is 50:50', 1 => 'To make %s your chance is slightly above 50:50', 2 => 'You could make with a good chance %s if you concentrate a lot', 3 => 'The chance you finish %s with some efford is high', 4 => 'You are nearly confident to finish %s', 5 => 'There is no chance you could fail to make %s', ); sub level_diff_to_str { my ($delta) = @_; $delta = -5 if $delta < -5; $delta = 5 if $delta > 5; return $LVL_DIFF_MSG{$delta} } sub level_diff_to_chance_perc { my ($delta) = @_; $delta = -5 if $delta < -5; $delta = 5 if $delta > 5; return $LVL_DIFF_CHANCES{$delta} } sub analyze { my ($sk, $chdl, $pl) = @_; for ($chdl->grep_by_type (cf::RING, cf::AMULET)) { my $sklvl = cf::exp_to_level ($sk->stats->exp); my $ringlvl = Jeweler::Object->new (object => $_)->power_to_level; if ($pl->get_flag (cf::FLAG_WIZ)) { $pl->message ("The " . $_->name . " is at level $ringlvl chance for you: " . level_diff_to_chance_perc ($sklvl - $ringlvl)); } else { my $tmpl = level_diff_to_str ($sklvl - $ringlvl); my $msg = sprintf $tmpl, $_->name; $pl->message ($msg); } } } # this function converts metals/minerals into a raw ring (of adornment) sub simple_converter { my ($pl, $ingred, $chdl, $conv) = @_; $conv = lc $conv; my $cnvs = $CFG->{conversions}; return unless $cnvs->{$conv}; my %ingred_groups; my @conv_cfg = @{$cnvs->{$conv}}; my $outarch = $conv; my ($ingr_grp, $outarchvalfact, $srcarchname, $xp_gain) = @conv_cfg; unless (@conv_cfg <= 4) { warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!"; return; } unless ($xp_gain > 0) { warn "WARNING: xp gain isn't > 0 in convesion '$outarch'\n"; return; } unless ($outarchvalfact) { warn "ERROR: source-arch-value-multiplier == 0 in convesion '$outarch'\n"; return; } unless ($outarchvalfact >= 1) { warn "WARNING: source-arch-value-multiplier < 1 in convesion '$outarch', results in more valuable output!\n"; } my $archvalsum = $ingred->value ($cnvs->{$outarch}->[0], $cnvs->{$outarch}->[2]); $ingred->remove ($cnvs->{$outarch}->[0], $cnvs->{$outarch}->[2]); my $outarchval = Jeweler::get_arch ($outarch)->value; my $nrof = int ($archvalsum / (($outarchval || 1000) * $outarchvalfact)); if ($nrof) { # XXX: yes, i know what i'm doing here, i don't set nrof, but it didn't work somehow (pls. chek sometimes) $chdl->put (cf::object::new $outarch) for 1..$nrof; my $xp_sum = ($xp_gain * $nrof); if ($xp_sum) { $pl->ob->message ("You got $xp_sum xp by making $nrof ${outarch}s"); $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_ADD_SKILL); } } } package Jeweler::CauldronHandler; use strict; =head2 CauldronHandler The Jeweler::CauldronHandler package, that helps you with handling the cauldron stuff. Can also be used for other skills. =cut sub new { my ($class, %arg) = @_; my $self = bless { %arg, }, $class; $self; } =over 4 =item find_cauldron ($arch_name, @map_stack) This finds the cauldron with C<$arch_name> on the C<@map_stack> and initalises the CauldronHandler. It takes the topmost cauldron that is found. Returns undef if no cauldron was found. Returns the cauldron object if it was found. =cut sub find_cauldron { my ($self, $arch_name, @map_stack) = @_; my @c = grep { $_->flag (cf::FLAG_IS_CAULDRON) and $_->archetype->name eq $arch_name } @map_stack; $self->{cauldron} = $c[0]; } =item grep_by_type (@types) Finds all objects in the cauldron that have the type of one of C<@types>. =cut sub grep_by_type { my ($self, @types) = @_; return () unless $self->{cauldron}; my @res = grep { my $ob = $_; (grep { $ob->type == $_ } @types) > 0 } $self->{cauldron}->inv; return @res } =item extract_jeweler_ingredients Extracts the ingredients that matter for the Jeweler skill and returns a Jeweler::Ingredients object. =cut sub extract_jeweler_ingredients { my ($self) = @_; return () unless $self->{cauldron}; my $ingreds = {}; my %type_to_key = ( cf::RING => 'rings', cf::AMULET => 'ammys', cf::INORGANIC => 'mets_and_mins', cf::GEM => 'gems', cf::POTION => 'potions', cf::SCROLL => 'scrolls', ); for ($self->{cauldron}->inv) { if (my $k = $type_to_key{$_->type}) { push @{$ingreds->{$k}}, $_; } else { Jeweler::Util::remove ($_); } } return Jeweler::Ingredients->new (ingredients => $ingreds, cauldron_helper => $self) } =item put ($object) Just puts the C<$object> into the cauldron. =cut sub put { my ($self, $obj) = @_; return undef unless $self->{cauldron}; $obj->insert_ob_in_ob ($self->{cauldron}); } =back =cut package Jeweler::Ingredients; use Storable qw/dclone/; use strict; =head2 Ingredients This class handles the ingredients. =over 4 =item new (ingredients => $ingred_hash) This is called from the CauldronHandler that gives you the ingredients. =cut sub new { my ($class, %arg) = @_; my $self = bless { %arg, }, $class; $self; } =item value ($group, $archname) Returns the value of the ingredients in C<$group> with the archetypename C<$archname>. =cut sub value { my ($self, $group, $archname) = @_; my @objs = grep { $_->archetype->name eq $archname } @{$self->{ingredients}->{$group} || []}; my $sum = 0; for (@objs) { $sum += $_->nrof * $_->value; } return $sum; } =item remove ($group, $archname) Removes the ingredients in C<$group> with archname C<$archname>. It removes all in C<$group> if archname is undef. =cut sub remove { my ($self, $group, $archname) = @_; my $ingred = $self->{ingredients}; my @out; for (@{$ingred->{$group}}) { if (defined $archname) { if ($_->archetype->name eq $archname) { Jeweler::Util::remove ($_); } else { push @out, $_; } } else { Jeweler::Util::remove ($_); } } @{$ingred->{$group}} = @out; } sub get_plan { my ($self) = @_; my $ingred = $self->{ingredients}; for my $grp (keys %$ingred) { for my $pot (@{$ingred->{$grp}}) { for my $plan (keys %{$Jeweler::CFG->{plans}}) { my $plg = $Jeweler::CFG->{plans}->{$plan}; my @plga = (); unless (ref $plg eq 'ARRAY') { push @plga, $plg; } else { @plga = @$plg; } next unless @plga > 0; if (Jeweler::Util::grep_for_match ($pot, @plga)) { warn "MATCHED: $plan: @plga\n"; return $plan; } } } } } sub get_ring { my ($self) = @_; return ( @{$self->{ingredients}->{ammys} || []}, @{$self->{ingredients}->{rings} || []} ); } sub improve_ring_by_plan { my ($self, $plan, $ring) = @_; $ring = dclone ($ring); my $ingred = $self->{ingredients}; my $impr = {}; if ($plan =~ m/^stat_(\S+)$/) { my $statname = $1; my $plingred = Jeweler::getcfg (plans => $plan) or die "ingredients for plan '$plan' not defined!"; my $cnt = 0; for my $pot (@{$ingred->{potions}}) { if (Jeweler::Util::grep_for_match ($pot, @$plingred)) { $cnt += $pot->nrof; } } warn "Found $cnt potions for plan $plan\n"; my $did_impr = 0; for my $x (reverse 1..10) { my $y = Jeweler::Object::fx ($x, 'stat_potions'); warn "TEST: fx($x): $y->[0] <= $cnt \n"; warn "FE: " . ($y->[0] == $cnt) . "\n"; if ($cnt >= $y->[0]) { $ring->{hash}->{stat}->{$statname} += $x; $did_impr = 1; warn "Found stat increase of $statname +$x\n"; last; } } # we want at least this improvement if we have a plan... $ring->{hash}->{stat}->{$statname} += 1 unless $did_impr; } elsif ($plan =~ m/^spec_(\S+)$/) { } elsif ($plan =~ m/^resist_(\S+)$/) { } return $ring; } sub do_grep { my ($self, $cb, @grepar) = @_; my $ingred = $self->{ingredients}; for my $cat (keys %$ingred) { my @rem; for my $ing (@{$ingred->{$cat}}) { if (Jeweler::Util::grep_for_match ($ing, @grepar)) { unless ($cb->($ing)) { push @rem, $ing; } } else { push @rem, $ing; } } @{$ingred->{$cat}} = @rem; } } sub check_costs { my ($self, $costs, $do_remove) = @_; my $costs = dclone ($costs); for my $key (keys %$costs) { my @grepar; if ($key =~ m/^stat_(\S+)$/) { @grepar = @{Jeweler::getcfg (plans => $key) || []}; } else { @grepar = (undef, undef, $key); } if ($do_remove) { my $rem = $costs->{$key}; $self->do_grep (sub { if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); } 1 }, @grepar); if ($rem > 0) { warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; } } else { my $nr; $self->do_grep (sub { warn "CNT!\n"; $nr += $_[0]->nrof; 0 }, @grepar); $costs->{$key} -= $nr; } } return $costs; } =back =cut sub put_to_bench { my ($self, $bench) = @_; my $ingred = $self->{ingredients}; for my $ik (keys %$ingred) { for (@{$ingred->{$ik} || []}) { $bench->put ($_); } } } package Jeweler::Object; use strict; use POSIX; use List::Util qw/max min sum/; sub new { my ($class, %arg) = @_; my $self = bless { }, $class; $self->ring_or_ammy_to_hash ($arg{object}); $self; } sub fx { my ($res, $cfg) = @_; my $or = $res; my $ar = $Jeweler::CFG->{functions}->{$cfg}; if (ref $ar->[0] eq 'ARRAY') { $res = $res - 1; } else { $res = ceil ($res / 5) - 1; } $ar->[max (min ($res, @$ar - 1), 0)]; } sub improve_by_ring { my ($self, @rings) = @_; my $ring = $self; for my $iring (@rings) { for my $cat (qw/stat spec resist/) { for my $k (keys %{$iring->{hash}->{$cat}}) { if ($ring->{hash}->{$cat}->{$k} < $iring->{hash}->{$cat}->{$k}) { $ring->{hash}->{$cat}->{$k} = $iring->{hash}->{$cat}->{$k}; } } } } } sub ring_or_ammy_to_hash { my ($self, $thing) = @_; my $obj = {}; for (@Jeweler::RESISTS) { $obj->{resist}->{$_} = $thing->get_resistance ($_); } my $stats = $thing->stats; for (qw/Str Dex Con Wis Cha Int Pow/) { $obj->{stat}->{lc $_} = $stats->$_; } $obj->{spec}->{regen} = $thing->hp; $obj->{spec}->{magic} = $thing->sp; $obj->{spec}->{wc} = $thing->wc; $obj->{spec}->{dam} = $thing->dam; $obj->{spec}->{ac} = $thing->ac; $obj->{spec}->{speed} = $thing->stats->exp; $obj->{spec}->{suste} = $thing->food; $obj->{name} = $thing->name; $obj->{arch} = $thing->archetype->name; $obj->{face} = $thing->face; $self->{hash} = $obj } sub to_object { my ($self) = @_; my $obj = cf::object::new $self->{hash}->{arch}; $obj->set_face ($self->{hash}->{face}); $obj->set_hp ($self->{hash}->{spec}->{regen} * 1); $obj->set_sp ($self->{hash}->{spec}->{magic} * 1); $obj->set_wc ($self->{hash}->{spec}->{wc} * 1); $obj->set_dam ($self->{hash}->{spec}->{dam} * 1); $obj->set_ac ($self->{hash}->{spec}->{ac} * 1); $obj->stats->exp ($self->{hash}->{spec}->{speed} * 1); $obj->set_food ($self->{hash}->{spec}->{suste} * 1); for (qw/Str Dex Con Wis Cha Int Pow/) { $obj->stats->$_ ($self->{hash}->{stat}->{lc $_} * 1); } for (@Jeweler::RESISTS) { $obj->set_resistance ($_, $self->{hash}->{resist}->{$_} * 1); } $obj->set_flag (cf::FLAG_IDENTIFIED, 1); return $obj; } sub stat_level { my ($self) = @_; my $stats = $self->{hash}->{stat} || {}; my $maxlevel = Jeweler::getcfg (maxlevels => 'stat_level'); my $maxstat = Jeweler::getcfg (maximprovements => 'stats'); my $stat_cnt = scalar (grep { $_ > 0 } values %$stats); my $stat_sum = sum (values %$stats); my $level = int (($maxlevel / $maxstat) * $stat_sum); ($level, $stat_cnt) } sub resist_level { my ($self) = @_; my $resists = $self->{hash}->{resist} || {}; my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level'); my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level'); my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances'); my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances'); my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances'); my $ressum = 0; my $rescnt = 0; my @reslevels; for my $resnam (keys %$resists) { my $res = $resists->{$resnam}; $rescnt++ if $res > 0; # negative resistancies are not an improvement $ressum += $res; # note: negative resistancies lower the sum next unless $res > 0; my $level = 0; if (grep { $resnam eq $_ } @Jeweler::EFFECT_RESISTS) { $level = ceil (($efc_res_lvl / $max_efc_res) * $res); } else { $level = ceil (($att_res_lvl / $max_att_res) * $res); } push @reslevels, $level; } my $overall_lvl = ($att_res_lvl / $max_ovr_res) * $ressum; (max (@reslevels, $overall_lvl), $rescnt); } sub special_level { my ($self) = @_; my $specials = $self->{hash}->{spec} || {}; my $max_spc_lvl = Jeweler::getcfg (maxlevels => 'spec_level'); my $max_specials = Jeweler::getcfg (maximprovements => 'specials'); my @speclvls; my $specsum = 0; my $imprs = 0; for my $spcnam (keys %$specials) { my $spc = $specials->{$spcnam}; next unless $spc > 0; $specsum += $spc; $imprs++; my $max_spc = Jeweler::getcfg (maxspecial => $spcnam); my $lvl = ($max_spc_lvl / $max_spc) * $spc; push @speclvls, $lvl; } my $sumlvl = ($max_spc_lvl / $max_specials) * $specsum; (max (@speclvls, $sumlvl), $imprs) } # this function calculated the 'level' of an amulet or a ring sub power_to_level { my ($self) = @_; my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements'); my $max_impr_lvl = Jeweler::getcfg (maxlevels => 'improve_level'); my ($stat_lvl, $stat_imprs) = $self->stat_level; my ($resist_lvl, $res_imprs) = $self->resist_level; my ($spec_lvl, $spec_imprs) = $self->special_level; my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs; my $impr_lvl = ceil (($max_impr_lvl / ($max_imprs + 1)) * ($impr_sum - 1)); # 1 improvemnt bonus my $levl = int max ($stat_lvl, $resist_lvl, $impr_lvl, $spec_lvl, 0); my $cost = $self->calc_costs; warn sprintf "%3d: %50s: %s\n", $levl, $self->{hash}->{name}, "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, " ."spec: $spec_lvl (num impr $stat_imprs + $res_imprs + $spec_imprs - 1)"; $levl } sub add_stat_costs { my ($self, $cost) = @_; my $stats = $self->{hash}->{stat}; for my $stat (keys %$stats) { my $sum = $stats->{$stat}; next unless $sum > 0; my $statfx = fx ($sum, 'stat_potions'); $cost->{"stat_$stat"} += $statfx->[0]; split_diamonds ($cost, $statfx->[1], 'stat_' . $stat); } } sub add_special_costs { my ($self, $cost) = @_; my $specials = $self->{hash}->{spec}; for my $spec (keys %$specials) { my $sum = $specials->{$spec}; next unless $sum > 0; my $specfx = fx ($sum, 'spec_potions'); $cost->{"spec_$spec"} += $specfx->[0]; split_diamonds ($cost, $specfx->[1], 'spec_' . $spec); } } sub calc_costs { my ($self) = @_; my $costs = {}; my $ring = $self->{hash}; for my $resnam (keys %{$ring->{resist} || {}}) { my $res = $ring->{resist}->{$resnam}; next unless $res > 0; $costs->{"resist_" . $Jeweler::RESMAP{$resnam} . "_item"} += $res; my $diamonds; if (grep { $resnam eq $_ } @Jeweler::EFFECT_RESISTS) { $diamonds += fx ($res, 'effect_resist_diamonds'); } else { $diamonds += fx ($res, 'attack_resist_diamonds'); } split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnam}); } $self->add_stat_costs ($costs); $self->add_special_costs ($costs); warn sprintf "JEWEL ANALYSE: %40s: %s" , $ring->{name}, join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs); return $costs; } sub split_diamonds { my ($cost, $diamonds, $category) = @_; my $stat_split = Jeweler::getcfg (diamond_split => $category); my $sum = sum (@$stat_split); if ($sum < (1 - 0.0001)) { warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!"; } my $emarch = Jeweler::get_arch ('emerald'); my $saarch = Jeweler::get_arch ('sapphire'); my $pearch = Jeweler::get_arch ('pearl'); my $ruarch = Jeweler::get_arch ('ruby'); my $diarch = Jeweler::get_arch ('gem'); my $sumvalue = $diarch->value * $diamonds; $cost->{emerald} += ceil (($sumvalue * $stat_split->[0]) / max (1, $emarch->value)); $cost->{sapphire} += ceil (($sumvalue * $stat_split->[1]) / max (1, $saarch->value)); $cost->{pearl} += ceil (($sumvalue * $stat_split->[2]) / max (1, $pearch->value)); $cost->{ruby} += ceil (($sumvalue * $stat_split->[3]) / max (1, $ruarch->value)); $cost->{gem} += ceil (($sumvalue * $stat_split->[4]) / max (1, $diarch->value)); } package Jeweler::Util; use strict; =head2 Util Some utility functions for the Jeweler skill. =over 4 =item remove ($object[, $nrof]) Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects. The returnvalue is the number of 'single' objects that couldn't be removed. =cut sub remove { my ($obj, $nrof) = @_; #XXX: waht about this: remove ($_) for ($obj->inv) ? my $cnt = $obj->nrof - (1 * $nrof); if ($cnt > 0) { $obj->set_nrof ($cnt); return 0; } else { $obj->remove; $obj->free; return $cnt; } } sub grep_for_match { my ($thing, @matchar) = @_; my $i = 0; for my $match (@matchar) { if ($match =~ m/^\s*$/) { $i++; next; } if ($i % 3 == 0) { $thing->name eq $match and return 1; } elsif ($i % 3 == 1) { $thing->title eq $match and return 1; } else { # $i % 3 == 2 $thing->archetype->name eq $match and return 1; } $i++; } return 0; } =back =back 1