mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-21 21:20:04 +00:00
[WIP] Convert most of Chunking to Entity-based system
Player and all NPCs now have a common superclass, with virtual functions so smooth over shared behavior. EntityRef is a simple class that points to an arbitrary Entity. This commit is not yet functional.
This commit is contained in:
parent
49f1cb0f00
commit
224ffe05e7
2
Makefile
2
Makefile
@ -50,6 +50,7 @@ CXXSRC=\
|
|||||||
src/db/email.cpp\
|
src/db/email.cpp\
|
||||||
src/Chat.cpp\
|
src/Chat.cpp\
|
||||||
src/CustomCommands.cpp\
|
src/CustomCommands.cpp\
|
||||||
|
src/Entities.cpp\
|
||||||
src/Email.cpp\
|
src/Email.cpp\
|
||||||
src/Eggs.cpp\
|
src/Eggs.cpp\
|
||||||
src/main.cpp\
|
src/main.cpp\
|
||||||
@ -93,6 +94,7 @@ CXXHDR=\
|
|||||||
vendor/JSON.hpp\
|
vendor/JSON.hpp\
|
||||||
src/Chat.hpp\
|
src/Chat.hpp\
|
||||||
src/CustomCommands.hpp\
|
src/CustomCommands.hpp\
|
||||||
|
src/Entities.hpp\
|
||||||
src/Email.hpp\
|
src/Email.hpp\
|
||||||
src/Eggs.hpp\
|
src/Eggs.hpp\
|
||||||
src/Missions.hpp\
|
src/Missions.hpp\
|
||||||
|
348
src/Chunking.cpp
348
src/Chunking.cpp
@ -16,20 +16,13 @@ static void newChunk(ChunkPos pos) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Chunk *chunk = new Chunk();
|
Chunk *chunk = new Chunk();
|
||||||
|
|
||||||
chunk->players = std::set<CNSocket*>();
|
|
||||||
chunk->NPCs = std::set<int32_t>();
|
|
||||||
|
|
||||||
chunks[pos] = chunk;
|
chunks[pos] = chunk;
|
||||||
|
|
||||||
// add the chunk to the cache of all players and NPCs in the surrounding chunks
|
// add the chunk to the cache of all players and NPCs in the surrounding chunks
|
||||||
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 (const EntityRef& ref : c->entities)
|
||||||
PlayerManager::getPlayer(sock)->viewableChunks.insert(chunk);
|
ref.getEntity()->viewableChunks.insert(chunk);
|
||||||
for (int32_t id : c->NPCs)
|
|
||||||
NPCManager::NPCs[id]->viewableChunks.insert(chunk);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void deleteChunk(ChunkPos pos) {
|
static void deleteChunk(ChunkPos pos) {
|
||||||
@ -43,260 +36,89 @@ static void deleteChunk(ChunkPos pos) {
|
|||||||
// remove the chunk from the cache of all players and NPCs in the surrounding chunks
|
// remove the chunk from the cache of all players and NPCs in the surrounding chunks
|
||||||
std::set<Chunk*> surroundings = getViewableChunks(pos);
|
std::set<Chunk*> surroundings = getViewableChunks(pos);
|
||||||
for(Chunk* c : surroundings)
|
for(Chunk* c : surroundings)
|
||||||
{
|
for (const EntityRef& ref : c->entities)
|
||||||
for (CNSocket* sock : c->players)
|
ref.getEntity()->viewableChunks.erase(chunk);
|
||||||
PlayerManager::getPlayer(sock)->viewableChunks.erase(chunk);
|
|
||||||
for (int32_t id : c->NPCs)
|
|
||||||
NPCManager::NPCs[id]->viewableChunks.erase(chunk);
|
|
||||||
}
|
|
||||||
|
|
||||||
chunks.erase(pos); // remove from map
|
chunks.erase(pos); // remove from map
|
||||||
delete chunk; // free from memory
|
delete chunk; // free from memory
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunking::trackPlayer(ChunkPos chunkPos, CNSocket* sock) {
|
void Chunking::trackEntity(ChunkPos chunkPos, const EntityRef& ref) {
|
||||||
if (!chunkExists(chunkPos))
|
if (!chunkExists(chunkPos))
|
||||||
return; // shouldn't happen
|
return; // shouldn't happen
|
||||||
|
|
||||||
chunks[chunkPos]->players.insert(sock);
|
chunks[chunkPos]->entities.insert(ref);
|
||||||
|
|
||||||
|
if (ref.type == EntityType::PLAYER)
|
||||||
|
chunks[chunkPos]->nplayers++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunking::trackNPC(ChunkPos chunkPos, int32_t id) {
|
void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef& ref) {
|
||||||
if (!chunkExists(chunkPos))
|
|
||||||
return; // shouldn't happen
|
|
||||||
|
|
||||||
chunks[chunkPos]->NPCs.insert(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Chunking::untrackPlayer(ChunkPos chunkPos, CNSocket* sock) {
|
|
||||||
if (!chunkExists(chunkPos))
|
if (!chunkExists(chunkPos))
|
||||||
return; // do nothing if chunk doesn't even exist
|
return; // do nothing if chunk doesn't even exist
|
||||||
|
|
||||||
Chunk* chunk = chunks[chunkPos];
|
Chunk* chunk = chunks[chunkPos];
|
||||||
|
|
||||||
chunk->players.erase(sock); // gone
|
chunk->entities.erase(ref); // gone
|
||||||
|
|
||||||
// if chunk is empty, free it
|
if (ref.type == EntityType::PLAYER)
|
||||||
if (chunk->NPCs.size() == 0 && chunk->players.size() == 0)
|
chunks[chunkPos]->nplayers--;
|
||||||
|
assert(chunks[chunkPos]->nplayers >= 0);
|
||||||
|
|
||||||
|
// if chunk is completely empty, free it
|
||||||
|
if (chunk->entities.size() == 0)
|
||||||
deleteChunk(chunkPos);
|
deleteChunk(chunkPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunking::untrackNPC(ChunkPos chunkPos, int32_t id) {
|
void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef& ref) {
|
||||||
if (!chunkExists(chunkPos))
|
Entity *ent = ref.getEntity();
|
||||||
return; // do nothing if chunk doesn't even exist
|
bool alive = ent->isAlive();
|
||||||
|
|
||||||
Chunk* chunk = chunks[chunkPos];
|
|
||||||
|
|
||||||
chunk->NPCs.erase(id); // gone
|
|
||||||
|
|
||||||
// if chunk is empty, free it
|
|
||||||
if (chunk->NPCs.size() == 0 && chunk->players.size() == 0)
|
|
||||||
deleteChunk(chunkPos);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Chunking::addPlayerToChunks(std::set<Chunk*> chnks, CNSocket* sock) {
|
|
||||||
INITSTRUCT(sP_FE2CL_PC_NEW, newPlayer);
|
|
||||||
|
|
||||||
|
// TODO: maybe optimize this, potentially using AROUND packets?
|
||||||
for (Chunk *chunk : chnks) {
|
for (Chunk *chunk : chnks) {
|
||||||
// add npcs
|
for (const EntityRef& otherRef : chunk->entities) {
|
||||||
for (int32_t id : chunk->NPCs) {
|
// skip oneself
|
||||||
BaseNPC* npc = NPCManager::NPCs[id];
|
if (ref == otherRef)
|
||||||
npc->playersInView++;
|
|
||||||
|
|
||||||
if (npc->appearanceData.iHP <= 0)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
switch (npc->npcClass) {
|
Entity *other = otherRef.getEntity();
|
||||||
case NPC_BUS:
|
|
||||||
INITSTRUCT(sP_FE2CL_TRANSPORTATION_ENTER, enterBusData);
|
// notify all visible players of the existence of this Entity
|
||||||
enterBusData.AppearanceData = { 3, npc->appearanceData.iNPC_ID, npc->appearanceData.iNPCType, npc->appearanceData.iX, npc->appearanceData.iY, npc->appearanceData.iZ };
|
if (alive && otherRef.type == EntityType::PLAYER) {
|
||||||
sock->sendPacket((void*)&enterBusData, P_FE2CL_TRANSPORTATION_ENTER, sizeof(sP_FE2CL_TRANSPORTATION_ENTER));
|
ent->enterIntoViewOf(otherRef.sock);
|
||||||
break;
|
|
||||||
case NPC_EGG:
|
|
||||||
INITSTRUCT(sP_FE2CL_SHINY_ENTER, enterEggData);
|
|
||||||
Eggs::npcDataToEggData(&npc->appearanceData, &enterEggData.ShinyAppearanceData);
|
|
||||||
sock->sendPacket((void*)&enterEggData, P_FE2CL_SHINY_ENTER, sizeof(sP_FE2CL_SHINY_ENTER));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
INITSTRUCT(sP_FE2CL_NPC_ENTER, enterData);
|
|
||||||
enterData.NPCAppearanceData = NPCManager::NPCs[id]->appearanceData;
|
|
||||||
sock->sendPacket((void*)&enterData, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add players
|
// notify this *player* of the existence of all visible Entities
|
||||||
for (CNSocket* otherSock : chunk->players) {
|
if (ref.type == EntityType::PLAYER && other->isAlive()) {
|
||||||
if (sock == otherSock)
|
other->enterIntoViewOf(ref.sock);
|
||||||
continue; // that's us :P
|
}
|
||||||
|
|
||||||
Player* otherPlr = PlayerManager::getPlayer(otherSock);
|
|
||||||
Player* plr = PlayerManager::getPlayer(sock);
|
|
||||||
|
|
||||||
newPlayer.PCAppearanceData.iID = plr->iID;
|
|
||||||
newPlayer.PCAppearanceData.iHP = plr->HP;
|
|
||||||
newPlayer.PCAppearanceData.iLv = plr->level;
|
|
||||||
newPlayer.PCAppearanceData.iX = plr->x;
|
|
||||||
newPlayer.PCAppearanceData.iY = plr->y;
|
|
||||||
newPlayer.PCAppearanceData.iZ = plr->z;
|
|
||||||
newPlayer.PCAppearanceData.iAngle = plr->angle;
|
|
||||||
newPlayer.PCAppearanceData.PCStyle = plr->PCStyle;
|
|
||||||
newPlayer.PCAppearanceData.Nano = plr->Nanos[plr->activeNano];
|
|
||||||
newPlayer.PCAppearanceData.iPCState = plr->iPCState;
|
|
||||||
newPlayer.PCAppearanceData.iSpecialState = plr->iSpecialState;
|
|
||||||
memcpy(newPlayer.PCAppearanceData.ItemEquip, plr->Equip, sizeof(sItemBase) * AEQUIP_COUNT);
|
|
||||||
|
|
||||||
otherSock->sendPacket((void*)&newPlayer, P_FE2CL_PC_NEW, sizeof(sP_FE2CL_PC_NEW));
|
|
||||||
|
|
||||||
newPlayer.PCAppearanceData.iID = otherPlr->iID;
|
|
||||||
newPlayer.PCAppearanceData.iHP = otherPlr->HP;
|
|
||||||
newPlayer.PCAppearanceData.iLv = otherPlr->level;
|
|
||||||
newPlayer.PCAppearanceData.iX = otherPlr->x;
|
|
||||||
newPlayer.PCAppearanceData.iY = otherPlr->y;
|
|
||||||
newPlayer.PCAppearanceData.iZ = otherPlr->z;
|
|
||||||
newPlayer.PCAppearanceData.iAngle = otherPlr->angle;
|
|
||||||
newPlayer.PCAppearanceData.PCStyle = otherPlr->PCStyle;
|
|
||||||
newPlayer.PCAppearanceData.Nano = otherPlr->Nanos[otherPlr->activeNano];
|
|
||||||
newPlayer.PCAppearanceData.iPCState = otherPlr->iPCState;
|
|
||||||
newPlayer.PCAppearanceData.iSpecialState = otherPlr->iSpecialState;
|
|
||||||
memcpy(newPlayer.PCAppearanceData.ItemEquip, otherPlr->Equip, sizeof(sItemBase) * AEQUIP_COUNT);
|
|
||||||
|
|
||||||
sock->sendPacket((void*)&newPlayer, P_FE2CL_PC_NEW, sizeof(sP_FE2CL_PC_NEW));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunking::addNPCToChunks(std::set<Chunk*> chnks, int32_t id) {
|
void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef& ref) {
|
||||||
BaseNPC* npc = NPCManager::NPCs[id];
|
Entity *ent = ref.getEntity();
|
||||||
|
bool alive = ent->isAlive();
|
||||||
switch (npc->npcClass) {
|
|
||||||
case NPC_BUS:
|
|
||||||
INITSTRUCT(sP_FE2CL_TRANSPORTATION_ENTER, enterBusData);
|
|
||||||
enterBusData.AppearanceData = { 3, npc->appearanceData.iNPC_ID, npc->appearanceData.iNPCType, npc->appearanceData.iX, npc->appearanceData.iY, npc->appearanceData.iZ };
|
|
||||||
|
|
||||||
|
// TODO: same as above
|
||||||
for (Chunk *chunk : chnks) {
|
for (Chunk *chunk : chnks) {
|
||||||
for (CNSocket* sock : chunk->players) {
|
for (const EntityRef& otherRef : chunk->entities) {
|
||||||
// send to socket
|
// skip oneself
|
||||||
sock->sendPacket((void*)&enterBusData, P_FE2CL_TRANSPORTATION_ENTER, sizeof(sP_FE2CL_TRANSPORTATION_ENTER));
|
if (ref == otherRef)
|
||||||
npc->playersInView++;
|
continue;
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case NPC_EGG:
|
|
||||||
INITSTRUCT(sP_FE2CL_SHINY_ENTER, enterEggData);
|
|
||||||
Eggs::npcDataToEggData(&npc->appearanceData, &enterEggData.ShinyAppearanceData);
|
|
||||||
|
|
||||||
for (Chunk* chunk : chnks) {
|
Entity *other = otherRef.getEntity();
|
||||||
for (CNSocket* sock : chunk->players) {
|
|
||||||
// send to socket
|
|
||||||
sock->sendPacket((void*)&enterEggData, P_FE2CL_SHINY_ENTER, sizeof(sP_FE2CL_SHINY_ENTER));
|
|
||||||
npc->playersInView++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// create struct
|
|
||||||
INITSTRUCT(sP_FE2CL_NPC_ENTER, enterData);
|
|
||||||
enterData.NPCAppearanceData = npc->appearanceData;
|
|
||||||
|
|
||||||
for (Chunk* chunk : chnks) {
|
// notify all visible players of the departure of this Entity
|
||||||
for (CNSocket* sock : chunk->players) {
|
if (alive && otherRef.type == EntityType::PLAYER) {
|
||||||
// send to socket
|
ent->disappearFromViewOf(otherRef.sock);
|
||||||
sock->sendPacket((void*)&enterData, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER));
|
|
||||||
npc->playersInView++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunking::removePlayerFromChunks(std::set<Chunk*> chnks, CNSocket* sock) {
|
// notify this *player* of the departure of all visible Entities
|
||||||
INITSTRUCT(sP_FE2CL_PC_EXIT, exitPlayer);
|
if (ref.type == EntityType::PLAYER && other->isAlive()) {
|
||||||
|
other->disappearFromViewOf(ref.sock);
|
||||||
// for chunks that need the player to be removed from
|
|
||||||
for (Chunk* chunk : chnks) {
|
|
||||||
|
|
||||||
// remove NPCs from view
|
|
||||||
for (int32_t id : chunk->NPCs) {
|
|
||||||
BaseNPC* npc = NPCManager::NPCs[id];
|
|
||||||
npc->playersInView--;
|
|
||||||
|
|
||||||
switch (npc->npcClass) {
|
|
||||||
case NPC_BUS:
|
|
||||||
INITSTRUCT(sP_FE2CL_TRANSPORTATION_EXIT, exitBusData);
|
|
||||||
exitBusData.eTT = 3;
|
|
||||||
exitBusData.iT_ID = id;
|
|
||||||
sock->sendPacket((void*)&exitBusData, P_FE2CL_TRANSPORTATION_EXIT, sizeof(sP_FE2CL_TRANSPORTATION_EXIT));
|
|
||||||
break;
|
|
||||||
case NPC_EGG:
|
|
||||||
INITSTRUCT(sP_FE2CL_SHINY_EXIT, exitEggData);
|
|
||||||
exitEggData.iShinyID = id;
|
|
||||||
sock->sendPacket((void*)&exitEggData, P_FE2CL_SHINY_EXIT, sizeof(sP_FE2CL_SHINY_EXIT));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
INITSTRUCT(sP_FE2CL_NPC_EXIT, exitData);
|
|
||||||
exitData.iNPC_ID = id;
|
|
||||||
sock->sendPacket((void*)&exitData, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT));
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove players from eachother's views
|
|
||||||
for (CNSocket* otherSock : chunk->players) {
|
|
||||||
if (sock == otherSock)
|
|
||||||
continue; // that's us :P
|
|
||||||
exitPlayer.iID = PlayerManager::getPlayer(sock)->iID;
|
|
||||||
otherSock->sendPacket((void*)&exitPlayer, P_FE2CL_PC_EXIT, sizeof(sP_FE2CL_PC_EXIT));
|
|
||||||
exitPlayer.iID = PlayerManager::getPlayer(otherSock)->iID;
|
|
||||||
sock->sendPacket((void*)&exitPlayer, P_FE2CL_PC_EXIT, sizeof(sP_FE2CL_PC_EXIT));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void Chunking::removeNPCFromChunks(std::set<Chunk*> chnks, int32_t id) {
|
|
||||||
BaseNPC* npc = NPCManager::NPCs[id];
|
|
||||||
|
|
||||||
switch (npc->npcClass) {
|
|
||||||
case NPC_BUS:
|
|
||||||
INITSTRUCT(sP_FE2CL_TRANSPORTATION_EXIT, exitBusData);
|
|
||||||
exitBusData.eTT = 3;
|
|
||||||
exitBusData.iT_ID = id;
|
|
||||||
|
|
||||||
for (Chunk* chunk : chnks) {
|
|
||||||
for (CNSocket* sock : chunk->players) {
|
|
||||||
// send to socket
|
|
||||||
sock->sendPacket((void*)&exitBusData, P_FE2CL_TRANSPORTATION_EXIT, sizeof(sP_FE2CL_TRANSPORTATION_EXIT));
|
|
||||||
npc->playersInView--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case NPC_EGG:
|
|
||||||
INITSTRUCT(sP_FE2CL_SHINY_EXIT, exitEggData);
|
|
||||||
exitEggData.iShinyID = id;
|
|
||||||
|
|
||||||
for (Chunk* chunk : chnks) {
|
|
||||||
for (CNSocket* sock : chunk->players) {
|
|
||||||
// send to socket
|
|
||||||
sock->sendPacket((void*)&exitEggData, P_FE2CL_SHINY_EXIT, sizeof(sP_FE2CL_SHINY_EXIT));
|
|
||||||
npc->playersInView--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// create struct
|
|
||||||
INITSTRUCT(sP_FE2CL_NPC_EXIT, exitData);
|
|
||||||
exitData.iNPC_ID = id;
|
|
||||||
|
|
||||||
// remove it from the clients
|
|
||||||
for (Chunk* chunk : chnks) {
|
|
||||||
for (CNSocket* sock : chunk->players) {
|
|
||||||
// send to socket
|
|
||||||
sock->sendPacket((void*)&exitData, P_FE2CL_NPC_EXIT, sizeof(sP_FE2CL_NPC_EXIT));
|
|
||||||
npc->playersInView--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,29 +130,32 @@ static void emptyChunk(ChunkPos chunkPos) {
|
|||||||
|
|
||||||
Chunk* chunk = chunks[chunkPos];
|
Chunk* chunk = chunks[chunkPos];
|
||||||
|
|
||||||
if (chunk->players.size() > 0) {
|
if (chunk->nplayers > 0) {
|
||||||
std::cout << "[WARN] Tried to empty chunk that still had players\n";
|
std::cout << "[WARN] Tried to empty chunk that still had players\n";
|
||||||
return; // chunk doesn't exist, we don't need to do anything
|
return; // chunk doesn't exist, we don't need to do anything
|
||||||
}
|
}
|
||||||
|
|
||||||
// unspawn all of the mobs/npcs
|
// unspawn all of the mobs/npcs
|
||||||
std::set npcIDs(chunk->NPCs);
|
std::set refs(chunk->entities);
|
||||||
for (uint32_t id : npcIDs) {
|
for (const EntityRef& ref : refs) {
|
||||||
|
if (ref.type == EntityType::PLAYER)
|
||||||
|
assert(0);
|
||||||
|
|
||||||
// every call of this will check if the chunk is empty and delete it if so
|
// every call of this will check if the chunk is empty and delete it if so
|
||||||
NPCManager::destroyNPC(id);
|
NPCManager::destroyNPC(ref.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunking::updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to) {
|
void Chunking::updateEntityChunk(const EntityRef& ref, ChunkPos from, ChunkPos to) {
|
||||||
Player* plr = PlayerManager::getPlayer(sock);
|
Entity* ent = ref.getEntity();
|
||||||
|
|
||||||
// if the new chunk doesn't exist, make it first
|
// if the new chunk doesn't exist, make it first
|
||||||
if (!chunkExists(to))
|
if (!chunkExists(to))
|
||||||
newChunk(to);
|
newChunk(to);
|
||||||
|
|
||||||
// move to other chunk's player set
|
// move to other chunk's player set
|
||||||
untrackPlayer(from, sock); // this will delete the chunk if it's empty
|
untrackEntity(from, ref); // this will delete the chunk if it's empty
|
||||||
trackPlayer(to, sock);
|
trackEntity(to, ref);
|
||||||
|
|
||||||
// calculate viewable chunks from both points
|
// calculate viewable chunks from both points
|
||||||
std::set<Chunk*> oldViewables = getViewableChunks(from);
|
std::set<Chunk*> oldViewables = getViewableChunks(from);
|
||||||
@ -348,49 +173,13 @@ void Chunking::updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to) {
|
|||||||
std::inserter(toEnter, toEnter.end())); // chunks we must be ENTERed into (new - old)
|
std::inserter(toEnter, toEnter.end())); // chunks we must be ENTERed into (new - old)
|
||||||
|
|
||||||
// update views
|
// update views
|
||||||
removePlayerFromChunks(toExit, sock);
|
removeEntityFromChunks(toExit, ref);
|
||||||
addPlayerToChunks(toEnter, sock);
|
addEntityToChunks(toEnter, ref);
|
||||||
|
|
||||||
plr->chunkPos = to; // update cached chunk position
|
ent->chunkPos = to; // update cached chunk position
|
||||||
// updated cached viewable chunks
|
// updated cached viewable chunks
|
||||||
plr->viewableChunks.clear();
|
ent->viewableChunks.clear();
|
||||||
plr->viewableChunks.insert(newViewables.begin(), newViewables.end());
|
ent->viewableChunks.insert(newViewables.begin(), newViewables.end());
|
||||||
}
|
|
||||||
|
|
||||||
void Chunking::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 Chunking::chunkExists(ChunkPos chunk) {
|
bool Chunking::chunkExists(ChunkPos chunk) {
|
||||||
@ -441,9 +230,8 @@ static std::vector<ChunkPos> getChunksInMap(uint64_t mapNum) {
|
|||||||
* Used only for eggs; use npc->playersInView for everything visible
|
* Used only for eggs; use npc->playersInView for everything visible
|
||||||
*/
|
*/
|
||||||
bool Chunking::inPopulatedChunks(std::set<Chunk*>* chnks) {
|
bool Chunking::inPopulatedChunks(std::set<Chunk*>* chnks) {
|
||||||
|
|
||||||
for (auto it = chnks->begin(); it != chnks->end(); it++) {
|
for (auto it = chnks->begin(); it != chnks->end(); it++) {
|
||||||
if (!(*it)->players.empty())
|
if ((*it)->nplayers > 0)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -451,7 +239,6 @@ bool Chunking::inPopulatedChunks(std::set<Chunk*>* chnks) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Chunking::createInstance(uint64_t instanceID) {
|
void Chunking::createInstance(uint64_t instanceID) {
|
||||||
|
|
||||||
std::vector<ChunkPos> templateChunks = getChunksInMap(MAPNUM(instanceID)); // base instance chunks
|
std::vector<ChunkPos> templateChunks = getChunksInMap(MAPNUM(instanceID)); // base instance chunks
|
||||||
if (getChunksInMap(instanceID).size() == 0) { // only instantiate if the instance doesn't exist already
|
if (getChunksInMap(instanceID).size() == 0) { // only instantiate if the instance doesn't exist already
|
||||||
std::cout << "Creating instance " << instanceID << std::endl;
|
std::cout << "Creating instance " << instanceID << std::endl;
|
||||||
@ -510,7 +297,6 @@ void Chunking::createInstance(uint64_t instanceID) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void destroyInstance(uint64_t instanceID) {
|
static void destroyInstance(uint64_t instanceID) {
|
||||||
|
|
||||||
std::vector<ChunkPos> instanceChunks = getChunksInMap(instanceID);
|
std::vector<ChunkPos> instanceChunks = getChunksInMap(instanceID);
|
||||||
std::cout << "Deleting instance " << instanceID << " (" << instanceChunks.size() << " chunks)" << std::endl;
|
std::cout << "Deleting instance " << instanceID << " (" << instanceChunks.size() << " chunks)" << std::endl;
|
||||||
for (ChunkPos& coords : instanceChunks) {
|
for (ChunkPos& coords : instanceChunks) {
|
||||||
@ -527,7 +313,7 @@ void Chunking::destroyInstanceIfEmpty(uint64_t instanceID) {
|
|||||||
for (ChunkPos& coords : sourceChunkCoords) {
|
for (ChunkPos& coords : sourceChunkCoords) {
|
||||||
Chunk* chunk = chunks[coords];
|
Chunk* chunk = chunks[coords];
|
||||||
|
|
||||||
if (chunk->players.size() > 0)
|
if (chunk->nplayers > 0)
|
||||||
return; // there are still players inside
|
return; // there are still players inside
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "core/Core.hpp"
|
#include "core/Core.hpp"
|
||||||
|
#include "Entities.hpp"
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <set>
|
#include <set>
|
||||||
@ -12,6 +13,8 @@ class Chunk {
|
|||||||
public:
|
public:
|
||||||
std::set<CNSocket*> players;
|
std::set<CNSocket*> players;
|
||||||
std::set<int32_t> NPCs;
|
std::set<int32_t> NPCs;
|
||||||
|
std::set<EntityRef> entities;
|
||||||
|
int nplayers = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
@ -23,18 +26,13 @@ enum {
|
|||||||
namespace Chunking {
|
namespace Chunking {
|
||||||
extern std::map<ChunkPos, Chunk*> chunks;
|
extern std::map<ChunkPos, Chunk*> chunks;
|
||||||
|
|
||||||
void updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to);
|
void updateEntityChunk(const EntityRef& ref, ChunkPos from, ChunkPos to);
|
||||||
void updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to);
|
|
||||||
|
|
||||||
void trackPlayer(ChunkPos chunkPos, CNSocket* sock);
|
void trackEntity(ChunkPos chunkPos, const EntityRef& ref);
|
||||||
void trackNPC(ChunkPos chunkPos, int32_t id);
|
void untrackEntity(ChunkPos chunkPos, const EntityRef& ref);
|
||||||
void untrackPlayer(ChunkPos chunkPos, CNSocket* sock);
|
|
||||||
void untrackNPC(ChunkPos chunkPos, int32_t id);
|
|
||||||
|
|
||||||
void addPlayerToChunks(std::set<Chunk*> chnks, CNSocket* sock);
|
void addEntityToChunks(std::set<Chunk*> chnks, const EntityRef& ref);
|
||||||
void addNPCToChunks(std::set<Chunk*> chnks, int32_t id);
|
void removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef& ref);
|
||||||
void removePlayerFromChunks(std::set<Chunk*> chnks, CNSocket* sock);
|
|
||||||
void removeNPCFromChunks(std::set<Chunk*> chnks, int32_t id);
|
|
||||||
|
|
||||||
bool chunkExists(ChunkPos chunk);
|
bool chunkExists(ChunkPos chunk);
|
||||||
ChunkPos chunkPosAt(int posX, int posY, uint64_t instanceID);
|
ChunkPos chunkPosAt(int posX, int posY, uint64_t instanceID);
|
||||||
@ -43,4 +41,18 @@ namespace Chunking {
|
|||||||
bool inPopulatedChunks(std::set<Chunk*>* chnks);
|
bool inPopulatedChunks(std::set<Chunk*>* chnks);
|
||||||
void createInstance(uint64_t);
|
void createInstance(uint64_t);
|
||||||
void destroyInstanceIfEmpty(uint64_t);
|
void destroyInstanceIfEmpty(uint64_t);
|
||||||
|
|
||||||
|
// death row below this point
|
||||||
|
//void updatePlayerChunk(CNSocket* sock, ChunkPos from, ChunkPos to);
|
||||||
|
//void updateNPCChunk(int32_t id, ChunkPos from, ChunkPos to);
|
||||||
|
|
||||||
|
//void trackPlayer(ChunkPos chunkPos, CNSocket* sock);
|
||||||
|
//void trackNPC(ChunkPos chunkPos, int32_t id);
|
||||||
|
//void untrackPlayer(ChunkPos chunkPos, CNSocket* sock);
|
||||||
|
//void untrackNPC(ChunkPos chunkPos, int32_t id);
|
||||||
|
|
||||||
|
//void addPlayerToChunks(std::set<Chunk*> chnks, CNSocket* sock);
|
||||||
|
//void addNPCToChunks(std::set<Chunk*> chnks, int32_t id);
|
||||||
|
//void removePlayerFromChunks(std::set<Chunk*> chnks, CNSocket* sock);
|
||||||
|
//void removeNPCFromChunks(std::set<Chunk*> chnks, int32_t id);
|
||||||
}
|
}
|
||||||
|
@ -361,11 +361,12 @@ static void npcRotateCommand(std::string full, std::vector<std::string>& args, C
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void refreshCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
|
static void refreshCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
|
||||||
Player* plr = PlayerManager::getPlayer(sock);
|
EntityRef ref = {sock};
|
||||||
|
Entity* plr = ref.getEntity();
|
||||||
ChunkPos currentChunk = plr->chunkPos;
|
ChunkPos currentChunk = plr->chunkPos;
|
||||||
ChunkPos nullChunk = std::make_tuple(0, 0, 0);
|
ChunkPos nullChunk = std::make_tuple(0, 0, 0);
|
||||||
Chunking::updatePlayerChunk(sock, currentChunk, nullChunk);
|
Chunking::updateEntityChunk(ref, currentChunk, nullChunk);
|
||||||
Chunking::updatePlayerChunk(sock, nullChunk, currentChunk);
|
Chunking::updateEntityChunk(ref, nullChunk, currentChunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void instanceCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
|
static void instanceCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
|
||||||
|
@ -133,7 +133,7 @@ static void eggStep(CNServer* serv, time_t currTime) {
|
|||||||
egg.second->deadUntil = 0;
|
egg.second->deadUntil = 0;
|
||||||
egg.second->appearanceData.iHP = 400;
|
egg.second->appearanceData.iHP = 400;
|
||||||
|
|
||||||
Chunking::addNPCToChunks(Chunking::getViewableChunks(egg.second->chunkPos), egg.first);
|
Chunking::addEntityToChunks(Chunking::getViewableChunks(egg.second->chunkPos), {egg.first});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,7 +246,7 @@ static void eggPickup(CNSocket* sock, CNPacketData* data) {
|
|||||||
if (egg->summoned)
|
if (egg->summoned)
|
||||||
NPCManager::destroyNPC(eggId);
|
NPCManager::destroyNPC(eggId);
|
||||||
else {
|
else {
|
||||||
Chunking::removeNPCFromChunks(Chunking::getViewableChunks(egg->chunkPos), eggId);
|
Chunking::removeEntityFromChunks(Chunking::getViewableChunks(egg->chunkPos), {eggId});
|
||||||
egg->dead = true;
|
egg->dead = true;
|
||||||
egg->deadUntil = getTime() + (time_t)type->regen * 1000;
|
egg->deadUntil = getTime() + (time_t)type->regen * 1000;
|
||||||
egg->appearanceData.iHP = 0;
|
egg->appearanceData.iHP = 0;
|
||||||
|
14
src/Eggs.hpp
14
src/Eggs.hpp
@ -1,19 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "core/Core.hpp"
|
#include "core/Core.hpp"
|
||||||
#include "NPC.hpp"
|
#include "Entities.hpp"
|
||||||
|
|
||||||
struct Egg : public BaseNPC {
|
|
||||||
bool summoned;
|
|
||||||
bool dead = false;
|
|
||||||
time_t deadUntil;
|
|
||||||
|
|
||||||
Egg(int x, int y, int z, uint64_t iID, int type, int32_t id, bool summon)
|
|
||||||
: BaseNPC(x, y, z, 0, iID, type, id) {
|
|
||||||
summoned = summon;
|
|
||||||
npcClass = NPCClass::NPC_EGG;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct EggType {
|
struct EggType {
|
||||||
int dropCrateId;
|
int dropCrateId;
|
||||||
|
120
src/Entities.cpp
Normal file
120
src/Entities.cpp
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
#include "core/Core.hpp"
|
||||||
|
#include "Entities.hpp"
|
||||||
|
#include "Chunking.hpp"
|
||||||
|
#include "PlayerManager.hpp"
|
||||||
|
#include "NPCManager.hpp"
|
||||||
|
#include "Eggs.hpp"
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
static_assert(std::is_standard_layout<EntityRef>::value);
|
||||||
|
static_assert(std::is_trivially_copyable<EntityRef>::value);
|
||||||
|
|
||||||
|
EntityRef::EntityRef(CNSocket *s) {
|
||||||
|
type = EntityType::PLAYER;
|
||||||
|
sock = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
EntityRef::EntityRef(int32_t i) {
|
||||||
|
id = i;
|
||||||
|
|
||||||
|
assert(NPCManager::NPCs.find(id) != NPCManager::NPCs.end());
|
||||||
|
type = NPCManager::NPCs[id]->type;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EntityRef::isValid() const {
|
||||||
|
if (type == EntityType::PLAYER)
|
||||||
|
return PlayerManager::players.find(sock) != PlayerManager::players.end();
|
||||||
|
|
||||||
|
return NPCManager::NPCs.find(id) != NPCManager::NPCs.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
Entity *EntityRef::getEntity() const {
|
||||||
|
assert(isValid());
|
||||||
|
|
||||||
|
if (type == EntityType::PLAYER)
|
||||||
|
return PlayerManager::getPlayer(sock);
|
||||||
|
|
||||||
|
return NPCManager::NPCs[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Entity coming into view.
|
||||||
|
*/
|
||||||
|
void BaseNPC::enterIntoViewOf(CNSocket *sock) {
|
||||||
|
INITSTRUCT(sP_FE2CL_NPC_ENTER, pkt);
|
||||||
|
pkt.NPCAppearanceData = appearanceData;
|
||||||
|
sock->sendPacket(pkt, P_FE2CL_NPC_ENTER);
|
||||||
|
|
||||||
|
playersInView++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bus::enterIntoViewOf(CNSocket *sock) {
|
||||||
|
INITSTRUCT(sP_FE2CL_TRANSPORTATION_ENTER, pkt);
|
||||||
|
|
||||||
|
// TODO: Potentially decouple this from BaseNPC?
|
||||||
|
pkt.AppearanceData = {
|
||||||
|
3, appearanceData.iNPC_ID, appearanceData.iNPCType,
|
||||||
|
appearanceData.iX, appearanceData.iY, appearanceData.iZ
|
||||||
|
};
|
||||||
|
|
||||||
|
sock->sendPacket(pkt, P_FE2CL_TRANSPORTATION_ENTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Egg::enterIntoViewOf(CNSocket *sock) {
|
||||||
|
INITSTRUCT(sP_FE2CL_SHINY_ENTER, pkt);
|
||||||
|
|
||||||
|
Eggs::npcDataToEggData(&appearanceData, &pkt.ShinyAppearanceData);
|
||||||
|
|
||||||
|
sock->sendPacket(pkt, P_FE2CL_SHINY_ENTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: this is less effiecient than it was, because of memset()
|
||||||
|
void Player::enterIntoViewOf(CNSocket *sock) {
|
||||||
|
INITSTRUCT(sP_FE2CL_PC_NEW, pkt);
|
||||||
|
|
||||||
|
pkt.PCAppearanceData.iID = iID;
|
||||||
|
pkt.PCAppearanceData.iHP = HP;
|
||||||
|
pkt.PCAppearanceData.iLv = level;
|
||||||
|
pkt.PCAppearanceData.iX = x;
|
||||||
|
pkt.PCAppearanceData.iY = y;
|
||||||
|
pkt.PCAppearanceData.iZ = z;
|
||||||
|
pkt.PCAppearanceData.iAngle = angle;
|
||||||
|
pkt.PCAppearanceData.PCStyle = PCStyle;
|
||||||
|
pkt.PCAppearanceData.Nano = Nanos[activeNano];
|
||||||
|
pkt.PCAppearanceData.iPCState = iPCState;
|
||||||
|
pkt.PCAppearanceData.iSpecialState = iSpecialState;
|
||||||
|
memcpy(pkt.PCAppearanceData.ItemEquip, Equip, sizeof(sItemBase) * AEQUIP_COUNT);
|
||||||
|
|
||||||
|
sock->sendPacket(pkt, P_FE2CL_PC_NEW);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Entity leaving view.
|
||||||
|
*/
|
||||||
|
void BaseNPC::disappearFromViewOf(CNSocket *sock) {
|
||||||
|
INITSTRUCT(sP_FE2CL_NPC_EXIT, pkt);
|
||||||
|
pkt.iNPC_ID = appearanceData.iNPC_ID;
|
||||||
|
sock->sendPacket(pkt, P_FE2CL_NPC_EXIT);
|
||||||
|
|
||||||
|
playersInView--;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bus::disappearFromViewOf(CNSocket *sock) {
|
||||||
|
INITSTRUCT(sP_FE2CL_TRANSPORTATION_EXIT, pkt);
|
||||||
|
pkt.eTT = 3;
|
||||||
|
pkt.iT_ID = appearanceData.iNPC_ID;
|
||||||
|
sock->sendPacket(pkt, P_FE2CL_TRANSPORTATION_EXIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Egg::disappearFromViewOf(CNSocket *sock) {
|
||||||
|
INITSTRUCT(sP_FE2CL_SHINY_EXIT, pkt);
|
||||||
|
pkt.iShinyID = appearanceData.iNPC_ID;
|
||||||
|
sock->sendPacket(pkt, P_FE2CL_SHINY_EXIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::disappearFromViewOf(CNSocket *sock) {
|
||||||
|
INITSTRUCT(sP_FE2CL_PC_EXIT, pkt);
|
||||||
|
pkt.iID = iID;
|
||||||
|
sock->sendPacket(pkt, P_FE2CL_PC_EXIT);
|
||||||
|
}
|
144
src/Entities.hpp
Normal file
144
src/Entities.hpp
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "core/Core.hpp"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
enum class EntityType {
|
||||||
|
PLAYER,
|
||||||
|
SIMPLE_NPC,
|
||||||
|
COMBAT_NPC,
|
||||||
|
EGG,
|
||||||
|
BUS
|
||||||
|
};
|
||||||
|
|
||||||
|
class Chunk;
|
||||||
|
|
||||||
|
struct Entity {
|
||||||
|
EntityType type;
|
||||||
|
int x, y, z;
|
||||||
|
uint64_t instanceID;
|
||||||
|
ChunkPos chunkPos;
|
||||||
|
std::set<Chunk*> viewableChunks;
|
||||||
|
|
||||||
|
// destructor must be virtual, apparently
|
||||||
|
virtual ~Entity() {}
|
||||||
|
|
||||||
|
virtual bool isAlive() { return true; }
|
||||||
|
|
||||||
|
// stubs
|
||||||
|
virtual void enterIntoViewOf(CNSocket *sock) {}
|
||||||
|
virtual void disappearFromViewOf(CNSocket *sock) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct EntityRef {
|
||||||
|
EntityType type;
|
||||||
|
union {
|
||||||
|
CNSocket *sock;
|
||||||
|
int32_t id;
|
||||||
|
};
|
||||||
|
|
||||||
|
EntityRef(CNSocket *s);
|
||||||
|
EntityRef(int32_t i);
|
||||||
|
|
||||||
|
bool isValid() const;
|
||||||
|
Entity *getEntity() const;
|
||||||
|
|
||||||
|
bool operator==(const EntityRef& other) const {
|
||||||
|
if (type != other.type)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (type == EntityType::PLAYER)
|
||||||
|
return sock == other.sock;
|
||||||
|
|
||||||
|
return id == other.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// arbitrary ordering
|
||||||
|
bool operator<(const EntityRef& other) const {
|
||||||
|
if (type == other.type) {
|
||||||
|
if (type == EntityType::PLAYER)
|
||||||
|
return sock < other.sock;
|
||||||
|
else
|
||||||
|
return id < other.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return type < other.type;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class BaseNPC : public Entity {
|
||||||
|
public:
|
||||||
|
sNPCAppearanceData appearanceData;
|
||||||
|
NPCClass npcClass;
|
||||||
|
|
||||||
|
int playersInView;
|
||||||
|
|
||||||
|
BaseNPC() {};
|
||||||
|
BaseNPC(int x, int y, int z, int angle, uint64_t iID, int type, int id) { // XXX
|
||||||
|
appearanceData.iX = x;
|
||||||
|
appearanceData.iY = y;
|
||||||
|
appearanceData.iZ = z;
|
||||||
|
appearanceData.iNPCType = type;
|
||||||
|
appearanceData.iHP = 400;
|
||||||
|
appearanceData.iAngle = angle;
|
||||||
|
appearanceData.iConditionBitFlag = 0;
|
||||||
|
appearanceData.iBarkerType = 0;
|
||||||
|
appearanceData.iNPC_ID = id;
|
||||||
|
|
||||||
|
npcClass = NPCClass::NPC_BASE;
|
||||||
|
|
||||||
|
instanceID = iID;
|
||||||
|
|
||||||
|
chunkPos = std::make_tuple(0, 0, 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) {
|
||||||
|
npcClass = classType;
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX: move to CombatNPC, probably
|
||||||
|
virtual bool isAlive() override { return appearanceData.iHP > 0; }
|
||||||
|
|
||||||
|
virtual void enterIntoViewOf(CNSocket *sock) override;
|
||||||
|
virtual void disappearFromViewOf(CNSocket *sock) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: decouple from BaseNPC
|
||||||
|
struct Egg : public BaseNPC {
|
||||||
|
bool summoned;
|
||||||
|
bool dead = false;
|
||||||
|
time_t deadUntil;
|
||||||
|
|
||||||
|
Egg(int x, int y, int z, uint64_t iID, int type, int32_t id, bool summon)
|
||||||
|
: BaseNPC(x, y, z, 0, iID, type, id) {
|
||||||
|
summoned = summon;
|
||||||
|
npcClass = NPCClass::NPC_EGG;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool isAlive() override { return !dead; }
|
||||||
|
|
||||||
|
virtual void enterIntoViewOf(CNSocket *sock) override;
|
||||||
|
virtual void disappearFromViewOf(CNSocket *sock) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: decouple from BaseNPC
|
||||||
|
struct Bus : public BaseNPC {
|
||||||
|
virtual void enterIntoViewOf(CNSocket *sock) override;
|
||||||
|
virtual void disappearFromViewOf(CNSocket *sock) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
struct NPC : public Entity {
|
||||||
|
void (*_stepAI)();
|
||||||
|
|
||||||
|
virtual void stepAI() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CombatNPC : public Entity {
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Mob : public CombatNPC {
|
||||||
|
};
|
||||||
|
#endif
|
35
src/NPC.hpp
35
src/NPC.hpp
@ -1,37 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Chunking.hpp"
|
#include "Chunking.hpp"
|
||||||
|
#include "Entities.hpp"
|
||||||
class BaseNPC {
|
|
||||||
public:
|
|
||||||
sNPCAppearanceData appearanceData;
|
|
||||||
NPCClass npcClass;
|
|
||||||
uint64_t instanceID;
|
|
||||||
ChunkPos chunkPos;
|
|
||||||
std::set<Chunk*> viewableChunks;
|
|
||||||
|
|
||||||
int playersInView;
|
|
||||||
|
|
||||||
BaseNPC() {};
|
|
||||||
BaseNPC(int x, int y, int z, int angle, uint64_t iID, int type, int id) {
|
|
||||||
appearanceData.iX = x;
|
|
||||||
appearanceData.iY = y;
|
|
||||||
appearanceData.iZ = z;
|
|
||||||
appearanceData.iNPCType = type;
|
|
||||||
appearanceData.iHP = 400;
|
|
||||||
appearanceData.iAngle = angle;
|
|
||||||
appearanceData.iConditionBitFlag = 0;
|
|
||||||
appearanceData.iBarkerType = 0;
|
|
||||||
appearanceData.iNPC_ID = id;
|
|
||||||
|
|
||||||
npcClass = NPCClass::NPC_BASE;
|
|
||||||
|
|
||||||
instanceID = iID;
|
|
||||||
|
|
||||||
chunkPos = std::make_tuple(0, 0, 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) {
|
|
||||||
npcClass = classType;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
@ -52,10 +52,11 @@ void NPCManager::destroyNPC(int32_t id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// remove NPC from the chunk
|
// remove NPC from the chunk
|
||||||
Chunking::untrackNPC(entity->chunkPos, id);
|
EntityRef ref = {id};
|
||||||
|
Chunking::untrackEntity(entity->chunkPos, ref);
|
||||||
|
|
||||||
// remove from viewable chunks
|
// remove from viewable chunks
|
||||||
Chunking::removeNPCFromChunks(Chunking::getViewableChunks(entity->chunkPos), id);
|
Chunking::removeEntityFromChunks(Chunking::getViewableChunks(entity->chunkPos), ref);
|
||||||
|
|
||||||
// remove from mob manager
|
// remove from mob manager
|
||||||
if (MobAI::Mobs.find(id) != MobAI::Mobs.end())
|
if (MobAI::Mobs.find(id) != MobAI::Mobs.end())
|
||||||
@ -81,7 +82,7 @@ void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z, uint64_t I,
|
|||||||
npc->instanceID = I;
|
npc->instanceID = I;
|
||||||
if (oldChunk == newChunk)
|
if (oldChunk == newChunk)
|
||||||
return; // didn't change chunks
|
return; // didn't change chunks
|
||||||
Chunking::updateNPCChunk(id, oldChunk, newChunk);
|
Chunking::updateEntityChunk({id}, oldChunk, newChunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
@ -251,7 +252,7 @@ static void handleWarp(CNSocket* sock, int32_t warpId) {
|
|||||||
Missions::failInstancedMissions(sock); // fail any instanced missions
|
Missions::failInstancedMissions(sock); // fail any instanced missions
|
||||||
sock->sendPacket(resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC);
|
sock->sendPacket(resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC);
|
||||||
|
|
||||||
Chunking::updatePlayerChunk(sock, plr->chunkPos, std::make_tuple(0, 0, 0)); // force player to reload chunks
|
Chunking::updateEntityChunk({sock}, plr->chunkPos, std::make_tuple(0, 0, 0)); // force player to reload chunks
|
||||||
PlayerManager::updatePlayerPosition(sock, resp.iX, resp.iY, resp.iZ, INSTANCE_OVERWORLD, plr->angle);
|
PlayerManager::updatePlayerPosition(sock, resp.iX, resp.iY, resp.iZ, INSTANCE_OVERWORLD, plr->angle);
|
||||||
|
|
||||||
// remove the player's ongoing race, if any
|
// remove the player's ongoing race, if any
|
||||||
|
@ -5,12 +5,13 @@
|
|||||||
|
|
||||||
#include "core/Core.hpp"
|
#include "core/Core.hpp"
|
||||||
#include "Chunking.hpp"
|
#include "Chunking.hpp"
|
||||||
|
#include "Entities.hpp"
|
||||||
|
|
||||||
#define ACTIVE_MISSION_COUNT 6
|
#define ACTIVE_MISSION_COUNT 6
|
||||||
|
|
||||||
#define PC_MAXHEALTH(level) (925 + 75 * (level))
|
#define PC_MAXHEALTH(level) (925 + 75 * (level))
|
||||||
|
|
||||||
struct Player {
|
struct Player : public Entity {
|
||||||
int accountId;
|
int accountId;
|
||||||
int accountLevel; // permission level (see CN_ACCOUNT_LEVEL enums)
|
int accountLevel; // permission level (see CN_ACCOUNT_LEVEL enums)
|
||||||
int64_t SerialKey;
|
int64_t SerialKey;
|
||||||
@ -37,10 +38,11 @@ struct Player {
|
|||||||
int32_t iSelfConditionBitFlag;
|
int32_t iSelfConditionBitFlag;
|
||||||
int8_t iSpecialState;
|
int8_t iSpecialState;
|
||||||
|
|
||||||
int x, y, z, angle;
|
//int x, y, z; in superclass
|
||||||
|
int angle;
|
||||||
int lastX, lastY, lastZ, lastAngle;
|
int lastX, lastY, lastZ, lastAngle;
|
||||||
int recallX, recallY, recallZ, recallInstance; // also Lair entrances
|
int recallX, recallY, recallZ, recallInstance; // also Lair entrances
|
||||||
uint64_t instanceID;
|
//uint64_t instanceID; in superclass
|
||||||
sItemBase Equip[AEQUIP_COUNT];
|
sItemBase Equip[AEQUIP_COUNT];
|
||||||
sItemBase Inven[AINVEN_COUNT];
|
sItemBase Inven[AINVEN_COUNT];
|
||||||
sItemBase Bank[ABANK_COUNT];
|
sItemBase Bank[ABANK_COUNT];
|
||||||
@ -82,11 +84,15 @@ struct Player {
|
|||||||
|
|
||||||
uint64_t iFirstUseFlag[2];
|
uint64_t iFirstUseFlag[2];
|
||||||
|
|
||||||
ChunkPos chunkPos;
|
// in superclass:
|
||||||
std::set<Chunk*> viewableChunks;
|
//ChunkPos chunkPos;
|
||||||
|
//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;
|
||||||
|
|
||||||
|
virtual void enterIntoViewOf(CNSocket *sock) override;
|
||||||
|
virtual void disappearFromViewOf(CNSocket *sock) override;
|
||||||
};
|
};
|
||||||
|
@ -31,10 +31,11 @@ std::map<CNSocket*, Player*> PlayerManager::players;
|
|||||||
static void addPlayer(CNSocket* key, Player plr) {
|
static void addPlayer(CNSocket* key, Player plr) {
|
||||||
Player *p = new Player();
|
Player *p = new Player();
|
||||||
|
|
||||||
memcpy(p, &plr, sizeof(Player));
|
// copy object into heap memory
|
||||||
|
*p = plr;
|
||||||
|
|
||||||
players[key] = p;
|
players[key] = p;
|
||||||
p->chunkPos = std::make_tuple(0, 0, 0);
|
p->chunkPos = std::make_tuple(0, 0, 0); // TODO: maybe replace with specialized "no chunk" value
|
||||||
p->lastHeartbeat = 0;
|
p->lastHeartbeat = 0;
|
||||||
|
|
||||||
std::cout << getPlayerName(p) << " has joined!" << std::endl;
|
std::cout << getPlayerName(p) << " has joined!" << std::endl;
|
||||||
@ -56,9 +57,10 @@ void PlayerManager::removePlayer(CNSocket* key) {
|
|||||||
// save player to DB
|
// save player to DB
|
||||||
Database::updatePlayer(plr);
|
Database::updatePlayer(plr);
|
||||||
|
|
||||||
|
EntityRef ref = {key};
|
||||||
// remove player visually and untrack
|
// remove player visually and untrack
|
||||||
Chunking::removePlayerFromChunks(Chunking::getViewableChunks(plr->chunkPos), key);
|
Chunking::removeEntityFromChunks(Chunking::getViewableChunks(plr->chunkPos), ref);
|
||||||
Chunking::untrackPlayer(plr->chunkPos, key);
|
Chunking::untrackEntity(plr->chunkPos, ref);
|
||||||
|
|
||||||
std::cout << getPlayerName(plr) << " has left!" << std::endl;
|
std::cout << getPlayerName(plr) << " has left!" << std::endl;
|
||||||
|
|
||||||
@ -92,7 +94,7 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z, ui
|
|||||||
plr->instanceID = I;
|
plr->instanceID = I;
|
||||||
if (oldChunk == newChunk)
|
if (oldChunk == newChunk)
|
||||||
return; // didn't change chunks
|
return; // didn't change chunks
|
||||||
Chunking::updatePlayerChunk(sock, oldChunk, newChunk);
|
Chunking::updateEntityChunk({sock}, oldChunk, newChunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerManager::sendPlayerTo(CNSocket* sock, int X, int Y, int Z, uint64_t I) {
|
void PlayerManager::sendPlayerTo(CNSocket* sock, int X, int Y, int Z, uint64_t I) {
|
||||||
@ -146,7 +148,7 @@ void PlayerManager::sendPlayerTo(CNSocket* sock, int X, int Y, int Z, uint64_t I
|
|||||||
pkt2.iZ = Z;
|
pkt2.iZ = Z;
|
||||||
sock->sendPacket(pkt2, P_FE2CL_REP_PC_GOTO_SUCC);
|
sock->sendPacket(pkt2, P_FE2CL_REP_PC_GOTO_SUCC);
|
||||||
|
|
||||||
Chunking::updatePlayerChunk(sock, plr->chunkPos, std::make_tuple(0, 0, 0)); // force player to reload chunks
|
Chunking::updateEntityChunk({sock}, plr->chunkPos, std::make_tuple(0, 0, 0)); // force player to reload chunks
|
||||||
updatePlayerPosition(sock, X, Y, Z, I, plr->angle);
|
updatePlayerPosition(sock, X, Y, Z, I, plr->angle);
|
||||||
|
|
||||||
// post-warp: check if the source instance has no more players in it and delete it if so
|
// post-warp: check if the source instance has no more players in it and delete it if so
|
||||||
@ -472,7 +474,7 @@ static void revivePlayer(CNSocket* sock, CNPacketData* data) {
|
|||||||
if (!move)
|
if (!move)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Chunking::updatePlayerChunk(sock, plr->chunkPos, std::make_tuple(0, 0, 0)); // force player to reload chunks
|
Chunking::updateEntityChunk({sock}, plr->chunkPos, std::make_tuple(0, 0, 0)); // force player to reload chunks
|
||||||
updatePlayerPosition(sock, x, y, z, plr->instanceID, plr->angle);
|
updatePlayerPosition(sock, x, y, z, plr->instanceID, plr->angle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +165,7 @@ static void transportWarpHandler(CNSocket* sock, CNPacketData* data) {
|
|||||||
if (target == nullptr)
|
if (target == nullptr)
|
||||||
return;
|
return;
|
||||||
// we warped; update position and chunks
|
// we warped; update position and chunks
|
||||||
Chunking::updatePlayerChunk(sock, plr->chunkPos, std::make_tuple(0, 0, 0)); // force player to reload chunks
|
Chunking::updateEntityChunk({sock}, plr->chunkPos, std::make_tuple(0, 0, 0)); // force player to reload chunks
|
||||||
PlayerManager::updatePlayerPosition(sock, target->x, target->y, target->z, INSTANCE_OVERWORLD, plr->angle);
|
PlayerManager::updatePlayerPosition(sock, target->x, target->y, target->z, INSTANCE_OVERWORLD, plr->angle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user