www.pudn.com > 天堂1服务端模拟程序.rar > WorldKnown.h


/* 
Copyright (C) 2004  freeplay.dk 
 
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. 
 
You may contact me for further information on admin@freeplay.dk 
*/ 
#define WIN32_LEAN_AND_MEAN 
 
// disable map warnings for std::map 
#pragma warning (disable: 4786) 
#pragma warning (disable: 4788) 
 
#include  
#include  
#include  
#include  
#include "Types.h" 
#include "Logging.h" 
 
using namespace std; 
 
// pointer to send packet from main file 
void sendpacket(char * data, int size, DWORD ptr); 
 
struct WorldKnownObjects 
{ 
	unsigned long obj; 
	unsigned long knowobj; 
	DWORD ptr; 
	unsigned int cp; 
}; 
 
class WorldKnown 
{ 
private: 
	multimap wo; 
	struct WorldKnownObjects wko; 
	 
	void sendnewknown(DWORD ptr, unsigned long obj); 
	void deleteobjpack(DWORD ptr, unsigned int cp, unsigned long obj); 
 
public: 
	void changedoorstatus(char * data); 
 
	//************************ 
	// std::map remove all maps belong to id 
	//************************ 
	void removeMap(unsigned long keyid) 
	{ 
		wo.erase(keyid); 
	} 
	//************************ 
	// std::map setters / getters 
	//************************ 
	//** Known Object ID ** 
    void setObj(unsigned long key, unsigned long obj) 
    { 
		multimap::iterator it = wo.find(key); 
		if(it != wo.end()) 
			(*it).second.obj = obj; 
		else 
		{			  
			wko.obj = obj; 
			wo.insert(make_pair(key, wko)); 
		} 
    } 
	//** Known Object ID ** 
	unsigned long getObj(unsigned long key) 
    { 
		multimap::iterator it = wo.find(key); 
        unsigned long obj = (*it).second.obj; 
        return obj; 
    } 
	//** User Object ID ** 
    void setKnowobj(unsigned long key, unsigned long knowobj) 
    { 
		multimap::iterator it = wo.find(key); 
		if(it != wo.end()) 
			(*it).second.knowobj = knowobj; 
		else 
		{			  
			wko.knowobj = knowobj; 
			wo.insert(make_pair(key, wko)); 
		} 
    } 
	//** User Object ID ** 
	unsigned long getKnowobj(unsigned long key) 
    { 
		multimap::iterator it = wo.find(key); 
        unsigned long knowobj = (*it).second.knowobj; 
        return knowobj; 
    } 
	//** User PTR ** 
    void setKnowptr(unsigned long key, DWORD ptr) 
    { 
		multimap::iterator it = wo.find(key); 
		if(it != wo.end()) 
			(*it).second.ptr = ptr; 
		else 
		{			  
			wko.ptr = ptr; 
			wo.insert(make_pair(key, wko)); 
		} 
    } 
	//** User PTR ** 
	DWORD getKnowptr(unsigned long key) 
    { 
		multimap::iterator it = wo.find(key); 
        unsigned long ptr = (*it).second.ptr; 
        return ptr; 
    } 
	//** User CP ** 
    void setKnowcp(unsigned long key, unsigned int cp) 
    { 
		multimap::iterator it = wo.find(key); 
		if(it != wo.end()) 
			(*it).second.cp = cp; 
		else 
		{			  
			wko.cp = cp; 
			wo.insert(make_pair(key, wko)); 
		} 
    } 
	//** User CP ** 
	unsigned int getKnowcp(unsigned long key) 
    { 
		multimap::iterator it = wo.find(key); 
        unsigned long cp = (*it).second.cp; 
        return cp; 
    } 
	//**************************// 
	//* Utilities for multimap *// 
	//**************************// 
	// check if known 
	void checkknownobject(unsigned long obj, unsigned long knowobj, unsigned int cp, DWORD ptr) 
	{	 
		bool found; 
 
		if (wo.empty() == true) 
		{ 
			// have to put something in at first time run, since its empty. no need to do any checking 
			// add it as a user known object. 
			setObj(obj, obj); 
			setKnowobj(obj, knowobj); 
			setKnowcp(obj, cp); 
			setKnowptr(obj, ptr); 
			// send new known obj 
			sendnewknown(ptr, obj); 
		} 
 
		else 
		{ 
			multimap::iterator it; 
			for (it = wo.begin(); it != wo.end(); ++it) 
			{ 
				unsigned long id = (*it).first; 
				unsigned int thiscp = (*it).second.cp; 
 
				if (obj == id && cp == thiscp) 
				{ 
					// found a object, so no need to add one. 
					found = true; 
					break; 
				} 
				else 
				{	 
					// didnt find a object so we add it 
					found = false; 
				} 
			} 
			if(found == true) 
			{ 
				// object is already known, only need to send move packet. 
				//sendmovepacket(ptr, myobj, key); 
			} 
			if(found == false) 
			{ 
				// add it as a user known object. 
				setObj(obj, obj); 
				setKnowobj(obj, knowobj); 
				setKnowcp(obj, cp); 
				setKnowptr(obj, ptr); 
				// send new known obj 
				sendnewknown(ptr, obj); 
			} 
		} 
		return; 
	} 
 
	// remove known object 
	void removeknownobject(DWORD ptr, unsigned int cp, unsigned long obj) 
	{ 
		if (wo.empty() == true) 
		{ 
			// map is empty no need to remove anything 
		} 
		else 
		{ 
			for (multimap::iterator it = wo.begin(); it != wo.end(); ++it) 
			{ 
				unsigned long id = (*it).first; 
				unsigned int thiscp = (*it).second.cp; 
 
				if (obj == id && cp == thiscp) 
				{ 
					wo.erase( id ); 
					deleteobjpack(ptr, cp, obj); 
					break; 
				} 
			} 
		} 
	} 
 
	// remove known object 
	void removeknownobjectlogout(DWORD ptr) 
	{ 
		if (wo.empty() == true) 
		{ 
			// map is empty no need to remove anything 
		} 
		else 
		{ 
			for (multimap::iterator it = wo.begin(); it != wo.end(); ++it) 
			{ 
				unsigned long id = (*it).first; 
				unsigned int thiscp = (*it).second.cp; 
				DWORD thisptr = (*it).second.ptr; 
				 
				if (thisptr == ptr) 
				{ 
					wo.erase( id ); 
					break; 
				} 
			} 
			return; 
		} 
	} 
};