ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cf.schmorp.de/maps/perl/map-grid-move.ext
Revision: 1.2
Committed: Fri Feb 10 07:37:13 2006 UTC (18 years, 5 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
State: FILE REMOVED
Log Message:
*** empty log message ***

File Contents

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