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

Comparing deliantra/server/ext/Jeweler.pm (file contents):
Revision 1.1 by root, Fri Dec 15 19:29:18 2006 UTC vs.
Revision 1.6 by elmex, Thu Feb 1 01:46:45 2007 UTC

117 cf::ATNR_LIFE_STEALING => "LIFE_STEALING", 117 cf::ATNR_LIFE_STEALING => "LIFE_STEALING",
118 cf::ATNR_BLIND => "BLIND", 118 cf::ATNR_BLIND => "BLIND",
119 cf::ATNR_DISEASE => "DISEASE", 119 cf::ATNR_DISEASE => "DISEASE",
120); 120);
121 121
122our %REV_RESMAP = map { $RESMAP{$_} => $_ } keys %RESMAP;
123
122our %LVL_DIFF_CHANCES = ( 124our %LVL_DIFF_CHANCES = (
123 +5 => 100, 125 +5 => 100,
124 +4 => 95, 126 +4 => 95,
125 +3 => 85, 127 +3 => 85,
126 +2 => 75, 128 +2 => 75,
222 my $outarchval = cf::arch::find ($outarch)->clone->value; 224 my $outarchval = cf::arch::find ($outarch)->clone->value;
223 225
224 my $nrof = int ($archvalsum / (($outarchval || 1000) * $outarchvalfact)); 226 my $nrof = int ($archvalsum / (($outarchval || 1000) * $outarchvalfact));
225 if ($nrof) { 227 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) 228 # XXX: yes, i know what i'm doing here, i don't set nrof, but it didn't work somehow (pls. chek sometimes)
229 for (1..$nrof) {
227 $chdl->put (cf::object::new $outarch) for 1..$nrof; 230 $chdl->put (my $ob = cf::object::new $outarch);
231 $ob->set_animation (cf::rndm $ob->num_animations);
232 $ob->flag (cf::FLAG_IDENTIFIED, 1);
233 }
228 234
229 my $xp_sum = ($xp_gain * $nrof); 235 my $xp_sum = ($xp_gain * $nrof);
230 236
231 if ($xp_sum) { 237 if ($xp_sum) {
232 $pl->ob->message ("You got $xp_sum xp by making $nrof ${outarch}s"); 238 $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); 239 $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_SKILL_ONLY);
234 } 240 }
241 } else {
242 $pl->ob->message ("You fail to make something, propably you used not enough source material?");
235 } 243 }
236} 244}
237 245
238 246
239package Jeweler::CauldronHandler; 247package Jeweler::CauldronHandler;
320 cf::POTION => 'potions', 328 cf::POTION => 'potions',
321 cf::SCROLL => 'scrolls', 329 cf::SCROLL => 'scrolls',
322 ); 330 );
323 331
324 for ($self->{cauldron}->inv) { 332 for ($self->{cauldron}->inv) {
333 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) {
334 die "unidentified";
335 } elsif ($_->flag (cf::FLAG_CURSED) || $_->flag (cf::FLAG_DAMNED)) {
336 die "cursed";
337 }
325 338
326 if (my $k = $type_to_key{$_->type}) { 339 if (my $k = $type_to_key{$_->type}) {
327 push @{$ingreds->{$k}}, $_; 340 push @{$ingreds->{$k}}, $_;
341 } else {
342 push @{$ingreds->{other}}, $_;
328 } 343 }
329 } 344 }
330 345
331 return Jeweler::Ingredients->new (ingredients => $ingreds, cauldron_helper => $self) 346 return Jeweler::Ingredients->new (ingredients => $ingreds, cauldron_helper => $self)
332} 347}
427sub get_plan { 442sub get_plan {
428 my ($self) = @_; 443 my ($self) = @_;
429 444
430 my $ingred = $self->{ingredients}; 445 my $ingred = $self->{ingredients};
431 446
432 for my $grp (keys %$ingred) {
433 for my $pot (@{$ingred->{$grp}}) {
434 for my $plan (keys %{$Jeweler::CFG->{plans}}) { 447 for my $plan (keys %{$Jeweler::CFG->{plans}}) {
435 my $plg = $Jeweler::CFG->{plans}->{$plan}; 448 my $plg = $Jeweler::CFG->{plans}->{$plan};
436 my @plga = (); 449 my @plga = ();
437 unless (ref $plg eq 'ARRAY') { 450 unless (ref $plg eq 'ARRAY') {
438 push @plga, $plg; 451 push @plga, $plg;
439 } else { 452 } else {
440 @plga = @$plg; 453 @plga = @$plg;
441 } 454 }
442 next unless @plga > 0; 455 next unless @plga > 0;
443 if (Jeweler::Util::grep_for_match ($pot, @plga)) { 456 if (Jeweler::Util::grep_for_match ($ingred, @plga)) {
444 return $plan; 457 return $plan;
445 }
446 }
447 } 458 }
448 } 459 }
449} 460}
450 461
451sub get_ring { 462sub get_ring {
454 @{$self->{ingredients}->{ammys} || []}, 465 @{$self->{ingredients}->{ammys} || []},
455 @{$self->{ingredients}->{rings} || []} 466 @{$self->{ingredients}->{rings} || []}
456 ); 467 );
457} 468}
458 469
470sub improve_max {
471 my ($stat, $impro) = @_;
472 if ($stat >= 0) {
473 $stat = $impro > $stat ? $impro : $stat;
474 }
475 $stat
476}
477
459sub improve_ring_by_plan { 478sub improve_ring_by_plan {
460 my ($self, $plan, $ring) = @_; 479 my ($self, $plan, $ring) = @_;
461 480
462 $ring = dclone ($ring); 481 $ring = dclone ($ring);
463 482
468 my $statname = $1; 487 my $statname = $1;
469 my $plingred = Jeweler::getcfg (plans => $plan) 488 my $plingred = Jeweler::getcfg (plans => $plan)
470 or die "ingredients for plan '$plan' not defined!"; 489 or die "ingredients for plan '$plan' not defined!";
471 490
472 my $cnt = 0; 491 my $cnt = 0;
473 for my $pot (@{$ingred->{potions}}) {
474 if (Jeweler::Util::grep_for_match ($pot, @$plingred)) { 492 if (my $pot = Jeweler::Util::grep_for_match ($ingred, @$plingred)) {
475 $cnt += $pot->nrof; 493 $cnt += $pot->nrof;
476 }
477 } 494 }
478 495
479 my $maxstat = Jeweler::getcfg (maximprovements => 'stats'); 496 my $maxstat = Jeweler::getcfg (maximprovements => 'stats');
480 my $did_impr = 0;
481 for my $x (1..$maxstat) { 497 for my $x (1..$maxstat) {
482 my $y = Jeweler::Object::fx ($x, 'stat_potions'); 498 my $y = Jeweler::Object::fx ($x, 'stat_items');
483 499
484 if ($cnt <= $y->[0]) { 500 if ($cnt <= $y->[0]) {
485 $ring->{hash}->{stat}->{$statname} += $x; 501 $ring->{hash}->{stat}->{$statname} =
486 $did_impr = 1; 502 improve_max $ring->{hash}->{stat}->{$statname}, $x;
487 last; 503 last;
488 } 504 }
489 } 505 }
490 506
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+)$/) { 507 } elsif ($plan =~ m/^spec_(\S+)$/) {
508 my $specname = $1;
509 my $plingred = Jeweler::getcfg (plans => $plan)
510 or die "ingredients for plan '$plan' not defined!";
511
512 my $cnt = 0;
513 if (my $pot = Jeweler::Util::grep_for_match ($ingred, @$plingred)) {
514 $cnt += $pot->nrof;
515 }
516
517 my $maxspec = Jeweler::getcfg (maximprovements => 'specials');
518 for my $x (1..$maxspec) {
519 my $y = Jeweler::Object::fx ($x, 'spec_items');
520
521 if ($cnt <= $y->[0]) {
522 $ring->{hash}->{spec}->{$specname} =
523 improve_max $ring->{hash}->{spec}->{$specname}, $x;
524 last;
525 }
526 }
527
495 } elsif ($plan =~ m/^resist_(\S+)$/) { 528 } elsif ($plan =~ m/^resist_(\S+)$/) {
529 my $resname = $1;
530 my $resnum = $REV_RESMAP{$resname};
531 my $plingred = Jeweler::getcfg (plans => $plan)
532 or die "ingredients for plan '$plan' not defined!";
533
534 my $cnt = 0;
535 if (my $it = Jeweler::Util::grep_for_match ($ingred, @$plingred)) {
536 $cnt += $it->nrof;
537 }
538 my $resist_item_nr = 0;
539 $self->do_grep (sub { $resist_item_nr += ($_[0]->nrof || 1); 0 }, @$plingred);
540
541 my $maximprovname = (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS)
542 ? 'effect_resistances'
543 : 'attack_resistances';
544
545 my $maxres = Jeweler::getcfg (maximprovements => $maximprovname);
546 $resist_item_nr = $maxres if ($resist_item_nr > $maxres);
547 $ring->{hash}->{resist}->{$resnum} =
548 improve_max $ring->{hash}->{resist}->{$resnum}, $resist_item_nr;
496 } 549 }
497 550
498 return $ring; 551 return $ring;
499} 552}
500 553
501sub do_grep { 554sub do_grep {
502 my ($self, $cb, @grepar) = @_; 555 my ($self, $cb, $cat, @grepar) = @_;
503 556
504 my $ingred = $self->{ingredients}; 557 my $ingred = $self->{ingredients};
505 558
506
507 for my $cat (keys %$ingred) {
508 my @rem; 559 my @rem;
509 for my $ing (@{$ingred->{$cat}}) { 560 for my $ing (@{$ingred->{$cat}}) {
510 if (Jeweler::Util::grep_for_match ($ing, @grepar)) { 561 if (Jeweler::Util::check_for_match ($ing, @grepar)) {
511 unless ($cb->($ing)) { 562 unless ($cb->($ing)) {
512 push @rem, $ing;
513 }
514 } else {
515 push @rem, $ing; 563 push @rem, $ing;
516 } 564 }
565 } else {
566 push @rem, $ing;
517 } 567 }
568 }
518 @{$ingred->{$cat}} = @rem; 569 @{$ingred->{$cat}} = @rem;
519 }
520} 570}
521 571
522sub check_costs { 572sub check_costs {
523 my ($self, $costs, $do_remove) = @_; 573 my ($self, $costs, $do_remove) = @_;
524 574
525 my $costs = dclone ($costs); 575 my $costs = dclone ($costs);
526 576
527 for my $key (keys %$costs) { 577 for my $key (keys %$costs) {
528 my @grepar; 578 my @grepar;
529 if ($key =~ m/^stat_(\S+)$/) { 579 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items
530 @grepar = @{Jeweler::getcfg (plans => $key) || []}; 580 @grepar = @{Jeweler::getcfg (plans => $key) || []};
531 } else { 581 } else { # check the gems
532 @grepar = (undef, undef, $key); 582 @grepar = ('gems', undef, undef, $key);
533 } 583 }
534 584
535 if ($do_remove) { 585 if ($do_remove) {
536 my $rem = $costs->{$key}; 586 my $rem = $costs->{$key};
537 $self->do_grep (sub { if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); } 1 }, @grepar); 587 $self->do_grep (sub { if ($rem) { $rem = Jeweler::Util::remove ($_[0], $rem); } 1 }, @grepar);
538 if ($rem > 0) { 588 if ($rem > 0) {
539 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 589 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!";
540 } 590 }
541 } else { 591 } else {
542 my $nr; 592 my $nr;
543 $self->do_grep (sub { $nr += $_[0]->nrof; 0 }, @grepar); 593 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
544 $costs->{$key} -= $nr; 594 $costs->{$key} -= $nr;
545 } 595 }
596
546 } 597 }
547 598
548 return $costs; 599 return $costs;
549} 600}
550 601
598sub wiz_analyze { 649sub wiz_analyze {
599 my ($self, $pl) = @_; 650 my ($self, $pl) = @_;
600 my $costs = $self->calc_costs; 651 my $costs = $self->calc_costs;
601 my $desc = ""; 652 my $desc = "";
602 my $lvl = $self->power_to_level (\$desc); 653 my $lvl = $self->power_to_level (\$desc);
654 my $emarch = cf::arch::find 'emerald';
655 my $saarch = cf::arch::find 'sapphire';
656 my $pearch = cf::arch::find 'pearl';
657 my $ruarch = cf::arch::find 'ruby';
658 my $diarch = cf::arch::find 'gem';
659 my $scosts = $emarch->clone->value * $costs->{emerald}
660 + $saarch->clone->value * $costs->{sapphire}
661 + $pearch->clone->value * $costs->{pearl}
662 + $ruarch->clone->value * $costs->{ruby}
663 + $diarch->clone->value * $costs->{gem};
664
603 $pl->message ("costs: " . join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)); 665 $pl->message ("costs: " . (join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)) . " (".($scosts / 5000)." royalties)");
604 $pl->message ("level: " . $desc); 666 $pl->message ("level: " . $desc);
605} 667}
606 668
607 669
608sub get_chance_perc { 670sub get_chance_perc {
675 my ($self, $thing) = @_; 737 my ($self, $thing) = @_;
676 738
677 my $obj = {}; 739 my $obj = {};
678 740
679 for (@Jeweler::RESISTS) { 741 for (@Jeweler::RESISTS) {
680 $obj->{resist}->{$_} = $thing->resistance ($_); 742 $obj->{resist}->{$_} = $thing->resist ($_);
681 } 743 }
682 744
683 my $stats = $thing->stats; 745 my $stats = $thing->stats;
684 746
685 for (qw/Str Dex Con Wis Cha Int Pow/) { 747 for (qw/Str Dex Con Wis Cha Int Pow/) {
704sub to_object { 766sub to_object {
705 my ($self) = @_; 767 my ($self) = @_;
706 768
707 my $obj = cf::object::new $self->{hash}->{arch}; 769 my $obj = cf::object::new $self->{hash}->{arch};
708 770
771 $obj->item_power ($self->power_to_level); # there have to be strings attached!
772
709 $obj->face ($self->{hash}{face}); 773 $obj->face ($self->{hash}{face});
710 774
711 my $stats = $obj->stats; 775 my $stats = $obj->stats;
712 776
713 $stats->hp ($self->{hash}{spec}{regen}); 777 $stats->hp ($self->{hash}{spec}{regen});
720 784
721 $stats->$_ ($self->{hash}{stat}{lc $_}) 785 $stats->$_ ($self->{hash}{stat}{lc $_})
722 for qw/Str Dex Con Wis Cha Int Pow/; 786 for qw/Str Dex Con Wis Cha Int Pow/;
723 787
724 for (@Jeweler::RESISTS) { 788 for (@Jeweler::RESISTS) {
725 $obj->resistance ($_, $self->{hash}->{resist}->{$_}); 789 $obj->resist ($_, $self->{hash}->{resist}->{$_});
726 } 790 }
727 791
728 $obj->flag (cf::FLAG_IDENTIFIED, 1); 792 $obj->flag (cf::FLAG_IDENTIFIED, 1);
729 793
730 return $obj; 794 return $obj;
795}
796
797sub is_better_than {
798 my ($self, $other) = @_;
799
800 for my $type (qw/spec stat resist/) {
801 for my $stat (keys %{$self->{hash}->{$type}}) {
802 if ($self->{hash}->{$type}->{$stat} > $other->{hash}->{$type}->{$stat}) {
803 return 1;
804 }
805 }
806 }
807
808 return 0;
731} 809}
732 810
733sub stat_level { 811sub stat_level {
734 my ($self) = @_; 812 my ($self) = @_;
735 my $stats = $self->{hash}->{stat} || {}; 813 my $stats = $self->{hash}->{stat} || {};
849 for my $stat (keys %$stats) { 927 for my $stat (keys %$stats) {
850 my $sum = $stats->{$stat}; 928 my $sum = $stats->{$stat};
851 929
852 next unless $sum > 0; 930 next unless $sum > 0;
853 931
854 my $statfx = fx ($sum, 'stat_potions'); 932 my $statfx = fx ($sum, 'stat_items');
855 $cost->{"stat_$stat"} += $statfx->[0]; 933 $cost->{"stat_$stat"} += $statfx->[0];
856 split_diamonds ($cost, $statfx->[1], 'stat_' . $stat); 934 split_diamonds ($cost, $statfx->[1], 'stat_' . $stat);
857 } 935 }
858} 936}
859 937
865 for my $spec (keys %$specials) { 943 for my $spec (keys %$specials) {
866 my $sum = $specials->{$spec}; 944 my $sum = $specials->{$spec};
867 945
868 next unless $sum > 0; 946 next unless $sum > 0;
869 947
870 my $specfx = fx ($sum, 'spec_potions'); 948 my $specfx = fx ($sum, 'spec_items');
871 $cost->{"spec_$spec"} += $specfx->[0]; 949 $cost->{"spec_$spec"} += $specfx->[0];
872 split_diamonds ($cost, $specfx->[1], 'spec_' . $spec); 950 split_diamonds ($cost, $specfx->[1], 'spec_' . $spec);
873 } 951 }
874} 952}
875 953
878 956
879 my $costs = {}; 957 my $costs = {};
880 958
881 my $ring = $self->{hash}; 959 my $ring = $self->{hash};
882 960
883 for my $resnam (keys %{$ring->{resist} || {}}) { 961 for my $resnum (keys %{$ring->{resist} || {}}) {
884 962
885 my $res = $ring->{resist}->{$resnam}; 963 my $res = $ring->{resist}->{$resnum};
886 964
887 next unless $res > 0; 965 next unless $res > 0;
888 966
889 $costs->{"resist_" . $Jeweler::RESMAP{$resnam} . "_item"} += $res; 967 $costs->{"resist_" . $Jeweler::RESMAP{$resnum}} += $res;
890 968
891 my $diamonds; 969 my $diamonds;
892 if (grep { $resnam eq $_ } @Jeweler::EFFECT_RESISTS) { 970 if (grep { $resnum eq $_ } @Jeweler::EFFECT_RESISTS) {
893 $diamonds += fx ($res, 'effect_resist_diamonds'); 971 $diamonds += fx ($res, 'effect_resist_diamonds');
894 } else { 972 } else {
895 $diamonds += fx ($res, 'attack_resist_diamonds'); 973 $diamonds += fx ($res, 'attack_resist_diamonds');
896 } 974 }
897 975
898 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnam}); 976 split_diamonds ($costs, $diamonds, 'resist_' . $Jeweler::RESMAP{$resnum});
899 } 977 }
900 978
901 $self->add_stat_costs ($costs); 979 $self->add_stat_costs ($costs);
902 $self->add_special_costs ($costs); 980 $self->add_special_costs ($costs);
903 981
950 my ($obj, $nrof) = @_; 1028 my ($obj, $nrof) = @_;
951 1029
952 my $cnt; 1030 my $cnt;
953 1031
954 if (defined $nrof) { 1032 if (defined $nrof) {
1033 # TODO: Check tihis line:
955 return 0 if ($nrof * 1) == 0; 1034 return 0 if ($nrof * 1) == 0; #XXX: ???
956 $cnt = int (($obj->nrof || 1) - (1 * $nrof)); 1035 $cnt = int (($obj->nrof || 1) - (1 * $nrof));
957 1036
958 if ($cnt > 0) { 1037 if ($cnt > 0) {
959 $obj->nrof ($cnt); 1038 $obj->nrof ($cnt);
960 return 0; 1039 return 0;
964 remove ($_) for $obj->inv; 1043 remove ($_) for $obj->inv;
965 $obj->destroy; 1044 $obj->destroy;
966 return $cnt; 1045 return $cnt;
967} 1046}
968 1047
969sub grep_for_match { 1048sub check_for_match {
970 my ($thing, @matchar) = @_; 1049 my ($thing, @matchar) = @_;
971 1050
972 my $i = 0; 1051 my $i = 0;
973 for my $match (@matchar) { 1052 for my $match (@matchar) {
974 if ($match =~ m/^\s*$/) { 1053 if ($match =~ m/^\s*$/) {
989 $i++; 1068 $i++;
990 } 1069 }
991 return 0; 1070 return 0;
992} 1071}
993 1072
1073sub grep_for_match {
1074 my ($ingred, $group, @matchar) = @_;
1075
1076 for my $thing (@{$ingred->{$group} || []}) {
1077 warn sprintf "DEB:(%s,%s,%s)<->%s\n", $thing->name, $thing->title, $thing->arch->name, "@matchar"; #d#
1078 if (check_for_match ($thing, @matchar)) {
1079 return $thing;
1080 }
1081 }
1082 return undef;
1083}
1084
994=back 1085=back
995 1086
996=back
997
9981 10871

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines