int CLuaTimerDefs::GetTimers ( lua_State* luaVM ) { // table getTimers ( [ time ] ) double dTime; CScriptArgReader argStream ( luaVM ); argStream.ReadNumber ( dTime, 0 ); if ( !argStream.HasErrors () ) { // Find our VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { // Create a new table lua_newtable ( luaVM ); // Add all the timers with less than ulTime left CLuaTimerManager* pLuaTimerManager = pLuaMain->GetTimerManager (); CTickCount llCurrentTime = CTickCount::Now (); unsigned int uiIndex = 0; CFastList < CLuaTimer* > ::const_iterator iter = pLuaTimerManager->IterBegin (); for ( ; iter != pLuaTimerManager->IterEnd (); iter++ ) { CLuaTimer* pLuaTimer = *iter; // If the time left is less than the time specified, or the time specifed is 0 CTickCount llTimeLeft = ( pLuaTimer->GetStartTime () + pLuaTimer->GetDelay () ) - llCurrentTime; if ( dTime == 0 || llTimeLeft.ToDouble () <= dTime ) { // Add it to the table lua_pushnumber ( luaVM, ++uiIndex ); lua_pushtimer ( luaVM, pLuaTimer ); lua_settable ( luaVM, -3 ); } } return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
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 (); }
int CLuaTimerDefs::GetTimerDetails ( lua_State* luaVM ) { // int, int, int getTimerDetails ( timer theTimer ) CLuaTimer* pLuaTimer; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pLuaTimer ); if ( !argStream.HasErrors () ) { lua_pushnumber ( luaVM, pLuaTimer->GetTimeLeft ().ToDouble () ); lua_pushnumber ( luaVM, pLuaTimer->GetRepeats () ); lua_pushnumber ( luaVM, pLuaTimer->GetDelay ().ToDouble () ); return 3; } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }