2014-08-04 08:36:20 +00:00
|
|
|
// vim: set ts=4 sw=4 tw=99 noet:
|
|
|
|
//
|
|
|
|
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
|
|
|
// Copyright (C) The AMX Mod X Development Team.
|
|
|
|
//
|
|
|
|
// This software is licensed under the GNU General Public License, version 3 or higher.
|
|
|
|
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
|
|
|
// https://alliedmods.net/amxmodx-license
|
2004-01-31 20:56:22 +00:00
|
|
|
|
2004-03-24 01:35:44 +00:00
|
|
|
#include "amxmodx.h"
|
2004-01-31 20:56:22 +00:00
|
|
|
#include "CTask.h"
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
/*********************** CTask ***********************/
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2005-10-18 21:05:52 +00:00
|
|
|
void CTaskMngr::CTask::set(CPluginMngr::CPlugin *pPlugin, int iFunc, int iFlags, cell iId, float fBase, int iParamsLen, const cell *pParams, int iRepeat, float fCurrentTime)
|
2004-07-18 18:48:20 +00:00
|
|
|
{
|
2005-07-25 20:39:58 +00:00
|
|
|
clear();
|
2006-01-06 05:10:17 +00:00
|
|
|
m_bFree = false;
|
2004-07-18 18:48:20 +00:00
|
|
|
|
|
|
|
m_pPlugin = pPlugin;
|
|
|
|
m_iFunc = iFunc;
|
|
|
|
m_iId = iId;
|
|
|
|
m_fBase = fBase;
|
2006-05-16 21:08:19 +00:00
|
|
|
m_bInExecute = false;
|
2004-07-18 18:48:20 +00:00
|
|
|
|
2005-08-17 18:16:26 +00:00
|
|
|
if (iFlags & 2)
|
|
|
|
{
|
|
|
|
m_bLoop = true;
|
|
|
|
m_iRepeat = -1;
|
2005-09-10 20:09:14 +00:00
|
|
|
}
|
|
|
|
else if (iFlags & 1)
|
|
|
|
{
|
2005-08-17 18:16:26 +00:00
|
|
|
m_bLoop = true;
|
|
|
|
m_iRepeat = iRepeat;
|
|
|
|
}
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2006-01-06 05:10:17 +00:00
|
|
|
m_bAfterStart = (iFlags & 4) ? true : false;
|
|
|
|
m_bBeforeEnd = (iFlags & 8) ? true : false;
|
2004-07-18 18:48:20 +00:00
|
|
|
|
|
|
|
m_fNextExecTime = fCurrentTime + m_fBase;
|
|
|
|
|
|
|
|
if (iParamsLen)
|
|
|
|
{
|
|
|
|
m_iParamLen = iParamsLen + 1;
|
2006-01-06 05:10:17 +00:00
|
|
|
m_pParams = new cell[m_iParamLen];
|
|
|
|
memcpy(m_pParams, pParams, sizeof(cell)*iParamsLen);
|
2004-07-18 18:48:20 +00:00
|
|
|
m_pParams[iParamsLen] = 0;
|
2004-08-29 17:11:08 +00:00
|
|
|
} else {
|
2004-07-18 18:48:20 +00:00
|
|
|
m_iParamLen = 0;
|
2006-01-06 05:10:17 +00:00
|
|
|
m_pParams = NULL;
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
void CTaskMngr::CTask::clear()
|
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
m_bFree = true;
|
|
|
|
|
2004-09-18 13:34:39 +00:00
|
|
|
if (m_iFunc >= 0)
|
|
|
|
{
|
|
|
|
unregisterSPForward(m_iFunc);
|
|
|
|
m_iFunc = -1;
|
|
|
|
}
|
2005-07-25 21:02:26 +00:00
|
|
|
|
2006-01-06 05:10:17 +00:00
|
|
|
if (m_pParams)
|
|
|
|
{
|
|
|
|
delete [] m_pParams;
|
|
|
|
m_pParams = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pPlugin = NULL;
|
2005-07-25 21:02:26 +00:00
|
|
|
m_iId = 0;
|
2006-01-06 05:10:17 +00:00
|
|
|
m_fBase = 0.0f;
|
|
|
|
|
|
|
|
m_iRepeat = 0;
|
|
|
|
m_bLoop = false;
|
|
|
|
m_bAfterStart = false;
|
|
|
|
m_bBeforeEnd = false;
|
|
|
|
|
2005-07-25 21:02:26 +00:00
|
|
|
m_fNextExecTime = 0.0f;
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
|
|
|
|
2006-01-06 05:10:17 +00:00
|
|
|
bool CTaskMngr::CTask::isFree() const
|
|
|
|
{
|
|
|
|
return m_bFree;
|
|
|
|
}
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
void CTaskMngr::CTask::changeBase(float fNewBase)
|
|
|
|
{
|
|
|
|
m_fBase = fNewBase;
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
void CTaskMngr::CTask::resetNextExecTime(float fCurrentTime)
|
2013-07-01 20:58:56 +00:00
|
|
|
{
|
|
|
|
// If we're here while we're executing we would add m_fBase twice
|
|
|
|
if (!m_bInExecute)
|
|
|
|
m_fNextExecTime = fCurrentTime + m_fBase;
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
|
|
|
|
2006-01-06 05:10:17 +00:00
|
|
|
void CTaskMngr::CTask::executeIfRequired(float fCurrentTime, float fTimeLimit, float fTimeLeft)
|
2004-07-18 18:48:20 +00:00
|
|
|
{
|
2005-09-10 20:09:14 +00:00
|
|
|
bool execute = false;
|
|
|
|
bool done = false;
|
2006-01-06 05:10:17 +00:00
|
|
|
|
|
|
|
if (m_bAfterStart)
|
2005-09-10 20:09:14 +00:00
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
if (fCurrentTime - fTimeLeft + 1.0f >= m_fBase)
|
|
|
|
execute = true;
|
|
|
|
}
|
|
|
|
else if (m_bBeforeEnd)
|
|
|
|
{
|
|
|
|
if (fTimeLimit != 0.0f && (fTimeLeft + fTimeLimit * 60.0f) - fCurrentTime - 1.0f <= m_fBase)
|
|
|
|
execute = true;
|
|
|
|
}
|
|
|
|
else if (m_fNextExecTime <= fCurrentTime)
|
|
|
|
{
|
|
|
|
execute = true;
|
2005-08-17 18:16:26 +00:00
|
|
|
}
|
2004-07-18 18:48:20 +00:00
|
|
|
|
|
|
|
if (execute)
|
|
|
|
{
|
2005-08-17 18:16:26 +00:00
|
|
|
//only bother calling if we have something to call
|
2005-09-10 20:09:14 +00:00
|
|
|
if (!(m_bLoop && !m_iRepeat))
|
2004-07-18 18:48:20 +00:00
|
|
|
{
|
2006-05-16 21:08:19 +00:00
|
|
|
m_bInExecute = true;
|
2005-08-17 18:16:26 +00:00
|
|
|
if (m_iParamLen) // call with parameters
|
|
|
|
{
|
|
|
|
cell arr = prepareCellArray(m_pParams, m_iParamLen);
|
|
|
|
executeForwards(m_iFunc, arr, m_iId);
|
|
|
|
} else {
|
|
|
|
executeForwards(m_iFunc, m_iId);
|
|
|
|
}
|
2006-05-16 21:08:19 +00:00
|
|
|
m_bInExecute = false;
|
2004-07-18 18:48:20 +00:00
|
|
|
}
|
2006-01-06 05:10:17 +00:00
|
|
|
|
|
|
|
if (isFree())
|
|
|
|
return;
|
2004-09-12 17:00:22 +00:00
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
// set new exec time OR remove the task if needed
|
2005-08-17 18:16:26 +00:00
|
|
|
if (m_bLoop)
|
2004-07-18 18:48:20 +00:00
|
|
|
{
|
2005-08-17 18:16:26 +00:00
|
|
|
if (m_iRepeat != -1 && --m_iRepeat <= 0)
|
|
|
|
done = true;
|
|
|
|
} else {
|
|
|
|
done = true;
|
2004-07-18 18:48:20 +00:00
|
|
|
}
|
2005-08-17 18:16:26 +00:00
|
|
|
|
2006-01-06 05:10:17 +00:00
|
|
|
if (done)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
} else {
|
2013-02-13 09:13:18 +00:00
|
|
|
m_fNextExecTime += m_fBase;
|
2006-01-06 05:10:17 +00:00
|
|
|
}
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
CTaskMngr::CTask::CTask()
|
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
m_bFree = true;
|
|
|
|
|
2005-07-25 21:02:26 +00:00
|
|
|
m_pPlugin = NULL;
|
|
|
|
m_iFunc = -1;
|
|
|
|
m_iId = 0;
|
|
|
|
m_fBase = 0.0f;
|
|
|
|
|
2005-09-16 23:48:51 +00:00
|
|
|
m_iRepeat = 0;
|
|
|
|
m_bLoop = false;
|
2006-01-06 05:10:17 +00:00
|
|
|
m_bAfterStart = false;
|
|
|
|
m_bBeforeEnd = false;
|
2006-05-16 21:08:19 +00:00
|
|
|
m_bInExecute = false;
|
2005-07-25 21:02:26 +00:00
|
|
|
|
|
|
|
m_fNextExecTime = 0.0f;
|
|
|
|
|
|
|
|
m_iParamLen = 0;
|
|
|
|
m_pParams = NULL;
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
CTaskMngr::CTask::~CTask()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
2004-01-31 20:56:22 +00:00
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
/*********************** CTaskMngr ***********************/
|
2005-09-10 20:09:14 +00:00
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
CTaskMngr::CTaskMngr()
|
|
|
|
{
|
|
|
|
m_pTmr_CurrentTime = NULL;
|
|
|
|
m_pTmr_TimeLimit = NULL;
|
|
|
|
m_pTmr_TimeLeft = NULL;
|
|
|
|
}
|
2004-01-31 20:56:22 +00:00
|
|
|
|
2005-07-25 19:09:49 +00:00
|
|
|
CTaskMngr::~CTaskMngr()
|
|
|
|
{
|
2005-11-22 04:14:07 +00:00
|
|
|
clear();
|
2005-07-25 19:09:49 +00:00
|
|
|
}
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
void CTaskMngr::registerTimers(float *pCurrentTime, float *pTimeLimit, float *pTimeLeft)
|
|
|
|
{
|
|
|
|
m_pTmr_CurrentTime = pCurrentTime;
|
|
|
|
m_pTmr_TimeLimit = pTimeLimit;
|
|
|
|
m_pTmr_TimeLeft = pTimeLeft;
|
|
|
|
}
|
2004-01-31 20:56:22 +00:00
|
|
|
|
2005-10-18 21:05:52 +00:00
|
|
|
void CTaskMngr::registerTask(CPluginMngr::CPlugin *pPlugin, int iFunc, int iFlags, cell iId, float fBase, int iParamsLen, const cell *pParams, int iRepeat)
|
2004-07-18 18:48:20 +00:00
|
|
|
{
|
|
|
|
// first, search for free tasks
|
2006-01-06 05:10:17 +00:00
|
|
|
TaskListIter iter = m_Tasks.find(CTaskDescriptor(0, NULL, true));
|
|
|
|
|
|
|
|
if (iter)
|
2004-01-31 20:56:22 +00:00
|
|
|
{
|
2004-07-18 18:48:20 +00:00
|
|
|
// found: reuse it
|
2006-01-06 05:10:17 +00:00
|
|
|
iter->set(pPlugin, iFunc, iFlags, iId, fBase, iParamsLen, pParams, iRepeat, *m_pTmr_CurrentTime);
|
2005-09-10 20:09:14 +00:00
|
|
|
} else {
|
2004-07-18 18:48:20 +00:00
|
|
|
// not found: make a new one
|
2006-01-06 05:10:17 +00:00
|
|
|
CTask *pTmp = new CTask;
|
|
|
|
|
|
|
|
if (!pTmp)
|
|
|
|
return;
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
pTmp->set(pPlugin, iFunc, iFlags, iId, fBase, iParamsLen, pParams, iRepeat, *m_pTmr_CurrentTime);
|
2006-01-06 05:10:17 +00:00
|
|
|
m_Tasks.put(pTmp);
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
2005-11-29 00:52:35 +00:00
|
|
|
}
|
2005-11-22 04:14:07 +00:00
|
|
|
|
2005-11-29 00:52:35 +00:00
|
|
|
int CTaskMngr::removeTasks(int iId, AMX *pAmx)
|
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
CTaskDescriptor descriptor(iId, pAmx);
|
|
|
|
TaskListIter iter = m_Tasks.find(descriptor);
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (iter)
|
2004-01-31 20:56:22 +00:00
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
iter->clear();
|
|
|
|
++i;
|
|
|
|
iter = m_Tasks.find(++iter, descriptor);
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
2006-01-06 05:10:17 +00:00
|
|
|
|
|
|
|
return i;
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
int CTaskMngr::changeTasks(int iId, AMX *pAmx, float fNewBase)
|
2004-07-18 08:07:50 +00:00
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
CTaskDescriptor descriptor(iId, pAmx);
|
|
|
|
TaskListIter iter = m_Tasks.find(descriptor);
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (iter)
|
2004-07-18 08:07:50 +00:00
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
iter->changeBase(fNewBase);
|
|
|
|
iter->resetNextExecTime(*m_pTmr_CurrentTime);
|
|
|
|
++i;
|
|
|
|
iter = m_Tasks.find(++iter, descriptor);
|
2004-07-18 08:07:50 +00:00
|
|
|
}
|
2006-01-06 05:10:17 +00:00
|
|
|
|
|
|
|
return i;
|
2004-07-18 08:07:50 +00:00
|
|
|
}
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
bool CTaskMngr::taskExists(int iId, AMX *pAmx)
|
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
return m_Tasks.find(CTaskDescriptor(iId, pAmx));
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
void CTaskMngr::startFrame()
|
2004-01-31 20:56:22 +00:00
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
for (TaskListIter iter = m_Tasks.begin(); iter; ++iter)
|
2005-11-29 00:52:35 +00:00
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
if (iter->isFree())
|
|
|
|
continue;
|
|
|
|
iter->executeIfRequired(*m_pTmr_CurrentTime, *m_pTmr_TimeLimit, *m_pTmr_TimeLeft);
|
2005-11-29 00:52:35 +00:00
|
|
|
}
|
2004-01-31 20:56:22 +00:00
|
|
|
}
|
|
|
|
|
2004-07-18 18:48:20 +00:00
|
|
|
void CTaskMngr::clear()
|
|
|
|
{
|
2006-01-06 05:10:17 +00:00
|
|
|
m_Tasks.clear();
|
2004-08-31 06:49:22 +00:00
|
|
|
}
|