Esempio n. 1
0
        /// <summary>
        /// Create new replay for a specific track number.
        /// </summary>
        /// <param name="setTrackNum">Set track number</param>
        public Replay(int setTrackNum, bool createNew, Track track)
        {
            trackNum = setTrackNum;

            // if creating new, we're done
            if (createNew == true)
            {
                return;
            }

            bool replayFileFound = false;

            FileHelper.StorageContainerMRE.WaitOne();
            FileHelper.StorageContainerMRE.Reset();

            try
            {
                StorageDevice storageDevice = FileHelper.XnaUserDevice;
                if ((storageDevice != null) && storageDevice.IsConnected)
                {
                    IAsyncResult async = storageDevice.BeginOpenContainer("RacingGame", null, null);

                    async.AsyncWaitHandle.WaitOne();

                    using (StorageContainer container =
                        storageDevice.EndOpenContainer(async))
                    {
                        async.AsyncWaitHandle.Close();
                        if (container.FileExists(ReplayFilenames[trackNum]))
                        {
                            using (Stream stream = container.OpenFile(ReplayFilenames[trackNum],
                                FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                using (BinaryReader reader = new BinaryReader(stream))
                                {
                                    // Load total lap time
                                    lapTime = reader.ReadSingle();

                                    // Load matrix values
                                    int numOfMatrixValues = reader.ReadInt32();
                                    for (int num = 0; num < numOfMatrixValues; num++)
                                        trackMatrixValues.Add(
                                            FileHelper.ReadMatrix(reader));

                                    // Load checkpoint times
                                    int numOfCheckpointTimes = reader.ReadInt32();
                                    for (int num = 0; num < numOfCheckpointTimes; num++)
                                        checkpointTimes.Add(reader.ReadSingle());
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                System.Diagnostics.Trace.WriteLine("Settings Load Failure: " + exc.ToString());
            }

            FileHelper.StorageContainerMRE.Set();

            // Load if possible
            if (!replayFileFound && File.Exists(Path.Combine(
                Directories.ContentDirectory, ReplayFilenames[trackNum])))
            {
                using (Stream stream = TitleContainer.OpenStream(
                    "Content\\" + ReplayFilenames[trackNum]))
                {
                    using (BinaryReader reader = new BinaryReader(stream))
                    {
                        // Load total lap time
                        lapTime = reader.ReadSingle();

                        // Load matrix values
                        int numOfMatrixValues = reader.ReadInt32();
                        for (int num = 0; num < numOfMatrixValues; num++)
                            trackMatrixValues.Add(
                                FileHelper.ReadMatrix(reader));

                        // Load checkpoint times
                        int numOfCheckpointTimes = reader.ReadInt32();
                        for (int num = 0; num < numOfCheckpointTimes; num++)
                            checkpointTimes.Add(reader.ReadSingle());
                    }
                }
            }

            if (!replayFileFound)
            {
                // Create new default replay for this track!
                // Get all track positions based on the current top highscore time!
                lapTime = Highscores.GetTopLapTime(trackNum);
                int numOfMatrixValues = 1 + (int)(lapTime / TrackMatrixIntervals);

                float lastTrackPos = 0.0f;
                int oldTrackSegmentNumber = 0;

                // Go twice as long and abort when we reach the finish line!
                for (int num = 0; num < numOfMatrixValues * 2; num++)
                {
                    // See Landscape.TestRenderLandscape for more details.
                    float carTrackPos = 0.00001f +
                        ((float)num / (float)(numOfMatrixValues - 1));// *
                    //not needed, scaled in GetTrackPositionMatrix: track.Length;
                    float difference = carTrackPos - lastTrackPos;
                    carTrackPos = lastTrackPos + difference * 0.1f;
                    lastTrackPos = carTrackPos;

                    float roadWidth, nextRoadWidth;
                    Matrix carMatrix =
                        track.GetTrackPositionMatrix(carTrackPos,
                        out roadWidth, out nextRoadWidth);

                    // Store
                    trackMatrixValues.Add(carMatrix);

                    // Also check if we passed a checkpoint
                    int trackSegmentNumber =
                        (int)(carTrackPos * track.NumberOfSegments);
                    // Segment changed
                    if (trackSegmentNumber != oldTrackSegmentNumber)
                    {
                        // Check if we passed a checkpoint.
                        for (int checkpointNum = 0; checkpointNum < track.
                            CheckpointSegmentPositions.Count; checkpointNum++)
                        {
                            // We have to check if we are between the old
                            // and the current track segement numbers, we
                            // might skip one or two in 200ms.
                            if (track.CheckpointSegmentPositions[checkpointNum] >
                                oldTrackSegmentNumber &&
                                track.CheckpointSegmentPositions[checkpointNum] <=
                                trackSegmentNumber)
                            {
                                // We passed that checkpoint, add the simulated time
                                checkpointTimes.Add(
                                   lapTime * (float)num / (float)(numOfMatrixValues - 1));
                                break;
                            }
                        }
                    }
                    oldTrackSegmentNumber = trackSegmentNumber;

                    // Reached finish?
                    if (carTrackPos >= 1.0f)
                        // Then abort, do not add more.
                        break;
                }

                // Add the final checkpoint for the laptime
                checkpointTimes.Add(lapTime);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Create new replay for a specific track number.
        /// </summary>
        /// <param name="setTrackNum">Set track number</param>
        public Replay(int setTrackNum, bool createNew, Track track)
        {
            trackNum = setTrackNum;

            // If not creating new, load it from file!
            if (createNew == false)
            {
                // Load if possible
                if (File.Exists(Path.Combine(
                    Directories.ContentDirectory, ReplayFilenames[trackNum])))
                {
                    FileStream stream = FileHelper.LoadGameContentFile(
                        "Content\\"+ReplayFilenames[trackNum]);
                    BinaryReader reader = new BinaryReader(stream);

                    // Load total lap time
                    lapTime = reader.ReadSingle();

                    // Load matrix values
                    int numOfMatrixValues = reader.ReadInt32();
                    for (int num = 0; num < numOfMatrixValues; num++)
                        trackMatrixValues.Add(
                            FileHelper.ReadMatrix(reader));

                    // Load checkpoint times
                    int numOfCheckpointTimes = reader.ReadInt32();
                    for (int num = 0; num < numOfCheckpointTimes; num++)
                        checkpointTimes.Add(reader.ReadSingle());

                    // Loading complete, close.
                    stream.Close();
                } // if
                else
                {
                    // Create new default replay for this track!
                    // Get all track positions based on the current top highscore time!
                    lapTime = Highscores.GetTopLapTime(trackNum);
                    int numOfMatrixValues = 1 + (int)(lapTime / TrackMatrixIntervals);

                    float lastTrackPos = 0.0f;
                    int oldTrackSegmentNumber = 0;

                    // Go twice as long and abort when we reach the finish line!
                    for (int num = 0; num < numOfMatrixValues*2; num++)
                    {
                        // See Landscape.TestRenderLandscape for more details.
                        float carTrackPos = 0.00001f +
                            ((float)num / (float)(numOfMatrixValues - 1));// *
                            //not needed, scaled in GetTrackPositionMatrix: track.Length;
                        float difference = carTrackPos - lastTrackPos;
                        carTrackPos = lastTrackPos + difference * 0.1f;
                        lastTrackPos = carTrackPos;

                        float roadWidth, nextRoadWidth;
                        Matrix carMatrix =
                            track.GetTrackPositionMatrix(carTrackPos,
                            out roadWidth, out nextRoadWidth);

                        //Log.Write("create num=" + num +
                        //	"\ncarTrackPos=" + carTrackPos +
                        //	"\ncarMatrix=" + carMatrix);

                        // Interpolate carPos a little
                        //not good:
                        //carPos = Vector3.SmoothStep(carPos, carMatrix.Translation, 0.25f);
                        //carMatrix.Translation = carPos;

                        // Store
                        trackMatrixValues.Add(carMatrix);

                        // Also check if we passed a checkpoint
                        int trackSegmentNumber = (int)(carTrackPos * track.NumberOfSegments);
                        // Segment changed
                        if (trackSegmentNumber != oldTrackSegmentNumber)
                        {
                            // Check if we passed a checkpoint.
                            for (int checkpointNum = 0; checkpointNum < track.
                                CheckpointSegmentPositions.Count; checkpointNum++)
                            {
                                // We have to check if we are between the old and the current
                                // track segement numbers, we might skip one or two in 200ms.
                                if (track.CheckpointSegmentPositions[checkpointNum] >
                                    oldTrackSegmentNumber &&
                                    track.CheckpointSegmentPositions[checkpointNum] <=
                                    trackSegmentNumber)
                                {
                                    // We passed that checkpoint, add the simulated time
                                    checkpointTimes.Add(
                                        lapTime * (float)num / (float)(numOfMatrixValues - 1));
                                    break;
                                } // if
                            } // for
                        } // if
                        oldTrackSegmentNumber = trackSegmentNumber;

                        // Reached finish?
                        if (carTrackPos >= 1.0f)
                            // Then abort, do not add more.
                            break;
                    } // for (all matrix values)

                    // Add the final checkpoint for the laptime
                    checkpointTimes.Add(lapTime);
                } // else (not loading)
            } // if (createNew == false)
        }