#! perl #CONVERSION: NONE use List::Util; sub set_stack { my ($map, $x, $y, $as) = @_; $_->insert_ob_in_map_at ($map, $_, cf::INS_ON_TOP, $x, $y) for @$as; } sub on_move { my ($event, $who) = @_; # on first call (there is no initialiser callback), initialise my $self = $who->{map_grid_move} ||= { x1 => $who->x, y1 => $who->y, split /(?:\s+|=)/, $event->options, }; my ($x1, $y1) = ($self->{x1}, $self->{y1}); # this is horribly ugly code.. why can't there be a simple function to just move objects, # instead of having 20+ of them that all crash in different ways. if (0.5 <= rand) { # horizontal my $y = $y1 + int rand $self->{height}; my @ass = map [grep $_->remove || 1, $who->map->at ($_, $y)], $x1 .. $x1 + $self->{width} - 1; if (0.5 <= rand) { my $as = pop @ass; unshift @ass, $as; } else { my $as = shift @ass; push @ass, $as; } set_stack $who->map, $_, $y, $ass[$_ - $x1] for $x1 .. $x1 + $self->{width} - 1; } else { # vertical my $x = $x1 + int rand $self->{width}; my @ass = map [grep $_->remove || 1, $who->map->at ($x, $_)], $y1 .. $y1 + $self->{height} - 1; if (0.5 <= rand) { my $as = pop @ass; unshift @ass, $as; } else { my $as = shift @ass; push @ass, $as; } set_stack $who->map, $x, $_, $ass[$_ - $y1] for $y1 .. $y1 + $self->{height} - 1; } 1 }