1 |
#!perl |
2 |
|
3 |
my %unit = ( |
4 |
silver => 1, |
5 |
gold => 10, |
6 |
platina => 50, |
7 |
royalty => 5000, |
8 |
); |
9 |
|
10 |
my %aliases = ( |
11 |
platinum => 'platina', |
12 |
royalties => 'royalty' |
13 |
); |
14 |
|
15 |
sub find_unpaid { |
16 |
my ($ob) = @_; |
17 |
|
18 |
my @unpaid; |
19 |
for ($ob->inv) { |
20 |
push @unpaid, $_ |
21 |
if $_->get_flag (cf::FLAG_UNPAID); |
22 |
|
23 |
if ($_->inv) { |
24 |
push @unpaid, find_unpaid ($_->inv); |
25 |
} |
26 |
} |
27 |
return @unpaid; |
28 |
} |
29 |
|
30 |
sub on_trigger { |
31 |
my ($ev, $ob, $who_caused, $who) = @_; |
32 |
my $opt = $ev->options; |
33 |
return 0 unless $opt =~ m/(\d+),(\d+)/; |
34 |
my $val = 0; |
35 |
$val += $_->value for find_unpaid ($who); |
36 |
warn "PAID $val\n"; |
37 |
return 0; |
38 |
} |
39 |
|
40 |
sub on_drop_on { |
41 |
my ($ev, $on, $who, $what) = @_; |
42 |
my $name = $what->custom_name; |
43 |
|
44 |
if ($what->get_flag (cf::FLAG_UNPAID)) { |
45 |
return 0; |
46 |
} |
47 |
|
48 |
unless ($name =~ m/\d+\s*\S+/) { |
49 |
$who->message ("The shopkeeper says: Sorry, i don't recognize '$name' as currency. Please name your item like '10 royalty' or '10 platinum 2 silver'"); |
50 |
$what->insert_ob_in_ob ($who); |
51 |
return 1; |
52 |
} |
53 |
|
54 |
my $value = 0; |
55 |
while ($name =~ s/^\s*(\d+)\s*(\S+)//) { |
56 |
if ($aliases{lc $2} or $unit{lc $2}) { |
57 |
$value += $1 * ($unit{lc $2} ? $unit{lc $2} : $unit{$aliases{lc $2}}); |
58 |
} else { |
59 |
$what->insert_ob_in_ob ($who); |
60 |
$who->message ("The shopkeeper says: I don't know the currency '$2'"); |
61 |
return 1; |
62 |
} |
63 |
} |
64 |
|
65 |
$what->set_value ($value); |
66 |
my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP); |
67 |
|
68 |
if ($cost) { |
69 |
my $fact = $value / $cost; |
70 |
$what->set_value (cf::ceil ($value * $fact)); |
71 |
} |
72 |
|
73 |
$what->set_flag (cf::FLAG_UNPAID, 1); |
74 |
$what->{seller} = $who->name; |
75 |
$what->set_custom_name (undef); |
76 |
$what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y); |
77 |
1; |
78 |
} |