Skip to content

Commit 69aa420

Browse files
jvstnstederis
andauthored
Adds new timer functions (#3683)
* Add SetPaused to timer class * Add pause variables to class * Add new functions "pauseTimer" and "isTimerPaused" * Add new timer function defs - pauseTimer - isTimerPaused * Add "PauseTimer" to CLuaTimerManager * Def "PauseTimer" in CLuaTimerManager * Adds pauseTimer() and isTimerPaused() * Add new timer function defs * Add "PauseTimer" to CLuaTimerManager * Def "PauseTimer" in CLuaTimerManager * fix typo * fix typo * A few minor adjustments * Minor adjustments #2 * Update to ArgumentParser * Update to ArgumentParser * minor adjustment * Minor adjustment to function name * Updated to ArgumentParser and changed function name * Update to ArgumentParser * Minor adjustment to function name * Minor adjustment to function name * Removed hungarian notation * Removed hungarian notation * Removed hungarian notation & useless luaState * Removed hungarian notation & useless luaState * Removed hungarian notations * Removed hungarian notation * Removed hungarian notation & useless luaState * Removed hungarian notation & useless luaState * Removed hungarian notations * Removed hungarian notations --------- Co-authored-by: TEDERIs <xcplay@gmail.com>
1 parent 82000c3 commit 69aa420

File tree

10 files changed

+103
-10
lines changed

10 files changed

+103
-10
lines changed

Client/mods/deathmatch/logic/lua/CLuaTimerManager.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ void CLuaTimerManager::DoPulse(CLuaMain* pLuaMain)
2121

2222
// Use a separate queue to avoid trouble
2323
for (CFastList<CLuaTimer*>::const_iterator iter = m_TimerList.begin(); iter != m_TimerList.end(); iter++)
24-
m_ProcessQueue.push_back(*iter);
24+
{
25+
if (!(*iter)->IsPaused())
26+
m_ProcessQueue.push_back(*iter);
27+
}
2528

2629
while (!m_ProcessQueue.empty())
2730
{
@@ -108,6 +111,16 @@ void CLuaTimerManager::RemoveAllTimers()
108111
m_pProcessingTimer = NULL;
109112
}
110113

114+
void CLuaTimerManager::SetTimerPaused(CLuaTimer* timer, bool paused)
115+
{
116+
assert(timer);
117+
118+
timer->SetPaused(paused);
119+
if (paused)
120+
ListRemove(m_ProcessQueue, timer);
121+
}
122+
123+
111124
void CLuaTimerManager::ResetTimer(CLuaTimer* pLuaTimer)
112125
{
113126
assert(pLuaTimer);

Client/mods/deathmatch/logic/lua/CLuaTimerManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CLuaTimerManager
3636
void RemoveAllTimers();
3737
unsigned long GetTimerCount() const { return m_TimerList.size(); }
3838

39+
void SetTimerPaused(CLuaTimer* timer, bool paused);
3940
void ResetTimer(CLuaTimer* pLuaTimer);
4041

4142
CFastList<CLuaTimer*>::const_iterator IterBegin() { return m_TimerList.begin(); }

Client/mods/deathmatch/logic/luadefs/CLuaTimerDefs.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
*****************************************************************************/
1111

1212
#include "StdInc.h"
13+
#include <lua/CLuaFunctionParser.h>
1314

1415
void CLuaTimerDefs::LoadFunctions()
1516
{
1617
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
17-
{"setTimer", SetTimer}, {"killTimer", KillTimer}, {"resetTimer", ResetTimer},
18-
{"getTimers", GetTimers}, {"isTimer", IsTimer}, {"getTimerDetails", GetTimerDetails},
18+
{"setTimer", SetTimer}, {"killTimer", KillTimer}, {"resetTimer", ResetTimer},
19+
{"setTimerPaused", ArgumentParser<SetTimerPaused>},{"isTimerPaused", ArgumentParser<IsTimerPaused>},
20+
{"getTimers", GetTimers}, {"isTimer", IsTimer}, {"getTimerDetails", GetTimerDetails},
1921
};
2022

2123
// Add functions
@@ -31,10 +33,10 @@ void CLuaTimerDefs::AddClass(lua_State* luaVM)
3133
lua_classfunction(luaVM, "destroy", "killTimer");
3234
lua_classfunction(luaVM, "reset", "resetTimer");
3335
lua_classfunction(luaVM, "isValid", "isTimer");
34-
3536
lua_classfunction(luaVM, "getDetails", "getTimerDetails");
3637

3738
lua_classvariable(luaVM, "valid", NULL, "isTimer");
39+
lua_classvariable(luaVM, "paused", "setTimerPaused", "isTimerPaused");
3840

3941
lua_registerclass(luaVM, "Timer");
4042
}
@@ -111,6 +113,22 @@ int CLuaTimerDefs::KillTimer(lua_State* luaVM)
111113
return 1;
112114
}
113115

