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.22 by root, Sun Oct 14 20:23:48 2007 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;
165 161
166sub analyze { 162sub analyze {
167 my ($sk, $chdl, $pl, $input_level) = @_; 163 my ($sk, $chdl, $pl, $input_level) = @_;
168 164
169 my $hadunid = 0; 165 my $hadunid = 0;
166 my $found = 0;
170 for ($chdl->grep_by_type (cf::RING, cf::AMULET)) { 167 for ($chdl->grep_by_type (cf::RING, cf::AMULET)) {
171 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) { 168 if (!$_->flag (cf::FLAG_IDENTIFIED) && $_->need_identify) {
172 $hadunid = 1; 169 $hadunid = 1;
173 next; 170 next;
174 } 171 }
172 $found = 1;
175 my $r = Jeweler::Object->new (object => $_); 173 my $r = Jeweler::Object->new (object => $_);
176 my $msg = $r->analyze ($sk, $pl, $input_level); 174 my $msg = $r->analyze ($sk, $pl, $input_level);
177 $pl->message ($r->to_string . ": " . $msg); 175 $pl->message ($r->to_string . ": " . $msg);
178 if ($pl->flag (cf::FLAG_WIZ)) { 176 if ($pl->flag (cf::FLAG_WIZ)) {
179 $r->wiz_analyze ($pl); 177 $r->wiz_analyze ($pl);
180 } 178 }
181 } 179 }
182 $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!")
183 if $hadunid; 181 if $hadunid;
182 $pl->message ("You couldn't find anything in the bench to analyse!")
183 unless $found;
184} 184}
185 185
186# this function converts metals/minerals into a raw ring (of adornment) 186# this function converts metals/minerals into a raw ring (of adornment)
187sub simple_converter { 187sub simple_converter {
188 my ($pl, $ingred, $chdl, $conv) = @_; 188 my ($pl, $ingred, $chdl, $conv, $sk_lvl, $low_skill_cb) = @_;
189 189
190 $conv = lc $conv; 190 $conv = lc $conv;
191 my $cnvs = $CFG->{conversions}; 191 my $cnvs = $CFG->{conversions};
192 192
193 return unless $cnvs->{$conv}; 193 return unless $cnvs->{$conv};
215 215
216 unless ($outarchvalfact >= 1) { 216 unless ($outarchvalfact >= 1) {
217 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";
218 } 218 }
219 219
220 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname); 220 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname);
221 $ingred->remove ($ingr_grp, $srcarchname);
222
223 my $outarchval = cf::arch::find ($outarch)->value; 221 my $outarchval = cf::arch::find ($outarch)->value;
224
225 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
226 if ($nrof) { 230 if ($nrof) {
227 # XXX: yes, I know what I'm doing here, I don't set nrof, but it didn't work somehow (pls. check 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);
228 for (1 .. $nrof) { 233 for (1 .. $nrof) {
229 $chdl->put (my $ob = cf::object::new $outarch); 234 $chdl->put (my $ob = cf::object::new $outarch);
230 $ob->set_animation (cf::rndm $ob->num_animations) 235 $ob->set_animation (cf::rndm $ob->num_animations)
231 if ($ob->type == cf::RING); 236 if ($ob->type == cf::RING);
232 $ob->flag (cf::FLAG_IDENTIFIED, 1); 237 $ob->flag (cf::FLAG_IDENTIFIED, 1);
244} 249}
245 250
246 251
247package Jeweler::CauldronHandler; 252package Jeweler::CauldronHandler;
248 253
249use strict; 254use common::sense;
250 255
251=head2 CauldronHandler 256=head2 CauldronHandler
252 257
253The Jeweler::CauldronHandler package, that helps you with handling the 258The Jeweler::CauldronHandler package, that helps you with handling the
254cauldron stuff. Can also be used for other skills. 259cauldron stuff. Can also be used for other skills.
362=back 367=back
363 368
364=cut 369=cut
365 370
366package Jeweler::Ingredients; 371package Jeweler::Ingredients;
372
373use common::sense;
374
367use Storable qw/dclone/; 375use Storable qw/dclone/;
368use strict;
369 376
370=head2 Ingredients 377=head2 Ingredients
371 378
372This class handles the ingredients. 379This class handles the ingredients.
373 380
476} 483}
477 484
478sub improve_ring_by_plan { 485sub improve_ring_by_plan {
479 my ($self, $plan, $ring) = @_; 486 my ($self, $plan, $ring) = @_;
480 487
481 $ring = do { my $guard = Coro::Storable::guard; dclone $ring }; 488 $ring = dclone $ring;
482 489
483 my $ingred = $self->{ingredients}; 490 my $ingred = $self->{ingredients};
484 my $impr = {}; 491 my $impr = {};
485 492
486 if ($plan =~ m/^stat_(\S+)$/) { 493 if ($plan =~ m/^stat_(\S+)$/) {
570} 577}
571 578
572sub check_costs { 579sub check_costs {
573 my ($self, $costs, $do_remove) = @_; 580 my ($self, $costs, $do_remove) = @_;
574 581
575 my $costs = do { my $guard = Coro::Storable::guard; dclone $costs }; 582 my $costs = dclone $costs;
576 583
577 for my $key (keys %$costs) { 584 for my $key (keys %$costs) {
578 my @grepar; 585 my @grepar;
579 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items 586 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items
580 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} }; 587 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} };
583 @grepar = ('gems', undef, undef, $key); 590 @grepar = ('gems', undef, undef, $key);
584 } 591 }
585 592
586 if ($do_remove) { 593 if ($do_remove) {
587 my $rem = $costs->{$key}; 594 my $rem = $costs->{$key};
588 $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);
589 if ($rem > 0) { 601 if ($rem > 0) {
590 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 602 warn "JEWELER BUG: removed ingredients ($key) $rem > 0 after removing!";
591 } 603 }
604
592 } else { 605 } else {
593 my $nr; 606 my $nr;
594 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar); 607 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
595 $costs->{$key} -= $nr; 608 $costs->{$key} -= $nr;
596 } 609 }
615 } 628 }
616 } 629 }
617} 630}
618 631
619package Jeweler::Object; 632package Jeweler::Object;
620use strict; 633
634use common::sense;
621use POSIX; 635use POSIX;
622use List::Util qw/max min sum/; 636use List::Util qw/max min sum/;
623 637
624sub new { 638sub new {
625 my ($class, %arg) = @_; 639 my ($class, %arg) = @_;
640 return 1 if $self->{hash}->{resist}->{$resnum}; 654 return 1 if $self->{hash}->{resist}->{$resnum};
641 } 655 }
642 return undef; 656 return undef;
643} 657}
644 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
645sub projected_exp { 666sub projected_exp {
646 my ($self, $input_level) = @_; 667 my ($self, $input_level) = @_;
647 668
648 my $lvl = max ($self->power_to_level, 1); 669 my $lvl = max ($self->power_to_level, 1);
649 my $exp = 670 my $exp = lvl2exp ($lvl);
650 (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1))
651 / (10 + max ($lvl - 1, 0)); # 10 + level times making such a ring
652 # should get you to the rings level at least.
653 671
654 if (defined $input_level) { 672 if (defined $input_level) { # in case we merge rings:
655 my $subexp = 673 my $subexp = lvl2exp ($input_level);
656 (cf::level_to_min_exp ($input_level)
657 - cf::level_to_min_exp ($input_level - 1))
658 / (10 + max ($input_level - 1, 0)); # see above for comment
659
660 $exp -= $subexp; 674 $exp -= $subexp;
661 $exp = max ($exp, 0); 675 $exp = max ($exp, 0);
662 676
663 } else { 677 } else {
664 # the experience bonus here is to make level 1 rings give you at least 678 # the experience bonus here is to make level 1 rings give you at least
665 # 100 exp points when making them. This also makes leveling in the 679 # 200 exp points when making them. This also makes leveling in the
666 # first few levels a bit easier. (probably until around level 5-6). 680 # first few levels a bit easier. (probably until around level 5-6).
667 my $expbonus = cf::level_to_min_exp (2) / 10; 681 my $expbonus = cf::level_to_min_exp (2) / 5;
668 # this bonus should also only be given for _new_ rings and not for merged 682 # this bonus should also only be given for _new_ rings and not for merged
669 # ones - to prevent infinite exp making. 683 # ones - to prevent infinite exp making.
670 $exp += $expbonus; 684 $exp += $expbonus;
671 } 685 }
672 686
716 if (defined $costs) { 730 if (defined $costs) {
717 my $desc = ""; 731 my $desc = "";
718 my $lvl = $self->power_to_level (\$desc); 732 my $lvl = $self->power_to_level (\$desc);
719 my $scosts = $self->calc_value_from_cost ($costs); 733 my $scosts = $self->calc_value_from_cost ($costs);
720 734
721 $pl->message ("costs: " . (join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)) . " (".($scosts / 5000)." royalties)"); 735 $pl->message ("costs: "
736 . (join ', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)
737 . " ("
738 . ($scosts / "platinacoin"->cf::arch::find->value)
739 . " platinum)");
722 $pl->message ("level: " . $desc); 740 $pl->message ("level: $desc");
723 } else { 741 } else {
724 $pl->message ("level: impossible to make, due to impossible resistancy configuration"); 742 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
725 } 743 }
726} 744}
727 745
830 $obj->{arch} = $thing->arch->archname; 848 $obj->{arch} = $thing->arch->archname;
831 $obj->{face} = $thing->face; 849 $obj->{face} = $thing->face;
832 850
833 $obj->{value} = $thing->value; 851 $obj->{value} = $thing->value;
834 852
853 $obj->{is_ring} = ($thing->type == cf::RING);
854
835 $self->{hash} = $obj 855 $self->{hash} = $obj
836} 856}
837 857
838sub to_object { 858sub to_object {
839 my ($self) = @_; 859 my ($self) = @_;
840 860
841 my $obj = cf::object::new $self->{hash}->{arch}; 861 my $obj = cf::object::new $self->{hash}->{arch};
842 862
843 $obj->item_power (floor ($self->power_to_level / 3)); # there have to be strings attached! 863 $obj->item_power (floor ($self->power_to_level / 5)); # there have to be strings attached!
844 864
845 $obj->face ($self->{hash}{face}); 865 $obj->face ($self->{hash}{face});
846 866
847 my $stats = $obj->stats; 867 my $stats = $obj->stats;
848 868
901sub resist_level { 921sub resist_level {
902 my ($self) = @_; 922 my ($self) = @_;
903 923
904 my $resists = $self->{hash}->{resist} || {}; 924 my $resists = $self->{hash}->{resist} || {};
905 925
906 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level'); 926 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level');
907 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level'); 927 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level');
908 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances'); 928 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances');
909 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances'); 929 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances');
910 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances'); 930 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances');
911 931
912 my $ressum = 0; 932 my $ressum = 0;
972sub power_to_level { 992sub power_to_level {
973 my ($self, $lvldescr) = @_; 993 my ($self, $lvldescr) = @_;
974 994
975 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements'); 995 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements');
976 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');
977 998
978 my ($stat_lvl, $stat_imprs) = $self->stat_level; 999 my ($stat_lvl, $stat_imprs) = $self->stat_level;
979 my ($resist_lvl, $res_imprs) = $self->resist_level; 1000 my ($resist_lvl, $res_imprs) = $self->resist_level;
980 my ($spec_lvl, $spec_imprs) = $self->special_level; 1001 my ($spec_lvl, $spec_imprs) = $self->special_level;
981 1002
982 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs; 1003 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs;
983 1004
984 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
985 1008
986 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);
987 1016
988 if ($lvldescr) { 1017 if ($lvldescr) {
989 $$lvldescr = 1018 $$lvldescr =
990 sprintf "%3d: %s\n", $levl, 1019 sprintf "%3d: %s\n", $levl,
991 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, " 1020 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, "
1082 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value; 1111 $cost->{gem} += ceil $sumvalue * $stat_split->[4] / max 1, $diarch->value;
1083} 1112}
1084 1113
1085package Jeweler::Util; 1114package Jeweler::Util;
1086 1115
1087use strict; 1116use common::sense;
1088 1117
1089=head2 Util 1118=head2 Util
1090 1119
1091Some utility functions for the Jeweler skill. 1120Some utility functions for the Jeweler skill.
1092 1121
1093=over 4 1122=over 4
1094 1123
1095=item remove ($object[, $nrof]) 1124=item remove ($object[, $nrof])
1096 1125
1097Removes 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.
1098The 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.
1099 1128
1100=cut 1129=cut
1101 1130
1102sub remove { 1131sub remove {
1103 my ($obj, $nrof) = @_; 1132 my ($obj, $nrof) = @_;
1104 1133
1105 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));
1106 1137
1107 if (defined $nrof) { 1138 $r
1108 # TODO: Check tihis line:
1109 return 0 if ($nrof * 1) == 0; #XXX: ???
1110 $cnt = int (($obj->nrof || 1) - (1 * $nrof));
1111
1112 if ($cnt > 0) {
1113 $obj->nrof ($cnt);
1114 return 0;
1115 }
1116 }
1117
1118 remove ($_) for $obj->inv;
1119 $obj->destroy;
1120 return $cnt;
1121} 1139}
1122 1140
1123sub check_for_match { 1141sub check_for_match {
1124 my ($thing, @matchar) = @_; 1142 my ($thing, @matchar) = @_;
1125 1143

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines