Exemplo n.º 1
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);
            }
        }