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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines