mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-02 18:03:37 +02:00
Example client: Use persistent state
This commit is contained in:
parent
7e994ba763
commit
abe9f7d8bc
1 changed files with 15 additions and 17 deletions
|
@ -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) {
|
||||||
|
@ -148,12 +145,13 @@ print $socket "defense 1.0\r\n";
|
||||||
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);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue