public long UpdateSpeed(long wiped)
{
values[position % MaxSpeedSteps] = wiped;
times[position % MaxSpeedSteps] = Environment.TickCount;
position++;
// update ?
if(Environment.TickCount - lastTick >= SpeedUpdateInterval) {
int count = Math.Min(position, SpeedUpdateInterval);
speed = 0;
if(count < SpeedUpdateInterval) {
for(int i = 0; i < position; i++) {
if(i >= 1) {
long speedDiff = Math.Abs(values[i % MaxSpeedSteps] - values[(i - 1) % MaxSpeedSteps]);
int timeDiff = Math.Abs(times[i % MaxSpeedSteps] - times[(i - 1) % MaxSpeedSteps]);
speed += (1000 * speedDiff) / Math.Max(1, timeDiff);
}
}
}
else {
for(int i = 0; i < count; i++) {
speed += values[position % MaxSpeedSteps] - values[(position - 1) % MaxSpeedSteps];
}
}
speed /= Math.Max(1, count);
lastTick = Environment.TickCount;
}
if(position == int.MaxValue) {
position = MaxSpeedSteps;
}
return speed;
}