Refined culling logic, added periodic query
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
#include "barrier_manager.hpp"
|
#include "barrier_manager.hpp"
|
||||||
|
|
||||||
void BarrierManager::DrawTo(SDL_Renderer* const dest, Sint16 x, Sint16 y, double scaleX, double scaleY) {
|
void BarrierManager::DrawTo(SDL_Renderer* const dest, Sint16 x, Sint16 y, double scaleX, double scaleY) {
|
||||||
for (auto& it : barrierMap) {
|
for (auto& it : elementMap) {
|
||||||
it.second.DrawTo(dest, x, y);
|
it.second.DrawTo(dest, x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,26 +47,38 @@ void BarrierManager::UnloadTemplateImages() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BaseBarrier* BarrierManager::Create(int index) {
|
BaseBarrier* BarrierManager::Create(int index) {
|
||||||
barrierMap.emplace(index, BaseBarrier(baseImage, templateImages));
|
elementMap.emplace(index, BaseBarrier(baseImage, templateImages));
|
||||||
return &barrierMap[index];
|
return &elementMap[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
void BarrierManager::Unload(int i) {
|
void BarrierManager::Unload(int i) {
|
||||||
barrierMap.erase(i);
|
elementMap.erase(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BarrierManager::UnloadAll() {
|
void BarrierManager::UnloadAll() {
|
||||||
barrierMap.clear();
|
elementMap.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BarrierManager::UnloadIf(std::function<bool(std::pair<const int, BaseBarrier const&>)> fn) {
|
||||||
|
std::map<int, BaseBarrier>::iterator it = elementMap.begin();
|
||||||
|
while (it != elementMap.end()) {
|
||||||
|
if (fn(*it)) {
|
||||||
|
it = elementMap.erase(it);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int BarrierManager::Size() {
|
int BarrierManager::Size() {
|
||||||
return barrierMap.size();
|
return elementMap.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseBarrier* BarrierManager::Find(int i) {
|
BaseBarrier* BarrierManager::Find(int i) {
|
||||||
std::map<int, BaseBarrier>::iterator it = barrierMap.find(i);
|
std::map<int, BaseBarrier>::iterator it = elementMap.find(i);
|
||||||
|
|
||||||
if (it == barrierMap.end()) {
|
if (it == elementMap.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +86,7 @@ BaseBarrier* BarrierManager::Find(int i) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::map<int, BaseBarrier>* BarrierManager::GetContainer() {
|
std::map<int, BaseBarrier>* BarrierManager::GetContainer() {
|
||||||
return &barrierMap;
|
return &elementMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, Image>* BarrierManager::GetTemplateContainer() {
|
std::map<std::string, Image>* BarrierManager::GetTemplateContainer() {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ public:
|
|||||||
BaseBarrier* Create(int index);
|
BaseBarrier* Create(int index);
|
||||||
void Unload(int i);
|
void Unload(int i);
|
||||||
void UnloadAll();
|
void UnloadAll();
|
||||||
|
void UnloadIf(std::function<bool(std::pair<const int, BaseBarrier const&>)> fn);
|
||||||
|
|
||||||
int Size();
|
int Size();
|
||||||
|
|
||||||
@@ -52,5 +53,5 @@ public:
|
|||||||
private:
|
private:
|
||||||
Image baseImage;
|
Image baseImage;
|
||||||
std::map<std::string, Image> templateImages;
|
std::map<std::string, Image> templateImages;
|
||||||
std::map<int, BaseBarrier> barrierMap;
|
std::map<int, BaseBarrier> elementMap;
|
||||||
};
|
};
|
||||||
+40
-27
@@ -185,7 +185,27 @@ void World::Update() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: (1) regular query interval
|
//TODO: (0) regular query interval
|
||||||
|
if (Clock::now() - queryTime > std::chrono::seconds(3)) {
|
||||||
|
queryTime = Clock::now();
|
||||||
|
//query the world state (room)
|
||||||
|
CharacterPacket characterPacket;
|
||||||
|
memset(&characterPacket, 0, MAX_PACKET_SIZE);
|
||||||
|
characterPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS;
|
||||||
|
characterPacket.roomIndex = roomIndex;
|
||||||
|
network.SendTo(Channels::SERVER, &characterPacket);
|
||||||
|
|
||||||
|
CreaturePacket creaturePacket;
|
||||||
|
creaturePacket.type = SerialPacketType::QUERY_CREATURE_EXISTS;
|
||||||
|
creaturePacket.roomIndex = roomIndex;
|
||||||
|
network.SendTo(Channels::SERVER, &creaturePacket);
|
||||||
|
|
||||||
|
BarrierPacket barrierPacket;
|
||||||
|
barrierPacket.type = SerialPacketType::QUERY_BARRIER_EXISTS;
|
||||||
|
barrierPacket.roomIndex = roomIndex;
|
||||||
|
network.SendTo(Channels::SERVER, &barrierPacket);
|
||||||
|
}
|
||||||
|
|
||||||
//cull creatures
|
//cull creatures
|
||||||
for (std::map<int, BaseCreature>::iterator it = creatureMap.begin(); it != creatureMap.end(); /* */) {
|
for (std::map<int, BaseCreature>::iterator it = creatureMap.begin(); it != creatureMap.end(); /* */) {
|
||||||
if ( (localCharacter->GetOrigin() - it->second.GetOrigin()).Length() > INFLUENCE_RADIUS) {
|
if ( (localCharacter->GetOrigin() - it->second.GetOrigin()).Length() > INFLUENCE_RADIUS) {
|
||||||
@@ -196,7 +216,10 @@ void World::Update() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: cull barriers
|
//cull barriers
|
||||||
|
barrierMgr.UnloadIf([&](std::pair<const int, BaseBarrier const&> barrierIt) -> bool {
|
||||||
|
return (localCharacter->GetOrigin() - barrierIt.second.GetOrigin()).Length() > INFLUENCE_RADIUS;
|
||||||
|
});
|
||||||
|
|
||||||
//get the collidable boxes
|
//get the collidable boxes
|
||||||
std::list<BoundingBox> boxList = GenerateCollisionGrid(localCharacter, tileSheet.GetTileW(), tileSheet.GetTileH());
|
std::list<BoundingBox> boxList = GenerateCollisionGrid(localCharacter, tileSheet.GetTileW(), tileSheet.GetTileH());
|
||||||
@@ -727,6 +750,9 @@ void World::hCharacterCreate(CharacterPacket* const argPacket) {
|
|||||||
if (character->GetOwner() == accountIndex) {
|
if (character->GetOwner() == accountIndex) {
|
||||||
localCharacter = static_cast<LocalCharacter*>(character);
|
localCharacter = static_cast<LocalCharacter*>(character);
|
||||||
|
|
||||||
|
//reset queries
|
||||||
|
queryTime = Clock::now() - std::chrono::seconds(4); //back 4 seconds to trigger automatically
|
||||||
|
|
||||||
//focus the camera on this character's sprite
|
//focus the camera on this character's sprite
|
||||||
camera.marginX = (camera.width / 2 - localCharacter->GetSprite()->GetClipW() / 2);
|
camera.marginX = (camera.width / 2 - localCharacter->GetSprite()->GetClipW() / 2);
|
||||||
camera.marginY = (camera.height/ 2 - localCharacter->GetSprite()->GetClipH() / 2);
|
camera.marginY = (camera.height/ 2 - localCharacter->GetSprite()->GetClipH() / 2);
|
||||||
@@ -734,23 +760,6 @@ void World::hCharacterCreate(CharacterPacket* const argPacket) {
|
|||||||
//focus on this character's info
|
//focus on this character's info
|
||||||
characterIndex = argPacket->characterIndex;
|
characterIndex = argPacket->characterIndex;
|
||||||
roomIndex = argPacket->roomIndex;
|
roomIndex = argPacket->roomIndex;
|
||||||
|
|
||||||
//query the world state (room)
|
|
||||||
CharacterPacket characterPacket;
|
|
||||||
memset(&characterPacket, 0, MAX_PACKET_SIZE);
|
|
||||||
characterPacket.type = SerialPacketType::QUERY_CHARACTER_EXISTS;
|
|
||||||
characterPacket.roomIndex = roomIndex;
|
|
||||||
network.SendTo(Channels::SERVER, &characterPacket);
|
|
||||||
|
|
||||||
CreaturePacket creaturePacket;
|
|
||||||
creaturePacket.type = SerialPacketType::QUERY_CREATURE_EXISTS;
|
|
||||||
creaturePacket.roomIndex = roomIndex;
|
|
||||||
network.SendTo(Channels::SERVER, &creaturePacket);
|
|
||||||
|
|
||||||
BarrierPacket barrierPacket;
|
|
||||||
barrierPacket.type = SerialPacketType::QUERY_BARRIER_EXISTS;
|
|
||||||
barrierPacket.roomIndex = roomIndex;
|
|
||||||
network.SendTo(Channels::SERVER, &barrierPacket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//debug
|
//debug
|
||||||
@@ -791,12 +800,12 @@ void World::hCharacterUnload(CharacterPacket* const argPacket) {
|
|||||||
void World::hQueryCharacterExists(CharacterPacket* const argPacket) {
|
void World::hQueryCharacterExists(CharacterPacket* const argPacket) {
|
||||||
//prevent a double message about this player's character
|
//prevent a double message about this player's character
|
||||||
//TODO: why is this commented out?
|
//TODO: why is this commented out?
|
||||||
// if (argPacket->accountIndex == accountIndex) {
|
if (argPacket->accountIndex == accountIndex) {
|
||||||
// return;
|
return;
|
||||||
// }
|
}
|
||||||
|
|
||||||
//ignore characters in a different room (sub-optimal)
|
//ignore characters in a different room (sub-optimal)
|
||||||
if (argPacket->roomIndex != roomIndex) {
|
if (argPacket->roomIndex != roomIndex || (localCharacter->GetOrigin() - argPacket->origin).Length() > INFLUENCE_RADIUS) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -913,10 +922,12 @@ void World::hCreatureUnload(CreaturePacket* const argPacket) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void World::hQueryCreatureExists(CreaturePacket* const argPacket) {
|
void World::hQueryCreatureExists(CreaturePacket* const argPacket) {
|
||||||
std::cout << "Creature Query" << std::endl;
|
if (!localCharacter) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//ignore creatures in a different room (sub-optimal)
|
//ignore creatures in a different room (sub-optimal)
|
||||||
if (argPacket->roomIndex != roomIndex) {
|
if (argPacket->roomIndex != roomIndex || (localCharacter->GetOrigin() - argPacket->origin).Length() > INFLUENCE_RADIUS) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1021,10 +1032,12 @@ void World::hBarrierUnload(BarrierPacket* const argPacket) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void World::hQueryBarrierExists(BarrierPacket* const argPacket) {
|
void World::hQueryBarrierExists(BarrierPacket* const argPacket) {
|
||||||
std::cout << "Barrier Query" << std::endl;
|
if (!localCharacter) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//ignore barriers in a different room (sub-optimal)
|
//ignore barriers in a different room (sub-optimal)
|
||||||
if (argPacket->roomIndex != roomIndex) {
|
if (argPacket->roomIndex != roomIndex || (localCharacter->GetOrigin() - argPacket->origin).Length() > INFLUENCE_RADIUS) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -168,6 +168,7 @@ private:
|
|||||||
//TODO: (2) Heartbeat needs it's own utility
|
//TODO: (2) Heartbeat needs it's own utility
|
||||||
typedef std::chrono::steady_clock Clock;
|
typedef std::chrono::steady_clock Clock;
|
||||||
Clock::time_point lastBeat = Clock::now();
|
Clock::time_point lastBeat = Clock::now();
|
||||||
|
Clock::time_point queryTime = Clock::now() - std::chrono::seconds(4); //back 4 seconds to trigger automatically
|
||||||
int attemptedBeats = 0;
|
int attemptedBeats = 0;
|
||||||
|
|
||||||
//ugly references; I hate this
|
//ugly references; I hate this
|
||||||
|
|||||||
Reference in New Issue
Block a user