mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-06 00:55:50 +00:00
675c0ab72f
This allows to distribute AI reaction calls over time. Before this change actors appearing at the same frame will react in the same frame over and over because AI reaction period is constant. It creates a non-uniform CPU usage over frames. If a single frame has too many AI reactions it may cause stuttering when there are too many actors on a scene for current system. Another concern is a synchronization of actions between creatures and NPC. They start to go or hit at the same frame that is unnatural.
43 lines
977 B
C++
43 lines
977 B
C++
#ifndef OPENMW_COMPONENTS_MISC_TIMER_H
|
|
#define OPENMW_COMPONENTS_MISC_TIMER_H
|
|
|
|
#include "rng.hpp"
|
|
|
|
namespace Misc
|
|
{
|
|
enum class TimerStatus
|
|
{
|
|
Waiting,
|
|
Elapsed,
|
|
};
|
|
|
|
class DeviatingPeriodicTimer
|
|
{
|
|
public:
|
|
explicit DeviatingPeriodicTimer(float period, float deviation, float timeLeft)
|
|
: mPeriod(period), mDeviation(deviation), mTimeLeft(timeLeft)
|
|
{}
|
|
|
|
TimerStatus update(float duration)
|
|
{
|
|
if (mTimeLeft > 0)
|
|
{
|
|
mTimeLeft -= duration;
|
|
return TimerStatus::Waiting;
|
|
}
|
|
|
|
mTimeLeft = Rng::deviate(mPeriod, mDeviation);
|
|
return TimerStatus::Elapsed;
|
|
}
|
|
|
|
void reset(float timeLeft) { mTimeLeft = timeLeft; }
|
|
|
|
private:
|
|
const float mPeriod;
|
|
const float mDeviation;
|
|
float mTimeLeft;
|
|
};
|
|
}
|
|
|
|
#endif
|