public static void HandleVideoCaptureGUI
        (
            GR gr,
            float videoSheetX,
            float videoSheetY,
            float videoSheetWidth,
            float videoSheetHeight,
            STGame game,
            int clientWidth,
            int clientHeight,
            int clientRelativeCursorX,
            int clientRelativeCursorY
        )
        {
            STGameState gameState = game.GetGameState( );


            if (false == game.GameIsSpawnFromVideoCapture( ))
            {
                return;
            }


            gr.glBindTexture
            (
                GR.GL_TEXTURE_2D,
                STEngine.GetVideoProcessing( ).mTextureOpenGLHandleBGR256x256
            );


            float x1 = 0.0f;
            float y1 = 0.0f;
            float x2 = 0.0f;
            float y2 = 0.0f;

            x1 = videoSheetX;
            y1 = videoSheetY;
            x2 = x1 + (videoSheetWidth - 1.0f);
            y2 = y1 + (videoSheetHeight - 1.0f);

            float u1 = 0.0f;
            float v1 = 0.0f;
            float u2 = 0.0f;
            float v2 = 0.0f;

            u1 = 0.0f;
            v1 = 0.0f;
            u2 = 0.5f;
            v2 = 1.0f;

            gr.glEnable(GR.GL_SCISSOR_TEST);
            gr.glScissor((int)(x1), (int)(y1), (int)((x2 - x1) + 1), (int)((y2 - y1) + 1));

            gr.glEnable(GR.GL_TEXTURE_2D);
            gr.glColor3f(1.0f, 1.0f, 1.0f);

            gr.glBegin(GR.GL_QUADS);
            gr.glTexCoord2f(u1, v2);
            gr.glVertex2f(x1, y2);

            gr.glTexCoord2f(u1, v1);
            gr.glVertex2f(x1, y1);

            gr.glTexCoord2f(u2, v1);
            gr.glVertex2f(x2, y1);

            gr.glTexCoord2f(u2, v2);
            gr.glVertex2f(x2, y2);
            gr.glEnd( );

            gr.glDisable(GR.GL_TEXTURE_2D);
            gr.glDisable(GR.GL_SCISSOR_TEST);



            int xTexelMin = 0;
            int yTexelMin = 0;
            int xTexelMax = 0;
            int yTexelMax = 0;


            int xScreenMin = 0;
            int yScreenMin = 0;
            int xScreenMax = 0;
            int yScreenMax = 0;



            // Only listen to the mouse in training/calibration mode
            if (true == gameState.mCalibrationModeFlag)
            {
                if (0 != GetAsyncKeyState(Keys.LButton))
                {
                    // Left button pressed
                    if (0 == gameState.mSelectionState)
                    {
                        gameState.mSelectionState = 1;
                        gameState.mSelectionX1    = clientRelativeCursorX;
                        gameState.mSelectionY1    = ((clientHeight - 1) - clientRelativeCursorY);
                        gameState.mSelectionX2    = clientRelativeCursorX;
                        gameState.mSelectionY2    = ((clientHeight - 1) - clientRelativeCursorY);
                    }
                    else
                    {
                        gameState.mSelectionX2 = clientRelativeCursorX;
                        gameState.mSelectionY2 = ((clientHeight - 1) - clientRelativeCursorY);
                    }
                }
                else
                {
                    // Left button released
                    if (0 == gameState.mSelectionState)
                    {
                        // Nothing to do...
                    }
                    else
                    {
                        gameState.mSelectionState = 0;
                    }
                }

                gr.glEnable(GR.GL_SCISSOR_TEST);
                gr.glScissor(0, 0, clientWidth, clientHeight);

                gr.glColor3f(1.0f, 0.0f, 0.0f);
                gr.glBegin(GR.GL_LINES);

                gr.glVertex2f((float)clientRelativeCursorX - 8.0f, (float)((clientHeight - 1) - clientRelativeCursorY));
                gr.glVertex2f((float)clientRelativeCursorX + 8.0f, (float)((clientHeight - 1) - clientRelativeCursorY));

                gr.glVertex2f((float)clientRelativeCursorX, (float)((clientHeight - 1) - clientRelativeCursorY) - 8.0f);
                gr.glVertex2f((float)clientRelativeCursorX, (float)((clientHeight - 1) - clientRelativeCursorY) + 8.0f);
                gr.glEnd( );
            }



            if (0 != ((GetAsyncKeyState(Keys.Shift)) & 0x8000))
            {
                if (0 != ((GetAsyncKeyState(Keys.Left)) & 0x8000))
                {
                    gameState.mSelectionX2--;
                }
                if (0 != ((GetAsyncKeyState(Keys.Right)) & 0x8000))
                {
                    gameState.mSelectionX2++;
                }
                if (0 != ((GetAsyncKeyState(Keys.Down)) & 0x8000))
                {
                    gameState.mSelectionY2--;
                }
                if (0 != ((GetAsyncKeyState(Keys.Up)) & 0x8000))
                {
                    gameState.mSelectionY2++;
                }
            }
            else
            {
                if (0 != ((GetAsyncKeyState(Keys.Left)) & 0x8000))
                {
                    gameState.mSelectionX1--;
                }
                if (0 != ((GetAsyncKeyState(Keys.Right)) & 0x8000))
                {
                    gameState.mSelectionX1++;
                }
                if (0 != ((GetAsyncKeyState(Keys.Down)) & 0x8000))
                {
                    gameState.mSelectionY1--;
                }
                if (0 != ((GetAsyncKeyState(Keys.Up)) & 0x8000))
                {
                    gameState.mSelectionY1++;
                }
            }



            xScreenMin = gameState.mSelectionX1;
            yScreenMin = gameState.mSelectionY1;
            xScreenMax = gameState.mSelectionX2;
            yScreenMax = gameState.mSelectionY2;



            xTexelMin = (int)(256.0f * (((float)xScreenMin - videoSheetX) / videoSheetHeight));
            yTexelMin = (int)(256.0f * (((float)yScreenMin - videoSheetY) / videoSheetHeight));
            xTexelMax = (int)(256.0f * (((float)xScreenMax - videoSheetX) / videoSheetHeight));
            yTexelMax = (int)(256.0f * (((float)yScreenMax - videoSheetY) / videoSheetHeight));

            int disregard = 0;

            if (xTexelMin < 0)
            {
                disregard = 1;
                xTexelMin = 0;
            }
            if (yTexelMin < 0)
            {
                disregard = 1;
                yTexelMin = 0;
            }
            if (xTexelMax < 0)
            {
                disregard = 1;
                xTexelMax = 0;
            }
            if (yTexelMax < 0)
            {
                disregard = 1;
                yTexelMax = 0;
            }

            if (xTexelMin > 255)
            {
                disregard = 1;
                xTexelMin = 255;
            }
            if (yTexelMin > 255)
            {
                disregard = 1;
                yTexelMin = 255;
            }
            if (xTexelMax > 255)
            {
                disregard = 1;
                xTexelMax = 255;
            }
            if (yTexelMax > 255)
            {
                disregard = 1;
                yTexelMax = 255;
            }

            if (xTexelMin > xTexelMax)
            {
                int swap = xTexelMin;
                xTexelMin = xTexelMax;
                xTexelMax = swap;
            }

            if (yTexelMin > yTexelMax)
            {
                int swap = yTexelMin;
                yTexelMin = yTexelMax;
                yTexelMax = swap;
            }


            // Only set region if in training mode!
            if ((true == gameState.mCalibrationModeFlag) && (0 == disregard))
            {
                STEngine.GetVideoProcessing( ).SetRegion(xTexelMin, yTexelMin, xTexelMax, yTexelMax);
            }


            STEngine.GetVideoProcessing( ).GetRegion(ref xTexelMin, ref yTexelMin, ref xTexelMax, ref yTexelMax);

            xScreenMin = (int)(videoSheetX + (videoSheetHeight * (float)xTexelMin / 256.0f));
            yScreenMin = (int)(videoSheetY + (videoSheetHeight * (float)yTexelMin / 256.0f));
            xScreenMax = (int)(videoSheetX + (videoSheetHeight * (float)xTexelMax / 256.0f));
            yScreenMax = (int)(videoSheetY + (videoSheetHeight * (float)yTexelMax / 256.0f));


            x1 = videoSheetX;
            y1 = videoSheetY;
            x2 = x1 + (videoSheetWidth - 1.0f);
            y2 = y1 + (videoSheetHeight - 1.0f);


            int currentClassification = STEngine.GetVideoProcessing( ).GetRegionClassification( );

            if (0 == currentClassification)
            {
                // If the previous classification was a PIECE, and the current classification
                // is something different, then submit the piece (which must have fallen
                // by a row by now).
                if ((gameState.mPreviousClassification >= 1) && (gameState.mPreviousClassification <= 7))
                {
                    game.SpawnSpecifiedPieceShape(STPiece.GetShapeCorrespondingToByteCode((byte)gameState.mPreviousClassification));
                }
            }

            gameState.mPreviousClassification = currentClassification;


            Color color;

            color =
                STGameDrawing.GetCellValueColorARGB // Returns WHITE for unknown
                (
                    (byte)currentClassification,    // 0..6
                    false                           // monochrome mode
                );

            float red   = 0.0f;
            float green = 0.0f;
            float blue  = 0.0f;

            red   = (float)(color.R) / 255.0f;
            green = (float)(color.G) / 255.0f;
            blue  = (float)(color.B) / 255.0f;


            gr.glColor3f(red, green, blue);

            gr.glBegin(GR.GL_LINES);

            gr.glVertex2f((float)xScreenMin, (float)yScreenMin);
            gr.glVertex2f((float)xScreenMin, (float)yScreenMax);

            gr.glVertex2f((float)xScreenMax, (float)yScreenMin);
            gr.glVertex2f((float)xScreenMax, (float)yScreenMax);

            gr.glVertex2f((float)xScreenMin, (float)yScreenMin);
            gr.glVertex2f((float)xScreenMax, (float)yScreenMin);

            gr.glVertex2f((float)xScreenMin, (float)yScreenMax);
            gr.glVertex2f((float)xScreenMax, (float)yScreenMax);

            // Horizontal divider
            gr.glVertex2f((float)xScreenMin, (float)((yScreenMin + yScreenMax) / 2));
            gr.glVertex2f((float)xScreenMax, (float)((yScreenMin + yScreenMax) / 2));

            // Vertical dividers
            gr.glVertex2f((float)(xScreenMin + ((xScreenMax - xScreenMin) / 4)), (float)yScreenMin);
            gr.glVertex2f((float)(xScreenMin + ((xScreenMax - xScreenMin) / 4)), (float)yScreenMax);

            gr.glVertex2f((float)(xScreenMin + 2 * ((xScreenMax - xScreenMin) / 4)), (float)yScreenMin);
            gr.glVertex2f((float)(xScreenMin + 2 * ((xScreenMax - xScreenMin) / 4)), (float)yScreenMax);

            gr.glVertex2f((float)(xScreenMin + 3 * ((xScreenMax - xScreenMin) / 4)), (float)yScreenMin);
            gr.glVertex2f((float)(xScreenMin + 3 * ((xScreenMax - xScreenMin) / 4)), (float)yScreenMax);

            gr.glEnd( );

            gr.glDisable(GR.GL_SCISSOR_TEST);
        }
Esempio n. 2
0
        public static bool WriteGameStateToFile
        (
            String filePath, // e.g., C:\ or C:\foo
            String fileName, // e.g., tetris_state_YYYYMMDD_HHMMSSMMM.txt
            STGameState gameState
        )
        {
            if (true == filePath.EndsWith("\\"))
            {
                filePath = filePath.Substring(0, (filePath.Length - 1));
            }


            String filePathAndName = "";

            filePathAndName = filePath + "\\" + fileName;



            STFileWriter file = new STFileWriter( );

            bool successfullyOpenedFile = false;

            successfullyOpenedFile = file.Open(filePathAndName);

            if (false == successfullyOpenedFile)
            {
                return(false);
            }



            // Simply print out game state information to the text file.
            // The order of fields doesn't matter.  Fields can be added
            // or removed.  It is the consumer's responsibility to
            // acquire any fields and compensate for "missing" fields.
            // NOTE: Not all of these fields are intended to be restored
            // on loading.  Many are DERIVED values, or are session-dependent,
            // or should be cleared upon loading to give the user the
            // chance to alter settings.  For example, don't restore the
            // pause/unpaused state, or the video capture state.



            file.WriteText("fileName                 \"" + fileName + "\"\r\n");
            file.WriteText("localDateAndTime         \"" + GetDateStringYYYY_MMM_DD_HH_MM_SS_MMM( ) + "\"\r\n");
            file.WriteText("standardTetrisVersion    \"" + STGame.GetApplicationName( ) + "\"\r\n");


            file.WriteText("\r\n");


            file.WriteText("boardWidth  " + gameState.mSTBoardCurrent.GetWidth( ) + "\r\n");
            file.WriteText("boardHeight " + gameState.mSTBoardCurrent.GetHeight( ) + "\r\n");

            file.WriteText("boardCurrent\r\n");

            int width  = 0;
            int height = 0;

            width  = gameState.mSTBoardCurrent.GetWidth( );
            height = gameState.mSTBoardCurrent.GetHeight( );

            int x = 0;
            int y = 0;

            for (y = height; y >= 1; y--) // top-down
            {
                for (x = 1; x <= width; x++)
                {
                    int cellShapeCode = 0;
                    cellShapeCode = (int)gameState.mSTBoardCurrent.GetCell(x, y);
                    file.WriteText(" " + String.Format("{0:d}", cellShapeCode));
                }
                file.WriteText("\r\n");
            }
            file.WriteText("\r\n");



            file.WriteText("pieceCurrent\r\n");
            file.WriteText("shape " + (int)gameState.mSTPieceCurrent.GetByteCodeValue( ) + "\r\n");
            file.WriteText("x " + gameState.mSTPieceCurrent.GetX( ) + "\r\n");
            file.WriteText("y " + gameState.mSTPieceCurrent.GetY( ) + "\r\n");
            file.WriteText("orientation " + gameState.mSTPieceCurrent.GetOrientation( ) + "\r\n");
            file.WriteText("\r\n");



            int intValue = 0;

            if (true == gameState.mGameOver)
            {
                intValue = 1;
            }
            file.WriteText("gameOver " + intValue + "\r\n");

            file.WriteText("iterationCountdownSeconds " + gameState.mIterationCountdownSeconds + "\r\n");
            file.WriteText("currentPiecePointValue " + gameState.mCurrentPiecePointValue + "\r\n");
            file.WriteText("completedRows " + gameState.mCompletedRows + "\r\n");
            file.WriteText("\r\n");



            file.WriteText("pieceSelectionSource " + (int)gameState.mSTPieceSequence.DirectGetPieceSelectionSource( ) + "\r\n");
            file.WriteText("pieceSelectionShapeCurrent " + (int)gameState.mSTPieceSequence.DirectGetCurrentPieceShape( ) + "\r\n");
            file.WriteText("pieceSelectionShapeNext " + (int)gameState.mSTPieceSequence.DirectGetNextPieceShape( ) + "\r\n");

            file.WriteText("cachedRandomSeedUsedMostRecentlyToInitializeRandomGenerator " + gameState.mSTPieceSequence.DirectGetSeedUsedMostRecentlyToInitializeRandomNumberGenerator( ) + "\r\n");


            file.WriteText("currentInternalRandomNumberGeneratorStateForPieceSequence " + gameState.mSTPieceSequence.DirectGetCurrentRandomNumberGeneratorInternalStateValue( ) + "\r\n");


            int alternateSZState = 0;

            if (true == gameState.mSTPieceSequence.DirectGetAlternateSZState( ))
            {
                alternateSZState = 1;
            }
            file.WriteText("alternatingSZState " + alternateSZState + "\r\n");

            file.WriteText("pieceSequenceTotalQueueElements " + gameState.mSTPieceSequence.DirectGetTotalQueueElements( ) + "\r\n");

            file.WriteText("pieceSequenceQueueElements " + "\r\n");

            int totalQueueElements = 0;

            totalQueueElements = gameState.mSTPieceSequence.DirectGetTotalQueueElements( );

            int indexQueueElement = 0;

            for (indexQueueElement = 0; indexQueueElement < totalQueueElements; indexQueueElement++)
            {
                int queueElementValue = 0;
                queueElementValue = gameState.mSTPieceSequence.DirectGetQueueElementByIndex(indexQueueElement);
                file.WriteText(" " + String.Format("{0:d}", queueElementValue));
                if (31 == (indexQueueElement % 32))
                {
                    file.WriteText("\r\n");
                }
            }
            file.WriteText("\r\n");
            file.WriteText("\r\n");


            // User options

            intValue = 0;
            if (true == gameState.mPaused)
            {
                intValue = 1;
            }
            file.WriteText("paused " + intValue + "\r\n");

            intValue = 0;
            if (true == gameState.mShowNextPiece)
            {
                intValue = 1;
            }
            file.WriteText("showNextPiece " + intValue + "\r\n");

            intValue = 0;
            if (true == gameState.mAI)
            {
                intValue = 1;
            }
            file.WriteText("aiActive " + intValue + "\r\n");

            intValue = 0;
            if (true == gameState.mSpawnFromVideoCapture)
            {
                intValue = 1;
            }
            file.WriteText("spawnFromVideoCapture " + intValue + "\r\n");

            intValue = 0;
            if (true == gameState.mOutputToRS232)
            {
                intValue = 1;
            }
            file.WriteText("outputToRS232 " + intValue + "\r\n");

            intValue = 0;
            if (true == gameState.mAutoRestart)
            {
                intValue = 1;
            }
            file.WriteText("autoRestart " + intValue + "\r\n");

            intValue = 0;
            if (true == gameState.mAutoWriteFile)
            {
                intValue = 1;
            }
            file.WriteText("autoWriteFile " + intValue + "\r\n");

            file.WriteText("gameSpeedAdjustment " + gameState.mGameSpeedAdjustment + "\r\n");

            intValue = 0;
            if (true == gameState.mShadowMode)
            {
                intValue = 1;
            }
            file.WriteText("shadowMode " + intValue + "\r\n");

            intValue = 0;
            if (true == gameState.mHintMode)
            {
                intValue = 1;
            }
            file.WriteText("hintMode " + intValue + "\r\n");

            intValue = 0;
            if (true == gameState.mMonochromeColorMode)
            {
                intValue = 1;
            }
            file.WriteText("monochromeColorMode " + intValue + "\r\n");


            // Currently-selected AI
            // (be sure to eliminate the double-quotes around the name when reading back in)
            file.WriteText("currentStrategyName " + "\"" + STStrategyManager.GetCurrentStrategyName( ) + "\"" + "\r\n");



            // Statistics for User Consideration Only

            file.WriteText("\r\n");

            file.WriteText("pieceHistogramLength " + (int)gameState.mPieceHistogram.Length + "\r\n");
            file.WriteText("pieceHistogram " + "\r\n");
            int i = 0;
            int n = 0;

            n = gameState.mPieceHistogram.Length;
            for (i = 0; i < n; i++)
            {
                file.WriteText(gameState.mPieceHistogram[i] + "\r\n");
            }
            file.WriteText("\r\n");
            file.WriteText("\r\n");



            // Height histogram
            // (find highest index with a non-zero count, and only show indices
            // up to that index on their own lines, with all subsequent zero
            // values condensed with 32 such values on a single line)

            file.WriteText("heightHistogramLength " + (int)gameState.mHeightHistogram.Length + "\r\n");

            file.WriteText("heightHistogram " + "\r\n");

            int maxIndexWithNonZeroCount = (-1);

            n = gameState.mHeightHistogram.Length;
            for (i = 0; i < n; i++)
            {
                if (gameState.mHeightHistogram[i] > 0)
                {
                    maxIndexWithNonZeroCount = i;
                }
            }

            for (i = 0; i <= maxIndexWithNonZeroCount; i++)
            {
                file.WriteText(gameState.mHeightHistogram[i] + "\r\n");
            }

            int k = 0;

            for (i = (maxIndexWithNonZeroCount + 1); i < n; i++)
            {
                file.WriteText(gameState.mHeightHistogram[i] + " ");
                k++;
                if (0 == (k % 32))
                {
                    file.WriteText("\r\n");
                }
            }

            file.WriteText("\r\n");
            file.WriteText("\r\n");



            file.WriteText("totalElapsedTimeSeconds " + gameState.mTotalElapsedTimeSeconds + "\r\n");

            file.WriteText("score " + gameState.mScore + "\r\n");

            file.WriteText("historicHighScore " + gameState.mHistoricHighScore + "\r\n");
            file.WriteText("historicHighRows " + gameState.mHistoricHighRows + "\r\n");
            file.WriteText("historicHighPieces " + gameState.mHistoricHighPieces + "\r\n");
            file.WriteText("historicCumulativeRows " + gameState.mHistoricCumulativeRows + "\r\n");
            file.WriteText("historicTotalGames " + gameState.mHistoricTotalGames + "\r\n");

            file.WriteText("\r\n");

            file.WriteText("historicRowsLength " + (int)gameState.mHistoricRows.Length + "\r\n");
            file.WriteText("historicRows " + "\r\n");
            n = gameState.mHistoricRows.Length;
            for (i = 0; i < n; i++)
            {
                file.WriteText(gameState.mHistoricRows[i] + "\r\n");
            }
            file.WriteText("\r\n");
            file.WriteText("\r\n");



            // Derived or copied values

            file.WriteText("pieceNext\r\n");
            file.WriteText("shape " + (int)gameState.mSTPieceNext.GetByteCodeValue( ) + "\r\n");
            file.WriteText("x " + gameState.mSTPieceNext.GetX( ) + "\r\n");
            file.WriteText("y " + gameState.mSTPieceNext.GetY( ) + "\r\n");
            file.WriteText("orientation " + gameState.mSTPieceNext.GetOrientation( ) + "\r\n");
            file.WriteText("\r\n");

            file.WriteText("pieceBestMove\r\n");
            file.WriteText("shape " + (int)gameState.mSTPieceBestMove.GetByteCodeValue( ) + "\r\n");
            file.WriteText("x " + gameState.mSTPieceBestMove.GetX( ) + "\r\n");
            file.WriteText("y " + gameState.mSTPieceBestMove.GetY( ) + "\r\n");
            file.WriteText("orientation " + gameState.mSTPieceBestMove.GetOrientation( ) + "\r\n");
            file.WriteText("\r\n");


            // state of animation of a an AI-executed move
            intValue = 0;
            if (true == gameState.mAnimateAIMovesEnable)
            {
                intValue = 1;
            }
            file.WriteText("animateAIMovesEnable " + intValue + "\r\n");
            file.WriteText("animateAIMovesStartingY " + gameState.mAnimateAIMovesStartingY + "\r\n");
            file.WriteText("animateAIMovesFinalSafeY " + gameState.mAnimateAIMovesFinalSafeY + "\r\n");
            file.WriteText("animateAITotalInitialCommands " + gameState.mAnimateAITotalInitialCommands + "\r\n");
            file.WriteText("animateAICommandsExecuted " + gameState.mAnimateAICommandsExecuted + "\r\n");
            file.WriteText("animateAICommandsPerRow " + gameState.mAnimateAICommandsPerRow + "\r\n");
            file.WriteText("animateAIMovesPendingRotation " + gameState.mAnimateAIMovesPendingRotation + "\r\n");
            file.WriteText("animateAIMovesPendingTranslation " + gameState.mAnimateAIMovesPendingTranslation + "\r\n");


            // Derived values

            // Scaled height histogram

            file.WriteText("\r\n");
            file.WriteText("\r\n");

            long sumOfCounts = 0;

            n = gameState.mHeightHistogram.Length;
            for (i = 0; i < n; i++)
            {
                sumOfCounts += gameState.mHeightHistogram[i];
            }

            if (sumOfCounts > 0)
            {
                file.WriteText("scaledHistogramOfPileHeights " + "\r\n");

                double fraction = 0.0;

                for (i = 0; i <= maxIndexWithNonZeroCount; i++)
                {
                    fraction = ((double)gameState.mHeightHistogram[i] / (double)sumOfCounts);
                    file.WriteText("" + fraction + "\r\n");
                }
            }



            file.WriteText("\r\n");
            file.WriteText("\r\n");


            file.Close( );

            return(true);
        }
        public static void HandleKeyPress
        (
            GR gr,
            IntPtr hwnd,
            int wParam,
            int lParam,
            STGame game,
            Keys keyCode,
            bool shiftKeyState,
            bool controlKeyState
        )
        {
            STGameState gameState = game.GetGameState( );



            // Priority of key press handling:
            //   (1) Instructions;    ESCAPE exits instructions;
            //   (2) File Menu;       ESCAPE exits file menu;
            //   (3) Calibrate;       ESCAPE cancels calibrate mode;
            //   (4) Video Capture;   ESCAPE quits application;
            //   (5) Normal;          ESCAPE quits application;



            // INSTRUCTIONS
            if (0 != game.InstructionGetState( ))
            {
                switch (keyCode)
                {
                case Keys.Down:
                case Keys.Next:     // Page-Down
                case Keys.Right:
                {
                    // Next page
                    game.InstructionsNextPage( );
                }
                break;

                case Keys.Up:
                case Keys.Prior:     // Page-Up
                case Keys.Left:
                {
                    // Previous page
                    game.InstructionsPreviousPage( );
                }
                break;

                default:
                {
                    // User hit a key, but it wasn't relevant, so exit menu.
                    game.InstructionsHide( );
                    // NOTE: Don't resume! : game.InputEventResume();
                }
                break;
                }
                return;
            }
            else if (keyCode == Keys.I)
            {
                game.InstructionsShow( );
                game.InputEventPause( );
                return;
            }



            // FILE LIST
            if (true == gameState.mShowFileList)
            {
                switch (keyCode)
                {
                case Keys.Next:     // Page-Down
                {
                    // Next page
                    gameState.mFirstItem += 20;
                }
                break;

                case Keys.Prior:     // Page-Up
                {
                    // Previous page
                    gameState.mFirstItem -= 20;
                }
                break;

                case Keys.Down:
                {
                    // Next Item
                    gameState.mRelativeItem++;
                    if (gameState.mRelativeItem > 19)
                    {
                        gameState.mFirstItem++;
                        gameState.mRelativeItem = 19;
                    }
                }
                break;

                case Keys.Up:
                {
                    // Previous Item
                    gameState.mRelativeItem--;
                    if (gameState.mRelativeItem < 0)
                    {
                        gameState.mFirstItem--;
                        gameState.mRelativeItem = 0;
                    }
                }
                break;

                case Keys.Return:
                {
                    // Load item
                    gameState.mLoadFlag = true;
                }
                break;

                default:
                {
                    // User hit a key, but it wasn't relevant, so exit menu.
                    gameState.mShowFileList = false;
                    // NOTE: Don't resume. : game.InputEvent_Resume();
                }
                break;
                }
                return;
            }
            else if ((keyCode == Keys.L) && (true == shiftKeyState))
            {
                // SHIFT-L will read a text file in to the game state.
                game.InputEventPause( );
                gameState.mShowFileList = true;
                gameState.mFirstItem    = 0;
                gameState.mRelativeItem = 0;
                gameState.mLoadFlag     = false;

                STEngine.GetFileList().ScanDirectory(STEngine.GetApplicationPath( ));

                return;
            }



            // Calibrate Mode
            // (NOTE: See how normal mode enters calibrate mode by pressing 'C'.)
            if (true == game.GetCalibrationModeFlagValue( ))
            {
                if ((Keys.Escape == keyCode) || (Keys.C == keyCode))
                {
                    game.SetCalibrationModeFlagValue(false);
                    game.InputEventResume( );
                    return;
                }

                if (keyCode == Keys.V)
                {
                    if (false == game.GameIsSpawnFromVideoCapture( ))
                    {
                        // Set up sane conditions
                        game.InputEventReset( );
                        game.InputEventShowNextPieceOff( );
                        game.InputEventAutoRestartOff( );
                        // Initialize Video Capture
                        STEngine.GetVideoProcessing( ).Initialize(gr, hwnd);
                        STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                        game.InputEventVideoStart( );
                        STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                    }
                    else
                    {
                        STEngine.GetVideoProcessing( ).Terminate( );
                        STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                        game.InputEventVideoStop( );
                        STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                    }
                    return;
                }

                switch (keyCode)
                {
                // training mode piece selection
                case Keys.D0: game.SetCalibrationModeShapeCode(0); break;

                case Keys.D1: game.SetCalibrationModeShapeCode(1); break;

                case Keys.D2: game.SetCalibrationModeShapeCode(2); break;

                case Keys.D3: game.SetCalibrationModeShapeCode(3); break;

                case Keys.D4: game.SetCalibrationModeShapeCode(4); break;

                case Keys.D5: game.SetCalibrationModeShapeCode(5); break;

                case Keys.D6: game.SetCalibrationModeShapeCode(6); break;

                case Keys.D7: game.SetCalibrationModeShapeCode(7); break;

                case Keys.D8: game.SetCalibrationModeShapeCode(0); break;

                case Keys.D9: game.SetCalibrationModeShapeCode(0); break;
                }
                return;
            }



            // Video Capture
            // The following is not mutually-exclusive with normal game play.
            if (true == game.GameIsSpawnFromVideoCapture( ))
            {
                if (keyCode == Keys.Return)
                {
                    STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                    game.ClearPreviousClassification( );
                    game.InputEventReset( );
                    System.Threading.Thread.Sleep(200);
                    STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                    game.ClearPreviousClassification( );
                    game.InputEventReset( );
                    System.Threading.Thread.Sleep(200);
                    STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                    game.ClearPreviousClassification( );
                    game.InputEventReset( );
                    System.Threading.Thread.Sleep(200);
                    STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                    game.ClearPreviousClassification( );
                    game.InputEventReset( );
                    System.Threading.Thread.Sleep(200);
                }

                if (keyCode == Keys.V)
                {
                    if (false == game.GameIsSpawnFromVideoCapture( ))
                    {
                        // Set up sane conditions
                        game.InputEventReset( );
                        game.InputEventShowNextPieceOff( );
                        game.InputEventAutoRestartOff( );
                        // Initialize Video Capture
                        STEngine.GetVideoProcessing( ).Initialize(gr, hwnd);
                        STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                        game.InputEventVideoStart( );
                        STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                    }
                    else
                    {
                        STEngine.GetVideoProcessing( ).Terminate( );
                        STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                        game.InputEventVideoStop( );
                        STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                    }
                    return;
                }
            }



            // Console Mode
            if (true == gameState.mShowConsole)
            {
                if (keyCode == Keys.Delete)
                {
                    STEngine.GetConsole( ).ClearAllLines( );
                }
                else
                {
                    // Any key other than delete or P (pause) exits console mode.
                    if (keyCode != Keys.P)
                    {
                        gameState.mShowConsole = false;
                    }
                }
            }
            else
            {
                if ((keyCode == Keys.Q) && (true == shiftKeyState))
                {
                    // SHIFT-Q : Console
                    gameState.mShowConsole = true;
                }
            }



            // Normal Game Play
            // QUIT KEY:  ESCAPE
            if (keyCode == Keys.Escape)
            {
                Form form = (Form)STEngine.GetMainForm( );
                form.Close( );
                return;
            }

            // Enter Calibrate Mode
            if (keyCode == Keys.C)
            {
                game.SetCalibrationModeFlagValue(true);
                game.SetCalibrationModeShapeCode(1);
                game.InputEventPause( );
            }

            // Enable Video Capture
            if (keyCode == Keys.V)
            {
                if (false == game.GameIsSpawnFromVideoCapture( ))
                {
                    // Set up sane conditions
                    game.InputEventReset( );
                    game.InputEventShowNextPieceOff( );
                    game.InputEventAutoRestartOff( );
                    // Initialize Video Capture
                    STEngine.GetVideoProcessing( ).Initialize(gr, hwnd);
                    STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                    game.InputEventVideoStart( );
                    STEngine.GetVideoProcessing( ).ClearRegionStatus( );
                }
            }

            // Reset Game
            if (keyCode == Keys.Return)
            {
                if (true == shiftKeyState)
                {
                    game.InputEventHardReset( );
                }
                else
                {
                    game.InputEventReset( );
                }
            }

            if (keyCode == Keys.P)
            {
                if (true == game.GameIsPaused( ))
                {
                    game.InputEventResume( );
                }
                else
                {
                    game.InputEventPause( );
                }
            }

            if (keyCode == Keys.A)
            {
                if (true == shiftKeyState)
                {
                    STStrategyManager.SelectNextStrategy( );
                }
                else
                {
                    if (true == game.GameIsAI( ))
                    {
                        game.InputEventAIStop( );
                    }
                    else
                    {
                        game.InputEventAIStart( );
                    }
                }
            }

            if (keyCode == Keys.T)
            {
                if (game.GameIsOutputToRS232( ))
                {
                    game.InputEventRS232Stop( );
                    STRS232.TerminatePort( );
                }
                else
                {
                    STRS232.InitializePort( );
                    game.InputEventRS232Start( );
                }
            }

            if ((keyCode == Keys.Subtract) || (keyCode == Keys.OemMinus)) //  0xbd
            {
                if (false == shiftKeyState)
                {
                    game.InputEventGameSpeedDecrease( );
                }
            }
            if ((keyCode == Keys.Add) || (keyCode == Keys.Oemplus)) // 0xbb
            {
                if (false == shiftKeyState)
                {
                    game.InputEventGameSpeedIncrease( );
                }
            }

            if ((keyCode == Keys.W) && (true == shiftKeyState))
            {
                // SHIFT-W will write out a text file (c:\tetris_state.txt)
                game.InputEventGameStateWriteToFile( );
            }

            if (keyCode == Keys.Next) // Page-Down
            {
                // Page-Down: Decrease Board Size
                game.InputEventGameBoardDecrease( );
            }
            if (keyCode == Keys.Prior) // Page-Up
            {
                // Page-Up:  Increase Board Size
                game.InputEventGameBoardIncrease( );
            }


            if (true == controlKeyState)
            {
                if (keyCode == Keys.Up)
                {
                    game.InputEventGameBoardIncreaseHeight( );
                }
                if (keyCode == Keys.Left)
                {
                    game.InputEventGameBoardDecreaseWidth( );
                }
                if (keyCode == Keys.Right)
                {
                    game.InputEventGameBoardIncreaseWidth( );
                }
                if (keyCode == Keys.Down)
                {
                    game.InputEventGameBoardDecreaseHeight( );
                }
            }



            // COLOR SCHEME
            if ((keyCode == Keys.K) && (true == shiftKeyState))
            {
                if (false == game.GetGameState( ).mMonochromeColorMode)
                {
                    game.GetGameState( ).mMonochromeColorMode = true;
                }
                else
                {
                    game.GetGameState( ).mMonochromeColorMode = false;
                }
            }



            // Non Video-Capture Options
            if (false == game.GameIsSpawnFromVideoCapture( ))
            {
                // Only respond to user piece-control input if AI is not active.
                if (false == game.GameIsAI( ))
                {
                    if (false == controlKeyState)
                    {
                        if (keyCode == Keys.Up)
                        {
                            game.InputEventRotate( );
                        }
                        if (keyCode == Keys.Left)
                        {
                            game.InputEventLeft( );
                        }
                        if (keyCode == Keys.Right)
                        {
                            game.InputEventRight( );
                        }
                        if (keyCode == Keys.Down)
                        {
                            game.InputEventDrop( );
                        }
                        if (keyCode == Keys.Space)
                        {
                            game.InputEventDrop( );
                        }
                    }
                }

                if (keyCode == Keys.Z)
                {
                    if ((STPieceSequence.STPieceSelectionSource.AlternatingSAndZ) ==
                        game.GetPieceSequenceSourceType( ))
                    {
                        // Since we're in S/Z mode, stop.
                        game.InputEventSZPieceModeStop( );
                    }
                    else
                    {
                        // Start S/Z mode.
                        game.InputEventSZPieceModeStart( );
                    }
                }

                if (keyCode == Keys.S)
                {
                    // S will cycle the shadow mode.
                    game.InputEventShadowModeCycle( );
                }

                if ((keyCode == Keys.J) && (true == shiftKeyState))
                {
                    // SHIFT-J : Add line of random junk to bottom of the pile.
                    game.InputEventAddRowOfJunk( );
                }

                if ((keyCode == Keys.H) && (true == shiftKeyState))
                {
                    // SHIFT-H : Hint Mode
                    if (true == game.GameIsHintMode( ))
                    {
                        game.InputEventHintModeStop( );
                    }
                    else
                    {
                        game.InputEventHintModeStart( );
                    }
                }

                if (keyCode == Keys.N)
                {
                    if (true == game.GameIsShowNextPiece( ))
                    {
                        game.InputEventShowNextPieceOff( );
                    }
                    else
                    {
                        game.InputEventShowNextPieceOn( );
                    }
                }

                if (keyCode == Keys.X)
                {
                    game.InputEventToggleMoveAnimation( );
                }

                if (keyCode == Keys.U)
                {
                    if (true == game.GameIsAutoRestart( ))
                    {
                        game.InputEventAutoRestartOff( );
                    }
                    else
                    {
                        game.InputEventAutoRestartOn( );
                    }
                }
                if (keyCode == Keys.F)
                {
                    game.InputEventToggleAutoWriteFile( );
                }


                if ((keyCode == Keys.R) && (true == shiftKeyState))
                {
                    // SHIFT-R : Soft reset (game goes back to same random seed)
                    game.InputEventSoftReset( );
                }
            }
        }
