ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/jeweler.ext
Revision: 1.8
Committed: Tue Apr 10 17:09:13 2007 UTC (17 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.7: +5 -2 lines
Log Message:
changed exp gaining formular

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.5 my $input_level;
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     $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 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     my $lvl = max ($ring->power_to_level, 1);
92 elmex 1.8 $exp =
93     (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1))
94     / (10 + max ($lvl - 1, 0));
95     # divided by 10 + level
96 elmex 1.5
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 elmex 1.8 / (10 + max ($input_level - 1, 0));
102 elmex 1.5 $exp -= $subexp;
103     $exp = max ($exp, 0);
104     }
105    
106 root 1.1 $pl->change_exp ($exp, "jeweler", cf::SK_EXP_SKILL_ONLY);
107 elmex 1.7 $pl->message ("You succeed and get $exp experience points.");
108     $make_status = "succeeded";
109    
110     $ring->set_value ($value * 0.8); # 20% of the input values will vanish
111    
112 root 1.1 } else {
113     $pl->message ("You fail!");
114     $ring->negate;
115 elmex 1.7 $make_status = "fail";
116     $exp = 0;
117     }
118    
119     my $ring_ob = $ring->to_object;
120    
121     { # some audit info calculation
122     my $sklvl = cf::exp_to_level ($sk->stats->exp);
123    
124     my $make_info = sprintf
125     "JEWELER AUDIT: '%s' made '%s' (%s) (sk lvl %d, ring lvl %d, got %d exp): %s",
126     $pl->name, $ring->to_string, $ring_ob->uuid, $sklvl,
127     $ring->power_to_level, $exp, $make_status;
128    
129     warn "$make_info\n" if $make_status eq 'succeeded';
130    
131     $ring_ob->set_ob_key_value (ext_jeweler_made_by => $pl->name);
132     $ring_ob->set_ob_key_value (ext_jeweler_make_info => $make_info);
133 root 1.1 }
134 elmex 1.7
135     $chdl->put ($ring_ob);
136 root 1.1 }
137    
138 elmex 1.5 sub get_ingred {
139     my ($pl, $chdl) = @_;
140     my $ingred = eval { $chdl->extract_jeweler_ingredients };
141     if ($@ =~ /cursed/) {
142     $pl->message ("There are cursed items in the workbench, take them out before you do anything.").
143     return
144     } elsif ($@ =~ /unidentified/) {
145     $pl->message ("There are unidentified items in the workbench, identify them before you do anything.").
146     return
147     } elsif ($@) {
148     warn "error in jeweler ingredient extraction: $@";
149     return;
150     }
151     $ingred;
152     }
153    
154 elmex 1.6 cf::object::attachment check_ring_drop_on =>
155     on_drop_on => sub {
156     my ($self, $obj, $who) = @_;
157     my $cfg = $self->{check_ring_drop_on};
158     if ($obj->type == cf::RING
159     && !$obj->flag (cf::FLAG_CURSED)
160     && !$obj->flag (cf::FLAG_DAMNED)
161     ) {
162     my $ringo = Jeweler::Object->new (object => $obj);
163     for (grep { /^resist_/ } keys %$cfg) {
164     if (/^resist_(\S+)$/) {
165     if ($ringo->has_resist ($1)) {
166     $self->map->trigger (
167     $cfg->{connection},
168     $cfg->{state}
169     );
170     cf::override;
171     }
172     }
173     }
174     }
175     };
176    
177 root 1.2 cf::object->attach (
178     type => cf::SKILL,
179     subtype => cf::SK_JEWELER,
180 root 1.1 on_use_skill => sub {
181     my ($sk, $ob, $part, $dir, $msg) = @_;
182     my $pl = $ob;
183    
184     my $skobj = $sk;
185    
186     my $chdl = new Jeweler::CauldronHandler;
187    
188     my $rv = 1;
189     eval {
190     $DEBUG = 1;
191    
192     my $player = $ob->contr;
193    
194     unless ($chdl->find_cauldron ('jeweler_bench', $ob->map->at ($ob->x, $ob->y))) {
195     return;
196     }
197    
198     cf::override;
199    
200     if ($msg =~ m/^\s*analy[sz]e\s*$/i) {
201     Jeweler::analyze ($sk, $chdl, $pl);
202    
203     } elsif ($msg =~ m/^\s*make\s*$/i) {
204     $pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}}));
205    
206     } elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) {
207 elmex 1.5 my $ingred = get_ingred ($pl, $chdl) || return;
208 root 1.1
209     unless ($Jeweler::CFG->{conversions}->{lc $1}) {
210     $pl->message ("You don't know how to make '$1', is does such a thing even exist?");
211     return
212     }
213    
214     Jeweler::simple_converter ($player, $ingred, $chdl, $1);
215    
216     } elsif ($msg =~ m/^\s*merge\s*analy[sz]e\s*$/i) {
217     merge ($chdl, $sk, $pl, 1);
218    
219     } elsif ($msg =~ m/^\s*merge\s*$/i) {
220     merge ($chdl, $sk, $pl, 0);
221    
222     } else {
223 elmex 1.5 my $ingred = get_ingred ($pl, $chdl) || return;
224 elmex 1.4
225 root 1.1 my $plan = $ingred->get_plan;
226    
227     if ($plan) {
228     my @ring = $ingred->get_ring;
229    
230     if ((@ring > 1) || ($ring[0]->nrof > 1)) {
231     # actually the algorithm cant handle more than one improvement at a time
232     $pl->message ("You can't manage to improve more than one thing at a time!");
233     return;
234    
235     } elsif (@ring < 1) {
236     # actually the algorithm cant
237     $pl->message ("You slap yourself, you forgot the jewelery!");
238     return;
239    
240     } else {
241     my $ringo = Jeweler::Object->new (object => $ring[0]);
242     my $iring = $ingred->improve_ring_by_plan ($plan, $ringo);
243 elmex 1.7 my $c1 = $ringo->calc_costs;
244     my $c2 = $iring->calc_costs;
245     my $value = $iring->calc_value_from_cost ($c2);
246 root 1.1
247     my %keys;
248     my %cdiff;
249     for (keys %$c1, keys %$c2) { $keys{$_} = 1 }
250 elmex 1.7 for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_}; }
251 root 1.1
252 elmex 1.3 unless ($iring->is_better_than ($ringo)) {
253 root 1.1 $pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed...");
254     return;
255     }
256    
257     my $remcosts = $ingred->check_costs (\%cdiff);
258    
259     if (grep { $_ > 0 } values %$remcosts) {
260     $pl->message ("You want to make a " . $iring->to_string . ": " . $iring->analyze ($sk, $pl));
261     $pl->message ("You recognize that you are short of: "
262     . (join ", ",
263     map { my $cost = $remcosts->{$_}; $cost . " " . ($cost > 1 ? "times" : "time") . " " . ingred_alias ($_) }
264     grep { $remcosts->{$_} > 0 } keys %$remcosts));
265    
266     if ($pl->flag (cf::FLAG_WIZ)) {
267     $iring->wiz_analyze ($pl);
268     }
269     } else {
270     if (!$pl->flag (cf::FLAG_WIZ)) {
271     $ingred->check_costs (\%cdiff, 1);
272     }
273 elmex 1.7 make_ring ($chdl, $ingred, $iring, $value, $sk, $pl);
274 root 1.1 }
275     }
276     } else {
277     $pl->message ("You've got no idea what you are planning to do!");
278     }
279     }
280     };
281     $@ and warn "ERROR: $@\n";
282     }
283 root 1.2 );
284 root 1.1
285 root 1.2 Jeweler::read_config (cf::datadir . '/jeweler.yaml');