using System;
using JetBrains.Annotations;
using Robust.Shared.IoC;
namespace Robust.Shared.Timing
{
///
/// This holds main loop timing information and helper functions.
///
public interface IGameTiming
{
///
/// Is program execution inside of the simulation, or rendering?
///
bool InSimulation { get; set; }
///
/// Is the simulation currently paused?
///
bool Paused { get; set; }
///
/// The current synchronized uptime of the simulation. Use this for in-game timing. This can be rewound for
/// prediction, and is affected by Paused and TimeScale.
///
TimeSpan CurTime { get; }
///
/// The current real uptime of the simulation. Use this for UI and out of game timing.
///
TimeSpan RealTime { get; }
///
/// The of the server.
///
///
/// 0 if we are the client and we are not connected to a server.
/// if we are the server.
///
TimeSpan ServerTime { get; }
///
/// The simulated time it took to render the last frame.
///
TimeSpan FrameTime { get; }
///
/// The real time it took to render the last frame.
///
TimeSpan RealFrameTime { get; }
///
/// Average real frame time over the last 50 frames.
///
TimeSpan RealFrameTimeAvg { get; }
///
/// Standard Deviation of the frame time over the last 50 frames.
///
TimeSpan RealFrameTimeStdDev { get; }
///
/// Current graphics frame since init OpenGL which is taken as frame 1. Useful to set a conditional breakpoint on specific frames, and
/// synchronize with OGL debugging tools that capture frames. Depending on the tools used, this frame
/// number will vary between 1 frame more or less due to how that tool is counting frames,
/// i.e. starting from 0 or 1, having a separate counter, etc. Available in timing debug panel.
///
uint CurFrame { get; set; }
///
/// Average real FPS over the last 50 frames.
///
double FramesPerSecondAvg { get; }
///
/// The current simulation tick being processed.
///
GameTick CurTick { get; set; }
///
/// Time, relative to , the last tick started at.
/// If we're currently in simulation, that's THIS tick.
///
TimeSpan LastTick { get; set; }
///
/// The target ticks/second of the simulation.
///
byte TickRate { get; set; }
///
/// The baseline time value that CurTime is calculated relatively to.
///
(TimeSpan, GameTick) TimeBase { get; set; }
///
/// The length of a tick at the current TickRate. 1/TickRate.
///
TimeSpan TickPeriod { get; }
///
/// The remaining time left over after the last tick was ran.
///
TimeSpan TickRemainder { get; set; }
TimeSpan CalcAdjustedTickPeriod();
///
/// Fraction of how far into the tick we are. 0 is 0% and is 100%.
///
ushort TickFraction
{
get
{
if (InSimulation)
{
return ushort.MaxValue;
}
return (ushort)(ushort.MaxValue * TickRemainder.TotalSeconds / TickPeriod.TotalSeconds);
}
}
///
/// If the client clock is a little behind or ahead of the server, you can
/// use the to adjust the timing of the clock speed. The default value is 0,
/// and you can run the clock from -1 (almost stopped) to 1 (almost no delay).
/// This has no effect on in-simulation timing, and only changes the speed at which
/// the simulation progresses in relation to Real time. Don't mess with this unless
/// you know what you are doing. DO NOT TOUCH THIS ON SERVER.
///
float TickTimingAdjustment { get; set; }
///
/// Ends the 'lap' of the timer, updating frame time info.
///
void StartFrame();
///
/// Is this the first time CurTick has been predicted?
///
bool IsFirstTimePredicted { get; }
///
/// True if CurTick is ahead of LastRealTick, and is false.
///
bool InPrediction { get; }
///
/// If true, the game is currently in the process of applying a game server-state.
///
bool ApplyingState { get; }
string TickStamp => $"{CurTick}, predFirst: {IsFirstTimePredicted}, tickRem: {TickRemainder.TotalSeconds}, sim: {InSimulation}";
///
/// Statically-accessible version of .
///
///
/// This is intended as a debugging aid, and should not be used in regular committed code.
///
static string TickStampStatic => IoCManager.Resolve().TickStamp;
///
/// Resets the simulation time. This should be called on round restarts.
///
void ResetSimTime();
void ResetSimTime((TimeSpan, GameTick) timeBase);
void SetTickRateAt( byte tickRate, GameTick atTick);
TimeSpan RealLocalToServer(TimeSpan local);
TimeSpan RealServerToLocal(TimeSpan server);
}
}