View Single Post
  #2 (permalink)  
Old 06-22-2007, 02:14 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,635
Last Blog:
CherryPy(thon)
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

I don't know how you exactly want it to work.

Every monster and/or player shall have an unique ID, and this unique ID shall change every time the monster and/or player dies. So when the monster and/or player dies, the member function CWorldServer::ClearClientID is called, and after that, CWorldServer::GetNewClientID. Each monster and/or player can't have the same ID that another monster and/or player has got previously.

If that's correct I would do it a little different, than you've done. First you've to make some kind of structure that holds all the IDs already used. Actually it can be done using a single integer, because we each time just get an ID there's one bigger than the last one. If we do that, we'll never get the same ID twice. In that way we gets a fast and powerful function. If a monster and/or gets the first ID, 0, then the next will get 1, and even though the monster or player with ID 0 dies, it'll get a new ID, 2. It that way we've done so not a single monster and/or gets the same ID as another one.

I've done an example, not the best, and not the most complex example in the world, but it'll maybe give you some inspiration to how it can be done.
Code:
#include <iostream>
#include <map>

class CWorldServer // Maybe some inhertiance?
{
	private:
		unsigned int CreaturesAmmount; // Ammount of creatures on the server
		unsigned int NextClientID; // The next client id
		std::map<int, int> Creatures; // Holds all the monsters and players
		
		// ...
		// Your other private functions and/or variables
	public:
		// Constructor for the class
		CWorldServer()
		{
			this->CreaturesAmmount = 0;
			this->NextClientID = 0;
		}
		
		// Make a new creature
		int NewCreature(int id)
		{
			this->Creatures[this->CreaturesAmmount] = id;
			return this->CreaturesAmmount++;
		}
		
		// This function gets a new client id for a player and/or monster
		unsigned int GetNewClientID()
		{
			return this->NextClientID++;
		}
		
		// This function will free our clientID
		void ClearClientID(int creature)
		{
			this->Creature[creature] = 0;
		}
		
		// ...
		// Your other public functions and/or variables
};

int main()
{
	CWorldServer ws;
	CPlayer v0id;
	CMonster evilv0id;
	
	// The player is born
	//  We're saving the ID of our player into the player class with SetID
	//  It can later be accessed with GetID
	v0id.SetID(ws.NewCreature(ws.GetNewClientID());
	
	// The monster is born
	//  We're saving the ID of our player into the player class with SetID
	//  It can later be accessed with GetID
	evilv0id.SetID(ws.NewCreature(ws.GetNewClientID());
	
	//////////////////////////////////////////////////////////////////////////////////////
	//// The world server instance will now hold both of the IDs to the creatures,
	//// and can be used in anyway you wants to.
	//////////////////////////////////////////////////////////////////////////////////////
	
	// The player dies
	//  Now we get the ID again so we can clear it from the world server
	ws.ClearClientID(v0id.GetID());
	
	// The monster dies
	//  Now we get the ID again so we can clear it from the world server
	ws.ClearClientID(evilv0id.GetID());
	
	return 0;
}
As you read in my first line of this post, I'm not sure if I'd understood it correctly, and if I haven't then please specify your question a bit.

The important part of the code I just posted is the CWorldServer-class. I haven't done the CPlayer and CMonster classes, because they're just for the example. It's the CWorldServer there has all the stuff about Client IDs, and CPlayer and CMonster is just to show how it maybe could work in a real-world application.
__________________
05-03-2007 - 11-13-2008

Last edited by v0id; 06-22-2007 at 02:16 AM.
Reply With Quote