ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/Jeweler.pm
(Generate patch)

Comparing deliantra/server/ext/Jeweler.pm (file contents):
Revision 1.8 by elmex, Sun Feb 4 00:35:23 2007 UTC vs.
Revision 1.36 by root, Tue May 4 22:49:21 2010 UTC

8 8
9=cut 9=cut
10 10
11package Jeweler; 11package Jeweler;
12 12
13use strict; 13use common::sense;
14use YAML;
15 14
16=over 4 15=over 4
17 16
18=item @RESISTS 17=item @RESISTS
19 18
21 20
22=cut 21=cut
23 22
24our $CFG; 23our $CFG;
25 24
26sub read_config { 25sub load_config {
27 my ($filename) = @_; 26 cf::trace "loading jeweler config from $cf::DATADIR/jeweler\n";
28 27
29 unless (-e $filename) { 28 0 < Coro::AIO::aio_load "$cf::DATADIR/jeweler", my $data
30 warn "$filename doesn't exists! no config for jeweler skill loaded!\n"; 29 or die "$cf::DATADIR/jeweler: $!";
31 $CFG = {};
32 return
33 }
34 30
35 $CFG = YAML::LoadFile $filename; 31 $CFG = cf::decode_json $data;
36} 32}
37 33
38sub getcfg { 34sub getcfg {
39 my ($sect, $key) = @_; 35 my ($sect, $key) = @_;
40 return $CFG->{$sect} unless defined $key; 36 return $CFG->{$sect} unless defined $key;
162 $delta = 5 if $delta > 5; 158 $delta = 5 if $delta > 5;
163 return $LVL_DIFF_CHANCES{$delta} 159 return $LVL_DIFF_CHANCES{$delta}
164} 160}
165 161
166sub analyze { 162sub analyze {
167 my ($sk, $chdl, $pl) = @_; 163 my ($sk, $chdl, $pl, $input_level) = @_;
168 164
169 my $hadunid = 0; 165 my $hadunid = 0;
166 my $found = 0;
170 for ($chdl->grep_by_type (cf::RING, cf::AMULET)) { 167 for ($chdl->grep_by_type (cf::RING, cf::AMULET)) {
171 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) { 168 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) {
172 $hadunid = 1; 169 $hadunid = 1;
173 next; 170 next;
174 } 171 }
172 $found = 1;
175 my $r = Jeweler::Object->new (object => $_); 173 my $r = Jeweler::Object->new (object => $_);
176 my $msg = $r->analyze ($sk, $pl); 174 my $msg = $r->analyze ($sk, $pl, $input_level);
177 $pl->message ($r->to_string . ": " . $msg); 175 $pl->message ($r->to_string . ": " . $msg);
178 if ($pl->flag (cf::FLAG_WIZ)) { 176 if ($pl->flag (cf::FLAG_WIZ)) {
179 $r->wiz_analyze ($pl); 177 $r->wiz_analyze ($pl);
180 } 178 }
181 } 179 }
182 if ($hadunid) {
183 $pl->message ("You couldn't identify the other rings and not analyze them!"); 180 $pl->message ("You couldn't identify the other rings and not analyze them!")
184 } 181 if $hadunid;
182 $pl->message ("You couldn't find anything in the bench to analyse!")
183 unless $found;
185} 184}
186 185
187# this function converts metals/minerals into a raw ring (of adornment) 186# this function converts metals/minerals into a raw ring (of adornment)
188sub simple_converter { 187sub simple_converter {
189 my ($pl, $ingred, $chdl, $conv) = @_; 188 my ($pl, $ingred, $chdl, $conv, $sk_lvl, $low_skill_cb) = @_;
190 189
191 $conv = lc $conv; 190 $conv = lc $conv;
192 my $cnvs = $CFG->{conversions}; 191 my $cnvs = $CFG->{conversions};
193 192
194 return unless $cnvs->{$conv}; 193 return unless $cnvs->{$conv};
203 warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!"; 202 warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!";
204 return; 203 return;
205 } 204 }
206 205
207 unless ($xp_gain > 0) { 206 unless ($xp_gain > 0) {
208 warn "WARNING: xp gain isn't > 0 in convesion '$outarch'\n"; 207 warn "WARNING: xp gain isn't > 0 in conversion '$outarch'\n";
209 return; 208 return;
210 } 209 }
211 210
212 unless ($outarchvalfact) { 211 unless ($outarchvalfact) {
213 warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n"; 212 warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n";
216 215
217 unless ($outarchvalfact >= 1) { 216 unless ($outarchvalfact >= 1) {
218 warn "WARNING: source-arch-value-multiplier < 1 in conversion '$outarch', results in more valuable output!\n"; 217 warn "WARNING: source-arch-value-multiplier < 1 in conversion '$outarch', results in more valuable output!\n";
219 } 218 }
220 219
221 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname); 220 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname);
222 $ingred->remove ($ingr_grp, $srcarchname);
223
224 my $outarchval = cf::arch::find ($outarch)->clone->value; 221 my $outarchval = cf::arch::find ($outarch)->value;
225
226 my $nrof = int ($archvalsum / (($outarchval || 1000) * $outarchvalfact)); 222 my $nrof = int $archvalsum / (($outarchval || 1000) * $outarchvalfact);
223 my $can_make_nr = int (($sk_lvl / 2) + 10);
224
225 if ($nrof > $can_make_nr) {
226 $pl->ob->message ("Your jeweler level is too low to make $nrof rings, you can only make $can_make_nr at your current level.");
227 return;
228 }
229
227 if ($nrof) { 230 if ($nrof) {
228 # XXX: yes, i know what i'm doing here, i don't set nrof, but it didn't work somehow (pls. chek sometimes) 231 # XXX: yes, I know what I'm doing here, I don't set nrof, but it didn't work somehow (pls. check sometimes)
232 $ingred->remove ($ingr_grp, $srcarchname);
229 for (1..$nrof) { 233 for (1 .. $nrof) {
230 $chdl->put (my $ob = cf::object::new $outarch); 234 $chdl->put (my $ob = cf::object::new $outarch);
231 $ob->set_animation (cf::rndm $ob->num_animations) 235 $ob->set_animation (cf::rndm $ob->num_animations)
232 if ($ob->type == cf::RING); 236 if ($ob->type == cf::RING);
233 $ob->flag (cf::FLAG_IDENTIFIED, 1); 237 $ob->flag (cf::FLAG_IDENTIFIED, 1);
234 } 238 }
235 239
236 my $xp_sum = ($xp_gain * $nrof); 240 my $xp_sum = $xp_gain * $nrof;
237 241
238 if ($xp_sum) { 242 if ($xp_sum) {
239 $pl->ob->message ("You got $xp_sum xp by making $nrof ${outarch}s"); 243 $pl->ob->message ("You got $xp_sum xp by making $nrof ${outarch}s");
240 $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_SKILL_ONLY); 244 $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_SKILL_ONLY);
241 } 245 }
242 } else { 246 } else {
243 $pl->ob->message ("You fail to make something, propably you used not enough source material?"); 247 $pl->ob->message ("You fail to make something, probably you used not enough source material?");
244 } 248 }
245} 249}
246 250
247 251
248package Jeweler::CauldronHandler; 252package Jeweler::CauldronHandler;
249 253
250use strict; 254use common::sense;
251 255
252=head2 CauldronHandler 256=head2 CauldronHandler
253 257
254The Jeweler::CauldronHandler package, that helps you with handling the 258The Jeweler::CauldronHandler package, that helps you with handling the
255cauldron stuff. Can also be used for other skills. 259cauldron stuff. Can also be used for other skills.
280 my ($self, $arch_name, @map_stack) = @_; 284 my ($self, $arch_name, @map_stack) = @_;
281 285
282 my @c = 286 my @c =
283 grep { 287 grep {
284 $_->flag (cf::FLAG_IS_CAULDRON) 288 $_->flag (cf::FLAG_IS_CAULDRON)
285 and $_->arch->name eq $arch_name 289 and $_->arch->archname eq $arch_name
286 } @map_stack; 290 } @map_stack;
287 291
288 $self->{cauldron} = $c[0]; 292 $self->{cauldron} = $c[0];
289} 293}
290 294
355 359
356sub put { 360sub put {
357 my ($self, $obj) = @_; 361 my ($self, $obj) = @_;
358 362
359 return undef unless $self->{cauldron}; 363 return undef unless $self->{cauldron};
360 $obj->insert_ob_in_ob ($self->{cauldron}); 364 $self->{cauldron}->insert ($obj);
361} 365}
362 366
363=back 367=back
364 368
365=cut 369=cut
366 370
367package Jeweler::Ingredients; 371package Jeweler::Ingredients;
372
373use common::sense;
374
368use Storable qw/dclone/; 375use Storable qw/dclone/;
369use strict;
370 376
371=head2 Ingredients 377=head2 Ingredients
372 378
373This class handles the ingredients. 379This class handles the ingredients.
374 380
398 404
399sub value { 405sub value {
400 my ($self, $group, $archname) = @_; 406 my ($self, $group, $archname) = @_;
401 407
402 my @objs = grep { 408 my @objs = grep {
403 $_->arch->name eq $archname 409 $_->arch->archname eq $archname
404 } @{$self->{ingredients}->{$group} || []}; 410 } @{$self->{ingredients}->{$group} || []};
405 411
406 my $sum = 0; 412 my $sum = 0;
407 for (@objs) { 413 for (@objs) {
408 $sum += ($_->nrof || 1) * $_->value; 414 $sum += ($_->nrof || 1) * $_->value;
425 431
426 my @out; 432 my @out;
427 433
428 for (@{$ingred->{$group}}) { 434 for (@{$ingred->{$group}}) {
429 if (defined $archname) { 435 if (defined $archname) {
430 if ($_->arch->name eq $archname) { 436 if ($_->arch->archname eq $archname) {
431 Jeweler::Util::remove ($_); 437 Jeweler::Util::remove ($_);
432 } else { 438 } else {
433 push @out, $_; 439 push @out, $_;
434 } 440 }
435 } else { 441 } else {
477} 483}
478 484
479sub improve_ring_by_plan { 485sub improve_ring_by_plan {
480 my ($self, $plan, $ring) = @_; 486 my ($self, $plan, $ring) = @_;
481 487
482 $ring = dclone ($ring); 488 $ring = dclone $ring;
483 489
484 my $ingred = $self->{ingredients}; 490 my $ingred = $self->{ingredients};
485 my $impr = {}; 491 my $impr = {};
486 492
487 if ($plan =~ m/^stat_(\S+)$/) { 493 if ($plan =~ m/^stat_(\S+)$/) {
571} 577}
572 578
573sub check_costs { 579sub check_costs {
574 my ($self, $costs, $do_remove) = @_; 580 my ($self, $costs, $do_remove) = @_;
575 581
576 my $costs = dclone ($costs); 582 my $costs = dclone $costs;
577 583
578 for my $key (keys %$costs) { 584 for my $key (keys %$costs) {
579 my @grepar; 585 my @grepar;
580 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items 586 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items
581 @grepar = @{Jeweler::getcfg (plans => $key) || []}; 587 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} };
588 next if $@;
582 } else { # check the gems 589 } else { # check the gems
583 @grepar = ('gems', undef, undef, $key); 590 @grepar = ('gems', undef, undef, $key);
584 } 591 }
585 592
586 if ($do_remove) { 593 if ($do_remove) {
587 my $rem = $costs->{$key}; 594 my $rem = $costs->{$key};
595 $self->do_grep (sub {
588 $self->do_grep (sub { if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); } 1 }, @grepar); 596 if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); }
597 1
598 }, @grepar);
589 if ($rem > 0) { 599 if ($rem > 0) {
590 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 600 warn "JEWELER BUG: removed ingredients ($key) $rem > 0 after removing!";
591 } 601 }
602
592 } else { 603 } else {
593 my $nr; 604 my $nr;
594 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar); 605 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
595 $costs->{$key} -= $nr; 606 $costs->{$key} -= $nr;
596 } 607 }
615 } 626 }
616 } 627 }
617} 628}
618 629
619package Jeweler::Object; 630package Jeweler::Object;
620use strict; 631
632use common::sense;
621use POSIX; 633use POSIX;
622use List::Util qw/max min sum/; 634use List::Util qw/max min sum/;
623 635
624sub new { 636sub new {
625 my ($class, %arg) = @_; 637 my ($class, %arg) = @_;
640 return 1 if $self->{hash}->{resist}->{$resnum}; 652 return 1 if $self->{hash}->{resist}->{$resnum};
641 } 653 }
642 return undef; 654 return undef;
643} 655}
644 656
657sub projected_exp {
658 my ($self, $input_level) = @_;
659
660 my $lvl = max ($self->power_to_level, 1);
661 my $exp =
662 (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1))
663 / (10 + max ($lvl - 1, 0)); # 10 + level times making such a ring
664 # should get you to the rings level at least.
665
666 if (defined $input_level) {
667 my $subexp =
668 (cf::level_to_min_exp ($input_level)
669 - cf::level_to_min_exp ($input_level - 1))
670 / (10 + max ($input_level - 1, 0)); # see above for comment
671
672 $exp -= $subexp;
673 $exp = max ($exp, 0);
674
675 } else {
676 # the experience bonus here is to make level 1 rings give you at least
677 # 200 exp points when making them. This also makes leveling in the
678 # first few levels a bit easier. (probably until around level 5-6).
679 my $expbonus = cf::level_to_min_exp (2) / 5;
680 # this bonus should also only be given for _new_ rings and not for merged
681 # ones - to prevent infinite exp making.
682 $exp += $expbonus;
683 }
684
685 $exp
686}
687
645sub analyze { 688sub analyze {
646 my ($self, $sk, $pl) = @_; 689 my ($self, $sk, $pl, $input_level) = @_;
690 my $costs = $self->calc_costs;
691
692 unless (defined $costs) {
693 return "This ring has a resistancy above 99%, you can't make that.";
694 }
647 695
648 my $sklvl = cf::exp_to_level ($sk->stats->exp); 696 my $sklvl = cf::exp_to_level ($sk->stats->exp);
649 my $ringlvl = $self->power_to_level; 697 my $ringlvl = $self->power_to_level;
650 698
651 my $tmpl; 699 my $tmpl;
652 if ($pl->flag (cf::FLAG_WIZ)) { 700 if ($pl->flag (cf::FLAG_WIZ)) {
653 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl); 701 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl);
654 } else { 702 } else {
655 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl); 703 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl);
656 } 704 }
657 my $msg = sprintf "Projected success rate: %s", $tmpl; 705 my $msg = sprintf "Projected success rate: %s, you would get %d exp for this.", $tmpl, $self->projected_exp ($input_level);
658 return $msg; 706 return $msg;
659} 707}
660 708
661sub wiz_analyze { 709sub calc_value_from_cost {
662 my ($self, $pl) = @_; 710 my ($self, $costs) = @_;
663 my $costs = $self->calc_costs;
664 my $desc = "";
665 my $lvl = $self->power_to_level (\$desc);
666 my $emarch = cf::arch::find 'emerald'; 711 my $emarch = cf::arch::find 'emerald';
667 my $saarch = cf::arch::find 'sapphire'; 712 my $saarch = cf::arch::find 'sapphire';
668 my $pearch = cf::arch::find 'pearl'; 713 my $pearch = cf::arch::find 'pearl';
669 my $ruarch = cf::arch::find 'ruby'; 714 my $ruarch = cf::arch::find 'ruby';
670 my $diarch = cf::arch::find 'gem'; 715 my $diarch = cf::arch::find 'gem';
671 my $scosts = $emarch->clone->value * $costs->{emerald} 716 my $value = $emarch->value * $costs->{emerald}
672 + $saarch->clone->value * $costs->{sapphire} 717 + $saarch->value * $costs->{sapphire}
673 + $pearch->clone->value * $costs->{pearl} 718 + $pearch->value * $costs->{pearl}
674 + $ruarch->clone->value * $costs->{ruby} 719 + $ruarch->value * $costs->{ruby}
675 + $diarch->clone->value * $costs->{gem}; 720 + $diarch->value * $costs->{gem};
676 721
677 $pl->message ("costs: " . (join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)) . " (".($scosts / 5000)." royalties)"); 722 $value
723}
724
725sub wiz_analyze {
726 my ($self, $pl) = @_;
727 my $costs = $self->calc_costs;
728 if (defined $costs) {
729 my $desc = "";
730 my $lvl = $self->power_to_level (\$desc);
731 my $scosts = $self->calc_value_from_cost ($costs);
732
733 $pl->message ("costs: "
734 . (join ', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)
735 . " ("
736 . ($scosts / "platinacoin"->cf::arch::find->value)
737 . " platinum)");
678 $pl->message ("level: " . $desc); 738 $pl->message ("level: $desc");
739 } else {
740 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
741 }
679} 742}
680
681 743
682sub get_chance_perc { 744sub get_chance_perc {
683 my ($self, $sk) = @_; 745 my ($self, $sk) = @_;
684 my $sklvl = cf::exp_to_level ($sk->stats->exp); 746 my $sklvl = cf::exp_to_level ($sk->stats->exp);
685 my $ringlvl = $self->power_to_level; 747 my $ringlvl = $self->power_to_level;
688 750
689sub fx { 751sub fx {
690 my ($res, $cfg) = @_; 752 my ($res, $cfg) = @_;
691 my $or = $res; 753 my $or = $res;
692 my $ar = $Jeweler::CFG->{functions}->{$cfg}; 754 my $ar = $Jeweler::CFG->{functions}->{$cfg};
755
693 if (ref $ar->[0] eq 'ARRAY') { 756 if (ref $ar && ref $ar->[0] eq 'ARRAY') {
694 $res = $res - 1; 757 $res = $res - 1;
758 return $ar->[max (min ($res, @$ar - 1), 0)];
759
695 } else { 760 } else {
696 $res = ceil ($res / 5) - 1; 761 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3...
697 } 762 # old code:
763 #my $idx = ceil (($res / 5) + 0.1) - 1;
698 $ar->[max (min ($res, @$ar - 1), 0)]; 764 #my $a = $ar->[max (min ($idx, @$ar - 1), 0)];
765 #my $b = $ar->[max (min ($idx + 1, @$ar - 1), 0)];
766 #my $diff = $b - $a; # use the difference of the cost to the next cost
767 #my $o_cost = $a + ($diff / 5) * ($res % 5); # and do some linear interpolation
768 #return $o_cost;
769 return 0 if $res <= 0;
770 return ($ar / (1 - ($res * 0.01)) - $ar)
771 }
699} 772}
700 773
701sub improve_by_ring { 774sub improve_by_ring {
702 my ($self, @rings) = @_; 775 my ($self, @rings) = @_;
703 my $ring = $self; 776 my $ring = $self;
719 if ($self->{hash}->{$cat}->{$k} > 0) { 792 if ($self->{hash}->{$cat}->{$k} > 0) {
720 $self->{hash}->{$cat}->{$k} *= -1; 793 $self->{hash}->{$cat}->{$k} *= -1;
721 } 794 }
722 } 795 }
723 } 796 }
797 $self->{hash}{value} = 0;
724} 798}
725 799
726sub to_string { 800sub to_string {
727 my ($self) = @_; 801 my ($self) = @_;
728 my $r = $self->{hash}; 802 my $r = $self->{hash};
767 $obj->{spec}{ac} = $stats->ac; 841 $obj->{spec}{ac} = $stats->ac;
768 $obj->{spec}{speed} = $stats->exp; 842 $obj->{spec}{speed} = $stats->exp;
769 $obj->{spec}{food} = $stats->food; 843 $obj->{spec}{food} = $stats->food;
770 844
771 $obj->{name} = $thing->name; 845 $obj->{name} = $thing->name;
772 $obj->{arch} = $thing->arch->name; 846 $obj->{arch} = $thing->arch->archname;
773 $obj->{face} = $thing->face; 847 $obj->{face} = $thing->face;
848
849 $obj->{value} = $thing->value;
850
851 $obj->{is_ring} = ($thing->type == cf::RING);
774 852
775 $self->{hash} = $obj 853 $self->{hash} = $obj
776} 854}
777 855
778sub to_object { 856sub to_object {
779 my ($self) = @_; 857 my ($self) = @_;
780 858
781 my $obj = cf::object::new $self->{hash}->{arch}; 859 my $obj = cf::object::new $self->{hash}->{arch};
782 860
783 $obj->item_power ($self->power_to_level); # there have to be strings attached! 861 $obj->item_power (floor ($self->power_to_level / 3)); # there have to be strings attached!
784 862
785 $obj->face ($self->{hash}{face}); 863 $obj->face ($self->{hash}{face});
786 864
787 my $stats = $obj->stats; 865 my $stats = $obj->stats;
788 866
801 $obj->resist ($_, $self->{hash}->{resist}->{$_}); 879 $obj->resist ($_, $self->{hash}->{resist}->{$_});
802 } 880 }
803 881
804 $obj->flag (cf::FLAG_IDENTIFIED, 1); 882 $obj->flag (cf::FLAG_IDENTIFIED, 1);
805 883
884 $obj->value ($self->{hash}{value});
885
806 return $obj; 886 return $obj;
807} 887}
888
889sub set_value { $_[0]->{hash}{value} = $_[1] }
808 890
809sub is_better_than { 891sub is_better_than {
810 my ($self, $other) = @_; 892 my ($self, $other) = @_;
811 893
812 for my $type (qw/spec stat resist/) { 894 for my $type (qw/spec stat resist/) {
837sub resist_level { 919sub resist_level {
838 my ($self) = @_; 920 my ($self) = @_;
839 921
840 my $resists = $self->{hash}->{resist} || {}; 922 my $resists = $self->{hash}->{resist} || {};
841 923
842 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level'); 924 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level');
843 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level'); 925 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level');
844 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances'); 926 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances');
845 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances'); 927 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances');
846 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances'); 928 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances');
847 929
848 my $ressum = 0; 930 my $ressum = 0;
908sub power_to_level { 990sub power_to_level {
909 my ($self, $lvldescr) = @_; 991 my ($self, $lvldescr) = @_;
910 992
911 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements'); 993 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements');
912 my $max_impr_lvl = Jeweler::getcfg (maxlevels => 'improve_level'); 994 my $max_impr_lvl = Jeweler::getcfg (maxlevels => 'improve_level');
995 my $ring_offs = Jeweler::getcfg (maxlevels => 'ring_offset');
913 996
914 my ($stat_lvl, $stat_imprs) = $self->stat_level; 997 my ($stat_lvl, $stat_imprs) = $self->stat_level;
915 my ($resist_lvl, $res_imprs) = $self->resist_level; 998 my ($resist_lvl, $res_imprs) = $self->resist_level;
916 my ($spec_lvl, $spec_imprs) = $self->special_level; 999 my ($spec_lvl, $spec_imprs) = $self->special_level;
917 1000
918 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs; 1001 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs;
919 1002
920 my $impr_lvl = ceil (($max_impr_lvl / ($max_imprs + 1)) * ($impr_sum - 1)); # 1 improvemnt bonus 1003 my $impr_lvl =
1004 ceil (($max_impr_lvl / ($max_imprs + 1))
1005 * ($impr_sum - 1)); # 1 improvemnt bonus
921 1006
922 my $levl = int max ($stat_lvl, $resist_lvl, $impr_lvl, $spec_lvl, 0); 1007 my $levl = int max ($stat_lvl, $resist_lvl, $impr_lvl, $spec_lvl, 0);
1008
1009 if ($self->{hash}->{is_ring}) {
1010 $levl += $ring_offs;
1011 }
1012
1013 $levl = min ($levl, cf::settings->max_level);
923 1014
924 if ($lvldescr) { 1015 if ($lvldescr) {
925 $$lvldescr = 1016 $$lvldescr =
926 sprintf "%3d: %s\n", $levl, 1017 sprintf "%3d: %s\n", $levl,
927 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, " 1018 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, "
974 1065
975 my $res = $ring->{resist}->{$resnum}; 1066 my $res = $ring->{resist}->{$resnum};
976 1067
977 next unless $res > 0; 1068 next unless $res > 0;
978 1069
1070 return undef if $res == 100;
1071
979 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res; 1072 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res;
980 1073
981 my $diamonds; 1074 my $diamonds;
982 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) { 1075 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) {
983 $diamonds += fx ($res, 'effect_resist_diamonds'); 1076 $diamonds += fx ($res, 'effect_resist_diamonds_x');
984 } else { 1077 } else {
985 $diamonds += fx ($res, 'attack_resist_diamonds'); 1078 $diamonds += fx ($res, 'attack_resist_diamonds_x');
986 } 1079 }
987 1080
988 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum}); 1081 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum});
989 } 1082 }
990 1083
998 my ($cost, $diamonds, $category) = @_; 1091 my ($cost, $diamonds, $category) = @_;
999 1092
1000 my $stat_split = Jeweler::getcfg (diamond_split => $category); 1093 my $stat_split = Jeweler::getcfg (diamond_split => $category);
1001 1094
1002 my $sum = sum (@$stat_split); 1095 my $sum = sum (@$stat_split);
1003 if ($sum < (1 - 0.0001)) {
1004 warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!";
1005 }
1006 1096
1007 my $emarch = cf::arch::find 'emerald'; 1097 my $emarch = cf::arch::find 'emerald';
1008 my $saarch = cf::arch::find 'sapphire'; 1098 my $saarch = cf::arch::find 'sapphire';
1009 my $pearch = cf::arch::find 'pearl'; 1099 my $pearch = cf::arch::find 'pearl';
1010 my $ruarch = cf::arch::find 'ruby'; 1100 my $ruarch = cf::arch::find 'ruby';
1011 my $diarch = cf::arch::find 'gem'; 1101 my $diarch = cf::arch::find 'gem';
1012 1102
1013 my $sumvalue = $diarch->clone->value * $diamonds; 1103 my $sumvalue = $diarch->value * $diamonds;
1014 1104
1015 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->clone->value; 1105 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->value;
1016 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->clone->value; 1106 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->value;
1017 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->clone->value; 1107 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->value;
1018 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->clone->value; 1108 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->value;
1019 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->clone->value; 1109 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value;
1020} 1110}
1021 1111
1022package Jeweler::Util; 1112package Jeweler::Util;
1023 1113
1024use strict; 1114use common::sense;
1025 1115
1026=head2 Util 1116=head2 Util
1027 1117
1028Some utility functions for the Jeweler skill. 1118Some utility functions for the Jeweler skill.
1029 1119
1030=over 4 1120=over 4
1031 1121
1032=item remove ($object[, $nrof]) 1122=item remove ($object[, $nrof])
1033 1123
1034Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects. 1124Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects.
1035The returnvalue is the number of 'single' objects that couldn't be removed. 1125The return value is the number of 'single' objects that couldn't be removed.
1036 1126
1037=cut 1127=cut
1038 1128
1039sub remove { 1129sub remove {
1040 my ($obj, $nrof) = @_; 1130 my ($obj, $nrof) = @_;
1131
1132 my $c = $obj->nrof || 1;
1133 my $r = $c > $nrof ? 0 : $nrof - $c;
1134 $obj->decrease (defined ($nrof) ? $nrof : ($obj->nrof || 1));
1041 1135
1042 my $cnt; 1136 $r
1043
1044 if (defined $nrof) {
1045 # TODO: Check tihis line:
1046 return 0 if ($nrof * 1) == 0; #XXX: ???
1047 $cnt = int (($obj->nrof || 1) - (1 * $nrof));
1048
1049 if ($cnt > 0) {
1050 $obj->nrof ($cnt);
1051 return 0;
1052 }
1053 }
1054
1055 remove ($_) for $obj->inv;
1056 $obj->destroy;
1057 return $cnt;
1058} 1137}
1059 1138
1060sub check_for_match { 1139sub check_for_match {
1061 my ($thing, @matchar) = @_; 1140 my ($thing, @matchar) = @_;
1062 1141
1063 my $i = 0; 1142 my $i = 0;
1143 my $check_cnts = 0;
1144 my $check_true = 0;
1064 for my $match (@matchar) { 1145 for my $match (@matchar) {
1146 if ($i % 3 == 0) {
1147 return 1 if $check_true && $check_cnts == $check_true;
1148 $check_cnts = 0;
1149 $check_true = 0;
1150 }
1151
1065 if ($match =~ m/^\s*$/) { 1152 if ($match =~ m/^\s*$/) {
1066 $i++; 1153 $i++;
1067 next; 1154 next;
1068 } 1155 }
1069 1156
1157 $check_cnts++;
1070 if ($i % 3 == 0) { 1158 if ($i % 3 == 0) {
1071 $thing->name eq $match 1159 $thing->name eq $match
1072 and return 1; 1160 and $check_true++;
1073 } elsif ($i % 3 == 1) { 1161 } elsif ($i % 3 == 1) {
1074 $thing->title eq $match 1162 $thing->title eq $match
1075 and return 1; 1163 and $check_true++;
1076 } else { # $i % 3 == 2 1164 } else { # $i % 3 == 2
1077 $thing->arch->name eq $match 1165 $thing->arch->archname eq $match
1078 and return 1; 1166 and $check_true++;
1079 } 1167 }
1080 $i++; 1168 $i++;
1081 } 1169 }
1170 #d# warn "CHECK $check_true | $check_cnts | [".(join ',', @matchar).":".join (",", ($thing->name, $thing->title, $thing->arch->archname))."\n";
1171 return 1 if $check_true && $check_cnts == $check_true;
1082 return 0; 1172 return 0;
1083} 1173}
1084 1174
1085sub grep_for_match { 1175sub grep_for_match {
1086 my ($ingred, $group, @matchar) = @_; 1176 my ($ingred, $group, @matchar) = @_;
1087 1177
1088 for my $thing (@{$ingred->{$group} || []}) { 1178 for my $thing (@{$ingred->{$group} || []}) {
1089 warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->name, "@matchar"; #d# 1179 #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->archname, "@matchar"; #d#
1090 if (check_for_match ($thing, @matchar)) { 1180 if (check_for_match ($thing, @matchar)) {
1091 return $thing; 1181 return $thing;
1092 } 1182 }
1093 } 1183 }
1094 return undef; 1184 return undef;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines