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.3 by root, Sat Dec 23 15:49:40 2006 UTC vs.
Revision 1.21 by root, Fri Oct 12 19:13:26 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines