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.34 by root, Wed Apr 28 21:07:41 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 warn "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)
236 if ($ob->type == cf::RING);
232 $ob->flag (cf::FLAG_IDENTIFIED, 1); 237 $ob->flag (cf::FLAG_IDENTIFIED, 1);
233 } 238 }
234 239
235 my $xp_sum = ($xp_gain * $nrof); 240 my $xp_sum = $xp_gain * $nrof;
236 241
237 if ($xp_sum) { 242 if ($xp_sum) {
238 $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");
239 $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);
240 } 245 }
241 } else { 246 } else {
242 $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?");
243 } 248 }
244} 249}
245 250
246 251
247package Jeweler::CauldronHandler; 252package Jeweler::CauldronHandler;
279 my ($self, $arch_name, @map_stack) = @_; 284 my ($self, $arch_name, @map_stack) = @_;
280 285
281 my @c = 286 my @c =
282 grep { 287 grep {
283 $_->flag (cf::FLAG_IS_CAULDRON) 288 $_->flag (cf::FLAG_IS_CAULDRON)
284 and $_->arch->name eq $arch_name 289 and $_->arch->archname eq $arch_name
285 } @map_stack; 290 } @map_stack;
286 291
287 $self->{cauldron} = $c[0]; 292 $self->{cauldron} = $c[0];
288} 293}
289 294
328 cf::POTION => 'potions', 333 cf::POTION => 'potions',
329 cf::SCROLL => 'scrolls', 334 cf::SCROLL => 'scrolls',
330 ); 335 );
331 336
332 for ($self->{cauldron}->inv) { 337 for ($self->{cauldron}->inv) {
338 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) {
339 die "unidentified";
340 } elsif ($_->flag (cf::FLAG_CURSED) || $_->flag (cf::FLAG_DAMNED)) {
341 die "cursed";
342 }
333 343
334 if (my $k = $type_to_key{$_->type}) { 344 if (my $k = $type_to_key{$_->type}) {
335 push @{$ingreds->{$k}}, $_; 345 push @{$ingreds->{$k}}, $_;
336 } else { 346 } else {
337 push @{$ingreds->{other}}, $_; 347 push @{$ingreds->{other}}, $_;
349 359
350sub put { 360sub put {
351 my ($self, $obj) = @_; 361 my ($self, $obj) = @_;
352 362
353 return undef unless $self->{cauldron}; 363 return undef unless $self->{cauldron};
354 $obj->insert_ob_in_ob ($self->{cauldron}); 364 $self->{cauldron}->insert ($obj);
355} 365}
356 366
357=back 367=back
358 368
359=cut 369=cut
392 402
393sub value { 403sub value {
394 my ($self, $group, $archname) = @_; 404 my ($self, $group, $archname) = @_;
395 405
396 my @objs = grep { 406 my @objs = grep {
397 $_->arch->name eq $archname 407 $_->arch->archname eq $archname
398 } @{$self->{ingredients}->{$group} || []}; 408 } @{$self->{ingredients}->{$group} || []};
399 409
400 my $sum = 0; 410 my $sum = 0;
401 for (@objs) { 411 for (@objs) {
402 $sum += ($_->nrof || 1) * $_->value; 412 $sum += ($_->nrof || 1) * $_->value;
419 429
420 my @out; 430 my @out;
421 431
422 for (@{$ingred->{$group}}) { 432 for (@{$ingred->{$group}}) {
423 if (defined $archname) { 433 if (defined $archname) {
424 if ($_->arch->name eq $archname) { 434 if ($_->arch->archname eq $archname) {
425 Jeweler::Util::remove ($_); 435 Jeweler::Util::remove ($_);
426 } else { 436 } else {
427 push @out, $_; 437 push @out, $_;
428 } 438 }
429 } else { 439 } else {
471} 481}
472 482
473sub improve_ring_by_plan { 483sub improve_ring_by_plan {
474 my ($self, $plan, $ring) = @_; 484 my ($self, $plan, $ring) = @_;
475 485
476 $ring = dclone ($ring); 486 $ring = dclone $ring;
477 487
478 my $ingred = $self->{ingredients}; 488 my $ingred = $self->{ingredients};
479 my $impr = {}; 489 my $impr = {};
480 490
481 if ($plan =~ m/^stat_(\S+)$/) { 491 if ($plan =~ m/^stat_(\S+)$/) {
565} 575}
566 576
567sub check_costs { 577sub check_costs {
568 my ($self, $costs, $do_remove) = @_; 578 my ($self, $costs, $do_remove) = @_;
569 579
570 my $costs = dclone ($costs); 580 my $costs = dclone $costs;
571 581
572 for my $key (keys %$costs) { 582 for my $key (keys %$costs) {
573 my @grepar; 583 my @grepar;
574 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items 584 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items
575 @grepar = @{Jeweler::getcfg (plans => $key) || []}; 585 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} };
586 next if $@;
576 } else { # check the gems 587 } else { # check the gems
577 @grepar = ('gems', undef, undef, $key); 588 @grepar = ('gems', undef, undef, $key);
578 } 589 }
579 590
580 if ($do_remove) { 591 if ($do_remove) {
581 my $rem = $costs->{$key}; 592 my $rem = $costs->{$key};
593 $self->do_grep (sub {
582 $self->do_grep (sub { if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); } 1 }, @grepar); 594 if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); }
595 1
596 }, @grepar);
583 if ($rem > 0) { 597 if ($rem > 0) {
584 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 598 warn "JEWELER BUG: removed ingredients ($key) $rem > 0 after removing!";
585 } 599 }
600
586 } else { 601 } else {
587 my $nr; 602 my $nr;
588 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar); 603 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
589 $costs->{$key} -= $nr; 604 $costs->{$key} -= $nr;
590 warn "CHECKCOST $key <-> $nr -> $costs->{$key}\n";
591 } 605 }
592 606
593 } 607 }
594 608
595 return $costs; 609 return $costs;
624 $self->ring_or_ammy_to_hash ($arg{object}); 638 $self->ring_or_ammy_to_hash ($arg{object});
625 639
626 $self; 640 $self;
627} 641}
628 642
643sub has_resist {
644 my ($self, $resistnam, $resistval) = @_;
645 my $resnum = $REV_RESMAP{uc $resistnam};
646 if (defined ($resistval)) {
647 return 1 if $self->{hash}->{resist}->{$resnum} == $resistval;
648 } else {
649 return 1 if $self->{hash}->{resist}->{$resnum};
650 }
651 return undef;
652}
653
654sub projected_exp {
655 my ($self, $input_level) = @_;
656
657 my $lvl = max ($self->power_to_level, 1);
658 my $exp =
659 (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1))
660 / (10 + max ($lvl - 1, 0)); # 10 + level times making such a ring
661 # should get you to the rings level at least.
662
663 if (defined $input_level) {
664 my $subexp =
665 (cf::level_to_min_exp ($input_level)
666 - cf::level_to_min_exp ($input_level - 1))
667 / (10 + max ($input_level - 1, 0)); # see above for comment
668
669 $exp -= $subexp;
670 $exp = max ($exp, 0);
671
672 } else {
673 # the experience bonus here is to make level 1 rings give you at least
674 # 200 exp points when making them. This also makes leveling in the
675 # first few levels a bit easier. (probably until around level 5-6).
676 my $expbonus = cf::level_to_min_exp (2) / 5;
677 # this bonus should also only be given for _new_ rings and not for merged
678 # ones - to prevent infinite exp making.
679 $exp += $expbonus;
680 }
681
682 $exp
683}
684
629sub analyze { 685sub analyze {
630 my ($self, $sk, $pl) = @_; 686 my ($self, $sk, $pl, $input_level) = @_;
687 my $costs = $self->calc_costs;
688
689 unless (defined $costs) {
690 return "This ring has a resistancy above 99%, you can't make that.";
691 }
631 692
632 my $sklvl = cf::exp_to_level ($sk->stats->exp); 693 my $sklvl = cf::exp_to_level ($sk->stats->exp);
633 my $ringlvl = $self->power_to_level; 694 my $ringlvl = $self->power_to_level;
634 695
635 my $tmpl; 696 my $tmpl;
636 if ($pl->flag (cf::FLAG_WIZ)) { 697 if ($pl->flag (cf::FLAG_WIZ)) {
637 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl); 698 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl);
638 } else { 699 } else {
639 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl); 700 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl);
640 } 701 }
641 my $msg = sprintf "Projected success rate: %s", $tmpl; 702 my $msg = sprintf "Projected success rate: %s, you would get %d exp for this.", $tmpl, $self->projected_exp ($input_level);
642 return $msg; 703 return $msg;
704}
705
706sub calc_value_from_cost {
707 my ($self, $costs) = @_;
708 my $emarch = cf::arch::find 'emerald';
709 my $saarch = cf::arch::find 'sapphire';
710 my $pearch = cf::arch::find 'pearl';
711 my $ruarch = cf::arch::find 'ruby';
712 my $diarch = cf::arch::find 'gem';
713 my $value = $emarch->value * $costs->{emerald}
714 + $saarch->value * $costs->{sapphire}
715 + $pearch->value * $costs->{pearl}
716 + $ruarch->value * $costs->{ruby}
717 + $diarch->value * $costs->{gem};
718
719 $value
643} 720}
644 721
645sub wiz_analyze { 722sub wiz_analyze {
646 my ($self, $pl) = @_; 723 my ($self, $pl) = @_;
647 my $costs = $self->calc_costs; 724 my $costs = $self->calc_costs;
725 if (defined $costs) {
648 my $desc = ""; 726 my $desc = "";
649 my $lvl = $self->power_to_level (\$desc); 727 my $lvl = $self->power_to_level (\$desc);
728 my $scosts = $self->calc_value_from_cost ($costs);
729
730 $pl->message ("costs: "
650 $pl->message ("costs: " . join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)); 731 . (join ', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)
732 . " ("
733 . ($scosts / "platinacoin"->cf::arch::find->value)
734 . " platinum)");
651 $pl->message ("level: " . $desc); 735 $pl->message ("level: $desc");
736 } else {
737 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
738 }
652} 739}
653
654 740
655sub get_chance_perc { 741sub get_chance_perc {
656 my ($self, $sk) = @_; 742 my ($self, $sk) = @_;
657 my $sklvl = cf::exp_to_level ($sk->stats->exp); 743 my $sklvl = cf::exp_to_level ($sk->stats->exp);
658 my $ringlvl = $self->power_to_level; 744 my $ringlvl = $self->power_to_level;
661 747
662sub fx { 748sub fx {
663 my ($res, $cfg) = @_; 749 my ($res, $cfg) = @_;
664 my $or = $res; 750 my $or = $res;
665 my $ar = $Jeweler::CFG->{functions}->{$cfg}; 751 my $ar = $Jeweler::CFG->{functions}->{$cfg};
752
666 if (ref $ar->[0] eq 'ARRAY') { 753 if (ref $ar && ref $ar->[0] eq 'ARRAY') {
667 $res = $res - 1; 754 $res = $res - 1;
755 return $ar->[max (min ($res, @$ar - 1), 0)];
756
668 } else { 757 } else {
669 $res = ceil ($res / 5) - 1; 758 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3...
670 } 759 # old code:
760 #my $idx = ceil (($res / 5) + 0.1) - 1;
671 $ar->[max (min ($res, @$ar - 1), 0)]; 761 #my $a = $ar->[max (min ($idx, @$ar - 1), 0)];
762 #my $b = $ar->[max (min ($idx + 1, @$ar - 1), 0)];
763 #my $diff = $b - $a; # use the difference of the cost to the next cost
764 #my $o_cost = $a + ($diff / 5) * ($res % 5); # and do some linear interpolation
765 #return $o_cost;
766 return 0 if $res <= 0;
767 return ($ar / (1 - ($res * 0.01)) - $ar)
768 }
672} 769}
673 770
674sub improve_by_ring { 771sub improve_by_ring {
675 my ($self, @rings) = @_; 772 my ($self, @rings) = @_;
676 my $ring = $self; 773 my $ring = $self;
692 if ($self->{hash}->{$cat}->{$k} > 0) { 789 if ($self->{hash}->{$cat}->{$k} > 0) {
693 $self->{hash}->{$cat}->{$k} *= -1; 790 $self->{hash}->{$cat}->{$k} *= -1;
694 } 791 }
695 } 792 }
696 } 793 }
794 $self->{hash}{value} = 0;
697} 795}
698 796
699sub to_string { 797sub to_string {
700 my ($self) = @_; 798 my ($self) = @_;
701 my $r = $self->{hash}; 799 my $r = $self->{hash};
740 $obj->{spec}{ac} = $stats->ac; 838 $obj->{spec}{ac} = $stats->ac;
741 $obj->{spec}{speed} = $stats->exp; 839 $obj->{spec}{speed} = $stats->exp;
742 $obj->{spec}{food} = $stats->food; 840 $obj->{spec}{food} = $stats->food;
743 841
744 $obj->{name} = $thing->name; 842 $obj->{name} = $thing->name;
745 $obj->{arch} = $thing->arch->name; 843 $obj->{arch} = $thing->arch->archname;
746 $obj->{face} = $thing->face; 844 $obj->{face} = $thing->face;
845
846 $obj->{value} = $thing->value;
847
848 $obj->{is_ring} = ($thing->type == cf::RING);
747 849
748 $self->{hash} = $obj 850 $self->{hash} = $obj
749} 851}
750 852
751sub to_object { 853sub to_object {
752 my ($self) = @_; 854 my ($self) = @_;
753 855
754 my $obj = cf::object::new $self->{hash}->{arch}; 856 my $obj = cf::object::new $self->{hash}->{arch};
857
858 $obj->item_power (floor ($self->power_to_level / 3)); # there have to be strings attached!
755 859
756 $obj->face ($self->{hash}{face}); 860 $obj->face ($self->{hash}{face});
757 861
758 my $stats = $obj->stats; 862 my $stats = $obj->stats;
759 863
772 $obj->resist ($_, $self->{hash}->{resist}->{$_}); 876 $obj->resist ($_, $self->{hash}->{resist}->{$_});
773 } 877 }
774 878
775 $obj->flag (cf::FLAG_IDENTIFIED, 1); 879 $obj->flag (cf::FLAG_IDENTIFIED, 1);
776 880
881 $obj->value ($self->{hash}{value});
882
777 return $obj; 883 return $obj;
778} 884}
885
886sub set_value { $_[0]->{hash}{value} = $_[1] }
779 887
780sub is_better_than { 888sub is_better_than {
781 my ($self, $other) = @_; 889 my ($self, $other) = @_;
782 890
783 for my $type (qw/spec stat resist/) { 891 for my $type (qw/spec stat resist/) {
808sub resist_level { 916sub resist_level {
809 my ($self) = @_; 917 my ($self) = @_;
810 918
811 my $resists = $self->{hash}->{resist} || {}; 919 my $resists = $self->{hash}->{resist} || {};
812 920
813 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level'); 921 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level');
814 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level'); 922 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level');
815 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances'); 923 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances');
816 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances'); 924 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances');
817 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances'); 925 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances');
818 926
819 my $ressum = 0; 927 my $ressum = 0;
879sub power_to_level { 987sub power_to_level {
880 my ($self, $lvldescr) = @_; 988 my ($self, $lvldescr) = @_;
881 989
882 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements'); 990 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements');
883 my $max_impr_lvl = Jeweler::getcfg (maxlevels => 'improve_level'); 991 my $max_impr_lvl = Jeweler::getcfg (maxlevels => 'improve_level');
992 my $ring_offs = Jeweler::getcfg (maxlevels => 'ring_offset');
884 993
885 my ($stat_lvl, $stat_imprs) = $self->stat_level; 994 my ($stat_lvl, $stat_imprs) = $self->stat_level;
886 my ($resist_lvl, $res_imprs) = $self->resist_level; 995 my ($resist_lvl, $res_imprs) = $self->resist_level;
887 my ($spec_lvl, $spec_imprs) = $self->special_level; 996 my ($spec_lvl, $spec_imprs) = $self->special_level;
888 997
889 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs; 998 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs;
890 999
891 my $impr_lvl = ceil (($max_impr_lvl / ($max_imprs + 1)) * ($impr_sum - 1)); # 1 improvemnt bonus 1000 my $impr_lvl =
1001 ceil (($max_impr_lvl / ($max_imprs + 1))
1002 * ($impr_sum - 1)); # 1 improvemnt bonus
892 1003
893 my $levl = int max ($stat_lvl, $resist_lvl, $impr_lvl, $spec_lvl, 0); 1004 my $levl = int max ($stat_lvl, $resist_lvl, $impr_lvl, $spec_lvl, 0);
1005
1006 if ($self->{hash}->{is_ring}) {
1007 $levl += $ring_offs;
1008 }
1009
1010 $levl = min ($levl, cf::settings->max_level);
894 1011
895 if ($lvldescr) { 1012 if ($lvldescr) {
896 $$lvldescr = 1013 $$lvldescr =
897 sprintf "%3d: %s\n", $levl, 1014 sprintf "%3d: %s\n", $levl,
898 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, " 1015 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, "
945 1062
946 my $res = $ring->{resist}->{$resnum}; 1063 my $res = $ring->{resist}->{$resnum};
947 1064
948 next unless $res > 0; 1065 next unless $res > 0;
949 1066
1067 return undef if $res == 100;
1068
950 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res; 1069 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res;
951 1070
952 my $diamonds; 1071 my $diamonds;
953 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) { 1072 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) {
954 $diamonds += fx ($res, 'effect_resist_diamonds'); 1073 $diamonds += fx ($res, 'effect_resist_diamonds_x');
955 } else { 1074 } else {
956 $diamonds += fx ($res, 'attack_resist_diamonds'); 1075 $diamonds += fx ($res, 'attack_resist_diamonds_x');
957 } 1076 }
958 1077
959 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum}); 1078 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum});
960 } 1079 }
961 1080
969 my ($cost, $diamonds, $category) = @_; 1088 my ($cost, $diamonds, $category) = @_;
970 1089
971 my $stat_split = Jeweler::getcfg (diamond_split => $category); 1090 my $stat_split = Jeweler::getcfg (diamond_split => $category);
972 1091
973 my $sum = sum (@$stat_split); 1092 my $sum = sum (@$stat_split);
974 if ($sum < (1 - 0.0001)) {
975 warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!";
976 }
977 1093
978 my $emarch = cf::arch::find 'emerald'; 1094 my $emarch = cf::arch::find 'emerald';
979 my $saarch = cf::arch::find 'sapphire'; 1095 my $saarch = cf::arch::find 'sapphire';
980 my $pearch = cf::arch::find 'pearl'; 1096 my $pearch = cf::arch::find 'pearl';
981 my $ruarch = cf::arch::find 'ruby'; 1097 my $ruarch = cf::arch::find 'ruby';
982 my $diarch = cf::arch::find 'gem'; 1098 my $diarch = cf::arch::find 'gem';
983 1099
984 my $sumvalue = $diarch->clone->value * $diamonds; 1100 my $sumvalue = $diarch->value * $diamonds;
985 1101
986 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->clone->value; 1102 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->value;
987 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->clone->value; 1103 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->value;
988 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->clone->value; 1104 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->value;
989 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->clone->value; 1105 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->value;
990 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->clone->value; 1106 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value;
991} 1107}
992 1108
993package Jeweler::Util; 1109package Jeweler::Util;
994 1110
995use strict; 1111use strict;
1001=over 4 1117=over 4
1002 1118
1003=item remove ($object[, $nrof]) 1119=item remove ($object[, $nrof])
1004 1120
1005Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects. 1121Removes 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. 1122The return value is the number of 'single' objects that couldn't be removed.
1007 1123
1008=cut 1124=cut
1009 1125
1010sub remove { 1126sub remove {
1011 my ($obj, $nrof) = @_; 1127 my ($obj, $nrof) = @_;
1128
1129 my $c = $obj->nrof || 1;
1130 my $r = $c > $nrof ? 0 : $nrof - $c;
1131 $obj->decrease (defined ($nrof) ? $nrof : ($obj->nrof || 1));
1012 1132
1013 my $cnt; 1133 $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} 1134}
1030 1135
1031sub check_for_match { 1136sub check_for_match {
1032 my ($thing, @matchar) = @_; 1137 my ($thing, @matchar) = @_;
1033 1138
1034 my $i = 0; 1139 my $i = 0;
1140 my $check_cnts = 0;
1141 my $check_true = 0;
1035 for my $match (@matchar) { 1142 for my $match (@matchar) {
1143 if ($i % 3 == 0) {
1144 return 1 if $check_true && $check_cnts == $check_true;
1145 $check_cnts = 0;
1146 $check_true = 0;
1147 }
1148
1036 if ($match =~ m/^\s*$/) { 1149 if ($match =~ m/^\s*$/) {
1037 $i++; 1150 $i++;
1038 next; 1151 next;
1039 } 1152 }
1040 1153
1154 $check_cnts++;
1041 if ($i % 3 == 0) { 1155 if ($i % 3 == 0) {
1042 $thing->name eq $match 1156 $thing->name eq $match
1043 and return 1; 1157 and $check_true++;
1044 } elsif ($i % 3 == 1) { 1158 } elsif ($i % 3 == 1) {
1045 $thing->title eq $match 1159 $thing->title eq $match
1046 and return 1; 1160 and $check_true++;
1047 } else { # $i % 3 == 2 1161 } else { # $i % 3 == 2
1048 $thing->arch->name eq $match 1162 $thing->arch->archname eq $match
1049 and return 1; 1163 and $check_true++;
1050 } 1164 }
1051 $i++; 1165 $i++;
1052 } 1166 }
1167 #d# warn "CHECK $check_true | $check_cnts | [".(join ',', @matchar).":".join (",", ($thing->name, $thing->title, $thing->arch->archname))."\n";
1168 return 1 if $check_true && $check_cnts == $check_true;
1053 return 0; 1169 return 0;
1054} 1170}
1055 1171
1056sub grep_for_match { 1172sub grep_for_match {
1057 my ($ingred, $group, @matchar) = @_; 1173 my ($ingred, $group, @matchar) = @_;
1058 1174
1059 for my $thing (@{$ingred->{$group} || []}) { 1175 for my $thing (@{$ingred->{$group} || []}) {
1060 warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->name, "@matchar"; #d# 1176 #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)) { 1177 if (check_for_match ($thing, @matchar)) {
1062 return $thing; 1178 return $thing;
1063 } 1179 }
1064 } 1180 }
1065 return undef; 1181 return undef;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines