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.24 by elmex, Sun Feb 17 22:37:34 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
603 $pl->message ("costs: " . join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)); 724 $pl->message ("costs: " . (join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)) . " (".($scosts / 5000)." royalties)");
604 $pl->message ("level: " . $desc); 725 $pl->message ("level: " . $desc);
726 } else {
727 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
728 }
605} 729}
606
607 730
608sub get_chance_perc { 731sub get_chance_perc {
609 my ($self, $sk) = @_; 732 my ($self, $sk) = @_;
610 my $sklvl = cf::exp_to_level ($sk->stats->exp); 733 my $sklvl = cf::exp_to_level ($sk->stats->exp);
611 my $ringlvl = $self->power_to_level; 734 my $ringlvl = $self->power_to_level;
614 737
615sub fx { 738sub fx {
616 my ($res, $cfg) = @_; 739 my ($res, $cfg) = @_;
617 my $or = $res; 740 my $or = $res;
618 my $ar = $Jeweler::CFG->{functions}->{$cfg}; 741 my $ar = $Jeweler::CFG->{functions}->{$cfg};
742
619 if (ref $ar->[0] eq 'ARRAY') { 743 if (ref $ar && ref $ar->[0] eq 'ARRAY') {
620 $res = $res - 1; 744 $res = $res - 1;
745 return $ar->[max (min ($res, @$ar - 1), 0)];
746
621 } else { 747 } else {
622 $res = ceil ($res / 5) - 1; 748 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3...
623 } 749 # old code:
750 #my $idx = ceil (($res / 5) + 0.1) - 1;
624 $ar->[max (min ($res, @$ar - 1), 0)]; 751 #my $a = $ar->[max (min ($idx, @$ar - 1), 0)];
752 #my $b = $ar->[max (min ($idx + 1, @$ar - 1), 0)];
753 #my $diff = $b - $a; # use the difference of the cost to the next cost
754 #my $o_cost = $a + ($diff / 5) * ($res % 5); # and do some linear interpolation
755 #return $o_cost;
756 return 0 if $res <= 0;
757 return ($ar / (1 - ($res * 0.01)) - $ar)
758 }
625} 759}
626 760
627sub improve_by_ring { 761sub improve_by_ring {
628 my ($self, @rings) = @_; 762 my ($self, @rings) = @_;
629 my $ring = $self; 763 my $ring = $self;
645 if ($self->{hash}->{$cat}->{$k} > 0) { 779 if ($self->{hash}->{$cat}->{$k} > 0) {
646 $self->{hash}->{$cat}->{$k} *= -1; 780 $self->{hash}->{$cat}->{$k} *= -1;
647 } 781 }
648 } 782 }
649 } 783 }
784 $self->{hash}{value} = 0;
650} 785}
651 786
652sub to_string { 787sub to_string {
653 my ($self) = @_; 788 my ($self) = @_;
654 my $r = $self->{hash}; 789 my $r = $self->{hash};
675 my ($self, $thing) = @_; 810 my ($self, $thing) = @_;
676 811
677 my $obj = {}; 812 my $obj = {};
678 813
679 for (@Jeweler::RESISTS) { 814 for (@Jeweler::RESISTS) {
680 $obj->{resist}->{$_} = $thing->resistance ($_); 815 $obj->{resist}->{$_} = $thing->resist ($_);
681 } 816 }
682 817
683 my $stats = $thing->stats; 818 my $stats = $thing->stats;
684 819
685 for (qw/Str Dex Con Wis Cha Int Pow/) { 820 for (qw/Str Dex Con Wis Cha Int Pow/) {
693 $obj->{spec}{ac} = $stats->ac; 828 $obj->{spec}{ac} = $stats->ac;
694 $obj->{spec}{speed} = $stats->exp; 829 $obj->{spec}{speed} = $stats->exp;
695 $obj->{spec}{food} = $stats->food; 830 $obj->{spec}{food} = $stats->food;
696 831
697 $obj->{name} = $thing->name; 832 $obj->{name} = $thing->name;
698 $obj->{arch} = $thing->arch->name; 833 $obj->{arch} = $thing->arch->archname;
699 $obj->{face} = $thing->face; 834 $obj->{face} = $thing->face;
835
836 $obj->{value} = $thing->value;
700 837
701 $self->{hash} = $obj 838 $self->{hash} = $obj
702} 839}
703 840
704sub to_object { 841sub to_object {
705 my ($self) = @_; 842 my ($self) = @_;
706 843
707 my $obj = cf::object::new $self->{hash}->{arch}; 844 my $obj = cf::object::new $self->{hash}->{arch};
845
846 $obj->item_power (floor ($self->power_to_level / 3)); # there have to be strings attached!
708 847
709 $obj->face ($self->{hash}{face}); 848 $obj->face ($self->{hash}{face});
710 849
711 my $stats = $obj->stats; 850 my $stats = $obj->stats;
712 851
720 859
721 $stats->$_ ($self->{hash}{stat}{lc $_}) 860 $stats->$_ ($self->{hash}{stat}{lc $_})
722 for qw/Str Dex Con Wis Cha Int Pow/; 861 for qw/Str Dex Con Wis Cha Int Pow/;
723 862
724 for (@Jeweler::RESISTS) { 863 for (@Jeweler::RESISTS) {
725 $obj->resistance ($_, $self->{hash}->{resist}->{$_}); 864 $obj->resist ($_, $self->{hash}->{resist}->{$_});
726 } 865 }
727 866
728 $obj->flag (cf::FLAG_IDENTIFIED, 1); 867 $obj->flag (cf::FLAG_IDENTIFIED, 1);
729 868
869 $obj->value ($self->{hash}{value});
870
730 return $obj; 871 return $obj;
872}
873
874sub set_value { $_[0]->{hash}{value} = $_[1] }
875
876sub is_better_than {
877 my ($self, $other) = @_;
878
879 for my $type (qw/spec stat resist/) {
880 for my $stat (keys %{$self->{hash}->{$type}}) {
881 if ($self->{hash}->{$type}->{$stat} > $other->{hash}->{$type}->{$stat}) {
882 return 1;
883 }
884 }
885 }
886
887 return 0;
731} 888}
732 889
733sub stat_level { 890sub stat_level {
734 my ($self) = @_; 891 my ($self) = @_;
735 my $stats = $self->{hash}->{stat} || {}; 892 my $stats = $self->{hash}->{stat} || {};
849 for my $stat (keys %$stats) { 1006 for my $stat (keys %$stats) {
850 my $sum = $stats->{$stat}; 1007 my $sum = $stats->{$stat};
851 1008
852 next unless $sum > 0; 1009 next unless $sum > 0;
853 1010
854 my $statfx = fx ($sum, 'stat_potions'); 1011 my $statfx = fx ($sum, 'stat_items');
855 $cost->{"stat_$stat"} += $statfx->[0]; 1012 $cost->{"stat_$stat"} += $statfx->[0];
856 split_diamonds ($cost, $statfx->[1], 'stat_' . $stat); 1013 split_diamonds ($cost, $statfx->[1], 'stat_' . $stat);
857 } 1014 }
858} 1015}
859 1016
865 for my $spec (keys %$specials) { 1022 for my $spec (keys %$specials) {
866 my $sum = $specials->{$spec}; 1023 my $sum = $specials->{$spec};
867 1024
868 next unless $sum > 0; 1025 next unless $sum > 0;
869 1026
870 my $specfx = fx ($sum, 'spec_potions'); 1027 my $specfx = fx ($sum, 'spec_items');
871 $cost->{"spec_$spec"} += $specfx->[0]; 1028 $cost->{"spec_$spec"} += $specfx->[0];
872 split_diamonds ($cost, $specfx->[1], 'spec_' . $spec); 1029 split_diamonds ($cost, $specfx->[1], 'spec_' . $spec);
873 } 1030 }
874} 1031}
875 1032
878 1035
879 my $costs = {}; 1036 my $costs = {};
880 1037
881 my $ring = $self->{hash}; 1038 my $ring = $self->{hash};
882 1039
883 for my $resnam (keys %{$ring->{resist} || {}}) { 1040 for my $resnum (keys %{$ring->{resist} || {}}) {
884 1041
885 my $res = $ring->{resist}->{$resnam}; 1042 my $res = $ring->{resist}->{$resnum};
886 1043
887 next unless $res > 0; 1044 next unless $res > 0;
888 1045
1046 return undef if $res == 100;
1047
889 $costs->{"resist_" . $Jeweler::RESMAP{$resnam} . "_item"} += $res; 1048 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res;
890 1049
891 my $diamonds; 1050 my $diamonds;
892 if (grep { $resnam eq $_ } @Jeweler::EFFECT_RESISTS) { 1051 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) {
893 $diamonds += fx ($res, 'effect_resist_diamonds'); 1052 $diamonds += fx ($res, 'effect_resist_diamonds_x');
894 } else { 1053 } else {
895 $diamonds += fx ($res, 'attack_resist_diamonds'); 1054 $diamonds += fx ($res, 'attack_resist_diamonds_x');
896 } 1055 }
897 1056
898 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnam}); 1057 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum});
899 } 1058 }
900 1059
901 $self->add_stat_costs ($costs); 1060 $self->add_stat_costs ($costs);
902 $self->add_special_costs ($costs); 1061 $self->add_special_costs ($costs);
903 1062
908 my ($cost, $diamonds, $category) = @_; 1067 my ($cost, $diamonds, $category) = @_;
909 1068
910 my $stat_split = Jeweler::getcfg (diamond_split => $category); 1069 my $stat_split = Jeweler::getcfg (diamond_split => $category);
911 1070
912 my $sum = sum (@$stat_split); 1071 my $sum = sum (@$stat_split);
913 if ($sum < (1 - 0.0001)) {
914 warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!";
915 }
916 1072
917 my $emarch = cf::arch::find 'emerald'; 1073 my $emarch = cf::arch::find 'emerald';
918 my $saarch = cf::arch::find 'sapphire'; 1074 my $saarch = cf::arch::find 'sapphire';
919 my $pearch = cf::arch::find 'pearl'; 1075 my $pearch = cf::arch::find 'pearl';
920 my $ruarch = cf::arch::find 'ruby'; 1076 my $ruarch = cf::arch::find 'ruby';
921 my $diarch = cf::arch::find 'gem'; 1077 my $diarch = cf::arch::find 'gem';
922 1078
923 my $sumvalue = $diarch->clone->value * $diamonds; 1079 my $sumvalue = $diarch->value * $diamonds;
924 1080
925 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->clone->value; 1081 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->value;
926 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->clone->value; 1082 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->value;
927 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->clone->value; 1083 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->value;
928 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->clone->value; 1084 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->value;
929 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->clone->value; 1085 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value;
930} 1086}
931 1087
932package Jeweler::Util; 1088package Jeweler::Util;
933 1089
934use strict; 1090use strict;
950 my ($obj, $nrof) = @_; 1106 my ($obj, $nrof) = @_;
951 1107
952 my $cnt; 1108 my $cnt;
953 1109
954 if (defined $nrof) { 1110 if (defined $nrof) {
1111 # TODO: Check tihis line:
955 return 0 if ($nrof * 1) == 0; 1112 return 0 if ($nrof * 1) == 0; #XXX: ???
956 $cnt = int (($obj->nrof || 1) - (1 * $nrof)); 1113 $cnt = int (($obj->nrof || 1) - (1 * $nrof));
957 1114
958 if ($cnt > 0) { 1115 if ($cnt > 0) {
959 $obj->nrof ($cnt); 1116 $obj->nrof ($cnt);
960 return 0; 1117 return 0;
964 remove ($_) for $obj->inv; 1121 remove ($_) for $obj->inv;
965 $obj->destroy; 1122 $obj->destroy;
966 return $cnt; 1123 return $cnt;
967} 1124}
968 1125
969sub grep_for_match { 1126sub check_for_match {
970 my ($thing, @matchar) = @_; 1127 my ($thing, @matchar) = @_;
971 1128
972 my $i = 0; 1129 my $i = 0;
1130 my $check_cnts = 0;
1131 my $check_true = 0;
973 for my $match (@matchar) { 1132 for my $match (@matchar) {
1133 if ($i % 3 == 0) {
1134 return 1 if $check_true && $check_cnts == $check_true;
1135 $check_cnts = 0;
1136 $check_true = 0;
1137 }
1138
974 if ($match =~ m/^\s*$/) { 1139 if ($match =~ m/^\s*$/) {
975 $i++; 1140 $i++;
976 next; 1141 next;
977 } 1142 }
978 1143
1144 $check_cnts++;
979 if ($i % 3 == 0) { 1145 if ($i % 3 == 0) {
980 $thing->name eq $match 1146 $thing->name eq $match
981 and return 1; 1147 and $check_true++;
982 } elsif ($i % 3 == 1) { 1148 } elsif ($i % 3 == 1) {
983 $thing->title eq $match 1149 $thing->title eq $match
984 and return 1; 1150 and $check_true++;
985 } else { # $i % 3 == 2 1151 } else { # $i % 3 == 2
986 $thing->arch->name eq $match 1152 $thing->arch->archname eq $match
987 and return 1; 1153 and $check_true++;
988 } 1154 }
989 $i++; 1155 $i++;
990 } 1156 }
1157 #d# warn "CHECK $check_true | $check_cnts | [".(join ',', @matchar).":".join (",", ($thing->name, $thing->title, $thing->arch->archname))."\n";
1158 return 1 if $check_true && $check_cnts == $check_true;
991 return 0; 1159 return 0;
992} 1160}
993 1161
1162sub grep_for_match {
1163 my ($ingred, $group, @matchar) = @_;
1164
1165 for my $thing (@{$ingred->{$group} || []}) {
1166 #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->archname, "@matchar"; #d#
1167 if (check_for_match ($thing, @matchar)) {
1168 return $thing;
1169 }
1170 }
1171 return undef;
1172}
1173
994=back 1174=back
995 1175
996=back
997
9981 11761

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines