ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/Jeweler.pm
(Generate patch)

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines