Algorithm-Gutter
view release on metacpan or search on metacpan
eg/rainmidi3000.pl view on Meta::CPAN
( 0, ~0, undef );
}
}
}
# What thing did we wire up? Some way to visualize this over time might
# also help direct one towards better wirings?
show_wiring($glist);
{
# TWEAK fewer slosh iterations makes the fluid more viscous and thus
# less able to spread out to adjacent cells
#my $slosh_iters;
my $slosh_iters = 2;
my %slosh;
my $offset = 0;
for my $turn ( 1 .. 128 ) {
$gobj->rain;
# TWEAK whether to drain all the fluid (1) or only up to the
# threshold (0) but whether this makes a difference depends on
lib/Algorithm/Gutter.pm view on Meta::CPAN
method rain ( $stash = undef ) {
die "no rain callback supplied" unless defined $rain;
$rain->( $gutter, $stash );
}
# Redistribute imbalances in the water level between adjacent cells.
# There are doubtless more complicated or more efficient ways to do
# this, but those would take time to figure out.
method slosh ( $max = ~0, $dmax = 1 ) {
return if @$gutter < 2;
my $iterations = 0;
my $end = @$gutter - 1;
while ( $max-- > 0 ) {
$iterations++;
my $done = 1;
for my $i ( 0 .. $end - 1 ) {
my $delta = $gutter->[$i]->amount - $gutter->[ $i + 1 ]->amount;
if ( $delta > $dmax ) {
$gutter->[$i]->amount--;
$gutter->[ $i + 1 ]->amount++;
$done = 0;
}
}
for my $i ( reverse 1 .. $end ) {
my $delta = $gutter->[$i]->amount - $gutter->[ $i - 1 ]->amount;
if ( $delta > $dmax ) {
$gutter->[$i]->amount--;
$gutter->[ $i - 1 ]->amount++;
$done = 0;
}
}
last if $done;
}
return $iterations;
}
}
1;
__END__
=head1 NAME
Algorithm::Gutter - cellular automata to simulate rain in a gutter
=head1 SYNOPSIS
lib/Algorithm/Gutter.pm view on Meta::CPAN
=item B<rain> [ I<stash> ]
A convenience method to call the user-supplied rain callback that
presumably adds some amount of fluid to some number of cells.
=item B<set_rain> [ I<code-reference> ]
Writer method for the B<rain> field.
=item B<slosh> [ I<max-iterations> [ I<delta> ] ]
Redistributes imbalances in fluid levels between adjacent cells.
I<max-iterations> is by default a very large number, though could be set
to a much lower strictly positive integer value to make the fluid act in
a more viscous manner, or less prone to slosh into adjacent cells during
a single call to this method. I<delta> (by default C<1>) controls when a
slosh is triggered, and could be set to higher values to make the fluid
behave more like a solid faulting into adjacent cells?
The return value is the number of loops the B<slosh> method took to
level out the fluid.
=back
( run in 1.286 second using v1.01-cache-2.11-cpan-71847e10f99 )