DarkflameServer/dWorldServer/ObjectIDManager.cpp

63 lines
1.7 KiB
C++
Raw Permalink Normal View History

#include "ObjectIDManager.h"
// Std
#include <random>
// Custom Classes
#include "MasterPackets.h"
#include "Database.h"
#include "dLogger.h"
#include "Game.h"
// Static Variables
2022-07-28 13:39:57 +00:00
ObjectIDManager* ObjectIDManager::m_Address = nullptr;
static std::uniform_int_distribution<int> uni(10000000, INT32_MAX);
//! Initializes the manager
void ObjectIDManager::Initialize(void) {
2022-07-28 13:39:57 +00:00
//this->currentRequestID = 0;
this->currentObjectID = uint32_t(1152921508165007067); //Initial value for this server's objectIDs
}
//! Requests a persistent ID
void ObjectIDManager::RequestPersistentID(std::function<void(uint32_t)> callback) {
2022-07-28 13:39:57 +00:00
PersistentIDRequest* request = new PersistentIDRequest();
request->requestID = ++this->currentRequestID;
request->callback = callback;
2022-07-28 13:39:57 +00:00
this->requests.push_back(request);
2022-07-28 13:39:57 +00:00
MasterPackets::SendPersistentIDRequest(Game::server, request->requestID);
}
//! Handles a persistent ID response
void ObjectIDManager::HandleRequestPersistentIDResponse(uint64_t requestID, uint32_t persistentID) {
2022-07-28 13:39:57 +00:00
for (uint32_t i = 0; i < this->requests.size(); ++i) {
if (this->requests[i]->requestID == requestID) {
// Call the callback function
this->requests[i]->callback(persistentID);
// Then delete the request
delete this->requests[i];
this->requests.erase(this->requests.begin() + i);
return;
}
}
}
//! Handles cases where we have to get a unique object ID synchronously
2022-07-28 13:39:57 +00:00
uint32_t ObjectIDManager::GenerateRandomObjectID() {
std::random_device rd;
2022-07-28 13:39:57 +00:00
std::mt19937 rng(rd());
return uni(rng);
}
//! Generates an object ID server-sided (used for regular entities like smashables)
uint32_t ObjectIDManager::GenerateObjectID(void) {
2022-07-28 13:39:57 +00:00
return ++this->currentObjectID;
}