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.9 by elmex, Sun Feb 4 11:17:52 2007 UTC vs.
Revision 1.28 by root, Sat Dec 13 20:34:37 2008 UTC

24our $CFG; 24our $CFG;
25 25
26sub read_config { 26sub read_config {
27 my ($filename) = @_; 27 my ($filename) = @_;
28 28
29 unless (-e $filename) { 29 if (my $meta = $cf::RESOURCE{$filename}) {
30 $CFG = cf::decode_json $meta->{data};
31 } else {
30 warn "$filename doesn't exists! no config for jeweler skill loaded!\n"; 32 warn "$filename doesn't exist! no config for jeweler skill loaded!\n";
31 $CFG = {}; 33 $CFG = {};
32 return
33 } 34 }
34
35 $CFG = YAML::LoadFile $filename;
36} 35}
37 36
38sub getcfg { 37sub getcfg {
39 my ($sect, $key) = @_; 38 my ($sect, $key) = @_;
40 return $CFG->{$sect} unless defined $key; 39 return $CFG->{$sect} unless defined $key;
162 $delta = 5 if $delta > 5; 161 $delta = 5 if $delta > 5;
163 return $LVL_DIFF_CHANCES{$delta} 162 return $LVL_DIFF_CHANCES{$delta}
164} 163}
165 164
166sub analyze { 165sub analyze {
167 my ($sk, $chdl, $pl) = @_; 166 my ($sk, $chdl, $pl, $input_level) = @_;
168 167
169 my $hadunid = 0; 168 my $hadunid = 0;
169 my $found = 0;
170 for ($chdl->grep_by_type (cf::RING, cf::AMULET)) { 170 for ($chdl->grep_by_type (cf::RING, cf::AMULET)) {
171 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) { 171 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) {
172 $hadunid = 1; 172 $hadunid = 1;
173 next; 173 next;
174 } 174 }
175 $found = 1;
175 my $r = Jeweler::Object->new (object => $_); 176 my $r = Jeweler::Object->new (object => $_);
176 my $msg = $r->analyze ($sk, $pl); 177 my $msg = $r->analyze ($sk, $pl, $input_level);
177 $pl->message ($r->to_string . ": " . $msg); 178 $pl->message ($r->to_string . ": " . $msg);
178 if ($pl->flag (cf::FLAG_WIZ)) { 179 if ($pl->flag (cf::FLAG_WIZ)) {
179 $r->wiz_analyze ($pl); 180 $r->wiz_analyze ($pl);
180 } 181 }
181 } 182 }
182 if ($hadunid) {
183 $pl->message ("You couldn't identify the other rings and not analyze them!"); 183 $pl->message ("You couldn't identify the other rings and not analyze them!")
184 } 184 if $hadunid;
185 $pl->message ("You couldn't find anything in the bench to analyse!")
186 unless $found;
185} 187}
186 188
187# this function converts metals/minerals into a raw ring (of adornment) 189# this function converts metals/minerals into a raw ring (of adornment)
188sub simple_converter { 190sub simple_converter {
189 my ($pl, $ingred, $chdl, $conv) = @_; 191 my ($pl, $ingred, $chdl, $conv) = @_;
203 warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!"; 205 warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!";
204 return; 206 return;
205 } 207 }
206 208
207 unless ($xp_gain > 0) { 209 unless ($xp_gain > 0) {
208 warn "WARNING: xp gain isn't > 0 in convesion '$outarch'\n"; 210 warn "WARNING: xp gain isn't > 0 in conversion '$outarch'\n";
209 return; 211 return;
210 } 212 }
211 213
212 unless ($outarchvalfact) { 214 unless ($outarchvalfact) {
213 warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n"; 215 warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n";
219 } 221 }
220 222
221 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname); 223 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname);
222 $ingred->remove ($ingr_grp, $srcarchname); 224 $ingred->remove ($ingr_grp, $srcarchname);
223 225
224 my $outarchval = cf::arch::find ($outarch)->clone->value; 226 my $outarchval = cf::arch::find ($outarch)->value;
225 227
226 my $nrof = int ($archvalsum / (($outarchval || 1000) * $outarchvalfact)); 228 my $nrof = int $archvalsum / (($outarchval || 1000) * $outarchvalfact);
227 if ($nrof) { 229 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) 230 # XXX: yes, I know what I'm doing here, I don't set nrof, but it didn't work somehow (pls. check sometimes)
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)
232 if ($ob->type == cf::RING); 234 if ($ob->type == cf::RING);
233 $ob->flag (cf::FLAG_IDENTIFIED, 1); 235 $ob->flag (cf::FLAG_IDENTIFIED, 1);
234 } 236 }
235 237
236 my $xp_sum = ($xp_gain * $nrof); 238 my $xp_sum = $xp_gain * $nrof;
237 239
238 if ($xp_sum) { 240 if ($xp_sum) {
239 $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");
240 $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);
241 } 243 }
242 } else { 244 } else {
243 $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?");
244 } 246 }
245} 247}
246 248
247 249
248package Jeweler::CauldronHandler; 250package Jeweler::CauldronHandler;
280 my ($self, $arch_name, @map_stack) = @_; 282 my ($self, $arch_name, @map_stack) = @_;
281 283
282 my @c = 284 my @c =
283 grep { 285 grep {
284 $_->flag (cf::FLAG_IS_CAULDRON) 286 $_->flag (cf::FLAG_IS_CAULDRON)
285 and $_->arch->name eq $arch_name 287 and $_->arch->archname eq $arch_name
286 } @map_stack; 288 } @map_stack;
287 289
288 $self->{cauldron} = $c[0]; 290 $self->{cauldron} = $c[0];
289} 291}
290 292
355 357
356sub put { 358sub put {
357 my ($self, $obj) = @_; 359 my ($self, $obj) = @_;
358 360
359 return undef unless $self->{cauldron}; 361 return undef unless $self->{cauldron};
360 $obj->insert_ob_in_ob ($self->{cauldron}); 362 $self->{cauldron}->insert ($obj);
361} 363}
362 364
363=back 365=back
364 366
365=cut 367=cut
398 400
399sub value { 401sub value {
400 my ($self, $group, $archname) = @_; 402 my ($self, $group, $archname) = @_;
401 403
402 my @objs = grep { 404 my @objs = grep {
403 $_->arch->name eq $archname 405 $_->arch->archname eq $archname
404 } @{$self->{ingredients}->{$group} || []}; 406 } @{$self->{ingredients}->{$group} || []};
405 407
406 my $sum = 0; 408 my $sum = 0;
407 for (@objs) { 409 for (@objs) {
408 $sum += ($_->nrof || 1) * $_->value; 410 $sum += ($_->nrof || 1) * $_->value;
425 427
426 my @out; 428 my @out;
427 429
428 for (@{$ingred->{$group}}) { 430 for (@{$ingred->{$group}}) {
429 if (defined $archname) { 431 if (defined $archname) {
430 if ($_->arch->name eq $archname) { 432 if ($_->arch->archname eq $archname) {
431 Jeweler::Util::remove ($_); 433 Jeweler::Util::remove ($_);
432 } else { 434 } else {
433 push @out, $_; 435 push @out, $_;
434 } 436 }
435 } else { 437 } else {
477} 479}
478 480
479sub improve_ring_by_plan { 481sub improve_ring_by_plan {
480 my ($self, $plan, $ring) = @_; 482 my ($self, $plan, $ring) = @_;
481 483
482 $ring = dclone ($ring); 484 $ring = dclone $ring;
483 485
484 my $ingred = $self->{ingredients}; 486 my $ingred = $self->{ingredients};
485 my $impr = {}; 487 my $impr = {};
486 488
487 if ($plan =~ m/^stat_(\S+)$/) { 489 if ($plan =~ m/^stat_(\S+)$/) {
571} 573}
572 574
573sub check_costs { 575sub check_costs {
574 my ($self, $costs, $do_remove) = @_; 576 my ($self, $costs, $do_remove) = @_;
575 577
576 my $costs = dclone ($costs); 578 my $costs = dclone $costs;
577 579
578 for my $key (keys %$costs) { 580 for my $key (keys %$costs) {
579 my @grepar; 581 my @grepar;
580 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items 582 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items
581 @grepar = @{Jeweler::getcfg (plans => $key) || []}; 583 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} };
584 next if $@;
582 } else { # check the gems 585 } else { # check the gems
583 @grepar = ('gems', undef, undef, $key); 586 @grepar = ('gems', undef, undef, $key);
584 } 587 }
585 588
586 if ($do_remove) { 589 if ($do_remove) {
587 my $rem = $costs->{$key}; 590 my $rem = $costs->{$key};
591 $self->do_grep (sub {
588 $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);
589 if ($rem > 0) { 595 if ($rem > 0) {
590 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 596 warn "JEWELER BUG: removed ingredients ($key) $rem > 0 after removing!";
591 } 597 }
598
592 } else { 599 } else {
593 my $nr; 600 my $nr;
594 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar); 601 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
595 $costs->{$key} -= $nr; 602 $costs->{$key} -= $nr;
596 } 603 }
640 return 1 if $self->{hash}->{resist}->{$resnum}; 647 return 1 if $self->{hash}->{resist}->{$resnum};
641 } 648 }
642 return undef; 649 return undef;
643} 650}
644 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
645sub analyze { 683sub analyze {
646 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 }
647 690
648 my $sklvl = cf::exp_to_level ($sk->stats->exp); 691 my $sklvl = cf::exp_to_level ($sk->stats->exp);
649 my $ringlvl = $self->power_to_level; 692 my $ringlvl = $self->power_to_level;
650 693
651 my $tmpl; 694 my $tmpl;
652 if ($pl->flag (cf::FLAG_WIZ)) { 695 if ($pl->flag (cf::FLAG_WIZ)) {
653 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl); 696 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl);
654 } else { 697 } else {
655 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl); 698 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl);
656 } 699 }
657 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);
658 return $msg; 701 return $msg;
659} 702}
660 703
661sub calc_value_from_cost { 704sub calc_value_from_cost {
662 my ($self, $costs) = @_; 705 my ($self, $costs) = @_;
663 my $emarch = cf::arch::find 'emerald'; 706 my $emarch = cf::arch::find 'emerald';
664 my $saarch = cf::arch::find 'sapphire'; 707 my $saarch = cf::arch::find 'sapphire';
665 my $pearch = cf::arch::find 'pearl'; 708 my $pearch = cf::arch::find 'pearl';
666 my $ruarch = cf::arch::find 'ruby'; 709 my $ruarch = cf::arch::find 'ruby';
667 my $diarch = cf::arch::find 'gem'; 710 my $diarch = cf::arch::find 'gem';
668 my $value = $emarch->clone->value * $costs->{emerald} 711 my $value = $emarch->value * $costs->{emerald}
669 + $saarch->clone->value * $costs->{sapphire} 712 + $saarch->value * $costs->{sapphire}
670 + $pearch->clone->value * $costs->{pearl} 713 + $pearch->value * $costs->{pearl}
671 + $ruarch->clone->value * $costs->{ruby} 714 + $ruarch->value * $costs->{ruby}
672 + $diarch->clone->value * $costs->{gem}; 715 + $diarch->value * $costs->{gem};
673 716
674 $value 717 $value
675} 718}
676 719
677sub wiz_analyze { 720sub wiz_analyze {
678 my ($self, $pl) = @_; 721 my ($self, $pl) = @_;
679 my $costs = $self->calc_costs; 722 my $costs = $self->calc_costs;
723 if (defined $costs) {
680 my $desc = ""; 724 my $desc = "";
681 my $lvl = $self->power_to_level (\$desc); 725 my $lvl = $self->power_to_level (\$desc);
682 my $scosts = $self->calc_value_from_cost ($costs); 726 my $scosts = $self->calc_value_from_cost ($costs);
683 727
684 $pl->message ("costs: " . (join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)) . " (".($scosts / 5000)." royalties)"); 728 $pl->message ("costs: "
729 . (join ', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)
730 . " ("
731 . ($scosts / "platinacoin"->cf::arch::find->value)
732 . " platinum)");
685 $pl->message ("level: " . $desc); 733 $pl->message ("level: $desc");
734 } else {
735 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
736 }
686} 737}
687 738
688sub get_chance_perc { 739sub get_chance_perc {
689 my ($self, $sk) = @_; 740 my ($self, $sk) = @_;
690 my $sklvl = cf::exp_to_level ($sk->stats->exp); 741 my $sklvl = cf::exp_to_level ($sk->stats->exp);
695sub fx { 746sub fx {
696 my ($res, $cfg) = @_; 747 my ($res, $cfg) = @_;
697 my $or = $res; 748 my $or = $res;
698 my $ar = $Jeweler::CFG->{functions}->{$cfg}; 749 my $ar = $Jeweler::CFG->{functions}->{$cfg};
699 750
700 if (ref $ar->[0] eq 'ARRAY') { 751 if (ref $ar && ref $ar->[0] eq 'ARRAY') {
701 $res = $res - 1; 752 $res = $res - 1;
702 return $ar->[max (min ($res, @$ar - 1), 0)]; 753 return $ar->[max (min ($res, @$ar - 1), 0)];
703 754
704 } else { 755 } else {
705 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3... 756 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3...
757 # old code:
706 my $idx = ceil (($res / 5) + 0.1) - 1; 758 #my $idx = ceil (($res / 5) + 0.1) - 1;
707 my $a = $ar->[max (min ($idx, @$ar - 1), 0)]; 759 #my $a = $ar->[max (min ($idx, @$ar - 1), 0)];
708 my $b = $ar->[max (min ($idx + 1, @$ar - 1), 0)]; 760 #my $b = $ar->[max (min ($idx + 1, @$ar - 1), 0)];
709 my $diff = $b - $a; # use the difference of the cost to the next cost 761 #my $diff = $b - $a; # use the difference of the cost to the next cost
710 my $o_cost = $a + ($diff / 5) * ($res % 5); # and do some linear interpolation 762 #my $o_cost = $a + ($diff / 5) * ($res % 5); # and do some linear interpolation
711 return $o_cost; 763 #return $o_cost;
764 return 0 if $res <= 0;
765 return ($ar / (1 - ($res * 0.01)) - $ar)
712 } 766 }
713} 767}
714 768
715sub improve_by_ring { 769sub improve_by_ring {
716 my ($self, @rings) = @_; 770 my ($self, @rings) = @_;
782 $obj->{spec}{ac} = $stats->ac; 836 $obj->{spec}{ac} = $stats->ac;
783 $obj->{spec}{speed} = $stats->exp; 837 $obj->{spec}{speed} = $stats->exp;
784 $obj->{spec}{food} = $stats->food; 838 $obj->{spec}{food} = $stats->food;
785 839
786 $obj->{name} = $thing->name; 840 $obj->{name} = $thing->name;
787 $obj->{arch} = $thing->arch->name; 841 $obj->{arch} = $thing->arch->archname;
788 $obj->{face} = $thing->face; 842 $obj->{face} = $thing->face;
789 843
790 $obj->{value} = $thing->value; 844 $obj->{value} = $thing->value;
791 845
792 $self->{hash} = $obj 846 $self->{hash} = $obj
795sub to_object { 849sub to_object {
796 my ($self) = @_; 850 my ($self) = @_;
797 851
798 my $obj = cf::object::new $self->{hash}->{arch}; 852 my $obj = cf::object::new $self->{hash}->{arch};
799 853
800 $obj->item_power ($self->power_to_level); # there have to be strings attached! 854 $obj->item_power (floor ($self->power_to_level / 3)); # there have to be strings attached!
801 855
802 $obj->face ($self->{hash}{face}); 856 $obj->face ($self->{hash}{face});
803 857
804 my $stats = $obj->stats; 858 my $stats = $obj->stats;
805 859
995 1049
996 my $res = $ring->{resist}->{$resnum}; 1050 my $res = $ring->{resist}->{$resnum};
997 1051
998 next unless $res > 0; 1052 next unless $res > 0;
999 1053
1054 return undef if $res == 100;
1055
1000 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res; 1056 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res;
1001 1057
1002 my $diamonds; 1058 my $diamonds;
1003 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) { 1059 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) {
1004 $diamonds += fx ($res, 'effect_resist_diamonds'); 1060 $diamonds += fx ($res, 'effect_resist_diamonds_x');
1005 } else { 1061 } else {
1006 $diamonds += fx ($res, 'attack_resist_diamonds'); 1062 $diamonds += fx ($res, 'attack_resist_diamonds_x');
1007 } 1063 }
1008 1064
1009 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum}); 1065 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum});
1010 } 1066 }
1011 1067
1019 my ($cost, $diamonds, $category) = @_; 1075 my ($cost, $diamonds, $category) = @_;
1020 1076
1021 my $stat_split = Jeweler::getcfg (diamond_split => $category); 1077 my $stat_split = Jeweler::getcfg (diamond_split => $category);
1022 1078
1023 my $sum = sum (@$stat_split); 1079 my $sum = sum (@$stat_split);
1024 if ($sum < (1 - 0.0001)) {
1025 warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!";
1026 }
1027 1080
1028 my $emarch = cf::arch::find 'emerald'; 1081 my $emarch = cf::arch::find 'emerald';
1029 my $saarch = cf::arch::find 'sapphire'; 1082 my $saarch = cf::arch::find 'sapphire';
1030 my $pearch = cf::arch::find 'pearl'; 1083 my $pearch = cf::arch::find 'pearl';
1031 my $ruarch = cf::arch::find 'ruby'; 1084 my $ruarch = cf::arch::find 'ruby';
1032 my $diarch = cf::arch::find 'gem'; 1085 my $diarch = cf::arch::find 'gem';
1033 1086
1034 my $sumvalue = $diarch->clone->value * $diamonds; 1087 my $sumvalue = $diarch->value * $diamonds;
1035 1088
1036 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->clone->value; 1089 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->value;
1037 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->clone->value; 1090 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->value;
1038 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->clone->value; 1091 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->value;
1039 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->clone->value; 1092 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->value;
1040 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->clone->value; 1093 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value;
1041} 1094}
1042 1095
1043package Jeweler::Util; 1096package Jeweler::Util;
1044 1097
1045use strict; 1098use strict;
1051=over 4 1104=over 4
1052 1105
1053=item remove ($object[, $nrof]) 1106=item remove ($object[, $nrof])
1054 1107
1055Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects. 1108Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects.
1056The returnvalue is the number of 'single' objects that couldn't be removed. 1109The return value is the number of 'single' objects that couldn't be removed.
1057 1110
1058=cut 1111=cut
1059 1112
1060sub remove { 1113sub remove {
1061 my ($obj, $nrof) = @_; 1114 my ($obj, $nrof) = @_;
1115
1116 my $c = $obj->nrof || 1;
1117 my $r = $c > $nrof ? 0 : $nrof - $c;
1118 $obj->decrease (defined ($nrof) ? $nrof : ($obj->nrof || 1));
1062 1119
1063 my $cnt; 1120 $r
1064
1065 if (defined $nrof) {
1066 # TODO: Check tihis line:
1067 return 0 if ($nrof * 1) == 0; #XXX: ???
1068 $cnt = int (($obj->nrof || 1) - (1 * $nrof));
1069
1070 if ($cnt > 0) {
1071 $obj->nrof ($cnt);
1072 return 0;
1073 }
1074 }
1075
1076 remove ($_) for $obj->inv;
1077 $obj->destroy;
1078 return $cnt;
1079} 1121}
1080 1122
1081sub check_for_match { 1123sub check_for_match {
1082 my ($thing, @matchar) = @_; 1124 my ($thing, @matchar) = @_;
1083 1125
1084 my $i = 0; 1126 my $i = 0;
1085 my $check_cnts = 0; 1127 my $check_cnts = 0;
1086 my $check_true = 0; 1128 my $check_true = 0;
1087 for my $match (@matchar) { 1129 for my $match (@matchar) {
1130 if ($i % 3 == 0) {
1131 return 1 if $check_true && $check_cnts == $check_true;
1132 $check_cnts = 0;
1133 $check_true = 0;
1134 }
1135
1088 if ($match =~ m/^\s*$/) { 1136 if ($match =~ m/^\s*$/) {
1089 $i++; 1137 $i++;
1090 next; 1138 next;
1091 } 1139 }
1092 1140
1096 and $check_true++; 1144 and $check_true++;
1097 } elsif ($i % 3 == 1) { 1145 } elsif ($i % 3 == 1) {
1098 $thing->title eq $match 1146 $thing->title eq $match
1099 and $check_true++; 1147 and $check_true++;
1100 } else { # $i % 3 == 2 1148 } else { # $i % 3 == 2
1101 $thing->arch->name eq $match 1149 $thing->arch->archname eq $match
1102 and $check_true++; 1150 and $check_true++;
1103 } 1151 }
1104 $i++; 1152 $i++;
1105 } 1153 }
1154 #d# warn "CHECK $check_true | $check_cnts | [".(join ',', @matchar).":".join (",", ($thing->name, $thing->title, $thing->arch->archname))."\n";
1106 if ($check_true && $check_cnts == $check_true) { 1155 return 1 if $check_true && $check_cnts == $check_true;
1107 return 1;
1108 }
1109 return 0; 1156 return 0;
1110} 1157}
1111 1158
1112sub grep_for_match { 1159sub grep_for_match {
1113 my ($ingred, $group, @matchar) = @_; 1160 my ($ingred, $group, @matchar) = @_;
1114 1161
1115 for my $thing (@{$ingred->{$group} || []}) { 1162 for my $thing (@{$ingred->{$group} || []}) {
1116 #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->name, "@matchar"; #d# 1163 #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->archname, "@matchar"; #d#
1117 if (check_for_match ($thing, @matchar)) { 1164 if (check_for_match ($thing, @matchar)) {
1118 return $thing; 1165 return $thing;
1119 } 1166 }
1120 } 1167 }
1121 return undef; 1168 return undef;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines