[refactor] Mark all internal functions static

All packet handlers and helper functions that are only used in the
source file they're declared in have been taken out of the namespaces in
the corresponding header files, have been marked static, and have been
reordered to avoid the need for declarations at the top of each source
file.

Each source file now contains a "using namespace" directive so that the
static functions don't need to prefix the source file's symbols with
their namespace. All redundant namespace prefixes found have been
removed.

An unused nano power resetting function in NanoManager has been removed.
This commit is contained in:
2021-03-16 22:06:10 +01:00
parent 04c56ce426
commit cee09f6344
36 changed files with 1808 additions and 1957 deletions

View File

@@ -4,11 +4,11 @@
#include "settings.hpp"
#include "Combat.hpp"
using namespace ChunkManager;
std::map<ChunkPos, Chunk*> ChunkManager::chunks;
void ChunkManager::init() {} // stubbed
void ChunkManager::newChunk(ChunkPos pos) {
static void newChunk(ChunkPos pos) {
if (chunkExists(pos)) {
std::cout << "[WARN] Tried to create a chunk that already exists\n";
return;
@@ -31,7 +31,7 @@ void ChunkManager::newChunk(ChunkPos pos) {
}
}
void ChunkManager::deleteChunk(ChunkPos pos) {
static void deleteChunk(ChunkPos pos) {
if (!chunkExists(pos)) {
std::cout << "[WARN] Tried to delete a chunk that doesn't exist\n";
return;
@@ -53,78 +53,6 @@ void ChunkManager::deleteChunk(ChunkPos pos) {
delete chunk; // free from memory
}
void ChunkManager::updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to) {
Player* plr = PlayerManager::getPlayer(sock);
// if the new chunk doesn't exist, make it first
if (!ChunkManager::chunkExists(to))
newChunk(to);
// move to other chunk's player set
untrackPlayer(from, sock); // this will delete the chunk if it's empty
trackPlayer(to, sock);
// calculate viewable chunks from both points
std::set<Chunk*> oldViewables = getViewableChunks(from);
std::set<Chunk*> newViewables = getViewableChunks(to);
std::set<Chunk*> toExit, toEnter;
/*
* Calculate diffs. This is done to prevent phasing on chunk borders.
* toExit will contain old viewables - new viewables, so the player will only be exited in chunks that are out of sight.
* toEnter contains the opposite: new viewables - old viewables, chunks where we previously weren't visible from before.
*/
std::set_difference(oldViewables.begin(), oldViewables.end(), newViewables.begin(), newViewables.end(),
std::inserter(toExit, toExit.end())); // chunks we must be EXITed from (old - new)
std::set_difference(newViewables.begin(), newViewables.end(), oldViewables.begin(), oldViewables.end(),
std::inserter(toEnter, toEnter.end())); // chunks we must be ENTERed into (new - old)
// update views
removePlayerFromChunks(toExit, sock);
addPlayerToChunks(toEnter, sock);
plr->chunkPos = to; // update cached chunk position
// updated cached viewable chunks
plr->viewableChunks->clear();
plr->viewableChunks->insert(newViewables.begin(), newViewables.end());
}
void ChunkManager::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) {
BaseNPC* npc = NPCManager::NPCs[id];
// if the new chunk doesn't exist, make it first
if (!ChunkManager::chunkExists(to))
newChunk(to);
// move to other chunk's player set
untrackNPC(from, id); // this will delete the chunk if it's empty
trackNPC(to, id);
// calculate viewable chunks from both points
std::set<Chunk*> oldViewables = getViewableChunks(from);
std::set<Chunk*> newViewables = getViewableChunks(to);
std::set<Chunk*> toExit, toEnter;
/*
* Calculate diffs. This is done to prevent phasing on chunk borders.
* toExit will contain old viewables - new viewables, so the player will only be exited in chunks that are out of sight.
* toEnter contains the opposite: new viewables - old viewables, chunks where we previously weren't visible from before.
*/
std::set_difference(oldViewables.begin(), oldViewables.end(), newViewables.begin(), newViewables.end(),
std::inserter(toExit, toExit.end())); // chunks we must be EXITed from (old - new)
std::set_difference(newViewables.begin(), newViewables.end(), oldViewables.begin(), oldViewables.end(),
std::inserter(toEnter, toEnter.end())); // chunks we must be ENTERed into (new - old)
// update views
removeNPCFromChunks(toExit, id);
addNPCToChunks(toEnter, id);
npc->chunkPos = to; // update cached chunk position
// updated cached viewable chunks
npc->viewableChunks->clear();
npc->viewableChunks->insert(newViewables.begin(), newViewables.end());
}
void ChunkManager::trackPlayer(ChunkPos chunkPos, CNSocket* sock) {
if (!chunkExists(chunkPos))
return; // shouldn't happen
@@ -371,7 +299,7 @@ void ChunkManager::removeNPCFromChunks(std::set<Chunk*> chnks, int32_t id) {
}
}
void ChunkManager::emptyChunk(ChunkPos chunkPos) {
static void emptyChunk(ChunkPos chunkPos) {
if (!chunkExists(chunkPos)) {
std::cout << "[WARN] Tried to empty chunk that doesn't exist\n";
return; // chunk doesn't exist, we don't need to do anything
@@ -392,6 +320,78 @@ void ChunkManager::emptyChunk(ChunkPos chunkPos) {
}
}
void ChunkManager::updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to) {
Player* plr = PlayerManager::getPlayer(sock);
// if the new chunk doesn't exist, make it first
if (!chunkExists(to))
newChunk(to);
// move to other chunk's player set
untrackPlayer(from, sock); // this will delete the chunk if it's empty
trackPlayer(to, sock);
// calculate viewable chunks from both points
std::set<Chunk*> oldViewables = getViewableChunks(from);
std::set<Chunk*> newViewables = getViewableChunks(to);
std::set<Chunk*> toExit, toEnter;
/*
* Calculate diffs. This is done to prevent phasing on chunk borders.
* toExit will contain old viewables - new viewables, so the player will only be exited in chunks that are out of sight.
* toEnter contains the opposite: new viewables - old viewables, chunks where we previously weren't visible from before.
*/
std::set_difference(oldViewables.begin(), oldViewables.end(), newViewables.begin(), newViewables.end(),
std::inserter(toExit, toExit.end())); // chunks we must be EXITed from (old - new)
std::set_difference(newViewables.begin(), newViewables.end(), oldViewables.begin(), oldViewables.end(),
std::inserter(toEnter, toEnter.end())); // chunks we must be ENTERed into (new - old)
// update views
removePlayerFromChunks(toExit, sock);
addPlayerToChunks(toEnter, sock);
plr->chunkPos = to; // update cached chunk position
// updated cached viewable chunks
plr->viewableChunks->clear();
plr->viewableChunks->insert(newViewables.begin(), newViewables.end());
}
void ChunkManager::updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to) {
BaseNPC* npc = NPCManager::NPCs[id];
// if the new chunk doesn't exist, make it first
if (!chunkExists(to))
newChunk(to);
// move to other chunk's player set
untrackNPC(from, id); // this will delete the chunk if it's empty
trackNPC(to, id);
// calculate viewable chunks from both points
std::set<Chunk*> oldViewables = getViewableChunks(from);
std::set<Chunk*> newViewables = getViewableChunks(to);
std::set<Chunk*> toExit, toEnter;
/*
* Calculate diffs. This is done to prevent phasing on chunk borders.
* toExit will contain old viewables - new viewables, so the player will only be exited in chunks that are out of sight.
* toEnter contains the opposite: new viewables - old viewables, chunks where we previously weren't visible from before.
*/
std::set_difference(oldViewables.begin(), oldViewables.end(), newViewables.begin(), newViewables.end(),
std::inserter(toExit, toExit.end())); // chunks we must be EXITed from (old - new)
std::set_difference(newViewables.begin(), newViewables.end(), oldViewables.begin(), oldViewables.end(),
std::inserter(toEnter, toEnter.end())); // chunks we must be ENTERed into (new - old)
// update views
removeNPCFromChunks(toExit, id);
addNPCToChunks(toEnter, id);
npc->chunkPos = to; // update cached chunk position
// updated cached viewable chunks
npc->viewableChunks->clear();
npc->viewableChunks->insert(newViewables.begin(), newViewables.end());
}
bool ChunkManager::chunkExists(ChunkPos chunk) {
return chunks.find(chunk) != chunks.end();
}
@@ -424,10 +424,10 @@ std::set<Chunk*> ChunkManager::getViewableChunks(ChunkPos chunk) {
/*
* inefficient algorithm to get all chunks from a specific instance
*/
std::vector<ChunkPos> ChunkManager::getChunksInMap(uint64_t mapNum) {
static std::vector<ChunkPos> getChunksInMap(uint64_t mapNum) {
std::vector<ChunkPos> chnks;
for (auto it = ChunkManager::chunks.begin(); it != ChunkManager::chunks.end(); it++) {
for (auto it = chunks.begin(); it != chunks.end(); it++) {
if (std::get<2>(it->first) == mapNum) {
chnks.push_back(it->first);
}
@@ -451,8 +451,8 @@ bool ChunkManager::inPopulatedChunks(std::set<Chunk*>* chnks) {
void ChunkManager::createInstance(uint64_t instanceID) {
std::vector<ChunkPos> templateChunks = ChunkManager::getChunksInMap(MAPNUM(instanceID)); // base instance chunks
if (ChunkManager::getChunksInMap(instanceID).size() == 0) { // only instantiate if the instance doesn't exist already
std::vector<ChunkPos> templateChunks = getChunksInMap(MAPNUM(instanceID)); // base instance chunks
if (getChunksInMap(instanceID).size() == 0) { // only instantiate if the instance doesn't exist already
std::cout << "Creating instance " << instanceID << std::endl;
for (ChunkPos &coords : templateChunks) {
for (int npcID : chunks[coords]->NPCs) {
@@ -508,9 +508,9 @@ void ChunkManager::createInstance(uint64_t instanceID) {
}
}
void ChunkManager::destroyInstance(uint64_t instanceID) {
static void destroyInstance(uint64_t instanceID) {
std::vector<ChunkPos> instanceChunks = ChunkManager::getChunksInMap(instanceID);
std::vector<ChunkPos> instanceChunks = getChunksInMap(instanceID);
std::cout << "Deleting instance " << instanceID << " (" << instanceChunks.size() << " chunks)" << std::endl;
for (ChunkPos& coords : instanceChunks) {
emptyChunk(coords);