… | |
… | |
8 | |
8 | |
9 | =cut |
9 | =cut |
10 | |
10 | |
11 | package Jeweler; |
11 | package Jeweler; |
12 | |
12 | |
13 | use strict; |
13 | use common::sense; |
14 | use YAML; |
|
|
15 | |
14 | |
16 | =over 4 |
15 | =over 4 |
17 | |
16 | |
18 | =item @RESISTS |
17 | =item @RESISTS |
19 | |
18 | |
… | |
… | |
24 | our $CFG; |
23 | our $CFG; |
25 | |
24 | |
26 | sub read_config { |
25 | sub 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 | |
38 | sub getcfg { |
36 | sub getcfg { |
39 | my ($sect, $key) = @_; |
37 | my ($sect, $key) = @_; |
40 | return $CFG->{$sect} unless defined $key; |
38 | return $CFG->{$sect} unless defined $key; |
… | |
… | |
117 | cf::ATNR_LIFE_STEALING => "LIFE_STEALING", |
115 | cf::ATNR_LIFE_STEALING => "LIFE_STEALING", |
118 | cf::ATNR_BLIND => "BLIND", |
116 | cf::ATNR_BLIND => "BLIND", |
119 | cf::ATNR_DISEASE => "DISEASE", |
117 | cf::ATNR_DISEASE => "DISEASE", |
120 | ); |
118 | ); |
121 | |
119 | |
|
|
120 | our %REV_RESMAP = map { $RESMAP{$_} => $_ } keys %RESMAP; |
|
|
121 | |
122 | our %LVL_DIFF_CHANCES = ( |
122 | our %LVL_DIFF_CHANCES = ( |
123 | +5 => 100, |
123 | +5 => 100, |
124 | +4 => 95, |
124 | +4 => 95, |
125 | +3 => 85, |
125 | +3 => 85, |
126 | +2 => 75, |
126 | +2 => 75, |
… | |
… | |
160 | $delta = 5 if $delta > 5; |
160 | $delta = 5 if $delta > 5; |
161 | return $LVL_DIFF_CHANCES{$delta} |
161 | return $LVL_DIFF_CHANCES{$delta} |
162 | } |
162 | } |
163 | |
163 | |
164 | sub analyze { |
164 | sub analyze { |
165 | my ($sk, $chdl, $pl) = @_; |
165 | my ($sk, $chdl, $pl, $input_level) = @_; |
166 | |
166 | |
167 | my $hadunid = 0; |
167 | my $hadunid = 0; |
|
|
168 | my $found = 0; |
168 | for ($chdl->grep_by_type (cf::RING, cf::AMULET)) { |
169 | for ($chdl->grep_by_type (cf::RING, cf::AMULET)) { |
169 | if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) { |
170 | if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) { |
170 | $hadunid = 1; |
171 | $hadunid = 1; |
171 | next; |
172 | next; |
172 | } |
173 | } |
|
|
174 | $found = 1; |
173 | my $r = Jeweler::Object->new (object => $_); |
175 | my $r = Jeweler::Object->new (object => $_); |
174 | my $msg = $r->analyze ($sk, $pl); |
176 | my $msg = $r->analyze ($sk, $pl, $input_level); |
175 | $pl->message ($r->to_string . ": " . $msg); |
177 | $pl->message ($r->to_string . ": " . $msg); |
176 | if ($pl->flag (cf::FLAG_WIZ)) { |
178 | if ($pl->flag (cf::FLAG_WIZ)) { |
177 | $r->wiz_analyze ($pl); |
179 | $r->wiz_analyze ($pl); |
178 | } |
180 | } |
179 | } |
181 | } |
180 | if ($hadunid) { |
|
|
181 | $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!") |
182 | } |
183 | if $hadunid; |
|
|
184 | $pl->message ("You couldn't find anything in the bench to analyse!") |
|
|
185 | unless $found; |
183 | } |
186 | } |
184 | |
187 | |
185 | # this function converts metals/minerals into a raw ring (of adornment) |
188 | # this function converts metals/minerals into a raw ring (of adornment) |
186 | sub simple_converter { |
189 | sub simple_converter { |
187 | my ($pl, $ingred, $chdl, $conv) = @_; |
190 | my ($pl, $ingred, $chdl, $conv, $sk_lvl, $low_skill_cb) = @_; |
188 | |
191 | |
189 | $conv = lc $conv; |
192 | $conv = lc $conv; |
190 | my $cnvs = $CFG->{conversions}; |
193 | my $cnvs = $CFG->{conversions}; |
191 | |
194 | |
192 | return unless $cnvs->{$conv}; |
195 | return unless $cnvs->{$conv}; |
… | |
… | |
201 | warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!"; |
204 | warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!"; |
202 | return; |
205 | return; |
203 | } |
206 | } |
204 | |
207 | |
205 | unless ($xp_gain > 0) { |
208 | unless ($xp_gain > 0) { |
206 | warn "WARNING: xp gain isn't > 0 in convesion '$outarch'\n"; |
209 | warn "WARNING: xp gain isn't > 0 in conversion '$outarch'\n"; |
207 | return; |
210 | return; |
208 | } |
211 | } |
209 | |
212 | |
210 | unless ($outarchvalfact) { |
213 | unless ($outarchvalfact) { |
211 | warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n"; |
214 | warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n"; |
… | |
… | |
214 | |
217 | |
215 | unless ($outarchvalfact >= 1) { |
218 | unless ($outarchvalfact >= 1) { |
216 | 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"; |
217 | } |
220 | } |
218 | |
221 | |
219 | my $archvalsum = $ingred->value ($ingr_grp, $srcarchname); |
222 | my $archvalsum = $ingred->value ($ingr_grp, $srcarchname); |
220 | $ingred->remove ($ingr_grp, $srcarchname); |
|
|
221 | |
|
|
222 | my $outarchval = cf::arch::find ($outarch)->clone->value; |
223 | my $outarchval = cf::arch::find ($outarch)->value; |
223 | |
|
|
224 | 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 | |
225 | if ($nrof) { |
232 | if ($nrof) { |
226 | # 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); |
|
|
235 | for (1 .. $nrof) { |
227 | $chdl->put (cf::object::new $outarch) for 1..$nrof; |
236 | $chdl->put (my $ob = cf::object::new $outarch); |
|
|
237 | $ob->set_animation (cf::rndm $ob->num_animations) |
|
|
238 | if ($ob->type == cf::RING); |
|
|
239 | $ob->flag (cf::FLAG_IDENTIFIED, 1); |
|
|
240 | } |
228 | |
241 | |
229 | my $xp_sum = ($xp_gain * $nrof); |
242 | my $xp_sum = $xp_gain * $nrof; |
230 | |
243 | |
231 | if ($xp_sum) { |
244 | if ($xp_sum) { |
232 | $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"); |
233 | $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_ADD_SKILL); |
246 | $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_SKILL_ONLY); |
234 | } |
247 | } |
|
|
248 | } else { |
|
|
249 | $pl->ob->message ("You fail to make something, probably you used not enough source material?"); |
235 | } |
250 | } |
236 | } |
251 | } |
237 | |
252 | |
238 | |
253 | |
239 | package Jeweler::CauldronHandler; |
254 | package Jeweler::CauldronHandler; |
… | |
… | |
271 | my ($self, $arch_name, @map_stack) = @_; |
286 | my ($self, $arch_name, @map_stack) = @_; |
272 | |
287 | |
273 | my @c = |
288 | my @c = |
274 | grep { |
289 | grep { |
275 | $_->flag (cf::FLAG_IS_CAULDRON) |
290 | $_->flag (cf::FLAG_IS_CAULDRON) |
276 | and $_->arch->name eq $arch_name |
291 | and $_->arch->archname eq $arch_name |
277 | } @map_stack; |
292 | } @map_stack; |
278 | |
293 | |
279 | $self->{cauldron} = $c[0]; |
294 | $self->{cauldron} = $c[0]; |
280 | } |
295 | } |
281 | |
296 | |
… | |
… | |
320 | cf::POTION => 'potions', |
335 | cf::POTION => 'potions', |
321 | cf::SCROLL => 'scrolls', |
336 | cf::SCROLL => 'scrolls', |
322 | ); |
337 | ); |
323 | |
338 | |
324 | for ($self->{cauldron}->inv) { |
339 | for ($self->{cauldron}->inv) { |
|
|
340 | if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) { |
|
|
341 | die "unidentified"; |
|
|
342 | } elsif ($_->flag (cf::FLAG_CURSED) || $_->flag (cf::FLAG_DAMNED)) { |
|
|
343 | die "cursed"; |
|
|
344 | } |
325 | |
345 | |
326 | if (my $k = $type_to_key{$_->type}) { |
346 | if (my $k = $type_to_key{$_->type}) { |
327 | push @{$ingreds->{$k}}, $_; |
347 | push @{$ingreds->{$k}}, $_; |
|
|
348 | } else { |
|
|
349 | push @{$ingreds->{other}}, $_; |
328 | } |
350 | } |
329 | } |
351 | } |
330 | |
352 | |
331 | return Jeweler::Ingredients->new (ingredients => $ingreds, cauldron_helper => $self) |
353 | return Jeweler::Ingredients->new (ingredients => $ingreds, cauldron_helper => $self) |
332 | } |
354 | } |
… | |
… | |
339 | |
361 | |
340 | sub put { |
362 | sub put { |
341 | my ($self, $obj) = @_; |
363 | my ($self, $obj) = @_; |
342 | |
364 | |
343 | return undef unless $self->{cauldron}; |
365 | return undef unless $self->{cauldron}; |
344 | $obj->insert_ob_in_ob ($self->{cauldron}); |
366 | $self->{cauldron}->insert ($obj); |
345 | } |
367 | } |
346 | |
368 | |
347 | =back |
369 | =back |
348 | |
370 | |
349 | =cut |
371 | =cut |
… | |
… | |
382 | |
404 | |
383 | sub value { |
405 | sub value { |
384 | my ($self, $group, $archname) = @_; |
406 | my ($self, $group, $archname) = @_; |
385 | |
407 | |
386 | my @objs = grep { |
408 | my @objs = grep { |
387 | $_->arch->name eq $archname |
409 | $_->arch->archname eq $archname |
388 | } @{$self->{ingredients}->{$group} || []}; |
410 | } @{$self->{ingredients}->{$group} || []}; |
389 | |
411 | |
390 | my $sum = 0; |
412 | my $sum = 0; |
391 | for (@objs) { |
413 | for (@objs) { |
392 | $sum += ($_->nrof || 1) * $_->value; |
414 | $sum += ($_->nrof || 1) * $_->value; |
… | |
… | |
409 | |
431 | |
410 | my @out; |
432 | my @out; |
411 | |
433 | |
412 | for (@{$ingred->{$group}}) { |
434 | for (@{$ingred->{$group}}) { |
413 | if (defined $archname) { |
435 | if (defined $archname) { |
414 | if ($_->arch->name eq $archname) { |
436 | if ($_->arch->archname eq $archname) { |
415 | Jeweler::Util::remove ($_); |
437 | Jeweler::Util::remove ($_); |
416 | } else { |
438 | } else { |
417 | push @out, $_; |
439 | push @out, $_; |
418 | } |
440 | } |
419 | } else { |
441 | } else { |
… | |
… | |
427 | sub get_plan { |
449 | sub get_plan { |
428 | my ($self) = @_; |
450 | my ($self) = @_; |
429 | |
451 | |
430 | my $ingred = $self->{ingredients}; |
452 | my $ingred = $self->{ingredients}; |
431 | |
453 | |
432 | for my $grp (keys %$ingred) { |
|
|
433 | for my $pot (@{$ingred->{$grp}}) { |
|
|
434 | for my $plan (keys %{$Jeweler::CFG->{plans}}) { |
454 | for my $plan (keys %{$Jeweler::CFG->{plans}}) { |
435 | my $plg = $Jeweler::CFG->{plans}->{$plan}; |
455 | my $plg = $Jeweler::CFG->{plans}->{$plan}; |
436 | my @plga = (); |
456 | my @plga = (); |
437 | unless (ref $plg eq 'ARRAY') { |
457 | unless (ref $plg eq 'ARRAY') { |
438 | push @plga, $plg; |
458 | push @plga, $plg; |
439 | } else { |
459 | } else { |
440 | @plga = @$plg; |
460 | @plga = @$plg; |
441 | } |
461 | } |
442 | next unless @plga > 0; |
462 | next unless @plga > 0; |
443 | if (Jeweler::Util::grep_for_match ($pot, @plga)) { |
463 | if (Jeweler::Util::grep_for_match ($ingred, @plga)) { |
444 | return $plan; |
464 | return $plan; |
445 | } |
|
|
446 | } |
|
|
447 | } |
465 | } |
448 | } |
466 | } |
449 | } |
467 | } |
450 | |
468 | |
451 | sub get_ring { |
469 | sub get_ring { |
… | |
… | |
454 | @{$self->{ingredients}->{ammys} || []}, |
472 | @{$self->{ingredients}->{ammys} || []}, |
455 | @{$self->{ingredients}->{rings} || []} |
473 | @{$self->{ingredients}->{rings} || []} |
456 | ); |
474 | ); |
457 | } |
475 | } |
458 | |
476 | |
|
|
477 | sub improve_max { |
|
|
478 | my ($stat, $impro) = @_; |
|
|
479 | if ($stat >= 0) { |
|
|
480 | $stat = $impro > $stat ? $impro : $stat; |
|
|
481 | } |
|
|
482 | $stat |
|
|
483 | } |
|
|
484 | |
459 | sub improve_ring_by_plan { |
485 | sub improve_ring_by_plan { |
460 | my ($self, $plan, $ring) = @_; |
486 | my ($self, $plan, $ring) = @_; |
461 | |
487 | |
462 | $ring = dclone ($ring); |
488 | $ring = dclone $ring; |
463 | |
489 | |
464 | my $ingred = $self->{ingredients}; |
490 | my $ingred = $self->{ingredients}; |
465 | my $impr = {}; |
491 | my $impr = {}; |
466 | |
492 | |
467 | if ($plan =~ m/^stat_(\S+)$/) { |
493 | if ($plan =~ m/^stat_(\S+)$/) { |
468 | my $statname = $1; |
494 | my $statname = $1; |
469 | my $plingred = Jeweler::getcfg (plans => $plan) |
495 | my $plingred = Jeweler::getcfg (plans => $plan) |
470 | or die "ingredients for plan '$plan' not defined!"; |
496 | or die "ingredients for plan '$plan' not defined!"; |
471 | |
497 | |
472 | my $cnt = 0; |
498 | my $cnt = 0; |
473 | for my $pot (@{$ingred->{potions}}) { |
|
|
474 | if (Jeweler::Util::grep_for_match ($pot, @$plingred)) { |
499 | if (my $pot = Jeweler::Util::grep_for_match ($ingred, @$plingred)) { |
475 | $cnt += $pot->nrof; |
500 | $cnt += $pot->nrof; |
476 | } |
|
|
477 | } |
501 | } |
478 | |
502 | |
479 | my $maxstat = Jeweler::getcfg (maximprovements => 'stats'); |
503 | my $maxstat = Jeweler::getcfg (maximprovements => 'stats'); |
480 | my $did_impr = 0; |
|
|
481 | for my $x (1..$maxstat) { |
504 | for my $x (1..$maxstat) { |
482 | my $y = Jeweler::Object::fx ($x, 'stat_potions'); |
505 | my $y = Jeweler::Object::fx ($x, 'stat_items'); |
483 | |
506 | |
484 | if ($cnt <= $y->[0]) { |
507 | if ($cnt <= $y->[0]) { |
485 | $ring->{hash}->{stat}->{$statname} += $x; |
508 | $ring->{hash}->{stat}->{$statname} = |
486 | $did_impr = 1; |
509 | improve_max $ring->{hash}->{stat}->{$statname}, $x; |
487 | last; |
510 | last; |
488 | } |
511 | } |
489 | } |
512 | } |
490 | |
513 | |
491 | # we want at least this improvement if we have a plan... |
|
|
492 | $ring->{hash}->{stat}->{$statname} += 1 unless $did_impr; |
|
|
493 | |
|
|
494 | } elsif ($plan =~ m/^spec_(\S+)$/) { |
514 | } elsif ($plan =~ m/^spec_(\S+)$/) { |
|
|
515 | my $specname = $1; |
|
|
516 | my $plingred = Jeweler::getcfg (plans => $plan) |
|
|
517 | or die "ingredients for plan '$plan' not defined!"; |
|
|
518 | |
|
|
519 | my $cnt = 0; |
|
|
520 | if (my $pot = Jeweler::Util::grep_for_match ($ingred, @$plingred)) { |
|
|
521 | $cnt += $pot->nrof; |
|
|
522 | } |
|
|
523 | |
|
|
524 | my $maxspec = Jeweler::getcfg (maximprovements => 'specials'); |
|
|
525 | for my $x (1..$maxspec) { |
|
|
526 | my $y = Jeweler::Object::fx ($x, 'spec_items'); |
|
|
527 | |
|
|
528 | if ($cnt <= $y->[0]) { |
|
|
529 | $ring->{hash}->{spec}->{$specname} = |
|
|
530 | improve_max $ring->{hash}->{spec}->{$specname}, $x; |
|
|
531 | last; |
|
|
532 | } |
|
|
533 | } |
|
|
534 | |
495 | } elsif ($plan =~ m/^resist_(\S+)$/) { |
535 | } elsif ($plan =~ m/^resist_(\S+)$/) { |
|
|
536 | my $resname = $1; |
|
|
537 | my $resnum = $REV_RESMAP{$resname}; |
|
|
538 | my $plingred = Jeweler::getcfg (plans => $plan) |
|
|
539 | or die "ingredients for plan '$plan' not defined!"; |
|
|
540 | |
|
|
541 | my $cnt = 0; |
|
|
542 | if (my $it = Jeweler::Util::grep_for_match ($ingred, @$plingred)) { |
|
|
543 | $cnt += $it->nrof; |
|
|
544 | } |
|
|
545 | my $resist_item_nr = 0; |
|
|
546 | $self->do_grep (sub { $resist_item_nr += ($_[0]->nrof || 1); 0 }, @$plingred); |
|
|
547 | |
|
|
548 | my $maximprovname = (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) |
|
|
549 | ? 'effect_resistances' |
|
|
550 | : 'attack_resistances'; |
|
|
551 | |
|
|
552 | my $maxres = Jeweler::getcfg (maximprovements => $maximprovname); |
|
|
553 | $resist_item_nr = $maxres if ($resist_item_nr > $maxres); |
|
|
554 | $ring->{hash}->{resist}->{$resnum} = |
|
|
555 | improve_max $ring->{hash}->{resist}->{$resnum}, $resist_item_nr; |
496 | } |
556 | } |
497 | |
557 | |
498 | return $ring; |
558 | return $ring; |
499 | } |
559 | } |
500 | |
560 | |
501 | sub do_grep { |
561 | sub do_grep { |
502 | my ($self, $cb, @grepar) = @_; |
562 | my ($self, $cb, $cat, @grepar) = @_; |
503 | |
563 | |
504 | my $ingred = $self->{ingredients}; |
564 | my $ingred = $self->{ingredients}; |
505 | |
565 | |
506 | |
|
|
507 | for my $cat (keys %$ingred) { |
|
|
508 | my @rem; |
566 | my @rem; |
509 | for my $ing (@{$ingred->{$cat}}) { |
567 | for my $ing (@{$ingred->{$cat}}) { |
510 | if (Jeweler::Util::grep_for_match ($ing, @grepar)) { |
568 | if (Jeweler::Util::check_for_match ($ing, @grepar)) { |
511 | unless ($cb->($ing)) { |
569 | unless ($cb->($ing)) { |
512 | push @rem, $ing; |
|
|
513 | } |
|
|
514 | } else { |
|
|
515 | push @rem, $ing; |
570 | push @rem, $ing; |
516 | } |
571 | } |
|
|
572 | } else { |
|
|
573 | push @rem, $ing; |
517 | } |
574 | } |
|
|
575 | } |
518 | @{$ingred->{$cat}} = @rem; |
576 | @{$ingred->{$cat}} = @rem; |
519 | } |
|
|
520 | } |
577 | } |
521 | |
578 | |
522 | sub check_costs { |
579 | sub check_costs { |
523 | my ($self, $costs, $do_remove) = @_; |
580 | my ($self, $costs, $do_remove) = @_; |
524 | |
581 | |
525 | my $costs = dclone ($costs); |
582 | my $costs = dclone $costs; |
526 | |
583 | |
527 | for my $key (keys %$costs) { |
584 | for my $key (keys %$costs) { |
528 | my @grepar; |
585 | my @grepar; |
529 | if ($key =~ m/^stat_(\S+)$/) { |
586 | if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items |
530 | @grepar = @{Jeweler::getcfg (plans => $key) || []}; |
587 | eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} }; |
531 | } else { |
588 | next if $@; |
|
|
589 | } else { # check the gems |
532 | @grepar = (undef, undef, $key); |
590 | @grepar = ('gems', undef, undef, $key); |
533 | } |
591 | } |
534 | |
592 | |
535 | if ($do_remove) { |
593 | if ($do_remove) { |
536 | my $rem = $costs->{$key}; |
594 | my $rem = $costs->{$key}; |
|
|
595 | $self->do_grep (sub { |
537 | $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); |
538 | if ($rem > 0) { |
599 | if ($rem > 0) { |
539 | warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; |
600 | warn "JEWELER BUG: removed ingredients ($key) $rem > 0 after removing!"; |
540 | } |
601 | } |
|
|
602 | |
541 | } else { |
603 | } else { |
542 | my $nr; |
604 | my $nr; |
543 | $self->do_grep (sub { $nr += $_[0]->nrof; 0 }, @grepar); |
605 | $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar); |
544 | $costs->{$key} -= $nr; |
606 | $costs->{$key} -= $nr; |
545 | } |
607 | } |
|
|
608 | |
546 | } |
609 | } |
547 | |
610 | |
548 | return $costs; |
611 | return $costs; |
549 | } |
612 | } |
550 | |
613 | |
… | |
… | |
577 | $self->ring_or_ammy_to_hash ($arg{object}); |
640 | $self->ring_or_ammy_to_hash ($arg{object}); |
578 | |
641 | |
579 | $self; |
642 | $self; |
580 | } |
643 | } |
581 | |
644 | |
|
|
645 | sub 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 | |
|
|
656 | sub 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 | |
582 | sub analyze { |
687 | sub analyze { |
583 | 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 | } |
584 | |
694 | |
585 | my $sklvl = cf::exp_to_level ($sk->stats->exp); |
695 | my $sklvl = cf::exp_to_level ($sk->stats->exp); |
586 | my $ringlvl = $self->power_to_level; |
696 | my $ringlvl = $self->power_to_level; |
587 | |
697 | |
588 | my $tmpl; |
698 | my $tmpl; |
589 | if ($pl->flag (cf::FLAG_WIZ)) { |
699 | if ($pl->flag (cf::FLAG_WIZ)) { |
590 | $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl); |
700 | $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl); |
591 | } else { |
701 | } else { |
592 | $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl); |
702 | $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl); |
593 | } |
703 | } |
594 | 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); |
595 | return $msg; |
705 | return $msg; |
|
|
706 | } |
|
|
707 | |
|
|
708 | sub calc_value_from_cost { |
|
|
709 | my ($self, $costs) = @_; |
|
|
710 | my $emarch = cf::arch::find 'emerald'; |
|
|
711 | my $saarch = cf::arch::find 'sapphire'; |
|
|
712 | my $pearch = cf::arch::find 'pearl'; |
|
|
713 | my $ruarch = cf::arch::find 'ruby'; |
|
|
714 | my $diarch = cf::arch::find 'gem'; |
|
|
715 | my $value = $emarch->value * $costs->{emerald} |
|
|
716 | + $saarch->value * $costs->{sapphire} |
|
|
717 | + $pearch->value * $costs->{pearl} |
|
|
718 | + $ruarch->value * $costs->{ruby} |
|
|
719 | + $diarch->value * $costs->{gem}; |
|
|
720 | |
|
|
721 | $value |
596 | } |
722 | } |
597 | |
723 | |
598 | sub wiz_analyze { |
724 | sub wiz_analyze { |
599 | my ($self, $pl) = @_; |
725 | my ($self, $pl) = @_; |
600 | my $costs = $self->calc_costs; |
726 | my $costs = $self->calc_costs; |
|
|
727 | if (defined $costs) { |
601 | my $desc = ""; |
728 | my $desc = ""; |
602 | my $lvl = $self->power_to_level (\$desc); |
729 | my $lvl = $self->power_to_level (\$desc); |
|
|
730 | my $scosts = $self->calc_value_from_cost ($costs); |
|
|
731 | |
|
|
732 | $pl->message ("costs: " |
603 | $pl->message ("costs: " . join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)); |
733 | . (join ', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs) |
|
|
734 | . " (" |
|
|
735 | . ($scosts / "platinacoin"->cf::arch::find->value) |
|
|
736 | . " platinum)"); |
604 | $pl->message ("level: " . $desc); |
737 | $pl->message ("level: $desc"); |
|
|
738 | } else { |
|
|
739 | $pl->message ("level: impossible to make, due to impossible resistancy configuration"); |
|
|
740 | } |
605 | } |
741 | } |
606 | |
|
|
607 | |
742 | |
608 | sub get_chance_perc { |
743 | sub get_chance_perc { |
609 | my ($self, $sk) = @_; |
744 | my ($self, $sk) = @_; |
610 | my $sklvl = cf::exp_to_level ($sk->stats->exp); |
745 | my $sklvl = cf::exp_to_level ($sk->stats->exp); |
611 | my $ringlvl = $self->power_to_level; |
746 | my $ringlvl = $self->power_to_level; |
… | |
… | |
614 | |
749 | |
615 | sub fx { |
750 | sub fx { |
616 | my ($res, $cfg) = @_; |
751 | my ($res, $cfg) = @_; |
617 | my $or = $res; |
752 | my $or = $res; |
618 | my $ar = $Jeweler::CFG->{functions}->{$cfg}; |
753 | my $ar = $Jeweler::CFG->{functions}->{$cfg}; |
|
|
754 | |
619 | if (ref $ar->[0] eq 'ARRAY') { |
755 | if (ref $ar && ref $ar->[0] eq 'ARRAY') { |
620 | $res = $res - 1; |
756 | $res = $res - 1; |
|
|
757 | return $ar->[max (min ($res, @$ar - 1), 0)]; |
|
|
758 | |
621 | } else { |
759 | } else { |
622 | $res = ceil ($res / 5) - 1; |
760 | # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3... |
623 | } |
761 | # old code: |
|
|
762 | #my $idx = ceil (($res / 5) + 0.1) - 1; |
624 | $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 | } |
625 | } |
771 | } |
626 | |
772 | |
627 | sub improve_by_ring { |
773 | sub improve_by_ring { |
628 | my ($self, @rings) = @_; |
774 | my ($self, @rings) = @_; |
629 | my $ring = $self; |
775 | my $ring = $self; |
… | |
… | |
645 | if ($self->{hash}->{$cat}->{$k} > 0) { |
791 | if ($self->{hash}->{$cat}->{$k} > 0) { |
646 | $self->{hash}->{$cat}->{$k} *= -1; |
792 | $self->{hash}->{$cat}->{$k} *= -1; |
647 | } |
793 | } |
648 | } |
794 | } |
649 | } |
795 | } |
|
|
796 | $self->{hash}{value} = 0; |
650 | } |
797 | } |
651 | |
798 | |
652 | sub to_string { |
799 | sub to_string { |
653 | my ($self) = @_; |
800 | my ($self) = @_; |
654 | my $r = $self->{hash}; |
801 | my $r = $self->{hash}; |
… | |
… | |
693 | $obj->{spec}{ac} = $stats->ac; |
840 | $obj->{spec}{ac} = $stats->ac; |
694 | $obj->{spec}{speed} = $stats->exp; |
841 | $obj->{spec}{speed} = $stats->exp; |
695 | $obj->{spec}{food} = $stats->food; |
842 | $obj->{spec}{food} = $stats->food; |
696 | |
843 | |
697 | $obj->{name} = $thing->name; |
844 | $obj->{name} = $thing->name; |
698 | $obj->{arch} = $thing->arch->name; |
845 | $obj->{arch} = $thing->arch->archname; |
699 | $obj->{face} = $thing->face; |
846 | $obj->{face} = $thing->face; |
|
|
847 | |
|
|
848 | $obj->{value} = $thing->value; |
|
|
849 | |
|
|
850 | $obj->{is_ring} = ($thing->type == cf::RING); |
700 | |
851 | |
701 | $self->{hash} = $obj |
852 | $self->{hash} = $obj |
702 | } |
853 | } |
703 | |
854 | |
704 | sub to_object { |
855 | sub to_object { |
705 | my ($self) = @_; |
856 | my ($self) = @_; |
706 | |
857 | |
707 | 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! |
708 | |
861 | |
709 | $obj->face ($self->{hash}{face}); |
862 | $obj->face ($self->{hash}{face}); |
710 | |
863 | |
711 | my $stats = $obj->stats; |
864 | my $stats = $obj->stats; |
712 | |
865 | |
… | |
… | |
725 | $obj->resist ($_, $self->{hash}->{resist}->{$_}); |
878 | $obj->resist ($_, $self->{hash}->{resist}->{$_}); |
726 | } |
879 | } |
727 | |
880 | |
728 | $obj->flag (cf::FLAG_IDENTIFIED, 1); |
881 | $obj->flag (cf::FLAG_IDENTIFIED, 1); |
729 | |
882 | |
|
|
883 | $obj->value ($self->{hash}{value}); |
|
|
884 | |
730 | return $obj; |
885 | return $obj; |
|
|
886 | } |
|
|
887 | |
|
|
888 | sub set_value { $_[0]->{hash}{value} = $_[1] } |
|
|
889 | |
|
|
890 | sub is_better_than { |
|
|
891 | my ($self, $other) = @_; |
|
|
892 | |
|
|
893 | for my $type (qw/spec stat resist/) { |
|
|
894 | for my $stat (keys %{$self->{hash}->{$type}}) { |
|
|
895 | if ($self->{hash}->{$type}->{$stat} > $other->{hash}->{$type}->{$stat}) { |
|
|
896 | return 1; |
|
|
897 | } |
|
|
898 | } |
|
|
899 | } |
|
|
900 | |
|
|
901 | return 0; |
731 | } |
902 | } |
732 | |
903 | |
733 | sub stat_level { |
904 | sub stat_level { |
734 | my ($self) = @_; |
905 | my ($self) = @_; |
735 | my $stats = $self->{hash}->{stat} || {}; |
906 | my $stats = $self->{hash}->{stat} || {}; |
… | |
… | |
747 | sub resist_level { |
918 | sub resist_level { |
748 | my ($self) = @_; |
919 | my ($self) = @_; |
749 | |
920 | |
750 | my $resists = $self->{hash}->{resist} || {}; |
921 | my $resists = $self->{hash}->{resist} || {}; |
751 | |
922 | |
752 | my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level'); |
923 | my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level'); |
753 | my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level'); |
924 | my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level'); |
754 | my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances'); |
925 | my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances'); |
755 | my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances'); |
926 | my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances'); |
756 | my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances'); |
927 | my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances'); |
757 | |
928 | |
758 | my $ressum = 0; |
929 | my $ressum = 0; |
… | |
… | |
818 | sub power_to_level { |
989 | sub power_to_level { |
819 | my ($self, $lvldescr) = @_; |
990 | my ($self, $lvldescr) = @_; |
820 | |
991 | |
821 | my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements'); |
992 | my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements'); |
822 | 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'); |
823 | |
995 | |
824 | my ($stat_lvl, $stat_imprs) = $self->stat_level; |
996 | my ($stat_lvl, $stat_imprs) = $self->stat_level; |
825 | my ($resist_lvl, $res_imprs) = $self->resist_level; |
997 | my ($resist_lvl, $res_imprs) = $self->resist_level; |
826 | my ($spec_lvl, $spec_imprs) = $self->special_level; |
998 | my ($spec_lvl, $spec_imprs) = $self->special_level; |
827 | |
999 | |
828 | my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs; |
1000 | my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs; |
829 | |
1001 | |
830 | 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 |
831 | |
1005 | |
832 | 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); |
833 | |
1013 | |
834 | if ($lvldescr) { |
1014 | if ($lvldescr) { |
835 | $$lvldescr = |
1015 | $$lvldescr = |
836 | sprintf "%3d: %s\n", $levl, |
1016 | sprintf "%3d: %s\n", $levl, |
837 | "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, " |
1017 | "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, " |
… | |
… | |
849 | for my $stat (keys %$stats) { |
1029 | for my $stat (keys %$stats) { |
850 | my $sum = $stats->{$stat}; |
1030 | my $sum = $stats->{$stat}; |
851 | |
1031 | |
852 | next unless $sum > 0; |
1032 | next unless $sum > 0; |
853 | |
1033 | |
854 | my $statfx = fx ($sum, 'stat_potions'); |
1034 | my $statfx = fx ($sum, 'stat_items'); |
855 | $cost->{"stat_$stat"} += $statfx->[0]; |
1035 | $cost->{"stat_$stat"} += $statfx->[0]; |
856 | split_diamonds ($cost, $statfx->[1], 'stat_' . $stat); |
1036 | split_diamonds ($cost, $statfx->[1], 'stat_' . $stat); |
857 | } |
1037 | } |
858 | } |
1038 | } |
859 | |
1039 | |
… | |
… | |
865 | for my $spec (keys %$specials) { |
1045 | for my $spec (keys %$specials) { |
866 | my $sum = $specials->{$spec}; |
1046 | my $sum = $specials->{$spec}; |
867 | |
1047 | |
868 | next unless $sum > 0; |
1048 | next unless $sum > 0; |
869 | |
1049 | |
870 | my $specfx = fx ($sum, 'spec_potions'); |
1050 | my $specfx = fx ($sum, 'spec_items'); |
871 | $cost->{"spec_$spec"} += $specfx->[0]; |
1051 | $cost->{"spec_$spec"} += $specfx->[0]; |
872 | split_diamonds ($cost, $specfx->[1], 'spec_' . $spec); |
1052 | split_diamonds ($cost, $specfx->[1], 'spec_' . $spec); |
873 | } |
1053 | } |
874 | } |
1054 | } |
875 | |
1055 | |
… | |
… | |
878 | |
1058 | |
879 | my $costs = {}; |
1059 | my $costs = {}; |
880 | |
1060 | |
881 | my $ring = $self->{hash}; |
1061 | my $ring = $self->{hash}; |
882 | |
1062 | |
883 | for my $resnam (keys %{$ring->{resist} || {}}) { |
1063 | for my $resnum (keys %{$ring->{resist} || {}}) { |
884 | |
1064 | |
885 | my $res = $ring->{resist}->{$resnam}; |
1065 | my $res = $ring->{resist}->{$resnum}; |
886 | |
1066 | |
887 | next unless $res > 0; |
1067 | next unless $res > 0; |
888 | |
1068 | |
|
|
1069 | return undef if $res == 100; |
|
|
1070 | |
889 | $costs->{"resist_" . $Jeweler::RESMAP{$resnam} . "_item"} += $res; |
1071 | $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res; |
890 | |
1072 | |
891 | my $diamonds; |
1073 | my $diamonds; |
892 | if (grep { $resnam eq $_ } @Jeweler::EFFECT_RESISTS) { |
1074 | if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) { |
893 | $diamonds += fx ($res, 'effect_resist_diamonds'); |
1075 | $diamonds += fx ($res, 'effect_resist_diamonds_x'); |
894 | } else { |
1076 | } else { |
895 | $diamonds += fx ($res, 'attack_resist_diamonds'); |
1077 | $diamonds += fx ($res, 'attack_resist_diamonds_x'); |
896 | } |
1078 | } |
897 | |
1079 | |
898 | split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnam}); |
1080 | split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum}); |
899 | } |
1081 | } |
900 | |
1082 | |
901 | $self->add_stat_costs ($costs); |
1083 | $self->add_stat_costs ($costs); |
902 | $self->add_special_costs ($costs); |
1084 | $self->add_special_costs ($costs); |
903 | |
1085 | |
… | |
… | |
908 | my ($cost, $diamonds, $category) = @_; |
1090 | my ($cost, $diamonds, $category) = @_; |
909 | |
1091 | |
910 | my $stat_split = Jeweler::getcfg (diamond_split => $category); |
1092 | my $stat_split = Jeweler::getcfg (diamond_split => $category); |
911 | |
1093 | |
912 | my $sum = sum (@$stat_split); |
1094 | my $sum = sum (@$stat_split); |
913 | if ($sum < (1 - 0.0001)) { |
|
|
914 | warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!"; |
|
|
915 | } |
|
|
916 | |
1095 | |
917 | my $emarch = cf::arch::find 'emerald'; |
1096 | my $emarch = cf::arch::find 'emerald'; |
918 | my $saarch = cf::arch::find 'sapphire'; |
1097 | my $saarch = cf::arch::find 'sapphire'; |
919 | my $pearch = cf::arch::find 'pearl'; |
1098 | my $pearch = cf::arch::find 'pearl'; |
920 | my $ruarch = cf::arch::find 'ruby'; |
1099 | my $ruarch = cf::arch::find 'ruby'; |
921 | my $diarch = cf::arch::find 'gem'; |
1100 | my $diarch = cf::arch::find 'gem'; |
922 | |
1101 | |
923 | my $sumvalue = $diarch->clone->value * $diamonds; |
1102 | my $sumvalue = $diarch->value * $diamonds; |
924 | |
1103 | |
925 | $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->clone->value; |
1104 | $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->value; |
926 | $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->clone->value; |
1105 | $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->value; |
927 | $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->clone->value; |
1106 | $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->value; |
928 | $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->clone->value; |
1107 | $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->value; |
929 | $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->clone->value; |
1108 | $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value; |
930 | } |
1109 | } |
931 | |
1110 | |
932 | package Jeweler::Util; |
1111 | package Jeweler::Util; |
933 | |
1112 | |
934 | use strict; |
1113 | use strict; |
… | |
… | |
940 | =over 4 |
1119 | =over 4 |
941 | |
1120 | |
942 | =item remove ($object[, $nrof]) |
1121 | =item remove ($object[, $nrof]) |
943 | |
1122 | |
944 | Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects. |
1123 | Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects. |
945 | The returnvalue is the number of 'single' objects that couldn't be removed. |
1124 | The return value is the number of 'single' objects that couldn't be removed. |
946 | |
1125 | |
947 | =cut |
1126 | =cut |
948 | |
1127 | |
949 | sub remove { |
1128 | sub remove { |
950 | 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)); |
951 | |
1134 | |
952 | my $cnt; |
1135 | $r |
953 | |
|
|
954 | if (defined $nrof) { |
|
|
955 | return 0 if ($nrof * 1) == 0; |
|
|
956 | $cnt = int (($obj->nrof || 1) - (1 * $nrof)); |
|
|
957 | |
|
|
958 | if ($cnt > 0) { |
|
|
959 | $obj->nrof ($cnt); |
|
|
960 | return 0; |
|
|
961 | } |
|
|
962 | } |
|
|
963 | |
|
|
964 | remove ($_) for $obj->inv; |
|
|
965 | $obj->destroy; |
|
|
966 | return $cnt; |
|
|
967 | } |
1136 | } |
968 | |
1137 | |
969 | sub grep_for_match { |
1138 | sub check_for_match { |
970 | my ($thing, @matchar) = @_; |
1139 | my ($thing, @matchar) = @_; |
971 | |
1140 | |
972 | my $i = 0; |
1141 | my $i = 0; |
|
|
1142 | my $check_cnts = 0; |
|
|
1143 | my $check_true = 0; |
973 | 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 | |
974 | if ($match =~ m/^\s*$/) { |
1151 | if ($match =~ m/^\s*$/) { |
975 | $i++; |
1152 | $i++; |
976 | next; |
1153 | next; |
977 | } |
1154 | } |
978 | |
1155 | |
|
|
1156 | $check_cnts++; |
979 | if ($i % 3 == 0) { |
1157 | if ($i % 3 == 0) { |
980 | $thing->name eq $match |
1158 | $thing->name eq $match |
981 | and return 1; |
1159 | and $check_true++; |
982 | } elsif ($i % 3 == 1) { |
1160 | } elsif ($i % 3 == 1) { |
983 | $thing->title eq $match |
1161 | $thing->title eq $match |
984 | and return 1; |
1162 | and $check_true++; |
985 | } else { # $i % 3 == 2 |
1163 | } else { # $i % 3 == 2 |
986 | $thing->arch->name eq $match |
1164 | $thing->arch->archname eq $match |
987 | and return 1; |
1165 | and $check_true++; |
988 | } |
1166 | } |
989 | $i++; |
1167 | $i++; |
990 | } |
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; |
991 | return 0; |
1171 | return 0; |
992 | } |
1172 | } |
993 | |
1173 | |
|
|
1174 | sub grep_for_match { |
|
|
1175 | my ($ingred, $group, @matchar) = @_; |
|
|
1176 | |
|
|
1177 | for my $thing (@{$ingred->{$group} || []}) { |
|
|
1178 | #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->archname, "@matchar"; #d# |
|
|
1179 | if (check_for_match ($thing, @matchar)) { |
|
|
1180 | return $thing; |
|
|
1181 | } |
|
|
1182 | } |
|
|
1183 | return undef; |
|
|
1184 | } |
|
|
1185 | |
994 | =back |
1186 | =back |
995 | |
1187 | |
996 | 1 |
1188 | 1 |