www.pudn.com > uoth_src.zip > Level.cpp


//----------------------------------------------------------------------------- 
//  
// @doc 
// 
// @module	Level.cpp - Level information class | 
// 
// This module contains the level information class. 
// 
// Copyright (c) 2002 - Descartes Systems Sciences, Inc. 
// 
// 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. Neither the name of Descartes Systems Sciences, Inc nor the names of  
//    its contributors may be used to endorse or promote products derived  
//    from this software without specific prior written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  
// "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 COPYRIGHT  
// OWNER OR CONTRIBUTORS 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. 
// 
// @end 
// 
// $History: Cnf.cpp $ 
//       
//----------------------------------------------------------------------------- 
 
#include "stdafx.h" 
#include "Level.h" 
#include "DataParser.h" 
 
// 
// Debug NEW 
// 
 
#if defined (_DEBUG) 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
// 
// Class globals 
// 
 
CAtlArray  CLevel::gm_vLevels; 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc  constructor. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
CLevel::CLevel () 
{ 
	Init (true); 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc  destructor. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
CLevel::~CLevel () 
{ 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Initialize the object 
// 
// @parm bool | fConstruct | If true, we are being called from the 
//		the constructor 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CLevel::Init (bool fConstruct) 
{ 
	m_nIndex = 0; 
	m_nCartography = 0; 
	m_nLockpicking = 0; 
	m_nGold = 0; 
	m_nGems = 0; 
	m_nMagicItems = 0; 
	m_nScrolls = 0; 
	m_strName .Empty (); 
	m_strWhatSpawns .Empty (); 
	m_strWhatDrops .Empty (); 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Begin the processing of a level 
// 
// @parm CDataParser * | pParser | Current parser 
// 
// @parm const XML_Char ** | papszAttrs | Attributes 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CLevel::OnStart (CDataParser *pParser, const XML_Char **papszAttrs) 
{ 
 
	// 
	// The only allowed attribute is Index and 
	// it is required 
	// 
 
	if (papszAttrs [0] == NULL || 
		strcmp (papszAttrs [0], "Index") != 0 || 
		papszAttrs [2] != NULL) 
	{ 
		pParser ->SetError (IDS_ERR_XML_ONLY_INDEX); 
		return; 
	} 
 
	// 
	// Get the index number 
	//  
 
	int nIndex = atol (papszAttrs [1]); 
 
	// 
	// Validate the level range 
	// 
 
	if (nIndex <= 0 || nIndex > Max_Level) 
	{ 
		pParser ->SetError (IDS_ERR_XML_INDEX_RANGE); 
		return; 
	} 
 
	// 
	// Initialize the level 
	// 
 
	Init (false); 
	m_nIndex = nIndex; 
 
	// 
	// Move to the new state 
	// 
 
	pParser ->SetState (CDataParser::InLevel); 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Begin the processing of an XML element 
// 
// @parm CDataParser * | pParser | Current parser 
// 
// @parm const XML_Char * | pszName | Name of the element 
// 
// @parm const XML_Char ** | papszAttrs | Attributes 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CLevel::OnStartElement (CDataParser *pParser,  
	const XML_Char *pszName, const XML_Char **papszAttrs) 
{ 
	// 
	// Get the element 
	// 
 
	_Element nElement = GetElement (pszName); 
	if (nElement == Unknown) 
	{ 
		pParser ->SetError (IDS_ERR_XML_UNEXPECTED_ELEMENT); 
		return; 
	} 
 
	// 
	// No attributes are allowed 
	// 
 
	if (papszAttrs [0] != NULL) 
	{ 
		pParser ->SetError (IDS_ERR_XML_NO_ATTRS); 
		return; 
	} 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc End an XML element 
// 
// @parm CDataParser * | pParser | Current parser 
// 
// @parm const XML_Char * | pszName | Name of the element 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CLevel::OnEndElement (CDataParser *pParser, const XML_Char *pszName) 
{ 
 
	// 
	// Get the element 
	// 
 
	_Element nElement = GetElement (pszName); 
	if (nElement == Unknown) 
	{ 
		pParser ->SetError (IDS_ERR_XML_UNEXPECTED_ELEMENT); 
		return; 
	} 
 
	// 
	// Switch based on the element 
	// 
 
	switch (nElement) 
	{ 
		 
		// 
		// If we are ending the level 
		// 
 
		case Level: 
			pParser ->SetState (CDataParser::InData); 
			gm_vLevels .SetAtGrow (m_nIndex, *this); 
			break; 
 
		// 
		// If this is the name 
		// 
 
		case Name: 
			m_strName = pParser ->GetText (false); 
			break; 
 
		// 
		// If this is what spawns 
		// 
 
		case WhatSpawns: 
			m_strWhatSpawns = pParser ->GetText (true); 
			break; 
 
		// 
		// If this is what drops 
		// 
 
		case WhatDrops: 
			m_strWhatDrops = pParser ->GetText (true); 
			break; 
 
		// 
		// If this is cartography 
		// 
 
		case Cartography: 
			m_nCartography = atol (pParser ->GetText (false)); 
			break; 
 
		// 
		// If this is Lockpicking 
		// 
 
		case Lockpicking: 
			m_nLockpicking = atol (pParser ->GetText (false)); 
			break; 
 
		// 
		// If this is gold 
		// 
 
		case Gold: 
			m_nGold = atol (pParser ->GetText (false)); 
			break; 
 
		// 
		// If this is gems 
		// 
 
		case Gems: 
			m_nGems = atol (pParser ->GetText (false)); 
			break; 
 
		// 
		// If this is magic items 
		// 
 
		case MagicItems: 
			m_nMagicItems = atol (pParser ->GetText (false)); 
			break; 
 
		// 
		// If this is scrolls 
		// 
 
		case Scrolls: 
			m_nScrolls = atol (pParser ->GetText (false)); 
			break; 
 
		// 
		// If this is reagents 
		// 
 
		case Reagents: 
			m_nReagents = atol (pParser ->GetText (false)); 
			break; 
	} 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Get the element number 
// 
// @parm const XML_Char * | pszName | Element name 
// 
// @rdesc Element id. 
// 
//----------------------------------------------------------------------------- 
 
CLevel::_Element CLevel::GetElement (const XML_Char *pszName) 
{ 
	if (stricmp (pszName, "Level") == 0) 
		return Level; 
	else if (stricmp (pszName, "Cartography") == 0) 
		return Cartography; 
	else if (stricmp (pszName, "Lockpicking") == 0) 
		return Lockpicking; 
	else if (stricmp (pszName, "Gold") == 0) 
		return Gold; 
	else if (stricmp (pszName, "Gems") == 0) 
		return Gems; 
	else if (stricmp (pszName, "MagicItems") == 0) 
		return MagicItems; 
	else if (stricmp (pszName, "Scrolls") == 0) 
		return Scrolls; 
	else if (stricmp (pszName, "Reagents") == 0) 
		return Reagents; 
	else if (stricmp (pszName, "WhatSpawns") == 0) 
		return WhatSpawns; 
	else if (stricmp (pszName, "WhatDrops") == 0) 
		return WhatDrops; 
	else if (stricmp (pszName, "Name") == 0) 
		return Name; 
	else 
		return Unknown; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Get the level information 
// 
// @parm bool | fTerse | If true, only provide limited data 
// 
// @rdesc String containing the text 
// 
//----------------------------------------------------------------------------- 
 
CString CLevel::GetInformation (bool fTerse) 
{ 
	CString str; 
 
	// 
	// Format the whole string 
	// 
 
	str .Format (IDS_INF_LEVEL, m_nIndex, (LPCTSTR) m_strName, m_nCartography, 
		m_nLockpicking, m_nGold, m_nGems, m_nMagicItems, m_nScrolls, m_nReagents); 
	if (!fTerse) 
	{ 
		str += CString (MAKEINTRESOURCE (IDS_INF_LEVEL_WHATSPAWNS)) +  
			m_strWhatSpawns + _T ( "\r\n"); 
		if (!m_strWhatSpawns .IsEmpty ()) 
			str += _T ( "\r\n"); 
		str += CString (MAKEINTRESOURCE (IDS_INF_LEVEL_WHATDROPS)) +  
			m_strWhatDrops + _T ( "\r\n"); 
	} 
	return str; 
}