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.12 by elmex, Sun Feb 4 20:57:35 2007 UTC vs.
Revision 1.29 by root, Mon Oct 12 14:00:58 2009 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) = @_;
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";
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 $ingred->remove ($ingr_grp, $srcarchname);
223 224
224 my $outarchval = cf::arch::find ($outarch)->clone->value; 225 my $outarchval = cf::arch::find ($outarch)->value;
225 226
226 my $nrof = int ($archvalsum / (($outarchval || 1000) * $outarchvalfact)); 227 my $nrof = int $archvalsum / (($outarchval || 1000) * $outarchvalfact);
227 if ($nrof) { 228 if ($nrof) {
228 # XXX: yes, i know what i'm doing here, i don't set nrof, but it didn't work somehow (pls. chek sometimes) 229 # XXX: yes, I know what I'm doing here, I don't set nrof, but it didn't work somehow (pls. check sometimes)
229 for (1..$nrof) { 230 for (1 .. $nrof) {
230 $chdl->put (my $ob = cf::object::new $outarch); 231 $chdl->put (my $ob = cf::object::new $outarch);
231 $ob->set_animation (cf::rndm $ob->num_animations) 232 $ob->set_animation (cf::rndm $ob->num_animations)
232 if ($ob->type == cf::RING); 233 if ($ob->type == cf::RING);
233 $ob->flag (cf::FLAG_IDENTIFIED, 1); 234 $ob->flag (cf::FLAG_IDENTIFIED, 1);
234 } 235 }
235 236
236 my $xp_sum = ($xp_gain * $nrof); 237 my $xp_sum = $xp_gain * $nrof;
237 238
238 if ($xp_sum) { 239 if ($xp_sum) {
239 $pl->ob->message ("You got $xp_sum xp by making $nrof ${outarch}s"); 240 $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); 241 $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_SKILL_ONLY);
241 } 242 }
242 } else { 243 } else {
243 $pl->ob->message ("You fail to make something, propably you used not enough source material?"); 244 $pl->ob->message ("You fail to make something, probably you used not enough source material?");
244 } 245 }
245} 246}
246 247
247 248
248package Jeweler::CauldronHandler; 249package Jeweler::CauldronHandler;
280 my ($self, $arch_name, @map_stack) = @_; 281 my ($self, $arch_name, @map_stack) = @_;
281 282
282 my @c = 283 my @c =
283 grep { 284 grep {
284 $_->flag (cf::FLAG_IS_CAULDRON) 285 $_->flag (cf::FLAG_IS_CAULDRON)
285 and $_->arch->name eq $arch_name 286 and $_->arch->archname eq $arch_name
286 } @map_stack; 287 } @map_stack;
287 288
288 $self->{cauldron} = $c[0]; 289 $self->{cauldron} = $c[0];
289} 290}
290 291
355 356
356sub put { 357sub put {
357 my ($self, $obj) = @_; 358 my ($self, $obj) = @_;
358 359
359 return undef unless $self->{cauldron}; 360 return undef unless $self->{cauldron};
360 $obj->insert_ob_in_ob ($self->{cauldron}); 361 $self->{cauldron}->insert ($obj);
361} 362}
362 363
363=back 364=back
364 365
365=cut 366=cut
398 399
399sub value { 400sub value {
400 my ($self, $group, $archname) = @_; 401 my ($self, $group, $archname) = @_;
401 402
402 my @objs = grep { 403 my @objs = grep {
403 $_->arch->name eq $archname 404 $_->arch->archname eq $archname
404 } @{$self->{ingredients}->{$group} || []}; 405 } @{$self->{ingredients}->{$group} || []};
405 406
406 my $sum = 0; 407 my $sum = 0;
407 for (@objs) { 408 for (@objs) {
408 $sum += ($_->nrof || 1) * $_->value; 409 $sum += ($_->nrof || 1) * $_->value;
425 426
426 my @out; 427 my @out;
427 428
428 for (@{$ingred->{$group}}) { 429 for (@{$ingred->{$group}}) {
429 if (defined $archname) { 430 if (defined $archname) {
430 if ($_->arch->name eq $archname) { 431 if ($_->arch->archname eq $archname) {
431 Jeweler::Util::remove ($_); 432 Jeweler::Util::remove ($_);
432 } else { 433 } else {
433 push @out, $_; 434 push @out, $_;
434 } 435 }
435 } else { 436 } else {
477} 478}
478 479
479sub improve_ring_by_plan { 480sub improve_ring_by_plan {
480 my ($self, $plan, $ring) = @_; 481 my ($self, $plan, $ring) = @_;
481 482
482 $ring = dclone ($ring); 483 $ring = dclone $ring;
483 484
484 my $ingred = $self->{ingredients}; 485 my $ingred = $self->{ingredients};
485 my $impr = {}; 486 my $impr = {};
486 487
487 if ($plan =~ m/^stat_(\S+)$/) { 488 if ($plan =~ m/^stat_(\S+)$/) {
571} 572}
572 573
573sub check_costs { 574sub check_costs {
574 my ($self, $costs, $do_remove) = @_; 575 my ($self, $costs, $do_remove) = @_;
575 576
576 my $costs = dclone ($costs); 577 my $costs = dclone $costs;
577 578
578 for my $key (keys %$costs) { 579 for my $key (keys %$costs) {
579 my @grepar; 580 my @grepar;
580 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items 581 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items
581 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} }; 582 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} };
584 @grepar = ('gems', undef, undef, $key); 585 @grepar = ('gems', undef, undef, $key);
585 } 586 }
586 587
587 if ($do_remove) { 588 if ($do_remove) {
588 my $rem = $costs->{$key}; 589 my $rem = $costs->{$key};
590 $self->do_grep (sub {
589 $self->do_grep (sub { if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); } 1 }, @grepar); 591 if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); }
592 1
593 }, @grepar);
590 if ($rem > 0) { 594 if ($rem > 0) {
591 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 595 warn "JEWELER BUG: removed ingredients ($key) $rem > 0 after removing!";
592 } 596 }
597
593 } else { 598 } else {
594 my $nr; 599 my $nr;
595 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar); 600 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
596 $costs->{$key} -= $nr; 601 $costs->{$key} -= $nr;
597 } 602 }
641 return 1 if $self->{hash}->{resist}->{$resnum}; 646 return 1 if $self->{hash}->{resist}->{$resnum};
642 } 647 }
643 return undef; 648 return undef;
644} 649}
645 650
651sub projected_exp {
652 my ($self, $input_level) = @_;
653
654 my $lvl = max ($self->power_to_level, 1);
655 my $exp =
656 (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1))
657 / (10 + max ($lvl - 1, 0)); # 10 + level times making such a ring
658 # should get you to the rings level at least.
659
660 if (defined $input_level) {
661 my $subexp =
662 (cf::level_to_min_exp ($input_level)
663 - cf::level_to_min_exp ($input_level - 1))
664 / (10 + max ($input_level - 1, 0)); # see above for comment
665
666 $exp -= $subexp;
667 $exp = max ($exp, 0);
668
669 } else {
670 # the experience bonus here is to make level 1 rings give you at least
671 # 200 exp points when making them. This also makes leveling in the
672 # first few levels a bit easier. (probably until around level 5-6).
673 my $expbonus = cf::level_to_min_exp (2) / 5;
674 # this bonus should also only be given for _new_ rings and not for merged
675 # ones - to prevent infinite exp making.
676 $exp += $expbonus;
677 }
678
679 $exp
680}
681
646sub analyze { 682sub analyze {
647 my ($self, $sk, $pl) = @_; 683 my ($self, $sk, $pl, $input_level) = @_;
684 my $costs = $self->calc_costs;
685
686 unless (defined $costs) {
687 return "This ring has a resistancy above 99%, you can't make that.";
688 }
648 689
649 my $sklvl = cf::exp_to_level ($sk->stats->exp); 690 my $sklvl = cf::exp_to_level ($sk->stats->exp);
650 my $ringlvl = $self->power_to_level; 691 my $ringlvl = $self->power_to_level;
651 692
652 my $tmpl; 693 my $tmpl;
653 if ($pl->flag (cf::FLAG_WIZ)) { 694 if ($pl->flag (cf::FLAG_WIZ)) {
654 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl); 695 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl);
655 } else { 696 } else {
656 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl); 697 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl);
657 } 698 }
658 my $msg = sprintf "Projected success rate: %s", $tmpl; 699 my $msg = sprintf "Projected success rate: %s, you would get %d exp for this.", $tmpl, $self->projected_exp ($input_level);
659 return $msg; 700 return $msg;
660} 701}
661 702
662sub calc_value_from_cost { 703sub calc_value_from_cost {
663 my ($self, $costs) = @_; 704 my ($self, $costs) = @_;
664 my $emarch = cf::arch::find 'emerald'; 705 my $emarch = cf::arch::find 'emerald';
665 my $saarch = cf::arch::find 'sapphire'; 706 my $saarch = cf::arch::find 'sapphire';
666 my $pearch = cf::arch::find 'pearl'; 707 my $pearch = cf::arch::find 'pearl';
667 my $ruarch = cf::arch::find 'ruby'; 708 my $ruarch = cf::arch::find 'ruby';
668 my $diarch = cf::arch::find 'gem'; 709 my $diarch = cf::arch::find 'gem';
669 my $value = $emarch->clone->value * $costs->{emerald} 710 my $value = $emarch->value * $costs->{emerald}
670 + $saarch->clone->value * $costs->{sapphire} 711 + $saarch->value * $costs->{sapphire}
671 + $pearch->clone->value * $costs->{pearl} 712 + $pearch->value * $costs->{pearl}
672 + $ruarch->clone->value * $costs->{ruby} 713 + $ruarch->value * $costs->{ruby}
673 + $diarch->clone->value * $costs->{gem}; 714 + $diarch->value * $costs->{gem};
674 715
675 $value 716 $value
676} 717}
677 718
678sub wiz_analyze { 719sub wiz_analyze {
679 my ($self, $pl) = @_; 720 my ($self, $pl) = @_;
680 my $costs = $self->calc_costs; 721 my $costs = $self->calc_costs;
722 if (defined $costs) {
681 my $desc = ""; 723 my $desc = "";
682 my $lvl = $self->power_to_level (\$desc); 724 my $lvl = $self->power_to_level (\$desc);
683 my $scosts = $self->calc_value_from_cost ($costs); 725 my $scosts = $self->calc_value_from_cost ($costs);
684 726
685 $pl->message ("costs: " . (join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)) . " (".($scosts / 5000)." royalties)"); 727 $pl->message ("costs: "
728 . (join ', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)
729 . " ("
730 . ($scosts / "platinacoin"->cf::arch::find->value)
731 . " platinum)");
686 $pl->message ("level: " . $desc); 732 $pl->message ("level: $desc");
733 } else {
734 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
735 }
687} 736}
688 737
689sub get_chance_perc { 738sub get_chance_perc {
690 my ($self, $sk) = @_; 739 my ($self, $sk) = @_;
691 my $sklvl = cf::exp_to_level ($sk->stats->exp); 740 my $sklvl = cf::exp_to_level ($sk->stats->exp);
696sub fx { 745sub fx {
697 my ($res, $cfg) = @_; 746 my ($res, $cfg) = @_;
698 my $or = $res; 747 my $or = $res;
699 my $ar = $Jeweler::CFG->{functions}->{$cfg}; 748 my $ar = $Jeweler::CFG->{functions}->{$cfg};
700 749
701 if (ref $ar->[0] eq 'ARRAY') { 750 if (ref $ar && ref $ar->[0] eq 'ARRAY') {
702 $res = $res - 1; 751 $res = $res - 1;
703 return $ar->[max (min ($res, @$ar - 1), 0)]; 752 return $ar->[max (min ($res, @$ar - 1), 0)];
704 753
705 } else { 754 } else {
706 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3... 755 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3...
756 # old code:
707 my $idx = ceil (($res / 5) + 0.1) - 1; 757 #my $idx = ceil (($res / 5) + 0.1) - 1;
708 my $a = $ar->[max (min ($idx, @$ar - 1), 0)]; 758 #my $a = $ar->[max (min ($idx, @$ar - 1), 0)];
709 my $b = $ar->[max (min ($idx + 1, @$ar - 1), 0)]; 759 #my $b = $ar->[max (min ($idx + 1, @$ar - 1), 0)];
710 my $diff = $b - $a; # use the difference of the cost to the next cost 760 #my $diff = $b - $a; # use the difference of the cost to the next cost
711 my $o_cost = $a + ($diff / 5) * ($res % 5); # and do some linear interpolation 761 #my $o_cost = $a + ($diff / 5) * ($res % 5); # and do some linear interpolation
712 return $o_cost; 762 #return $o_cost;
763 return 0 if $res <= 0;
764 return ($ar / (1 - ($res * 0.01)) - $ar)
713 } 765 }
714} 766}
715 767
716sub improve_by_ring { 768sub improve_by_ring {
717 my ($self, @rings) = @_; 769 my ($self, @rings) = @_;
783 $obj->{spec}{ac} = $stats->ac; 835 $obj->{spec}{ac} = $stats->ac;
784 $obj->{spec}{speed} = $stats->exp; 836 $obj->{spec}{speed} = $stats->exp;
785 $obj->{spec}{food} = $stats->food; 837 $obj->{spec}{food} = $stats->food;
786 838
787 $obj->{name} = $thing->name; 839 $obj->{name} = $thing->name;
788 $obj->{arch} = $thing->arch->name; 840 $obj->{arch} = $thing->arch->archname;
789 $obj->{face} = $thing->face; 841 $obj->{face} = $thing->face;
790 842
791 $obj->{value} = $thing->value; 843 $obj->{value} = $thing->value;
792 844
793 $self->{hash} = $obj 845 $self->{hash} = $obj
796sub to_object { 848sub to_object {
797 my ($self) = @_; 849 my ($self) = @_;
798 850
799 my $obj = cf::object::new $self->{hash}->{arch}; 851 my $obj = cf::object::new $self->{hash}->{arch};
800 852
801 $obj->item_power ($self->power_to_level); # there have to be strings attached! 853 $obj->item_power (floor ($self->power_to_level / 3)); # there have to be strings attached!
802 854
803 $obj->face ($self->{hash}{face}); 855 $obj->face ($self->{hash}{face});
804 856
805 my $stats = $obj->stats; 857 my $stats = $obj->stats;
806 858
996 1048
997 my $res = $ring->{resist}->{$resnum}; 1049 my $res = $ring->{resist}->{$resnum};
998 1050
999 next unless $res > 0; 1051 next unless $res > 0;
1000 1052
1053 return undef if $res == 100;
1054
1001 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res; 1055 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res;
1002 1056
1003 my $diamonds; 1057 my $diamonds;
1004 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) { 1058 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) {
1005 $diamonds += fx ($res, 'effect_resist_diamonds'); 1059 $diamonds += fx ($res, 'effect_resist_diamonds_x');
1006 } else { 1060 } else {
1007 $diamonds += fx ($res, 'attack_resist_diamonds'); 1061 $diamonds += fx ($res, 'attack_resist_diamonds_x');
1008 } 1062 }
1009 1063
1010 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum}); 1064 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum});
1011 } 1065 }
1012 1066
1020 my ($cost, $diamonds, $category) = @_; 1074 my ($cost, $diamonds, $category) = @_;
1021 1075
1022 my $stat_split = Jeweler::getcfg (diamond_split => $category); 1076 my $stat_split = Jeweler::getcfg (diamond_split => $category);
1023 1077
1024 my $sum = sum (@$stat_split); 1078 my $sum = sum (@$stat_split);
1025 if ($sum < (1 - 0.0001)) {
1026 warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!";
1027 }
1028 1079
1029 my $emarch = cf::arch::find 'emerald'; 1080 my $emarch = cf::arch::find 'emerald';
1030 my $saarch = cf::arch::find 'sapphire'; 1081 my $saarch = cf::arch::find 'sapphire';
1031 my $pearch = cf::arch::find 'pearl'; 1082 my $pearch = cf::arch::find 'pearl';
1032 my $ruarch = cf::arch::find 'ruby'; 1083 my $ruarch = cf::arch::find 'ruby';
1033 my $diarch = cf::arch::find 'gem'; 1084 my $diarch = cf::arch::find 'gem';
1034 1085
1035 my $sumvalue = $diarch->clone->value * $diamonds; 1086 my $sumvalue = $diarch->value * $diamonds;
1036 1087
1037 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->clone->value; 1088 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->value;
1038 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->clone->value; 1089 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->value;
1039 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->clone->value; 1090 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->value;
1040 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->clone->value; 1091 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->value;
1041 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->clone->value; 1092 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value;
1042} 1093}
1043 1094
1044package Jeweler::Util; 1095package Jeweler::Util;
1045 1096
1046use strict; 1097use strict;
1052=over 4 1103=over 4
1053 1104
1054=item remove ($object[, $nrof]) 1105=item remove ($object[, $nrof])
1055 1106
1056Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects. 1107Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects.
1057The returnvalue is the number of 'single' objects that couldn't be removed. 1108The return value is the number of 'single' objects that couldn't be removed.
1058 1109
1059=cut 1110=cut
1060 1111
1061sub remove { 1112sub remove {
1062 my ($obj, $nrof) = @_; 1113 my ($obj, $nrof) = @_;
1114
1115 my $c = $obj->nrof || 1;
1116 my $r = $c > $nrof ? 0 : $nrof - $c;
1117 $obj->decrease (defined ($nrof) ? $nrof : ($obj->nrof || 1));
1063 1118
1064 my $cnt; 1119 $r
1065
1066 if (defined $nrof) {
1067 # TODO: Check tihis line:
1068 return 0 if ($nrof * 1) == 0; #XXX: ???
1069 $cnt = int (($obj->nrof || 1) - (1 * $nrof));
1070
1071 if ($cnt > 0) {
1072 $obj->nrof ($cnt);
1073 return 0;
1074 }
1075 }
1076
1077 remove ($_) for $obj->inv;
1078 $obj->destroy;
1079 return $cnt;
1080} 1120}
1081 1121
1082sub check_for_match { 1122sub check_for_match {
1083 my ($thing, @matchar) = @_; 1123 my ($thing, @matchar) = @_;
1084 1124
1085 my $i = 0; 1125 my $i = 0;
1086 my $check_cnts = 0; 1126 my $check_cnts = 0;
1087 my $check_true = 0; 1127 my $check_true = 0;
1088 for my $match (@matchar) { 1128 for my $match (@matchar) {
1089 if ($i % 3 == 0) { 1129 if ($i % 3 == 0) {
1130 return 1 if $check_true && $check_cnts == $check_true;
1090 $check_cnts = 0; 1131 $check_cnts = 0;
1091 $check_true = 0; 1132 $check_true = 0;
1092 } 1133 }
1093 1134
1094 if ($match =~ m/^\s*$/) { 1135 if ($match =~ m/^\s*$/) {
1102 and $check_true++; 1143 and $check_true++;
1103 } elsif ($i % 3 == 1) { 1144 } elsif ($i % 3 == 1) {
1104 $thing->title eq $match 1145 $thing->title eq $match
1105 and $check_true++; 1146 and $check_true++;
1106 } else { # $i % 3 == 2 1147 } else { # $i % 3 == 2
1107 $thing->arch->name eq $match 1148 $thing->arch->archname eq $match
1108 and $check_true++; 1149 and $check_true++;
1109 } 1150 }
1110 $i++; 1151 $i++;
1111 } 1152 }
1112 #d# warn "CHECK $check_true | $check_cnts | [".(join ',', @matchar).":".join (",", ($thing->name, $thing->title, $thing->arch->name))."\n"; 1153 #d# warn "CHECK $check_true | $check_cnts | [".(join ',', @matchar).":".join (",", ($thing->name, $thing->title, $thing->arch->archname))."\n";
1113 if ($check_true && $check_cnts == $check_true) { 1154 return 1 if $check_true && $check_cnts == $check_true;
1114 return 1;
1115 }
1116 return 0; 1155 return 0;
1117} 1156}
1118 1157
1119sub grep_for_match { 1158sub grep_for_match {
1120 my ($ingred, $group, @matchar) = @_; 1159 my ($ingred, $group, @matchar) = @_;
1121 1160
1122 for my $thing (@{$ingred->{$group} || []}) { 1161 for my $thing (@{$ingred->{$group} || []}) {
1123 #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->name, "@matchar"; #d# 1162 #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->archname, "@matchar"; #d#
1124 if (check_for_match ($thing, @matchar)) { 1163 if (check_for_match ($thing, @matchar)) {
1125 return $thing; 1164 return $thing;
1126 } 1165 }
1127 } 1166 }
1128 return undef; 1167 return undef;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines