1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | use Data::Dumper; |
3 | use strict; |
|
|
4 | |
4 | use Jeweler; |
5 | use Jeweler; |
5 | use List::Util qw/max min sum/; |
6 | use List::Util qw/max min sum/; |
6 | use strict; |
|
|
7 | |
7 | |
8 | sub ingred_alias { |
8 | sub 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 | |
38 | my $DEBUG = 1; |
42 | my $DEBUG = 1; |
39 | |
43 | |
40 | sub merge { |
44 | sub 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 | |
63 | sub make_ring { |
75 | sub 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_LOW); |
|
|
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"); |
78 | $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 | |
79 | } else { |
100 | } else { |
80 | $pl->message ("You fail!"); |
101 | $pl->message ("You fail!"); |
81 | $ring->negate; |
102 | $ring->negate; |
|
|
103 | $make_status = "fail"; |
|
|
104 | $exp = 0; |
|
|
105 | } |
|
|
106 | |
|
|
107 | my $ring_ob = $ring->to_object; |
|
|
108 | |
|
|
109 | { # some audit info calculation |
|
|
110 | my $sklvl = cf::exp_to_level ($sk->stats->exp); |
|
|
111 | |
|
|
112 | my $make_info = sprintf |
|
|
113 | "JEWELER AUDIT: '%s' made '%s' (%s) (sk lvl %d, ring lvl %d, got %d exp): %s", |
|
|
114 | $pl->name, $ring->to_string, $ring_ob->uuid, $sklvl, |
|
|
115 | $ring->power_to_level, $exp, $make_status; |
|
|
116 | |
|
|
117 | cf::debug "$make_info\n" |
|
|
118 | if $make_status eq 'succeeded'; |
|
|
119 | } |
|
|
120 | |
|
|
121 | $chdl->put ($ring_ob); |
|
|
122 | } |
|
|
123 | |
|
|
124 | sub get_ingred { |
|
|
125 | my ($pl, $chdl) = @_; |
|
|
126 | my $ingred = eval { $chdl->extract_jeweler_ingredients }; |
|
|
127 | if ($@ =~ /cursed/) { |
|
|
128 | $pl->message ("There are cursed items in the workbench, take them out before you do anything."). |
|
|
129 | return |
|
|
130 | } elsif ($@ =~ /unidentified/) { |
|
|
131 | $pl->message ("There are unidentified items in the workbench, identify them before you do anything."). |
|
|
132 | return |
|
|
133 | } elsif ($@) { |
|
|
134 | cf::error "error in jeweler ingredient extraction: $@"; |
|
|
135 | return; |
|
|
136 | } |
|
|
137 | $ingred; |
|
|
138 | } |
|
|
139 | |
|
|
140 | cf::object::attachment check_ring_drop_on => |
|
|
141 | on_drop_on => sub { |
|
|
142 | my ($self, $obj, $who) = @_; |
|
|
143 | my $cfg = $self->{check_ring_drop_on}; |
|
|
144 | if ($obj->type == cf::RING |
|
|
145 | && !$obj->flag (cf::FLAG_CURSED) |
|
|
146 | && !$obj->flag (cf::FLAG_DAMNED) |
|
|
147 | ) { |
|
|
148 | my $ringo = Jeweler::Object->new (object => $obj); |
|
|
149 | for (grep { /^resist_/ } keys %$cfg) { |
|
|
150 | if (/^resist_(\S+)$/) { |
|
|
151 | if ($ringo->has_resist ($1)) { |
|
|
152 | $self->map->trigger ( |
|
|
153 | $cfg->{connection}, |
|
|
154 | $cfg->{state} |
|
|
155 | ); |
|
|
156 | $obj->decrease (1); |
|
|
157 | cf::override; |
|
|
158 | } |
|
|
159 | } |
|
|
160 | } |
|
|
161 | } |
82 | } |
162 | }; |
83 | $chdl->put ($ring->to_object); |
|
|
84 | } |
|
|
85 | |
163 | |
86 | cf::attach_to_type cf::SKILL, cf::SK_JEWELER, |
164 | cf::object->attach ( |
|
|
165 | type => cf::SKILL, |
|
|
166 | subtype => cf::SK_JEWELER, |
87 | on_use_skill => sub { |
167 | on_use_skill => sub { |
88 | my ($sk, $ob, $part, $dir, $msg) = @_; |
168 | my ($sk, $ob, $part, $dir, $msg) = @_; |
89 | my $pl = $ob; |
169 | my $pl = $ob; |
90 | |
170 | |
91 | my $skobj = $sk; |
171 | my $skobj = $sk; |
92 | |
172 | |
93 | my $chdl = new Jeweler::CauldronHandler; |
173 | my $chdl = new Jeweler::CauldronHandler; |
94 | |
174 | |
95 | my $rv = 1; |
175 | my $rv = 1; |
96 | eval { |
176 | eval { |
97 | Jeweler::read_config ($ENV{CROSSFIRE_LIBDIR} . '/jeweler.yaml'); |
|
|
98 | $DEBUG = 1; |
177 | $DEBUG = 1; |
99 | |
178 | |
100 | my $player = $ob->contr; |
179 | my $player = $ob->contr; |
101 | |
180 | |
102 | unless ($chdl->find_cauldron ('jeweler_bench', $ob->map->at ($ob->x, $ob->y))) { |
181 | unless ($chdl->find_cauldron ('jeweler_bench', $ob->map->at ($ob->x, $ob->y))) { |
… | |
… | |
110 | |
189 | |
111 | } elsif ($msg =~ m/^\s*make\s*$/i) { |
190 | } elsif ($msg =~ m/^\s*make\s*$/i) { |
112 | $pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}})); |
191 | $pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}})); |
113 | |
192 | |
114 | } elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) { |
193 | } elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) { |
115 | my $ingred = $chdl->extract_jeweler_ingredients; |
194 | my $ingred = get_ingred ($pl, $chdl) || return; |
116 | |
195 | |
117 | unless ($Jeweler::CFG->{conversions}->{lc $1}) { |
196 | unless ($Jeweler::CFG->{conversions}->{lc $1}) { |
118 | $pl->message ("You don't know how to make '$1', is does such a thing even exist?"); |
197 | $pl->message ("You don't know how to make '$1', is does such a thing even exist?"); |
119 | return |
198 | return |
120 | } |
199 | } |
121 | |
200 | |
122 | Jeweler::simple_converter ($player, $ingred, $chdl, $1); |
201 | Jeweler::simple_converter ( |
|
|
202 | $player, $ingred, $chdl, $1, |
|
|
203 | cf::exp_to_level ($sk->stats->exp)); |
123 | |
204 | |
124 | } elsif ($msg =~ m/^\s*merge\s*analy[sz]e\s*$/i) { |
205 | } elsif ($msg =~ m/^\s*merge\s*analy[sz]e\s*$/i) { |
125 | merge ($chdl, $sk, $pl, 1); |
206 | merge ($chdl, $sk, $pl, 1); |
126 | |
207 | |
127 | } elsif ($msg =~ m/^\s*merge\s*$/i) { |
208 | } elsif ($msg =~ m/^\s*merge\s*$/i) { |
128 | merge ($chdl, $sk, $pl, 0); |
209 | merge ($chdl, $sk, $pl, 0); |
129 | |
210 | |
130 | } else { |
211 | } else { |
131 | my $ingred = $chdl->extract_jeweler_ingredients; |
212 | my $ingred = get_ingred ($pl, $chdl) || return; |
|
|
213 | |
132 | my $plan = $ingred->get_plan; |
214 | my $plan = $ingred->get_plan; |
133 | |
215 | |
134 | if ($plan) { |
216 | if ($plan) { |
135 | my @ring = $ingred->get_ring; |
217 | my @ring = $ingred->get_ring; |
136 | |
218 | |
|
|
219 | if (!@ring) { |
|
|
220 | # actually the algorithm cant |
|
|
221 | $pl->message ("You slap yourself, you forgot the jewelery!"); |
|
|
222 | return; |
|
|
223 | |
137 | if ((@ring > 1) || ($ring[0]->nrof > 1)) { |
224 | } elsif ((@ring > 1) || (grep { $_->nrof > 1 } @ring)) { |
138 | # actually the algorithm cant handle more than one improvement at a time |
225 | # actually the algorithm cant handle more than one improvement at a time |
139 | $pl->message ("You can't manage to improve more than one thing at a time!"); |
226 | $pl->message ("You can't manage to improve more than one thing at a time!"); |
140 | return; |
227 | return; |
141 | |
|
|
142 | } elsif (@ring < 1) { |
|
|
143 | # actually the algorithm cant |
|
|
144 | $pl->message ("You slap yourself, you forgot the jewelery!"); |
|
|
145 | return; |
|
|
146 | |
|
|
147 | } else { |
228 | } else { |
148 | my $ringo = Jeweler::Object->new (object => $ring[0]); |
229 | my $ringo = Jeweler::Object->new (object => $ring[0]); |
149 | my $iring = $ingred->improve_ring_by_plan ($plan, $ringo); |
230 | my $iring = $ingred->improve_ring_by_plan ($plan, $ringo); |
150 | my $c1 = $ringo->calc_costs; |
231 | my $c1 = $ringo->calc_costs; |
151 | my $c2 = $iring->calc_costs; |
232 | my $c2 = $iring->calc_costs; |
|
|
233 | my $value = $iring->calc_value_from_cost ($c2); |
|
|
234 | |
|
|
235 | if ((not defined $c1) || (not defined $c2)) { |
|
|
236 | $pl->message ("The jewel has or will become a resistancy above 99%,\n" |
|
|
237 | ."that is completly impossible to make!"); |
|
|
238 | return; |
|
|
239 | } |
152 | |
240 | |
153 | my %keys; |
241 | my %keys; |
154 | my %cdiff; |
242 | my %cdiff; |
155 | for (keys %$c1, keys %$c2) { $keys{$_} = 1 } |
243 | for (keys %$c1, keys %$c2) { $keys{$_} = 1 } |
156 | for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_} } |
244 | for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_}; } |
157 | |
245 | |
158 | unless (grep { $_ > 0 } values %cdiff) { |
246 | unless ($iring->is_better_than ($ringo)) { |
159 | $pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed..."); |
247 | $pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed..."); |
160 | return; |
248 | return; |
161 | } |
249 | } |
162 | |
250 | |
163 | my $remcosts = $ingred->check_costs (\%cdiff); |
251 | my $remcosts = $ingred->check_costs (\%cdiff); |
… | |
… | |
174 | } |
262 | } |
175 | } else { |
263 | } else { |
176 | if (!$pl->flag (cf::FLAG_WIZ)) { |
264 | if (!$pl->flag (cf::FLAG_WIZ)) { |
177 | $ingred->check_costs (\%cdiff, 1); |
265 | $ingred->check_costs (\%cdiff, 1); |
178 | } |
266 | } |
179 | make_ring ($chdl, $ingred, $iring, $sk, $pl); |
267 | make_ring ($chdl, $ingred, $iring, $value, $sk, $pl); |
180 | } |
268 | } |
181 | } |
269 | } |
182 | } else { |
270 | } else { |
183 | $pl->message ("You've got no idea what you are planning to do!"); |
271 | $pl->message ("You've got no idea what you are planning to do!"); |
184 | } |
272 | } |
185 | } |
273 | } |
186 | }; |
274 | }; |
187 | $@ and warn "ERROR: $@\n"; |
275 | $@ and cf::error "$@\n"; |
188 | } |
276 | } |
189 | ; |
277 | ); |
190 | |
278 | |
|
|
279 | Jeweler::load_config; |
|
|
280 | |