ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/jeweler.ext
Revision: 1.24
Committed: Thu Apr 29 07:52:01 2010 UTC (14 years ago) by root
Branch: MAIN
Changes since 1.23: +4 -3 lines
Log Message:
logging

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3 root 1.15 use strict;
4    
5 root 1.1 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 elmex 1.7 gem => 'diamond',
20 root 1.1 );
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 elmex 1.7 } elsif ($aliases{$ing}) {
35     $aliases{$ing}
36    
37 root 1.1 } else {
38     $ing
39     }
40     }
41    
42     my $DEBUG = 1;
43    
44     sub merge {
45     my ($chdl, $sk, $pl, $do_analyze) = @_;
46    
47 elmex 1.5 my $ingred = get_ingred ($pl, $chdl) || return;
48 elmex 1.4
49 root 1.1 my @ring = $ingred->get_ring;
50     my @rings = map { Jeweler::Object->new (object => $_) } @ring;
51    
52     @rings >= 2
53 elmex 1.16 or return $pl->message ("You slap yourself, you forgot to put at least 2 jewels in!");
54 root 1.1
55 elmex 1.12 my $input_level = 0;
56 elmex 1.7 my $value;
57     for (@rings) {
58     $input_level = max ($_->power_to_level, $input_level);
59     $value += $_->{hash}->{value};
60     }
61 elmex 1.5
62 root 1.1 my $ring = shift @rings;
63     $ring->improve_by_ring (@rings);
64    
65     if ($do_analyze) {
66 elmex 1.16 $pl->message ("You want to make a " . $ring->to_string . ": " . $ring->analyze ($sk, $pl, $input_level));
67 root 1.1 $ring->wiz_analyze ($pl)
68     if $pl->flag (cf::FLAG_WIZ);
69     return;
70     }
71    
72 elmex 1.7 make_ring ($chdl, $ingred, $ring, $value, $sk, $pl, $input_level);
73 root 1.1 }
74    
75     sub make_ring {
76 elmex 1.7 my ($chdl, $ingred, $ring, $value, $sk, $pl, $input_level) = @_;
77 root 1.1
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 elmex 1.20 my $r = cf::random_roll (0, 100, $pl, cf::PREFER_LOW);
86 elmex 1.5
87 elmex 1.7 my $make_status;
88     my $exp;
89    
90 root 1.1 if ($r <= $ch or $pl->flag (cf::FLAG_WIZ)) {
91 elmex 1.12 $exp = $ring->projected_exp ($input_level);
92 elmex 1.5
93 elmex 1.22 $pl->change_exp ($exp, "jeweler");
94 elmex 1.18 $pl->message (
95     "You succeed and get " . int ($exp) . " experience points.");
96 elmex 1.7 $make_status = "succeeded";
97    
98 elmex 1.21 $ring->set_value ($value);
99 elmex 1.7
100 root 1.1 } else {
101     $pl->message ("You fail!");
102     $ring->negate;
103 elmex 1.7 $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 root 1.24 cf::debug "$make_info\n"
118     if $make_status eq 'succeeded';
119 root 1.1 }
120 elmex 1.7
121     $chdl->put ($ring_ob);
122 root 1.1 }
123    
124 elmex 1.5 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 root 1.24 cf::error "error in jeweler ingredient extraction: $@";
135 elmex 1.5 return;
136     }
137     $ingred;
138     }
139    
140 elmex 1.6 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 elmex 1.17 $obj->decrease (1);
157 elmex 1.6 cf::override;
158     }
159     }
160     }
161     }
162     };
163    
164 root 1.2 cf::object->attach (
165     type => cf::SKILL,
166     subtype => cf::SK_JEWELER,
167 root 1.1 on_use_skill => sub {
168     my ($sk, $ob, $part, $dir, $msg) = @_;
169     my $pl = $ob;
170    
171     my $skobj = $sk;
172    
173     my $chdl = new Jeweler::CauldronHandler;
174    
175     my $rv = 1;
176     eval {
177     $DEBUG = 1;
178    
179     my $player = $ob->contr;
180    
181     unless ($chdl->find_cauldron ('jeweler_bench', $ob->map->at ($ob->x, $ob->y))) {
182     return;
183     }
184    
185     cf::override;
186    
187     if ($msg =~ m/^\s*analy[sz]e\s*$/i) {
188     Jeweler::analyze ($sk, $chdl, $pl);
189    
190     } elsif ($msg =~ m/^\s*make\s*$/i) {
191     $pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}}));
192    
193     } elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) {
194 elmex 1.5 my $ingred = get_ingred ($pl, $chdl) || return;
195 root 1.1
196     unless ($Jeweler::CFG->{conversions}->{lc $1}) {
197     $pl->message ("You don't know how to make '$1', is does such a thing even exist?");
198     return
199     }
200    
201 elmex 1.19 Jeweler::simple_converter (
202     $player, $ingred, $chdl, $1,
203     cf::exp_to_level ($sk->stats->exp));
204 root 1.1
205     } elsif ($msg =~ m/^\s*merge\s*analy[sz]e\s*$/i) {
206     merge ($chdl, $sk, $pl, 1);
207    
208     } elsif ($msg =~ m/^\s*merge\s*$/i) {
209     merge ($chdl, $sk, $pl, 0);
210    
211     } else {
212 elmex 1.5 my $ingred = get_ingred ($pl, $chdl) || return;
213 elmex 1.4
214 root 1.1 my $plan = $ingred->get_plan;
215    
216     if ($plan) {
217     my @ring = $ingred->get_ring;
218    
219 elmex 1.14 if (!@ring) {
220 root 1.1 # actually the algorithm cant
221     $pl->message ("You slap yourself, you forgot the jewelery!");
222     return;
223    
224 elmex 1.14 } elsif ((@ring > 1) || (grep { $_->nrof > 1 } @ring)) {
225     # actually the algorithm cant handle more than one improvement at a time
226     $pl->message ("You can't manage to improve more than one thing at a time!");
227     return;
228 root 1.1 } else {
229     my $ringo = Jeweler::Object->new (object => $ring[0]);
230     my $iring = $ingred->improve_ring_by_plan ($plan, $ringo);
231 elmex 1.7 my $c1 = $ringo->calc_costs;
232     my $c2 = $iring->calc_costs;
233     my $value = $iring->calc_value_from_cost ($c2);
234 root 1.1
235 elmex 1.12 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     }
240    
241 root 1.1 my %keys;
242     my %cdiff;
243     for (keys %$c1, keys %$c2) { $keys{$_} = 1 }
244 elmex 1.7 for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_}; }
245 root 1.1
246 elmex 1.3 unless ($iring->is_better_than ($ringo)) {
247 root 1.1 $pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed...");
248     return;
249     }
250    
251     my $remcosts = $ingred->check_costs (\%cdiff);
252    
253     if (grep { $_ > 0 } values %$remcosts) {
254     $pl->message ("You want to make a " . $iring->to_string . ": " . $iring->analyze ($sk, $pl));
255     $pl->message ("You recognize that you are short of: "
256     . (join ", ",
257     map { my $cost = $remcosts->{$_}; $cost . " " . ($cost > 1 ? "times" : "time") . " " . ingred_alias ($_) }
258     grep { $remcosts->{$_} > 0 } keys %$remcosts));
259    
260     if ($pl->flag (cf::FLAG_WIZ)) {
261     $iring->wiz_analyze ($pl);
262     }
263     } else {
264     if (!$pl->flag (cf::FLAG_WIZ)) {
265     $ingred->check_costs (\%cdiff, 1);
266     }
267 elmex 1.7 make_ring ($chdl, $ingred, $iring, $value, $sk, $pl);
268 root 1.1 }
269     }
270     } else {
271     $pl->message ("You've got no idea what you are planning to do!");
272     }
273     }
274     };
275 root 1.24 $@ and cf::error "$@\n";
276 root 1.1 }
277 root 1.2 );
278 root 1.1
279 root 1.23 Jeweler::load_config;
280 root 1.10