View Single Post
Old 01-24-2008, 01:56 PM   #1
ensiform
The Stig
 
ensiform's Avatar
 
Join Date: Nov 2004
Location: Sawtooth Cauldron
Posts: 1,241
Current Game: Borderlands 2
TAG Sys Rewrite (untested, ugly)

Code:
extern "C"
{
	#include "g_local.h"
};

#include <string>
#include <vector>

#include <boost/shared_ptr.hpp>

typedef enum eTagType
{
	TAG_NONE,
	TAG_NAVGOAL
} TagType;

#define TAG_WORLD "__WORLD__"

class TagManager
{
public:
	class Tag
	{
	public:
		const std::string &GetName(void) const { return name; }
		const std::string &GetOwner(void) const { return owner; }
		const char *GetNameStr(void) const { return name.c_str(); }
		const char *GetOwnerStr(void) const { return owner.c_str(); }
		void GetOrigin(vec3_t _origin) const { VectorCopy(origin, _origin); }
		void GetAngles(vec3_t _angles) const { VectorCopy(angles, _angles); }
		const int GetRadius(void) const { return radius; }
		const TagType GetTagType(void) const { return type; }

		inline Tag(const char *_name, const char *_owner, const vec3_t _origin, const vec3_t _angles, const int _radius, const TagType _type)
		{
			name = _name;
			std::transform(name.begin(), name.end(), name.begin(), tolower);
			owner = _owner;
			std::transform(owner.begin(), owner.end(), owner.begin(), tolower);
			VectorCopy(_origin, origin);
			VectorCopy(_angles, angles);
			radius = _radius;
			type = _type;
		}
		inline void Set(const char *_name, const char *_owner, const vec3_t _origin, const vec3_t _angles, const int _radius, const TagType _type)
		{
			name = _name;
			std::transform(name.begin(), name.end(), name.begin(), tolower);
			owner = _owner;
			std::transform(owner.begin(), owner.end(), owner.begin(), tolower);
			VectorCopy(_origin, origin);
			VectorCopy(_angles, angles);
			radius = _radius;
			type = _type;
		}
		~Tag() {};

	private:
		std::string	name;
		std::string owner;
		vec3_t		origin;
		vec3_t		angles;
		int			radius;
		TagType		type;
	};

	typedef boost::shared_ptr<Tag> TagPtr;
	typedef std::vector<TagPtr> TagList;
	TagList	m_TagList;

	bool AddTag(const TagPtr & m_Tag);
	TagPtr GetTagByName(const char *_name);
	TagPtr GetTagByOwner(const char *_owner);
	TagPtr GetTag(const char *_owner, const char *_name);

	const TagList &GetTagList(void) const { return m_TagList; }

	static TagManager *GetInstance(void);
	static void DeleteInstance(void);
protected:
	static TagManager *m_Instance;
	~TagManager();
};

TagManager *TagManager::m_Instance = NULL;

TagManager::~TagManager()
{
	m_TagList.clear();
}

TagManager *TagManager::GetInstance(void)
{
	if(!m_Instance)
		m_Instance = new TagManager;
	return m_Instance;
}

void TagManager::DeleteInstance()
{
	if(m_Instance) {
		delete m_Instance;
		m_Instance = NULL;
	}
}

bool TagManager::AddTag(const TagPtr & m_Tag)
{
	if(std::find(m_TagList.begin(), m_TagList.end(), m_Tag) == m_TagList.end())
	{
		m_TagList.push_back(m_Tag);
		return true;
	}
	return false;
}

TagManager::TagPtr TagManager::GetTagByName(const char *_name)
{
	TagPtr ptr;

	TagList::iterator it = m_TagList.begin(), itEnd = m_TagList.end();
	while(it != itEnd)
	{
		if(!Q_stricmp((*it)->GetName().c_str(), _name))
		{
			ptr = (*it);
			break;
		}
		++it;
	}

	return ptr;
}

