ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/jeweler.ext
Revision: 1.11
Committed: Thu Sep 14 18:14:57 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.10: +1 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 = $chdl->extract_jeweler_ingredients;
44 my @ring = $ingred->get_ring;
45 my @rings = map { Jeweler::Object->new (object => $_) } @ring;
46
47 if (@rings < 2) {
48 $pl->message ("You slap yourself, you forgot to put at least 2 jeweles in!");
49 return;
50 }
51
52 my $ring = shift @rings;
53 $ring->improve_by_ring (@rings);
54
55 if ($do_analyze) {
56 $pl->message ("You want to make a " . $ring->to_string . ": " . $ring->analyze ($sk, $pl));
57 if ($pl->flag (cf::FLAG_WIZ)) {
58 $ring->wiz_analyze ($pl);
59 }
60 return;
61 }
62
63 make_ring ($chdl, $ingred, $ring, $sk, $pl);
64 }
65
66 sub make_ring {
67 my ($chdl, $ingred, $ring, $sk, $pl) = @_;
68
69 if (!$pl->flag (cf::FLAG_WIZ)) {
70 $ingred->remove ('rings');
71 $ingred->remove ('ammys');
72 }
73
74 my $ch = $ring->get_chance_perc ($sk);
75 my $succ = 0;
76 my $r = cf::random_roll (0, 100, $pl, cf::PREFER_HIGH);
77 if ($r <= $ch or $pl->flag (cf::FLAG_WIZ)) {
78 my $lvl = max ($ring->power_to_level, 1);
79 my $exp = (cf::level_to_min_exp ($lvl) - cf::level_to_min_exp ($lvl - 1)) / 100;
80 $pl->change_exp ($exp, "jeweler", cf::SK_EXP_SKILL_ONLY);
81 $pl->message ("You succeed and get $exp experience.");
82 } else {
83 $pl->message ("You fail!");
84 $ring->negate;
85 }
86 $chdl->put ($ring->to_object);
87 }
88
89 cf::attach_to_type cf::SKILL, cf::SK_JEWELER,
90 on_use_skill => sub {
91 my ($sk, $ob, $part, $dir, $msg) = @_;
92 my $pl = $ob;
93
94 my $skobj = $sk;
95
96 my $chdl = new Jeweler::CauldronHandler;
97
98 my $rv = 1;
99 eval {
100 Jeweler::read_config ($ENV{CROSSFIRE_LIBDIR} . '/jeweler.yaml');
101 $DEBUG = 1;
102
103 my $player = $ob->contr;
104
105 unless ($chdl->find_cauldron ('jeweler_bench', $ob->map->at ($ob->x, $ob->y))) {
106 return;
107 }
108
109 cf::override;
110
111 if ($msg =~ m/^\s*analy[sz]e\s*$/i) {
112 Jeweler::analyze ($sk, $chdl, $pl);
113
114 } elsif ($msg =~ m/^\s*make\s*$/i) {
115 $pl->message ("You can make: " . (join ', ', keys %{Jeweler::getcfg ('conversions') || {}}));
116
117 } elsif ($msg =~ m/^\s*make\s+(\S+)\s*$/i) {
118 my $ingred = $chdl->extract_jeweler_ingredients;
119
120 unless ($Jeweler::CFG->{conversions}->{lc $1}) {
121 $pl->message ("You don't know how to make '$1', is does such a thing even exist?");
122 return
123 }
124
125 Jeweler::simple_converter ($player, $ingred, $chdl, $1);
126
127 } elsif ($msg =~ m/^\s*merge\s*analy[sz]e\s*$/i) {
128 merge ($chdl, $sk, $pl, 1);
129
130 } elsif ($msg =~ m/^\s*merge\s*$/i) {
131 merge ($chdl, $sk, $pl, 0);
132
133 } else {
134 my $ingred = $chdl->extract_jeweler_ingredients;
135 my $plan = $ingred->get_plan;
136
137 if ($plan) {
138 my @ring = $ingred->get_ring;
139
140 if ((@ring > 1) || ($ring[0]->nrof > 1)) {
141 # actually the algorithm cant handle more than one improvement at a time
142 $pl->message ("You can't manage to improve more than one thing at a time!");
143 return;
144
145 } elsif (@ring < 1) {
146 # actually the algorithm cant
147 $pl->message ("You slap yourself, you forgot the jewelery!");
148 return;
149
150 } else {
151 my $ringo = Jeweler::Object->new (object => $ring[0]);
152 my $iring = $ingred->improve_ring_by_plan ($plan, $ringo);
153 my $c1 = $ringo->calc_costs;
154 my $c2 = $iring->calc_costs;
155
156 my %keys;
157 my %cdiff;
158 for (keys %$c1, keys %$c2) { $keys{$_} = 1 }
159 for (keys %keys) { $cdiff{$_} = $c2->{$_} - $c1->{$_} }
160
161 unless (grep { $_ > 0 } values %cdiff) {
162 $pl->message ("This plan doesn't improve anything, you find yourself puzzled about what you missed...");
163 return;
164 }
165
166 my $remcosts = $ingred->check_costs (\%cdiff);
167
168 if (grep { $_ > 0 } values %$remcosts) {
169 $pl->message ("You want to make a " . $iring->to_string . ": " . $iring->analyze ($sk, $pl));
170 $pl->message ("You recognize that you are short of: "
171 . (join ", ",
172 map { my $cost = $remcosts->{$_}; $cost . " " . ($cost > 1 ? "times" : "time") . " " . ingred_alias ($_) }
173 grep { $remcosts->{$_} > 0 } keys %$remcosts));
174
175 if ($pl->flag (cf::FLAG_WIZ)) {
176 $iring->wiz_analyze ($pl);
177 }
178 } else {
179 if (!$pl->flag (cf::FLAG_WIZ)) {
180 $ingred->check_costs (\%cdiff, 1);
181 }
182 make_ring ($chdl, $ingred, $iring, $sk, $pl);
183 }
184 }
185 } else {
186 $pl->message ("You've got no idea what you are planning to do!");
187 }
188 }
189 };
190 $@ and warn "ERROR: $@\n";
191 }
192 ;
193