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

Comparing deliantra/server/ext/jeweler.ext (file contents):
Revision 1.2 by root, Thu Dec 21 22:41:34 2006 UTC vs.
Revision 1.17 by elmex, Mon Aug 17 08:50:54 2009 UTC

1#! perl 1#! perl
2 2
3use Data::Dumper; 3use strict;
4
4use Jeweler; 5use Jeweler;
5use List::Util qw/max min sum/; 6use List::Util qw/max min sum/;
6use strict;
7 7
8sub ingred_alias { 8sub ingred_alias {
9 my ($ing) = @_; 9 my ($ing) = @_;
10 10
11 my %aliases = ( 11 my %aliases = (
14 wis => 'wisdom', 14 wis => 'wisdom',
15 int => 'intelligence', 15 int => 'intelligence',
16 dex => 'dexterity', 16 dex => 'dexterity',
17 con => 'constitution', 17 con => 'constitution',
18 str => 'strength', 18 str => 'strength',
19 gem => 'diamond',
19 ); 20 );
20 21
21 if ($ing =~ m/resist_(\S+)/) { 22 if ($ing =~ m/resist_(\S+)/) {
22 my $a = $aliases{lc $1} || $1; 23 my $a = $aliases{lc $1} || $1;
23 "something for '". lc ($a). "' resistance"; 24 "something for '". lc ($a). "' resistance";
28 29
29 } elsif ($ing =~ m/spec_(\S+)/) { 30 } elsif ($ing =~ m/spec_(\S+)/) {
30 my $a = $aliases{lc $1} || $1; 31 my $a = $aliases{lc $1} || $1;
31 "something for the ". lc ($a). "' special"; 32 "something for the ". lc ($a). "' special";
32 33
34 } elsif ($aliases{$ing}) {
35 $aliases{$ing}
36
33 } else { 37 } else {
34 $ing 38 $ing
35 } 39 }
36} 40}
37 41
38my $DEBUG = 1; 42my $DEBUG = 1;
39 43
40sub merge { 44sub merge {
41 my ($chdl, $sk, $pl, $do_analyze) = @_; 45 my ($chdl, $sk, $pl, $do_analyze) = @_;
42 46
43 my $ingred = $chdl->extract_jeweler_ingredients; 47 my $ingred = get_ingred ($pl, $chdl) || return;
48
44 my @ring = $ingred->get_ring; 49 my @ring = $ingred->get_ring;
45 my @rings = map { Jeweler::Object->new (object => $_) } @ring; 50 my @rings = map { Jeweler::Object->new (object => $_) } @ring;
46 51
47 @rings >= 2 52 @rings >= 2
48 or return $pl->reply (undef, "You slap yourself, you forgot to put at least 2 jewels in!"); 53 or return $pl->message ("You slap yourself, you forgot to put at least 2 jewels in!");
54
55 my $input_level = 0;
56 my $value;
57 for (@rings) {
58 $input_level = max ($_->power_to_level, $input_level);
59 $value += $_->{hash}->{value};
60 }
49 61
50 my $ring = shift @rings; 62 my $ring = shift @rings;
51 $ring->improve_by_ring (@rings); 63 $ring->improve_by_ring (@rings);
52 64
53 if ($do_analyze) { 65 if ($do_analyze) {
54 $pl->reply (undef, "You want to make a " . $ring->to_string . ": " . $ring->analyze ($sk, $pl)); 66 $pl->message ("You want to make a " . $ring->to_string . ": " . $ring->analyze ($sk, $pl, $input_level));
55 $ring->wiz_analyze ($pl) 67 $ring->wiz_analyze ($pl)
56 if $pl->flag (cf::FLAG_WIZ); 68 if $pl->flag (cf::FLAG_WIZ);
57 return; 69 return;
58 } 70 }
59 71
60 make_ring ($chdl, $ingred, $ring, $sk, $pl); 72 make_ring ($chdl, $ingred, $ring, $value, $sk, $pl, $input_level);
61} 73}
62 74
63sub make_ring { 75sub make_ring {
64 my ($chdl, $ingred, $ring, $sk, $pl) = @_; 76 my ($chdl, $ingred, $ring, $value, $sk, $pl, $input_level) = @_;
65 77
66 if (!$pl->flag (cf::FLAG_WIZ)) { 78 if (!$pl->flag (cf::FLAG_WIZ)) {
67 $ingred->remove ('rings'); 79 $ingred->remove ('rings');
68 $ingred->remove ('ammys'); 80 $ingred->remove ('ammys');
69 } 81 }
70 82
71 my $ch = $ring->get_chance_perc ($sk); 83 my $ch = $ring->get_chance_perc ($sk);
72 my $succ = 0; 84 my $succ = 0;
73 my $r = cf::random_roll (0, 100, $pl, cf::PREFER_HIGH); 85 my $r = cf::random_roll (0, 100, $pl, cf::PREFER_HIGH);
86
87 my $make_status;
88 my $exp;
89
74 if ($r <= $ch or $pl->flag (cf::FLAG_WIZ)) { 90 if ($r <= $ch or $pl->flag (cf::FLAG_WIZ)) {
75 my $lvl = max ($ring->power_to_level, 1); 91 $exp = $ring->projected_exp ($input_level);
76 my $exp = (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1)) / 100; 92
77 $pl->change_exp ($exp, "jeweler", cf::SK_EXP_SKILL_ONLY); 93 $pl->change_exp ($exp, "jeweler", cf::SK_EXP_SKILL_ONLY);
78 $pl->message ("You succeed and get $exp experience."); 94 $pl->message ("You succeed and get $exp experience points.");
95 $make_status = "succeeded";
96
97 $ring->set_value ($value * 0.8); # 20% of the input values will vanish
98
79 } else { 99 } else {
80 $pl->message ("You fail!"); 100 $pl->message ("You fail!");
81 $ring->negate; 101 $ring->negate;
102 $make_status = "fail";
103 $exp = 0;
104 }
105
106 my $ring_ob = $ring->to_object;
107
108 { # some audit info calculation
109 my $sklvl = cf::exp_to_level ($sk->stats->exp);
110
111 my $make_info = sprintf
112 "JEWELER AUDIT: '%s' made '%s' (%s) (sk lvl %d, ring lvl %d, got %d exp): %s",
113 $pl->name, $ring->to_string, $ring_ob->uuid, $sklvl,
114 $ring->power_to_level, $exp, $make_status;
115
116 warn "$make_info\n" if $make_status eq 'succeeded';
117 }
118
119 $chdl->put ($ring_ob);
120}
121
122sub get_ingred {
123 my ($pl, $chdl) = @_;
124 my $ingred = eval { $chdl->extract_jeweler_ingredients };
125 if ($@ =~ /cursed/) {
126 $pl->message ("There are cursed items in the workbench, take them out before you do anything.").
127 return
128 } elsif ($@ =~ /unidentified/) {
129 $pl->message ("There are unidentified items in the workbench, identify them before you do anything.").
130 return
131 } elsif ($@) {
132 warn "error in jeweler ingredient extraction: $@";
133 return;
134 }
135 $ingred;
136}
137
138cf::object::attachment check_ring_drop_on =>
139 on_drop_on => sub {
140 my ($self, $obj, $who) = @_;
141 my $cfg = $self->{check_ring_drop_on};
142 if ($obj->type == cf::RING
143 && !$obj->flag (cf::FLAG_CURSED)
144 && !$obj->flag (cf::FLAG_DAMNED)
145 ) {
146 my $ringo = Jeweler::Object->new (object => $obj);
147 for (grep { /^resist_/ } keys %$cfg) {
148 if (/^resist_(\S+)$/) {
149 if ($ringo->has_resist ($1)) {
150 $self->map->trigger (
151 $cfg->{connection},
152 $cfg->{state}
153 );
154 $obj->decrease (1);
155 cf::override;
156 }
157 }
158 }
159 }
82 } 160 };
83 $chdl->put ($ring->to_object);
84}
85 161
86cf::object->attach ( 162cf::object->attach (
87 type => cf::SKILL, 163 type => cf::SKILL,
88 subtype => cf::SK_JEWELER, 164 subtype => cf::SK_JEWELER,
89 on_use_skill => sub { 165 on_use_skill => sub {
111 187
112 } elsif ($msg =~ m/^\s*make\s*$/i) { 188 } elsif ($msg =~ m/^\s*make\s*$/i) {
113 $pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}})); 189 $pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}}));
114 190
115 } elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) { 191 } elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) {
116 my $ingred = $chdl->extract_jeweler_ingredients; 192 my $ingred = get_ingred ($pl, $chdl) || return;
117 193
118 unless ($Jeweler::CFG->{conversions}->{lc $1}) { 194 unless ($Jeweler::CFG->{conversions}->{lc $1}) {
119 $pl->message ("You don't know how to make '$1', is does such a thing even exist?"); 195 $pl->message ("You don't know how to make '$1', is does such a thing even exist?");
120 return 196 return
121 } 197 }
127 203
128 } elsif ($msg =~ m/^\s*merge\s*$/i) { 204 } elsif ($msg =~ m/^\s*merge\s*$/i) {
129 merge ($chdl, $sk, $pl, 0); 205 merge ($chdl, $sk, $pl, 0);
130 206
131 } else { 207 } else {
132 my $ingred = $chdl->extract_jeweler_ingredients; 208 my $ingred = get_ingred ($pl, $chdl) || return;
209
133 my $plan = $ingred->get_plan; 210 my $plan = $ingred->get_plan;
134 211
135 if ($plan) { 212 if ($plan) {
136 my @ring = $ingred->get_ring; 213 my @ring = $ingred->get_ring;
137 214
215 if (!@ring) {
216 # actually the algorithm cant
217 $pl->message ("You slap yourself, you forgot the jewelery!");
218 return;
219
138 if ((@ring > 1) || ($ring[0]->nrof > 1)) { 220 } elsif ((@ring > 1) || (grep { $_->nrof > 1 } @ring)) {
139 # actually the algorithm cant handle more than one improvement at a time 221 # actually the algorithm cant handle more than one improvement at a time
140 $pl->message ("You can't manage to improve more than one thing at a time!"); 222 $pl->message ("You can't manage to improve more than one thing at a time!");
141 return; 223 return;
142
143 } elsif (@ring < 1) {
144 # actually the algorithm cant
145 $pl->message ("You slap yourself, you forgot the jewelery!");
146 return;
147
148 } else { 224 } else {
149 my $ringo = Jeweler::Object->new (object => $ring[0]); 225 my $ringo = Jeweler::Object->new (object => $ring[0]);
150 my $iring = $ingred->improve_ring_by_plan ($plan, $ringo); 226 my $iring = $ingred->improve_ring_by_plan ($plan, $ringo);
151 my $c1 = $ringo->calc_costs; 227 my $c1 = $ringo->calc_costs;
152 my $c2 = $iring->calc_costs; 228 my $c2 = $iring->calc_costs;
229 my $value = $iring->calc_value_from_cost ($c2);
230
231 if ((not defined $c1) || (not defined $c2)) {
232 $pl->message ("The jewel has or will become a resistancy above 99%,\n"
233 ."that is completly impossible to make!");
234 return;
235 }
153 236
154 my %keys; 237 my %keys;
155 my %cdiff; 238 my %cdiff;
156 for (keys %$c1, keys %$c2) { $keys{$_} = 1 } 239 for (keys %$c1, keys %$c2) { $keys{$_} = 1 }
157 for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_} } 240 for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_}; }
158 241
159 unless (grep { $_ > 0 } values %cdiff) { 242 unless ($iring->is_better_than ($ringo)) {
160 $pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed..."); 243 $pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed...");
161 return; 244 return;
162 } 245 }
163 246
164 my $remcosts = $ingred->check_costs (\%cdiff); 247 my $remcosts = $ingred->check_costs (\%cdiff);
175 } 258 }
176 } else { 259 } else {
177 if (!$pl->flag (cf::FLAG_WIZ)) { 260 if (!$pl->flag (cf::FLAG_WIZ)) {
178 $ingred->check_costs (\%cdiff, 1); 261 $ingred->check_costs (\%cdiff, 1);
179 } 262 }
180 make_ring ($chdl, $ingred, $iring, $sk, $pl); 263 make_ring ($chdl, $ingred, $iring, $value, $sk, $pl);
181 } 264 }
182 } 265 }
183 } else { 266 } else {
184 $pl->message ("You've got no idea what you are planning to do!"); 267 $pl->message ("You've got no idea what you are planning to do!");
185 } 268 }
187 }; 270 };
188 $@ and warn "ERROR: $@\n"; 271 $@ and warn "ERROR: $@\n";
189 } 272 }
190); 273);
191 274
192Jeweler::read_config (cf::datadir . '/jeweler.yaml'); 275Jeweler::read_config "res/jeweler.yaml";
276

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines