Compare commits
6 commits
Author | SHA1 | Date | |
---|---|---|---|
|
241fef5cc8 | ||
|
f278308780 | ||
|
2bbb7877f9 | ||
|
26f2835644 | ||
|
b84c5f34cc | ||
|
c544e7a4fb |
3 changed files with 59 additions and 215 deletions
50
lua/random_player.lua
Executable file
50
lua/random_player.lua
Executable file
|
@ -0,0 +1,50 @@
|
||||||
|
#! /usr/bin/lua
|
||||||
|
|
||||||
|
-- use socket to communicate with the server directly
|
||||||
|
ttrts = require "ttrts"
|
||||||
|
|
||||||
|
-- usage text
|
||||||
|
local USAGE = [[
|
||||||
|
NAME
|
||||||
|
random_player.lua
|
||||||
|
|
||||||
|
USAGE
|
||||||
|
random_player.lua --host=HOSTNAME
|
||||||
|
|
||||||
|
SUMMARY
|
||||||
|
Random ttrts player using lua Connects directly to ttrts server
|
||||||
|
|
||||||
|
OPTIONS
|
||||||
|
HOSTNAME - host to connect to
|
||||||
|
]]
|
||||||
|
|
||||||
|
-- [[ Get our options and set up state ]]
|
||||||
|
local opts = ttrts.getopt(arg, "host")
|
||||||
|
|
||||||
|
-- if no host or host not set
|
||||||
|
if not opts.host or opts.host == true then
|
||||||
|
print(USAGE) return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- [[ Connect to the host ]]
|
||||||
|
print( "Connecting to " .. opts.host )
|
||||||
|
local player, name = ttrts.ConnectToHost(opts.host)
|
||||||
|
|
||||||
|
print( "Player: " .. player )
|
||||||
|
print( "Name: " .. name )
|
||||||
|
|
||||||
|
--[[ Main Loop ]]
|
||||||
|
while true do
|
||||||
|
|
||||||
|
-- Grab the current gamestate
|
||||||
|
local gamestate = ttrts.GetStateFromHost()
|
||||||
|
|
||||||
|
print("TURN " .. gamestate.turn )
|
||||||
|
|
||||||
|
-- get the orders
|
||||||
|
local orders = ttrts.GetRandomOrders( player, gamestate )
|
||||||
|
|
||||||
|
-- send the orders
|
||||||
|
ttrts.SendOrdersToHost( orders )
|
||||||
|
|
||||||
|
end
|
|
@ -3,13 +3,10 @@ use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use Term::ANSIColor;
|
use Term::ANSIColor;
|
||||||
|
|
||||||
# From http://perlmaven.com/how-to-create-a-perl-module-for-code-reuse
|
our $VERBOSE = $ENV{"VERBOSE"};
|
||||||
# expect ttrts perl module in cwd
|
|
||||||
use File::Basename qw(dirname);
|
|
||||||
use Cwd qw(abs_path);
|
|
||||||
use lib dirname(abs_path($0));
|
|
||||||
|
|
||||||
# Use our ttrts perl library
|
# Use our ttrts perl library
|
||||||
|
# located within the main https://github.com/mdiluz/ttrts repository
|
||||||
use ttrts;
|
use ttrts;
|
||||||
|
|
||||||
our $usage_text=<<TEXT;
|
our $usage_text=<<TEXT;
|
||||||
|
@ -62,24 +59,24 @@ printf("Launching with player %i\n",$player);
|
||||||
while ( 1 )
|
while ( 1 )
|
||||||
{
|
{
|
||||||
# Wait for turn file
|
# Wait for turn file
|
||||||
our $turnFile = GetTurnFile($turn);
|
our $turnFile = GetTurnFileName($turn);
|
||||||
|
|
||||||
# Wait for the turn file
|
# Wait for the turn file
|
||||||
printf("Waiting for %s\n", $turnFile);
|
$VERBOSE and printf("Waiting for %s\n", $turnFile);
|
||||||
WaitForFile $turnFile;
|
WaitForFile $turnFile;
|
||||||
|
|
||||||
# Read in the game state from turnFile
|
# Read in the game state from turnFile
|
||||||
my @units = GetUnitsForTurn($turnFile);
|
my @units = GetUnitStringsFromFile($turnFile);
|
||||||
my ($gameName,$gameX,$gameY) = GetHeaderForTurn($turnFile);
|
my ($major,$minor,$patch,$gameName,$gameX,$gameY) = GetGameInfoFromFile($turnFile);
|
||||||
|
|
||||||
# Get units on my player
|
# Get units on my player
|
||||||
my @myUnits = getUnitsOnPlayer($player,@units);
|
my @myUnits = GetPlayerUnits($player,@units);
|
||||||
|
|
||||||
# Generate some commands
|
# Generate some commands
|
||||||
my $commands = OrderEverythingRandom(@myUnits);
|
my $commands = OrderEverythingRandom(@myUnits);
|
||||||
|
|
||||||
# At this point, print the game map
|
# At this point, print the game map
|
||||||
PrintGameMap($gameX,$gameY,@units);
|
PrintGameMapForTurn($turn);
|
||||||
|
|
||||||
if( scalar(@units) == 0 )
|
if( scalar(@units) == 0 )
|
||||||
{
|
{
|
||||||
|
@ -96,6 +93,7 @@ while ( 1 )
|
||||||
printf "Game over, you lose!\n";
|
printf "Game over, you lose!\n";
|
||||||
exit 0;
|
exit 0;
|
||||||
}
|
}
|
||||||
|
# TODO: Detect lack of possible movement
|
||||||
|
|
||||||
OutputCommandsFile $turn,$player,$commands;
|
OutputCommandsFile $turn,$player,$commands;
|
||||||
|
|
||||||
|
|
204
perl/ttrts.pm
204
perl/ttrts.pm
|
@ -1,204 +0,0 @@
|
||||||
#! /usr/bin/perl
|
|
||||||
use strict;
|
|
||||||
use warnings;
|
|
||||||
|
|
||||||
our $ttrts_perlai_versioncompat_major = 0;
|
|
||||||
our $ttrts_perlai_versioncompat_minor = 2;
|
|
||||||
|
|
||||||
# Get information about a unit from it's descriptor
|
|
||||||
sub getUnit
|
|
||||||
{
|
|
||||||
return ($_[0] =~ /UNIT:(\d+) pl:(\d+) vs:([^ ]+) dr:([^ ]+) ps:\[(\d+),(\d+)\]/);
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get the units from a turn file
|
|
||||||
sub GetUnitsForTurn
|
|
||||||
{
|
|
||||||
my $turnFile = shift;
|
|
||||||
|
|
||||||
# Open the turn file
|
|
||||||
open (my $TURNHANDLE, '<', $turnFile) or die "Could not open '$turnFile' $!";
|
|
||||||
|
|
||||||
# Skip the header information
|
|
||||||
my $num = 0;
|
|
||||||
while( !( <$TURNHANDLE> =~ /~~~~/ ) )
|
|
||||||
{
|
|
||||||
$num++;
|
|
||||||
$num > 20 and die "gamestate file did not reach ~~~~ line within 10 lines";
|
|
||||||
}
|
|
||||||
|
|
||||||
my @units;
|
|
||||||
while( my $unitLine = <$TURNHANDLE> )
|
|
||||||
{
|
|
||||||
chomp $unitLine;
|
|
||||||
if( !($unitLine eq "END") )
|
|
||||||
{
|
|
||||||
push(@units,$unitLine);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close $TURNHANDLE;
|
|
||||||
|
|
||||||
return @units;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check version numbers against ttrts.pm version
|
|
||||||
sub checkVersion
|
|
||||||
{
|
|
||||||
my $version_major = shift;
|
|
||||||
my $version_minor = shift;
|
|
||||||
if( ($version_major != $ttrts_perlai_versioncompat_major)
|
|
||||||
or ($version_minor != $ttrts_perlai_versioncompat_minor) )
|
|
||||||
{
|
|
||||||
printf "ttrts.pm version does not match with this ttrts version\n";
|
|
||||||
die "ttrts.pm = v$ttrts_perlai_versioncompat_minor.$ttrts_perlai_versioncompat_major ttrts = v$version_major.$version_minor";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get information from the header for this turn
|
|
||||||
sub GetHeaderForTurn
|
|
||||||
{
|
|
||||||
my $turnFile = shift;
|
|
||||||
|
|
||||||
# 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 $nameLine = <$TURNHANDLE>;
|
|
||||||
chomp $nameLine;
|
|
||||||
my $sizeLine = <$TURNHANDLE>;
|
|
||||||
chomp $sizeLine;
|
|
||||||
my $turnLine = <$TURNHANDLE>;
|
|
||||||
chomp $turnLine;
|
|
||||||
|
|
||||||
my ($version_major,$version_minor) = ( $headerLine =~ /==== ttrts v(\d+)\.(\d+)\.\d+ ====/ );
|
|
||||||
checkVersion $version_major,$version_minor;
|
|
||||||
|
|
||||||
my ($gameName) = ( $nameLine =~ /NAME:(.+)/ );
|
|
||||||
my ($gameX,$gameY) = ( $sizeLine =~ /SIZE:\[(\d+),(\d+)\]/ );
|
|
||||||
|
|
||||||
close $TURNHANDLE;
|
|
||||||
|
|
||||||
return ($gameName,$gameX,$gameY);
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get units from a specific player
|
|
||||||
sub getUnitsOnPlayer
|
|
||||||
{
|
|
||||||
my $thePlayer = shift;
|
|
||||||
my @allUnits = @_;
|
|
||||||
my @myUnits;
|
|
||||||
|
|
||||||
for my $unit (@allUnits)
|
|
||||||
{
|
|
||||||
my ($unitplayer) = $unit =~ /pl:(\d+)/;
|
|
||||||
if ( $unitplayer == $thePlayer )
|
|
||||||
{
|
|
||||||
push(@myUnits,$unit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return @myUnits;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub GetTurnFile
|
|
||||||
{
|
|
||||||
my $turn = shift;
|
|
||||||
my $turnFile = "Turn_TURN.txt";
|
|
||||||
$turnFile =~ s/TURN/$turn/;
|
|
||||||
return $turnFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub GetCommandFile
|
|
||||||
{
|
|
||||||
my $turn = shift;
|
|
||||||
my $player = shift;
|
|
||||||
my $cmdFileName = "Player_PLAYER_Turn_TURN.txt";
|
|
||||||
$cmdFileName =~ s/TURN/$turn/;
|
|
||||||
$cmdFileName =~ s/PLAYER/$player/;
|
|
||||||
return $cmdFileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Output the commands file
|
|
||||||
sub OutputCommandsFile
|
|
||||||
{
|
|
||||||
my $turn = shift;
|
|
||||||
my $player = shift;
|
|
||||||
my $commands = shift;
|
|
||||||
|
|
||||||
# Get output file
|
|
||||||
our $cmdFileName = GetCommandFile($turn,$player);
|
|
||||||
|
|
||||||
if (! -e $cmdFileName)
|
|
||||||
{
|
|
||||||
open(my $cmdFile, '>', $cmdFileName) or die "Couldn't open '$cmdFileName' $!";
|
|
||||||
print $cmdFile $commands;
|
|
||||||
print $cmdFile "END";
|
|
||||||
close $cmdFile;
|
|
||||||
|
|
||||||
printf "Outputted $cmdFileName\n";
|
|
||||||
printf "$commands";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
open(my $cmdFile, '<', $cmdFileName) or die "Couldn't open '$cmdFileName' $!";
|
|
||||||
my $old_commands = do { <$cmdFile> };
|
|
||||||
|
|
||||||
printf "Replaying $cmdFileName\n";
|
|
||||||
printf "$old_commands";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Print a game map
|
|
||||||
sub PrintGameMap
|
|
||||||
{
|
|
||||||
my $gameX = shift;
|
|
||||||
my $gameY = shift;
|
|
||||||
my @units = @_;
|
|
||||||
|
|
||||||
my @map;
|
|
||||||
|
|
||||||
# Fill with blanks
|
|
||||||
for my $x (0 .. $gameX-1)
|
|
||||||
{
|
|
||||||
for my $y (0 .. $gameY-1)
|
|
||||||
{
|
|
||||||
$map[$x][$y] = "-";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Fill with units
|
|
||||||
for my $unit (@units)
|
|
||||||
{
|
|
||||||
my ($id,$pl,$vs,$dr,$psx,$psy) = getUnit($unit);
|
|
||||||
|
|
||||||
$pl += 31;
|
|
||||||
$vs = "\e[".$pl."m".$vs."\e[0m";
|
|
||||||
|
|
||||||
$map[$psx][$psy] = $vs;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Print whole map bottom left is 0,0
|
|
||||||
for my $y ( reverse 0 .. $gameY-1 )
|
|
||||||
{
|
|
||||||
for my $x (0 .. $gameX-1)
|
|
||||||
{
|
|
||||||
printf($map[$x][$y]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Wait for a file to exist
|
|
||||||
sub WaitForFile
|
|
||||||
{
|
|
||||||
my $file = $_[0];
|
|
||||||
while( ! -e $file )
|
|
||||||
{
|
|
||||||
select(undef, undef, undef, 0.01);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
Loading…
Add table
Reference in a new issue