116+
bool CLuaTimerDefs::IsTimerPaused(CLuaTimer* timer) noexcept
117+
{
118+
return timer->IsPaused();
119+
}
120+
121+
bool CLuaTimerDefs::SetTimerPaused(lua_State* luaVM, CLuaTimer* timer, bool paused)
122+
{
123+
// bool setTimerPaused ( timer theTimer, bool paused )
124+
CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM);
125+
if (!luaMain)
126+
return false;
127+
128+
luaMain->GetTimerManager()->SetTimerPaused(timer, paused);
129+
return true;
130+
}
131+
114132
int CLuaTimerDefs::ResetTimer(lua_State* luaVM)
115133
{
116134
// bool resetTimer ( timer theTimer )

Client/mods/deathmatch/logic/luadefs/CLuaTimerDefs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ class CLuaTimerDefs : public CLuaDefs
2424
LUA_DECLARE(GetTimers);
2525
LUA_DECLARE(IsTimer);
2626
LUA_DECLARE(GetTimerDetails);
27+
static bool IsTimerPaused(CLuaTimer* timer) noexcept;
28+
static bool SetTimerPaused(lua_State* luaVM, CLuaTimer* timer, bool paused);
2729
};

Server/mods/deathmatch/logic/lua/CLuaTimerManager.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ void CLuaTimerManager::DoPulse(CLuaMain* pLuaMain)
2626
// Use a separate queue to avoid trouble
2727
// What kind of problems are we trying to avoid? Doing a copy each frame isn't quite efficient
2828
for (CFastList<CLuaTimer*>::const_iterator iter = m_TimerList.begin(); iter != m_TimerList.end(); ++iter)
29-
m_ProcessQueue.push_back(*iter);
29+
{
30+
if (!(*iter)->IsPaused())
31+
m_ProcessQueue.push_back(*iter);
32+
}
3033

3134
while (!m_ProcessQueue.empty())
3235
{
@@ -113,6 +116,15 @@ void CLuaTimerManager::RemoveAllTimers()
113116
m_pProcessingTimer = NULL;
114117
}
115118

119+
void CLuaTimerManager::SetTimerPaused(CLuaTimer* timer, bool paused)
120+
{
121+
assert(timer);
122+
123+
timer->SetPaused(paused);
124+
if (paused)
125+
ListRemove(m_ProcessQueue, timer);
126+
}
127+
116128
void CLuaTimerManager::ResetTimer(CLuaTimer* pLuaTimer)
117129
{
118130
assert(pLuaTimer);

Server/mods/deathmatch/logic/lua/CLuaTimerManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CLuaTimerManager
3636
void RemoveAllTimers();
3737
unsigned long GetTimerCount() const { return m_TimerList.size(); }
3838

39+
void SetTimerPaused(CLuaTimer* timer, bool paused);
3940
void ResetTimer(CLuaTimer* pLuaTimer);
4041

4142
CFastList<CLuaTimer*>::const_iterator IterBegin() { return m_TimerList.begin(); }

Server/mods/deathmatch/logic/luadefs/CLuaTimerDefs.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
void CLuaTimerDefs::LoadFunctions()
1717
{
1818
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
19-
{"setTimer", SetTimer}, {"killTimer", KillTimer}, {"resetTimer", ResetTimer},
20-
{"getTimers", GetTimers}, {"isTimer", IsTimer}, {"getTimerDetails", GetTimerDetails},
19+
{"setTimer", SetTimer}, {"killTimer", KillTimer}, {"resetTimer", ResetTimer},
20+
{"setTimerPaused", ArgumentParser<SetTimerPaused>},{"isTimerPaused", ArgumentParser<IsTimerPaused>},
21+
{"getTimers", GetTimers}, {"isTimer", IsTimer},{"getTimerDetails", GetTimerDetails},
2122
};
2223

2324
// Add functions
@@ -33,10 +34,10 @@ void CLuaTimerDefs::AddClass(lua_State* luaVM)
3334
lua_classfunction(luaVM, "destroy", "killTimer");
3435
lua_classfunction(luaVM, "reset", "resetTimer");
3536
lua_classfunction(luaVM, "isValid", "isTimer");
36-
3737
lua_classfunction(luaVM, "getDetails", "getTimerDetails");
3838

3939
lua_classvariable(luaVM, "valid", NULL, "isTimer");
40+
lua_classvariable(luaVM, "paused", "setTimerPaused", "isTimerPaused");
4041

4142
lua_registerclass(luaVM, "Timer");
4243
}
@@ -114,6 +115,22 @@ int CLuaTimerDefs::KillTimer(lua_State* luaVM)
114115
return 1;
115116
}
116117

118+
bool CLuaTimerDefs::IsTimerPaused(CLuaTimer* timer) noexcept
119+
{
120+
return timer->IsPaused();
121+
}
122+
123+
bool CLuaTimerDefs::SetTimerPaused(lua_State* luaVM, CLuaTimer* timer, bool paused)
124+
{
125+
// bool setTimerPaused ( timer theTimer, bool paused )
126+
CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM);
127+
if (!luaMain)
128+
return false;
129+
130+
luaMain->GetTimerManager()->SetTimerPaused(timer, paused);
131+
return true;
132+
}
133+
117134
int CLuaTimerDefs::ResetTimer(lua_State* luaVM)
118135
{
119136
// bool resetTimer ( timer theTimer )

Server/mods/deathmatch/logic/luadefs/CLuaTimerDefs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ class CLuaTimerDefs : public CLuaDefs
2424
LUA_DECLARE(GetTimers);
2525
LUA_DECLARE(IsTimer);
2626
LUA_DECLARE(GetTimerDetails);
27-
};
27+
static bool IsTimerPaused(CLuaTimer* timer) noexcept;
28+
static bool SetTimerPaused(lua_State* luaVM, CLuaTimer* timer, bool paused);
29+
};

Shared/mods/deathmatch/logic/lua/CLuaTimer.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ CLuaTimer::CLuaTimer(const CLuaFunctionRef& iLuaFunction, const CLuaArguments& A
2323
m_uiRepeats = 1;
2424
m_iLuaFunction = iLuaFunction;
2525
m_Arguments = Arguments;
26+
m_paused = false;
2627
}
2728

2829
CLuaTimer::~CLuaTimer()
@@ -64,8 +65,30 @@ void CLuaTimer::ExecuteTimer(CLuaMain* pLuaMain)
6465
}
6566
}
6667

68+
void CLuaTimer::SetPaused(bool paused)
69+
{
70+
if (paused == IsPaused())
71+
return;
72+
73+
CTickCount llTimeRemaining = GetTimeLeft();
74+
if (paused)
75+
{
76+
m_pausedRemainingTime = llTimeRemaining.ToLongLong() == 0LL ? m_llDelay : llTimeRemaining;
77+
}
78+
else
79+
{
80+
CTickCount llCurrentTime = CTickCount::Now();
81+
CTickCount llNewStartTime = llCurrentTime - (m_llDelay - llTimeRemaining);
82+
SetStartTime(llNewStartTime);
83+
}
84+
m_paused = paused;
85+
}
86+
6787
CTickCount CLuaTimer::GetTimeLeft()
6888
{
89+
if (IsPaused())
90+
return m_pausedRemainingTime;
91+
6992
CTickCount llCurrentTime = CTickCount::Now();
7093
CTickCount llTimeLeft = m_llStartTime + m_llDelay - llCurrentTime;
7194
return llTimeLeft.ToLongLong() < 0LL ? CTickCount(0LL) : llTimeLeft;

Shared/mods/deathmatch/logic/lua/CLuaTimer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CLuaTimer;
1717
#include "lua/LuaCommon.h"
1818
#include "lua/CLuaArguments.h"
1919

20-
#define LUA_TIMER_MIN_INTERVAL 0
20+
#define LUA_TIMER_MIN_INTERVAL 0
2121

2222
class CLuaTimer
2323
{
@@ -35,6 +35,8 @@ class CLuaTimer
3535

3636
unsigned int GetRepeats() const { return m_uiRepeats; };
3737
void SetRepeats(unsigned int uiRepeats) { m_uiRepeats = uiRepeats; }
38+
bool IsPaused() const noexcept { return m_paused; };
39+
void SetPaused(bool paused);
3840

3941
void ExecuteTimer(class CLuaMain* pLuaMain);
4042

@@ -45,10 +47,12 @@ class CLuaTimer
4547
void SetLuaDebugInfo(const SLuaDebugInfo& luaDebugInfo) { m_LuaDebugInfo = luaDebugInfo; }
4648

4749
private:
50+
bool m_paused;
4851
CLuaFunctionRef m_iLuaFunction;
4952
CLuaArguments m_Arguments;
5053
CTickCount m_llStartTime;
5154
CTickCount m_llDelay;
55+
CTickCount m_pausedRemainingTime;
5256
unsigned int m_uiRepeats;
5357
uint m_uiScriptID;
5458
SLuaDebugInfo m_LuaDebugInfo;

0 commit comments

Comments
 (0)