mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-22 05:20:05 +00:00
Compare commits
1 Commits
fffdbf2af6
...
a87c8aadbd
Author | SHA1 | Date | |
---|---|---|---|
a87c8aadbd |
1
Makefile
1
Makefile
@ -115,7 +115,6 @@ CXXHDR=\
|
|||||||
src/settings.hpp\
|
src/settings.hpp\
|
||||||
src/Transport.hpp\
|
src/Transport.hpp\
|
||||||
src/TableData.hpp\
|
src/TableData.hpp\
|
||||||
src/Bucket.hpp\
|
|
||||||
src/Chunking.hpp\
|
src/Chunking.hpp\
|
||||||
src/Buddies.hpp\
|
src/Buddies.hpp\
|
||||||
src/Groups.hpp\
|
src/Groups.hpp\
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <optional>
|
|
||||||
|
|
||||||
template<class T, size_t N>
|
|
||||||
class Bucket {
|
|
||||||
std::array<T, N> buf;
|
|
||||||
size_t sz;
|
|
||||||
public:
|
|
||||||
Bucket() {
|
|
||||||
sz = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void add(const T& item) {
|
|
||||||
if (sz < N) {
|
|
||||||
buf[sz++] = item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<T> get(size_t idx) const {
|
|
||||||
if (idx < sz) {
|
|
||||||
return buf[idx];
|
|
||||||
}
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size() const {
|
|
||||||
return sz;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isFull() const {
|
|
||||||
return sz == N;
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear() {
|
|
||||||
sz = 0;
|
|
||||||
}
|
|
||||||
};
|
|
114
src/Chunking.cpp
114
src/Chunking.cpp
@ -3,7 +3,6 @@
|
|||||||
#include "Player.hpp"
|
#include "Player.hpp"
|
||||||
#include "MobAI.hpp"
|
#include "MobAI.hpp"
|
||||||
#include "NPCManager.hpp"
|
#include "NPCManager.hpp"
|
||||||
#include "Bucket.hpp"
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
@ -13,11 +12,11 @@ using namespace Chunking;
|
|||||||
* The initial chunkPos value before a player is placed into the world.
|
* The initial chunkPos value before a player is placed into the world.
|
||||||
*/
|
*/
|
||||||
const ChunkPos Chunking::INVALID_CHUNK = {};
|
const ChunkPos Chunking::INVALID_CHUNK = {};
|
||||||
constexpr size_t MAX_PC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sPCAppearanceData);
|
constexpr size_t MAX_PC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4) / sizeof(sPCAppearanceData);
|
||||||
constexpr size_t MAX_NPC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sNPCAppearanceData);
|
constexpr size_t MAX_NPC_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4) / sizeof(sNPCAppearanceData);
|
||||||
constexpr size_t MAX_SHINY_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sShinyAppearanceData);
|
constexpr size_t MAX_SHINY_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4) / sizeof(sShinyAppearanceData);
|
||||||
constexpr size_t MAX_TRANSPORTATION_PER_AROUND = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(sTransportationAppearanceData);
|
constexpr size_t MAX_TRANSPORTATION_PER_AROUND = (CN_PACKET_BUFFER_SIZE - 4) / sizeof(sTransportationAppearanceData);
|
||||||
constexpr size_t MAX_IDS_PER_AROUND_DEL = (CN_PACKET_BUFFER_SIZE - sizeof(int32_t)) / sizeof(int32_t);
|
constexpr size_t MAX_IDS_PER_AROUND_DEL = (CN_PACKET_BUFFER_SIZE - 4) / sizeof(int32_t);
|
||||||
|
|
||||||
std::map<ChunkPos, Chunk*> Chunking::chunks;
|
std::map<ChunkPos, Chunk*> Chunking::chunks;
|
||||||
|
|
||||||
@ -82,32 +81,36 @@ void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef ref) {
|
|||||||
deleteChunk(chunkPos);
|
deleteChunk(chunkPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T, size_t N>
|
template<class T>
|
||||||
static void sendAroundPacket(const EntityRef recipient, std::vector<Bucket<T, N>>& buckets, uint32_t packetId) {
|
static void sendAroundPacket(const EntityRef recipient, std::vector<std::vector<T>>& slices, size_t maxCnt, uint32_t packetId) {
|
||||||
assert(recipient.kind == EntityKind::PLAYER);
|
assert(recipient.kind == EntityKind::PLAYER);
|
||||||
|
|
||||||
uint8_t pktBuf[CN_PACKET_BUFFER_SIZE];
|
uint8_t pktBuf[CN_PACKET_BUFFER_SIZE];
|
||||||
for (const auto& bucket : buckets) {
|
for (const auto& slice : slices) {
|
||||||
memset(pktBuf, 0, CN_PACKET_BUFFER_SIZE);
|
memset(pktBuf, 0, CN_PACKET_BUFFER_SIZE);
|
||||||
int count = bucket.size();
|
int count = slice.size();
|
||||||
|
assert(count <= maxCnt);
|
||||||
*((int32_t*)pktBuf) = count;
|
*((int32_t*)pktBuf) = count;
|
||||||
T* data = (T*)(pktBuf + sizeof(int32_t));
|
T* data = (T*)(pktBuf + sizeof(int32_t));
|
||||||
for (size_t i = 0; i < count; i++) {
|
for (size_t i = 0; i < count; i++) {
|
||||||
data[i] = bucket.get(i).value();
|
data[i] = slice[i];
|
||||||
}
|
}
|
||||||
recipient.sock->sendPacket(pktBuf, packetId, sizeof(int32_t) + (count * sizeof(T)));
|
recipient.sock->sendPacket(pktBuf, packetId, sizeof(int32_t) + (count * sizeof(T)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t N>
|
static void sendAroundDelPacket(const EntityRef recipient, std::vector<std::vector<int32_t>>& slices, bool isTransportation, uint32_t packetId) {
|
||||||
static void sendAroundDelPacket(const EntityRef recipient, std::vector<Bucket<int32_t, N>>& buckets, bool isTransportation, uint32_t packetId) {
|
|
||||||
assert(recipient.kind == EntityKind::PLAYER);
|
assert(recipient.kind == EntityKind::PLAYER);
|
||||||
|
|
||||||
|
size_t maxCnt = MAX_IDS_PER_AROUND_DEL;
|
||||||
|
if (isTransportation)
|
||||||
|
maxCnt -= 1; // account for eTT. sad.
|
||||||
|
|
||||||
uint8_t pktBuf[CN_PACKET_BUFFER_SIZE];
|
uint8_t pktBuf[CN_PACKET_BUFFER_SIZE];
|
||||||
for (const auto& bucket : buckets) {
|
for (const auto& slice : slices) {
|
||||||
memset(pktBuf, 0, CN_PACKET_BUFFER_SIZE);
|
memset(pktBuf, 0, CN_PACKET_BUFFER_SIZE);
|
||||||
int count = bucket.size();
|
int count = slice.size();
|
||||||
assert(count <= N);
|
assert(count <= maxCnt);
|
||||||
|
|
||||||
size_t baseSize;
|
size_t baseSize;
|
||||||
if (isTransportation) {
|
if (isTransportation) {
|
||||||
@ -122,42 +125,39 @@ static void sendAroundDelPacket(const EntityRef recipient, std::vector<Bucket<in
|
|||||||
int32_t* ids = (int32_t*)(pktBuf + baseSize);
|
int32_t* ids = (int32_t*)(pktBuf + baseSize);
|
||||||
|
|
||||||
for (size_t i = 0; i < count; i++) {
|
for (size_t i = 0; i < count; i++) {
|
||||||
ids[i] = bucket.get(i).value();
|
ids[i] = slice[i];
|
||||||
}
|
}
|
||||||
recipient.sock->sendPacket(pktBuf, packetId, baseSize + (count * sizeof(int32_t)));
|
recipient.sock->sendPacket(pktBuf, packetId, baseSize + (count * sizeof(int32_t)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T, size_t N>
|
template<class T>
|
||||||
static void bufferAppearanceData(std::vector<Bucket<T, N>>& buckets, const T& data) {
|
static void bufferAppearanceData(std::vector<std::vector<T>>& slices, const T& data, size_t maxCnt) {
|
||||||
if (buckets.empty())
|
if (slices.empty())
|
||||||
buckets.push_back(Bucket<T, N>());
|
slices.push_back(std::vector<T>());
|
||||||
Bucket<T, N>& bucket = buckets[buckets.size() - 1];
|
std::vector<T>& slice = slices[slices.size() - 1];
|
||||||
assert(!bucket.isFull());
|
slice.push_back(data);
|
||||||
bucket.add(data);
|
if (slice.size() == maxCnt)
|
||||||
if (bucket.isFull())
|
slices.push_back(std::vector<T>());
|
||||||
buckets.push_back(Bucket<T, N>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t N>
|
static void bufferIdForDisappearance(std::vector<std::vector<int32_t>>& slices, int32_t id, size_t maxCnt) {
|
||||||
static void bufferIdForDisappearance(std::vector<Bucket<int32_t, N>>& buckets, int32_t id) {
|
if (slices.empty())
|
||||||
if (buckets.empty())
|
slices.push_back(std::vector<int32_t>());
|
||||||
buckets.push_back(Bucket<int32_t, N>());
|
std::vector<int32_t>& slice = slices[slices.size() - 1];
|
||||||
Bucket<int32_t, N>& bucket = buckets[buckets.size() - 1];
|
slice.push_back(id);
|
||||||
assert(!bucket.isFull());
|
if (slice.size() == maxCnt)
|
||||||
bucket.add(id);
|
slices.push_back(std::vector<int32_t>());
|
||||||
if (bucket.isFull())
|
|
||||||
buckets.push_back(Bucket<int32_t, N>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
|
void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
|
||||||
Entity *ent = ref.getEntity();
|
Entity *ent = ref.getEntity();
|
||||||
bool alive = ent->isExtant();
|
bool alive = ent->isExtant();
|
||||||
|
|
||||||
std::vector<Bucket<sPCAppearanceData, MAX_PC_PER_AROUND>> pcAppearances;
|
std::vector<std::vector<sPCAppearanceData>> pcAppearances;
|
||||||
std::vector<Bucket<sNPCAppearanceData, MAX_NPC_PER_AROUND>> npcAppearances;
|
std::vector<std::vector<sNPCAppearanceData>> npcAppearances;
|
||||||
std::vector<Bucket<sShinyAppearanceData, MAX_SHINY_PER_AROUND>> shinyAppearances;
|
std::vector<std::vector<sShinyAppearanceData>> shinyAppearances;
|
||||||
std::vector<Bucket<sTransportationAppearanceData, MAX_TRANSPORTATION_PER_AROUND>> transportationAppearances;
|
std::vector<std::vector<sTransportationAppearanceData>> transportationAppearances;
|
||||||
for (Chunk *chunk : chnks) {
|
for (Chunk *chunk : chnks) {
|
||||||
for (const EntityRef otherRef : chunk->entities) {
|
for (const EntityRef otherRef : chunk->entities) {
|
||||||
// skip oneself
|
// skip oneself
|
||||||
@ -181,27 +181,27 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
|
|||||||
{
|
{
|
||||||
case EntityKind::PLAYER:
|
case EntityKind::PLAYER:
|
||||||
pcData = dynamic_cast<Player*>(other)->getAppearanceData();
|
pcData = dynamic_cast<Player*>(other)->getAppearanceData();
|
||||||
bufferAppearanceData(pcAppearances, pcData);
|
bufferAppearanceData(pcAppearances, pcData, MAX_PC_PER_AROUND);
|
||||||
break;
|
break;
|
||||||
case EntityKind::SIMPLE_NPC:
|
case EntityKind::SIMPLE_NPC:
|
||||||
npcData = dynamic_cast<BaseNPC*>(other)->getAppearanceData();
|
npcData = dynamic_cast<BaseNPC*>(other)->getAppearanceData();
|
||||||
bufferAppearanceData(npcAppearances, npcData);
|
bufferAppearanceData(npcAppearances, npcData, MAX_NPC_PER_AROUND);
|
||||||
break;
|
break;
|
||||||
case EntityKind::COMBAT_NPC:
|
case EntityKind::COMBAT_NPC:
|
||||||
npcData = dynamic_cast<CombatNPC*>(other)->getAppearanceData();
|
npcData = dynamic_cast<CombatNPC*>(other)->getAppearanceData();
|
||||||
bufferAppearanceData(npcAppearances, npcData);
|
bufferAppearanceData(npcAppearances, npcData, MAX_NPC_PER_AROUND);
|
||||||
break;
|
break;
|
||||||
case EntityKind::MOB:
|
case EntityKind::MOB:
|
||||||
npcData = dynamic_cast<Mob*>(other)->getAppearanceData();
|
npcData = dynamic_cast<Mob*>(other)->getAppearanceData();
|
||||||
bufferAppearanceData(npcAppearances, npcData);
|
bufferAppearanceData(npcAppearances, npcData, MAX_NPC_PER_AROUND);
|
||||||
break;
|
break;
|
||||||
case EntityKind::EGG:
|
case EntityKind::EGG:
|
||||||
eggData = dynamic_cast<Egg*>(other)->getShinyAppearanceData();
|
eggData = dynamic_cast<Egg*>(other)->getShinyAppearanceData();
|
||||||
bufferAppearanceData(shinyAppearances, eggData);
|
bufferAppearanceData(shinyAppearances, eggData, MAX_SHINY_PER_AROUND);
|
||||||
break;
|
break;
|
||||||
case EntityKind::BUS:
|
case EntityKind::BUS:
|
||||||
busData = dynamic_cast<Bus*>(other)->getTransportationAppearanceData();
|
busData = dynamic_cast<Bus*>(other)->getTransportationAppearanceData();
|
||||||
bufferAppearanceData(transportationAppearances, busData);
|
bufferAppearanceData(transportationAppearances, busData, MAX_TRANSPORTATION_PER_AROUND);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -220,23 +220,23 @@ void Chunking::addEntityToChunks(std::set<Chunk*> chnks, const EntityRef ref) {
|
|||||||
return; // nothing to send
|
return; // nothing to send
|
||||||
|
|
||||||
if (!pcAppearances.empty())
|
if (!pcAppearances.empty())
|
||||||
sendAroundPacket(ref, pcAppearances, P_FE2CL_PC_AROUND);
|
sendAroundPacket(ref, pcAppearances, MAX_PC_PER_AROUND, P_FE2CL_PC_AROUND);
|
||||||
if (!npcAppearances.empty())
|
if (!npcAppearances.empty())
|
||||||
sendAroundPacket(ref, npcAppearances, P_FE2CL_NPC_AROUND);
|
sendAroundPacket(ref, npcAppearances, MAX_NPC_PER_AROUND, P_FE2CL_NPC_AROUND);
|
||||||
if (!shinyAppearances.empty())
|
if (!shinyAppearances.empty())
|
||||||
sendAroundPacket(ref, shinyAppearances, P_FE2CL_SHINY_AROUND);
|
sendAroundPacket(ref, shinyAppearances, MAX_SHINY_PER_AROUND, P_FE2CL_SHINY_AROUND);
|
||||||
if (!transportationAppearances.empty())
|
if (!transportationAppearances.empty())
|
||||||
sendAroundPacket(ref, transportationAppearances, P_FE2CL_TRANSPORTATION_AROUND);
|
sendAroundPacket(ref, transportationAppearances, MAX_TRANSPORTATION_PER_AROUND, P_FE2CL_TRANSPORTATION_AROUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef ref) {
|
void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef ref) {
|
||||||
Entity *ent = ref.getEntity();
|
Entity *ent = ref.getEntity();
|
||||||
bool alive = ent->isExtant();
|
bool alive = ent->isExtant();
|
||||||
|
|
||||||
std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL>> pcDisappearances;
|
std::vector<std::vector<int32_t>> pcDisappearances;
|
||||||
std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL>> npcDisappearances;
|
std::vector<std::vector<int32_t>> npcDisappearances;
|
||||||
std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL>> shinyDisappearances;
|
std::vector<std::vector<int32_t>> shinyDisappearances;
|
||||||
std::vector<Bucket<int32_t, MAX_IDS_PER_AROUND_DEL - 1>> transportationDisappearances;
|
std::vector<std::vector<int32_t>> transportationDisappearances;
|
||||||
for (Chunk *chunk : chnks) {
|
for (Chunk *chunk : chnks) {
|
||||||
for (const EntityRef otherRef : chunk->entities) {
|
for (const EntityRef otherRef : chunk->entities) {
|
||||||
// skip oneself
|
// skip oneself
|
||||||
@ -257,21 +257,21 @@ void Chunking::removeEntityFromChunks(std::set<Chunk*> chnks, const EntityRef re
|
|||||||
{
|
{
|
||||||
case EntityKind::PLAYER:
|
case EntityKind::PLAYER:
|
||||||
id = dynamic_cast<Player*>(other)->iID;
|
id = dynamic_cast<Player*>(other)->iID;
|
||||||
bufferIdForDisappearance(pcDisappearances, id);
|
bufferIdForDisappearance(pcDisappearances, id, MAX_IDS_PER_AROUND_DEL);
|
||||||
break;
|
break;
|
||||||
case EntityKind::SIMPLE_NPC:
|
case EntityKind::SIMPLE_NPC:
|
||||||
case EntityKind::COMBAT_NPC:
|
case EntityKind::COMBAT_NPC:
|
||||||
case EntityKind::MOB:
|
case EntityKind::MOB:
|
||||||
id = dynamic_cast<BaseNPC*>(other)->id;
|
id = dynamic_cast<BaseNPC*>(other)->id;
|
||||||
bufferIdForDisappearance(npcDisappearances, id);
|
bufferIdForDisappearance(npcDisappearances, id, MAX_IDS_PER_AROUND_DEL);
|
||||||
break;
|
break;
|
||||||
case EntityKind::EGG:
|
case EntityKind::EGG:
|
||||||
id = dynamic_cast<Egg*>(other)->id;
|
id = dynamic_cast<Egg*>(other)->id;
|
||||||
bufferIdForDisappearance(shinyDisappearances, id);
|
bufferIdForDisappearance(shinyDisappearances, id, MAX_IDS_PER_AROUND_DEL);
|
||||||
break;
|
break;
|
||||||
case EntityKind::BUS:
|
case EntityKind::BUS:
|
||||||
id = dynamic_cast<Bus*>(other)->id;
|
id = dynamic_cast<Bus*>(other)->id;
|
||||||
bufferIdForDisappearance(transportationDisappearances, id);
|
bufferIdForDisappearance(transportationDisappearances, id, MAX_IDS_PER_AROUND_DEL - 1);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user