[PATCH] Remove arguments, patch vixen, add hunter AI
[progcomp2012.git] / judge / manager / game.cpp
index f25c9f3..c1238f0 100644 (file)
@@ -7,7 +7,7 @@ using namespace std;
 Game* Game::theGame = NULL;
 bool Game::gameCreated = false;
 
-Game::Game(const char * redPath, const char * bluePath, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const  Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard), timeoutTime(newTimeoutTime)
+Game::Game(const char * redPath, const char * bluePath, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const  Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime, const char * newImageOutput) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard), timeoutTime(newTimeoutTime), imageOutput(newImageOutput)
 {
        gameCreated = false;
        if (gameCreated)
@@ -43,10 +43,10 @@ Game::Game(const char * redPath, const char * bluePath, const bool enableGraphic
                        fprintf(stderr, "BLUE! (blue: \"%s\")\n", bluePath);
                exit(EXIT_FAILURE);
        }
-       logMessage("Game initialised.\n");
+//     logMessage("Game initialised.\n");
 }
 
-Game::Game(const char * fromFile, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const  Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard), timeoutTime(newTimeoutTime)
+Game::Game(const char * fromFile, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const  Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard, double newTimeoutTime,const char * newImageOutput) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard), timeoutTime(newTimeoutTime), imageOutput(newImageOutput)
 {
        gameCreated = false;
        if (gameCreated)
@@ -208,21 +208,23 @@ void Game::Wait(double wait)
        if (wait <= 0)
                return;
 
-       TimerThread timer(wait*1000000); //Wait in seconds
-       timer.Start();
+
+
 
        #ifdef BUILD_GRAPHICS
+
+
        if (!graphicsEnabled)
        {
-               while (!timer.Finished());
-               timer.Stop();
+               usleep(1000000*wait); //Wait in seconds
                return;
        }
-       #endif //BUILD_GRAPHICS
 
+       TimerThread timer(wait*1000000); //Wait in seconds
+       timer.Start();
        while (!timer.Finished())
        {
-               #ifdef BUILD_GRAPHICS
+       
                SDL_Event  event;
                while (SDL_PollEvent(&event))
                {
@@ -234,9 +236,12 @@ void Game::Wait(double wait)
                                        break;
                        }
                }
-               #endif //BUILD_GRAPHICS
        }
        timer.Stop();
+
+       #else
+       usleep(wait*1000000); //Wait in seconds
+       #endif //BUILD_GRAPHICS
        
 }
 
@@ -479,10 +484,10 @@ MovementResult Game::Play()
        
        
 
-       logMessage("Messaging red with \"START\"\n");
+//     logMessage("Messaging red with \"START\"\n");
        red->Message("START");
        
-
+       int moveCount = 0;
 
        while (!Board::HaltResult(result) && (turnCount < maxTurns || maxTurns < 0))
        {
@@ -501,7 +506,15 @@ MovementResult Game::Play()
 
                #ifdef BUILD_GRAPHICS
                if (graphicsEnabled)
+               {
                        theBoard.Draw(toReveal);
+                       if (imageOutput != "")
+                       {
+                               string imageFile = "" + imageOutput + "/"+ itostr(moveCount) + ".bmp";
+                               Graphics::ScreenShot(imageFile.c_str());
+                       }
+
+               }
                #endif //BUILD_GRAPHICS
                
                turn = Piece::RED;
@@ -541,10 +554,19 @@ MovementResult Game::Play()
                        theBoard.PrintPretty(stdout, toReveal);
                        fprintf(stdout, "\n\n");
                }
+
+               ++moveCount;
                
                #ifdef BUILD_GRAPHICS
                if (graphicsEnabled)
+               {
                        theBoard.Draw(toReveal);
+                       if (imageOutput != "")
+                       {
+                               string imageFile = "" + imageOutput + "/" + itostr(moveCount) + ".bmp";
+                               Graphics::ScreenShot(imageFile.c_str());
+                       }
+               }
                #endif //BUILD_GRAPHICS
 
                
@@ -589,7 +611,7 @@ MovementResult Game::Play()
                else
                        ReadUserCommand();
        
-               
+               ++moveCount;
 
                ++turnCount;
        }
@@ -771,6 +793,9 @@ void Game::MakeControllers(const char * redPath, const char * bluePath)
 {
        Network * redNetwork = NULL;
        Network * blueNetwork = NULL;
+       //To allow for running two network controllers (I don't know why you would, but beside the point...) use two ports
+       static const int port1 = 4560;
+       static const int port2 = 4561;
 
        if (redPath[0] == '@')
        {
@@ -789,7 +814,7 @@ void Game::MakeControllers(const char * redPath, const char * bluePath)
                        if (network == NULL)
                        {
                                logMessage("Creating server for red AI... ");
-                               redNetwork = new Server();
+                               redNetwork = new Server(port1);
                                logMessage("Successful!\n");
 
                        }
@@ -797,7 +822,7 @@ void Game::MakeControllers(const char * redPath, const char * bluePath)
                        {
                                network = (const char*)(network+1);
                                logMessage("Creating client for red AI... ");
-                               redNetwork = new Client(network);
+                               redNetwork = new Client(network, port2);
                                logMessage("Connected to address %s\n", network);
                        }
 
@@ -825,7 +850,7 @@ void Game::MakeControllers(const char * redPath, const char * bluePath)
                        if (network == NULL)
                        {
                                logMessage("Creating server for blue AI... ");
-                               blueNetwork = new Server();
+                               blueNetwork = new Server(port2);
                                logMessage("Successful!\n");
 
                        }
@@ -833,7 +858,7 @@ void Game::MakeControllers(const char * redPath, const char * bluePath)
                        {
                                network = (const char*)(network+1);
                                logMessage("Creating client for blue AI... ");
-                               blueNetwork = new Client(network);
+                               blueNetwork = new Client(network, port1);
                                logMessage("Connected to address %s\n", network);
                        }
                        logMessage("    (Blue's responses will be received over the connection)\n");
@@ -855,6 +880,15 @@ void Game::MakeControllers(const char * redPath, const char * bluePath)
                red = new NetworkSender(Piece::RED, red, blueNetwork);
                logMessage("    (Red's responses will be copied over the connection)\n");
        }
+
+       red->FixName(); blue->FixName();
        
 }
 
+string itostr(int i)
+{
+       stringstream s;
+       s << i;
+       return s.str();
+}
+

UCC git Repository :: git.ucc.asn.au