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.37 by elmex, Wed May 5 08:19:06 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;
240 253
241use strict; 254use common::sense;
242 255
243=head2 CauldronHandler 256=head2 CauldronHandler
244 257
245The Jeweler::CauldronHandler package, that helps you with handling the 258The Jeweler::CauldronHandler package, that helps you with handling the
246cauldron stuff. Can also be used for other skills. 259cauldron stuff. Can also be used for other skills.
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
350 370
351package Jeweler::Ingredients; 371package Jeweler::Ingredients;
372
373use common::sense;
374
352use Storable qw/dclone/; 375use Storable qw/dclone/;
353use strict;
354 376
355=head2 Ingredients 377=head2 Ingredients
356 378
357This class handles the ingredients. 379This class handles the ingredients.
358 380
382 404
383sub value { 405sub value {
384 my ($self, $group, $archname) = @_; 406 my ($self, $group, $archname) = @_;
385 407
386 my @objs = grep { 408 my @objs = grep {
387 $_->arch->name eq $archname 409 $_->arch->archname eq $archname
388 } @{$self->{ingredients}->{$group} || []}; 410 } @{$self->{ingredients}->{$group} || []};
389 411
390 my $sum = 0; 412 my $sum = 0;
391 for (@objs) { 413 for (@objs) {
392 $sum += ($_->nrof || 1) * $_->value; 414 $sum += ($_->nrof || 1) * $_->value;
409 431
410 my @out; 432 my @out;
411 433
412 for (@{$ingred->{$group}}) { 434 for (@{$ingred->{$group}}) {
413 if (defined $archname) { 435 if (defined $archname) {
414 if ($_->arch->name eq $archname) { 436 if ($_->arch->archname eq $archname) {
415 Jeweler::Util::remove ($_); 437 Jeweler::Util::remove ($_);
416 } else { 438 } else {
417 push @out, $_; 439 push @out, $_;
418 } 440 }
419 } else { 441 } else {
427sub get_plan { 449sub get_plan {
428 my ($self) = @_; 450 my ($self) = @_;
429 451
430 my $ingred = $self->{ingredients}; 452 my $ingred = $self->{ingredients};
431 453
432 for my $grp (keys %$ingred) {
433 for my $pot (@{$ingred->{$grp}}) {
434 for my $plan (keys %{$Jeweler::CFG->{plans}}) { 454 for my $plan (keys %{$Jeweler::CFG->{plans}}) {
435 my $plg = $Jeweler::CFG->{plans}->{$plan}; 455 my $plg = $Jeweler::CFG->{plans}->{$plan};
436 my @plga = (); 456 my @plga = ();
437 unless (ref $plg eq 'ARRAY') { 457 unless (ref $plg eq 'ARRAY') {
438 push @plga, $plg; 458 push @plga, $plg;
439 } else { 459 } else {
440 @plga = @$plg; 460 @plga = @$plg;
441 } 461 }
442 next unless @plga > 0; 462 next unless @plga > 0;
443 if (Jeweler::Util::grep_for_match ($pot, @plga)) { 463 if (Jeweler::Util::grep_for_match ($ingred, @plga)) {
444 return $plan; 464 return $plan;
445 }
446 }
447 } 465 }
448 } 466 }
449} 467}
450 468
451sub get_ring { 469sub get_ring {
454 @{$self->{ingredients}->{ammys} || []}, 472 @{$self->{ingredients}->{ammys} || []},
455 @{$self->{ingredients}->{rings} || []} 473 @{$self->{ingredients}->{rings} || []}
456 ); 474 );
457} 475}
458 476
477sub improve_max {
478 my ($stat, $impro) = @_;
479 if ($stat >= 0) {
480 $stat = $impro > $stat ? $impro : $stat;
481 }
482 $stat
483}
484
459sub improve_ring_by_plan { 485sub improve_ring_by_plan {
460 my ($self, $plan, $ring) = @_; 486 my ($self, $plan, $ring) = @_;
461 487
462 $ring = dclone ($ring); 488 $ring = dclone $ring;
463 489
464 my $ingred = $self->{ingredients}; 490 my $ingred = $self->{ingredients};
465 my $impr = {}; 491 my $impr = {};
466 492
467 if ($plan =~ m/^stat_(\S+)$/) { 493 if ($plan =~ m/^stat_(\S+)$/) {
468 my $statname = $1; 494 my $statname = $1;
469 my $plingred = Jeweler::getcfg (plans => $plan) 495 my $plingred = Jeweler::getcfg (plans => $plan)
470 or die "ingredients for plan '$plan' not defined!"; 496 or die "ingredients for plan '$plan' not defined!";
471 497
472 my $cnt = 0; 498 my $cnt = 0;
473 for my $pot (@{$ingred->{potions}}) {
474 if (Jeweler::Util::grep_for_match ($pot, @$plingred)) { 499 if (my $pot = Jeweler::Util::grep_for_match ($ingred, @$plingred)) {
475 $cnt += $pot->nrof; 500 $cnt += $pot->nrof;
476 }
477 } 501 }
478 502
479 my $maxstat = Jeweler::getcfg (maximprovements => 'stats'); 503 my $maxstat = Jeweler::getcfg (maximprovements => 'stats');
480 my $did_impr = 0;
481 for my $x (1..$maxstat) { 504 for my $x (1..$maxstat) {
482 my $y = Jeweler::Object::fx ($x, 'stat_potions'); 505 my $y = Jeweler::Object::fx ($x, 'stat_items');
483 506
484 if ($cnt <= $y->[0]) { 507 if ($cnt <= $y->[0]) {
485 $ring->{hash}->{stat}->{$statname} += $x; 508 $ring->{hash}->{stat}->{$statname} =
486 $did_impr = 1; 509 improve_max $ring->{hash}->{stat}->{$statname}, $x;
487 last; 510 last;
488 } 511 }
489 } 512 }
490 513
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+)$/) { 514 } elsif ($plan =~ m/^spec_(\S+)$/) {
515 my $specname = $1;
516 my $plingred = Jeweler::getcfg (plans => $plan)
517 or die "ingredients for plan '$plan' not defined!";
518
519 my $cnt = 0;
520 if (my $pot = Jeweler::Util::grep_for_match ($ingred, @$plingred)) {
521 $cnt += $pot->nrof;
522 }
523
524 my $maxspec = Jeweler::getcfg (maximprovements => 'specials');
525 for my $x (1..$maxspec) {
526 my $y = Jeweler::Object::fx ($x, 'spec_items');
527
528 if ($cnt <= $y->[0]) {
529 $ring->{hash}->{spec}->{$specname} =
530 improve_max $ring->{hash}->{spec}->{$specname}, $x;
531 last;
532 }
533 }
534
495 } elsif ($plan =~ m/^resist_(\S+)$/) { 535 } elsif ($plan =~ m/^resist_(\S+)$/) {
536 my $resname = $1;
537 my $resnum = $REV_RESMAP{$resname};
538 my $plingred = Jeweler::getcfg (plans => $plan)
539 or die "ingredients for plan '$plan' not defined!";
540
541 my $cnt = 0;
542 if (my $it = Jeweler::Util::grep_for_match ($ingred, @$plingred)) {
543 $cnt += $it->nrof;
544 }
545 my $resist_item_nr = 0;
546 $self->do_grep (sub { $resist_item_nr += ($_[0]->nrof || 1); 0 }, @$plingred);
547
548 my $maximprovname = (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS)
549 ? 'effect_resistances'
550 : 'attack_resistances';
551
552 my $maxres = Jeweler::getcfg (maximprovements => $maximprovname);
553 $resist_item_nr = $maxres if ($resist_item_nr > $maxres);
554 $ring->{hash}->{resist}->{$resnum} =
555 improve_max $ring->{hash}->{resist}->{$resnum}, $resist_item_nr;
496 } 556 }
497 557
498 return $ring; 558 return $ring;
499} 559}
500 560
501sub do_grep { 561sub do_grep {
502 my ($self, $cb, @grepar) = @_; 562 my ($self, $cb, $cat, @grepar) = @_;
503 563
504 my $ingred = $self->{ingredients}; 564 my $ingred = $self->{ingredients};
505 565
506
507 for my $cat (keys %$ingred) {
508 my @rem; 566 my @rem;
509 for my $ing (@{$ingred->{$cat}}) { 567 for my $ing (@{$ingred->{$cat}}) {
510 if (Jeweler::Util::grep_for_match ($ing, @grepar)) { 568 if (Jeweler::Util::check_for_match ($ing, @grepar)) {
511 unless ($cb->($ing)) { 569 unless ($cb->($ing)) {
512 push @rem, $ing;
513 }
514 } else {
515 push @rem, $ing; 570 push @rem, $ing;
516 } 571 }
572 } else {
573 push @rem, $ing;
517 } 574 }
575 }
518 @{$ingred->{$cat}} = @rem; 576 @{$ingred->{$cat}} = @rem;
519 }
520} 577}
521 578
522sub check_costs { 579sub check_costs {
523 my ($self, $costs, $do_remove) = @_; 580 my ($self, $costs, $do_remove) = @_;
524 581
525 my $costs = dclone ($costs); 582 my $costs = dclone $costs;
526 583
527 for my $key (keys %$costs) { 584 for my $key (keys %$costs) {
528 my @grepar; 585 my @grepar;
529 if ($key =~ m/^stat_(\S+)$/) { 586 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items
530 @grepar = @{Jeweler::getcfg (plans => $key) || []}; 587 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} };
531 } else { 588 next if $@;
589 } else { # check the gems
532 @grepar = (undef, undef, $key); 590 @grepar = ('gems', undef, undef, $key);
533 } 591 }
534 592
535 if ($do_remove) { 593 if ($do_remove) {
536 my $rem = $costs->{$key}; 594 my $rem = $costs->{$key};
537 $self->do_grep (sub { if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); } 1 }, @grepar); 595 $self->do_grep (sub {
596 if ($rem) {
597 $rem = Jeweler::Util::remove ($_[0], $rem);
598 }
599 1
600 }, @grepar);
538 if ($rem > 0) { 601 if ($rem > 0) {
539 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 602 warn "JEWELER BUG: removed ingredients ($key) $rem > 0 after removing!";
540 } 603 }
604
541 } else { 605 } else {
542 my $nr; 606 my $nr;
543 $self->do_grep (sub { $nr += $_[0]->nrof; 0 }, @grepar); 607 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
544 $costs->{$key} -= $nr; 608 $costs->{$key} -= $nr;
545 } 609 }
610
546 } 611 }
547 612
548 return $costs; 613 return $costs;
549} 614}
550 615
563 } 628 }
564 } 629 }
565} 630}
566 631
567package Jeweler::Object; 632package Jeweler::Object;
568use strict; 633
634use common::sense;
569use POSIX; 635use POSIX;
570use List::Util qw/max min sum/; 636use List::Util qw/max min sum/;
571 637
572sub new { 638sub new {
573 my ($class, %arg) = @_; 639 my ($class, %arg) = @_;
577 $self->ring_or_ammy_to_hash ($arg{object}); 643 $self->ring_or_ammy_to_hash ($arg{object});
578 644
579 $self; 645 $self;
580} 646}
581 647
648sub has_resist {
649 my ($self, $resistnam, $resistval) = @_;
650 my $resnum = $REV_RESMAP{uc $resistnam};
651 if (defined ($resistval)) {
652 return 1 if $self->{hash}->{resist}->{$resnum} == $resistval;
653 } else {
654 return 1 if $self->{hash}->{resist}->{$resnum};
655 }
656 return undef;
657}
658
659sub lvl2exp {
660 my $lvl = shift;
661 (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1))
662 / (20 + max ($lvl - 1, 0)) # 20 + level times making such a ring
663 # should get you to the rings level at least.
664}
665
666sub projected_exp {
667 my ($self, $input_level) = @_;
668
669 my $lvl = max ($self->power_to_level, 1);
670 my $exp = lvl2exp ($lvl);
671
672 if (defined $input_level) { # in case we merge rings:
673 my $subexp = lvl2exp ($input_level);
674 $exp -= $subexp;
675 $exp = max ($exp, 0);
676
677 } else {
678 # the experience bonus here is to make level 1 rings give you at least
679 # 200 exp points when making them. This also makes leveling in the
680 # first few levels a bit easier. (probably until around level 5-6).
681 my $expbonus = cf::level_to_min_exp (2) / 5;
682 # this bonus should also only be given for _new_ rings and not for merged
683 # ones - to prevent infinite exp making.
684 $exp += $expbonus;
685 }
686
687 $exp
688}
689
582sub analyze { 690sub analyze {
583 my ($self, $sk, $pl) = @_; 691 my ($self, $sk, $pl, $input_level) = @_;
692 my $costs = $self->calc_costs;
693
694 unless (defined $costs) {
695 return "This ring has a resistancy above 99%, you can't make that.";
696 }
584 697
585 my $sklvl = cf::exp_to_level ($sk->stats->exp); 698 my $sklvl = cf::exp_to_level ($sk->stats->exp);
586 my $ringlvl = $self->power_to_level; 699 my $ringlvl = $self->power_to_level;
587 700
588 my $tmpl; 701 my $tmpl;
589 if ($pl->flag (cf::FLAG_WIZ)) { 702 if ($pl->flag (cf::FLAG_WIZ)) {
590 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl); 703 $tmpl = Jeweler::level_diff_to_chance_perc ($sklvl - $ringlvl);
591 } else { 704 } else {
592 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl); 705 $tmpl = Jeweler::level_diff_to_str ($sklvl - $ringlvl);
593 } 706 }
594 my $msg = sprintf "Projected success rate: %s", $tmpl; 707 my $msg = sprintf "Projected success rate: %s, you would get %d exp for this.", $tmpl, $self->projected_exp ($input_level);
595 return $msg; 708 return $msg;
709}
710
711sub calc_value_from_cost {
712 my ($self, $costs) = @_;
713 my $emarch = cf::arch::find 'emerald';
714 my $saarch = cf::arch::find 'sapphire';
715 my $pearch = cf::arch::find 'pearl';
716 my $ruarch = cf::arch::find 'ruby';
717 my $diarch = cf::arch::find 'gem';
718 my $value = $emarch->value * $costs->{emerald}
719 + $saarch->value * $costs->{sapphire}
720 + $pearch->value * $costs->{pearl}
721 + $ruarch->value * $costs->{ruby}
722 + $diarch->value * $costs->{gem};
723
724 $value
596} 725}
597 726
598sub wiz_analyze { 727sub wiz_analyze {
599 my ($self, $pl) = @_; 728 my ($self, $pl) = @_;
600 my $costs = $self->calc_costs; 729 my $costs = $self->calc_costs;
730 if (defined $costs) {
601 my $desc = ""; 731 my $desc = "";
602 my $lvl = $self->power_to_level (\$desc); 732 my $lvl = $self->power_to_level (\$desc);
733 my $scosts = $self->calc_value_from_cost ($costs);
734
735 $pl->message ("costs: "
603 $pl->message ("costs: " . join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)); 736 . (join ', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)
737 . " ("
738 . ($scosts / "platinacoin"->cf::arch::find->value)
739 . " platinum)");
604 $pl->message ("level: " . $desc); 740 $pl->message ("level: $desc");
741 } else {
742 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
743 }
605} 744}
606
607 745
608sub get_chance_perc { 746sub get_chance_perc {
609 my ($self, $sk) = @_; 747 my ($self, $sk) = @_;
610 my $sklvl = cf::exp_to_level ($sk->stats->exp); 748 my $sklvl = cf::exp_to_level ($sk->stats->exp);
611 my $ringlvl = $self->power_to_level; 749 my $ringlvl = $self->power_to_level;
614 752
615sub fx { 753sub fx {
616 my ($res, $cfg) = @_; 754 my ($res, $cfg) = @_;
617 my $or = $res; 755 my $or = $res;
618 my $ar = $Jeweler::CFG->{functions}->{$cfg}; 756 my $ar = $Jeweler::CFG->{functions}->{$cfg};
757
619 if (ref $ar->[0] eq 'ARRAY') { 758 if (ref $ar && ref $ar->[0] eq 'ARRAY') {
620 $res = $res - 1; 759 $res = $res - 1;
760 return $ar->[max (min ($res, @$ar - 1), 0)];
761
621 } else { 762 } else {
622 $res = ceil ($res / 5) - 1; 763 # +0.1 is for a jump to the next index when $res / 5 is exactly 1, 2, 3...
623 } 764 # old code:
765 #my $idx = ceil (($res / 5) + 0.1) - 1;
624 $ar->[max (min ($res, @$ar - 1), 0)]; 766 #my $a = $ar->[max (min ($idx, @$ar - 1), 0)];
767 #my $b = $ar->[max (min ($idx + 1, @$ar - 1), 0)];
768 #my $diff = $b - $a; # use the difference of the cost to the next cost
769 #my $o_cost = $a + ($diff / 5) * ($res % 5); # and do some linear interpolation
770 #return $o_cost;
771 return 0 if $res <= 0;
772 return ($ar / (1 - ($res * 0.01)) - $ar)
773 }
625} 774}
626 775
627sub improve_by_ring { 776sub improve_by_ring {
628 my ($self, @rings) = @_; 777 my ($self, @rings) = @_;
629 my $ring = $self; 778 my $ring = $self;
645 if ($self->{hash}->{$cat}->{$k} > 0) { 794 if ($self->{hash}->{$cat}->{$k} > 0) {
646 $self->{hash}->{$cat}->{$k} *= -1; 795 $self->{hash}->{$cat}->{$k} *= -1;
647 } 796 }
648 } 797 }
649 } 798 }
799 $self->{hash}{value} = 0;
650} 800}
651 801
652sub to_string { 802sub to_string {
653 my ($self) = @_; 803 my ($self) = @_;
654 my $r = $self->{hash}; 804 my $r = $self->{hash};
675 my ($self, $thing) = @_; 825 my ($self, $thing) = @_;
676 826
677 my $obj = {}; 827 my $obj = {};
678 828
679 for (@Jeweler::RESISTS) { 829 for (@Jeweler::RESISTS) {
680 $obj->{resist}->{$_} = $thing->resistance ($_); 830 $obj->{resist}->{$_} = $thing->resist ($_);
681 } 831 }
682 832
683 my $stats = $thing->stats; 833 my $stats = $thing->stats;
684 834
685 for (qw/Str Dex Con Wis Cha Int Pow/) { 835 for (qw/Str Dex Con Wis Cha Int Pow/) {
693 $obj->{spec}{ac} = $stats->ac; 843 $obj->{spec}{ac} = $stats->ac;
694 $obj->{spec}{speed} = $stats->exp; 844 $obj->{spec}{speed} = $stats->exp;
695 $obj->{spec}{food} = $stats->food; 845 $obj->{spec}{food} = $stats->food;
696 846
697 $obj->{name} = $thing->name; 847 $obj->{name} = $thing->name;
698 $obj->{arch} = $thing->arch->name; 848 $obj->{arch} = $thing->arch->archname;
699 $obj->{face} = $thing->face; 849 $obj->{face} = $thing->face;
850
851 $obj->{value} = $thing->value;
852
853 $obj->{is_ring} = ($thing->type == cf::RING);
700 854
701 $self->{hash} = $obj 855 $self->{hash} = $obj
702} 856}
703 857
704sub to_object { 858sub to_object {
705 my ($self) = @_; 859 my ($self) = @_;
706 860
707 my $obj = cf::object::new $self->{hash}->{arch}; 861 my $obj = cf::object::new $self->{hash}->{arch};
862
863 $obj->item_power (floor ($self->power_to_level / 5)); # there have to be strings attached!
708 864
709 $obj->face ($self->{hash}{face}); 865 $obj->face ($self->{hash}{face});
710 866
711 my $stats = $obj->stats; 867 my $stats = $obj->stats;
712 868
720 876
721 $stats->$_ ($self->{hash}{stat}{lc $_}) 877 $stats->$_ ($self->{hash}{stat}{lc $_})
722 for qw/Str Dex Con Wis Cha Int Pow/; 878 for qw/Str Dex Con Wis Cha Int Pow/;
723 879
724 for (@Jeweler::RESISTS) { 880 for (@Jeweler::RESISTS) {
725 $obj->resistance ($_, $self->{hash}->{resist}->{$_}); 881 $obj->resist ($_, $self->{hash}->{resist}->{$_});
726 } 882 }
727 883
728 $obj->flag (cf::FLAG_IDENTIFIED, 1); 884 $obj->flag (cf::FLAG_IDENTIFIED, 1);
729 885
886 $obj->value ($self->{hash}{value});
887
730 return $obj; 888 return $obj;
889}
890
891sub set_value { $_[0]->{hash}{value} = $_[1] }
892
893sub is_better_than {
894 my ($self, $other) = @_;
895
896 for my $type (qw/spec stat resist/) {
897 for my $stat (keys %{$self->{hash}->{$type}}) {
898 if ($self->{hash}->{$type}->{$stat} > $other->{hash}->{$type}->{$stat}) {
899 return 1;
900 }
901 }
902 }
903
904 return 0;
731} 905}
732 906
733sub stat_level { 907sub stat_level {
734 my ($self) = @_; 908 my ($self) = @_;
735 my $stats = $self->{hash}->{stat} || {}; 909 my $stats = $self->{hash}->{stat} || {};
747sub resist_level { 921sub resist_level {
748 my ($self) = @_; 922 my ($self) = @_;
749 923
750 my $resists = $self->{hash}->{resist} || {}; 924 my $resists = $self->{hash}->{resist} || {};
751 925
752 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level'); 926 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level');
753 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level'); 927 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level');
754 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances'); 928 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances');
755 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances'); 929 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances');
756 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances'); 930 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances');
757 931
758 my $ressum = 0; 932 my $ressum = 0;
818sub power_to_level { 992sub power_to_level {
819 my ($self, $lvldescr) = @_; 993 my ($self, $lvldescr) = @_;
820 994
821 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements'); 995 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements');
822 my $max_impr_lvl = Jeweler::getcfg (maxlevels => 'improve_level'); 996 my $max_impr_lvl = Jeweler::getcfg (maxlevels => 'improve_level');
997 my $ring_offs = Jeweler::getcfg (maxlevels => 'ring_offset');
823 998
824 my ($stat_lvl, $stat_imprs) = $self->stat_level; 999 my ($stat_lvl, $stat_imprs) = $self->stat_level;
825 my ($resist_lvl, $res_imprs) = $self->resist_level; 1000 my ($resist_lvl, $res_imprs) = $self->resist_level;
826 my ($spec_lvl, $spec_imprs) = $self->special_level; 1001 my ($spec_lvl, $spec_imprs) = $self->special_level;
827 1002
828 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs; 1003 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs;
829 1004
830 my $impr_lvl = ceil (($max_impr_lvl / ($max_imprs + 1)) * ($impr_sum - 1)); # 1 improvemnt bonus 1005 my $impr_lvl =
1006 ceil (($max_impr_lvl / ($max_imprs + 1))
1007 * ($impr_sum - 1)); # 1 improvemnt bonus
831 1008
832 my $levl = int max ($stat_lvl, $resist_lvl, $impr_lvl, $spec_lvl, 0); 1009 my $levl = int max ($stat_lvl, $resist_lvl, $impr_lvl, $spec_lvl, 0);
1010
1011 if ($self->{hash}->{is_ring}) {
1012 $levl += $ring_offs;
1013 }
1014
1015 $levl = min ($levl, cf::settings->max_level);
833 1016
834 if ($lvldescr) { 1017 if ($lvldescr) {
835 $$lvldescr = 1018 $$lvldescr =
836 sprintf "%3d: %s\n", $levl, 1019 sprintf "%3d: %s\n", $levl,
837 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, " 1020 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, "
849 for my $stat (keys %$stats) { 1032 for my $stat (keys %$stats) {
850 my $sum = $stats->{$stat}; 1033 my $sum = $stats->{$stat};
851 1034
852 next unless $sum > 0; 1035 next unless $sum > 0;
853 1036
854 my $statfx = fx ($sum, 'stat_potions'); 1037 my $statfx = fx ($sum, 'stat_items');
855 $cost->{"stat_$stat"} += $statfx->[0]; 1038 $cost->{"stat_$stat"} += $statfx->[0];
856 split_diamonds ($cost, $statfx->[1], 'stat_' . $stat); 1039 split_diamonds ($cost, $statfx->[1], 'stat_' . $stat);
857 } 1040 }
858} 1041}
859 1042
865 for my $spec (keys %$specials) { 1048 for my $spec (keys %$specials) {
866 my $sum = $specials->{$spec}; 1049 my $sum = $specials->{$spec};
867 1050
868 next unless $sum > 0; 1051 next unless $sum > 0;
869 1052
870 my $specfx = fx ($sum, 'spec_potions'); 1053 my $specfx = fx ($sum, 'spec_items');
871 $cost->{"spec_$spec"} += $specfx->[0]; 1054 $cost->{"spec_$spec"} += $specfx->[0];
872 split_diamonds ($cost, $specfx->[1], 'spec_' . $spec); 1055 split_diamonds ($cost, $specfx->[1], 'spec_' . $spec);
873 } 1056 }
874} 1057}
875 1058
878 1061
879 my $costs = {}; 1062 my $costs = {};
880 1063
881 my $ring = $self->{hash}; 1064 my $ring = $self->{hash};
882 1065
883 for my $resnam (keys %{$ring->{resist} || {}}) { 1066 for my $resnum (keys %{$ring->{resist} || {}}) {
884 1067
885 my $res = $ring->{resist}->{$resnam}; 1068 my $res = $ring->{resist}->{$resnum};
886 1069
887 next unless $res > 0; 1070 next unless $res > 0;
888 1071
1072 return undef if $res == 100;
1073
889 $costs->{"resist_" . $Jeweler::RESMAP{$resnam} . "_item"} += $res; 1074 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res;
890 1075
891 my $diamonds; 1076 my $diamonds;
892 if (grep { $resnam eq $_ } @Jeweler::EFFECT_RESISTS) { 1077 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) {
893 $diamonds += fx ($res, 'effect_resist_diamonds'); 1078 $diamonds += fx ($res, 'effect_resist_diamonds_x');
894 } else { 1079 } else {
895 $diamonds += fx ($res, 'attack_resist_diamonds'); 1080 $diamonds += fx ($res, 'attack_resist_diamonds_x');
896 } 1081 }
897 1082
898 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnam}); 1083 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum});
899 } 1084 }
900 1085
901 $self->add_stat_costs ($costs); 1086 $self->add_stat_costs ($costs);
902 $self->add_special_costs ($costs); 1087 $self->add_special_costs ($costs);
903 1088
908 my ($cost, $diamonds, $category) = @_; 1093 my ($cost, $diamonds, $category) = @_;
909 1094
910 my $stat_split = Jeweler::getcfg (diamond_split => $category); 1095 my $stat_split = Jeweler::getcfg (diamond_split => $category);
911 1096
912 my $sum = sum (@$stat_split); 1097 my $sum = sum (@$stat_split);
913 if ($sum < (1 - 0.0001)) {
914 warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!";
915 }
916 1098
917 my $emarch = cf::arch::find 'emerald'; 1099 my $emarch = cf::arch::find 'emerald';
918 my $saarch = cf::arch::find 'sapphire'; 1100 my $saarch = cf::arch::find 'sapphire';
919 my $pearch = cf::arch::find 'pearl'; 1101 my $pearch = cf::arch::find 'pearl';
920 my $ruarch = cf::arch::find 'ruby'; 1102 my $ruarch = cf::arch::find 'ruby';
921 my $diarch = cf::arch::find 'gem'; 1103 my $diarch = cf::arch::find 'gem';
922 1104
923 my $sumvalue = $diarch->clone->value * $diamonds; 1105 my $sumvalue = $diarch->value * $diamonds;
924 1106
925 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->clone->value; 1107 $cost->{emerald} += ceil $sumvalue * $stat_split->[0] / max 1, $emarch->value;
926 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->clone->value; 1108 $cost->{sapphire} += ceil $sumvalue * $stat_split->[1] / max 1, $saarch->value;
927 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->clone->value; 1109 $cost->{pearl} += ceil $sumvalue * $stat_split->[2] / max 1, $pearch->value;
928 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->clone->value; 1110 $cost->{ruby} += ceil $sumvalue * $stat_split->[3] / max 1, $ruarch->value;
929 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->clone->value; 1111 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value;
930} 1112}
931 1113
932package Jeweler::Util; 1114package Jeweler::Util;
933 1115
934use strict; 1116use common::sense;
935 1117
936=head2 Util 1118=head2 Util
937 1119
938Some utility functions for the Jeweler skill. 1120Some utility functions for the Jeweler skill.
939 1121
940=over 4 1122=over 4
941 1123
942=item remove ($object[, $nrof]) 1124=item remove ($object[, $nrof])
943 1125
944Removes the C<$object>. If C<$nrof> is given, remove only C<$nrof> objects. 1126Removes 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. 1127The return value is the number of 'single' objects that couldn't be removed.
946 1128
947=cut 1129=cut
948 1130
949sub remove { 1131sub remove {
950 my ($obj, $nrof) = @_; 1132 my ($obj, $nrof) = @_;
951 1133
952 my $cnt; 1134 my $c = $obj->number_of;
1135 my $r = $c > $nrof ? 0 : $nrof - $c;
1136 $obj->decrease (defined ($nrof) ? $nrof : ($obj->nrof || 1));
953 1137
954 if (defined $nrof) { 1138 $r
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} 1139}
968 1140
969sub grep_for_match { 1141sub check_for_match {
970 my ($thing, @matchar) = @_; 1142 my ($thing, @matchar) = @_;
971 1143
972 my $i = 0; 1144 my $i = 0;
1145 my $check_cnts = 0;
1146 my $check_true = 0;
973 for my $match (@matchar) { 1147 for my $match (@matchar) {
1148 if ($i % 3 == 0) {
1149 return 1 if $check_true && $check_cnts == $check_true;
1150 $check_cnts = 0;
1151 $check_true = 0;
1152 }
1153
974 if ($match =~ m/^\s*$/) { 1154 if ($match =~ m/^\s*$/) {
975 $i++; 1155 $i++;
976 next; 1156 next;
977 } 1157 }
978 1158
1159 $check_cnts++;
979 if ($i % 3 == 0) { 1160 if ($i % 3 == 0) {
980 $thing->name eq $match 1161 $thing->name eq $match
981 and return 1; 1162 and $check_true++;
982 } elsif ($i % 3 == 1) { 1163 } elsif ($i % 3 == 1) {
983 $thing->title eq $match 1164 $thing->title eq $match
984 and return 1; 1165 and $check_true++;
985 } else { # $i % 3 == 2 1166 } else { # $i % 3 == 2
986 $thing->arch->name eq $match 1167 $thing->arch->archname eq $match
987 and return 1; 1168 and $check_true++;
988 } 1169 }
989 $i++; 1170 $i++;
990 } 1171 }
1172 #d# warn "CHECK $check_true | $check_cnts | [".(join ',', @matchar).":".join (",", ($thing->name, $thing->title, $thing->arch->archname))."\n";
1173 return 1 if $check_true && $check_cnts == $check_true;
991 return 0; 1174 return 0;
992} 1175}
993 1176
1177sub grep_for_match {
1178 my ($ingred, $group, @matchar) = @_;
1179
1180 for my $thing (@{$ingred->{$group} || []}) {
1181 #d# warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->archname, "@matchar"; #d#
1182 if (check_for_match ($thing, @matchar)) {
1183 return $thing;
1184 }
1185 }
1186 return undef;
1187}
1188
994=back 1189=back
995 1190
9961 11911

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines