CLuaTimer* CLuaTimerManager::AddTimer ( const CLuaFunctionRef& iLuaFunction, CTickCount llTimeDelay, unsigned int uiRepeats, const CLuaArguments& Arguments ) { // Check for the minimum interval if ( llTimeDelay.ToLongLong () < LUA_TIMER_MIN_INTERVAL ) return NULL; if ( VERIFY_FUNCTION ( iLuaFunction ) ) { // Add the timer CLuaTimer* pLuaTimer = new CLuaTimer ( iLuaFunction, Arguments ); pLuaTimer->SetStartTime ( CTickCount::Now () ); pLuaTimer->SetDelay ( llTimeDelay ); pLuaTimer->SetRepeats ( uiRepeats ); m_TimerList.push_back ( pLuaTimer ); return pLuaTimer; } return NULL; }
void CLuaTimerManager::DoPulse ( CLuaMain* pLuaMain ) { CTickCount llCurrentTime = CTickCount::Now (); m_bIteratingList = true; list < CLuaTimer* > ::iterator iter = m_TimerList.begin (); for ( ; iter != m_TimerList.end (); ) { CLuaTimer* pLuaTimer = *iter; CTickCount llStartTime = pLuaTimer->GetStartTime (); CTickCount llDelay = pLuaTimer->GetDelay (); unsigned int uiRepeats = pLuaTimer->GetRepeats (); // Is the time up and is not being deleted if ( !pLuaTimer->IsBeingDeleted() && llCurrentTime >= ( llStartTime + llDelay ) ) { pLuaTimer->ExecuteTimer ( pLuaMain ); // If this is the last repeat, remove if ( uiRepeats == 1 ) { delete pLuaTimer; iter = m_TimerList.erase ( iter ); } else { // Decrease repeats if not infinite if ( uiRepeats != 0 ) (*iter)->SetRepeats ( uiRepeats - 1 ); pLuaTimer->SetStartTime ( llCurrentTime ); iter++; } } else { iter ++; } } m_bIteratingList = false; TakeOutTheTrash (); }
CLuaTimer* CLuaTimerManager::AddTimer ( lua_State* luaVM ) { if ( luaVM ) { int iArgument2 = lua_type ( luaVM, 2 ); int iArgument3 = lua_type ( luaVM, 3 ); if ( lua_type ( luaVM, 1 ) && (( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) || ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) )) { // Grab the string argument, start-time, delay and repeats unsigned long ulTimeDelay = static_cast < unsigned long > ( lua_tonumber ( luaVM, 2 ) ); unsigned int uiRepeats = static_cast < unsigned int > ( lua_tonumber ( luaVM, 3 ) ); // Check for the minimum interval if ( ulTimeDelay < LUA_TIMER_MIN_INTERVAL ) return NULL; // Grab the arguments from argument 4 and up CLuaArguments Arguments; Arguments.ReadArguments ( luaVM, 4 ); int iLuaFunction = luaM_toref ( luaVM, 1 ); if ( iLuaFunction != LUA_REFNIL ) { // Add the timer CLuaTimer* pLuaTimer = new CLuaTimer ( iLuaFunction, Arguments ); pLuaTimer->SetStartTime ( GetTime () ); pLuaTimer->SetDelay ( ulTimeDelay ); pLuaTimer->SetRepeats ( uiRepeats ); m_TimerList.push_back ( pLuaTimer ); return pLuaTimer; } } } return false; }