TagManager::TagPtr TagManager::GetTagByOwner(const char *_owner)
{
	TagPtr ptr;

	TagList::iterator it = m_TagList.begin(), itEnd = m_TagList.end();
	while(it != itEnd)
	{
		if(!_owner || !_owner[0])
		{
			if(!Q_stricmp((*it)->GetOwner().c_str(), TAG_WORLD))
			{
				ptr = (*it);
				break;
			}
		}
		else
		{
			if(!Q_stricmp((*it)->GetOwner().c_str(), _owner))
			{
				ptr = (*it);
				break;
			}
		}
		++it;
	}

	return ptr;
}

TagManager::TagPtr TagManager::GetTag(const char *_owner, const char *_name)
{
	TagPtr ptr;

	TagList::iterator it = m_TagList.begin(), itEnd = m_TagList.end();
	while(it != itEnd)
	{
		if(!_owner || !_owner[0])
		{
			if(!Q_stricmp((*it)->GetName().c_str(), _name) && !Q_stricmp((*it)->GetOwner().c_str(), TAG_WORLD))
			{
				ptr = (*it);
				break;
			}
		}
		else
		{
			if(!Q_stricmp((*it)->GetName().c_str(), _name) && !Q_stricmp((*it)->GetOwner().c_str(), _owner))
			{
				ptr = (*it);
				break;
			}
		}
		++it;
	}

	return ptr;
}

extern "C"
{
	void TagManager_Init(void)
	{
		TagManager::GetInstance();
	}

	void TagManager_Shutdown(void)
	{
		TagManager::DeleteInstance();
	}

	void TagManager_AddTag(const char *name, const char *owner, vec3_t origin, vec3_t angles, int radius, TagType type)
	{
		if (!name || !name[0])
		{
			G_Printf(S_COLOR_RED "ERROR: Nameless ref_tag found at (%.2f %.2f %.2f)\n", origin[0], origin[1], origin[2]);
			return;
		}

		if(TagManager::GetInstance()->GetTag(owner, name))
		{
			G_Printf(S_COLOR_RED "Duplicate tag name \"%s\"\n", name);
			return;
		}

		TagManager::TagPtr tag;
		if(!owner || !owner[0])
			tag->Set(name, TAG_WORLD, origin, angles, radius, type);
		else
			tag->Set(name, owner, origin, angles, radius, type);

		if(!tag)
		{
			G_Printf(S_COLOR_RED "Failed to add data to tag \"%s\"\n", name);
			return;
		}

		if(!TagManager::GetInstance()->AddTag(tag))
			G_Printf(S_COLOR_RED "Failed to add tag \"%s\" to global list\n", name);
	}

	int TagManager_Tag_GetOrigin( const char *owner, const char *name, vec3_t origin )
	{
		TagManager::TagPtr tag = TagManager::GetInstance()->GetTag(owner, name);
		if(!tag)
		{
			VectorClear(origin);
			return 0;
		}
		tag->GetOrigin(origin);
		return 1;
	}

	int TagManager_Tag_GetOrigin2( const char *owner, const char *name, vec3_t origin )
	{
		TagManager::TagPtr tag = TagManager::GetInstance()->GetTag(owner, name);
		if(!tag)
		{
			return 0;
		}
		tag->GetOrigin(origin);
		return 1;
	}

	int TagManager_Tag_GetAngles( const char *owner, const char *name, vec3_t angles )
	{
		TagManager::TagPtr tag = TagManager::GetInstance()->GetTag(owner, name);
		if(!tag)
		{
			return 0;
		}
		tag->GetAngles(angles);
		return 1;
	}

	int TagManager_Tag_GetRadius( const char *owner, const char *name )
	{
		TagManager::TagPtr tag = TagManager::GetInstance()->GetTag(owner, name);
		if(!tag)
		{
			return 0;
		}
		return tag->GetRadius();
	}

	TagType TagManager_Tag_GetType( const char *owner, const char *name )
	{
		TagManager::TagPtr tag = TagManager::GetInstance()->GetTag(owner, name);
		if(!tag)
		{
			return TAG_NONE;
		}
		return tag->GetTagType();
	}
};


iojamp project lead / coder

Last edited by ensiform; 01-26-2008 at 01:21 AM.
ensiform is offline   you may: quote & reply,