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 |
); |
20 |
|
21 |
if ($ing =~ m/resist_(\S+)/) { |
22 |
my $a = $aliases{lc $1} || $1; |
23 |
"something for '". lc ($a). "' resistance"; |
24 |
|
25 |
} elsif ($ing =~ m/stat_(\S+)/) { |
26 |
my $a = $aliases{lc $1} || $1; |
27 |
"something for the ". lc ($a). " stat"; |
28 |
|
29 |
} elsif ($ing =~ m/spec_(\S+)/) { |
30 |
my $a = $aliases{lc $1} || $1; |
31 |
"something for the ". lc ($a). "' special"; |
32 |
|
33 |
} else { |
34 |
$ing |
35 |
} |
36 |
} |
37 |
|
38 |
my $DEBUG = 1; |
39 |
|
40 |
sub merge { |
41 |
my ($chdl, $sk, $pl, $do_analyze) = @_; |
42 |
|
43 |
my $ingred = get_ingred ($pl, $chdl) || return; |
44 |
|
45 |
my @ring = $ingred->get_ring; |
46 |
my @rings = map { Jeweler::Object->new (object => $_) } @ring; |
47 |
|
48 |
@rings >= 2 |
49 |
or return $pl->reply (undef, "You slap yourself, you forgot to put at least 2 jewels in!"); |
50 |
|
51 |
my $input_level; |
52 |
$input_level = max ($_->power_to_level, $input_level) for @rings; |
53 |
|
54 |
my $ring = shift @rings; |
55 |
$ring->improve_by_ring (@rings); |
56 |
|
57 |
if ($do_analyze) { |
58 |
$pl->reply (undef, "You want to make a " . $ring->to_string . ": " . $ring->analyze ($sk, $pl)); |
59 |
$ring->wiz_analyze ($pl) |
60 |
if $pl->flag (cf::FLAG_WIZ); |
61 |
return; |
62 |
} |
63 |
|
64 |
make_ring ($chdl, $ingred, $ring, $sk, $pl, $input_level); |
65 |
} |
66 |
|
67 |
sub make_ring { |
68 |
my ($chdl, $ingred, $ring, $sk, $pl, $input_level) = @_; |
69 |
|
70 |
if (!$pl->flag (cf::FLAG_WIZ)) { |
71 |
$ingred->remove ('rings'); |
72 |
$ingred->remove ('ammys'); |
73 |
} |
74 |
|
75 |
my $ch = $ring->get_chance_perc ($sk); |
76 |
my $succ = 0; |
77 |
my $r = cf::random_roll (0, 100, $pl, cf::PREFER_HIGH); |
78 |
|
79 |
if ($r <= $ch or $pl->flag (cf::FLAG_WIZ)) { |
80 |
my $lvl = max ($ring->power_to_level, 1); |
81 |
my $exp = (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1)) / 100; |
82 |
|
83 |
if (defined $input_level) { |
84 |
my $subexp = |
85 |
(cf::level_to_min_exp ($input_level) |
86 |
- cf::level_to_min_exp ($input_level - 1)) |
87 |
/ 100; |
88 |
warn "INPUT: $lvl <-> $input_level ($exp <-> $subexp)\n"; |
89 |
$exp -= $subexp; |
90 |
$exp = max ($exp, 0); |
91 |
} |
92 |
|
93 |
$pl->change_exp ($exp, "jeweler", cf::SK_EXP_SKILL_ONLY); |
94 |
$pl->message ("You succeed and get $exp experience."); |
95 |
} else { |
96 |
$pl->message ("You fail!"); |
97 |
$ring->negate; |
98 |
} |
99 |
$chdl->put ($ring->to_object); |
100 |
} |
101 |
|
102 |
sub get_ingred { |
103 |
my ($pl, $chdl) = @_; |
104 |
my $ingred = eval { $chdl->extract_jeweler_ingredients }; |
105 |
if ($@ =~ /cursed/) { |
106 |
$pl->message ("There are cursed items in the workbench, take them out before you do anything."). |
107 |
return |
108 |
} elsif ($@ =~ /unidentified/) { |
109 |
$pl->message ("There are unidentified items in the workbench, identify them before you do anything."). |
110 |
return |
111 |
} elsif ($@) { |
112 |
warn "error in jeweler ingredient extraction: $@"; |
113 |
return; |
114 |
} |
115 |
$ingred; |
116 |
} |
117 |
|
118 |
cf::object::attachment check_ring_drop_on => |
119 |
on_drop_on => sub { |
120 |
my ($self, $obj, $who) = @_; |
121 |
my $cfg = $self->{check_ring_drop_on}; |
122 |
if ($obj->type == cf::RING |
123 |
&& !$obj->flag (cf::FLAG_CURSED) |
124 |
&& !$obj->flag (cf::FLAG_DAMNED) |
125 |
) { |
126 |
my $ringo = Jeweler::Object->new (object => $obj); |
127 |
for (grep { /^resist_/ } keys %$cfg) { |
128 |
if (/^resist_(\S+)$/) { |
129 |
if ($ringo->has_resist ($1)) { |
130 |
$self->map->trigger ( |
131 |
$cfg->{connection}, |
132 |
$cfg->{state} |
133 |
); |
134 |
cf::override; |
135 |
} |
136 |
} |
137 |
} |
138 |
} |
139 |
}; |
140 |
|
141 |
cf::object->attach ( |
142 |
type => cf::SKILL, |
143 |
subtype => cf::SK_JEWELER, |
144 |
on_use_skill => sub { |
145 |
my ($sk, $ob, $part, $dir, $msg) = @_; |
146 |
my $pl = $ob; |
147 |
|
148 |
my $skobj = $sk; |
149 |
|
150 |
my $chdl = new Jeweler::CauldronHandler; |
151 |
|
152 |
my $rv = 1; |
153 |
eval { |
154 |
$DEBUG = 1; |
155 |
|
156 |
my $player = $ob->contr; |
157 |
|
158 |
unless ($chdl->find_cauldron ('jeweler_bench', $ob->map->at ($ob->x, $ob->y))) { |
159 |
return; |
160 |
} |
161 |
|
162 |
cf::override; |
163 |
|
164 |
if ($msg =~ m/^\s*analy[sz]e\s*$/i) { |
165 |
Jeweler::analyze ($sk, $chdl, $pl); |
166 |
|
167 |
} elsif ($msg =~ m/^\s*make\s*$/i) { |
168 |
$pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}})); |
169 |
|
170 |
} elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) { |
171 |
my $ingred = get_ingred ($pl, $chdl) || return; |
172 |
|
173 |
unless ($Jeweler::CFG->{conversions}->{lc $1}) { |
174 |
$pl->message ("You don't know how to make '$1', is does such a thing even exist?"); |
175 |
return |
176 |
} |
177 |
|
178 |
Jeweler::simple_converter ($player, $ingred, $chdl, $1); |
179 |
|
180 |
} elsif ($msg =~ m/^\s*merge\s*analy[sz]e\s*$/i) { |
181 |
merge ($chdl, $sk, $pl, 1); |
182 |
|
183 |
} elsif ($msg =~ m/^\s*merge\s*$/i) { |
184 |
merge ($chdl, $sk, $pl, 0); |
185 |
|
186 |
} else { |
187 |
my $ingred = get_ingred ($pl, $chdl) || return; |
188 |
|
189 |
my $plan = $ingred->get_plan; |
190 |
|
191 |
if ($plan) { |
192 |
my @ring = $ingred->get_ring; |
193 |
|
194 |
if ((@ring > 1) || ($ring[0]->nrof > 1)) { |
195 |
# actually the algorithm cant handle more than one improvement at a time |
196 |
$pl->message ("You can't manage to improve more than one thing at a time!"); |
197 |
return; |
198 |
|
199 |
} elsif (@ring < 1) { |
200 |
# actually the algorithm cant |
201 |
$pl->message ("You slap yourself, you forgot the jewelery!"); |
202 |
return; |
203 |
|
204 |
} else { |
205 |
my $ringo = Jeweler::Object->new (object => $ring[0]); |
206 |
my $iring = $ingred->improve_ring_by_plan ($plan, $ringo); |
207 |
my $c1 = $ringo->calc_costs; |
208 |
my $c2 = $iring->calc_costs; |
209 |
|
210 |
my %keys; |
211 |
my %cdiff; |
212 |
for (keys %$c1, keys %$c2) { $keys{$_} = 1 } |
213 |
for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_}; warn "$_: $c2->{$_} | $c1->{$_}\n"; } |
214 |
|
215 |
unless ($iring->is_better_than ($ringo)) { |
216 |
$pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed..."); |
217 |
return; |
218 |
} |
219 |
|
220 |
my $remcosts = $ingred->check_costs (\%cdiff); |
221 |
|
222 |
if (grep { $_ > 0 } values %$remcosts) { |
223 |
$pl->message ("You want to make a " . $iring->to_string . ": " . $iring->analyze ($sk, $pl)); |
224 |
$pl->message ("You recognize that you are short of: " |
225 |
. (join ", ", |
226 |
map { my $cost = $remcosts->{$_}; $cost . " " . ($cost > 1 ? "times" : "time") . " " . ingred_alias ($_) } |
227 |
grep { $remcosts->{$_} > 0 } keys %$remcosts)); |
228 |
|
229 |
if ($pl->flag (cf::FLAG_WIZ)) { |
230 |
$iring->wiz_analyze ($pl); |
231 |
} |
232 |
} else { |
233 |
if (!$pl->flag (cf::FLAG_WIZ)) { |
234 |
$ingred->check_costs (\%cdiff, 1); |
235 |
} |
236 |
make_ring ($chdl, $ingred, $iring, $sk, $pl); |
237 |
} |
238 |
} |
239 |
} else { |
240 |
$pl->message ("You've got no idea what you are planning to do!"); |
241 |
} |
242 |
} |
243 |
}; |
244 |
$@ and warn "ERROR: $@\n"; |
245 |
} |
246 |
); |
247 |
|
248 |
Jeweler::read_config (cf::datadir . '/jeweler.yaml'); |