ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/jeweler.ext
Revision: 1.12
Committed: Tue Jul 31 09:40:15 2007 UTC (16 years, 9 months ago) by elmex
Branch: MAIN
CVS Tags: rel-2_2
Changes since 1.11: +9 -26 lines
Log Message:
fixed up the jeweler skill

File Contents

# User Rev Content
1 root 1.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 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     or return $pl->reply (undef, "You slap yourself, you forgot to put at least 2 jewels in!");
54    
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.12 $pl->reply (undef, "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     my $r = cf::random_roll (0, 100, $pl, cf::PREFER_HIGH);
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 root 1.1 $pl->change_exp ($exp, "jeweler", cf::SK_EXP_SKILL_ONLY);
94 elmex 1.7 $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 root 1.1 } else {
100     $pl->message ("You fail!");
101     $ring->negate;
102 elmex 1.7 $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     $ring_ob->set_ob_key_value (ext_jeweler_made_by => $pl->name);
119     $ring_ob->set_ob_key_value (ext_jeweler_make_info => $make_info);
120 root 1.1 }
121 elmex 1.7
122     $chdl->put ($ring_ob);
123 root 1.1 }
124    
125 elmex 1.5 sub get_ingred {
126     my ($pl, $chdl) = @_;
127     my $ingred = eval { $chdl->extract_jeweler_ingredients };
128     if ($@ =~ /cursed/) {
129     $pl->message ("There are cursed items in the workbench, take them out before you do anything.").
130     return
131     } elsif ($@ =~ /unidentified/) {
132     $pl->message ("There are unidentified items in the workbench, identify them before you do anything.").
133     return
134     } elsif ($@) {
135     warn "error in jeweler ingredient extraction: $@";
136     return;
137     }
138     $ingred;
139     }
140    
141 elmex 1.6 cf::object::attachment check_ring_drop_on =>
142     on_drop_on => sub {
143     my ($self, $obj, $who) = @_;
144     my $cfg = $self->{check_ring_drop_on};
145     if ($obj->type == cf::RING
146     && !$obj->flag (cf::FLAG_CURSED)
147     && !$obj->flag (cf::FLAG_DAMNED)
148     ) {
149     my $ringo = Jeweler::Object->new (object => $obj);
150     for (grep { /^resist_/ } keys %$cfg) {
151     if (/^resist_(\S+)$/) {
152     if ($ringo->has_resist ($1)) {
153     $self->map->trigger (
154     $cfg->{connection},
155     $cfg->{state}
156     );
157     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     Jeweler::simple_converter ($player, $ingred, $chdl, $1);
202    
203     } elsif ($msg =~ m/^\s*merge\s*analy[sz]e\s*$/i) {
204     merge ($chdl, $sk, $pl, 1);
205    
206     } elsif ($msg =~ m/^\s*merge\s*$/i) {
207     merge ($chdl, $sk, $pl, 0);
208    
209     } else {
210 elmex 1.5 my $ingred = get_ingred ($pl, $chdl) || return;
211 elmex 1.4
212 root 1.1 my $plan = $ingred->get_plan;
213    
214     if ($plan) {
215     my @ring = $ingred->get_ring;
216    
217     if ((@ring > 1) || ($ring[0]->nrof > 1)) {
218     # actually the algorithm cant handle more than one improvement at a time
219     $pl->message ("You can't manage to improve more than one thing at a time!");
220     return;
221    
222     } elsif (@ring < 1) {
223     # actually the algorithm cant
224     $pl->message ("You slap yourself, you forgot the jewelery!");
225     return;
226    
227     } else {
228     my $ringo = Jeweler::Object->new (object => $ring[0]);
229     my $iring = $ingred->improve_ring_by_plan ($plan, $ringo);
230 elmex 1.7 my $c1 = $ringo->calc_costs;
231     my $c2 = $iring->calc_costs;
232     my $value = $iring->calc_value_from_cost ($c2);
233 root 1.1
234 elmex 1.12 if ((not defined $c1) || (not defined $c2)) {
235     $pl->message ("The jewel has or will become a resistancy above 99%,\n"
236     ."that is completly impossible to make!");
237     return;
238     }
239    
240 root 1.1 my %keys;
241     my %cdiff;
242     for (keys %$c1, keys %$c2) { $keys{$_} = 1 }
243 elmex 1.7 for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_}; }
244 root 1.1
245 elmex 1.3 unless ($iring->is_better_than ($ringo)) {
246 root 1.1 $pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed...");
247     return;
248     }
249    
250     my $remcosts = $ingred->check_costs (\%cdiff);
251    
252     if (grep { $_ > 0 } values %$remcosts) {
253     $pl->message ("You want to make a " . $iring->to_string . ": " . $iring->analyze ($sk, $pl));
254     $pl->message ("You recognize that you are short of: "
255     . (join ", ",
256     map { my $cost = $remcosts->{$_}; $cost . " " . ($cost > 1 ? "times" : "time") . " " . ingred_alias ($_) }
257     grep { $remcosts->{$_} > 0 } keys %$remcosts));
258    
259     if ($pl->flag (cf::FLAG_WIZ)) {
260     $iring->wiz_analyze ($pl);
261     }
262     } else {
263     if (!$pl->flag (cf::FLAG_WIZ)) {
264     $ingred->check_costs (\%cdiff, 1);
265     }
266 elmex 1.7 make_ring ($chdl, $ingred, $iring, $value, $sk, $pl);
267 root 1.1 }
268     }
269     } else {
270     $pl->message ("You've got no idea what you are planning to do!");
271     }
272     }
273     };
274     $@ and warn "ERROR: $@\n";
275     }
276 root 1.2 );
277 root 1.1
278 root 1.10 Jeweler::read_config "$DATADIR/jeweler.yaml";
279