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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines