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