#!perl my %unit = ( silver => 1, gold => 10, platina => 50, royalty => 5000, ); my %aliases = ( platinum => 'platina', royalties => 'royalty' ); sub find_unpaid { my ($ob) = @_; my @unpaid; for ($ob->inv) { push @unpaid, $_ if $_->get_flag (cf::FLAG_UNPAID); if ($_->inv) { push @unpaid, find_unpaid ($_->inv); } } return @unpaid; } sub on_trigger { my ($ev, $ob, $who_caused, $who) = @_; my $opt = $ev->options; return 0 unless $opt =~ m/(\d+),(\d+)/; my $val = 0; $val += $_->value for find_unpaid ($who); warn "PAID $val\n"; return 0; } sub on_drop_on { my ($ev, $on, $who, $what) = @_; my $name = $what->custom_name; if ($what->get_flag (cf::FLAG_UNPAID)) { return 0; } unless ($name =~ m/\d+\s*\S+/) { $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'"); $what->insert_ob_in_ob ($who); return 1; } my $value = 0; while ($name =~ s/^\s*(\d+)\s*(\S+)//) { if ($aliases{lc $2} or $unit{lc $2}) { $value += $1 * ($unit{lc $2} ? $unit{lc $2} : $unit{$aliases{lc $2}}); } else { $what->insert_ob_in_ob ($who); $who->message ("The shopkeeper says: I don't know the currency '$2'"); return 1; } } $what->set_value ($value); my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP); if ($cost) { my $fact = $value / $cost; $what->set_value (cf::ceil ($value * $fact)); } $what->set_flag (cf::FLAG_UNPAID, 1); $what->{seller} = $who->name; $what->set_custom_name (undef); $what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y); 1; }