mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-05 06:50:04 +00:00
Use direct members instead of pointers for viewableChunks and buyback
We had avoided putting STL containers into Players back when we thought Players was still POD and needed to remain POD, but it turned out that neither were the case all along, so there's no need for the indirection.
This commit is contained in:
parent
8afe175bd1
commit
7f9cdfc9ae
@ -26,9 +26,9 @@ static void newChunk(ChunkPos pos) {
|
|||||||
std::set<Chunk*> surroundings = getViewableChunks(pos);
|
std::set<Chunk*> surroundings = getViewableChunks(pos);
|
||||||
for (Chunk* c : surroundings) {
|
for (Chunk* c : surroundings) {
|
||||||
for (CNSocket* sock : c->players)
|
for (CNSocket* sock : c->players)
|
||||||
PlayerManager::getPlayer(sock)->viewableChunks->insert(chunk);
|
PlayerManager::getPlayer(sock)->viewableChunks.insert(chunk);
|
||||||
for (int32_t id : c->NPCs)
|
for (int32_t id : c->NPCs)
|
||||||
NPCManager::NPCs[id]->viewableChunks->insert(chunk);
|
NPCManager::NPCs[id]->viewableChunks.insert(chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,9 +45,9 @@ static void deleteChunk(ChunkPos pos) {
|
|||||||
for(Chunk* c : surroundings)
|
for(Chunk* c : surroundings)
|
||||||
{
|
{
|
||||||
for (CNSocket* sock : c->players)
|
for (CNSocket* sock : c->players)
|
||||||
PlayerManager::getPlayer(sock)->viewableChunks->erase(chunk);
|
PlayerManager::getPlayer(sock)->viewableChunks.erase(chunk);
|
||||||
for (int32_t id : c->NPCs)
|
for (int32_t id : c->NPCs)
|
||||||
NPCManager::NPCs[id]->viewableChunks->erase(chunk);
|
NPCManager::NPCs[id]->viewableChunks.erase(chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
chunks.erase(pos); // remove from map
|
chunks.erase(pos); // remove from map
|
||||||
@ -353,8 +353,8 @@ void Chunking::updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to) {
|
|||||||
|
|
||||||
plr->chunkPos = to; // update cached chunk position
|
plr->chunkPos = to; // update cached chunk position
|
||||||
// updated cached viewable chunks
|
// updated cached viewable chunks
|
||||||
plr->viewableChunks->clear();
|
plr->viewableChunks.clear();
|
||||||
plr->viewableChunks->insert(newViewables.begin(), newViewables.end());
|
plr->viewableChunks.insert(newViewables.begin(), newViewables.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunking::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) {
|
void Chunking::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) {
|
||||||
@ -389,8 +389,8 @@ void Chunking::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) {
|
|||||||
|
|
||||||
npc->chunkPos = to; // update cached chunk position
|
npc->chunkPos = to; // update cached chunk position
|
||||||
// updated cached viewable chunks
|
// updated cached viewable chunks
|
||||||
npc->viewableChunks->clear();
|
npc->viewableChunks.clear();
|
||||||
npc->viewableChunks->insert(newViewables.begin(), newViewables.end());
|
npc->viewableChunks.insert(newViewables.begin(), newViewables.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Chunking::chunkExists(ChunkPos chunk) {
|
bool Chunking::chunkExists(ChunkPos chunk) {
|
||||||
|
@ -249,7 +249,7 @@ static void summonWCommand(std::string full, std::vector<std::string>& args, CNS
|
|||||||
static void unsummonWCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
|
static void unsummonWCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
|
||||||
Player* plr = PlayerManager::getPlayer(sock);
|
Player* plr = PlayerManager::getPlayer(sock);
|
||||||
|
|
||||||
BaseNPC* npc = NPCManager::getNearestNPC(plr->viewableChunks, plr->x, plr->y, plr->z);
|
BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z);
|
||||||
|
|
||||||
if (npc == nullptr) {
|
if (npc == nullptr) {
|
||||||
Chat::sendServerMessage(sock, "/unsummonW: No NPCs found nearby");
|
Chat::sendServerMessage(sock, "/unsummonW: No NPCs found nearby");
|
||||||
@ -331,7 +331,7 @@ static void toggleAiCommand(std::string full, std::vector<std::string>& args, CN
|
|||||||
static void npcRotateCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
|
static void npcRotateCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
|
||||||
Player* plr = PlayerManager::getPlayer(sock);
|
Player* plr = PlayerManager::getPlayer(sock);
|
||||||
|
|
||||||
BaseNPC* npc = NPCManager::getNearestNPC(plr->viewableChunks, plr->x, plr->y, plr->z);
|
BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z);
|
||||||
|
|
||||||
if (npc == nullptr) {
|
if (npc == nullptr) {
|
||||||
Chat::sendServerMessage(sock, "[NPCR] No NPCs found nearby");
|
Chat::sendServerMessage(sock, "[NPCR] No NPCs found nearby");
|
||||||
@ -413,7 +413,7 @@ static void npcInstanceCommand(std::string full, std::vector<std::string>& args,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseNPC* npc = NPCManager::getNearestNPC(plr->viewableChunks, plr->x, plr->y, plr->z);
|
BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z);
|
||||||
|
|
||||||
if (npc == nullptr) {
|
if (npc == nullptr) {
|
||||||
Chat::sendServerMessage(sock, "[NPCI] No NPCs found nearby");
|
Chat::sendServerMessage(sock, "[NPCI] No NPCs found nearby");
|
||||||
@ -650,7 +650,7 @@ static void flushCommand(std::string full, std::vector<std::string>& args, CNSoc
|
|||||||
|
|
||||||
static void whoisCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
|
static void whoisCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
|
||||||
Player* plr = PlayerManager::getPlayer(sock);
|
Player* plr = PlayerManager::getPlayer(sock);
|
||||||
BaseNPC* npc = NPCManager::getNearestNPC(plr->viewableChunks, plr->x, plr->y, plr->z);
|
BaseNPC* npc = NPCManager::getNearestNPC(&plr->viewableChunks, plr->x, plr->y, plr->z);
|
||||||
|
|
||||||
if (npc == nullptr) {
|
if (npc == nullptr) {
|
||||||
Chat::sendServerMessage(sock, "[WHOIS] No NPCs found nearby");
|
Chat::sendServerMessage(sock, "[WHOIS] No NPCs found nearby");
|
||||||
|
@ -125,7 +125,7 @@ static void eggStep(CNServer* serv, time_t currTime) {
|
|||||||
|
|
||||||
// check dead eggs and eggs in inactive chunks
|
// check dead eggs and eggs in inactive chunks
|
||||||
for (auto egg : Eggs::Eggs) {
|
for (auto egg : Eggs::Eggs) {
|
||||||
if (!egg.second->dead || !Chunking::inPopulatedChunks(egg.second->viewableChunks))
|
if (!egg.second->dead || !Chunking::inPopulatedChunks(&egg.second->viewableChunks))
|
||||||
continue;
|
continue;
|
||||||
if (egg.second->deadUntil <= timeStamp) {
|
if (egg.second->deadUntil <= timeStamp) {
|
||||||
// respawn it
|
// respawn it
|
||||||
|
@ -119,7 +119,7 @@ bool MobAI::aggroCheck(Mob *mob, time_t currTime) {
|
|||||||
CNSocket *closest = nullptr;
|
CNSocket *closest = nullptr;
|
||||||
int closestDistance = INT_MAX;
|
int closestDistance = INT_MAX;
|
||||||
|
|
||||||
for (auto it = mob->viewableChunks->begin(); it != mob->viewableChunks->end(); it++) {
|
for (auto it = mob->viewableChunks.begin(); it != mob->viewableChunks.end(); it++) {
|
||||||
Chunk* chunk = *it;
|
Chunk* chunk = *it;
|
||||||
for (CNSocket *s : chunk->players) {
|
for (CNSocket *s : chunk->players) {
|
||||||
Player *plr = PlayerManager::getPlayer(s);
|
Player *plr = PlayerManager::getPlayer(s);
|
||||||
@ -296,7 +296,7 @@ static void useAbilities(Mob *mob, time_t currTime) {
|
|||||||
std::vector<int> targetData = {0, 0, 0, 0, 0};
|
std::vector<int> targetData = {0, 0, 0, 0, 0};
|
||||||
|
|
||||||
// find the players within range of eruption
|
// find the players within range of eruption
|
||||||
for (auto it = mob->viewableChunks->begin(); it != mob->viewableChunks->end(); it++) {
|
for (auto it = mob->viewableChunks.begin(); it != mob->viewableChunks.end(); it++) {
|
||||||
Chunk* chunk = *it;
|
Chunk* chunk = *it;
|
||||||
for (CNSocket *s : chunk->players) {
|
for (CNSocket *s : chunk->players) {
|
||||||
Player *plr = PlayerManager::getPlayer(s);
|
Player *plr = PlayerManager::getPlayer(s);
|
||||||
|
@ -8,7 +8,7 @@ public:
|
|||||||
NPCClass npcClass;
|
NPCClass npcClass;
|
||||||
uint64_t instanceID;
|
uint64_t instanceID;
|
||||||
ChunkPos chunkPos;
|
ChunkPos chunkPos;
|
||||||
std::set<Chunk*>* viewableChunks;
|
std::set<Chunk*> viewableChunks;
|
||||||
|
|
||||||
int playersInView;
|
int playersInView;
|
||||||
|
|
||||||
@ -29,7 +29,6 @@ public:
|
|||||||
instanceID = iID;
|
instanceID = iID;
|
||||||
|
|
||||||
chunkPos = std::make_tuple(0, 0, 0);
|
chunkPos = std::make_tuple(0, 0, 0);
|
||||||
viewableChunks = new std::set<Chunk*>();
|
|
||||||
playersInView = 0;
|
playersInView = 0;
|
||||||
};
|
};
|
||||||
BaseNPC(int x, int y, int z, int angle, uint64_t iID, int type, int id, NPCClass classType) : BaseNPC(x, y, z, angle, iID, type, id) {
|
BaseNPC(int x, int y, int z, int angle, uint64_t iID, int type, int id, NPCClass classType) : BaseNPC(x, y, z, angle, iID, type, id) {
|
||||||
|
@ -66,7 +66,6 @@ void NPCManager::destroyNPC(int32_t id) {
|
|||||||
Eggs::Eggs.erase(id);
|
Eggs::Eggs.erase(id);
|
||||||
|
|
||||||
// finally, remove it from the map and free it
|
// finally, remove it from the map and free it
|
||||||
delete entity->viewableChunks;
|
|
||||||
NPCs.erase(id);
|
NPCs.erase(id);
|
||||||
delete entity;
|
delete entity;
|
||||||
}
|
}
|
||||||
@ -86,7 +85,7 @@ void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z, uint64_t I,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void NPCManager::sendToViewable(BaseNPC *npc, void *buf, uint32_t type, size_t size) {
|
void NPCManager::sendToViewable(BaseNPC *npc, void *buf, uint32_t type, size_t size) {
|
||||||
for (auto it = npc->viewableChunks->begin(); it != npc->viewableChunks->end(); it++) {
|
for (auto it = npc->viewableChunks.begin(); it != npc->viewableChunks.end(); it++) {
|
||||||
Chunk* chunk = *it;
|
Chunk* chunk = *it;
|
||||||
for (CNSocket *s : chunk->players) {
|
for (CNSocket *s : chunk->players) {
|
||||||
s->sendPacket(buf, type, size);
|
s->sendPacket(buf, type, size);
|
||||||
|
@ -83,10 +83,10 @@ struct Player {
|
|||||||
uint64_t iFirstUseFlag[2];
|
uint64_t iFirstUseFlag[2];
|
||||||
|
|
||||||
ChunkPos chunkPos;
|
ChunkPos chunkPos;
|
||||||
std::set<Chunk*> *viewableChunks;
|
std::set<Chunk*> viewableChunks;
|
||||||
time_t lastHeartbeat;
|
time_t lastHeartbeat;
|
||||||
|
|
||||||
int suspicionRating;
|
int suspicionRating;
|
||||||
time_t lastShot;
|
time_t lastShot;
|
||||||
std::vector<sItemBase> *buyback;
|
std::vector<sItemBase> buyback;
|
||||||
};
|
};
|
||||||
|
@ -35,9 +35,7 @@ static void addPlayer(CNSocket* key, Player plr) {
|
|||||||
|
|
||||||
players[key] = p;
|
players[key] = p;
|
||||||
p->chunkPos = std::make_tuple(0, 0, 0);
|
p->chunkPos = std::make_tuple(0, 0, 0);
|
||||||
p->viewableChunks = new std::set<Chunk*>();
|
|
||||||
p->lastHeartbeat = 0;
|
p->lastHeartbeat = 0;
|
||||||
p->buyback = new std::vector<sItemBase>();
|
|
||||||
|
|
||||||
std::cout << getPlayerName(p) << " has joined!" << std::endl;
|
std::cout << getPlayerName(p) << " has joined!" << std::endl;
|
||||||
std::cout << players.size() << " players" << std::endl;
|
std::cout << players.size() << " players" << std::endl;
|
||||||
@ -64,8 +62,6 @@ void PlayerManager::removePlayer(CNSocket* key) {
|
|||||||
|
|
||||||
std::cout << getPlayerName(plr) << " has left!" << std::endl;
|
std::cout << getPlayerName(plr) << " has left!" << std::endl;
|
||||||
|
|
||||||
delete plr->buyback;
|
|
||||||
delete plr->viewableChunks;
|
|
||||||
delete plr;
|
delete plr;
|
||||||
players.erase(key);
|
players.erase(key);
|
||||||
|
|
||||||
@ -331,7 +327,7 @@ static void enterPlayer(CNSocket* sock, CNPacketData* data) {
|
|||||||
|
|
||||||
void PlayerManager::sendToViewable(CNSocket* sock, void* buf, uint32_t type, size_t size) {
|
void PlayerManager::sendToViewable(CNSocket* sock, void* buf, uint32_t type, size_t size) {
|
||||||
Player* plr = getPlayer(sock);
|
Player* plr = getPlayer(sock);
|
||||||
for (auto it = plr->viewableChunks->begin(); it != plr->viewableChunks->end(); it++) {
|
for (auto it = plr->viewableChunks.begin(); it != plr->viewableChunks.end(); it++) {
|
||||||
Chunk* chunk = *it;
|
Chunk* chunk = *it;
|
||||||
for (CNSocket* otherSock : chunk->players) {
|
for (CNSocket* otherSock : chunk->players) {
|
||||||
if (otherSock == sock)
|
if (otherSock == sock)
|
||||||
|
@ -38,7 +38,7 @@ namespace PlayerManager {
|
|||||||
template<class T>
|
template<class T>
|
||||||
void sendToViewable(CNSocket *sock, T& pkt, uint32_t type) {
|
void sendToViewable(CNSocket *sock, T& pkt, uint32_t type) {
|
||||||
Player* plr = getPlayer(sock);
|
Player* plr = getPlayer(sock);
|
||||||
for (auto it = plr->viewableChunks->begin(); it != plr->viewableChunks->end(); it++) {
|
for (auto it = plr->viewableChunks.begin(); it != plr->viewableChunks.end(); it++) {
|
||||||
Chunk* chunk = *it;
|
Chunk* chunk = *it;
|
||||||
for (CNSocket* otherSock : chunk->players) {
|
for (CNSocket* otherSock : chunk->players) {
|
||||||
if (otherSock == sock)
|
if (otherSock == sock)
|
||||||
|
@ -104,11 +104,11 @@ static void vendorSell(CNSocket* sock, CNPacketData* data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add to buyback list
|
// add to buyback list
|
||||||
plr->buyback->push_back(original);
|
plr->buyback.push_back(original);
|
||||||
// forget oldest member if there's more than 5
|
// forget oldest member if there's more than 5
|
||||||
if (plr->buyback->size() > 5)
|
if (plr->buyback.size() > 5)
|
||||||
plr->buyback->erase(plr->buyback->begin());
|
plr->buyback.erase(plr->buyback.begin());
|
||||||
//std::cout << (int)plr->buyback->size() << " items in buyback\n";
|
//std::cout << (int)plr->buyback.size() << " items in buyback\n";
|
||||||
|
|
||||||
// response parameters
|
// response parameters
|
||||||
resp.iInvenSlotNum = req->iInvenSlotNum;
|
resp.iInvenSlotNum = req->iInvenSlotNum;
|
||||||
@ -133,13 +133,13 @@ static void vendorBuyback(CNSocket* sock, CNPacketData* data) {
|
|||||||
int idx = req->iListID - 1;
|
int idx = req->iListID - 1;
|
||||||
|
|
||||||
// sanity check
|
// sanity check
|
||||||
if (idx < 0 || idx >= plr->buyback->size()) {
|
if (idx < 0 || idx >= plr->buyback.size()) {
|
||||||
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL));
|
sock->sendPacket((void*)&failResp, P_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL, sizeof(sP_FE2CL_REP_PC_VENDOR_ITEM_RESTORE_BUY_FAIL));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the item out of the buyback list
|
// get the item out of the buyback list
|
||||||
sItemBase item = (*plr->buyback)[idx];
|
sItemBase item = plr->buyback[idx];
|
||||||
/*
|
/*
|
||||||
* NOTE: The client sends the index of the exact item the user clicked on.
|
* NOTE: The client sends the index of the exact item the user clicked on.
|
||||||
* We then operate on that item, but we remove the *first* identical item
|
* We then operate on that item, but we remove the *first* identical item
|
||||||
@ -149,18 +149,18 @@ static void vendorBuyback(CNSocket* sock, CNPacketData* data) {
|
|||||||
* does the exact same thing, so this *is* the correct thing to do to keep
|
* does the exact same thing, so this *is* the correct thing to do to keep
|
||||||
* them in sync.
|
* them in sync.
|
||||||
*/
|
*/
|
||||||
for (auto it = plr->buyback->begin(); it != plr->buyback->end(); it++) {
|
for (auto it = plr->buyback.begin(); it != plr->buyback.end(); it++) {
|
||||||
/*
|
/*
|
||||||
* XXX: we really need a standard item comparison function that
|
* XXX: we really need a standard item comparison function that
|
||||||
* will work properly across all builds (ex. with iSerial)
|
* will work properly across all builds (ex. with iSerial)
|
||||||
*/
|
*/
|
||||||
if (it->iType == item.iType && it->iID == item.iID && it->iOpt == item.iOpt
|
if (it->iType == item.iType && it->iID == item.iID && it->iOpt == item.iOpt
|
||||||
&& it->iTimeLimit == item.iTimeLimit) {
|
&& it->iTimeLimit == item.iTimeLimit) {
|
||||||
plr->buyback->erase(it);
|
plr->buyback.erase(it);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//std::cout << (int)plr->buyback->size() << " items in buyback\n";
|
//std::cout << (int)plr->buyback.size() << " items in buyback\n";
|
||||||
|
|
||||||
Items::Item* itemDat = Items::getItemData(item.iID, item.iType);
|
Items::Item* itemDat = Items::getItemData(item.iID, item.iType);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user