amxmodx/amxmodx/CTask.cpp

228 lines
5.4 KiB
C++
Raw Normal View History

2004-03-05 21:03:14 +00:00
/* AMX Mod X
*
* by the AMX Mod X Development Team
* originally developed by OLO
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*/
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 ***********************/
int CTaskMngr::CTask::getTaskId() const
{
return m_iId;
}
2004-01-31 20:56:22 +00:00
2004-07-18 18:48:20 +00:00
CPluginMngr::CPlugin *CTaskMngr::CTask::getPlugin() const
{
return m_pPlugin;
2004-01-31 20:56:22 +00:00
}
2004-07-18 18:48:20 +00:00
void CTaskMngr::CTask::set(CPluginMngr::CPlugin *pPlugin, int iFunc, int iFlags, int iId, float fBase, int iParamsLen, const cell *pParams, int iRepeat, float fCurrentTime)
{
m_bFree = false;
m_pPlugin = pPlugin;
m_iFunc = iFunc;
m_iId = iId;
m_fBase = fBase;
m_iRepeat = (iFlags & 1) ? iRepeat : 0;
m_bLoop = (iFlags & 2) ? true : false;
m_bAfterStart = (iFlags & 4) ? true : false;
m_bBeforeEnd = (iFlags & 8) ? true : false;
m_fNextExecTime = fCurrentTime + m_fBase;
if (iParamsLen)
{
m_iParamLen = iParamsLen + 1;
m_pParams = new cell[m_iParamLen];
memcpy(m_pParams, pParams, sizeof(cell)*iParamsLen);
m_pParams[iParamsLen] = 0;
2004-08-29 17:11:08 +00:00
} else {
2004-07-18 18:48:20 +00:00
m_iParamLen = 0;
m_pParams = NULL;
2004-01-31 20:56:22 +00:00
}
}
2004-07-18 18:48:20 +00:00
void CTaskMngr::CTask::clear()
{
m_bFree = true;
if (m_pParams)
delete [] m_pParams;
2004-01-31 20:56:22 +00:00
}
2004-07-18 18:48:20 +00:00
bool CTaskMngr::CTask::isFree() const
{
return m_bFree;
}
2004-01-31 20:56:22 +00:00
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)
{
m_fNextExecTime = fCurrentTime + m_fBase;
2004-01-31 20:56:22 +00:00
}
2004-07-18 18:48:20 +00:00
void CTaskMngr::CTask::executeIfRequired(float fCurrentTime, float fTimeLimit, float fTimeLeft)
{
bool execute=false;
if (m_bAfterStart)
{
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;
if (execute)
{
2004-08-29 17:11:08 +00:00
if (m_iParamLen) // call with parameters
2004-07-18 18:48:20 +00:00
{
2004-08-29 17:11:08 +00:00
cell arr = prepareCellArray(m_pParams, m_iParamLen);
executeForwards(m_iFunc, arr, m_iId);
} else {
executeForwards(m_iFunc, m_iId);
2004-07-18 18:48:20 +00:00
}
// set new exec time OR remove the task if needed
if (m_bLoop || (--m_iRepeat > 0))
{
m_fNextExecTime += m_fBase;
}
else
{
2004-08-29 17:11:08 +00:00
unregisterSPForward(m_iFunc);
m_iFunc = 0;
2004-07-18 18:48:20 +00:00
m_bFree = true;
}
2004-01-31 20:56:22 +00:00
}
}
2004-07-18 18:48:20 +00:00
CTaskMngr::CTask::CTask()
{
m_bFree = true;
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 ***********************/
CTaskMngr::CTaskMngr()
{
m_pTmr_CurrentTime = NULL;
m_pTmr_TimeLimit = NULL;
m_pTmr_TimeLeft = NULL;
}
2004-01-31 20:56:22 +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
2004-07-18 18:48:20 +00:00
void CTaskMngr::registerTask(CPluginMngr::CPlugin *pPlugin, int iFunc, int iFlags, int iId, float fBase, int iParamsLen, const cell *pParams, int iRepeat)
{
// first, search for free tasks
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
iter->set(pPlugin, iFunc, iFlags, iId, fBase, iParamsLen, pParams, iRepeat, *m_pTmr_CurrentTime);
2004-01-31 20:56:22 +00:00
}
2004-07-18 18:48:20 +00:00
else
{
// not found: make a new one
CTask *pTmp = new CTask;
if (!pTmp)
return;
pTmp->set(pPlugin, iFunc, iFlags, iId, fBase, iParamsLen, pParams, iRepeat, *m_pTmr_CurrentTime);
m_Tasks.put(pTmp);
2004-01-31 20:56:22 +00:00
}
}
2004-07-18 18:48:20 +00:00
int CTaskMngr::removeTasks(int iId, AMX *pAmx)
2004-01-31 20:56:22 +00:00
{
2004-07-18 18:48:20 +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
{
2004-07-18 18:48:20 +00:00
iter->clear();
++i;
iter = m_Tasks.find(++iter, descriptor);
2004-01-31 20:56:22 +00:00
}
2004-07-18 18:48:20 +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 18:48:20 +00:00
CTaskDescriptor descriptor(iId, pAmx);
TaskListIter iter = m_Tasks.find(descriptor);
int i=0;
while (iter)
{
2004-07-18 18:48:20 +00:00
iter->changeBase(fNewBase);
iter->resetNextExecTime(*m_pTmr_CurrentTime);
++i;
iter = m_Tasks.find(++iter, descriptor);
}
return i;
}
2004-07-18 18:48:20 +00:00
bool CTaskMngr::taskExists(int iId, AMX *pAmx)
{
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
{
2004-07-18 18:48:20 +00:00
for (TaskListIter iter = m_Tasks.begin(); iter; ++iter)
{
if (iter->isFree())
continue;
iter->executeIfRequired(*m_pTmr_CurrentTime, *m_pTmr_TimeLimit, *m_pTmr_TimeLeft);
2004-01-31 20:56:22 +00:00
}
}
2004-07-18 18:48:20 +00:00
void CTaskMngr::clear()
{
m_Tasks.clear();
}