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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines