constuctor provided for free by LBW
[language-befunge.git] / lib / Language / Befunge / Wrapping / LaheySpace.pm
blob1768161fee84a45b9a17ecf35c350f2f4fea9530
2 # This file is part of Language::Befunge.
3 # Copyright (c) 2001-2008 Jerome Quelin, all rights reserved.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the same terms as Perl itself.
10 package Language::Befunge::Wrapping::LaheySpace;
12 use strict;
13 use warnings;
15 use base qw{ Language::Befunge::Wrapping };
18 # -- PUBLIC METHODS
21 # $wrapping->wrap( $storage, $ip );
23 # Wrap $ip in $storage according to this module wrapping algorithm. Note
24 # that $ip is already out of bounds, ie, it has been moved once by LBI.
25 # As a side effect, $ip will have its position changed.
27 # LBW::LaheySpace implements a wrapping as defined in befunge specs -
28 # ie, when hitting a bound of the storage, the ip reverses and
29 # backtraces until it bumps into another bound, and then it reverses one
30 # last time to keep its velocity.
32 sub wrap {
33 my ($self, $storage, $ip) = @_;
35 # fetch the current position/velocity of the ip.
36 my $v = $ip->get_position;
37 my $d = $ip->get_delta;
39 # fetch the storage min / max
40 my $min = $storage->min;
41 my $max = $storage->max;
43 # funge98 says we should walk our current trajectory in reverse,
44 # until we hit the other side of the storage, and then continue
45 # along the same path.
46 do {
47 $v -= $d;
48 } while ( $v->bounds_check($min, $max) );
50 # now that we've hit the wall, walk back into the valid code range.
51 $v += $d;
53 # store new position.
54 $ip->set_position($v);
58 __END__
61 =head1 NAME
63 Language::Befunge::Wrapping::LaheySpace - a LaheySpace wrapping
66 =head1 DESCRIPTION
68 C<LBW::LaheySpace> implements a wrapping as defined in befunge specs - ie,
69 when hitting a bound of the storage, the ip reverses and backtraces
70 until it bumps into another bound, and then it reverses one last time to
71 keep its velocity.
75 =head1 CONSTRUCTOR
77 =head2 LBW::LaheySpace->new;
79 Creates a new LaheySpace wrapping.
82 =head1 PUBLIC METHODS
84 =head2 $wrapping->wrap( $storage, $ip )
86 Wrap C<$ip> in C<$storage> according to this module wrapping algorithm.
87 See L<DESCRIPTION> for an overview of the algorithm used.
89 Note that C<$ip> is already out of bounds, ie, it has been moved once by
90 LBI.
92 As a side effect, $ip will have its position changed.
96 =head1 SEE ALSO
98 L<Language::Befunge>.
101 =head1 AUTHOR
103 Jerome Quelin, C<< <jquelin@cpan.org> >>
106 =head1 COPYRIGHT & LICENSE
108 Copyright (c) 2001-2008 Jerome Quelin, all rights reserved.
110 This program is free software; you can redistribute it and/or modify
111 it under the same terms as Perl itself.
114 =cut