Merge branch 'master' of github.com:brmlab/brmlife

This commit is contained in:
Petr Baudis 2011-12-09 14:52:41 +01:00
commit 724cf937d7

View file

@ -15,12 +15,11 @@ use IO::Socket;
$/ = "\r\n"; $/ = "\r\n";
sub tick($) { sub tick($$) {
my ($socket) = @_; my ($socket, $state) = @_;
# read message from socket and parse it # read message from socket and parse it
my $line = ''; my $line = '';
my %state = ();
print "\n"; print "\n";
while ( chomp($line = <$socket>) ) { while ( chomp($line = <$socket>) ) {
print "# $line\n"; print "# $line\n";
@ -41,23 +40,21 @@ sub tick($) {
if ($type eq 'tick') { if ($type eq 'tick') {
$value =~ /^\d+$/ or die "[ee] type tick wrong value ($value)\n"; $value =~ /^\d+$/ or die "[ee] type tick wrong value ($value)\n";
$state{tick} = $value; $state->{tick} = $value;
} elsif ($type eq 'energy') { } elsif ($type eq 'energy') {
$value =~ /^\d+$/ or die "[ee] type energy wrong value ($value)\n"; $value =~ /^\d+$/ or die "[ee] type energy wrong value ($value)\n";
$state{energy} = $value; $state->{energy} = $value;
} elsif ($type eq 'visual') { } elsif ($type eq 'visual') {
$value =~ /^([^ ][^ ] )+([^ ][^ ])$/ or die "[ee] type visual wrong value ($value)\n"; $value =~ /^([^ ][^ ] )+([^ ][^ ])$/ or die "[ee] type visual wrong value ($value)\n";
$state{visual} = [ split(" ", $value) ]; $state->{visual} = [ split(" ", $value) ];
} }
} }
%state;
} }
sub take_action($%) { sub take_action($$) {
my ($socket, %state) = @_; my ($socket, $state) = @_;
# FIXME: We use a common direction choice for both move_dir # FIXME: We use a common direction choice for both move_dir
# and attack_dir, but in fact the agent can do both actions # and attack_dir, but in fact the agent can do both actions
@ -84,8 +81,8 @@ sub take_action($%) {
# Default direction in case of nothing interesting in the vicinity # Default direction in case of nothing interesting in the vicinity
# is [1, -1]. # is [1, -1].
for my $i (0..$#{$state{visual}}) { for my $i (0..$#{$state->{visual}}) {
my ($type, $agent) = split(//, $state{visual}->[$i]); my ($type, $agent) = split(//, $state->{visual}->[$i]);
my $dir = $vdirs[$i]; my $dir = $vdirs[$i];
if (abs($dir->[0]) > 1 or abs($dir->[1]) > 1) { if (abs($dir->[0]) > 1 or abs($dir->[1]) > 1) {
@ -154,12 +151,13 @@ if ($ARGV[1]) {
print $socket "\r\n"; print $socket "\r\n";
print "[ii] agent created\r\n"; print "[ii] agent created\r\n";
my %state = (); my $state = {};
while (%state = tick($socket)) { while (1) {
print $state{energy} . "\n"; tick($socket, $state);
print "[", join('], [', @{$state{visual}}), "]\n"; print $state->{energy} . "\n";
print "[", join('], [', @{$state->{visual}}), "]\n";
take_action($socket, %state); take_action($socket, $state);
} }
shutdown($socket, 2); shutdown($socket, 2);