From c0ef1f88b635c04da05c72fb80d263bdddd2dd06 Mon Sep 17 00:00:00 2001 From: mdiluzio Date: Wed, 17 Dec 2014 08:36:49 +0000 Subject: [PATCH] Some addded functionality to simplePlayer --- simplePlayer.pl | 76 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/simplePlayer.pl b/simplePlayer.pl index b05583d..fb42a89 100755 --- a/simplePlayer.pl +++ b/simplePlayer.pl @@ -14,6 +14,16 @@ DESCRIPTION TEXT +# 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; + # Wait for a file to exist sub WaitForFile { @@ -25,19 +35,59 @@ sub WaitForFile } } -# Exit with usage if not given a number -scalar(@ARGV) or printf $usage_text and exit 1; +# Get the units from a turn file +sub GetUnitsForTurn +{ + my $turnFile = $_[0]; -# Grab the team -our $team = $ARGV[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"; -# If team is non-numeric -($team =~ m/\D+/) and printf $usage_text and exit 1; + my @units; + while( my $unitLine = <$TURNHANDLE> ) + { + chomp $unitLine; + push(@units,$unitLine); + } + return @units; +} + +# Output the commands file +sub OutputCommandsFile +{ + my $commands = $_[0]; + + # Get output file + our $orderFile = "Turn_TURN_Team_TEAM.txt"; + $orderFile =~ s/TURN/$turn/; + $orderFile =~ s/TEAM/$team/; + + 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); -our $turn = 0; - # Stay looping the AI while ( 1 ) { @@ -49,16 +99,12 @@ while ( 1 ) WaitForFile $turnFile; # Read in the game state from turnFile + my @units = GetUnitsForTurn($turnFile); # Generate some commands - my $commands = ""; + my $commands = GetCommandsForTurn @units; - # Get output file - our $orderFile = "Turn_TURN_Team_TEAM.txt"; - $orderFile =~ s/TURN/$turn/; - $orderFile =~ s/TEAM/$team/; - - system ("echo $commands > $orderFile"); + OutputCommandsFile $commands; $turn++;