⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 events.cpp

📁 MudMaster 2000 的C++源码
💻 CPP
字号:
/************************************************************************************
	Copyright (c) 2000 Aaron O'Neil
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

		1) Redistributions of source code must retain the above copyright notice, 
				this list of conditions and the following disclaimer.
		2) Redistributions in binary form must reproduce the above copyright notice, 
				this list of conditions and the following disclaimer in the documentation
				and/or other materials provided with the distribution.
		3) Redistributions in binary form must reproduce the above copyright notice on
				program startup. Additional credits for program modification are acceptable
				but original copyright and credits must be visible at startup.
		4) You may charge a reasonable copying fee for any distribution of Mud Master. 
				You may charge any fee you choose for support of Mud Master. You may not 
				charge a fee for Mud Master itself.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************************/

#include "StdAfx.h"
#include "Events.h"

BOOL _bCheckEvents = FALSE;
DWORD _dwTime = 0;

// Checks for elapsed time and updates _dwTime.
DWORD _dwTimeTemp;
void CheckTime()
{
	_dwTimeTemp = GetTickCount() / 1000;
	if (_dwTimeTemp != _dwTime)
	{
		_dwTime = _dwTimeTemp;
		_bCheckEvents = TRUE;
	}
}

CEvents::CEvents()
{
	m_ptrList.SetSize(DEFAULT_EVENT_ARRAY_SIZE,DEFAULT_EVENT_GROW_SIZE);
	m_nCount = 0;
	m_nGetIndex = 0;
}

CEvents::~CEvents()
{
	RemoveAll();
}

void CEvents::RemoveAll()
{
	EVENT *pAct;
	for (int i=0;i<m_nCount;i++)
	{
		pAct = (EVENT *)m_ptrList.GetAt(i);
		delete pAct;
	}
	m_ptrList.RemoveAll();
	m_nCount = 0;
	m_nGetIndex = 0;
	HashIt();
}

int CEvents::RemoveGroup(const char *pszGroup)
{
	EVENT *pEvent;
	int nCount = 0;
	for (int i=m_nCount-1;i>-1;i--)
	{
		pEvent = (EVENT *)m_ptrList.GetAt(i);
		if (pEvent->strGroup == pszGroup)
		{
			Remove(i);
			nCount++;
		}
	}
	HashIt();
	return(nCount);
}

int CEvents::DisableGroup(const char *pszGroup)
{
	int nCount = 0;
	EVENT *pEvent = GetFirst();
	while(pEvent != NULL)
	{
		if (pEvent->strGroup == pszGroup)
		{
			pEvent->bEnabled = FALSE;
			nCount++;
		}
		pEvent = GetNext();
	}
	return(nCount);
}

int CEvents::EnableGroup(const char *pszGroup)
{
	int nCount = 0;
	EVENT *pEVENT = GetFirst();
	while(pEVENT != NULL)
	{
		if (pEVENT->strGroup == pszGroup)
		{
			pEVENT->bEnabled = TRUE;
			nCount++;
		}
		pEVENT = GetNext();
	}
	return(nCount);
}

int CEvents::Add(const char *pszName, int nSeconds, const char *pszEvent,
	const char *pszGroup)
{

	EVENT *pEvent;
	for (int i=0;i<m_nCount;i++)
	{
		pEvent = (EVENT *)m_ptrList.GetAt(i);

		// Replace the existing EVENT.
		if (pEvent->strName == pszName)
		{
			pEvent->nSeconds = nSeconds;
			pEvent->dwElapse = nSeconds;
			pEvent->dwStarted = _dwTime;
			pEvent->strEvent = pszEvent;
			pEvent->strGroup = pszGroup;
			HashIt();
			return(i+1);
		}

		// Insert a new EVENT.
		if (pEvent->strName > pszName)
		{
			EVENT *pNew = new EVENT;
			pNew->nSeconds = nSeconds;
			pNew->dwElapse = nSeconds;
			pNew->strEvent = pszEvent;
			pNew->dwStarted = _dwTime;
			pNew->strName = pszName;
			pNew->strGroup = pszGroup;
			pNew->bEnabled = TRUE;

			m_ptrList.InsertAt(i,pNew);
			m_nCount++;
			HashIt();
			return(i+1);
		}
	}

	// Add it to the tail.
	EVENT *pNew = new EVENT;
	pNew->nSeconds = nSeconds;
	pNew->dwElapse = nSeconds;
	pNew->strEvent = pszEvent;
	pNew->dwStarted = _dwTime;
	pNew->strName = pszName;
	pNew->strGroup = pszGroup;
	pNew->bEnabled = TRUE;
	m_ptrList.SetAtGrow(i,pNew);
	m_nCount++;
	HashIt();
	return(m_nCount);
}

