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.6 by elmex, Thu Feb 1 01:46:45 2007 UTC vs.
Revision 1.30 by elmex, Tue Jan 26 16:13:47 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
354 361
355sub put { 362sub put {
356 my ($self, $obj) = @_; 363 my ($self, $obj) = @_;
357 364
358 return undef unless $self->{cauldron}; 365 return undef unless $self->{cauldron};
359 $obj->insert_ob_in_ob ($self->{cauldron}); 366 $self->{cauldron}->insert ($obj);
360} 367}
361 368
362=back 369=back
363 370
364=cut 371=cut
397 404
398sub value { 405sub value {
399 my ($self, $group, $archname) = @_; 406 my ($self, $group, $archname) = @_;
400 407
401 my @objs = grep { 408 my @objs = grep {
402 $_->arch->name eq $archname 409 $_->arch->archname eq $archname
403 } @{$self->{ingredients}->{$group} || []}; 410 } @{$self->{ingredients}->{$group} || []};
404 411
405 my $sum = 0; 412 my $sum = 0;
406 for (@objs) { 413 for (@objs) {
407 $sum += ($_->nrof || 1) * $_->value; 414 $sum += ($_->nrof || 1) * $_->value;
424 431
425 my @out; 432 my @out;
426 433
427 for (@{$ingred->{$group}}) { 434 for (@{$ingred->{$group}}) {
428 if (defined $archname) { 435 if (defined $archname) {
429 if ($_->arch->name eq $archname) { 436 if ($_->arch->archname eq $archname) {
430 Jeweler::Util::remove ($_); 437 Jeweler::Util::remove ($_);
431 } else { 438 } else {
432 push @out, $_; 439 push @out, $_;
433 } 440 }
434 } else { 441 } else {
476} 483}
477 484
478sub improve_ring_by_plan { 485sub improve_ring_by_plan {
479 my ($self, $plan, $ring) = @_; 486 my ($self, $plan, $ring) = @_;
480 487
481 $ring = dclone ($ring); 488 $ring = dclone $ring;
482 489
483 my $ingred = $self->{ingredients}; 490 my $ingred = $self->{ingredients};
484 my $impr = {}; 491 my $impr = {};
485 492
486 if ($plan =~ m/^stat_(\S+)$/) { 493 if ($plan =~ m/^stat_(\S+)$/) {
570} 577}
571 578
572sub check_costs { 579sub check_costs {
573 my ($self, $costs, $do_remove) = @_; 580 my ($self, $costs, $do_remove) = @_;
574 581
575 my $costs = dclone ($costs); 582 my $costs = dclone $costs;
576 583
577 for my $key (keys %$costs) { 584 for my $key (keys %$costs) {
578 my @grepar; 585 my @grepar;
579 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items 586 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items
580 @grepar = @{Jeweler::getcfg (plans => $key) || []}; 587 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} };
588 next if $@;
581 } else { # check the gems 589 } else { # check the gems
582 @grepar = ('gems', undef, undef, $key); 590 @grepar = ('gems', undef, undef, $key);
583 } 591 }
584 592
585 if ($do_remove) { 593 if ($do_remove) {
586 my $rem = $costs->{$key}; 594 my $rem = $costs->{$key};
595 $self->do_grep (sub {
587 $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);
588 if ($rem > 0) { 599 if ($rem > 0) {
589 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 600 warn "JEWELER BUG: removed ingredients ($key) $rem > 0 after removing!";
590 } 601 }
602
591 } else { 603 } else {
592 my $nr; 604 my $nr;
593 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar); 605 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
594 $costs->{$key} -= $nr; 606 $costs->{$key} -= $nr;
595 } 607 }
628 $self->ring_or_ammy_to_hash ($arg{object}); 640 $self->ring_or_ammy_to_hash ($arg{object});
629 641
630 $self; 642 $self;
631} 643}
632 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
633sub analyze { 687sub analyze {
634 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 }
635 694
636 my $sklvl = cf::exp_to_level ($sk->stats->exp); 695 my $sklvl = cf::exp_to_level ($sk->stats->exp);
637 my $ringlvl = $self->power_to_level; 696 my $ringlvl = $self->power_to_level;
638 697
639 my $tmpl; 698 my $tmpl;
640 if ($pl->flag (cf::FLAG_WIZ)) { 699 if ($pl->flag (cf::FLAG_WIZ)) {
641 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl); 700 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl);
642 } else { 701 } else {
643 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl); 702 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl);
644 } 703 }
645 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);
646 return $msg; 705 return $msg;
647} 706}
648 707
649sub wiz_analyze { 708sub calc_value_from_cost {
650 my ($self, $pl) = @_; 709 my ($self, $costs) = @_;
651 my $costs = $self->calc_costs;
652 my $desc = "";
653 my $lvl = $self->power_to_level (\$desc);
654 my $emarch = cf::arch::find 'emerald'; 710 my $emarch = cf::arch::find 'emerald';
655 my $saarch = cf::arch::find 'sapphire'; 711 my $saarch = cf::arch::find 'sapphire';
656 my $pearch = cf::arch::find 'pearl'; 712 my $pearch = cf::arch::find 'pearl';
657 my $ruarch = cf::arch::find 'ruby'; 713 my $ruarch = cf::arch::find 'ruby';
658 my $diarch = cf::arch::find 'gem'; 714 my $diarch = cf::arch::find 'gem';
659 my $scosts = $emarch->clone->value * $costs->{emerald} 715 my $value = $emarch->value * $costs->{emerald}
660 + $saarch->clone->value * $costs->{sapphire} 716 + $saarch->value * $costs->{sapphire}
661 + $pearch->clone->value * $costs->{pearl} 717 + $pearch->value * $costs->{pearl}
662 + $ruarch->clone->value * $costs->{ruby} 718 + $ruarch->value * $costs->{ruby}
663 + $diarch->clone->value * $costs->{gem}; 719 + $diarch->value * $costs->{gem};
664 720
665 $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)");
666 $pl->message ("level: " . $desc); 737 $pl->message ("level: $desc");
738 } else {
739 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
740 }
667} 741}
668
669 742
670sub get_chance_perc { 743sub get_chance_perc {
671 my ($self, $sk) = @_; 744 my ($self, $sk) = @_;
672 my $sklvl = cf::exp_to_level ($sk->stats->exp); 745 my $sklvl = cf::exp_to_level ($sk->stats->exp);
673 my $ringlvl = $self->power_to_level; 746 my $ringlvl = $self->power_to_level;
676 749
677sub fx { 750sub fx {
678 my ($res, $cfg) = @_; 751 my ($res, $cfg) = @_;
679 my $or = $res; 752 my $or = $res;
680 my $ar = $Jeweler::CFG->{functions}->{$cfg}; 753 my $ar = $Jeweler::CFG->{functions}->{$cfg};
754
681 if (ref $ar->[0] eq 'ARRAY') { 755 if (ref $ar && ref $ar->[0] eq 'ARRAY') {
682 $res = $res - 1; 756 $res = $res - 1;
757 return $ar->[max (min ($res, @$ar - 1), 0)];
758
683 } else { 759 } else {
684 $res = ceil ($res / 5) - 1; 760 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3...
685 } 761 # old code:
762 #my $idx = ceil (($res / 5) + 0.1) - 1;
686 $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 }
687} 771}
688 772
689sub improve_by_ring { 773sub improve_by_ring {
690 my ($self, @rings) = @_; 774 my ($self, @rings) = @_;
691 my $ring = $self; 775 my $ring = $self;
707 if ($self->{hash}->{$cat}->{$k} > 0) { 791 if ($self->{hash}->{$cat}->{$k} > 0) {
708 $self->{hash}->{$cat}->{$k} *= -1; 792 $self->{hash}->{$cat}->{$k} *= -1;
709 } 793 }
710 } 794 }
711 } 795 }
796 $self->{hash}{value} = 0;
712} 797}
713 798
714sub to_string { 799sub to_string {
715 my ($self) = @_; 800 my ($self) = @_;
716 my $r = $self->{hash}; 801 my $r = $self->{hash};
755 $obj->{spec}{ac} = $stats->ac; 840 $obj->{spec}{ac} = $stats->ac;
756 $obj->{spec}{speed} = $stats->exp; 841 $obj->{spec}{speed} = $stats->exp;
757 $obj->{spec}{food} = $stats->food; 842 $obj->{spec}{food} = $stats->food;
758 843
759 $obj->{name} = $thing->name; 844 $obj->{name} = $thing->name;
760 $obj->{arch} = $thing->arch->name; 845 $obj->{arch} = $thing->arch->archname;
761 $obj->{face} = $thing->face; 846 $obj->{face} = $thing->face;
847
848 $obj->{value} = $thing->value;
762 849
763 $self->{hash} = $obj 850 $self->{hash} = $obj
764} 851}
765 852
766sub to_object { 853sub to_object {
767 my ($self) = @_; 854 my ($self) = @_;
768 855
769 my $obj = cf::object::new $self->{hash}->{arch}; 856 my $obj = cf::object::new $self->{hash}->{arch};
770 857
771 $obj->item_power ($self->power_to_level); # there have to be strings attached! 858 $obj->item_power (floor ($self->power_to_level / 3)); # there have to be strings attached!
772 859
773 $obj->face ($self->{hash}{face}); 860 $obj->face ($self->{hash}{face});
774 861
775 my $stats = $obj->stats; 862 my $stats = $obj->stats;
776 863
789 $obj->resist ($_, $self->{hash}->{resist}->{$_}); 876 $obj->resist ($_, $self->{hash}->{resist}->{$_});
790 } 877 }
791 878
792 $obj->flag (cf::FLAG_IDENTIFIED, 1); 879 $obj->flag (cf::FLAG_IDENTIFIED, 1);
793 880
881 $obj->value ($self->{hash}{value});
882
794 return $obj; 883 return $obj;
795} 884}
885
886sub set_value { $_[0]->{hash}{value} = $_[1] }
796 887
797sub is_better_than { 888sub is_better_than {
798 my ($self, $other) = @_; 889 my ($self, $other) = @_;
799 890
800 for my $type (qw/spec stat resist/) { 891 for my $type (qw/spec stat resist/) {
962 1053
963 my $res = $ring->{resist}->{$resnum}; 1054 my $res = $ring->{resist}->{$resnum};
964 1055
965 next unless $res > 0; 1056 next unless $res > 0;
966 1057
1058 return undef if $res == 100;
1059
967 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res; 1060 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res;
968 1061
969 my $diamonds; 1062 my $diamonds;
970 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) { 1063 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) {
971 $diamonds += fx ($res, 'effect_resist_diamonds'); 1064 $diamonds += fx ($res, 'effect_resist_diamonds_x');
972 } else { 1065 } else {
973 $diamonds += fx ($res, 'attack_resist_diamonds'); 1066 $diamonds += fx ($res, 'attack_resist_diamonds_x');
974 } 1067 }
975 1068
976 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum}); 1069 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum});
977 } 1070 }
978 1071
986 my ($cost, $diamonds, $category) = @_; 1079 my ($cost, $diamonds, $category) = @_;
987 1080
988 my $stat_split = Jeweler::getcfg (diamond_split => $category); 1081 my $stat_split = Jeweler::getcfg (diamond_split => $category);
989 1082
990 my $sum = sum (@$stat_split); 1083 my $sum = sum (@$stat_split);
991 if ($sum < (1 - 0.0001)) {
992 warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!";
993 }
994 1084
995 my $emarch = cf::arch::find 'emerald'; 1085 my $emarch = cf::arch::find 'emerald';
996 my $saarch = cf::arch::find 'sapphire'; 1086 my $saarch = cf::arch::find 'sapphire';
997 my $pearch = cf::arch::find 'pearl'; 1087 my $pearch = cf::arch::find 'pearl';
998 my $ruarch = cf::arch::find 'ruby'; 1088 my $ruarch = cf::arch::find 'ruby';
999 my $diarch = cf::arch::find 'gem'; 1089 my $diarch = cf::arch::find 'gem';
1000 1090
1001 my $sumvalue = $diarch->clone->value * $diamonds; 1091 my $sumvalue = $diarch->value * $diamonds;
1002 1092
1003 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->clone->value; 1093 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->value;
1004 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->clone->value; 1094 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->value;
1005 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->clone->value; 1095 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->value;
1006 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->clone->value; 1096 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->value;
1007 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->clone->value; 1097 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value;
1008} 1098}
1009 1099
1010package Jeweler::Util; 1100package Jeweler::Util;
1011 1101
1012use strict; 1102use strict;
1018=over 4 1108=over 4
1019 1109
1020=item remove ($object[, $nrof]) 1110=item remove ($object[, $nrof])
1021 1111
1022Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects. 1112Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects.
1023The returnvalue is the number of 'single' objects that couldn't be removed. 1113The return value is the number of 'single' objects that couldn't be removed.
1024 1114
1025=cut 1115=cut
1026 1116
1027sub remove { 1117sub remove {
1028 my ($obj, $nrof) = @_; 1118 my ($obj, $nrof) = @_;
1119
1120 my $c = $obj->nrof || 1;
1121 my $r = $c > $nrof ? 0 : $nrof - $c;
1122 $obj->decrease (defined ($nrof) ? $nrof : ($obj->nrof || 1));
1029 1123
1030 my $cnt; 1124 $r
1031
1032 if (defined $nrof) {
1033 # TODO: Check tihis line:
1034 return 0 if ($nrof * 1) == 0; #XXX: ???
1035 $cnt = int (($obj->nrof || 1) - (1 * $nrof));
1036
1037 if ($cnt > 0) {
1038 $obj->nrof ($cnt);
1039 return 0;
1040 }
1041 }
1042
1043 remove ($_) for $obj->inv;
1044 $obj->destroy;
1045 return $cnt;
1046} 1125}
1047 1126
1048sub check_for_match { 1127sub check_for_match {
1049 my ($thing, @matchar) = @_; 1128 my ($thing, @matchar) = @_;
1050 1129
1051 my $i = 0; 1130 my $i = 0;
1131 my $check_cnts = 0;
1132 my $check_true = 0;
1052 for my $match (@matchar) { 1133 for my $match (@matchar) {
1134 if ($i % 3 == 0) {
1135 return 1 if $check_true && $check_cnts == $check_true;
1136 $check_cnts = 0;
1137 $check_true = 0;
1138 }
1139
1053 if ($match =~ m/^\s*$/) { 1140 if ($match =~ m/^\s*$/) {
1054 $i++; 1141 $i++;
1055 next; 1142 next;
1056 } 1143 }
1057 1144
1145 $check_cnts++;
1058 if ($i % 3 == 0) { 1146 if ($i % 3 == 0) {
1059 $thing->name eq $match 1147 $thing->name eq $match
1060 and return 1; 1148 and $check_true++;
1061 } elsif ($i % 3 == 1) { 1149 } elsif ($i % 3 == 1) {
1062 $thing->title eq $match 1150 $thing->title eq $match
1063 and return 1; 1151 and $check_true++;
1064 } else { # $i % 3 == 2 1152 } else { # $i % 3 == 2
1065 $thing->arch->name eq $match 1153 $thing->arch->archname eq $match
1066 and return 1; 1154 and $check_true++;
1067 } 1155 }
1068 $i++; 1156 $i++;
1069 } 1157 }
1158 #d# warn "CHECK $check_true | $check_cnts | [".(join ',', @matchar).":".join (",", ($thing->name, $thing->title, $thing->arch->archname))."\n";
1159 return 1 if $check_true && $check_cnts == $check_true;
1070 return 0; 1160 return 0;
1071} 1161}
1072 1162
1073sub grep_for_match { 1163sub grep_for_match {
1074 my ($ingred, $group, @matchar) = @_; 1164 my ($ingred, $group, @matchar) = @_;
1075 1165
1076 for my $thing (@{$ingred->{$group} || []}) { 1166 for my $thing (@{$ingred->{$group} || []}) {
1077 warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->name, "@matchar"; #d# 1167 #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->archname, "@matchar"; #d#
1078 if (check_for_match ($thing, @matchar)) { 1168 if (check_for_match ($thing, @matchar)) {
1079 return $thing; 1169 return $thing;
1080 } 1170 }
1081 } 1171 }
1082 return undef; 1172 return undef;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines