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.1 by root, Fri Dec 15 19:29:18 2006 UTC vs.
Revision 1.25 by root, Thu Apr 10 15:35:16 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;
117 cf::ATNR_LIFE_STEALING => "LIFE_STEALING", 116 cf::ATNR_LIFE_STEALING => "LIFE_STEALING",
118 cf::ATNR_BLIND => "BLIND", 117 cf::ATNR_BLIND => "BLIND",
119 cf::ATNR_DISEASE => "DISEASE", 118 cf::ATNR_DISEASE => "DISEASE",
120); 119);
121 120
121our %REV_RESMAP = map { $RESMAP{$_} => $_ } keys %RESMAP;
122
122our %LVL_DIFF_CHANCES = ( 123our %LVL_DIFF_CHANCES = (
123 +5 => 100, 124 +5 => 100,
124 +4 => 95, 125 +4 => 95,
125 +3 => 85, 126 +3 => 85,
126 +2 => 75, 127 +2 => 75,
160 $delta = 5 if $delta > 5; 161 $delta = 5 if $delta > 5;
161 return $LVL_DIFF_CHANCES{$delta} 162 return $LVL_DIFF_CHANCES{$delta}
162} 163}
163 164
164sub analyze { 165sub analyze {
165 my ($sk, $chdl, $pl) = @_; 166 my ($sk, $chdl, $pl, $input_level) = @_;
166 167
167 my $hadunid = 0; 168 my $hadunid = 0;
169 my $found = 0;
168 for ($chdl->grep_by_type (cf::RING, cf::AMULET)) { 170 for ($chdl->grep_by_type (cf::RING, cf::AMULET)) {
169 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) { 171 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) {
170 $hadunid = 1; 172 $hadunid = 1;
171 next; 173 next;
172 } 174 }
175 $found = 1;
173 my $r = Jeweler::Object->new (object => $_); 176 my $r = Jeweler::Object->new (object => $_);
174 my $msg = $r->analyze ($sk, $pl); 177 my $msg = $r->analyze ($sk, $pl, $input_level);
175 $pl->message ($r->to_string . ": " . $msg); 178 $pl->message ($r->to_string . ": " . $msg);
176 if ($pl->flag (cf::FLAG_WIZ)) { 179 if ($pl->flag (cf::FLAG_WIZ)) {
177 $r->wiz_analyze ($pl); 180 $r->wiz_analyze ($pl);
178 } 181 }
179 } 182 }
180 if ($hadunid) {
181 $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!")
182 } 184 if $hadunid;
185 $pl->message ("You couldn't find anything in the bench to analyse!")
186 unless $found;
183} 187}
184 188
185# this function converts metals/minerals into a raw ring (of adornment) 189# this function converts metals/minerals into a raw ring (of adornment)
186sub simple_converter { 190sub simple_converter {
187 my ($pl, $ingred, $chdl, $conv) = @_; 191 my ($pl, $ingred, $chdl, $conv) = @_;
201 warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!"; 205 warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!";
202 return; 206 return;
203 } 207 }
204 208
205 unless ($xp_gain > 0) { 209 unless ($xp_gain > 0) {
206 warn "WARNING: xp gain isn't > 0 in convesion '$outarch'\n"; 210 warn "WARNING: xp gain isn't > 0 in conversion '$outarch'\n";
207 return; 211 return;
208 } 212 }
209 213
210 unless ($outarchvalfact) { 214 unless ($outarchvalfact) {
211 warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n"; 215 warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n";
217 } 221 }
218 222
219 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname); 223 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname);
220 $ingred->remove ($ingr_grp, $srcarchname); 224 $ingred->remove ($ingr_grp, $srcarchname);
221 225
222 my $outarchval = cf::arch::find ($outarch)->clone->value; 226 my $outarchval = cf::arch::find ($outarch)->value;
223 227
224 my $nrof = int ($archvalsum / (($outarchval || 1000) * $outarchvalfact)); 228 my $nrof = int $archvalsum / (($outarchval || 1000) * $outarchvalfact);
225 if ($nrof) { 229 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) 230 # XXX: yes, I know what I'm doing here, I don't set nrof, but it didn't work somehow (pls. check sometimes)
231 for (1 .. $nrof) {
227 $chdl->put (cf::object::new $outarch) for 1..$nrof; 232 $chdl->put (my $ob = cf::object::new $outarch);
233 $ob->set_animation (cf::rndm $ob->num_animations)
234 if ($ob->type == cf::RING);
235 $ob->flag (cf::FLAG_IDENTIFIED, 1);
236 }
228 237
229 my $xp_sum = ($xp_gain * $nrof); 238 my $xp_sum = $xp_gain * $nrof;
230 239
231 if ($xp_sum) { 240 if ($xp_sum) {
232 $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");
233 $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_ADD_SKILL); 242 $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_SKILL_ONLY);
234 } 243 }
244 } else {
245 $pl->ob->message ("You fail to make something, probably you used not enough source material?");
235 } 246 }
236} 247}
237 248
238 249
239package Jeweler::CauldronHandler; 250package Jeweler::CauldronHandler;
271 my ($self, $arch_name, @map_stack) = @_; 282 my ($self, $arch_name, @map_stack) = @_;
272 283
273 my @c = 284 my @c =
274 grep { 285 grep {
275 $_->flag (cf::FLAG_IS_CAULDRON) 286 $_->flag (cf::FLAG_IS_CAULDRON)
276 and $_->arch->name eq $arch_name 287 and $_->arch->archname eq $arch_name
277 } @map_stack; 288 } @map_stack;
278 289
279 $self->{cauldron} = $c[0]; 290 $self->{cauldron} = $c[0];
280} 291}
281 292
320 cf::POTION => 'potions', 331 cf::POTION => 'potions',
321 cf::SCROLL => 'scrolls', 332 cf::SCROLL => 'scrolls',
322 ); 333 );
323 334
324 for ($self->{cauldron}->inv) { 335 for ($self->{cauldron}->inv) {
336 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) {
337 die "unidentified";
338 } elsif ($_->flag (cf::FLAG_CURSED) || $_->flag (cf::FLAG_DAMNED)) {
339 die "cursed";
340 }
325 341
326 if (my $k = $type_to_key{$_->type}) { 342 if (my $k = $type_to_key{$_->type}) {
327 push @{$ingreds->{$k}}, $_; 343 push @{$ingreds->{$k}}, $_;
344 } else {
345 push @{$ingreds->{other}}, $_;
328 } 346 }
329 } 347 }
330 348
331 return Jeweler::Ingredients->new (ingredients => $ingreds, cauldron_helper => $self) 349 return Jeweler::Ingredients->new (ingredients => $ingreds, cauldron_helper => $self)
332} 350}
339 357
340sub put { 358sub put {
341 my ($self, $obj) = @_; 359 my ($self, $obj) = @_;
342 360
343 return undef unless $self->{cauldron}; 361 return undef unless $self->{cauldron};
344 $obj->insert_ob_in_ob ($self->{cauldron}); 362 $self->{cauldron}->insert ($obj);
345} 363}
346 364
347=back 365=back
348 366
349=cut 367=cut
382 400
383sub value { 401sub value {
384 my ($self, $group, $archname) = @_; 402 my ($self, $group, $archname) = @_;
385 403
386 my @objs = grep { 404 my @objs = grep {
387 $_->arch->name eq $archname 405 $_->arch->archname eq $archname
388 } @{$self->{ingredients}->{$group} || []}; 406 } @{$self->{ingredients}->{$group} || []};
389 407
390 my $sum = 0; 408 my $sum = 0;
391 for (@objs) { 409 for (@objs) {
392 $sum += ($_->nrof || 1) * $_->value; 410 $sum += ($_->nrof || 1) * $_->value;
409 427
410 my @out; 428 my @out;
411 429
412 for (@{$ingred->{$group}}) { 430 for (@{$ingred->{$group}}) {
413 if (defined $archname) { 431 if (defined $archname) {
414 if ($_->arch->name eq $archname) { 432 if ($_->arch->archname eq $archname) {
415 Jeweler::Util::remove ($_); 433 Jeweler::Util::remove ($_);
416 } else { 434 } else {
417 push @out, $_; 435 push @out, $_;
418 } 436 }
419 } else { 437 } else {
427sub get_plan { 445sub get_plan {
428 my ($self) = @_; 446 my ($self) = @_;
429 447
430 my $ingred = $self->{ingredients}; 448 my $ingred = $self->{ingredients};
431 449
432 for my $grp (keys %$ingred) {
433 for my $pot (@{$ingred->{$grp}}) {
434 for my $plan (keys %{$Jeweler::CFG->{plans}}) { 450 for my $plan (keys %{$Jeweler::CFG->{plans}}) {
435 my $plg = $Jeweler::CFG->{plans}->{$plan}; 451 my $plg = $Jeweler::CFG->{plans}->{$plan};
436 my @plga = (); 452 my @plga = ();
437 unless (ref $plg eq 'ARRAY') { 453 unless (ref $plg eq 'ARRAY') {
438 push @plga, $plg; 454 push @plga, $plg;
439 } else { 455 } else {
440 @plga = @$plg; 456 @plga = @$plg;
441 } 457 }
442 next unless @plga > 0; 458 next unless @plga > 0;
443 if (Jeweler::Util::grep_for_match ($pot, @plga)) { 459 if (Jeweler::Util::grep_for_match ($ingred, @plga)) {
444 return $plan; 460 return $plan;
445 }
446 }
447 } 461 }
448 } 462 }
449} 463}
450 464
451sub get_ring { 465sub get_ring {
454 @{$self->{ingredients}->{ammys} || []}, 468 @{$self->{ingredients}->{ammys} || []},
455 @{$self->{ingredients}->{rings} || []} 469 @{$self->{ingredients}->{rings} || []}
456 ); 470 );
457} 471}
458 472
473sub improve_max {
474 my ($stat, $impro) = @_;
475 if ($stat >= 0) {
476 $stat = $impro > $stat ? $impro : $stat;
477 }
478 $stat
479}
480
459sub improve_ring_by_plan { 481sub improve_ring_by_plan {
460 my ($self, $plan, $ring) = @_; 482 my ($self, $plan, $ring) = @_;
461 483
462 $ring = dclone ($ring); 484 $ring = do { my $guard = Coro::Storable::guard; dclone $ring };
463 485
464 my $ingred = $self->{ingredients}; 486 my $ingred = $self->{ingredients};
465 my $impr = {}; 487 my $impr = {};
466 488
467 if ($plan =~ m/^stat_(\S+)$/) { 489 if ($plan =~ m/^stat_(\S+)$/) {
468 my $statname = $1; 490 my $statname = $1;
469 my $plingred = Jeweler::getcfg (plans => $plan) 491 my $plingred = Jeweler::getcfg (plans => $plan)
470 or die "ingredients for plan '$plan' not defined!"; 492 or die "ingredients for plan '$plan' not defined!";
471 493
472 my $cnt = 0; 494 my $cnt = 0;
473 for my $pot (@{$ingred->{potions}}) {
474 if (Jeweler::Util::grep_for_match ($pot, @$plingred)) { 495 if (my $pot = Jeweler::Util::grep_for_match ($ingred, @$plingred)) {
475 $cnt += $pot->nrof; 496 $cnt += $pot->nrof;
476 }
477 } 497 }
478 498
479 my $maxstat = Jeweler::getcfg (maximprovements => 'stats'); 499 my $maxstat = Jeweler::getcfg (maximprovements => 'stats');
480 my $did_impr = 0;
481 for my $x (1..$maxstat) { 500 for my $x (1..$maxstat) {
482 my $y = Jeweler::Object::fx ($x, 'stat_potions'); 501 my $y = Jeweler::Object::fx ($x, 'stat_items');
483 502
484 if ($cnt <= $y->[0]) { 503 if ($cnt <= $y->[0]) {
485 $ring->{hash}->{stat}->{$statname} += $x; 504 $ring->{hash}->{stat}->{$statname} =
486 $did_impr = 1; 505 improve_max $ring->{hash}->{stat}->{$statname}, $x;
487 last; 506 last;
488 } 507 }
489 } 508 }
490 509
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+)$/) { 510 } elsif ($plan =~ m/^spec_(\S+)$/) {
511 my $specname = $1;
512 my $plingred = Jeweler::getcfg (plans => $plan)
513 or die "ingredients for plan '$plan' not defined!";
514
515 my $cnt = 0;
516 if (my $pot = Jeweler::Util::grep_for_match ($ingred, @$plingred)) {
517 $cnt += $pot->nrof;
518 }
519
520 my $maxspec = Jeweler::getcfg (maximprovements => 'specials');
521 for my $x (1..$maxspec) {
522 my $y = Jeweler::Object::fx ($x, 'spec_items');
523
524 if ($cnt <= $y->[0]) {
525 $ring->{hash}->{spec}->{$specname} =
526 improve_max $ring->{hash}->{spec}->{$specname}, $x;
527 last;
528 }
529 }
530
495 } elsif ($plan =~ m/^resist_(\S+)$/) { 531 } elsif ($plan =~ m/^resist_(\S+)$/) {
532 my $resname = $1;
533 my $resnum = $REV_RESMAP{$resname};
534 my $plingred = Jeweler::getcfg (plans => $plan)
535 or die "ingredients for plan '$plan' not defined!";
536
537 my $cnt = 0;
538 if (my $it = Jeweler::Util::grep_for_match ($ingred, @$plingred)) {
539 $cnt += $it->nrof;
540 }
541 my $resist_item_nr = 0;
542 $self->do_grep (sub { $resist_item_nr += ($_[0]->nrof || 1); 0 }, @$plingred);
543
544 my $maximprovname = (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS)
545 ? 'effect_resistances'
546 : 'attack_resistances';
547
548 my $maxres = Jeweler::getcfg (maximprovements => $maximprovname);
549 $resist_item_nr = $maxres if ($resist_item_nr > $maxres);
550 $ring->{hash}->{resist}->{$resnum} =
551 improve_max $ring->{hash}->{resist}->{$resnum}, $resist_item_nr;
496 } 552 }
497 553
498 return $ring; 554 return $ring;
499} 555}
500 556
501sub do_grep { 557sub do_grep {
502 my ($self, $cb, @grepar) = @_; 558 my ($self, $cb, $cat, @grepar) = @_;
503 559
504 my $ingred = $self->{ingredients}; 560 my $ingred = $self->{ingredients};
505 561
506
507 for my $cat (keys %$ingred) {
508 my @rem; 562 my @rem;
509 for my $ing (@{$ingred->{$cat}}) { 563 for my $ing (@{$ingred->{$cat}}) {
510 if (Jeweler::Util::grep_for_match ($ing, @grepar)) { 564 if (Jeweler::Util::check_for_match ($ing, @grepar)) {
511 unless ($cb->($ing)) { 565 unless ($cb->($ing)) {
512 push @rem, $ing;
513 }
514 } else {
515 push @rem, $ing; 566 push @rem, $ing;
516 } 567 }
568 } else {
569 push @rem, $ing;
517 } 570 }
571 }
518 @{$ingred->{$cat}} = @rem; 572 @{$ingred->{$cat}} = @rem;
519 }
520} 573}
521 574
522sub check_costs { 575sub check_costs {
523 my ($self, $costs, $do_remove) = @_; 576 my ($self, $costs, $do_remove) = @_;
524 577
525 my $costs = dclone ($costs); 578 my $costs = do { my $guard = Coro::Storable::guard; dclone $costs };
526 579
527 for my $key (keys %$costs) { 580 for my $key (keys %$costs) {
528 my @grepar; 581 my @grepar;
529 if ($key =~ m/^stat_(\S+)$/) { 582 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items
530 @grepar = @{Jeweler::getcfg (plans => $key) || []}; 583 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} };
531 } else { 584 next if $@;
585 } else { # check the gems
532 @grepar = (undef, undef, $key); 586 @grepar = ('gems', undef, undef, $key);
533 } 587 }
534 588
535 if ($do_remove) { 589 if ($do_remove) {
536 my $rem = $costs->{$key}; 590 my $rem = $costs->{$key};
537 $self->do_grep (sub { if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); } 1 }, @grepar); 591 $self->do_grep (sub { if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); } 1 }, @grepar);
538 if ($rem > 0) { 592 if ($rem > 0) {
539 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 593 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!";
540 } 594 }
541 } else { 595 } else {
542 my $nr; 596 my $nr;
543 $self->do_grep (sub { $nr += $_[0]->nrof; 0 }, @grepar); 597 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
544 $costs->{$key} -= $nr; 598 $costs->{$key} -= $nr;
545 } 599 }
600
546 } 601 }
547 602
548 return $costs; 603 return $costs;
549} 604}
550 605
577 $self->ring_or_ammy_to_hash ($arg{object}); 632 $self->ring_or_ammy_to_hash ($arg{object});
578 633
579 $self; 634 $self;
580} 635}
581 636
637sub has_resist {
638 my ($self, $resistnam, $resistval) = @_;
639 my $resnum = $REV_RESMAP{uc $resistnam};
640 if (defined ($resistval)) {
641 return 1 if $self->{hash}->{resist}->{$resnum} == $resistval;
642 } else {
643 return 1 if $self->{hash}->{resist}->{$resnum};
644 }
645 return undef;
646}
647
648sub projected_exp {
649 my ($self, $input_level) = @_;
650
651 my $lvl = max ($self->power_to_level, 1);
652 my $exp =
653 (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1))
654 / (10 + max ($lvl - 1, 0)); # 10 + level times making such a ring
655 # should get you to the rings level at least.
656
657 if (defined $input_level) {
658 my $subexp =
659 (cf::level_to_min_exp ($input_level)
660 - cf::level_to_min_exp ($input_level - 1))
661 / (10 + max ($input_level - 1, 0)); # see above for comment
662
663 $exp -= $subexp;
664 $exp = max ($exp, 0);
665
666 } else {
667 # the experience bonus here is to make level 1 rings give you at least
668 # 100 exp points when making them. This also makes leveling in the
669 # first few levels a bit easier. (probably until around level 5-6).
670 my $expbonus = cf::level_to_min_exp (2) / 10;
671 # this bonus should also only be given for _new_ rings and not for merged
672 # ones - to prevent infinite exp making.
673 $exp += $expbonus;
674 }
675
676 $exp
677}
678
582sub analyze { 679sub analyze {
583 my ($self, $sk, $pl) = @_; 680 my ($self, $sk, $pl, $input_level) = @_;
681 my $costs = $self->calc_costs;
682
683 unless (defined $costs) {
684 return "This ring has a resistancy above 99%, you can't make that.";
685 }
584 686
585 my $sklvl = cf::exp_to_level ($sk->stats->exp); 687 my $sklvl = cf::exp_to_level ($sk->stats->exp);
586 my $ringlvl = $self->power_to_level; 688 my $ringlvl = $self->power_to_level;
587 689
588 my $tmpl; 690 my $tmpl;
589 if ($pl->flag (cf::FLAG_WIZ)) { 691 if ($pl->flag (cf::FLAG_WIZ)) {
590 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl); 692 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl);
591 } else { 693 } else {
592 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl); 694 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl);
593 } 695 }
594 my $msg = sprintf "Projected success rate: %s", $tmpl; 696 my $msg = sprintf "Projected success rate: %s, you would get %d exp for this.", $tmpl, $self->projected_exp ($input_level);
595 return $msg; 697 return $msg;
698}
699
700sub calc_value_from_cost {
701 my ($self, $costs) = @_;
702 my $emarch = cf::arch::find 'emerald';
703 my $saarch = cf::arch::find 'sapphire';
704 my $pearch = cf::arch::find 'pearl';
705 my $ruarch = cf::arch::find 'ruby';
706 my $diarch = cf::arch::find 'gem';
707 my $value = $emarch->value * $costs->{emerald}
708 + $saarch->value * $costs->{sapphire}
709 + $pearch->value * $costs->{pearl}
710 + $ruarch->value * $costs->{ruby}
711 + $diarch->value * $costs->{gem};
712
713 $value
596} 714}
597 715
598sub wiz_analyze { 716sub wiz_analyze {
599 my ($self, $pl) = @_; 717 my ($self, $pl) = @_;
600 my $costs = $self->calc_costs; 718 my $costs = $self->calc_costs;
719 if (defined $costs) {
601 my $desc = ""; 720 my $desc = "";
602 my $lvl = $self->power_to_level (\$desc); 721 my $lvl = $self->power_to_level (\$desc);
722 my $scosts = $self->calc_value_from_cost ($costs);
723
724 $pl->message ("costs: "
603 $pl->message ("costs: " . join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)); 725 . (join ', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)
726 . " ("
727 . ($scosts / "royalty"->cf::arch::find->value)
728 . " royalties)");
604 $pl->message ("level: " . $desc); 729 $pl->message ("level: $desc");
730 } else {
731 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
732 }
605} 733}
606
607 734
608sub get_chance_perc { 735sub get_chance_perc {
609 my ($self, $sk) = @_; 736 my ($self, $sk) = @_;
610 my $sklvl = cf::exp_to_level ($sk->stats->exp); 737 my $sklvl = cf::exp_to_level ($sk->stats->exp);
611 my $ringlvl = $self->power_to_level; 738 my $ringlvl = $self->power_to_level;
614 741
615sub fx { 742sub fx {
616 my ($res, $cfg) = @_; 743 my ($res, $cfg) = @_;
617 my $or = $res; 744 my $or = $res;
618 my $ar = $Jeweler::CFG->{functions}->{$cfg}; 745 my $ar = $Jeweler::CFG->{functions}->{$cfg};
746
619 if (ref $ar->[0] eq 'ARRAY') { 747 if (ref $ar && ref $ar->[0] eq 'ARRAY') {
620 $res = $res - 1; 748 $res = $res - 1;
749 return $ar->[max (min ($res, @$ar - 1), 0)];
750
621 } else { 751 } else {
622 $res = ceil ($res / 5) - 1; 752 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3...
623 } 753 # old code:
754 #my $idx = ceil (($res / 5) + 0.1) - 1;
624 $ar->[max (min ($res, @$ar - 1), 0)]; 755 #my $a = $ar->[max (min ($idx, @$ar - 1), 0)];
756 #my $b = $ar->[max (min ($idx + 1, @$ar - 1), 0)];
757 #my $diff = $b - $a; # use the difference of the cost to the next cost
758 #my $o_cost = $a + ($diff / 5) * ($res % 5); # and do some linear interpolation
759 #return $o_cost;
760 return 0 if $res <= 0;
761 return ($ar / (1 - ($res * 0.01)) - $ar)
762 }
625} 763}
626 764
627sub improve_by_ring { 765sub improve_by_ring {
628 my ($self, @rings) = @_; 766 my ($self, @rings) = @_;
629 my $ring = $self; 767 my $ring = $self;
645 if ($self->{hash}->{$cat}->{$k} > 0) { 783 if ($self->{hash}->{$cat}->{$k} > 0) {
646 $self->{hash}->{$cat}->{$k} *= -1; 784 $self->{hash}->{$cat}->{$k} *= -1;
647 } 785 }
648 } 786 }
649 } 787 }
788 $self->{hash}{value} = 0;
650} 789}
651 790
652sub to_string { 791sub to_string {
653 my ($self) = @_; 792 my ($self) = @_;
654 my $r = $self->{hash}; 793 my $r = $self->{hash};
675 my ($self, $thing) = @_; 814 my ($self, $thing) = @_;
676 815
677 my $obj = {}; 816 my $obj = {};
678 817
679 for (@Jeweler::RESISTS) { 818 for (@Jeweler::RESISTS) {
680 $obj->{resist}->{$_} = $thing->resistance ($_); 819 $obj->{resist}->{$_} = $thing->resist ($_);
681 } 820 }
682 821
683 my $stats = $thing->stats; 822 my $stats = $thing->stats;
684 823
685 for (qw/Str Dex Con Wis Cha Int Pow/) { 824 for (qw/Str Dex Con Wis Cha Int Pow/) {
693 $obj->{spec}{ac} = $stats->ac; 832 $obj->{spec}{ac} = $stats->ac;
694 $obj->{spec}{speed} = $stats->exp; 833 $obj->{spec}{speed} = $stats->exp;
695 $obj->{spec}{food} = $stats->food; 834 $obj->{spec}{food} = $stats->food;
696 835
697 $obj->{name} = $thing->name; 836 $obj->{name} = $thing->name;
698 $obj->{arch} = $thing->arch->name; 837 $obj->{arch} = $thing->arch->archname;
699 $obj->{face} = $thing->face; 838 $obj->{face} = $thing->face;
839
840 $obj->{value} = $thing->value;
700 841
701 $self->{hash} = $obj 842 $self->{hash} = $obj
702} 843}
703 844
704sub to_object { 845sub to_object {
705 my ($self) = @_; 846 my ($self) = @_;
706 847
707 my $obj = cf::object::new $self->{hash}->{arch}; 848 my $obj = cf::object::new $self->{hash}->{arch};
849
850 $obj->item_power (floor ($self->power_to_level / 3)); # there have to be strings attached!
708 851
709 $obj->face ($self->{hash}{face}); 852 $obj->face ($self->{hash}{face});
710 853
711 my $stats = $obj->stats; 854 my $stats = $obj->stats;
712 855
720 863
721 $stats->$_ ($self->{hash}{stat}{lc $_}) 864 $stats->$_ ($self->{hash}{stat}{lc $_})
722 for qw/Str Dex Con Wis Cha Int Pow/; 865 for qw/Str Dex Con Wis Cha Int Pow/;
723 866
724 for (@Jeweler::RESISTS) { 867 for (@Jeweler::RESISTS) {
725 $obj->resistance ($_, $self->{hash}->{resist}->{$_}); 868 $obj->resist ($_, $self->{hash}->{resist}->{$_});
726 } 869 }
727 870
728 $obj->flag (cf::FLAG_IDENTIFIED, 1); 871 $obj->flag (cf::FLAG_IDENTIFIED, 1);
729 872
873 $obj->value ($self->{hash}{value});
874
730 return $obj; 875 return $obj;
876}
877
878sub set_value { $_[0]->{hash}{value} = $_[1] }
879
880sub is_better_than {
881 my ($self, $other) = @_;
882
883 for my $type (qw/spec stat resist/) {
884 for my $stat (keys %{$self->{hash}->{$type}}) {
885 if ($self->{hash}->{$type}->{$stat} > $other->{hash}->{$type}->{$stat}) {
886 return 1;
887 }
888 }
889 }
890
891 return 0;
731} 892}
732 893
733sub stat_level { 894sub stat_level {
734 my ($self) = @_; 895 my ($self) = @_;
735 my $stats = $self->{hash}->{stat} || {}; 896 my $stats = $self->{hash}->{stat} || {};
849 for my $stat (keys %$stats) { 1010 for my $stat (keys %$stats) {
850 my $sum = $stats->{$stat}; 1011 my $sum = $stats->{$stat};
851 1012
852 next unless $sum > 0; 1013 next unless $sum > 0;
853 1014
854 my $statfx = fx ($sum, 'stat_potions'); 1015 my $statfx = fx ($sum, 'stat_items');
855 $cost->{"stat_$stat"} += $statfx->[0]; 1016 $cost->{"stat_$stat"} += $statfx->[0];
856 split_diamonds ($cost, $statfx->[1], 'stat_' . $stat); 1017 split_diamonds ($cost, $statfx->[1], 'stat_' . $stat);
857 } 1018 }
858} 1019}
859 1020
865 for my $spec (keys %$specials) { 1026 for my $spec (keys %$specials) {
866 my $sum = $specials->{$spec}; 1027 my $sum = $specials->{$spec};
867 1028
868 next unless $sum > 0; 1029 next unless $sum > 0;
869 1030
870 my $specfx = fx ($sum, 'spec_potions'); 1031 my $specfx = fx ($sum, 'spec_items');
871 $cost->{"spec_$spec"} += $specfx->[0]; 1032 $cost->{"spec_$spec"} += $specfx->[0];
872 split_diamonds ($cost, $specfx->[1], 'spec_' . $spec); 1033 split_diamonds ($cost, $specfx->[1], 'spec_' . $spec);
873 } 1034 }
874} 1035}
875 1036
878 1039
879 my $costs = {}; 1040 my $costs = {};
880 1041
881 my $ring = $self->{hash}; 1042 my $ring = $self->{hash};
882 1043
883 for my $resnam (keys %{$ring->{resist} || {}}) { 1044 for my $resnum (keys %{$ring->{resist} || {}}) {
884 1045
885 my $res = $ring->{resist}->{$resnam}; 1046 my $res = $ring->{resist}->{$resnum};
886 1047
887 next unless $res > 0; 1048 next unless $res > 0;
888 1049
1050 return undef if $res == 100;
1051
889 $costs->{"resist_" . $Jeweler::RESMAP{$resnam} . "_item"} += $res; 1052 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res;
890 1053
891 my $diamonds; 1054 my $diamonds;
892 if (grep { $resnam eq $_ } @Jeweler::EFFECT_RESISTS) { 1055 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) {
893 $diamonds += fx ($res, 'effect_resist_diamonds'); 1056 $diamonds += fx ($res, 'effect_resist_diamonds_x');
894 } else { 1057 } else {
895 $diamonds += fx ($res, 'attack_resist_diamonds'); 1058 $diamonds += fx ($res, 'attack_resist_diamonds_x');
896 } 1059 }
897 1060
898 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnam}); 1061 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum});
899 } 1062 }
900 1063
901 $self->add_stat_costs ($costs); 1064 $self->add_stat_costs ($costs);
902 $self->add_special_costs ($costs); 1065 $self->add_special_costs ($costs);
903 1066
908 my ($cost, $diamonds, $category) = @_; 1071 my ($cost, $diamonds, $category) = @_;
909 1072
910 my $stat_split = Jeweler::getcfg (diamond_split => $category); 1073 my $stat_split = Jeweler::getcfg (diamond_split => $category);
911 1074
912 my $sum = sum (@$stat_split); 1075 my $sum = sum (@$stat_split);
913 if ($sum < (1 - 0.0001)) {
914 warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!";
915 }
916 1076
917 my $emarch = cf::arch::find 'emerald'; 1077 my $emarch = cf::arch::find 'emerald';
918 my $saarch = cf::arch::find 'sapphire'; 1078 my $saarch = cf::arch::find 'sapphire';
919 my $pearch = cf::arch::find 'pearl'; 1079 my $pearch = cf::arch::find 'pearl';
920 my $ruarch = cf::arch::find 'ruby'; 1080 my $ruarch = cf::arch::find 'ruby';
921 my $diarch = cf::arch::find 'gem'; 1081 my $diarch = cf::arch::find 'gem';
922 1082
923 my $sumvalue = $diarch->clone->value * $diamonds; 1083 my $sumvalue = $diarch->value * $diamonds;
924 1084
925 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->clone->value; 1085 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->value;
926 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->clone->value; 1086 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->value;
927 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->clone->value; 1087 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->value;
928 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->clone->value; 1088 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->value;
929 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->clone->value; 1089 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value;
930} 1090}
931 1091
932package Jeweler::Util; 1092package Jeweler::Util;
933 1093
934use strict; 1094use strict;
950 my ($obj, $nrof) = @_; 1110 my ($obj, $nrof) = @_;
951 1111
952 my $cnt; 1112 my $cnt;
953 1113
954 if (defined $nrof) { 1114 if (defined $nrof) {
1115 # TODO: Check tihis line:
955 return 0 if ($nrof * 1) == 0; 1116 return 0 if ($nrof * 1) == 0; #XXX: ???
956 $cnt = int (($obj->nrof || 1) - (1 * $nrof)); 1117 $cnt = int (($obj->nrof || 1) - (1 * $nrof));
957 1118
958 if ($cnt > 0) { 1119 if ($cnt > 0) {
959 $obj->nrof ($cnt); 1120 $obj->nrof ($cnt);
960 return 0; 1121 return 0;
964 remove ($_) for $obj->inv; 1125 remove ($_) for $obj->inv;
965 $obj->destroy; 1126 $obj->destroy;
966 return $cnt; 1127 return $cnt;
967} 1128}
968 1129
969sub grep_for_match { 1130sub check_for_match {
970 my ($thing, @matchar) = @_; 1131 my ($thing, @matchar) = @_;
971 1132
972 my $i = 0; 1133 my $i = 0;
1134 my $check_cnts = 0;
1135 my $check_true = 0;
973 for my $match (@matchar) { 1136 for my $match (@matchar) {
1137 if ($i % 3 == 0) {
1138 return 1 if $check_true && $check_cnts == $check_true;
1139 $check_cnts = 0;
1140 $check_true = 0;
1141 }
1142
974 if ($match =~ m/^\s*$/) { 1143 if ($match =~ m/^\s*$/) {
975 $i++; 1144 $i++;
976 next; 1145 next;
977 } 1146 }
978 1147
1148 $check_cnts++;
979 if ($i % 3 == 0) { 1149 if ($i % 3 == 0) {
980 $thing->name eq $match 1150 $thing->name eq $match
981 and return 1; 1151 and $check_true++;
982 } elsif ($i % 3 == 1) { 1152 } elsif ($i % 3 == 1) {
983 $thing->title eq $match 1153 $thing->title eq $match
984 and return 1; 1154 and $check_true++;
985 } else { # $i % 3 == 2 1155 } else { # $i % 3 == 2
986 $thing->arch->name eq $match 1156 $thing->arch->archname eq $match
987 and return 1; 1157 and $check_true++;
988 } 1158 }
989 $i++; 1159 $i++;
990 } 1160 }
1161 #d# warn "CHECK $check_true | $check_cnts | [".(join ',', @matchar).":".join (",", ($thing->name, $thing->title, $thing->arch->archname))."\n";
1162 return 1 if $check_true && $check_cnts == $check_true;
991 return 0; 1163 return 0;
992} 1164}
993 1165
1166sub grep_for_match {
1167 my ($ingred, $group, @matchar) = @_;
1168
1169 for my $thing (@{$ingred->{$group} || []}) {
1170 #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->archname, "@matchar"; #d#
1171 if (check_for_match ($thing, @matchar)) {
1172 return $thing;
1173 }
1174 }
1175 return undef;
1176}
1177
994=back 1178=back
995 1179
996=back
997
9981 11801

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines