ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/jeweler.ext
Revision: 1.7
Committed: Fri Sep 1 13:27:23 2006 UTC (17 years, 8 months ago) by elmex
Branch: MAIN
Changes since 1.6: +51 -20 lines
Log Message:
implemented merge and merge analyze so that it works now.
all thats missing is experience gaining.

File Contents

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