BOOL CEvents::Remove(int nIndex)
{
	if (nIndex < 0 || nIndex >= m_nCount)
		return(FALSE);

	EVENT *pEVENT = (EVENT *)m_ptrList.GetAt(nIndex);
	if (pEVENT == NULL)
		return(FALSE);

	m_ptrList.RemoveAt(nIndex);
	m_nCount--;
	delete pEVENT;
	HashIt();
	return(TRUE);
}

BOOL CEvents::Remove(const char *pszName)
{
	EVENT *pEVENT;
	for (int i=0;i<m_nCount;i++)
	{	
		pEVENT = (EVENT *)m_ptrList.GetAt(i);
		if (pEVENT->strName == pszName)
		{
			m_ptrList.RemoveAt(i);
			m_nCount--;
			delete pEVENT;
			HashIt();
			return(TRUE);
		}
	}
	return(FALSE);
}

EVENT* CEvents::FindByName(const char *pszName)
{
	if (!*pszName)
		return(NULL);

	HASHITEM *phi = m_htHash[*pszName];
	if (!phi || phi->nStart == HASHITEM_EMPTY || phi->nEnd == HASHITEM_EMPTY)
		return(NULL);

	EVENT *pEvent;
	for (int i=phi->nStart;i<=phi->nEnd;i++)
	{
		pEvent = (EVENT *)m_ptrList.GetAt(i);
		if (!pEvent)
			continue;
		if (pEvent->strName == pszName)
		{
			m_nGetIndex = i;
			return(pEvent);
		}
	}
	return(NULL);
}

EVENT* CEvents::GetFirst()
{
	if (!m_nCount)
		return(NULL);
	m_nGetIndex = 0;
	return((EVENT *)m_ptrList.GetAt(m_nGetIndex));
}

EVENT* CEvents::GetNext()
{
	if (m_nGetIndex+1 == m_nCount)
		return(NULL);
	m_nGetIndex++;
	return((EVENT *)m_ptrList.GetAt(m_nGetIndex));
}

EVENT* CEvents::GetAt(int nIndex)
{
	if (nIndex < 0 || nIndex >= m_nCount)
		return(NULL);
	return((EVENT *)m_ptrList.GetAt(nIndex));
}

void CEvents::HashIt()
{
	EVENT *pEvent;
	UINT nCurEntry;		// Current hash table entry.
	UINT nEntry;			// Entry of the item being checked.
	int i;
	int nStart;
	int nEnd;

 	m_htHash.Clear();

	nStart = HASHITEM_EMPTY;
	nEnd = HASHITEM_EMPTY;
	for (i=0;i<m_nCount;i++)
	{
		pEvent = (EVENT *)m_ptrList.GetAt(i);
		if (!pEvent)
			continue;

		nEntry = pEvent->strName.GetAt(0);

		// Will only be this value first time thru.
		if (!i)
		{
			nCurEntry = nEntry;
			nStart = i;
		}

		// If the entry we just found is not equal the to the entry we are working
		// with, need to finish off the last entry and start a new one.
		if (nEntry != nCurEntry)
		{
			m_htHash.SetAt(nCurEntry,nStart,nEnd);
			nStart = i;
			nCurEntry = nEntry;
		}

		nEnd = i;
	}

	// Last time thru loop need to add last hash entry.
	if (m_nCount)
		m_htHash.SetAt(nCurEntry,nStart,nEnd);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -