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.18 by elmex, Tue Jul 31 09:40:15 2007 UTC vs.
Revision 1.34 by root, Wed Apr 28 21:07:41 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 warn "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 if ($hadunid) {
183 $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!")
184 } 181 if $hadunid;
182 $pl->message ("You couldn't find anything in the bench to analyse!")
183 unless $found;
185} 184}
186 185
187# this function converts metals/minerals into a raw ring (of adornment) 186# this function converts metals/minerals into a raw ring (of adornment)
188sub simple_converter { 187sub simple_converter {
189 my ($pl, $ingred, $chdl, $conv) = @_; 188 my ($pl, $ingred, $chdl, $conv, $sk_lvl, $low_skill_cb) = @_;
190 189
191 $conv = lc $conv; 190 $conv = lc $conv;
192 my $cnvs = $CFG->{conversions}; 191 my $cnvs = $CFG->{conversions};
193 192
194 return unless $cnvs->{$conv}; 193 return unless $cnvs->{$conv};
203 warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!"; 202 warn "ERROR: Conversion for '$outarch' has only " . (@conv_cfg) . " arguments!";
204 return; 203 return;
205 } 204 }
206 205
207 unless ($xp_gain > 0) { 206 unless ($xp_gain > 0) {
208 warn "WARNING: xp gain isn't > 0 in convesion '$outarch'\n"; 207 warn "WARNING: xp gain isn't > 0 in conversion '$outarch'\n";
209 return; 208 return;
210 } 209 }
211 210
212 unless ($outarchvalfact) { 211 unless ($outarchvalfact) {
213 warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n"; 212 warn "ERROR: source-arch-value-multiplier == 0 in conversion '$outarch'\n";
216 215
217 unless ($outarchvalfact >= 1) { 216 unless ($outarchvalfact >= 1) {
218 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";
219 } 218 }
220 219
221 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname); 220 my $archvalsum = $ingred->value ($ingr_grp, $srcarchname);
222 $ingred->remove ($ingr_grp, $srcarchname);
223
224 my $outarchval = cf::arch::find ($outarch)->value; 221 my $outarchval = cf::arch::find ($outarch)->value;
225
226 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
227 if ($nrof) { 230 if ($nrof) {
228 # 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);
229 for (1..$nrof) { 233 for (1 .. $nrof) {
230 $chdl->put (my $ob = cf::object::new $outarch); 234 $chdl->put (my $ob = cf::object::new $outarch);
231 $ob->set_animation (cf::rndm $ob->num_animations) 235 $ob->set_animation (cf::rndm $ob->num_animations)
232 if ($ob->type == cf::RING); 236 if ($ob->type == cf::RING);
233 $ob->flag (cf::FLAG_IDENTIFIED, 1); 237 $ob->flag (cf::FLAG_IDENTIFIED, 1);
234 } 238 }
235 239
236 my $xp_sum = ($xp_gain * $nrof); 240 my $xp_sum = $xp_gain * $nrof;
237 241
238 if ($xp_sum) { 242 if ($xp_sum) {
239 $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");
240 $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_SKILL_ONLY); 244 $pl->ob->change_exp ($xp_sum, "jeweler", cf::SK_EXP_SKILL_ONLY);
241 } 245 }
355 359
356sub put { 360sub put {
357 my ($self, $obj) = @_; 361 my ($self, $obj) = @_;
358 362
359 return undef unless $self->{cauldron}; 363 return undef unless $self->{cauldron};
360 $obj->insert_ob_in_ob ($self->{cauldron}); 364 $self->{cauldron}->insert ($obj);
361} 365}
362 366
363=back 367=back
364 368
365=cut 369=cut
477} 481}
478 482
479sub improve_ring_by_plan { 483sub improve_ring_by_plan {
480 my ($self, $plan, $ring) = @_; 484 my ($self, $plan, $ring) = @_;
481 485
482 $ring = dclone ($ring); 486 $ring = dclone $ring;
483 487
484 my $ingred = $self->{ingredients}; 488 my $ingred = $self->{ingredients};
485 my $impr = {}; 489 my $impr = {};
486 490
487 if ($plan =~ m/^stat_(\S+)$/) { 491 if ($plan =~ m/^stat_(\S+)$/) {
571} 575}
572 576
573sub check_costs { 577sub check_costs {
574 my ($self, $costs, $do_remove) = @_; 578 my ($self, $costs, $do_remove) = @_;
575 579
576 my $costs = dclone ($costs); 580 my $costs = dclone $costs;
577 581
578 for my $key (keys %$costs) { 582 for my $key (keys %$costs) {
579 my @grepar; 583 my @grepar;
580 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items 584 if ($key =~ m/^(resist_|spec_|stat_)/) { # check the special items
581 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} }; 585 eval { @grepar = @{Jeweler::getcfg (plans => $key) || []} };
584 @grepar = ('gems', undef, undef, $key); 588 @grepar = ('gems', undef, undef, $key);
585 } 589 }
586 590
587 if ($do_remove) { 591 if ($do_remove) {
588 my $rem = $costs->{$key}; 592 my $rem = $costs->{$key};
593 $self->do_grep (sub {
589 $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);
590 if ($rem > 0) { 597 if ($rem > 0) {
591 warn "JEWELER BUG: removed ingredients $rem > 0 after removing!"; 598 warn "JEWELER BUG: removed ingredients ($key) $rem > 0 after removing!";
592 } 599 }
600
593 } else { 601 } else {
594 my $nr; 602 my $nr;
595 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar); 603 $self->do_grep (sub { $nr += ($_[0]->nrof || 1); 0 }, @grepar);
596 $costs->{$key} -= $nr; 604 $costs->{$key} -= $nr;
597 } 605 }
661 $exp -= $subexp; 669 $exp -= $subexp;
662 $exp = max ($exp, 0); 670 $exp = max ($exp, 0);
663 671
664 } else { 672 } else {
665 # the experience bonus here is to make level 1 rings give you at least 673 # the experience bonus here is to make level 1 rings give you at least
666 # 100 exp points when making them. This also makes leveling in the 674 # 200 exp points when making them. This also makes leveling in the
667 # first few levels a bit easier. (probably until around level 5-6). 675 # first few levels a bit easier. (probably until around level 5-6).
668 my $expbonus = cf::level_to_min_exp (2) / 10; 676 my $expbonus = cf::level_to_min_exp (2) / 5;
669 # this bonus should also only be given for _new_ rings and not for merged 677 # this bonus should also only be given for _new_ rings and not for merged
670 # ones - to prevent infinite exp making. 678 # ones - to prevent infinite exp making.
671 $exp += $expbonus; 679 $exp += $expbonus;
672 } 680 }
673 681
717 if (defined $costs) { 725 if (defined $costs) {
718 my $desc = ""; 726 my $desc = "";
719 my $lvl = $self->power_to_level (\$desc); 727 my $lvl = $self->power_to_level (\$desc);
720 my $scosts = $self->calc_value_from_cost ($costs); 728 my $scosts = $self->calc_value_from_cost ($costs);
721 729
722 $pl->message ("costs: " . (join (', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)) . " (".($scosts / 5000)." royalties)"); 730 $pl->message ("costs: "
731 . (join ', ', map { "$_: " . sprintf "%.2f", $costs->{$_} } keys %$costs)
732 . " ("
733 . ($scosts / "platinacoin"->cf::arch::find->value)
734 . " platinum)");
723 $pl->message ("level: " . $desc); 735 $pl->message ("level: $desc");
724 } else { 736 } else {
725 $pl->message ("level: impossible to make, due to impossible resistancy configuration"); 737 $pl->message ("level: impossible to make, due to impossible resistancy configuration");
726 } 738 }
727} 739}
728 740
831 $obj->{arch} = $thing->arch->archname; 843 $obj->{arch} = $thing->arch->archname;
832 $obj->{face} = $thing->face; 844 $obj->{face} = $thing->face;
833 845
834 $obj->{value} = $thing->value; 846 $obj->{value} = $thing->value;
835 847
848 $obj->{is_ring} = ($thing->type == cf::RING);
849
836 $self->{hash} = $obj 850 $self->{hash} = $obj
837} 851}
838 852
839sub to_object { 853sub to_object {
840 my ($self) = @_; 854 my ($self) = @_;
902sub resist_level { 916sub resist_level {
903 my ($self) = @_; 917 my ($self) = @_;
904 918
905 my $resists = $self->{hash}->{resist} || {}; 919 my $resists = $self->{hash}->{resist} || {};
906 920
907 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level'); 921 my $att_res_lvl = Jeweler::getcfg (maxlevels => 'resist_level');
908 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level'); 922 my $efc_res_lvl = Jeweler::getcfg (maxlevels => 'effect_resist_level');
909 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances'); 923 my $max_att_res = Jeweler::getcfg (maximprovements => 'attack_resistances');
910 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances'); 924 my $max_efc_res = Jeweler::getcfg (maximprovements => 'effect_resistances');
911 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances'); 925 my $max_ovr_res = Jeweler::getcfg (maximprovements => 'resistances');
912 926
913 my $ressum = 0; 927 my $ressum = 0;
973sub power_to_level { 987sub power_to_level {
974 my ($self, $lvldescr) = @_; 988 my ($self, $lvldescr) = @_;
975 989
976 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements'); 990 my $max_imprs = Jeweler::getcfg (maximprovements => 'improvements');
977 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');
978 993
979 my ($stat_lvl, $stat_imprs) = $self->stat_level; 994 my ($stat_lvl, $stat_imprs) = $self->stat_level;
980 my ($resist_lvl, $res_imprs) = $self->resist_level; 995 my ($resist_lvl, $res_imprs) = $self->resist_level;
981 my ($spec_lvl, $spec_imprs) = $self->special_level; 996 my ($spec_lvl, $spec_imprs) = $self->special_level;
982 997
983 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs; 998 my $impr_sum = $stat_imprs + $res_imprs + $spec_imprs;
984 999
985 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
986 1003
987 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);
988 1011
989 if ($lvldescr) { 1012 if ($lvldescr) {
990 $$lvldescr = 1013 $$lvldescr =
991 sprintf "%3d: %s\n", $levl, 1014 sprintf "%3d: %s\n", $levl,
992 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, " 1015 "stat: $stat_lvl, resist: $resist_lvl, improve: $impr_lvl, "
1065 my ($cost, $diamonds, $category) = @_; 1088 my ($cost, $diamonds, $category) = @_;
1066 1089
1067 my $stat_split = Jeweler::getcfg (diamond_split => $category); 1090 my $stat_split = Jeweler::getcfg (diamond_split => $category);
1068 1091
1069 my $sum = sum (@$stat_split); 1092 my $sum = sum (@$stat_split);
1070 if ($sum < (1 - 0.0001)) {
1071 warn "JEWELER BUG: sum (@$stat_split) = $sum < 1 for $category!";
1072 }
1073 1093
1074 my $emarch = cf::arch::find 'emerald'; 1094 my $emarch = cf::arch::find 'emerald';
1075 my $saarch = cf::arch::find 'sapphire'; 1095 my $saarch = cf::arch::find 'sapphire';
1076 my $pearch = cf::arch::find 'pearl'; 1096 my $pearch = cf::arch::find 'pearl';
1077 my $ruarch = cf::arch::find 'ruby'; 1097 my $ruarch = cf::arch::find 'ruby';
1097=over 4 1117=over 4
1098 1118
1099=item remove ($object[, $nrof]) 1119=item remove ($object[, $nrof])
1100 1120
1101Removes 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.
1102The 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.
1103 1123
1104=cut 1124=cut
1105 1125
1106sub remove { 1126sub remove {
1107 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));
1108 1132
1109 my $cnt; 1133 $r
1110
1111 if (defined $nrof) {
1112 # TODO: Check tihis line:
1113 return 0 if ($nrof * 1) == 0; #XXX: ???
1114 $cnt = int (($obj->nrof || 1) - (1 * $nrof));
1115
1116 if ($cnt > 0) {
1117 $obj->nrof ($cnt);
1118 return 0;
1119 }
1120 }
1121
1122 remove ($_) for $obj->inv;
1123 $obj->destroy;
1124 return $cnt;
1125} 1134}
1126 1135
1127sub check_for_match { 1136sub check_for_match {
1128 my ($thing, @matchar) = @_; 1137 my ($thing, @matchar) = @_;
1129 1138

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines