added basework for instancing

This commit is contained in:
CPunch 2020-09-30 20:44:37 -05:00
parent 8ff97ec0b3
commit 4cd7b7cb53
11 changed files with 61 additions and 47 deletions

View File

@ -3,12 +3,12 @@
#include "NPCManager.hpp" #include "NPCManager.hpp"
#include "settings.hpp" #include "settings.hpp"
std::map<std::pair<int, int>, Chunk*> ChunkManager::chunks; std::map<std::tuple<int, int, int>, Chunk*> ChunkManager::chunks;
void ChunkManager::init() {} // stubbed void ChunkManager::init() {} // stubbed
void ChunkManager::addNPC(int posX, int posY, int32_t id) { void ChunkManager::addNPC(int posX, int posY, int instanceID, int32_t id) {
std::pair<int, int> pos = grabChunk(posX, posY); std::tuple<int, int, int> pos = grabChunk(posX, posY, instanceID);
// make chunk if it doesn't exist! // make chunk if it doesn't exist!
if (chunks.find(pos) == chunks.end()) { if (chunks.find(pos) == chunks.end()) {
@ -22,8 +22,8 @@ void ChunkManager::addNPC(int posX, int posY, int32_t id) {
chunk->NPCs.insert(id); chunk->NPCs.insert(id);
} }
void ChunkManager::addPlayer(int posX, int posY, CNSocket* sock) { void ChunkManager::addPlayer(int posX, int posY, int instanceID, CNSocket* sock) {
std::pair<int, int> pos = grabChunk(posX, posY); std::tuple<int, int, int> pos = grabChunk(posX, posY, instanceID);
// make chunk if it doesn't exist! // make chunk if it doesn't exist!
if (chunks.find(pos) == chunks.end()) { if (chunks.find(pos) == chunks.end()) {
@ -37,7 +37,7 @@ void ChunkManager::addPlayer(int posX, int posY, CNSocket* sock) {
chunk->players.insert(sock); chunk->players.insert(sock);
} }
void ChunkManager::removePlayer(std::pair<int, int> chunkPos, CNSocket* sock) { void ChunkManager::removePlayer(std::tuple<int, int, int> chunkPos, CNSocket* sock) {
if (!checkChunk(chunkPos)) if (!checkChunk(chunkPos))
return; // do nothing if chunk doesn't even exist return; // do nothing if chunk doesn't even exist
@ -48,7 +48,7 @@ void ChunkManager::removePlayer(std::pair<int, int> chunkPos, CNSocket* sock) {
// TODO: if players and NPCs are empty, free chunk and remove it from surrounding views // TODO: if players and NPCs are empty, free chunk and remove it from surrounding views
} }
void ChunkManager::removeNPC(std::pair<int, int> chunkPos, int32_t id) { void ChunkManager::removeNPC(std::tuple<int, int, int> chunkPos, int32_t id) {
if (!checkChunk(chunkPos)) if (!checkChunk(chunkPos))
return; // do nothing if chunk doesn't even exist return; // do nothing if chunk doesn't even exist
@ -59,22 +59,25 @@ void ChunkManager::removeNPC(std::pair<int, int> chunkPos, int32_t id) {
// TODO: if players and NPCs are empty, free chunk and remove it from surrounding views // TODO: if players and NPCs are empty, free chunk and remove it from surrounding views
} }
bool ChunkManager::checkChunk(std::pair<int, int> chunk) { bool ChunkManager::checkChunk(std::tuple<int, int, int> chunk) {
return chunks.find(chunk) != chunks.end(); return chunks.find(chunk) != chunks.end();
} }
std::pair<int, int> ChunkManager::grabChunk(int posX, int posY) { std::tuple<int, int, int> ChunkManager::grabChunk(int posX, int posY, int instanceID) {
return std::make_pair<int, int>(posX / (settings::CHUNKSIZE / 3), posY / (settings::CHUNKSIZE / 3)); return std::make_tuple(posX / (settings::CHUNKSIZE / 3), posY / (settings::CHUNKSIZE / 3), instanceID);
} }
std::vector<Chunk*> ChunkManager::grabChunks(std::pair<int, int> chunk) { std::vector<Chunk*> ChunkManager::grabChunks(std::tuple<int, int, int> chunk) {
std::vector<Chunk*> chnks; std::vector<Chunk*> chnks;
chnks.reserve(9); chnks.reserve(9);
int x, y, inst;
std::tie(x, y, inst) = chunk;
// grabs surrounding chunks if they exist // grabs surrounding chunks if they exist
for (int i = -1; i < 2; i++) { for (int i = -1; i < 2; i++) {
for (int z = -1; z < 2; z++) { for (int z = -1; z < 2; z++) {
std::pair<int, int> pos(chunk.first+i, chunk.second+z); std::tuple<int, int, int> pos = std::make_tuple(x+i, y+z, inst);
// if chunk exists, add it to the vector // if chunk exists, add it to the vector
if (checkChunk(pos)) if (checkChunk(pos))
@ -108,8 +111,8 @@ std::vector<Chunk*> ChunkManager::getDeltaChunks(std::vector<Chunk*> from, std::
return delta; return delta;
} }
bool ChunkManager::inPopulatedChunks(int posX, int posY) { bool ChunkManager::inPopulatedChunks(int posX, int posY, int instanceID) {
auto chunk = ChunkManager::grabChunk(posX, posY); auto chunk = ChunkManager::grabChunk(posX, posY, instanceID);
auto nearbyChunks = ChunkManager::grabChunks(chunk); auto nearbyChunks = ChunkManager::grabChunks(chunk);
for (Chunk *c: nearbyChunks) { for (Chunk *c: nearbyChunks) {

View File

@ -6,6 +6,7 @@
#include <vector> #include <vector>
#include <set> #include <set>
#include <map> #include <map>
#include <tuple>
class Chunk { class Chunk {
public: public:
@ -13,19 +14,25 @@ public:
std::set<int32_t> NPCs; std::set<int32_t> NPCs;
}; };
enum {
INSTANCE_OVERWORLD, // default instance every player starts in
INSTANCE_IZ, // all infected zones share an instance
INSTANCE_UNIQUE // fusion lairs are generated as requested (+ uid)
};
namespace ChunkManager { namespace ChunkManager {
void init(); void init();
void cleanup(); void cleanup();
extern std::map<std::pair<int, int>, Chunk*> chunks; extern std::map<std::tuple<int, int, int>, Chunk*> chunks;
void addNPC(int posX, int posY, int32_t id); void addNPC(int posX, int posY, int instanceID, int32_t id);
void addPlayer(int posX, int posY, CNSocket* sock); void addPlayer(int posX, int posY, int instanceID, CNSocket* sock);
void removePlayer(std::pair<int, int> chunkPos, CNSocket* sock); void removePlayer(std::tuple<int, int, int> chunkPos, CNSocket* sock);
void removeNPC(std::pair<int, int> chunkPos, int32_t id); void removeNPC(std::tuple<int, int, int> chunkPos, int32_t id);
bool checkChunk(std::pair<int, int> chunk); bool checkChunk(std::tuple<int, int, int> chunk);
std::pair<int, int> grabChunk(int posX, int posY); std::tuple<int, int, int> grabChunk(int posX, int posY, int instanceID);
std::vector<Chunk*> grabChunks(std::pair<int, int> chunkPos); std::vector<Chunk*> grabChunks(std::tuple<int, int, int> chunkPos);
std::vector<Chunk*> getDeltaChunks(std::vector<Chunk*> from, std::vector<Chunk*> to); std::vector<Chunk*> getDeltaChunks(std::vector<Chunk*> from, std::vector<Chunk*> to);
bool inPopulatedChunks(int posX, int posY); bool inPopulatedChunks(int posX, int posY, int instanceID);
} }

View File

@ -457,7 +457,7 @@ void MobManager::step(CNServer *serv, time_t currTime) {
int y = pair.second->appearanceData.iY; int y = pair.second->appearanceData.iY;
// skip chunks without players // skip chunks without players
if (!ChunkManager::inPopulatedChunks(x, y)) if (!ChunkManager::inPopulatedChunks(x, y, pair.second->instanceID))
continue; continue;
// skip mob movement and combat if disabled // skip mob movement and combat if disabled

View File

@ -42,8 +42,8 @@ struct Mob : public BaseNPC {
// temporary; until we're sure what's what // temporary; until we're sure what's what
nlohmann::json data; nlohmann::json data;
Mob(int x, int y, int z, int type, int hp, int angle, nlohmann::json d, int32_t id) Mob(int x, int y, int z, int iID, int type, int hp, int angle, nlohmann::json d, int32_t id)
: BaseNPC(x, y, z, type, id), maxHealth(hp) { : BaseNPC(x, y, z, iID, type, id), maxHealth(hp) {
state = MobState::ROAMING; state = MobState::ROAMING;
data = d; data = d;
@ -68,8 +68,8 @@ struct Mob : public BaseNPC {
} }
// constructor for /summon // constructor for /summon
Mob(int x, int y, int z, int type, nlohmann::json d, int32_t id) Mob(int x, int y, int z, int iID, int type, nlohmann::json d, int32_t id)
: Mob(x, y, z, type, 0, 0, d, id) { : Mob(x, y, z, iID, type, 0, 0, d, id) {
summoned = true; // will be despawned and deallocated when killed summoned = true; // will be despawned and deallocated when killed
appearanceData.iHP = maxHealth = d["m_iHP"]; appearanceData.iHP = maxHealth = d["m_iHP"];
} }

View File

@ -7,11 +7,12 @@ class BaseNPC {
public: public:
sNPCAppearanceData appearanceData; sNPCAppearanceData appearanceData;
NPCClass npcClass; NPCClass npcClass;
std::pair<int, int> chunkPos; int instanceID;
std::tuple<int, int, int> chunkPos;
std::vector<Chunk*> currentChunks; std::vector<Chunk*> currentChunks;
BaseNPC() {}; BaseNPC() {};
BaseNPC(int x, int y, int z, int type, int id) { BaseNPC(int x, int y, int z, int iID, int type, int id) {
appearanceData.iX = x; appearanceData.iX = x;
appearanceData.iY = y; appearanceData.iY = y;
appearanceData.iZ = z; appearanceData.iZ = z;
@ -22,9 +23,11 @@ public:
appearanceData.iBarkerType = 0; appearanceData.iBarkerType = 0;
appearanceData.iNPC_ID = id; appearanceData.iNPC_ID = id;
chunkPos = std::pair<int, int>(0, 0); instanceID = iID;
chunkPos = std::make_tuple(0, 0, instanceID);
}; };
BaseNPC(int x, int y, int z, int type, int id, NPCClass classType) : BaseNPC(x, y, z, type, id) { BaseNPC(int x, int y, int z, int iID, int type, int id, NPCClass classType) : BaseNPC(x, y, z, iID, type, id) {
npcClass = classType; npcClass = classType;
} }
}; };

View File

@ -140,7 +140,7 @@ void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z) {
npc->appearanceData.iX = X; npc->appearanceData.iX = X;
npc->appearanceData.iY = Y; npc->appearanceData.iY = Y;
npc->appearanceData.iZ = Z; npc->appearanceData.iZ = Z;
std::pair<int, int> newPos = ChunkManager::grabChunk(X, Y); std::tuple<int, int, int> newPos = ChunkManager::grabChunk(X, Y, npc->instanceID);
// nothing to be done (but we should also update currentChunks to add/remove stale chunks) // nothing to be done (but we should also update currentChunks to add/remove stale chunks)
if (newPos == npc->chunkPos) { if (newPos == npc->chunkPos) {
@ -158,7 +158,7 @@ void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z) {
// update chunks // update chunks
ChunkManager::removeNPC(npc->chunkPos, id); ChunkManager::removeNPC(npc->chunkPos, id);
ChunkManager::addNPC(X, Y, id); ChunkManager::addNPC(X, Y, npc->instanceID, id);
npc->chunkPos = newPos; npc->chunkPos = newPos;
npc->currentChunks = allChunks; npc->currentChunks = allChunks;
@ -544,10 +544,10 @@ void NPCManager::npcSummonHandler(CNSocket* sock, CNPacketData* data) {
resp.NPCAppearanceData.iZ = plr->z; resp.NPCAppearanceData.iZ = plr->z;
if (team == 2) { if (team == 2) {
NPCs[resp.NPCAppearanceData.iNPC_ID] = new Mob(plr->x, plr->y, plr->z, req->iNPCType, NPCData[req->iNPCType], resp.NPCAppearanceData.iNPC_ID); NPCs[resp.NPCAppearanceData.iNPC_ID] = new Mob(plr->x, plr->y, plr->z, plr->instanceID, req->iNPCType, NPCData[req->iNPCType], resp.NPCAppearanceData.iNPC_ID);
MobManager::Mobs[resp.NPCAppearanceData.iNPC_ID] = (Mob*)NPCs[resp.NPCAppearanceData.iNPC_ID]; MobManager::Mobs[resp.NPCAppearanceData.iNPC_ID] = (Mob*)NPCs[resp.NPCAppearanceData.iNPC_ID];
} else } else
NPCs[resp.NPCAppearanceData.iNPC_ID] = new BaseNPC(plr->x, plr->y, plr->z, req->iNPCType, resp.NPCAppearanceData.iNPC_ID); NPCs[resp.NPCAppearanceData.iNPC_ID] = new BaseNPC(plr->x, plr->y, plr->z, plr->instanceID, req->iNPCType, resp.NPCAppearanceData.iNPC_ID);
updateNPCPosition(resp.NPCAppearanceData.iNPC_ID, plr->x, plr->y, plr->z); updateNPCPosition(resp.NPCAppearanceData.iNPC_ID, plr->x, plr->y, plr->z);
} }
@ -585,7 +585,7 @@ void NPCManager::handleWarp(CNSocket* sock, int32_t warpId) {
// force player & NPC reload // force player & NPC reload
PlayerManager::removePlayerFromChunks(plrv.currentChunks, sock); PlayerManager::removePlayerFromChunks(plrv.currentChunks, sock);
plrv.currentChunks.clear(); plrv.currentChunks.clear();
plrv.chunkPos = std::make_pair<int, int>(0, 0); plrv.chunkPos = std::make_tuple(0, 0, plrv.plr->instanceID);
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC)); sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC));
} }

View File

@ -37,7 +37,7 @@ struct Player {
int32_t iConditionBitFlag; int32_t iConditionBitFlag;
int8_t iSpecialState; int8_t iSpecialState;
int x, y, z, angle; int x, y, z, angle, instanceID;
sItemBase Equip[AEQUIP_COUNT]; sItemBase Equip[AEQUIP_COUNT];
sItemBase Inven[AINVEN_COUNT]; sItemBase Inven[AINVEN_COUNT];
sItemBase Bank[ABANK_COUNT]; sItemBase Bank[ABANK_COUNT];

View File

@ -47,7 +47,7 @@ void PlayerManager::addPlayer(CNSocket* key, Player plr) {
memcpy(p, &plr, sizeof(Player)); memcpy(p, &plr, sizeof(Player));
players[key] = PlayerView(); players[key] = PlayerView();
players[key].chunkPos = std::make_pair<int, int>(0, 0); players[key].chunkPos = std::make_tuple(0, 0, p->instanceID);
players[key].currentChunks = std::vector<Chunk*>(); players[key].currentChunks = std::vector<Chunk*>();
players[key].plr = p; players[key].plr = p;
players[key].lastHeartbeat = 0; players[key].lastHeartbeat = 0;
@ -191,7 +191,7 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
void PlayerManager::updatePlayerChunk(CNSocket* sock, int X, int Y) { void PlayerManager::updatePlayerChunk(CNSocket* sock, int X, int Y) {
PlayerView& view = players[sock]; PlayerView& view = players[sock];
std::pair<int, int> newPos = ChunkManager::grabChunk(X, Y); std::tuple<int, int, int> newPos = ChunkManager::grabChunk(X, Y, view.plr->instanceID);
// nothing to be done // nothing to be done
if (newPos == view.chunkPos) if (newPos == view.chunkPos)
@ -206,7 +206,7 @@ void PlayerManager::updatePlayerChunk(CNSocket* sock, int X, int Y) {
// now, add all the new npcs & players! // now, add all the new npcs & players!
addPlayerToChunks(ChunkManager::getDeltaChunks(allChunks, view.currentChunks), sock); addPlayerToChunks(ChunkManager::getDeltaChunks(allChunks, view.currentChunks), sock);
ChunkManager::addPlayer(X, Y, sock); // takes care of adding the player to the chunk if it exists or not ChunkManager::addPlayer(X, Y, view.plr->instanceID, sock); // takes care of adding the player to the chunk if it exists or not
view.chunkPos = newPos; view.chunkPos = newPos;
view.currentChunks = allChunks; view.currentChunks = allChunks;
} }
@ -309,6 +309,7 @@ void PlayerManager::enterPlayer(CNSocket* sock, CNPacketData* data) {
response.PCLoadData2CL.iFirstUseFlag2 = UINT64_MAX; response.PCLoadData2CL.iFirstUseFlag2 = UINT64_MAX;
plr.SerialKey = enter->iEnterSerialKey; plr.SerialKey = enter->iEnterSerialKey;
plr.instanceID = INSTANCE_OVERWORLD; // TODO: load this from the database (as long as it's not a unique instance)
motd.iType = 1; motd.iType = 1;
U8toU16(settings::MOTDSTRING, (char16_t*)motd.szSystemMsg); U8toU16(settings::MOTDSTRING, (char16_t*)motd.szSystemMsg);
@ -647,7 +648,7 @@ void PlayerManager::gotoPlayer(CNSocket* sock, CNPacketData* data) {
// force player & NPC reload // force player & NPC reload
PlayerManager::removePlayerFromChunks(plrv.currentChunks, sock); PlayerManager::removePlayerFromChunks(plrv.currentChunks, sock);
plrv.currentChunks.clear(); plrv.currentChunks.clear();
plrv.chunkPos = std::make_pair<int, int>(0, 0); plrv.chunkPos = std::make_tuple(0, 0, plrv.plr->instanceID);
sock->sendPacket((void*)&response, P_FE2CL_REP_PC_GOTO_SUCC, sizeof(sP_FE2CL_REP_PC_GOTO_SUCC)); sock->sendPacket((void*)&response, P_FE2CL_REP_PC_GOTO_SUCC, sizeof(sP_FE2CL_REP_PC_GOTO_SUCC));
} }

View File

@ -13,7 +13,7 @@
struct WarpLocation; struct WarpLocation;
struct PlayerView { struct PlayerView {
std::pair<int, int> chunkPos; std::tuple<int, int, int> chunkPos;
std::vector<Chunk*> currentChunks; std::vector<Chunk*> currentChunks;
Player *plr; Player *plr;
time_t lastHeartbeat; time_t lastHeartbeat;

View File

@ -25,7 +25,7 @@ void TableData::init() {
for (nlohmann::json::iterator _npc = npcData.begin(); _npc != npcData.end(); _npc++) { for (nlohmann::json::iterator _npc = npcData.begin(); _npc != npcData.end(); _npc++) {
auto npc = _npc.value(); auto npc = _npc.value();
BaseNPC *tmp = new BaseNPC(npc["x"], npc["y"], npc["z"], npc["id"], nextId); BaseNPC *tmp = new BaseNPC(npc["x"], npc["y"], npc["z"], INSTANCE_OVERWORLD, npc["id"], nextId);
NPCManager::NPCs[nextId] = tmp; NPCManager::NPCs[nextId] = tmp;
NPCManager::updateNPCPosition(nextId, npc["x"], npc["y"], npc["z"]); NPCManager::updateNPCPosition(nextId, npc["x"], npc["y"], npc["z"]);
@ -179,7 +179,7 @@ void TableData::init() {
for (nlohmann::json::iterator _npc = npcData.begin(); _npc != npcData.end(); _npc++) { for (nlohmann::json::iterator _npc = npcData.begin(); _npc != npcData.end(); _npc++) {
auto npc = _npc.value(); auto npc = _npc.value();
auto td = NPCManager::NPCData[(int)npc["iNPCType"]]; auto td = NPCManager::NPCData[(int)npc["iNPCType"]];
Mob *tmp = new Mob(npc["iX"], npc["iY"], npc["iZ"], npc["iNPCType"], npc["iHP"], npc["iAngle"], td, nextId); Mob *tmp = new Mob(npc["iX"], npc["iY"], npc["iZ"], INSTANCE_OVERWORLD, npc["iNPCType"], npc["iHP"], npc["iAngle"], td, nextId);
NPCManager::NPCs[nextId] = tmp; NPCManager::NPCs[nextId] = tmp;
MobManager::Mobs[nextId] = (Mob*)NPCManager::NPCs[nextId]; MobManager::Mobs[nextId] = (Mob*)NPCManager::NPCs[nextId];

View File

@ -149,7 +149,7 @@ void TransportManager::transportWarpHandler(CNSocket* sock, CNPacketData* data)
*/ */
PlayerManager::removePlayerFromChunks(plrv.currentChunks, sock); PlayerManager::removePlayerFromChunks(plrv.currentChunks, sock);
plrv.currentChunks.clear(); plrv.currentChunks.clear();
plrv.chunkPos = std::make_pair<int, int>(0, 0); plrv.chunkPos = std::make_tuple(0, 0, plrv.plr->instanceID);
break; break;
case 2: // Monkey Skyway case 2: // Monkey Skyway
if (SkywayPaths.find(route.mssRouteNum) != SkywayPaths.end()) { // check if route exists if (SkywayPaths.find(route.mssRouteNum) != SkywayPaths.end()) { // check if route exists
@ -268,7 +268,7 @@ void TransportManager::stepNPCPathing() {
NPCManager::updateNPCPosition(npc->appearanceData.iNPC_ID, point.x, point.y, point.z); NPCManager::updateNPCPosition(npc->appearanceData.iNPC_ID, point.x, point.y, point.z);
// get chunks in view // get chunks in view
auto chunk = ChunkManager::grabChunk(npc->appearanceData.iX, npc->appearanceData.iY); auto chunk = ChunkManager::grabChunk(npc->appearanceData.iX, npc->appearanceData.iY, npc->instanceID);
auto chunks = ChunkManager::grabChunks(chunk); auto chunks = ChunkManager::grabChunks(chunk);
switch (npc->npcClass) { switch (npc->npcClass) {