Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-21-2007, 01:40 PM
SamehSpiky SamehSpiky is offline
Newbie
 
Join Date: Jun 2007
Posts: 6
Credits: 2
Rep Power: 0
SamehSpiky is on a distinguished road
Default small help in this generator function plz

are there better way to make this function faster and more powerfull?

the GetNewClientID function generates new ids for the players or the monsters whenever they die

now, i don't want this function to get the same id as the old one for the same monster/player

Code:
// This function gets a new clientID for a player, monster
unsigned CWorldServer::GetNewClientID( )
{
	for (unsigned i=1; i<=0xffff; i++) 
    {
		if (ClientIDList[i]!=0 && time(NULL)-ClientIDList[i]>10) 
        {
			ClientIDList[i] = 0;
			return i;
		}
	}
	return 0;
}

// This function will free our clientID
void CWorldServer::ClearClientID( unsigned int id )
{
	ClientIDList[id] = (unsigned)time(NULL);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-22-2007, 01:14 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 55
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.

Last edited by v0id; 06-22-2007 at 01:16 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-22-2007, 07:36 AM
SamehSpiky SamehSpiky is offline
Newbie
 
Join Date: Jun 2007
Posts: 6
Credits: 2
Rep Power: 0
SamehSpiky is on a distinguished road
Default

thats good void, but i want the function to reuse and reallow the old id ( which was signed to the old player ) again after about 20 second here example

1. monster with id number (1285)
2. player enter the game and take id number (1876)
3. the player (1876) killed the monster (1285)
4. the id (1285) will be temporary avoid for 20 seconds
4. the new monster will take another id ( not rather than 1285), it will be (1877)
5. after 10 or 15 seconds the id (1285) will be free to taken by any other new player/monster
6. then if the new monster killed the player - the player will back to the game by the first free id, it will be now (1285)

** this delay to allow the visability function to take its time and the delay inbetween the server and the client, as the server may give id to player while this id not deleted at the client side ( this will give us some problems in the visability of the player - as some players will not see other monsters or the other player )

hope u get it now
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-22-2007, 11:47 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 55
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

If you want the old IDs to be used again, by other monsters/players, then you can implement some kind of structure to hold the already used IDs in the monster/player-class. In the CWorldServer-class you have to have an identical structure to hold the IDs there's currently used.

* Player have an ID (1001)
* Monster have an ID (1002)
** CWorldServer now knows the IDs there are used (1001, 1002)
* Monster kills Player, and the used Player ID is saved in the Player-class
** The Player ID is now deleted in CWorldServers currently used IDs (1002)
** CWorldServer gives Player a new ID, but first it checks that it isn't in the Players used-IDs (1001), so CWorldServer gives it a new ID (1003)
* Now Player have a new

Think about that one "star" (*) is something there's going on in the monster/player class and two "stars" (**) is something there's going on in the CWorldServer class. I hope you get the idea of it.

It's pretty hard, as you see, to help you exactly. Can you maybe come with an more detailed/complex example of what you're trying to do?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT a small javascript project $35! Hyde Request Services (Paid) 1 06-12-2007 07:18 AM
Best way to save lots of small values (in a database)? Vantage General Programming 2 05-14-2007 07:57 AM
need help with a small program scott0999 General Programming 5 03-01-2007 12:19 AM
In-text Ads for small sites xtraze Affiliate Marketplace 5 01-24-2007 11:30 AM
A small problem in the output The_Master C and C++ 3 12-13-2006 12:04 PM


All times are GMT -5. The time now is 12:55 AM.

Contest Stats

Xav ........ 1333.07
MeTh0Dz|Reb0rn ........ 1059.52
John ........ 887.37
morefood2001 ........ 879.43
marwex89 ........ 869.98
WingedPanther ........ 851.68
Brandon W ........ 764.23
chili5 ........ 312.39
Steve.L ........ 254.16
dcs ........ 225.25

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads