Ejemplo n.º 1
0
        public void updateData()
        {
            // Check if the SDK is connected
            if (sdk.IsConnected())
            {
                // telemetry
                gear           = (Int32)sdk.GetData("Gear");
                rpm            = (Int32)(Single)sdk.GetData("RPM");
                speed          = (Int32)((Single)sdk.GetData("Speed") * 3.6);
                fuel           = (Int32)((Single)sdk.GetData("FuelLevel"));
                shiftindicator = (Single)sdk.GetData("ShiftIndicatorPct");


                Int32 enwarn = (Int32)sdk.GetData("EngineWarnings");
                if (((Int32)sdk.GetData("EngineWarnings") & 0x10) > 0)
                {
                    pitlimiter = true;
                }
                else
                {
                    pitlimiter = false;
                }

                lap     = (Int32)sdk.GetData("Lap");
                lapsrem = (Int32)sdk.GetData("SessionLapsRemain");

                Double sessionTime = new Double();

                Boolean curontrack = (Boolean)sdk.GetData("IsOnTrack");
                if (curontrack == false && ontrack == true)
                {
                    SaveBestLap();
                }

                ontrack = curontrack;

                if (ontrack)
                {
                    sessionTime = (Double)sdk.GetData("SessionTime");
                }
                else
                {
                    sessionTime = (Double)sdk.GetData("ReplaySessionTime");
                }

                if (carIdx >= 0) // skip things that require caridx if we don't have it
                {
                    Int32[] driverLaps = new Int32[64];
                    driverLaps = (Int32[])sdk.GetData("CarIdxLap");

                    Single[] driverTrkPos = new Single[64];
                    driverTrkPos = (Single[])sdk.GetData("CarIdxLapDistPct");

                    Int32 lapPointer = (Int32)Math.Floor((driverTrkPos[carIdx] % 1) * (trackLength / 10));

                    timedelta.Update(sessionTime, driverTrkPos);

                    if (driverTrkPos[carIdx] < 0.1 && lastTickTrackPos > 0.9)
                    {
                        Double distance       = (1 - lastTickTrackPos) + driverTrkPos[carIdx];
                        Double time           = sessionTime - lastTickTime;
                        Double tickCorrection = (1 - lastTickTrackPos) / distance;

                        // save lap time
                        if (lapTimeValid)
                        {
                            Double laptime = (sessionTime - (1 - tickCorrection) * time) - lapStartTime;
                            prevlap = new TimeSpan(0, 0, 0, (Int32)Math.Floor(laptime), (Int32)Math.Floor((laptime % 1) * 1000));

                            fuelcons[fuelconsPtr % fuelcons.Length] = (Single)sdk.GetData("FuelLevel");

                            // update fuel consumption after one full lap
                            if (fuelconsPtr > 0)
                            {
                                if (fuelconsPtr >= fuelcons.Length)
                                {
                                    Single[] consrate = new Single[fuelcons.Length - 1];
                                    Int32    j        = 0;
                                    for (int i = fuelconsPtr; i < fuelconsPtr + consrate.Length; i++)
                                    {
                                        consrate[j++] = fuelcons[(i + 1) % fuelcons.Length] - fuelcons[(i + 2) % fuelcons.Length];
                                    }
                                    fuelneed        = (Int32)(fuelcons[fuelconsPtr % fuelcons.Length] - (lapsrem * consrate.Average()));
                                    fuelconsumption = consrate.Average();
                                }
                                else if (fuelconsPtr > 0)
                                {
                                    fuelneed        = (Int32)(fuelcons[fuelconsPtr % fuelcons.Length] - (lapsrem * fuelcons[(fuelconsPtr - 1) % fuelcons.Length]));
                                    fuelconsumption = fuelcons[(fuelconsPtr - 1) % fuelcons.Length] - fuelcons[fuelconsPtr % fuelcons.Length];
                                }
                            }
                            fuelconsPtr++;
                        }

                        // start new lap
                        lapStartTime = sessionTime - (1 - tickCorrection) * time;
                        lapTimeValid = true;
                    }
                    else if (Math.Abs(driverTrkPos[carIdx] - lastTickTrackPos) > 0.1)
                    {
                        // invalidate lap time if jumping too much
                        lapTimeValid = false;
                    }

                    // reset fuel consumption when in pits
                    TrackLocation[] trackSurface = (iRSDKSharp.TrackLocation[])sdk.GetData("CarIdxTrackSurface");
                    if (trackSurface[carIdx] == TrackLocation.irsdk_InPitStall)
                    {
                        fuelcons    = new Single[fuelconslaps];
                        fuelconsPtr = 0;
                    }

                    lastTickTrackPos = driverTrkPos[carIdx]; // save for next tick
                    lastTickTime     = sessionTime;

                    Int32[] driverCarIdx = new Int32[64];

                    if (sessiontype == SessionTypes.race)
                    {
                        // in race calculate who is infront using trackpct and lap number
                        for (Int32 i = 0; i < 64; i++)
                        {
                            driverTrkPos[i] += (Single)driverLaps[i];
                            driverCarIdx[i]  = i;
                        }

                        Array.Sort(driverTrkPos, driverCarIdx);
                        Array.Reverse(driverCarIdx);
                        position = (Int32)(Array.IndexOf(driverCarIdx, carIdx) + 1);

                        delta = timedelta.GetDelta(carIdx, driverCarIdx[Math.Max(position - 2, 0)]);
                    }
                    else
                    {
                        if (sdk.Header.SessionInfoUpdate != lastSesInfoUpdate)
                        {
                            // parse position
                            String yaml = sdk.GetSessionInfo();

                            Int32 sessionmatch  = yaml.IndexOf(" - SessionNum: " + ((Int32)sdk.GetData("SessionNum")).ToString());
                            Int32 carmatch      = yaml.IndexOf("CarIdx: " + carIdx.ToString(), sessionmatch);
                            Int32 positionmatch = yaml.LastIndexOf("Position:", carmatch);
                            if (positionmatch < 0)
                            {
                                position = 0;
                            }
                            else
                            {
                                position = Int32.Parse(yaml.Substring(positionmatch + "Position:".Length, 2));
                            }
                        }

                        delta = timedelta.GetBestLapDelta(driverTrkPos[carIdx] % 1);
                    }
                }

                lastSesInfoUpdate = sdk.Header.SessionInfoUpdate;
            }
            else
            {
                init = false;
            }
        }
Ejemplo n.º 2
0
        public void updateData()
        {
            if (initialized)
            {
                Int64 pos = 0;

                state = shmem.ReadInt32(pos);
                pos  += sizeof(Int32);

                carid = shmem.ReadInt32(pos);
                pos  += sizeof(Int32);

                gear = shmem.ReadInt32(pos);
                pos += sizeof(Int32);

                lap  = shmem.ReadInt32(pos);
                pos += sizeof(Int32);

                totallaps = shmem.ReadInt32(pos);
                pos      += sizeof(Int32);

                position = shmem.ReadInt32(pos);
                pos     += sizeof(Int32);

                pitlimiter = shmem.ReadInt32(pos);
                pos       += sizeof(Int32);

                sessiontype = shmem.ReadInt32(pos);
                pos        += sizeof(Int32);

                carinfront = shmem.ReadInt32(pos);
                pos       += sizeof(Int32);

                timestamp = shmem.ReadDouble(pos);
                pos      += sizeof(Double);

                speed = shmem.ReadDouble(pos);
                pos  += sizeof(Double);

                rpm  = shmem.ReadDouble(pos);
                pos += sizeof(Double);

                fuel = shmem.ReadDouble(pos);
                pos += sizeof(Double);

                prevlap = shmem.ReadDouble(pos);
                pos    += sizeof(Double);

                trackLength = shmem.ReadDouble(pos);
                pos        += sizeof(Double);

                maxrpm = shmem.ReadDouble(pos);
                pos   += sizeof(Double);

                shmem.ReadArray <Double>(pos, driverTrkPos, 0, maxcars);
                pos += maxcars * sizeof(Double);

                Byte[] buf = new Byte[64];

                shmem.ReadArray <Byte>(pos, buf, 0, 64);
                pos    += 64 * sizeof(Byte);
                buf     = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, buf);
                carname = Encoding.UTF8.GetString(buf, 0, 64);
                carname = carname.Substring(0, carname.IndexOf('\0'));

                shmem.ReadArray <Byte>(pos, buf, 0, 64);
                pos      += 64 * sizeof(Byte);
                buf       = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, buf);
                trackname = Encoding.UTF8.GetString(buf, 0, 64);
                trackname = trackname.Substring(0, trackname.IndexOf('\0'));

                // process
                if (needReset && trackLength > 0)
                {
                    reset();
                    LoadBestLap();
                    needReset = false;
                }

                if (prevstate != state)
                {
                    if (state == 2) // entering driving mode
                    {
                        LoadBestLap();
                    }
                    else if (prevstate == 2) // exiting driving mode
                    {
                        SaveBestLap();
                    }

                    prevstate = state;
                }

                timedelta.Update(timestamp, driverTrkPos);

                if (driverTrkPos[carid] < 0.1 && lastTickTrackPos > 0.9)
                {
                    Double distance       = (1 - lastTickTrackPos) + driverTrkPos[carid];
                    Double time           = timestamp - lastTickTime;
                    Double tickCorrection = (1 - lastTickTrackPos) / distance;

                    // save lap time
                    if (lapTimeValid)
                    {
                        fuelcons[fuelconsPtr % fuelcons.Length] = fuel;

                        // update fuel consumption after one full lap
                        if (fuelconsPtr > 0)
                        {
                            if (fuelconsPtr >= fuelcons.Length)
                            {
                                Double[] consrate = new Double[fuelcons.Length - 1];
                                Int32    j        = 0;
                                for (int i = fuelconsPtr; i < fuelconsPtr + consrate.Length; i++)
                                {
                                    consrate[j++] = fuelcons[(i + 1) % fuelcons.Length] - fuelcons[(i + 2) % fuelcons.Length];
                                }
                                fuelneed        = (Int32)(fuelcons[fuelconsPtr % fuelcons.Length] - ((totallaps - lap) * consrate.Average()));
                                fuelconsumption = consrate.Average();
                            }
                            else if (fuelconsPtr > 0)
                            {
                                fuelneed        = (Int32)(fuelcons[fuelconsPtr % fuelcons.Length] - ((totallaps - lap) * fuelcons[(fuelconsPtr - 1) % fuelcons.Length]));
                                fuelconsumption = fuelcons[(fuelconsPtr - 1) % fuelcons.Length] - fuelcons[fuelconsPtr % fuelcons.Length];
                            }
                        }
                        fuelconsPtr++;
                    }

                    // start new lap
                    lapStartTime = timestamp - (1 - tickCorrection) * time;
                    lapTimeValid = true;

                    // reset fuel consumption when in pits
                    if (driverTrkPos[carid] < 0)
                    {
                        fuelcons    = new Double[fuelconslaps];
                        fuelconsPtr = 0;
                    }
                }
                else if (Math.Abs(driverTrkPos[carid] - lastTickTrackPos) > 0.1)
                {
                    // invalidate lap time if jumping too much
                    lapTimeValid = false;
                }

                lastTickTrackPos = driverTrkPos[carid]; // save for next tick
                lastTickTime     = timestamp;

                if (sessiontype >= 10) // if (race)
                {
                    delta = timedelta.GetDelta(carid, carinfront);
                }
                else
                {
                    delta = timedelta.GetBestLapDelta(driverTrkPos[carid] % 1);
                }
            }
        }