Esempio n. 4
0
        public static bool ReadGameStateFromFile
        (
            String filePathAndName,
            STGameState gameState
        )
        {
            STFileReader file = new STFileReader( );

            bool successfullyOpenedFile = false;

            successfullyOpenedFile =
                file.ReadFileAndGetClusterStrings(filePathAndName);

            if (false == successfullyOpenedFile)
            {
                return(false);
            }

            int totalStrings = 0;

            totalStrings = file.GetTotalStrings( );

            int indexString = 0;

            int offset = 0;

            int intValue = 0;

            long longValue = 0;



            // BOARD

            int boardWidth = 0;

            file.FindTextAndInteger32("boardWidth", ref boardWidth);

            int boardHeight = 0;

            file.FindTextAndInteger32("boardHeight", ref boardHeight);

            int x = 0;
            int y = 0;

            if ((boardWidth > 0) && (boardHeight > 0))
            {
                gameState.mSTBoardCurrent.SetBoardDimensions(boardWidth, boardHeight);

                indexString = file.FindString("boardCurrent");
                if (indexString >= 0)
                {
                    for (y = boardHeight; y >= 1; y--) // top-down
                    {
                        for (x = 1; x <= boardWidth; x++)
                        {
                            // string at (index + 1) corresponds to
                            //   ( x = 0, y = boardHeight )

                            offset =
                                (1 + ((boardWidth * (boardHeight - y)) + (x - 1)));

                            intValue =
                                file.GetStringAsInteger32ByIndex(indexString + offset);

                            gameState.mSTBoardCurrent.SetCell(x, y, (byte)intValue);
                        }
                    }
                }
            }



            int shape       = 0;
            int orientation = 0;

            // CURRENT PIECE

            gameState.mSTPieceCurrent.Clear( );

            indexString = file.FindString("pieceCurrent");

            if (indexString >= 0)
            {
                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "shape", ref shape);

                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "x", ref x);

                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "y", ref y);

                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "orientation", ref orientation);

                gameState.mSTPieceCurrent.SetShape((STPiece.STPieceShape)shape);
                gameState.mSTPieceCurrent.SetX(x);
                gameState.mSTPieceCurrent.SetY(y);
                gameState.mSTPieceCurrent.SetOrientation(orientation);
            }



            // BASIC GAME STATE

            bool gameOver = false;

            file.FindTextAndInteger32("gameOver", ref intValue);
            if (0 != intValue)
            {
                gameOver = true;
            }
            gameState.mGameOver = gameOver;

            double iterationCountdownSeconds = 0.0;

            file.FindTextAndDouble("iterationCountdownSeconds", ref iterationCountdownSeconds);
            gameState.mIterationCountdownSeconds = iterationCountdownSeconds;

            long currentPiecePointValue = 0;

            file.FindTextAndInteger64("currentPiecePointValue", ref currentPiecePointValue);
            gameState.mCurrentPiecePointValue = currentPiecePointValue;

            long completedRows = 0;

            file.FindTextAndInteger64("completedRows", ref completedRows);
            gameState.mCompletedRows = completedRows;



            // PIECE SELECTION STATE

            int pieceSelectionSource = 0;

            file.FindTextAndInteger32("pieceSelectionSource", ref pieceSelectionSource);
            gameState.mSTPieceSequence.DirectSetPieceSelectionSource((STPieceSequence.STPieceSelectionSource)pieceSelectionSource);

            int pieceSelectionShapeCurrent = 0;

            file.FindTextAndInteger32("pieceSelectionShapeCurrent", ref pieceSelectionShapeCurrent);
            gameState.mSTPieceSequence.DirectSetCurrentPieceShape((STPiece.STPieceShape)pieceSelectionShapeCurrent);

            int pieceSelectionShapeNext = 0;

            file.FindTextAndInteger32("pieceSelectionShapeNext", ref pieceSelectionShapeNext);
            gameState.mSTPieceSequence.DirectSetNextPieceShape((STPiece.STPieceShape)pieceSelectionShapeNext);

            long cachedRandomSeedUsedMostRecentlyToInitializeRandomGenerator = 0;

            file.FindTextAndInteger64("cachedRandomSeedUsedMostRecentlyToInitializeRandomGenerator", ref cachedRandomSeedUsedMostRecentlyToInitializeRandomGenerator);
            gameState.mSTPieceSequence.DirectSetSeedUsedMostRecentlyToInitializeRandomNumberGenerator(cachedRandomSeedUsedMostRecentlyToInitializeRandomGenerator);

            long currentInternalRandomNumberGeneratorStateForPieceSequence = 0;

            file.FindTextAndInteger64("currentInternalRandomNumberGeneratorStateForPieceSequence", ref currentInternalRandomNumberGeneratorStateForPieceSequence);
            gameState.mSTPieceSequence.DirectSetCurrentRandomNumberGeneratorInternalStateValue(currentInternalRandomNumberGeneratorStateForPieceSequence);

            bool alternatingSZState = false;

            file.FindTextAndInteger32("alternatingSZState", ref intValue);
            if (0 != intValue)
            {
                alternatingSZState = true;
            }
            gameState.mSTPieceSequence.DirectSetAlternateSZState(alternatingSZState);

            gameState.mSTPieceSequence.DirectQueueClear( );

            int pieceSequenceTotalQueueElements = 0;

            file.FindTextAndInteger32("pieceSequenceTotalQueueElements", ref pieceSequenceTotalQueueElements);

            indexString = file.FindString("pieceSequenceQueueElements");
            if ((pieceSequenceTotalQueueElements > 0) && (indexString >= 0))
            {
                for (offset = 0; offset < pieceSequenceTotalQueueElements; offset++)
                {
                    intValue = file.GetStringAsInteger32ByIndex((indexString + 1) + offset);
                    gameState.mSTPieceSequence.DirectAddQueueElement(intValue);
                }
            }



            // USER OPTIONS


            // DO NOT RESTORE PAUSED STATE
            // bool paused = false;
            // file.FindTextAndInteger32( "paused", ref intValue );
            // if (0 != intValue) { paused = true; }
            // gameState.mPaused = paused;
            gameState.mPaused = true; // FORCE TO BE PAUSED UPON LOAD


            bool showNextPiece = false;

            file.FindTextAndInteger32("showNextPiece", ref intValue);
            if (0 != intValue)
            {
                showNextPiece = true;
            }
            gameState.mShowNextPiece = showNextPiece;

            bool aiActive = false;

            file.FindTextAndInteger32("aiActive", ref intValue);
            if (0 != intValue)
            {
                aiActive = true;
            }
            gameState.mAI = aiActive;

            bool spawnFromVideoCapture = false;

            file.FindTextAndInteger32("spawnFromVideoCapture", ref intValue);
            if (0 != intValue)
            {
                spawnFromVideoCapture = true;
            }
            gameState.mSpawnFromVideoCapture = spawnFromVideoCapture;

            bool outputToRS232 = false;

            file.FindTextAndInteger32("outputToRS232", ref intValue);
            if (0 != intValue)
            {
                outputToRS232 = true;
            }
            gameState.mOutputToRS232 = outputToRS232;

            bool autoRestart = false;

            file.FindTextAndInteger32("autoRestart", ref intValue);
            if (0 != intValue)
            {
                autoRestart = true;
            }
            gameState.mAutoRestart = autoRestart;

            bool autoWriteFile = false;

            file.FindTextAndInteger32("autoWriteFile", ref intValue);
            if (0 != intValue)
            {
                autoWriteFile = true;
            }
            gameState.mAutoWriteFile = autoWriteFile;

            int gameSpeedAdjustment = 0;

            file.FindTextAndInteger32("gameSpeedAdjustment", ref gameSpeedAdjustment);
            gameState.mGameSpeedAdjustment = gameSpeedAdjustment;

            bool shadowMode = false;

            file.FindTextAndInteger32("shadowMode", ref intValue);
            if (0 != intValue)
            {
                shadowMode = true;
            }
            gameState.mShadowMode = shadowMode;

            bool hintMode = false;

            file.FindTextAndInteger32("hintMode", ref intValue);
            if (0 != intValue)
            {
                hintMode = true;
            }
            gameState.mHintMode = hintMode;

            bool monochromeColorMode = false;

            file.FindTextAndInteger32("monochromeColorMode", ref intValue);
            if (0 != intValue)
            {
                monochromeColorMode = true;
            }
            gameState.mMonochromeColorMode = monochromeColorMode;



            String currentStrategyName = "";

            indexString = file.FindString("currentStrategyName");
            if (indexString >= 0)
            {
                int doubleQuoteCount = 0;
                for
                (
                    offset = 1;
                    ((offset < 64) && (doubleQuoteCount < 2) && ((indexString + offset) < totalStrings));
                    offset++
                )
                {
                    String stringTemp = "";
                    file.GetStringByIndex((indexString + offset), ref stringTemp);
                    if (offset > 1)
                    {
                        currentStrategyName += " ";
                    }
                    if (true == stringTemp.Contains("\""))
                    {
                        doubleQuoteCount++;
                        stringTemp = stringTemp.Replace("\"", "");
                    }
                    currentStrategyName += stringTemp;
                }
            }
            STStrategyManager.SetCurrentStrategyByName(currentStrategyName);



            // PIECE HISTOGRAM

            for (offset = 0; offset < gameState.mPieceHistogram.Length; offset++)
            {
                gameState.mPieceHistogram[offset] = 0;
            }

            int pieceHistogramLength = 0;

            file.FindTextAndInteger32("pieceHistogramLength", ref pieceHistogramLength);

            if (pieceHistogramLength >= gameState.mPieceHistogram.Length)
            {
                indexString = file.FindString("pieceHistogram");
                if (indexString >= 0)
                {
                    for (offset = 0; offset < gameState.mPieceHistogram.Length; offset++)
                    {
                        longValue = file.GetStringAsInteger64ByIndex((indexString + 1) + offset);

                        gameState.mPieceHistogram[offset] = longValue;
                    }
                }
            }



            // HEIGHT HISTOGRAM

            for (offset = 0; offset < gameState.mHeightHistogram.Length; offset++)
            {
                gameState.mHeightHistogram[offset] = 0;
            }

            int heightHistogramLength = 0;

            file.FindTextAndInteger32("heightHistogramLength", ref heightHistogramLength);

            if (heightHistogramLength >= gameState.mHeightHistogram.Length)
            {
                indexString = file.FindString("heightHistogram");
                if (indexString >= 0)
                {
                    for (offset = 0; offset < gameState.mHeightHistogram.Length; offset++)
                    {
                        longValue = file.GetStringAsInteger64ByIndex((indexString + 1) + offset);

                        gameState.mHeightHistogram[offset] = longValue;
                    }
                }
            }



            // OTHER STATISTICS

            double totalElapsedTimeSeconds = 0.0;

            file.FindTextAndDouble("totalElapsedTimeSeconds", ref totalElapsedTimeSeconds);
            gameState.mTotalElapsedTimeSeconds = totalElapsedTimeSeconds;

            long score = 0;

            file.FindTextAndInteger64("score", ref score);
            gameState.mScore = score;

            long historicHighScore = 0;

            file.FindTextAndInteger64("historicHighScore", ref historicHighScore);
            gameState.mHistoricHighScore = historicHighScore;

            long historicHighRows = 0;

            file.FindTextAndInteger64("historicHighRows", ref historicHighRows);
            gameState.mHistoricHighRows = historicHighRows;

            long historicHighPieces = 0;

            file.FindTextAndInteger64("historicHighPieces", ref historicHighPieces);
            gameState.mHistoricHighPieces = historicHighPieces;

            long historicCumulativeRows = 0;

            file.FindTextAndInteger64("historicCumulativeRows", ref historicCumulativeRows);
            gameState.mHistoricCumulativeRows = historicCumulativeRows;

            long historicTotalGames = 0;

            file.FindTextAndInteger64("historicTotalGames", ref historicTotalGames);
            gameState.mHistoricTotalGames = historicTotalGames;



            // HISTORIC ROWS LIST

            for (offset = 0; offset < gameState.mHistoricRows.Length; offset++)
            {
                gameState.mHistoricRows[offset] = 0;
            }

            int historicRowsLength = 0;

            file.FindTextAndInteger32("historicRowsLength", ref historicRowsLength);

            if (historicRowsLength >= gameState.mHistoricRows.Length)
            {
                indexString = file.FindString("historicRows");
                if (indexString >= 0)
                {
                    for (offset = 0; offset < gameState.mHistoricRows.Length; offset++)
                    {
                        longValue = file.GetStringAsInteger64ByIndex((indexString + 1) + offset);

                        gameState.mHistoricRows[offset] = longValue;
                    }
                }
            }



            // NEXT PIECE

            gameState.mSTPieceNext.Clear( );

            indexString = file.FindString("pieceNext");

            if (indexString >= 0)
            {
                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "shape", ref shape);

                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "x", ref x);

                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "y", ref y);

                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "orientation", ref orientation);

                gameState.mSTPieceNext.SetShape((STPiece.STPieceShape)shape);
                gameState.mSTPieceNext.SetX(x);
                gameState.mSTPieceNext.SetY(y);
                gameState.mSTPieceNext.SetOrientation(orientation);
            }



            // BEST-MOVE PIECE

            gameState.mSTPieceBestMove.Clear( );

            indexString = file.FindString("pieceBestMove");

            if (indexString >= 0)
            {
                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "shape", ref shape);

                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "x", ref x);

                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "y", ref y);

                file.FindTextAndInteger32AtOrAfterIndex
                    (indexString, "orientation", ref orientation);

                gameState.mSTPieceBestMove.SetShape((STPiece.STPieceShape)shape);
                gameState.mSTPieceBestMove.SetX(x);
                gameState.mSTPieceBestMove.SetY(y);
                gameState.mSTPieceBestMove.SetOrientation(orientation);
            }



            // move animation state

            bool animateAIMovesEnable = false;

            file.FindTextAndInteger32("animateAIMovesEnable", ref intValue);
            if (0 != intValue)
            {
                animateAIMovesEnable = true;
            }
            gameState.mAnimateAIMovesEnable = animateAIMovesEnable;

            int animateAIMovesStartingY = 0;

            file.FindTextAndInteger32("animateAIMovesStartingY", ref animateAIMovesStartingY);
            gameState.mAnimateAIMovesStartingY = animateAIMovesStartingY;

            int animateAIMovesFinalSafeY = 0;

            file.FindTextAndInteger32("animateAIMovesFinalSafeY", ref animateAIMovesFinalSafeY);
            gameState.mAnimateAIMovesFinalSafeY = animateAIMovesFinalSafeY;

            int animateAITotalInitialCommands = 0;

            file.FindTextAndInteger32("animateAITotalInitialCommands", ref animateAITotalInitialCommands);
            gameState.mAnimateAITotalInitialCommands = animateAITotalInitialCommands;

            int animateAICommandsExecuted = 0;

            file.FindTextAndInteger32("animateAICommandsExecuted", ref animateAICommandsExecuted);
            gameState.mAnimateAICommandsExecuted = animateAICommandsExecuted;

            double animateAICommandsPerRow = 0.0;

            file.FindTextAndDouble("animateAICommandsPerRow", ref animateAICommandsPerRow);
            gameState.mAnimateAICommandsPerRow = animateAICommandsPerRow;

            int animateAIMovesPendingRotation = 0;

            file.FindTextAndInteger32("animateAIMovesPendingRotation", ref animateAIMovesPendingRotation);
            gameState.mAnimateAIMovesPendingRotation = animateAIMovesPendingRotation;

            int animateAIMovesPendingTranslation = 0;

            file.FindTextAndInteger32("animateAIMovesPendingTranslation", ref animateAIMovesPendingTranslation);
            gameState.mAnimateAIMovesPendingTranslation = animateAIMovesPendingTranslation;


            return(true);
        }