From f6f6d6fafd332b11fd51c78d47287d5ffa57de76 Mon Sep 17 00:00:00 2001 From: mdiluzio <thefoips@gmail.com> Date: Sat, 20 Dec 2014 16:24:07 +0000 Subject: [PATCH] Add an END specifiers to gamestate and order files Game will now wait until the last line of an order file matches "END" AI's MUST now finalise their order file with "END" fixes #1 --- source/game/game.cpp | 1 + source/ttrts/README.md | 4 +++- source/ttrts/main.cpp | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/source/game/game.cpp b/source/game/game.cpp index 3608f70..9b9ab6b 100644 --- a/source/game/game.cpp +++ b/source/game/game.cpp @@ -510,6 +510,7 @@ std::string CTTRTSGame::GetStateAsString() const state += '\n'; state += GAME_HEADER_DELIMITER; state += units; + state += "END"; return state; } diff --git a/source/ttrts/README.md b/source/ttrts/README.md index a6d197a..aa57c51 100644 --- a/source/ttrts/README.md +++ b/source/ttrts/README.md @@ -43,13 +43,15 @@ ~~~~ UNIT:{ID} tm:{TEAM} vs:{VIS} dr:{DIR(NESW)} ps:[{X},{Y}] ... {continue for all units} + END ## ORDER FILE FORMAT ### Name Turn_{TURN_NUMBER}_Team_{TEAM_NUMBER}.txt ### Contents ORDER:{ORDER_CHAR} id:{UNIT_ID} - ... + ... {continue for all orders} + END ### Orders F - move unit [F]orward one space diff --git a/source/ttrts/main.cpp b/source/ttrts/main.cpp index e783116..abc457b 100644 --- a/source/ttrts/main.cpp +++ b/source/ttrts/main.cpp @@ -4,6 +4,7 @@ #include <thread> #include <sys/stat.h> #include <stdio.h> +#include <stdbool.h> #include "game.h" #include "version.h" @@ -155,7 +156,40 @@ int main(int argc, char* argv[]) // Wait for the team order file to be created std::cout<<"Waiting for "<<teamOrderFileName<<std::endl; - WaitForFile(teamOrderFileName,sk_waitTime); // Wait for the file + bool hasOrderFile = false; + while(!hasOrderFile) + { + WaitForFile(teamOrderFileName,sk_waitTime); // Wait for the file + + // File must have END + // Method taken from http://stackoverflow.com/questions/11876290/c-fastest-way-to-read-only-last-line-of-text-file + std::ifstream turnFile(teamOrderFileName); + turnFile.seekg(-1,std::ios_base::end); + + // Loop back from the end of file + bool keepLooping = true; + while(keepLooping) { + char ch; + turnFile.get(ch); // Get current byte's data + + if((int)turnFile.tellg() <= 1) { // If the data was at or before the 0th byte + turnFile.seekg(0); // The first line is the last line + keepLooping = false; // So stop there + } + else if(ch == '\n') { // If the data was a newline + keepLooping = false; // Stop at the current position. + } + else { // If the data was neither a newline nor at the 0 byte + turnFile.seekg(-2,std::ios_base::cur); // Move to the front of that data, then to the front of the data before it + } + } + + // Grab this line + std::string lastLine; + std::getline(turnFile,lastLine); + if(lastLine == "END") + hasOrderFile = true; + } std::ifstream turnFile(teamOrderFileName);