mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-03 02:13:35 +02:00
Example client: Add many comments
This commit is contained in:
parent
9baf771807
commit
df066955cd
1 changed files with 38 additions and 13 deletions
|
@ -1,27 +1,31 @@
|
||||||
#!/usr/bin/perl -w
|
#!/usr/bin/perl
|
||||||
#
|
#
|
||||||
# Example brmlife client.
|
# Example brmlife client (originally based on the btraptor).
|
||||||
# Partially based on the btraptor.
|
#
|
||||||
|
# This client is meant only as an example of implementation of all
|
||||||
|
# the main features without sophisticated architecture or decision
|
||||||
|
# making strategies.
|
||||||
|
#
|
||||||
|
# Usage: example.pl [PORT [AGENTID]]
|
||||||
#
|
#
|
||||||
# To run e.g. 15 instances of this client, run this command inside screen:
|
# To run e.g. 15 instances of this client, run this command inside screen:
|
||||||
#
|
|
||||||
# for i in `seq 1 15`; do screen ./example.pl; done
|
# for i in `seq 1 15`; do screen ./example.pl; done
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
use Switch;
|
# Socket communication should use CR-LF line endings, not just LF.
|
||||||
use IO::Socket;
|
|
||||||
|
|
||||||
$/ = "\r\n";
|
$/ = "\r\n";
|
||||||
|
|
||||||
|
|
||||||
|
# Read server input associated with a single tick and update the state
|
||||||
|
# structure accordingly.
|
||||||
sub tick($$) {
|
sub tick($$) {
|
||||||
my ($socket, $state) = @_;
|
my ($socket, $state) = @_;
|
||||||
|
|
||||||
# read message from socket and parse it
|
|
||||||
my $line = '';
|
my $line = '';
|
||||||
print "\n";
|
print "\n";
|
||||||
while ( chomp($line = <$socket>) ) {
|
while (chomp($line = <$socket>)) {
|
||||||
print "# $line\n";
|
print "# $line\n";
|
||||||
last if $line eq '';
|
last if $line eq '';
|
||||||
if ($line eq 'DEAD') {
|
if ($line eq 'DEAD') {
|
||||||
|
@ -53,6 +57,7 @@ sub tick($$) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Execute an appropriate action based on the current agent state.
|
||||||
sub take_action($$) {
|
sub take_action($$) {
|
||||||
my ($socket, $state) = @_;
|
my ($socket, $state) = @_;
|
||||||
|
|
||||||
|
@ -65,8 +70,14 @@ sub take_action($$) {
|
||||||
|
|
||||||
# Move/attack desires for each direction.
|
# Move/attack desires for each direction.
|
||||||
# We prefer moves in the diagonal direction.
|
# We prefer moves in the diagonal direction.
|
||||||
my @move = ((1, 0, 1), (0, 0, 0), (1, 0, 1));
|
my @move = (
|
||||||
my @attack = ((0, 0, 0), (0, 0, 0), (0, 0, 0));
|
(1, 0, 1),
|
||||||
|
(0, 0, 0),
|
||||||
|
(1, 0, 1));
|
||||||
|
my @attack = (
|
||||||
|
(0, 0, 0),
|
||||||
|
(0, 0, 0),
|
||||||
|
(0, 0, 0));
|
||||||
|
|
||||||
# dirindex($x) returns @move, @attack index for given @dirs item.
|
# dirindex($x) returns @move, @attack index for given @dirs item.
|
||||||
sub dirindex { my ($dir) = @_; $dir->[0]+1 + 3*($dir->[1]+1) }
|
sub dirindex { my ($dir) = @_; $dir->[0]+1 + 3*($dir->[1]+1) }
|
||||||
|
@ -81,6 +92,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].
|
||||||
|
|
||||||
|
# Examine our neighborhood and adjust preference for each direction
|
||||||
|
# based on what we sense.
|
||||||
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];
|
||||||
|
@ -113,24 +126,30 @@ sub take_action($$) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Debug print
|
||||||
print "moves ".join(", ", @move)." => (".dirindex($max).":$max->[0],$max->[1])\n";
|
print "moves ".join(", ", @move)." => (".dirindex($max).":$max->[0],$max->[1])\n";
|
||||||
|
|
||||||
|
# Execute actions!
|
||||||
if ($attack[dirindex($max)]) {
|
if ($attack[dirindex($max)]) {
|
||||||
print $socket $state->{tick}." attack_dir $max->[0] $max->[1] 100\r\n";
|
print $socket $state->{tick}." attack_dir $max->[0] $max->[1] 100\r\n";
|
||||||
} else {
|
} else {
|
||||||
print $socket $state->{tick}." move_dir $max->[0] $max->[1]\r\n";
|
print $socket $state->{tick}." move_dir $max->[0] $max->[1]\r\n";
|
||||||
}
|
}
|
||||||
|
# We unconditionally secrete this pheromone for identification
|
||||||
|
# by others of our kin.
|
||||||
print $socket "secrete 65536 1\r\n";
|
print $socket "secrete 65536 1\r\n";
|
||||||
print $socket "\r\n";
|
print $socket "\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# connect
|
# Connect
|
||||||
|
|
||||||
my ($remote_host, $remote_port, $socket);
|
my ($remote_host, $remote_port, $socket);
|
||||||
$remote_host = "localhost";
|
$remote_host = "localhost";
|
||||||
$remote_port = $ARGV[0];
|
$remote_port = $ARGV[0];
|
||||||
$remote_port ||= 27753;
|
$remote_port ||= 27753;
|
||||||
|
|
||||||
|
use IO::Socket;
|
||||||
$socket = IO::Socket::INET->new(
|
$socket = IO::Socket::INET->new(
|
||||||
PeerAddr => $remote_host,
|
PeerAddr => $remote_host,
|
||||||
PeerPort => $remote_port,
|
PeerPort => $remote_port,
|
||||||
|
@ -139,7 +158,9 @@ $socket = IO::Socket::INET->new(
|
||||||
) or die "Couldn't connect to $remote_host:$remote_port : $@\n";
|
) or die "Couldn't connect to $remote_host:$remote_port : $@\n";
|
||||||
print "[ii] connected\r\n";
|
print "[ii] connected\r\n";
|
||||||
|
|
||||||
# negotiate attributs
|
|
||||||
|
# Negotiate attributs
|
||||||
|
|
||||||
if ($ARGV[1]) {
|
if ($ARGV[1]) {
|
||||||
print "[ii] recovering agent $ARGV[1]\r\n";
|
print "[ii] recovering agent $ARGV[1]\r\n";
|
||||||
print $socket "agent_id $ARGV[1]\r\n";
|
print $socket "agent_id $ARGV[1]\r\n";
|
||||||
|
@ -151,9 +172,13 @@ if ($ARGV[1]) {
|
||||||
print $socket "\r\n";
|
print $socket "\r\n";
|
||||||
print "[ii] agent created\r\n";
|
print "[ii] agent created\r\n";
|
||||||
|
|
||||||
|
|
||||||
|
# Start tick loop
|
||||||
|
|
||||||
my $state = {};
|
my $state = {};
|
||||||
while (1) {
|
while (1) {
|
||||||
tick($socket, $state);
|
tick($socket, $state);
|
||||||
|
# Debug print
|
||||||
print $state->{energy} . "\n";
|
print $state->{energy} . "\n";
|
||||||
print "[", join('], [', @{$state->{visual}}), "]\n";
|
print "[", join('], [', @{$state->{visual}}), "]\n";
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue