ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/map-grid-move-random.ext
Revision: 1.2
Committed: Fri Mar 31 22:47:19 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.1: +2 -4 lines
Log Message:
api change

File Contents

# Content
1 #! perl
2
3 use List::Util;
4
5 sub on_move {
6 my ($event, $who) = @_;
7
8 my ($x1, $y1) = ($who->x, $who->y);
9
10 # on first call (there is no initialiser callback), initialise
11 my $self = $who->{map_grid_move} ||= do {
12 my %opt = split /(?:\s+|=)/, $event->options;
13
14 for my $ob ($who->inv) {
15 my $num = $opt{"num_" . $ob->name} || 1;
16
17 for (1 .. $num) {
18 my $ob = $ob->clone (0);
19
20 $who->map->insert_object ($ob, $x1 + rand $opt{width}, $y1 + rand $opt{height});
21
22 push @{ $opt{ob} }, $ob;
23 }
24 }
25
26 \%opt
27 };
28
29 my ($x2, $y2) = ($x1 + $self->{width} - 1, $y1 + $self->{height} - 1);
30
31 for my $ob (@{$self->{ob}}) {
32 my ($x, $y) = ($ob->x, $ob->y);
33
34 if (0.5 <= rand) {
35 $x += 1 - int rand 3;
36 } else {
37 $y += 1 - int rand 3;
38 }
39
40 $x = List::Util::max $x1, List::Util::min $x2, $x;
41 $y = List::Util::max $y1, List::Util::min $y2, $y;
42
43 $ob->remove;
44 $ob->insert_ob_in_map_at ($ob->map, undef, cf::INS_ABOVE_FLOOR_ONLY, $x, $y);
45 }
46
47 1
48 }
49
50
51