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.4 by elmex, Wed Jan 31 15:53:17 2007 UTC vs.
Revision 1.26 by elmex, Sun May 30 19:49:40 2010 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 = eval { $chdl->extract_jeweler_ingredients }; 47 my $ingred = get_ingred ($pl, $chdl) || return;
44 if ($@ =~ /cursed/) {
45 $pl->reply ("There are cursed items in the workbench, take them out before you do anything.").
46 return
47 } elsif ($@) {
48 warn "error in jeweler ingredient extraction: $@";
49 return;
50 }
51 48
52 my @ring = $ingred->get_ring; 49 my @ring = $ingred->get_ring;
53 my @rings = map { Jeweler::Object->new (object => $_) } @ring; 50 my @rings = map { Jeweler::Object->new (object => $_) } @ring;
54 51
55 @rings >= 2 52 @rings >= 2
56 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 }
57 61
58 my $ring = shift @rings; 62 my $ring = shift @rings;
59 $ring->improve_by_ring (@rings); 63 $ring->improve_by_ring (@rings);
60 64
61 if ($do_analyze) { 65 if ($do_analyze) {
62 $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));
63 $ring->wiz_analyze ($pl) 67 $ring->wiz_analyze ($pl)
64 if $pl->flag (cf::FLAG_WIZ); 68 if $pl->flag (cf::FLAG_WIZ);
65 return; 69 return;
66 } 70 }
67 71
68 make_ring ($chdl, $ingred, $ring, $sk, $pl); 72 make_ring ($chdl, $ingred, $ring, $value, $sk, $pl, $input_level);
69} 73}
70 74
71sub make_ring { 75sub make_ring {
72 my ($chdl, $ingred, $ring, $sk, $pl) = @_; 76 my ($chdl, $ingred, $ring, $value, $sk, $pl, $input_level) = @_;
73 77
74 if (!$pl->flag (cf::FLAG_WIZ)) { 78 if (!$pl->flag (cf::FLAG_WIZ)) {
75 $ingred->remove ('rings'); 79 $ingred->remove ('rings');
76 $ingred->remove ('ammys'); 80 $ingred->remove ('ammys');
77 } 81 }
78 82
79 my $ch = $ring->get_chance_perc ($sk); 83 my $ch = $ring->get_chance_perc ($sk);
80 my $succ = 0; 84 my $succ = 0;
81 my $r = cf::random_roll (0, 100, $pl, cf::PREFER_HIGH); 85 my $r = cf::random_roll (0, 100, $pl, cf::PREFER_LOW);
86
87 my $make_status;
88 my $exp;
89
82 if ($r <= $ch or $pl->flag (cf::FLAG_WIZ)) { 90 if ($r <= $ch or $pl->flag (cf::FLAG_WIZ)) {
83 my $lvl = max ($ring->power_to_level, 1); 91 $exp = $ring->projected_exp ($input_level);
84 my $exp = (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1)) / 100; 92
85 $pl->change_exp ($exp, "jeweler", cf::SK_EXP_SKILL_ONLY); 93 $pl->change_exp ($exp, "jeweler", cf::SK_EXP_SKILL_ONLY);
86 $pl->message ("You succeed and get $exp experience."); 94 $pl->message (
95 "You succeed and get " . int ($exp) . " experience points.");
96 $make_status = "succeeded";
97
98 $ring->set_value ($value);
99
87 } else { 100 } else {
88 $pl->message ("You fail!"); 101 $pl->message ("You fail!");
89 $ring->negate; 102 $ring->negate;
103 $make_status = "fail";
104 $exp = 0;
105 }
106
107 my $ring_ob = $ring->to_object;
108
109 $ring_ob->kv_set (ext_jeweler_maker => $pl->name);
110
111 { # some audit info calculation
112 my $sklvl = cf::exp_to_level ($sk->stats->exp);
113
114 my $make_info = sprintf
115 "JEWELER AUDIT: '%s' made '%s' (%s) (sk lvl %d, ring lvl %d, got %d exp): %s",
116 $pl->name, $ring->to_string, $ring_ob->uuid, $sklvl,
117 $ring->power_to_level, $exp, $make_status;
118
119 cf::debug "$make_info\n"
120 if $make_status eq 'succeeded';
121 }
122
123 $chdl->put ($ring_ob);
124}
125
126sub get_ingred {
127 my ($pl, $chdl) = @_;
128 my $ingred = eval { $chdl->extract_jeweler_ingredients };
129 if ($@ =~ /cursed/) {
130 $pl->message ("There are cursed items in the workbench, take them out before you do anything.").
131 return
132 } elsif ($@ =~ /unidentified/) {
133 $pl->message ("There are unidentified items in the workbench, identify them before you do anything.").
134 return
135 } elsif ($@) {
136 cf::error "error in jeweler ingredient extraction: $@";
137 return;
138 }
139 $ingred;
140}
141
142cf::object::attachment check_ring_drop_on =>
143 on_drop_on => sub {
144 my ($self, $obj, $who) = @_;
145 my $cfg = $self->{check_ring_drop_on};
146 if ($obj->type == cf::RING
147 && !$obj->flag (cf::FLAG_CURSED)
148 && !$obj->flag (cf::FLAG_DAMNED)
149 ) {
150 my $ringo = Jeweler::Object->new (object => $obj);
151 for (grep { /^resist_/ } keys %$cfg) {
152 if (/^resist_(\S+)$/) {
153 if ($ringo->has_resist ($1)) {
154 $self->map->trigger (
155 $cfg->{connection},
156 $cfg->{state}
157 );
158 $obj->decrease (1);
159 cf::override;
160 }
161 }
162 }
163 }
90 } 164 };
91 $chdl->put ($ring->to_object);
92}
93 165
94cf::object->attach ( 166cf::object->attach (
95 type => cf::SKILL, 167 type => cf::SKILL,
96 subtype => cf::SK_JEWELER, 168 subtype => cf::SK_JEWELER,
97 on_use_skill => sub { 169 on_use_skill => sub {
119 191
120 } elsif ($msg =~ m/^\s*make\s*$/i) { 192 } elsif ($msg =~ m/^\s*make\s*$/i) {
121 $pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}})); 193 $pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}}));
122 194
123 } elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) { 195 } elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) {
124 my $ingred = eval { $chdl->extract_jeweler_ingredients }; 196 my $ingred = get_ingred ($pl, $chdl) || return;
125 if ($@ =~ /cursed/) {
126 $pl->message ("There are cursed items in the workbench, take them out before you do anything.").
127 return
128 } elsif ($@) {
129 warn "error in jeweler ingredient extraction: $@";
130 return;
131 }
132 197
133 unless ($Jeweler::CFG->{conversions}->{lc $1}) { 198 unless ($Jeweler::CFG->{conversions}->{lc $1}) {
134 $pl->message ("You don't know how to make '$1', is does such a thing even exist?"); 199 $pl->message ("You don't know how to make '$1', is does such a thing even exist?");
135 return 200 return
136 } 201 }
137 202
138 Jeweler::simple_converter ($player, $ingred, $chdl, $1); 203 Jeweler::simple_converter (
204 $player, $ingred, $chdl, $1,
205 cf::exp_to_level ($sk->stats->exp));
139 206
140 } elsif ($msg =~ m/^\s*merge\s*analy[sz]e\s*$/i) { 207 } elsif ($msg =~ m/^\s*merge\s*analy[sz]e\s*$/i) {
141 merge ($chdl, $sk, $pl, 1); 208 merge ($chdl, $sk, $pl, 1);
142 209
143 } elsif ($msg =~ m/^\s*merge\s*$/i) { 210 } elsif ($msg =~ m/^\s*merge\s*$/i) {
144 merge ($chdl, $sk, $pl, 0); 211 merge ($chdl, $sk, $pl, 0);
145 212
146 } else { 213 } else {
147 my $ingred = eval { $chdl->extract_jeweler_ingredients }; 214 my $ingred = get_ingred ($pl, $chdl) || return;
148 if ($@ =~ /cursed/) {
149 $pl->message ("There are cursed items in the workbench, take them out before you do anything.").
150 return
151 } elsif ($@) {
152 warn "error in jeweler ingredient extraction: $@";
153 return;
154 }
155 215
156 my $plan = $ingred->get_plan; 216 my $plan = $ingred->get_plan;
157 217
158 if ($plan) { 218 if ($plan) {
159 my @ring = $ingred->get_ring; 219 my @ring = $ingred->get_ring;
160 220
221 if (!@ring) {
222 # actually the algorithm cant
223 $pl->message ("You slap yourself, you forgot the jewelery!");
224 return;
225
161 if ((@ring > 1) || ($ring[0]->nrof > 1)) { 226 } elsif ((@ring > 1) || (grep { $_->nrof > 1 } @ring)) {
162 # actually the algorithm cant handle more than one improvement at a time 227 # actually the algorithm cant handle more than one improvement at a time
163 $pl->message ("You can't manage to improve more than one thing at a time!"); 228 $pl->message ("You can't manage to improve more than one thing at a time!");
164 return; 229 return;
165
166 } elsif (@ring < 1) {
167 # actually the algorithm cant
168 $pl->message ("You slap yourself, you forgot the jewelery!");
169 return;
170
171 } else { 230 } else {
172 my $ringo = Jeweler::Object->new (object => $ring[0]); 231 my $ringo = Jeweler::Object->new (object => $ring[0]);
173 my $iring = $ingred->improve_ring_by_plan ($plan, $ringo); 232 my $iring = $ingred->improve_ring_by_plan ($plan, $ringo);
174 my $c1 = $ringo->calc_costs; 233 my $c1 = $ringo->calc_costs;
175 my $c2 = $iring->calc_costs; 234 my $c2 = $iring->calc_costs;
235 my $value = $iring->calc_value_from_cost ($c2);
236
237 if ((not defined $c1) || (not defined $c2)) {
238 $pl->message ("The jewel has or will become a resistancy above 99%,\n"
239 ."that is completly impossible to make!");
240 return;
241 }
176 242
177 my %keys; 243 my %keys;
178 my %cdiff; 244 my %cdiff;
179 for (keys %$c1, keys %$c2) { $keys{$_} = 1 } 245 for (keys %$c1, keys %$c2) { $keys{$_} = 1 }
180 for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_}; warn "$_: $c2->{$_} | $c1->{$_}\n"; } 246 for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_}; }
181 247
182 unless ($iring->is_better_than ($ringo)) { 248 unless ($iring->is_better_than ($ringo)) {
183 $pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed..."); 249 $pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed...");
184 return; 250 return;
185 } 251 }
198 } 264 }
199 } else { 265 } else {
200 if (!$pl->flag (cf::FLAG_WIZ)) { 266 if (!$pl->flag (cf::FLAG_WIZ)) {
201 $ingred->check_costs (\%cdiff, 1); 267 $ingred->check_costs (\%cdiff, 1);
202 } 268 }
203 make_ring ($chdl, $ingred, $iring, $sk, $pl); 269 make_ring ($chdl, $ingred, $iring, $value, $sk, $pl);
204 } 270 }
205 } 271 }
206 } else { 272 } else {
207 $pl->message ("You've got no idea what you are planning to do!"); 273 $pl->message ("You've got no idea what you are planning to do!");
208 } 274 }
209 } 275 }
210 }; 276 };
211 $@ and warn "ERROR: $@\n"; 277 $@ and cf::error "$@\n";
212 } 278 }
213); 279);
214 280
215Jeweler::read_config (cf::datadir . '/jeweler.yaml'); 281Jeweler::load_config;
282

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines