diff options
author | Benoit Bolsee <benoit.bolsee@online.be> | 2008-03-01 21:46:23 +0300 |
---|---|---|
committer | Benoit Bolsee <benoit.bolsee@online.be> | 2008-03-01 21:46:23 +0300 |
commit | 8d81e154f60744b49c89ef79ec2f4b0325b1effe (patch) | |
tree | 4b472831b8ed5304754e750464a8a9cc152c4313 /source/gameengine | |
parent | 6b48255e5c1eeac17a50a532281e317cfaa7198f (diff) |
random sensor fixed (patch 8414)
Diffstat (limited to 'source/gameengine')
-rw-r--r-- | source/gameengine/GameLogic/SCA_RandomSensor.cpp | 34 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_RandomSensor.h | 1 |
2 files changed, 23 insertions, 12 deletions
diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.cpp b/source/gameengine/GameLogic/SCA_RandomSensor.cpp index f747504a697..56c7ed538b8 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.cpp +++ b/source/gameengine/GameLogic/SCA_RandomSensor.cpp @@ -54,11 +54,15 @@ SCA_RandomSensor::SCA_RandomSensor(SCA_EventManager* eventmgr, : SCA_ISensor(gameobj,eventmgr, T) { m_iteration = 0; + m_interval = 0; m_lastdraw = false; + // m_basegenerator is never deleted => memory leak m_basegenerator = new SCA_RandomNumberGenerator(startseed); m_currentDraw = m_basegenerator->Draw(); - RegisterToManager(); + //registration is done globally, don't do it here + //Note: it was probably done to work around a bug in Evaluate(). It is now fixed + //RegisterToManager(); } @@ -73,6 +77,7 @@ SCA_RandomSensor::~SCA_RandomSensor() CValue* SCA_RandomSensor::GetReplica() { CValue* replica = new SCA_RandomSensor(*this); + // replication copies m_basegenerator pointer => share same generator // this will copy properties and so on... CValue::AddDataToReplica(replica); @@ -98,20 +103,25 @@ bool SCA_RandomSensor::Evaluate(CValue* event) /* this is a reasonable way of generating bools. Check Knuth. */ /* Furthermore, we only draw each <delay>-eth frame. */ - bool drawResult = false; - - if (m_iteration > 31) { - m_currentDraw = m_basegenerator->Draw(); - drawResult = (m_currentDraw & 0x1) == 0; - m_iteration = 1; - } else { - drawResult = ((m_currentDraw >> m_iteration) & 0x1) == 0; - m_iteration++; + bool evaluateResult = false; + + if (++m_interval > m_pulse_frequency) { + bool drawResult = false; + m_interval = 0; + if (m_iteration > 31) { + m_currentDraw = m_basegenerator->Draw(); + drawResult = (m_currentDraw & 0x1) == 0; + m_iteration = 1; + } else { + drawResult = ((m_currentDraw >> m_iteration) & 0x1) == 0; + m_iteration++; + } + evaluateResult = drawResult != m_lastdraw; + m_lastdraw = drawResult; } /* now pass this result to some controller */ - m_lastdraw = drawResult; - return drawResult; + return evaluateResult; } /* ------------------------------------------------------------------------- */ diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.h b/source/gameengine/GameLogic/SCA_RandomSensor.h index 1a3c0402e31..08bb5226be1 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.h +++ b/source/gameengine/GameLogic/SCA_RandomSensor.h @@ -45,6 +45,7 @@ class SCA_RandomSensor : public SCA_ISensor unsigned int m_currentDraw; int m_iteration; + int m_interval; SCA_RandomNumberGenerator *m_basegenerator; bool m_lastdraw; public: |