Esempio n. 1
0
        public static rF2VehicleScoring GetPlayerScoring(ref rF2Scoring scoring)
        {
            var playerVehScoring = new rF2VehicleScoring();

            for (int i = 0; i < scoring.mScoringInfo.mNumVehicles; ++i)
            {
                var vehicle = scoring.mVehicles[i];
                switch ((rFactor2Constants.rF2Control)vehicle.mControl)
                {
                case rFactor2Constants.rF2Control.AI:
                case rFactor2Constants.rF2Control.Player:
                case rFactor2Constants.rF2Control.Remote:
                    if (vehicle.mIsPlayer == 1)
                    {
                        playerVehScoring = vehicle;
                    }

                    break;

                default:
                    continue;
                }

                if (playerVehScoring.mIsPlayer == 1)
                {
                    break;
                }
            }

            return(playerVehScoring);
        }
Esempio n. 2
0
        internal void AddDriversData(SimulatorDataSet data, Rf2FullData rfData)
        {
            if (rfData.scoring.mScoringInfo.mNumVehicles < 1)
            {
                return;
            }

            data.DriversInfo = new DriverInfo[rfData.scoring.mScoringInfo.mNumVehicles];
            DriverInfo playersInfo = null;

            for (int i = 0; i < rfData.scoring.mScoringInfo.mNumVehicles; i++)
            {
                rF2VehicleScoring rF2VehicleScoring = rfData.scoring.mVehicles[i];
                DriverInfo        driverInfo        = CreateDriverInfo(rfData, rF2VehicleScoring);

                if (driverInfo.IsPlayer)
                {
                    playersInfo = driverInfo;
                    driverInfo.CurrentLapValid = true;
                    _lastPlayerId = rF2VehicleScoring.mID;
                }
                else
                {
                    driverInfo.CurrentLapValid = true;
                }

                data.DriversInfo[i] = driverInfo;
                if (driverInfo.Position == 1)
                {
                    data.SessionInfo.LeaderCurrentLap = driverInfo.CompletedLaps + 1;
                    data.LeaderInfo = driverInfo;
                }

                AddLappingInformation(data, rfData, driverInfo);
                FillTimingInfo(driverInfo, rF2VehicleScoring, rfData);

                if (driverInfo.FinishStatus == DriverFinishStatus.Finished && !driverInfo.IsPlayer && driverInfo.Position > _lastPlayer.Position)
                {
                    driverInfo.CompletedLaps--;
                    driverInfo.FinishStatus = DriverFinishStatus.None;
                }
            }
            CheckValidityByPlayer(playersInfo);
            _lastPlayer = playersInfo;
            if (playersInfo != null)
            {
                data.PlayerInfo = playersInfo;
            }

            FillGapInformation(data.DriversInfo);
        }
Esempio n. 3
0
        private long GetRemainingTime(ref rF2VehicleScoring playerScoring)
        {
            if (playerScoring.mTotalLaps < 1)
            {
                return(0);
            }

            if (scoring.mScoringInfo.mEndET > 0.0)
            {
                return((long)((scoring.mScoringInfo.mEndET - (Normalize(playerScoring.mLastLapTime) * playerScoring.mTotalLaps)) * 1000));
            }
            else
            {
                return((long)(GetRemainingLaps(ref playerScoring) * Normalize(playerScoring.mLastLapTime) * 1000));
            }
        }
Esempio n. 4
0
        public void ReadStandings()
        {
            Console.WriteLine("[Position Data]");

            Console.Write("Car.Count="); Console.WriteLine(scoring.mScoringInfo.mNumVehicles);

            for (int i = 1; i <= scoring.mScoringInfo.mNumVehicles; ++i)
            {
                rF2VehicleScoring vehicle = scoring.mVehicles[i - 1];

                Console.Write("Car."); Console.Write(i); Console.Write(".Nr="); Console.WriteLine(vehicle.mID);
                Console.Write("Car."); Console.Write(i); Console.Write(".Position="); Console.WriteLine(vehicle.mPlace);

                Console.Write("Car."); Console.Write(i); Console.Write(".Lap="); Console.WriteLine(vehicle.mTotalLaps);
                Console.Write("Car."); Console.Write(i); Console.Write(".Lap.Running="); Console.WriteLine(vehicle.mLapDist / scoring.mScoringInfo.mLapDist);
                Console.Write("Car."); Console.Write(i); Console.Write(".Lap.Valid="); Console.WriteLine(vehicle.mCountLapFlag == 2 ? "true" : "false");

                int lapTime     = (int)Math.Round(Normalize(vehicle.mLastLapTime) * 1000);
                int sector1Time = (int)Math.Round(Normalize(vehicle.mLastSector1) * 1000);
                int sector2Time = (int)Math.Round(Normalize(vehicle.mLastSector2) * 1000);
                int sector3Time = lapTime - sector1Time - sector2Time;

                Console.Write("Car."); Console.Write(i); Console.Write(".Time="); Console.WriteLine(lapTime);
                Console.Write("Car."); Console.Write(i); Console.Write(".Time.Sectors="); Console.WriteLine(sector1Time + "," + sector2Time + "," + sector3Time);


                string carModel = GetCarName(GetStringFromBytes(vehicle.mVehicleClass), GetStringFromBytes(vehicle.mVehicleName));
                string carNr    = GetCarNr(vehicle.mID, GetStringFromBytes(vehicle.mVehicleClass), GetStringFromBytes(vehicle.mVehicleName));

                Console.Write("Car."); Console.Write(i); Console.Write(".Nr="); Console.WriteLine(carNr);
                Console.Write("Car."); Console.Write(i); Console.Write(".Car="); Console.WriteLine(carModel);

                Console.Write("Car."); Console.Write(i); Console.Write(".Driver.Forname="); Console.WriteLine(GetForname(vehicle.mDriverName));
                Console.Write("Car."); Console.Write(i); Console.Write(".Driver.Surname="); Console.WriteLine(GetSurname(vehicle.mDriverName));
                Console.Write("Car."); Console.Write(i); Console.Write(".Driver.Nickname="); Console.WriteLine(GetNickname(vehicle.mDriverName));

                Console.Write("Car."); Console.Write(i); Console.Write(".InPitLane="); Console.WriteLine(vehicle.mInPits != 0 ? "true" : "false");
                Console.Write("Car."); Console.Write(i); Console.Write(".InPit="); Console.WriteLine(vehicle.mPitState != 3 ? "true" : "false");

                if (vehicle.mIsPlayer != 0)
                {
                    Console.Write("Driver.Car=");
                    Console.WriteLine(i);
                }
            }
        }
Esempio n. 5
0
        bool writeCoordinates(ref rF2VehicleScoring playerScoring)
        {
            double lVelocityX = playerScoring.mLocalVel.x;
            double lVelocityY = playerScoring.mLocalVel.y;
            double lVelocityZ = playerScoring.mLocalVel.z;

            int carID = 0;

            for (int i = 0; i < scoring.mScoringInfo.mNumVehicles; ++i)
            {
                if (scoring.mVehicles[i].mIsPlayer != 0)
                {
                    carID = i;

                    break;
                }
            }

            var ori = playerScoring.mOri;

            double velocityX = ori[RowX].x * lVelocityX + ori[RowX].y * lVelocityY + ori[RowX].z * lVelocityZ;
            double velocityY = ori[RowY].x * lVelocityX + ori[RowY].y * lVelocityY + ori[RowY].z * lVelocityZ;
            double velocityZ = (ori[RowZ].x * lVelocityX + ori[RowZ].y * lVelocityY + ori[RowZ].z * lVelocityZ) * -1;

            if ((velocityX != 0) || (velocityY != 0) || (velocityZ != 0))
            {
                double coordinateX = playerScoring.mPos.x;
                double coordinateY = (-playerScoring.mPos.z);

                Console.WriteLine(coordinateX + "," + coordinateY);

                if (coordCount == 0)
                {
                    initialX = coordinateX;
                    initialY = coordinateY;
                }
                else if (coordCount > 100 && Math.Abs(coordinateX - initialX) < 10.0 && Math.Abs(coordinateY - initialY) < 10.0)
                {
                    return(false);
                }

                coordCount += 1;
            }

            return(true);
        }
Esempio n. 6
0
        void checkCoordinates(ref rF2VehicleScoring playerScoring)
        {
            if (DateTimeOffset.Now.ToUnixTimeMilliseconds() > (lastUpdate + 2000))
            {
                double lVelocityX = playerScoring.mLocalVel.x;
                double lVelocityY = playerScoring.mLocalVel.y;
                double lVelocityZ = playerScoring.mLocalVel.z;

                int carID = 0;

                for (int i = 0; i < scoring.mScoringInfo.mNumVehicles; ++i)
                {
                    if (scoring.mVehicles[i].mIsPlayer != 0)
                    {
                        carID = i;

                        break;
                    }
                }

                var ori = playerScoring.mOri;

                double velocityX = ori[RowX].x * lVelocityX + ori[RowX].y * lVelocityY + ori[RowX].z * lVelocityZ;
                double velocityY = ori[RowY].x * lVelocityX + ori[RowY].y * lVelocityY + ori[RowY].z * lVelocityZ;
                double velocityZ = (ori[RowZ].x * lVelocityX + ori[RowZ].y * lVelocityY + ori[RowZ].z * lVelocityZ) * -1;

                if ((velocityX != 0) || (velocityY != 0) || (velocityZ != 0))
                {
                    double coordinateX = playerScoring.mPos.x;
                    double coordinateY = (-playerScoring.mPos.z);

                    for (int i = 0; i < numCoordinates; i += 1)
                    {
                        if (Math.Abs(xCoordinates[i] - coordinateX) < 20 && Math.Abs(yCoordinates[i] - coordinateY) < 20)
                        {
                            SendAutomationMessage("positionTrigger:" + (i + 1) + ";" + xCoordinates[i] + ";" + yCoordinates[i]);

                            lastUpdate = DateTimeOffset.Now.ToUnixTimeMilliseconds();

                            break;
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        private DriverInfo CreateDriverInfo(Rf2FullData rfData, rF2VehicleScoring rfVehicleInfo)
        {
            DriverInfo driverInfo = new DriverInfo
            {
                DriverName    = StringExtensions.FromArray(rfVehicleInfo.mDriverName),
                CompletedLaps = rfVehicleInfo.mTotalLaps,
                CarName       = StringExtensions.FromArray(rfVehicleInfo.mVehicleName),
                InPits        = rfVehicleInfo.mInPits == 1
            };

            driverInfo.IsPlayer = rfVehicleInfo.mIsPlayer == 1;
            driverInfo.Position = rfVehicleInfo.mPlace;
            driverInfo.Speed    = Velocity.FromMs(Math.Sqrt((rfVehicleInfo.mLocalVel.x * rfVehicleInfo.mLocalVel.x)
                                                            + (rfVehicleInfo.mLocalVel.y * rfVehicleInfo.mLocalVel.y)
                                                            + (rfVehicleInfo.mLocalVel.z * rfVehicleInfo.mLocalVel.z)));
            driverInfo.LapDistance   = rfVehicleInfo.mLapDist;
            driverInfo.TotalDistance = rfVehicleInfo.mTotalLaps * rfData.scoring.mScoringInfo.mLapDist + rfVehicleInfo.mLapDist;
            driverInfo.FinishStatus  = FromRFStatus(rfVehicleInfo.mFinishStatus);
            driverInfo.WorldPosition = new Point3D(Distance.FromMeters(rfVehicleInfo.mPos.x), Distance.FromMeters(rfVehicleInfo.mPos.y), Distance.FromMeters(rfVehicleInfo.mPos.z));
            ComputeDistanceToPlayer(_lastPlayer, driverInfo, rfData);
            return(driverInfo);
        }
Esempio n. 8
0
        internal void FillTimingInfo(DriverInfo driverInfo, rF2VehicleScoring rfVehicleInfo, Rf2FullData Rf2FullData)
        {
            driverInfo.Timing.LastSector1Time = CreateTimeSpan(rfVehicleInfo.mCurSector1);
            driverInfo.Timing.LastSector2Time = CreateTimeSpan(rfVehicleInfo.mCurSector2 - rfVehicleInfo.mCurSector1);
            driverInfo.Timing.LastSector3Time = CreateTimeSpan(rfVehicleInfo.mLastLapTime - rfVehicleInfo.mLastSector2);
            driverInfo.Timing.LastLapTime     = CreateTimeSpan(rfVehicleInfo.mLastLapTime);
            driverInfo.Timing.CurrentSector   = rfVehicleInfo.mSector == 0 ? 3 : rfVehicleInfo.mSector;

            switch (driverInfo.Timing.CurrentSector)
            {
            case 1:
                driverInfo.Timing.CurrentLapTime = CreateTimeSpan(rfVehicleInfo.mCurSector1);
                break;

            case 2:
                driverInfo.Timing.CurrentLapTime = CreateTimeSpan(rfVehicleInfo.mCurSector2);
                break;

            case 0:
                driverInfo.Timing.CurrentLapTime = CreateTimeSpan(rfVehicleInfo.mLastLapTime);
                break;
            }
        }
Esempio n. 9
0
        private long GetRemainingLaps(ref rF2VehicleScoring playerScoring)
        {
            if (playerScoring.mTotalLaps < 1)
            {
                return(0);
            }

            if (scoring.mScoringInfo.mEndET <= 0.0)
            {
                return(scoring.mScoringInfo.mMaxLaps - playerScoring.mTotalLaps);
            }
            else
            {
                if (playerScoring.mLastLapTime > 0)
                {
                    return((long)Math.Round(GetRemainingTime(ref playerScoring) / (Normalize(playerScoring.mLastLapTime) * 1000)) + 1);
                }
                else
                {
                    return(0);
                }
            }
        }
Esempio n. 10
0
        bool checkPitWindow(ref rF2VehicleScoring playerScoring)
        {
            // No support by rFactor 2

            return(false);
        }
Esempio n. 11
0
        public async Task Run(CancellationToken token)
        {
            var hubConnection = await _connectionManager.Connect();

            var telemetry = new rF2Telemetry();
            var scoring   = new rF2Scoring();

            var playerScoring   = new rF2VehicleScoring();
            var playerTelemetry = new rF2VehicleTelemetry();

            while (true)
            {
                await Task.Delay(200);

                try
                {
                    _scoringBuffer.GetMappedDataPartial(ref scoring);
                    _telemetryBuffer.GetMappedDataPartial(ref telemetry);
                }
                catch (Exception)
                {
                    await _connectionManager.Disconnect(hubConnection);

                    return;
                }

                if (telemetry.mNumVehicles != 0 && scoring.mScoringInfo.mNumVehicles != 0 && (rFactor2Constants.rF2GamePhase)scoring.mScoringInfo.mGamePhase == rFactor2Constants.rF2GamePhase.GreenFlag)
                {
                    var mappedIds = MapmIdToPositionInArray(ref telemetry);

                    foreach (var scoringVehicle in scoring.mVehicles)
                    {
                        switch ((rFactor2Constants.rF2Control)scoringVehicle.mControl)
                        {
                        case rFactor2Constants.rF2Control.AI:
                        case rFactor2Constants.rF2Control.Player:
                        case rFactor2Constants.rF2Control.Remote:
                            if (scoringVehicle.mIsPlayer == 1)
                            {
                                playerScoring = scoringVehicle;
                            }
                            break;

                        default:
                            continue;
                        }
                        if (playerScoring.mIsPlayer == 1)
                        {
                            break;
                        }
                    }

                    var playerTelemetryId = -1;
                    if (mappedIds.TryGetValue(playerScoring.mID, out playerTelemetryId))
                    {
                        playerTelemetry = telemetry.mVehicles[playerTelemetryId];
                    }
                    else
                    {
                        continue;
                    }

                    var telemetryToSend = new Test
                    {
                        Telemetry = new Telemetry(ref playerTelemetry)
                    };
                    await hubConnection.SendAsync("Send", telemetryToSend);
                }
            }
        }
Esempio n. 12
0
        ApiResponse getData()
        {
            ApiResponse res = new ApiResponse();

            Console.Clear();
            Program.extendedBuffer.GetMappedData(ref Program.extended);
            Program.scoringBuffer.GetMappedData(ref Program.scoring);
            Program.telemetryBuffer.GetMappedData(ref Program.telemetry);
            Program.rulesBuffer.GetMappedData(ref Program.rules);

            var scoring   = Program.scoring;
            var telemetry = Program.telemetry;
            var rules     = Program.rules;

            res.Session = new LiveTiming.Session
            {
                MaxLaps          = scoring.mScoringInfo.mMaxLaps,
                MaxTime          = scoring.mScoringInfo.mEndET - Constants.MAXTIMECOUNTDOWN, // I assume that the seconds are just becaus of the red light countdown until the lights go on
                CurrentTime      = Math.Floor(scoring.mScoringInfo.mCurrentET),
                CurrentLaps      = 0,
                YellowFlags      = new bool[3],
                IsRace           = scoring.mScoringInfo.mSession >= 10 && scoring.mScoringInfo.mSession <= 13,
                IsSessionStarted = scoring.mScoringInfo.mGamePhase == 5,
                IsVCY            = scoring.mScoringInfo.mGamePhase == 6
            };
            for (int i = 0; i < 3; i++)
            {
                int raw = Convert.ToInt32(scoring.mScoringInfo.mSectorFlag[i]);
                res.Session.YellowFlags[i] = raw != 11;
            }
            foreach (sbyte flag in scoring.mScoringInfo.mSectorFlag)
            {
                rF2YellowFlagState state = (rF2YellowFlagState)flag;

                Console.WriteLine("Flag {0}", (rF2YellowFlagState)flag);
            }

            List <Entry> entries = new List <Entry>();

            for (int i = 0; i < scoring.mScoringInfo.mNumVehicles; ++i)
            {
                rF2VehicleScoring   vehicle           = scoring.mVehicles[i];
                rF2VehicleTelemetry vehicleTelementry = telemetry.mVehicles[i];
                dynamic             foo = vehicle.mTrackEdge; //??
                Console.WriteLine(foo);
                rF2TrackRulesParticipant participant = rules.mParticipants[i];


                String[] nameParts = this.GetStringFromBytes(vehicle.mDriverName).Split(' ');

                String   vehicleName      = this.GetStringFromBytes(vehicleTelementry.mVehicleName);
                String[] vehicleNameParts = vehicleName.Split('#');
                int      number           = 999;
                try
                {
                    number = Convert.ToInt32(vehicleNameParts[1]);
                }
                catch (Exception)
                {
                }
                String teamName = this.GetStringFromBytes(vehicle.mPitGroup);
                String format   = "{0}";

                foreach (JObject driverEntry in this.entries.Children())
                {
                    if (driverEntry["driverNumber"].ToString() == number.ToString())
                    {
                        teamName = driverEntry["teamName"].ToString();
                        format   = driverEntry["driverNumberFormat"].ToString();
                    }
                }

                TimeSpan diff          = TimeSpan.FromSeconds(vehicle.mTimeBehindNext);
                String   diffString    = diff.ToString();
                String   bestLapString = TimeSpan.FromSeconds(vehicle.mBestLapTime).ToString(@"mm\:ss\:fff");
                String   lastLapString = TimeSpan.FromSeconds(vehicle.mLastLapTime).ToString(@"mm\:ss\:fff");

                if (diff.TotalHours < 1)
                {
                    if (diff.TotalMinutes < 1)
                    {
                        diffString = diff.ToString(@"ss\:fff");
                    }
                    else
                    {
                        diffString = diff.ToString(@"mm\:ss\:fff");
                    }
                }

                diffString = String.Format("+ {0}", diffString);
                int positionDifference = Program.lastResponse != null ? vehicle.mPlace - Program.lastResponse.Drivers.First(d => d.SlotID == vehicle.mID).Position : 0;
                Console.Write(positionDifference);
                Entry entry = new Entry
                {
                    SlotID             = vehicle.mID,
                    TeamName           = teamName,
                    VehicleName        = vehicleNameParts.Length > 1 ? vehicleNameParts[0] : vehicleName,
                    NumberFormat       = format,
                    Number             = number,
                    FormattedNumber    = String.Format(format, number),
                    FirstName          = nameParts.Length > 1 ? nameParts[0] : "",
                    LastName           = nameParts.Length > 1 ? nameParts[1] : nameParts[0],
                    Position           = vehicle.mPlace,
                    EntryClass         = this.GetStringFromBytes(vehicle.mVehicleClass),
                    FrontTires         = this.GetStringFromBytes(vehicleTelementry.mFrontTireCompoundName),
                    RearTires          = this.GetStringFromBytes(vehicleTelementry.mRearTireCompoundName),
                    PitState           = Constants.PitStates[vehicle.mPitState],
                    TimeBehind         = vehicle.mTimeBehindNext,
                    TimeBehindString   = diffString,
                    LapsBehind         = vehicle.mLapsBehindNext,
                    Stops              = vehicle.mNumPitstops,
                    Status             = Constants.Status[vehicle.mFinishStatus],
                    HasHeatingProblem  = vehicleTelementry.mOverheating != 0,
                    HasLostParts       = vehicleTelementry.mDetached != 0,
                    CurrentLapString   = TimeSpan.FromSeconds(res.Session.CurrentTime - vehicleTelementry.mLapStartET).ToString(@"mm\:ss\:fff"),
                    PositionDifference = vehicle.mQualification - vehicle.mPlace,
                    LastSectorTimes    = new double[]
                    {
                        vehicle.mLastSector1,
                        vehicle.mLastSector2,
                        vehicle.mLastLapTime - vehicle.mLastSector2
                    },
                    BestSectorTimes = new double[]
                    {
                        vehicle.mBestSector1,
                        vehicle.mBestSector2,
                        vehicle.mBestLapTime - vehicle.mBestSector2
                    },
                    BestLap       = vehicle.mBestLapTime,
                    LastLap       = vehicle.mLastLapTime,
                    LastLapString = lastLapString,
                    BestLapString = bestLapString,
                    Laps          = vehicle.mTotalLaps,
                    CurrentSessionPositionDifference = positionDifference
                };
                if (entry.Position == 1)
                {
                    res.Session.CurrentLaps = entry.Laps;
                }
                entries.Add(entry);
            }
            // Set fastest lap
            Entry fastestDriver = entries.First(d => d.BestLap == entries.Min(e => e.BestLap));

            entries.ForEach(e =>
            {
                e.BestLapDelta = e.BestLap - fastestDriver.BestLap;
                TimeSpan diff  = TimeSpan.FromSeconds(e.BestLapDelta);
                if (diff.TotalMinutes < 1.0)
                {
                    e.BestLapDeltaString = "+ " + diff.ToString(@"ss\:fff");
                }
                else
                {
                    e.BestLapDeltaString = "+ " + diff.ToString(@"mm\:ss\:fff");
                }
            });

            if (res.Session.MaxLaps == int.MaxValue)
            {
                TimeSpan current = TimeSpan.FromSeconds(Math.Floor(scoring.mScoringInfo.mCurrentET));
                TimeSpan max     = TimeSpan.FromSeconds(Math.Floor(scoring.mScoringInfo.mEndET - Constants.MAXTIMECOUNTDOWN));
                res.Session.SessionLeftString = String.Format("{0}/ {1}", current, max);
            }
            else
            {
                res.Session.CurrentLaps       = entries.Max(e => e.Laps);
                res.Session.SessionLeftString = String.Format("{0}/ {1}", res.Session.CurrentLaps, res.Session.MaxLaps);
            }
            res.Drivers = entries.ToArray();
            res.RaceOverlayControlSet   = this.parsedJSON["controlSet"].ToString();
            res.CommandId               = Convert.ToInt32(this.parsedJSON["commandId"]);
            res.Session.IsSessionPaused = res.RaceOverlayControlSet.IndexOf("pause") != -1;
            // Translate the slot id (wich means the position) to the proper rfactor 2 id:
            bool driverFound = false;

            if (res.RaceOverlayControlSet.IndexOf("currentDriver") != -1)
            {
                String driverName = JObject.Parse(res.RaceOverlayControlSet)["currentDriver"].ToString();
                foreach (Entry driver in res.Drivers)
                {
                    if (driver.FirstName + " " + driver.LastName == driverName)
                    {
                        res.SlotId  = driver.SlotID;
                        driverFound = true;
                        break;
                    }
                }
            }
            else
            {
                foreach (Entry driver in res.Drivers)
                {
                    if (driver.Position == Convert.ToInt32(this.parsedJSON["slotId"].ToString()))
                    {
                        res.SlotId  = driver.SlotID;
                        driverFound = true;
                        break;
                    }
                }
            }

            res.SlotId   = Program.lastResponse != null && res.SlotId == 0 ? Program.lastResponse.SlotId : res.SlotId;
            res.CameraId = Convert.ToInt32(this.parsedJSON["cameraId"].ToString());
            if (!driverFound && Program.lastResponse != null)
            {
                res.SlotId    = Program.lastResponse.SlotId;
                res.CameraId  = Program.lastResponse.CameraId;
                res.CommandId = Program.lastResponse.CameraId;
            }
            // Write control file for rfactor plugin
            // TODO: ADD A PROPER TIMEOUT
            try
            {
                if (Program.lastResponse == null || Program.lastResponse.CameraId != res.CameraId || Program.lastResponse.SlotId != res.SlotId)
                {
                    string[] lines = { res.SlotId.ToString(), res.CameraId.ToString() };
                    String   path  = Path.Combine(Path.GetTempPath(), "cameraslots.txt");
                    System.IO.File.WriteAllLines(path, lines);
                }
            }
            catch
            {
            }
            Program.lastResponse = res;
            return(res);
        }
Esempio n. 13
0
        public void Run(bool mapTrack, bool positionTrigger)
        {
            bool running   = false;
            int  countdown = 4000;

            while (true)
            {
                if (!connected)
                {
                    Connect();
                }

                if (connected)
                {
                    try
                    {
                        extendedBuffer.GetMappedData(ref extended);
                        scoringBuffer.GetMappedData(ref scoring);
                    }
                    catch (Exception)
                    {
                        this.Disconnect();
                    }

                    if (connected)
                    {
                        bool wait = true;

                        rF2VehicleScoring playerScoring = GetPlayerScoring(ref scoring);

                        if (mapTrack)
                        {
                            if (!writeCoordinates(ref playerScoring))
                            {
                                break;
                            }
                        }
                        else if (positionTrigger)
                        {
                            checkCoordinates(ref playerScoring);
                        }
                        else
                        {
                            bool startGo = (scoring.mScoringInfo.mGamePhase == (byte)rF2GamePhase.GreenFlag);

                            if (!running)
                            {
                                if (startGo || (countdown-- <= 0))
                                {
                                    running = true;
                                }
                            }

                            if (running)
                            {
                                if (extended.mSessionStarted != 0 && scoring.mScoringInfo.mGamePhase < (byte)SessionStopped &&
                                    playerScoring.mPitState < (byte)Entering)
                                {
                                    if (!startGo || !greenFlag())
                                    {
                                        if (!checkFlagState(ref playerScoring) && !checkPositions(ref playerScoring))
                                        {
                                            wait = !checkPitWindow(ref playerScoring);
                                        }
                                        else
                                        {
                                            wait = false;
                                        }
                                    }
                                }
                                else
                                {
                                    longitudinalRearDistance = 5;

                                    lastSituation     = CLEAR;
                                    carBehind         = false;
                                    carBehindReported = false;

                                    lastFlagState = 0;
                                }
                            }
                        }

                        if (positionTrigger)
                        {
                            Thread.Sleep(10);
                        }
                        else if (wait)
                        {
                            Thread.Sleep(50);
                        }
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
        }
Esempio n. 14
0
        public void ReadData()
        {
            rF2VehicleScoring   playerScoring   = GetPlayerScoring(ref scoring);
            rF2VehicleTelemetry playerTelemetry = GetPlayerTelemetry(playerScoring.mID, ref telemetry);

            string session = "";

            Console.WriteLine("[Session Data]");
            Console.Write("Active="); Console.WriteLine((connected && (extended.mSessionStarted != 0)) ? "true" : "false");
            if (connected)
            {
                if (playerTelemetry.mWheels == null)
                {
                    Console.WriteLine("Paused=true");
                }
                else
                {
                    Console.Write("Paused=");
                    Console.WriteLine(scoring.mScoringInfo.mGamePhase <= (byte)GridWalk || scoring.mScoringInfo.mGamePhase == (byte)PausedOrHeartbeat ? "true" : "false");
                }

                if (scoring.mScoringInfo.mSession >= 10 && scoring.mScoringInfo.mSession <= 13)
                {
                    session = "Race";
                }
                else if (scoring.mScoringInfo.mSession >= 0 && scoring.mScoringInfo.mSession <= 4)
                {
                    session = "Practice";
                }
                else if (scoring.mScoringInfo.mSession >= 5 && scoring.mScoringInfo.mSession <= 8)
                {
                    session = "Qualification";
                }
                else
                {
                    session = "Other";
                }

                Console.Write("Session="); Console.WriteLine(session);

                string vehicleClass = GetStringFromBytes(playerScoring.mVehicleClass);
                string vehicleName  = GetStringFromBytes(playerScoring.mVehicleName);

                Console.Write("Car="); Console.WriteLine(GetCarName(vehicleClass, vehicleName));
                Console.Write("CarName="); Console.WriteLine(vehicleName);
                Console.Write("CarClass="); Console.WriteLine(vehicleClass);
                Console.Write("Track="); Console.WriteLine(GetStringFromBytes(playerTelemetry.mTrackName));
                Console.Write("SessionFormat="); Console.WriteLine((scoring.mScoringInfo.mEndET < 0.0) ? "Lap" : "Time");
                Console.Write("FuelAmount="); Console.WriteLine(Math.Round(playerTelemetry.mFuelCapacity));

                if (session == "Practice")
                {
                    Console.WriteLine("SessionTimeRemaining=3600000");

                    Console.WriteLine("SessionLapsRemaining=30");
                }
                else
                {
                    long time = GetRemainingTime(ref playerScoring);

                    Console.Write("SessionTimeRemaining="); Console.WriteLine(time);

                    Console.Write("SessionLapsRemaining="); Console.WriteLine(GetRemainingLaps(ref playerScoring));
                }
            }

            Console.WriteLine("[Stint Data]");
            if (connected)
            {
                Console.Write("DriverForname="); Console.WriteLine(GetForname(scoring.mScoringInfo.mPlayerName));
                Console.Write("DriverSurname="); Console.WriteLine(GetSurname(scoring.mScoringInfo.mPlayerName));
                Console.Write("DriverNickname="); Console.WriteLine(GetNickname(scoring.mScoringInfo.mPlayerName));

                Console.Write("LapValid="); Console.WriteLine((playerScoring.mCountLapFlag > 0) ? "true" : "false");

                Console.Write("LapLastTime="); Console.WriteLine(Math.Round(Normalize(playerScoring.mLastLapTime) * 1000));
                Console.Write("LapBestTime="); Console.WriteLine(Math.Round(Normalize(playerScoring.mBestLapTime) * 1000));

                Console.Write("Sector="); Console.WriteLine(playerScoring.mSector == 0 ? 3 : playerScoring.mSector);
                Console.Write("Laps="); Console.WriteLine(playerScoring.mTotalLaps);

                if (session == "Practice")
                {
                    Console.WriteLine("StintTimeRemaining=3600000");
                    Console.WriteLine("DriverTimeRemaining=3600000");
                }
                else
                {
                    long time = GetRemainingTime(ref playerScoring);

                    Console.Write("StintTimeRemaining="); Console.WriteLine(time);
                    Console.Write("DriverTimeRemaining="); Console.WriteLine(time);
                }
                Console.Write("InPit="); Console.WriteLine(playerScoring.mPitState == (byte)Stopped ? "true" : "false");
            }

            Console.WriteLine("[Car Data]");
            if (connected && (playerTelemetry.mWheels != null))
            {
                Console.WriteLine("MAP=n/a");
                Console.Write("TC="); Console.WriteLine(extended.mPhysics.mTractionControl);
                Console.Write("ABS="); Console.WriteLine(extended.mPhysics.mAntiLockBrakes);

                Console.Write("FuelRemaining="); Console.WriteLine(playerTelemetry.mFuel);
                Console.Write("TyreTemperature=");
                Console.WriteLine(GetCelcius(playerTelemetry.mWheels[0].mTireCarcassTemperature) + "," +
                                  GetCelcius(playerTelemetry.mWheels[1].mTireCarcassTemperature) + "," +
                                  GetCelcius(playerTelemetry.mWheels[2].mTireCarcassTemperature) + "," +
                                  GetCelcius(playerTelemetry.mWheels[3].mTireCarcassTemperature));
                Console.Write("TyrePressure=");
                Console.WriteLine(GetPsi(playerTelemetry.mWheels[0].mPressure) + "," +
                                  GetPsi(playerTelemetry.mWheels[1].mPressure) + "," +
                                  GetPsi(playerTelemetry.mWheels[2].mPressure) + "," +
                                  GetPsi(playerTelemetry.mWheels[3].mPressure));
                Console.Write("TyreWear=");
                if (extended.mPhysics.mTireMult > 0)
                {
                    Console.WriteLine((100 - Math.Round(playerTelemetry.mWheels[0].mWear * 100)) + "," +
                                      (100 - Math.Round(playerTelemetry.mWheels[1].mWear * 100)) + "," +
                                      (100 - Math.Round(playerTelemetry.mWheels[2].mWear * 100)) + "," +
                                      (100 - Math.Round(playerTelemetry.mWheels[3].mWear * 100)));
                }
                else
                {
                    Console.WriteLine("0,0,0,0");
                }
                Console.Write("BrakeTemperature=");
                Console.WriteLine(GetCelcius(playerTelemetry.mWheels[0].mBrakeTemp) + "," +
                                  GetCelcius(playerTelemetry.mWheels[1].mBrakeTemp) + "," +
                                  GetCelcius(playerTelemetry.mWheels[2].mBrakeTemp) + "," +
                                  GetCelcius(playerTelemetry.mWheels[3].mBrakeTemp));

                string compound = GetStringFromBytes(playerTelemetry.mFrontTireCompoundName);

                Console.Write("TyreCompoundRaw="); Console.WriteLine(compound);

                Console.Write("BodyworkDamage=0, 0, 0, 0, "); Console.WriteLine(extended.mTrackedDamages[playerTelemetry.mID].mAccumulatedImpactMagnitude / 1000);
                Console.WriteLine("SuspensionDamage=0, 0, 0, 0");
                Console.WriteLine("EngineDamage=0");
            }

            Console.WriteLine("[Track Data]");

            if (connected)
            {
                Console.WriteLine("Grip=Optimum");
                Console.Write("Temperature="); Console.WriteLine(scoring.mScoringInfo.mTrackTemp);

                for (int i = 0; i < scoring.mScoringInfo.mNumVehicles; ++i)
                {
                    var vehicle = scoring.mVehicles[i];

                    Console.WriteLine("Car." + (i + 1) + ".Position=" + vehicle.mPos.x + "," + (-vehicle.mPos.z));
                }
            }

            Console.WriteLine("[Weather Data]");

            if (connected)
            {
                Console.Write("Temperature="); Console.WriteLine(scoring.mScoringInfo.mAmbientTemp);

                string theWeather = GetWeather(scoring.mScoringInfo.mDarkCloud, scoring.mScoringInfo.mRaining);

                Console.Write("Weather="); Console.WriteLine(theWeather);
                Console.Write("Weather10Min="); Console.WriteLine(theWeather);
                Console.Write("Weather30Min="); Console.WriteLine(theWeather);
            }

            Console.WriteLine("[Test Data]");
            if (connected)
            {
                Console.Write("Category="); Console.Write(pitInfo.mPitMenu.mCategoryIndex);
                Console.Write(" -> "); Console.WriteLine(GetStringFromBytes(pitInfo.mPitMenu.mCategoryName));
                Console.Write("Choices="); Console.Write(pitInfo.mPitMenu.mChoiceIndex);
                Console.Write(" -> "); Console.WriteLine(GetStringFromBytes(pitInfo.mPitMenu.mChoiceString));
                Console.Write("NumChoices="); Console.WriteLine(pitInfo.mPitMenu.mNumChoices);
            }
        }
Esempio n. 15
0
        bool checkFlagState(ref rF2VehicleScoring playerScoring)
        {
            if ((waitYellowFlagState & YELLOW_SECTOR_1) != 0 || (waitYellowFlagState & YELLOW_SECTOR_2) != 0 || (waitYellowFlagState & YELLOW_SECTOR_3) != 0)
            {
                yellowCount += 1;

                if (yellowCount > 50)
                {
                    if (scoring.mScoringInfo.mSectorFlag[0] == 0)
                    {
                        waitYellowFlagState &= ~YELLOW_SECTOR_1;
                    }

                    if (scoring.mScoringInfo.mSectorFlag[1] == 0)
                    {
                        waitYellowFlagState &= ~YELLOW_SECTOR_2;
                    }

                    if (scoring.mScoringInfo.mSectorFlag[1] == 0)
                    {
                        waitYellowFlagState &= ~YELLOW_SECTOR_3;
                    }

                    yellowCount = 0;

                    if ((waitYellowFlagState & YELLOW_SECTOR_1) != 0)
                    {
                        SendSpotterMessage("yellowFlag:Sector;1");

                        waitYellowFlagState &= ~YELLOW_SECTOR_1;

                        return(true);
                    }

                    if ((waitYellowFlagState & YELLOW_SECTOR_2) != 0)
                    {
                        SendSpotterMessage("yellowFlag:Sector;2");

                        waitYellowFlagState &= ~YELLOW_SECTOR_2;

                        return(true);
                    }

                    if ((waitYellowFlagState & YELLOW_SECTOR_3) != 0)
                    {
                        SendSpotterMessage("yellowFlag:Sector;3");

                        waitYellowFlagState &= ~YELLOW_SECTOR_3;

                        return(true);
                    }
                }
            }
            else
            {
                yellowCount = 0;
            }

            if (playerScoring.mFlag == (byte)rF2PrimaryFlag.Blue)
            {
                if ((lastFlagState & BLUE) == 0)
                {
                    SendSpotterMessage("blueFlag");

                    lastFlagState |= BLUE;

                    return(true);
                }
                else if (blueCount > 1000)
                {
                    lastFlagState &= ~BLUE;

                    blueCount = 0;
                }
                else
                {
                    blueCount += 1;
                }
            }
            else
            {
                lastFlagState &= ~BLUE;

                blueCount = 0;
            }

            if (scoring.mScoringInfo.mGamePhase == (byte)rF2GamePhase.FullCourseYellow)
            {
                if ((lastFlagState & YELLOW_FULL) == 0)
                {
                    SendSpotterMessage("yellowFlag:Full");

                    lastFlagState |= YELLOW_FULL;

                    return(true);
                }
            }
            else if (scoring.mScoringInfo.mSectorFlag[0] == 1)
            {
                if ((lastFlagState & YELLOW_SECTOR_1) == 0)
                {
                    /*
                     * SendSpotterMessage("yellowFlag:Sector;1");
                     *
                     * lastFlagState |= YELLOW_SECTOR_1;
                     *
                     * return true;
                     */

                    lastFlagState       |= YELLOW_SECTOR_1;
                    waitYellowFlagState |= YELLOW_SECTOR_1;
                    yellowCount          = 0;
                }
            }
            else if (scoring.mScoringInfo.mSectorFlag[1] == 1)
            {
                if ((lastFlagState & YELLOW_SECTOR_2) == 0)
                {
                    /*
                     * SendSpotterMessage("yellowFlag:Sector;2");
                     *
                     * lastFlagState |= YELLOW_SECTOR_2;
                     *
                     * return true;
                     */

                    lastFlagState       |= YELLOW_SECTOR_2;
                    waitYellowFlagState |= YELLOW_SECTOR_2;
                    yellowCount          = 0;
                }
            }
            else if (scoring.mScoringInfo.mSectorFlag[2] == 1)
            {
                if ((lastFlagState & YELLOW_SECTOR_3) == 0)
                {
                    /*
                     * SendSpotterMessage("yellowFlag:Sector;3");
                     *
                     * lastFlagState |= YELLOW_SECTOR_3;
                     *
                     * return true;
                     */

                    lastFlagState       |= YELLOW_SECTOR_2;
                    waitYellowFlagState |= YELLOW_SECTOR_2;
                    yellowCount          = 0;
                }
            }
            else
            {
                if ((lastFlagState & YELLOW_SECTOR_1) != 0 || (lastFlagState & YELLOW_SECTOR_2) != 0 ||
                    (lastFlagState & YELLOW_SECTOR_3) != 0)
                {
                    if (waitYellowFlagState != lastFlagState)
                    {
                        SendSpotterMessage("yellowFlag:Clear");
                    }

                    lastFlagState       &= ~YELLOW_FULL;
                    waitYellowFlagState &= ~YELLOW_FULL;
                    yellowCount          = 0;

                    return(true);
                }
            }

            return(false);
        }
Esempio n. 16
0
        bool checkPositions(ref rF2VehicleScoring playerScoring)
        {
            if (!hasLastCoordinates)
            {
                lastCoordinates = new double[scoring.mScoringInfo.mNumVehicles, 3];
            }

            double lVelocityX = playerScoring.mLocalVel.x;
            double lVelocityY = playerScoring.mLocalVel.y;
            double lVelocityZ = playerScoring.mLocalVel.z;

            int carID = 0;

            for (int i = 0; i < scoring.mScoringInfo.mNumVehicles; ++i)
            {
                if (scoring.mVehicles[i].mIsPlayer != 0)
                {
                    carID = i;

                    break;
                }
            }

            var ori = playerScoring.mOri;

            double velocityX = ori[RowX].x * lVelocityX + ori[RowX].y * lVelocityY + ori[RowX].z * lVelocityZ;
            double velocityY = ori[RowY].x * lVelocityX + ori[RowY].y * lVelocityY + ori[RowY].z * lVelocityZ;
            double velocityZ = (ori[RowZ].x * lVelocityX + ori[RowZ].y * lVelocityY + ori[RowZ].z * lVelocityZ) * -1;

            if ((velocityX != 0) || (velocityY != 0) || (velocityZ != 0))
            {
                double angle = vectorAngle(velocityX, velocityZ);

                // Console.WriteLine(vectorAngle(lVelocityX, lVelocityY));
                // Console.WriteLine(angle);
                // Console.WriteLine();

                double coordinateX = playerScoring.mPos.x;
                double coordinateY = playerScoring.mPos.y;
                double coordinateZ = (-playerScoring.mPos.z);
                double speed       = 0.0;

                if (hasLastCoordinates)
                {
                    speed = vectorLength(lastCoordinates[carID, 0] - coordinateX, lastCoordinates[carID, 2] - coordinateZ);
                }

                int newSituation = CLEAR;

                carBehind      = false;
                carBehindLeft  = false;
                carBehindRight = false;

                for (int i = 0; i < scoring.mScoringInfo.mNumVehicles; ++i)
                {
                    var vehicle = scoring.mVehicles[i];

                    if ((vehicle.mIsPlayer == 0) && (vehicle.mInPits == 0))
                    {
                        // Console.Write(i); Console.Write(" "); Console.Write(vehicle.mPos.x); Console.Write(" ");
                        // Console.Write(vehicle.mPos.z); Console.Write(" "); Console.WriteLine(vehicle.mPos.y);

                        bool faster = false;

                        if (hasLastCoordinates)
                        {
                            faster = vectorLength(lastCoordinates[i, 0] - vehicle.mPos.x,
                                                  lastCoordinates[i, 2] - (-vehicle.mPos.z)) > speed * 1.01;
                        }

                        newSituation |= checkCarPosition(coordinateX, coordinateZ, coordinateY, angle, faster,
                                                         vehicle.mPos.x, (-vehicle.mPos.z), vehicle.mPos.y);

                        if ((newSituation == THREE) && carBehind)
                        {
                            break;
                        }
                    }
                }

                for (int i = 0; i < scoring.mScoringInfo.mNumVehicles; ++i)
                {
                    var position = scoring.mVehicles[i].mPos;

                    lastCoordinates[i, 0] = position.x;
                    lastCoordinates[i, 1] = position.y;
                    lastCoordinates[i, 2] = (-position.z);
                }

                hasLastCoordinates = true;

                if (newSituation != CLEAR)
                {
                    carBehind         = false;
                    carBehindLeft     = false;
                    carBehindRight    = false;
                    carBehindReported = false;
                }

                if (carBehindCount++ > 200)
                {
                    carBehindCount = 0;
                }

                string alert = computeAlert(newSituation);

                if (alert != noAlert)
                {
                    longitudinalRearDistance = 4;

                    SendSpotterMessage("proximityAlert:" + alert);

                    return(true);
                }
                else
                {
                    longitudinalRearDistance = 5;

                    if (carBehind)
                    {
                        if (!carBehindReported)
                        {
                            if (carBehindLeft || carBehindRight || (carBehindCount < 20))
                            {
                                carBehindReported = true;

                                SendSpotterMessage(carBehindLeft ? "proximityAlert:BehindLeft" :
                                                   (carBehindRight ? "proximityAlert:BehindRight" : "proximityAlert:Behind"));

                                return(true);
                            }
                        }
                    }
                    else
                    {
                        carBehindReported = false;
                    }
                }
            }
            else
            {
                longitudinalRearDistance = 5;

                lastSituation     = CLEAR;
                carBehind         = false;
                carBehindReported = false;
            }

            return(false);
        }
Esempio n. 17
0
 private bool tryGetVehicleInfo(CrewChiefV4.rFactor2.RF2SharedMemoryReader.RF2StructWrapper shared, out rF2VehicleScoring vehicleScoring)
 {
     for (int i = 0; i < shared.scoring.mScoringInfo.mNumVehicles; ++i)
     {
         if (shared.scoring.mVehicles[i].mIsPlayer == 1)
         {
             vehicleScoring = shared.scoring.mVehicles[i];
             return(true);
         }
     }
     vehicleScoring = default(rF2VehicleScoring);
     return(false);
 }
Esempio n. 18
0
        internal void AddDriversData(SimulatorDataSet data, Rf2FullData rfData)
        {
            if (rfData.scoring.mScoringInfo.mNumVehicles < 1)
            {
                return;
            }

            data.DriversInfo = new DriverInfo[rfData.scoring.mScoringInfo.mNumVehicles];
            DriverInfo playersInfo = null;

            for (int i = 0; i < rfData.scoring.mScoringInfo.mNumVehicles; i++)
            {
                rF2VehicleScoring   rF2VehicleScoring   = rfData.scoring.mVehicles[i];
                rF2VehicleTelemetry rF2VehicleTelemetry = rfData.telemetry.mVehicles[i];
                DriverInfo          driverInfo          = CreateDriverInfo(rfData, rF2VehicleScoring);

                if (driverInfo.IsPlayer)
                {
                    playersInfo = driverInfo;
                    driverInfo.CurrentLapValid = true;
                    _lastPlayerId = rF2VehicleScoring.mID;
                }
                else
                {
                    driverInfo.CarInfo.WheelsInfo.FrontLeft.TyreType  = StringExtensions.FromArray(rF2VehicleTelemetry.mFrontTireCompoundName);
                    driverInfo.CarInfo.WheelsInfo.FrontRight.TyreType = driverInfo.CarInfo.WheelsInfo.FrontLeft.TyreType;
                    driverInfo.CarInfo.WheelsInfo.RearRight.TyreType  = StringExtensions.FromArray(rF2VehicleTelemetry.mRearTireCompoundName);
                    driverInfo.CarInfo.WheelsInfo.RearLeft.TyreType   = driverInfo.CarInfo.WheelsInfo.RearRight.TyreType;
                    driverInfo.CurrentLapValid = true;
                }

                data.DriversInfo[i] = driverInfo;
                if (driverInfo.Position == 1)
                {
                    data.SessionInfo.LeaderCurrentLap = driverInfo.CompletedLaps + 1;
                    data.LeaderInfo = driverInfo;
                }

                if (rF2VehicleScoring.mControl == 2)
                {
                    data.SessionInfo.IsMultiplayer = true;
                }

                AddLappingInformation(data, rfData, driverInfo);
                FillTimingInfo(driverInfo, rF2VehicleScoring, rfData);

                if (driverInfo.FinishStatus == DriverFinishStatus.Finished && !driverInfo.IsPlayer && driverInfo.Position > _lastPlayer.Position)
                {
                    driverInfo.CompletedLaps--;
                    driverInfo.FinishStatus = DriverFinishStatus.None;
                }
            }
            CheckValidityByPlayer(playersInfo);
            _lastPlayer = playersInfo;
            if (playersInfo != null)
            {
                data.PlayerInfo = playersInfo;
            }

            FillGapInformation(data.DriversInfo);
        }