AI-Termites

 view release on metacpan or  search on metacpan

samples/termites.pl  view on Meta::CPAN

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use GD;
use AI::Termites;

use Getopt::Long;

my $world = 1;
my $specie = 'NemusNidor';
my $termites = 20;
my $wood = 200;
my $near;
my $one_of = 5;
my $width = 1024;
my $dim = 2;
my $taken = 0;
my $output = "output";
my $truecolor = 0;
my $top = 0;

my $result = GetOptions( "world-size=s" => \$world,
                         "specie=s"     => \$specie,
                         "termites=i"   => \$termites,
                         "wood=i"       => \$wood,
                         "near=s"       => \$near,
                         "one-of=i"     => \$one_of,
                         "width=i"      => \$width,
                         "dim=i"        => \$dim,
                         "taken"        => \$taken,
                         "output=s"     => \$output,
                         "truecolor"    => \$truecolor,
                         "top=i"          => \$top,
                       );

sub scl {
    my $p = shift;
    @{$width * $p}[0, 1];
}

sub sscl {
    my $s = shift;
    $width * $s;
}

$| = 1;

my $class = "AI::Termites::$specie";
eval "require $class; 1" or die "unable to load $class: $@";

my $ters = $class->new(dim => $dim, world_size => $world,
                       n_wood => $wood, n_termites => $termites,
                       near => $near);

my $n = 0;
my $fn = 0;

while (1) {

    my $im = GD::Image->new($width, $width, $truecolor);

    my $white = $im->colorAllocate(255,255,255);
    $im->filledRectangle(0, 0, $width, $width, $white);

    my $black = $im->colorAllocate(0, 0, 0);
    # $im->interlaced('true');

    my $red = $im->colorAllocate(255, 0, 0);
    my $blue = $im->colorAllocate(0, 0, 255);
    my $orange = $im->colorAllocate(255, 128, 0);
    my $green = $im->colorAllocate(0, 255, 0);

    my $txt = sprintf ("dim: %d, near: %.2f%%, termites: %d, wood: %d, wood taken: %d, iteration %d",
                       $dim,
                       100 * $ters->{near} / $world,
                       $termites, $wood, $ters->{taken},
                       $n );

    $im->string(gdSmallFont, 4, 4, $txt, $black);
    
    for my $wood (@{$ters->{wood}}) {
        if ($wood->{taken}) {
            $taken and $im->filledEllipse(scl($wood->{pos}), 8, 8, $orange);
        }
        else {
            $im->filledEllipse(scl($wood->{pos}), 5, 5, $blue);
        }
    }

    for my $ter (@{$ters->{termites}}) {
        my $color = (defined($ter->{wood_ix}) ? $red : $green);
        $im->filledEllipse(scl($ter->{pos}), 3, 3, $color);
    }

    my $name = sprintf "%s-%05d.png", $output, $fn;
    open my $fh, ">", $name;
    print $fh $im->png;
    close $fh;

    print "$n ($fn)\r";
    for (1..$one_of) {



( run in 0.599 second using v1.01-cache-2.11-cpan-39bf76dae61 )