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";
|
||||
|
||||
sub tick($) {
|
||||
my ($socket) = @_;
|
||||
sub tick($$) {
|
||||
my ($socket, $state) = @_;
|
||||
|
||||
# read message from socket and parse it
|
||||
my $line = '';
|
||||
my %state = ();
|
||||
print "\n";
|
||||
while ( chomp($line = <$socket>) ) {
|
||||
print "# $line\n";
|
||||
|
@ -41,23 +40,21 @@ sub tick($) {
|
|||
|
||||
if ($type eq 'tick') {
|
||||
$value =~ /^\d+$/ or die "[ee] type tick wrong value ($value)\n";
|
||||
$state{tick} = $value;
|
||||
$state->{tick} = $value;
|
||||
|
||||
} elsif ($type eq 'energy') {
|
||||
$value =~ /^\d+$/ or die "[ee] type energy wrong value ($value)\n";
|
||||
$state{energy} = $value;
|
||||
$state->{energy} = $value;
|
||||
|
||||
} elsif ($type eq 'visual') {
|
||||
$value =~ /^([^ ][^ ] )+([^ ][^ ])$/ or die "[ee] type visual wrong value ($value)\n";
|
||||
$state{visual} = [ split(" ", $value) ];
|
||||
$state->{visual} = [ split(" ", $value) ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
%state;
|
||||
}
|
||||
|
||||
sub take_action($%) {
|
||||
my ($socket, %state) = @_;
|
||||
sub take_action($$) {
|
||||
my ($socket, $state) = @_;
|
||||
|
||||
# FIXME: We use a common direction choice for both move_dir
|
||||
# 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
|
||||
# is [1, -1].
|
||||
|
||||
for my $i (0..$#{$state{visual}}) {
|
||||
my ($type, $agent) = split(//, $state{visual}->[$i]);
|
||||
for my $i (0..$#{$state->{visual}}) {
|
||||
my ($type, $agent) = split(//, $state->{visual}->[$i]);
|
||||
my $dir = $vdirs[$i];
|
||||
|
||||
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 "[ii] agent created\r\n";
|
||||
|
||||
my %state = ();
|
||||
while (%state = tick($socket)) {
|
||||
print $state{energy} . "\n";
|
||||
print "[", join('], [', @{$state{visual}}), "]\n";
|
||||
my $state = {};
|
||||
while (1) {
|
||||
tick($socket, $state);
|
||||
print $state->{energy} . "\n";
|
||||
print "[", join('], [', @{$state->{visual}}), "]\n";
|
||||
|
||||
take_action($socket, %state);
|
||||
take_action($socket, $state);
|
||||
}
|
||||
|
||||
shutdown($socket, 2);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue