1 |
#! perl |
2 |
|
3 |
use List::Util; |
4 |
|
5 |
sub set_stack { |
6 |
my ($map, $x, $y, $as) = @_; |
7 |
|
8 |
$_->insert_ob_in_map_at ($map, $_, cf::INS_ON_TOP, $x, $y) |
9 |
for @$as; |
10 |
|
11 |
$_->contr->MapNewmapCmd |
12 |
for grep $_->isa (cf::object::player::), @$as; |
13 |
} |
14 |
|
15 |
sub on_move { |
16 |
my ($event, $who) = @_; |
17 |
|
18 |
# on first call (there is no initialiser callback), initialise |
19 |
my $self = $who->{map_grid_move} ||= { |
20 |
x1 => $who->x, |
21 |
y1 => $who->y, |
22 |
split /(?:\s+|=)/, $event->options, |
23 |
}; |
24 |
|
25 |
my ($x1, $y1) = ($self->{x1}, $self->{y1}); |
26 |
|
27 |
# this is horribly ugly code.. why can't there be a simple function to just move objects, |
28 |
# instead of having 20+ of them that all crash in different ways. |
29 |
|
30 |
if (0.5 <= rand) { |
31 |
# horizontal |
32 |
|
33 |
my $y = $y1 + int rand $self->{height}; |
34 |
|
35 |
my @ass = map [grep $_->remove || 1, $who->map->at ($_, $y)], $x1 .. $x1 + $self->{width} - 1; |
36 |
|
37 |
if (0.5 <= rand) { |
38 |
my $as = pop @ass; unshift @ass, $as; |
39 |
} else { |
40 |
my $as = shift @ass; push @ass, $as; |
41 |
} |
42 |
|
43 |
set_stack $who->map, $_, $y, $ass[$_ - $x1] for $x1 .. $x1 + $self->{width} - 1; |
44 |
|
45 |
} else { |
46 |
# vertical |
47 |
|
48 |
my $x = $x1 + int rand $self->{width}; |
49 |
|
50 |
my @ass = map [grep $_->remove || 1, $who->map->at ($x, $_)], $y1 .. $y1 + $self->{height} - 1; |
51 |
|
52 |
if (0.5 <= rand) { |
53 |
my $as = pop @ass; unshift @ass, $as; |
54 |
} else { |
55 |
my $as = shift @ass; push @ass, $as; |
56 |
} |
57 |
|
58 |
set_stack $who->map, $x, $_, $ass[$_ - $y1] for $y1 .. $y1 + $self->{height} - 1; |
59 |
|
60 |
} |
61 |
|
62 |
1 |
63 |
} |
64 |
|
65 |
|
66 |
|