|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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);
}
|
| Sponsored Links |
|
|
|
|||||
|
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;
}
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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... Last edited by v0id; 06-22-2007 at 01:16 AM. |
|
|||
|
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 |
|
|||||
|
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?
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| 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 |
Goal: 100,000 Posts
Complete: 82%