ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/jeweler.ext
Revision: 1.6
Committed: Thu Aug 31 12:39:19 2006 UTC (17 years, 8 months ago) by elmex
Branch: MAIN
Changes since 1.5: +31 -15 lines
Log Message:
further work on the jeweler skill

File Contents

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