Linked and unlinked vector packets

This commit is contained in:
Dan Paulat 2021-12-27 11:59:00 -06:00
parent ab702e9927
commit 96cd27adcb
6 changed files with 237 additions and 151 deletions

View file

@ -1,9 +1,6 @@
#include <scwx/wsr88d/rpg/linked_vector_packet.hpp>
#include <scwx/wsr88d/rpg/vector2d.hpp>
#include <array>
#include <istream>
#include <set>
#include <string>
#include <boost/log/trivial.hpp>
@ -22,14 +19,23 @@ class LinkedVectorPacketImpl
{
public:
explicit LinkedVectorPacketImpl() :
packetCode_ {}, lengthOfBlock_ {}, valueOfVector_ {}, vectorList_ {} {};
packetCode_ {},
lengthOfBlock_ {},
valueOfVector_ {},
startI_ {},
startJ_ {},
endI_ {},
endJ_ {} {};
~LinkedVectorPacketImpl() = default;
uint16_t packetCode_;
uint16_t lengthOfBlock_;
uint16_t valueOfVector_;
std::vector<Vector2D> vectorList_;
uint16_t startI_;
uint16_t startJ_;
std::vector<uint16_t> endI_;
std::vector<uint16_t> endJ_;
};
LinkedVectorPacket::LinkedVectorPacket() :
@ -52,9 +58,16 @@ uint16_t LinkedVectorPacket::length_of_block() const
return p->lengthOfBlock_;
}
uint16_t LinkedVectorPacket::value_of_vector() const
std::optional<uint16_t> LinkedVectorPacket::value_of_vector() const
{
return p->valueOfVector_;
std::optional<uint16_t> value;
if (p->packetCode_ == 9)
{
value = p->valueOfVector_;
}
return value;
}
size_t LinkedVectorPacket::data_size() const
@ -72,7 +85,7 @@ bool LinkedVectorPacket::Parse(std::istream& is)
p->packetCode_ = ntohs(p->packetCode_);
p->lengthOfBlock_ = ntohs(p->lengthOfBlock_);
size_t vectorSize = p->lengthOfBlock_;
int vectorSize = static_cast<int>(p->lengthOfBlock_) - 2;
if (is.eof())
{
@ -87,15 +100,28 @@ bool LinkedVectorPacket::Parse(std::istream& is)
vectorSize -= 2;
}
// The number of vectors is equal to the size divided by the number of bytes
// in a vector
size_t vectorCount = Vector2D::SIZE / 8;
is.read(reinterpret_cast<char*>(&p->startI_), 2);
is.read(reinterpret_cast<char*>(&p->startJ_), 2);
for (size_t v = 0; v < vectorCount && !is.eof(); v++)
p->startI_ = ntohs(p->startI_);
p->startJ_ = ntohs(p->startJ_);
// The number of vectors is equal to the size divided by the number of bytes
// in a vector coordinate
int vectorCount = vectorSize / 4;
uint16_t endI;
uint16_t endJ;
for (int v = 0; v < vectorCount && !is.eof(); v++)
{
Vector2D vector;
vector.Parse(is);
p->vectorList_.push_back(std::move(vector));
is.read(reinterpret_cast<char*>(&endI), 2);
is.read(reinterpret_cast<char*>(&endJ), 2);
endI = ntohs(endI);
endJ = ntohs(endJ);
p->endI_.push_back(endI);
p->endJ_.push_back(endJ);
}
if (is.eof())