osb/source/core/StarNetElementGroup.cpp

124 lines
3.7 KiB
C++
Raw Normal View History

2023-06-20 14:33:09 +10:00
#include "StarNetElementGroup.hpp"
namespace Star {
void NetElementGroup::addNetElement(NetElement* element, bool propagateInterpolation) {
starAssert(!m_elements.any([element](auto p) { return p.first == element; }));
element->initNetVersion(m_version);
if (m_interpolationEnabled && propagateInterpolation)
element->enableNetInterpolation(m_extrapolationHint);
m_elements.append(pair<NetElement*, bool>(element, propagateInterpolation));
}
void NetElementGroup::clearNetElements() {
m_elements.clear();
}
void NetElementGroup::initNetVersion(NetElementVersion const* version) {
m_version = version;
for (auto& p : m_elements)
2023-06-20 14:33:09 +10:00
p.first->initNetVersion(m_version);
}
2024-09-05 19:15:47 +10:00
void NetElementGroup::netStore(DataStream& ds, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return;
for (auto& p : m_elements)
2024-09-05 19:15:47 +10:00
if (p.first->checkWithRules(rules))
p.first->netStore(ds, rules);
2023-06-20 14:33:09 +10:00
}
2024-09-05 19:15:47 +10:00
void NetElementGroup::netLoad(DataStream& ds, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
for (auto& p : m_elements)
2024-09-05 19:15:47 +10:00
if (p.first->checkWithRules(rules))
p.first->netLoad(ds, rules);
2023-06-20 14:33:09 +10:00
}
void NetElementGroup::enableNetInterpolation(float extrapolationHint) {
m_interpolationEnabled = true;
m_extrapolationHint = extrapolationHint;
for (auto& p : m_elements) {
2023-06-20 14:33:09 +10:00
if (p.second)
p.first->enableNetInterpolation(extrapolationHint);
}
}
void NetElementGroup::disableNetInterpolation() {
m_interpolationEnabled = false;
m_extrapolationHint = 0;
for (auto& p : m_elements) {
2023-06-20 14:33:09 +10:00
if (p.second)
p.first->disableNetInterpolation();
}
}
void NetElementGroup::tickNetInterpolation(float dt) {
if (m_interpolationEnabled) {
for (auto& p : m_elements)
2023-06-20 14:33:09 +10:00
p.first->tickNetInterpolation(dt);
}
}
2024-09-05 19:15:47 +10:00
bool NetElementGroup::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
if (!checkWithRules(rules)) return false;
2023-06-20 14:33:09 +10:00
if (m_elements.size() == 0) {
return false;
} else if (m_elements.size() == 1) {
2024-09-05 19:15:47 +10:00
return m_elements[0].first->writeNetDelta(ds, fromVersion, rules);
2023-06-20 14:33:09 +10:00
} else {
bool deltaWritten = false;
2024-09-05 19:15:47 +10:00
uint64_t i = 0;
m_buffer.setStreamCompatibilityVersion(rules);
for (auto& element : m_elements) {
if (!element.first->checkWithRules(rules))
continue;
++i;
if (element.first->writeNetDelta(m_buffer, fromVersion, rules)) {
2023-06-20 14:33:09 +10:00
deltaWritten = true;
2024-09-05 19:15:47 +10:00
ds.writeVlqU(i);
2023-06-20 14:33:09 +10:00
ds.writeBytes(m_buffer.data());
m_buffer.clear();
}
}
if (deltaWritten)
ds.writeVlqU(0);
return deltaWritten;
}
}
2024-09-05 19:15:47 +10:00
void NetElementGroup::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
if (!checkWithRules(rules)) return;
2023-06-20 14:33:09 +10:00
if (m_elements.size() == 0) {
throw IOException("readNetDelta called on empty NetElementGroup");
} else if (m_elements.size() == 1) {
2024-09-05 19:15:47 +10:00
m_elements[0].first->readNetDelta(ds, interpolationTime, rules);
2023-06-20 14:33:09 +10:00
} else {
uint64_t readIndex = ds.readVlqU();
2024-09-05 19:15:47 +10:00
uint64_t i = 0;
for (auto& element : m_elements) {
if (!element.first->checkWithRules(rules))
continue;
2023-06-20 14:33:09 +10:00
if (readIndex == 0 || readIndex - 1 > i) {
if (m_interpolationEnabled)
m_elements[i].first->blankNetDelta(interpolationTime);
} else if (readIndex - 1 == i) {
2024-09-05 19:15:47 +10:00
m_elements[i].first->readNetDelta(ds, interpolationTime, rules);
2023-06-20 14:33:09 +10:00
readIndex = ds.readVlqU();
} else {
throw IOException("group indexes out of order in NetElementGroup::readNetDelta");
}
2024-09-05 19:15:47 +10:00
++i;
2023-06-20 14:33:09 +10:00
}
}
}
void NetElementGroup::blankNetDelta(float interpolationTime) {
if (m_interpolationEnabled) {
for (auto& p : m_elements)
2023-06-20 14:33:09 +10:00
p.first->blankNetDelta(interpolationTime);
}
}
}