2014-12-17 07:51:49 +00:00
|
|
|
#! /usr/bin/perl
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
our $usage_text=<<TEXT;
|
|
|
|
NAME
|
|
|
|
simple_player.pl
|
|
|
|
|
|
|
|
USAGE
|
|
|
|
simple_player.pl TEAM [OPTIONS]
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
A very simple perl AI for ttrts
|
|
|
|
|
|
|
|
TEXT
|
|
|
|
|
2014-12-17 08:36:49 +00:00
|
|
|
# Exit with usage if not given a number
|
|
|
|
scalar(@ARGV) or printf $usage_text and exit 1;
|
|
|
|
|
|
|
|
# Grab the team
|
|
|
|
our $team = $ARGV[0];
|
|
|
|
our $turn = 0;
|
|
|
|
|
|
|
|
# If team is non-numeric
|
|
|
|
($team =~ m/\D+/) and printf $usage_text and exit 1;
|
|
|
|
|
2014-12-17 07:51:49 +00:00
|
|
|
# Wait for a file to exist
|
|
|
|
sub WaitForFile
|
|
|
|
{
|
|
|
|
my $file = $_[0];
|
|
|
|
printf("Waiting for %s\n", $file);
|
|
|
|
while( ! -e $file )
|
|
|
|
{
|
|
|
|
sleep 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 08:36:49 +00:00
|
|
|
# Get the units from a turn file
|
|
|
|
sub GetUnitsForTurn
|
|
|
|
{
|
|
|
|
my $turnFile = $_[0];
|
|
|
|
|
|
|
|
# Open the turn file
|
|
|
|
open (my $TURNHANDLE, '<', $turnFile) or die "Could not open '$turnFile' $!";
|
|
|
|
|
|
|
|
# Pull in the header information
|
|
|
|
my $headerLine = <$TURNHANDLE>;
|
|
|
|
chomp $headerLine;
|
|
|
|
my $sizeLine = <$TURNHANDLE>;
|
|
|
|
chomp $sizeLine;
|
|
|
|
my $turnLine = <$TURNHANDLE>;
|
|
|
|
chomp $turnLine;
|
|
|
|
( <$TURNHANDLE> =~ m/~~~~/ ) or die "Gamestate file did not match expected format";
|
|
|
|
|
|
|
|
my @units;
|
|
|
|
while( my $unitLine = <$TURNHANDLE> )
|
|
|
|
{
|
|
|
|
chomp $unitLine;
|
|
|
|
push(@units,$unitLine);
|
|
|
|
}
|
2014-12-17 07:51:49 +00:00
|
|
|
|
2014-12-17 08:36:49 +00:00
|
|
|
return @units;
|
|
|
|
}
|
2014-12-17 07:51:49 +00:00
|
|
|
|
2014-12-17 08:36:49 +00:00
|
|
|
# Output the commands file
|
|
|
|
sub OutputCommandsFile
|
|
|
|
{
|
|
|
|
my $commands = $_[0];
|
2014-12-17 07:51:49 +00:00
|
|
|
|
2014-12-17 08:36:49 +00:00
|
|
|
# Get output file
|
|
|
|
our $orderFile = "Turn_TURN_Team_TEAM.txt";
|
|
|
|
$orderFile =~ s/TURN/$turn/;
|
|
|
|
$orderFile =~ s/TEAM/$team/;
|
2014-12-17 07:51:49 +00:00
|
|
|
|
2014-12-17 08:36:49 +00:00
|
|
|
system ("echo $commands > $orderFile");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get commands for a turn
|
|
|
|
sub GetCommandsForTurn
|
|
|
|
{
|
|
|
|
my @units = @_;
|
|
|
|
|
|
|
|
# perform AI here
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Show launch params
|
|
|
|
printf("Launching with team %i\n",$team);
|
2014-12-17 07:51:49 +00:00
|
|
|
|
|
|
|
# Stay looping the AI
|
|
|
|
while ( 1 )
|
|
|
|
{
|
|
|
|
# Wait for turn file
|
|
|
|
our $turnFile = "Turn_TURN.txt";
|
|
|
|
$turnFile =~ s/TURN/$turn/;
|
|
|
|
|
|
|
|
# Wait for the turn file
|
|
|
|
WaitForFile $turnFile;
|
|
|
|
|
|
|
|
# Read in the game state from turnFile
|
2014-12-17 08:36:49 +00:00
|
|
|
my @units = GetUnitsForTurn($turnFile);
|
2014-12-17 07:51:49 +00:00
|
|
|
|
|
|
|
# Generate some commands
|
2014-12-17 08:36:49 +00:00
|
|
|
my $commands = GetCommandsForTurn @units;
|
2014-12-17 07:51:49 +00:00
|
|
|
|
2014-12-17 08:36:49 +00:00
|
|
|
OutputCommandsFile $commands;
|
2014-12-17 07:51:49 +00:00
|
|
|
|
|
|
|
$turn++;
|
|
|
|
|
|
|
|
}
|