1 |
#! perl |
2 |
|
3 |
use strict; |
4 |
|
5 |
use Jeweler; |
6 |
use List::Util qw/max min sum/; |
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->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 |
} |
61 |
|
62 |
my $ring = shift @rings; |
63 |
$ring->improve_by_ring (@rings); |
64 |
|
65 |
if ($do_analyze) { |
66 |
$pl->message ("You want to make a " . $ring->to_string . ": " . $ring->analyze ($sk, $pl, $input_level)); |
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 |
$exp = $ring->projected_exp ($input_level); |
92 |
|
93 |
$pl->change_exp ($exp, "jeweler", cf::SK_EXP_SKILL_ONLY); |
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 |
|
99 |
} else { |
100 |
$pl->message ("You fail!"); |
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 |
|
122 |
sub 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 |
|
138 |
cf::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 |
} |
160 |
}; |
161 |
|
162 |
cf::object->attach ( |
163 |
type => cf::SKILL, |
164 |
subtype => cf::SK_JEWELER, |
165 |
on_use_skill => sub { |
166 |
my ($sk, $ob, $part, $dir, $msg) = @_; |
167 |
my $pl = $ob; |
168 |
|
169 |
my $skobj = $sk; |
170 |
|
171 |
my $chdl = new Jeweler::CauldronHandler; |
172 |
|
173 |
my $rv = 1; |
174 |
eval { |
175 |
$DEBUG = 1; |
176 |
|
177 |
my $player = $ob->contr; |
178 |
|
179 |
unless ($chdl->find_cauldron ('jeweler_bench', $ob->map->at ($ob->x, $ob->y))) { |
180 |
return; |
181 |
} |
182 |
|
183 |
cf::override; |
184 |
|
185 |
if ($msg =~ m/^\s*analy[sz]e\s*$/i) { |
186 |
Jeweler::analyze ($sk, $chdl, $pl); |
187 |
|
188 |
} elsif ($msg =~ m/^\s*make\s*$/i) { |
189 |
$pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}})); |
190 |
|
191 |
} elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) { |
192 |
my $ingred = get_ingred ($pl, $chdl) || return; |
193 |
|
194 |
unless ($Jeweler::CFG->{conversions}->{lc $1}) { |
195 |
$pl->message ("You don't know how to make '$1', is does such a thing even exist?"); |
196 |
return |
197 |
} |
198 |
|
199 |
Jeweler::simple_converter ($player, $ingred, $chdl, $1); |
200 |
|
201 |
} elsif ($msg =~ m/^\s*merge\s*analy[sz]e\s*$/i) { |
202 |
merge ($chdl, $sk, $pl, 1); |
203 |
|
204 |
} elsif ($msg =~ m/^\s*merge\s*$/i) { |
205 |
merge ($chdl, $sk, $pl, 0); |
206 |
|
207 |
} else { |
208 |
my $ingred = get_ingred ($pl, $chdl) || return; |
209 |
|
210 |
my $plan = $ingred->get_plan; |
211 |
|
212 |
if ($plan) { |
213 |
my @ring = $ingred->get_ring; |
214 |
|
215 |
if (!@ring) { |
216 |
# actually the algorithm cant |
217 |
$pl->message ("You slap yourself, you forgot the jewelery!"); |
218 |
return; |
219 |
|
220 |
} elsif ((@ring > 1) || (grep { $_->nrof > 1 } @ring)) { |
221 |
# actually the algorithm cant handle more than one improvement at a time |
222 |
$pl->message ("You can't manage to improve more than one thing at a time!"); |
223 |
return; |
224 |
} else { |
225 |
my $ringo = Jeweler::Object->new (object => $ring[0]); |
226 |
my $iring = $ingred->improve_ring_by_plan ($plan, $ringo); |
227 |
my $c1 = $ringo->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 |
} |
236 |
|
237 |
my %keys; |
238 |
my %cdiff; |
239 |
for (keys %$c1, keys %$c2) { $keys{$_} = 1 } |
240 |
for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_}; } |
241 |
|
242 |
unless ($iring->is_better_than ($ringo)) { |
243 |
$pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed..."); |
244 |
return; |
245 |
} |
246 |
|
247 |
my $remcosts = $ingred->check_costs (\%cdiff); |
248 |
|
249 |
if (grep { $_ > 0 } values %$remcosts) { |
250 |
$pl->message ("You want to make a " . $iring->to_string . ": " . $iring->analyze ($sk, $pl)); |
251 |
$pl->message ("You recognize that you are short of: " |
252 |
. (join ", ", |
253 |
map { my $cost = $remcosts->{$_}; $cost . " " . ($cost > 1 ? "times" : "time") . " " . ingred_alias ($_) } |
254 |
grep { $remcosts->{$_} > 0 } keys %$remcosts)); |
255 |
|
256 |
if ($pl->flag (cf::FLAG_WIZ)) { |
257 |
$iring->wiz_analyze ($pl); |
258 |
} |
259 |
} else { |
260 |
if (!$pl->flag (cf::FLAG_WIZ)) { |
261 |
$ingred->check_costs (\%cdiff, 1); |
262 |
} |
263 |
make_ring ($chdl, $ingred, $iring, $value, $sk, $pl); |
264 |
} |
265 |
} |
266 |
} else { |
267 |
$pl->message ("You've got no idea what you are planning to do!"); |
268 |
} |
269 |
} |
270 |
}; |
271 |
$@ and warn "ERROR: $@\n"; |
272 |
} |
273 |
); |
274 |
|
275 |
Jeweler::read_config "res/jeweler.yaml"; |
276 |
|