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.2 by elmex, Tue Dec 19 15:43:22 2006 UTC vs.
Revision 1.33 by root, Wed Apr 28 21:05:33 2010 UTC

8 8
9=cut 9=cut
10 10
11package Jeweler; 11package Jeweler;
12 12
13use strict; 13use common::sense;
14use YAML;
15 14
16=over 4 15=over 4
17 16
18=item @RESISTS 17=item @RESISTS
19 18
21 20
22=cut 21=cut
23 22
24our $CFG; 23our $CFG;
25 24
26sub read_config { 25sub load_config {
27 my ($filename) = @_; 26 0 < Coro::AIO::aio_load "$cf::DATADIR/jeweler", my $data
27 or die "$cf::DATADIR/jeweler: $!";
28 28
29 unless (-e $filename) { 29 $CFG = cf::decode_json $data;
30 warn "$filename doesn't exists! no config for jeweler skill loaded!\n";
31 $CFG = {};
32 return
33 }
34
35 $CFG = YAML::LoadFile $filename;
36} 30}
37 31
38sub getcfg { 32sub getcfg {
39 my ($sect, $key) = @_; 33 my ($sect, $key) = @_;
40 return $CFG->{$sect} unless defined $key; 34 return $CFG->{$sect} unless defined $key;
117 cf::ATNR_LIFE_STEALING => "LIFE_STEALING", 111 cf::ATNR_LIFE_STEALING => "LIFE_STEALING",
118 cf::ATNR_BLIND => "BLIND", 112 cf::ATNR_BLIND => "BLIND",
119 cf::ATNR_DISEASE => "DISEASE", 113 cf::ATNR_DISEASE => "DISEASE",
120); 114);
121 115
116our %REV_RESMAP = map { $RESMAP{$_} => $_ } keys %RESMAP;
117
122our %LVL_DIFF_CHANCES = ( 118our %LVL_DIFF_CHANCES = (
123 +5 => 100, 119 +5 => 100,
124 +4 => 95, 120 +4 => 95,
125 +3 => 85, 121 +3 => 85,
126 +2 => 75, 122 +2 => 75,
160 $delta = 5 if $delta > 5; 156 $delta = 5 if $delta > 5;
161 return $LVL_DIFF_CHANCES{$delta} 157 return $LVL_DIFF_CHANCES{$delta}
162} 158}
163 159
164sub analyze { 160sub analyze {
165 my ($sk, $chdl, $pl) = @_; 161 my ($sk, $chdl, $pl, $input_level) = @_;
166 162
167 my $hadunid = 0; 163 my $hadunid = 0;
164 my $found = 0;
168 for ($chdl->grep_by_type (cf::RING, cf::AMULET)) { 165 for ($chdl->grep_by_type (cf::RING, cf::AMULET)) {
169 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) { 166 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) {
170 $hadunid = 1; 167 $hadunid = 1;
171 next; 168 next;
172 } 169 }
170 $found = 1;
173 my $r = Jeweler::Object->new (object => $_); 171 my $r = Jeweler::Object->new (object => $_);
174 my $msg = $r->analyze ($sk, $pl); 172 my $msg = $r->analyze ($sk, $pl, $input_level);
175 $pl->message ($r->to_string . ": " . $msg); 173 $pl->message ($r->to_string . ": " . $msg);
176 if ($pl->flag (cf::FLAG_WIZ)) { 174 if ($pl->flag (cf::FLAG_WIZ)) {
177 $r->wiz_analyze ($pl); 175 $r->wiz_analyze ($pl);
178 } 176 }
179 } 177 }
180 if ($hadunid) {
181 $pl->message ("You couldn't identify the other rings and not analyze them!"); 178 $pl->message ("You couldn't identify the other rings and not analyze them!")
182 } 179 if $hadunid;
180 $pl->message ("You couldn't find anything in the bench to analyse!")
181 unless $found;
183} 182}
184 183
185# this function converts metals/minerals into a raw ring (of adornment) 184# this function converts metals/minerals into a raw ring (of adornment)
186sub simple_converter { 185sub simple_converter {
187 my ($pl, $ingred, $chdl, $conv) = @_; 186 my ($pl, $ingred, $chdl, $conv, $sk_lvl, $low_skill_cb) = @_;
188 187
189 $conv = lc $conv; 188 $conv = lc $conv;
190 my $cnvs = $CFG->{conversions}; 189 my $cnvs = $CFG->{conversions};
191 190
192 return unless $cnvs->{$conv}; 191 return unless $cnvs->{$conv};
201 warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!"; 200 warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!";
202 return; 201 return;
203 } 202 }
204 203
205 unless ($xp_gain > 0) { 204 unless ($xp_gain > 0) {
206 warn "WARNING: xp gain isn't > 0 in convesion '$outarch'\n"; 205 warn "WARNING: xp gain isn't > 0 in conversion '$outarch'\n";
207 return; 206 return;
208 } 207 }
209 208
210 unless ($outarchvalfact) { 209 unless ($outarchvalfact) {
211 warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n"; 210 warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n";
214 213
215 unless ($outarchvalfact >= 1) { 214 unless ($outarchvalfact >= 1) {
216 warn "WARNING: source-arch-value-multiplier < 1 in conversion '$outarch', results in more valuable output!\n"; 215 warn "WARNING: source-arch-value-multiplier < 1 in conversion '$outarch', results in more valuable output!\n";
217 } 216 }
218 217
219 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname); 218 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname);
220 $ingred->remove ($ingr_grp, $srcarchname);
221
222 my $outarchval = cf::arch::find ($outarch)->clone->value; 219 my $outarchval = cf::arch::find ($outarch)->value;
223
224 my $nrof = int ($archvalsum / (($outarchval || 1000) * $outarchvalfact)); 220 my $nrof = int $archvalsum / (($outarchval || 1000) * $outarchvalfact);
221 my $can_make_nr = int (($sk_lvl / 2) + 10);
222
223 if ($nrof > $can_make_nr) {
224 $pl->ob->message ("Your jeweler level is too low to make $nrof rings, you can only make $can_make_nr at your current level.");
225 return;
226 }
227
225 if ($nrof) { 228 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) 229 # XXX: yes, I know what I'm doing here, I don't set nrof, but it didn't work somehow (pls. check sometimes)
230 $ingred->remove ($ingr_grp, $srcarchname);
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 = 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 = 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};
591 $self->do_grep (sub {
537 $self->do_grep (sub { if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); } 1 }, @grepar); 592 if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); }
593 1
594 }, @grepar);
538 if ($rem > 0) { 595 if ($rem > 0) {
539 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 596 warn "JEWELER BUG: removed ingredients ($key) $rem > 0 after removing!";
540 } 597 }
598
541 } else { 599 } else {
542 my $nr; 600 my $nr;
543 $self->do_grep (sub { $nr += $_[0]->nrof; 0 }, @grepar); 601 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
544 $costs->{$key} -= $nr; 602 $costs->{$key} -= $nr;
545 } 603 }
604
546 } 605 }
547 606
548 return $costs; 607 return $costs;
549} 608}
550 609
577 $self->ring_or_ammy_to_hash ($arg{object}); 636 $self->ring_or_ammy_to_hash ($arg{object});
578 637
579 $self; 638 $self;
580} 639}
581 640
641sub has_resist {
642 my ($self, $resistnam, $resistval) = @_;
643 my $resnum = $REV_RESMAP{uc $resistnam};
644 if (defined ($resistval)) {
645 return 1 if $self->{hash}->{resist}->{$resnum} == $resistval;
646 } else {
647 return 1 if $self->{hash}->{resist}->{$resnum};
648 }
649 return undef;
650}
651
652sub projected_exp {
653 my ($self, $input_level) = @_;
654
655 my $lvl = max ($self->power_to_level, 1);
656 my $exp =
657 (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1))
658 / (10 + max ($lvl - 1, 0)); # 10 + level times making such a ring
659 # should get you to the rings level at least.
660
661 if (defined $input_level) {
662 my $subexp =
663 (cf::level_to_min_exp ($input_level)
664 - cf::level_to_min_exp ($input_level - 1))
665 / (10 + max ($input_level - 1, 0)); # see above for comment
666
667 $exp -= $subexp;
668 $exp = max ($exp, 0);
669
670 } else {
671 # the experience bonus here is to make level 1 rings give you at least
672 # 200 exp points when making them. This also makes leveling in the
673 # first few levels a bit easier. (probably until around level 5-6).
674 my $expbonus = cf::level_to_min_exp (2) / 5;
675 # this bonus should also only be given for _new_ rings and not for merged
676 # ones - to prevent infinite exp making.
677 $exp += $expbonus;
678 }
679
680 $exp
681}
682
582sub analyze { 683sub analyze {
583 my ($self, $sk, $pl) = @_; 684 my ($self, $sk, $pl, $input_level) = @_;
685 my $costs = $self->calc_costs;
686
687 unless (defined $costs) {
688 return "This ring has a resistancy above 99%, you can't make that.";
689 }
584 690
585 my $sklvl = cf::exp_to_level ($sk->stats->exp); 691 my $sklvl = cf::exp_to_level ($sk->stats->exp);
586 my $ringlvl = $self->power_to_level; 692 my $ringlvl = $self->power_to_level;
587 693
588 my $tmpl; 694 my $tmpl;
589 if ($pl->flag (cf::FLAG_WIZ)) { 695 if ($pl->flag (cf::FLAG_WIZ)) {
590 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl); 696 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl);
591 } else { 697 } else {
592 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl); 698 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl);
593 } 699 }
594 my $msg = sprintf "Projected success rate: %s", $tmpl; 700 my $msg = sprintf "Projected success rate: %s, you would get %d exp for this.", $tmpl, $self->projected_exp ($input_level);
595 return $msg; 701 return $msg;
702}
703
704sub calc_value_from_cost {
705 my ($self, $costs) = @_;
706 my $emarch = cf::arch::find 'emerald';
707 my $saarch = cf::arch::find 'sapphire';
708 my $pearch = cf::arch::find 'pearl';
709 my $ruarch = cf::arch::find 'ruby';
710 my $diarch = cf::arch::find 'gem';
711 my $value = $emarch->value * $costs->{emerald}
712 + $saarch->value * $costs->{sapphire}
713 + $pearch->value * $costs->{pearl}
714 + $ruarch->value * $costs->{ruby}
715 + $diarch->value * $costs->{gem};
716
717 $value
596} 718}
597 719
598sub wiz_analyze { 720sub wiz_analyze {
599 my ($self, $pl) = @_; 721 my ($self, $pl) = @_;
600 my $costs = $self->calc_costs; 722 my $costs = $self->calc_costs;
723 if (defined $costs) {
601 my $desc = ""; 724 my $desc = "";
602 my $lvl = $self->power_to_level (\$desc); 725 my $lvl = $self->power_to_level (\$desc);
726 my $scosts = $self->calc_value_from_cost ($costs);
727
728 $pl->message ("costs: "
603 $pl->message ("costs: " . join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)); 729 . (join ', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)
730 . " ("
731 . ($scosts / "platinacoin"->cf::arch::find->value)
732 . " platinum)");
604 $pl->message ("level: " . $desc); 733 $pl->message ("level: $desc");
734 } else {
735 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
736 }
605} 737}
606
607 738
608sub get_chance_perc { 739sub get_chance_perc {
609 my ($self, $sk) = @_; 740 my ($self, $sk) = @_;
610 my $sklvl = cf::exp_to_level ($sk->stats->exp); 741 my $sklvl = cf::exp_to_level ($sk->stats->exp);
611 my $ringlvl = $self->power_to_level; 742 my $ringlvl = $self->power_to_level;
614 745
615sub fx { 746sub fx {
616 my ($res, $cfg) = @_; 747 my ($res, $cfg) = @_;
617 my $or = $res; 748 my $or = $res;
618 my $ar = $Jeweler::CFG->{functions}->{$cfg}; 749 my $ar = $Jeweler::CFG->{functions}->{$cfg};
750
619 if (ref $ar->[0] eq 'ARRAY') { 751 if (ref $ar && ref $ar->[0] eq 'ARRAY') {
620 $res = $res - 1; 752 $res = $res - 1;
753 return $ar->[max (min ($res, @$ar - 1), 0)];
754
621 } else { 755 } else {
622 $res = ceil ($res / 5) - 1; 756 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3...
623 } 757 # old code:
758 #my $idx = ceil (($res / 5) + 0.1) - 1;
624 $ar->[max (min ($res, @$ar - 1), 0)]; 759 #my $a = $ar->[max (min ($idx, @$ar - 1), 0)];
760 #my $b = $ar->[max (min ($idx + 1, @$ar - 1), 0)];
761 #my $diff = $b - $a; # use the difference of the cost to the next cost
762 #my $o_cost = $a + ($diff / 5) * ($res % 5); # and do some linear interpolation
763 #return $o_cost;
764 return 0 if $res <= 0;
765 return ($ar / (1 - ($res * 0.01)) - $ar)
766 }
625} 767}
626 768
627sub improve_by_ring { 769sub improve_by_ring {
628 my ($self, @rings) = @_; 770 my ($self, @rings) = @_;
629 my $ring = $self; 771 my $ring = $self;
645 if ($self->{hash}->{$cat}->{$k} > 0) { 787 if ($self->{hash}->{$cat}->{$k} > 0) {
646 $self->{hash}->{$cat}->{$k} *= -1; 788 $self->{hash}->{$cat}->{$k} *= -1;
647 } 789 }
648 } 790 }
649 } 791 }
792 $self->{hash}{value} = 0;
650} 793}
651 794
652sub to_string { 795sub to_string {
653 my ($self) = @_; 796 my ($self) = @_;
654 my $r = $self->{hash}; 797 my $r = $self->{hash};
675 my ($self, $thing) = @_; 818 my ($self, $thing) = @_;
676 819
677 my $obj = {}; 820 my $obj = {};
678 821
679 for (@Jeweler::RESISTS) { 822 for (@Jeweler::RESISTS) {
680 $obj->{resist}->{$_} = $thing->resistance ($_); 823 $obj->{resist}->{$_} = $thing->resist ($_);
681 } 824 }
682 825
683 my $stats = $thing->stats; 826 my $stats = $thing->stats;
684 827
685 for (qw/Str Dex Con Wis Cha Int Pow/) { 828 for (qw/Str Dex Con Wis Cha Int Pow/) {
693 $obj->{spec}{ac} = $stats->ac; 836 $obj->{spec}{ac} = $stats->ac;
694 $obj->{spec}{speed} = $stats->exp; 837 $obj->{spec}{speed} = $stats->exp;
695 $obj->{spec}{food} = $stats->food; 838 $obj->{spec}{food} = $stats->food;
696 839
697 $obj->{name} = $thing->name; 840 $obj->{name} = $thing->name;
698 $obj->{arch} = $thing->arch->name; 841 $obj->{arch} = $thing->arch->archname;
699 $obj->{face} = $thing->face; 842 $obj->{face} = $thing->face;
843
844 $obj->{value} = $thing->value;
845
846 $obj->{is_ring} = ($thing->type == cf::RING);
700 847
701 $self->{hash} = $obj 848 $self->{hash} = $obj
702} 849}
703 850
704sub to_object { 851sub to_object {
705 my ($self) = @_; 852 my ($self) = @_;
706 853
707 my $obj = cf::object::new $self->{hash}->{arch}; 854 my $obj = cf::object::new $self->{hash}->{arch};
855
856 $obj->item_power (floor ($self->power_to_level / 3)); # there have to be strings attached!
708 857
709 $obj->face ($self->{hash}{face}); 858 $obj->face ($self->{hash}{face});
710 859
711 my $stats = $obj->stats; 860 my $stats = $obj->stats;
712 861
720 869
721 $stats->$_ ($self->{hash}{stat}{lc $_}) 870 $stats->$_ ($self->{hash}{stat}{lc $_})
722 for qw/Str Dex Con Wis Cha Int Pow/; 871 for qw/Str Dex Con Wis Cha Int Pow/;
723 872
724 for (@Jeweler::RESISTS) { 873 for (@Jeweler::RESISTS) {
725 $obj->resistance ($_, $self->{hash}->{resist}->{$_}); 874 $obj->resist ($_, $self->{hash}->{resist}->{$_});
726 } 875 }
727 876
728 $obj->flag (cf::FLAG_IDENTIFIED, 1); 877 $obj->flag (cf::FLAG_IDENTIFIED, 1);
729 878
879 $obj->value ($self->{hash}{value});
880
730 return $obj; 881 return $obj;
882}
883
884sub set_value { $_[0]->{hash}{value} = $_[1] }
885
886sub is_better_than {
887 my ($self, $other) = @_;
888
889 for my $type (qw/spec stat resist/) {
890 for my $stat (keys %{$self->{hash}->{$type}}) {
891 if ($self->{hash}->{$type}->{$stat} > $other->{hash}->{$type}->{$stat}) {
892 return 1;
893 }
894 }
895 }
896
897 return 0;
731} 898}
732 899
733sub stat_level { 900sub stat_level {
734 my ($self) = @_; 901 my ($self) = @_;
735 my $stats = $self->{hash}->{stat} || {}; 902 my $stats = $self->{hash}->{stat} || {};
747sub resist_level { 914sub resist_level {
748 my ($self) = @_; 915 my ($self) = @_;
749 916
750 my $resists = $self->{hash}->{resist} || {}; 917 my $resists = $self->{hash}->{resist} || {};
751 918
752 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level'); 919 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level');
753 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level'); 920 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level');
754 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances'); 921 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances');
755 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances'); 922 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances');
756 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances'); 923 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances');
757 924
758 my $ressum = 0; 925 my $ressum = 0;
818sub power_to_level { 985sub power_to_level {
819 my ($self, $lvldescr) = @_; 986 my ($self, $lvldescr) = @_;
820 987
821 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements'); 988 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements');
822 my $max_impr_lvl = Jeweler::getcfg (maxlevels => 'improve_level'); 989 my $max_impr_lvl = Jeweler::getcfg (maxlevels => 'improve_level');
990 my $ring_offs = Jeweler::getcfg (maxlevels => 'ring_offset');
823 991
824 my ($stat_lvl, $stat_imprs) = $self->stat_level; 992 my ($stat_lvl, $stat_imprs) = $self->stat_level;
825 my ($resist_lvl, $res_imprs) = $self->resist_level; 993 my ($resist_lvl, $res_imprs) = $self->resist_level;
826 my ($spec_lvl, $spec_imprs) = $self->special_level; 994 my ($spec_lvl, $spec_imprs) = $self->special_level;
827 995
828 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs; 996 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs;
829 997
830 my $impr_lvl = ceil (($max_impr_lvl / ($max_imprs + 1)) * ($impr_sum - 1)); # 1 improvemnt bonus 998 my $impr_lvl =
999 ceil (($max_impr_lvl / ($max_imprs + 1))
1000 * ($impr_sum - 1)); # 1 improvemnt bonus
831 1001
832 my $levl = int max ($stat_lvl, $resist_lvl, $impr_lvl, $spec_lvl, 0); 1002 my $levl = int max ($stat_lvl, $resist_lvl, $impr_lvl, $spec_lvl, 0);
1003
1004 if ($self->{hash}->{is_ring}) {
1005 $levl += $ring_offs;
1006 }
1007
1008 $levl = min ($levl, cf::settings->max_level);
833 1009
834 if ($lvldescr) { 1010 if ($lvldescr) {
835 $$lvldescr = 1011 $$lvldescr =
836 sprintf "%3d: %s\n", $levl, 1012 sprintf "%3d: %s\n", $levl,
837 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, " 1013 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, "
849 for my $stat (keys %$stats) { 1025 for my $stat (keys %$stats) {
850 my $sum = $stats->{$stat}; 1026 my $sum = $stats->{$stat};
851 1027
852 next unless $sum > 0; 1028 next unless $sum > 0;
853 1029
854 my $statfx = fx ($sum, 'stat_potions'); 1030 my $statfx = fx ($sum, 'stat_items');
855 $cost->{"stat_$stat"} += $statfx->[0]; 1031 $cost->{"stat_$stat"} += $statfx->[0];
856 split_diamonds ($cost, $statfx->[1], 'stat_' . $stat); 1032 split_diamonds ($cost, $statfx->[1], 'stat_' . $stat);
857 } 1033 }
858} 1034}
859 1035
865 for my $spec (keys %$specials) { 1041 for my $spec (keys %$specials) {
866 my $sum = $specials->{$spec}; 1042 my $sum = $specials->{$spec};
867 1043
868 next unless $sum > 0; 1044 next unless $sum > 0;
869 1045
870 my $specfx = fx ($sum, 'spec_potions'); 1046 my $specfx = fx ($sum, 'spec_items');
871 $cost->{"spec_$spec"} += $specfx->[0]; 1047 $cost->{"spec_$spec"} += $specfx->[0];
872 split_diamonds ($cost, $specfx->[1], 'spec_' . $spec); 1048 split_diamonds ($cost, $specfx->[1], 'spec_' . $spec);
873 } 1049 }
874} 1050}
875 1051
878 1054
879 my $costs = {}; 1055 my $costs = {};
880 1056
881 my $ring = $self->{hash}; 1057 my $ring = $self->{hash};
882 1058
883 for my $resnam (keys %{$ring->{resist} || {}}) { 1059 for my $resnum (keys %{$ring->{resist} || {}}) {
884 1060
885 my $res = $ring->{resist}->{$resnam}; 1061 my $res = $ring->{resist}->{$resnum};
886 1062
887 next unless $res > 0; 1063 next unless $res > 0;
888 1064
1065 return undef if $res == 100;
1066
889 $costs->{"resist_" . $Jeweler::RESMAP{$resnam} . "_item"} += $res; 1067 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res;
890 1068
891 my $diamonds; 1069 my $diamonds;
892 if (grep { $resnam eq $_ } @Jeweler::EFFECT_RESISTS) { 1070 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) {
893 $diamonds += fx ($res, 'effect_resist_diamonds'); 1071 $diamonds += fx ($res, 'effect_resist_diamonds_x');
894 } else { 1072 } else {
895 $diamonds += fx ($res, 'attack_resist_diamonds'); 1073 $diamonds += fx ($res, 'attack_resist_diamonds_x');
896 } 1074 }
897 1075
898 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnam}); 1076 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum});
899 } 1077 }
900 1078
901 $self->add_stat_costs ($costs); 1079 $self->add_stat_costs ($costs);
902 $self->add_special_costs ($costs); 1080 $self->add_special_costs ($costs);
903 1081
908 my ($cost, $diamonds, $category) = @_; 1086 my ($cost, $diamonds, $category) = @_;
909 1087
910 my $stat_split = Jeweler::getcfg (diamond_split => $category); 1088 my $stat_split = Jeweler::getcfg (diamond_split => $category);
911 1089
912 my $sum = sum (@$stat_split); 1090 my $sum = sum (@$stat_split);
913 if ($sum < (1 - 0.0001)) {
914 warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!";
915 }
916 1091
917 my $emarch = cf::arch::find 'emerald'; 1092 my $emarch = cf::arch::find 'emerald';
918 my $saarch = cf::arch::find 'sapphire'; 1093 my $saarch = cf::arch::find 'sapphire';
919 my $pearch = cf::arch::find 'pearl'; 1094 my $pearch = cf::arch::find 'pearl';
920 my $ruarch = cf::arch::find 'ruby'; 1095 my $ruarch = cf::arch::find 'ruby';
921 my $diarch = cf::arch::find 'gem'; 1096 my $diarch = cf::arch::find 'gem';
922 1097
923 my $sumvalue = $diarch->clone->value * $diamonds; 1098 my $sumvalue = $diarch->value * $diamonds;
924 1099
925 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->clone->value; 1100 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->value;
926 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->clone->value; 1101 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->value;
927 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->clone->value; 1102 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->value;
928 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->clone->value; 1103 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->value;
929 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->clone->value; 1104 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value;
930} 1105}
931 1106
932package Jeweler::Util; 1107package Jeweler::Util;
933 1108
934use strict; 1109use strict;
940=over 4 1115=over 4
941 1116
942=item remove ($object[, $nrof]) 1117=item remove ($object[, $nrof])
943 1118
944Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects. 1119Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects.
945The returnvalue is the number of 'single' objects that couldn't be removed. 1120The return value is the number of 'single' objects that couldn't be removed.
946 1121
947=cut 1122=cut
948 1123
949sub remove { 1124sub remove {
950 my ($obj, $nrof) = @_; 1125 my ($obj, $nrof) = @_;
1126
1127 my $c = $obj->nrof || 1;
1128 my $r = $c > $nrof ? 0 : $nrof - $c;
1129 $obj->decrease (defined ($nrof) ? $nrof : ($obj->nrof || 1));
951 1130
952 my $cnt; 1131 $r
953
954 if (defined $nrof) {
955 return 0 if ($nrof * 1) == 0;
956 $cnt = int (($obj->nrof || 1) - (1 * $nrof));
957
958 if ($cnt > 0) {
959 $obj->nrof ($cnt);
960 return 0;
961 }
962 }
963
964 remove ($_) for $obj->inv;
965 $obj->destroy;
966 return $cnt;
967} 1132}
968 1133
969sub grep_for_match { 1134sub check_for_match {
970 my ($thing, @matchar) = @_; 1135 my ($thing, @matchar) = @_;
971 1136
972 my $i = 0; 1137 my $i = 0;
1138 my $check_cnts = 0;
1139 my $check_true = 0;
973 for my $match (@matchar) { 1140 for my $match (@matchar) {
1141 if ($i % 3 == 0) {
1142 return 1 if $check_true && $check_cnts == $check_true;
1143 $check_cnts = 0;
1144 $check_true = 0;
1145 }
1146
974 if ($match =~ m/^\s*$/) { 1147 if ($match =~ m/^\s*$/) {
975 $i++; 1148 $i++;
976 next; 1149 next;
977 } 1150 }
978 1151
1152 $check_cnts++;
979 if ($i % 3 == 0) { 1153 if ($i % 3 == 0) {
980 $thing->name eq $match 1154 $thing->name eq $match
981 and return 1; 1155 and $check_true++;
982 } elsif ($i % 3 == 1) { 1156 } elsif ($i % 3 == 1) {
983 $thing->title eq $match 1157 $thing->title eq $match
984 and return 1; 1158 and $check_true++;
985 } else { # $i % 3 == 2 1159 } else { # $i % 3 == 2
986 $thing->arch->name eq $match 1160 $thing->arch->archname eq $match
987 and return 1; 1161 and $check_true++;
988 } 1162 }
989 $i++; 1163 $i++;
990 } 1164 }
1165 #d# warn "CHECK $check_true | $check_cnts | [".(join ',', @matchar).":".join (",", ($thing->name, $thing->title, $thing->arch->archname))."\n";
1166 return 1 if $check_true && $check_cnts == $check_true;
991 return 0; 1167 return 0;
992} 1168}
993 1169
1170sub grep_for_match {
1171 my ($ingred, $group, @matchar) = @_;
1172
1173 for my $thing (@{$ingred->{$group} || []}) {
1174 #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->archname, "@matchar"; #d#
1175 if (check_for_match ($thing, @matchar)) {
1176 return $thing;
1177 }
1178 }
1179 return undef;
1180}
1181
994=back 1182=back
995 1183
9961 11841

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines