Fix constness in certain operations

This commit is contained in:
2021-07-26 13:12:44 -04:00
parent 4052503093
commit 49484cf2f0
7 changed files with 95 additions and 54 deletions

View File

@@ -425,9 +425,8 @@ std::string AnimQueue::debug_string() const {
return oss.str(); return oss.str();
} }
void AnimQueue::serialize(StreamBuffer *sb) { void AnimQueue::serialize_size_and_steps(StreamBuffer *sb) const {
assert(valid()); // can't serialize an invalid animqueue. assert(valid()); // can't serialize an invalid animqueue.
sb->write_int64(version_number_);
sb->write_uint8(size_limit_); sb->write_uint8(size_limit_);
sb->write_uint8(steps_.size()); sb->write_uint8(steps_.size());
for (const AnimStep &step : steps_) { for (const AnimStep &step : steps_) {
@@ -435,8 +434,12 @@ void AnimQueue::serialize(StreamBuffer *sb) {
} }
} }
void AnimQueue::deserialize(StreamBuffer *sb) { void AnimQueue::serialize(StreamBuffer *sb) const {
version_number_ = sb->read_int64(); serialize_size_and_steps(sb);
sb->write_int64(version_number_);
}
void AnimQueue::deserialize_size_and_steps(StreamBuffer *sb) {
size_limit_ = sb->read_uint8(); size_limit_ = sb->read_uint8();
size_t nsteps = sb->read_uint8(); size_t nsteps = sb->read_uint8();
steps_.resize(nsteps); steps_.resize(nsteps);
@@ -447,6 +450,11 @@ void AnimQueue::deserialize(StreamBuffer *sb) {
} }
} }
void AnimQueue::deserialize(StreamBuffer *sb) {
deserialize_size_and_steps(sb);
version_number_ = sb->read_int64();
}
bool AnimQueue::need_patch(const AnimQueue &auth) const { bool AnimQueue::need_patch(const AnimQueue &auth) const {
// Sanity check. // Sanity check.
assert(valid()); assert(valid());

View File

@@ -182,7 +182,9 @@ public:
void add(int64_t id, const AnimStep &step); void add(int64_t id, const AnimStep &step);
// Serialize or deserialize to a StreamBuffer // Serialize or deserialize to a StreamBuffer
void serialize(StreamBuffer *sb); void serialize_size_and_steps(StreamBuffer *sb) const;
void deserialize_size_and_steps(StreamBuffer *sb);
void serialize(StreamBuffer *sb) const;
void deserialize(StreamBuffer *sb); void deserialize(StreamBuffer *sb);
// Difference transmission // Difference transmission

View File

@@ -80,7 +80,7 @@ int64_t IdGlobalPool::alloc_id_for_thread(lua_State *L) {
} }
} }
void IdGlobalPool::serialize(StreamBuffer *sb) { void IdGlobalPool::serialize(StreamBuffer *sb) const {
sb->write_int64(next_batch_); sb->write_int64(next_batch_);
sb->write_int64(next_id_); sb->write_int64(next_id_);
sb->write_uint32(salvaged_.size()); sb->write_uint32(salvaged_.size());
@@ -171,7 +171,7 @@ void IdPlayerPool::prepare_thread(lua_State *L) {
lua_setnextid(L, get_batch()); lua_setnextid(L, get_batch());
} }
void IdPlayerPool::serialize(StreamBuffer *sb) { void IdPlayerPool::serialize(StreamBuffer *sb) const {
sb->write_uint8(fifo_capacity_); sb->write_uint8(fifo_capacity_);
sb->write_uint8(ranges_.size()); sb->write_uint8(ranges_.size());
for (int64_t batch : ranges_) { for (int64_t batch : ranges_) {

View File

@@ -110,7 +110,7 @@ public:
int64_t alloc_id_for_thread(lua_State *L); int64_t alloc_id_for_thread(lua_State *L);
// Serialize to or deserialize from a streambuffer. // Serialize to or deserialize from a streambuffer.
void serialize(StreamBuffer *sb); void serialize(StreamBuffer *sb) const;
void deserialize(StreamBuffer *sb); void deserialize(StreamBuffer *sb);
// Generate a debug string. // Generate a debug string.
@@ -176,7 +176,7 @@ public:
// Serialize to or deserialize from a streambuffer. // Serialize to or deserialize from a streambuffer.
// Caution: the pointer to the global pool is not serialized or deserialized. // Caution: the pointer to the global pool is not serialized or deserialized.
void serialize(StreamBuffer *sb); void serialize(StreamBuffer *sb) const;
void deserialize(StreamBuffer *sb); void deserialize(StreamBuffer *sb);
// Difference transmission // Difference transmission

View File

@@ -8,6 +8,7 @@
#include <ostream> #include <ostream>
#include <tuple> #include <tuple>
#include <utility> #include <utility>
#include "luastack.hpp"
namespace util { namespace util {

View File

@@ -98,8 +98,17 @@ Tangible *World::tangible_get(int64_t id) {
} }
} }
std::vector<Tangible*> World::tangible_get_all(const std::vector<int64_t> &ids) { const Tangible *World::tangible_get(int64_t id) const {
std::vector<Tangible*> result(ids.size()); auto iter = tangibles_.find(id);
if (iter == tangibles_.end()) {
return nullptr;
} else {
return iter->second.get();
}
}
World::TanVector World::tangible_get_all(const IdVector &ids) const {
TanVector result(ids.size());
for (int i = 0; i < int(ids.size()); i++) { for (int i = 0; i < int(ids.size()); i++) {
result[i] = tangible_get(ids[i]); result[i] = tangible_get(ids[i]);
} }
@@ -152,8 +161,8 @@ void World::tangible_delete(lua_State *L, int64_t id) {
LS.result(); LS.result();
} }
util::IdVector World::get_near(int64_t player_id, float radius, bool exclude_nowhere) { util::IdVector World::get_near(int64_t player_id, float radius, bool exclude_nowhere) const {
Tangible *player = tangible_get(player_id); const Tangible *player = tangible_get(player_id);
if (player == nullptr) { if (player == nullptr) {
return IdVector(); return IdVector();
} }
@@ -494,24 +503,54 @@ void World::rollback() {
deserialize(&snapshot_); deserialize(&snapshot_);
} }
void World::difference_transmit(int64_t actor_id, World *master, StreamBuffer *sb) { void World::difference_transmit(int64_t actor_id, const World *master, StreamBuffer *sb) {
StreamBuffer tsb; StreamBuffer tsb;
diff_actor_essentials(actor_id, master, &tsb); // Get the actor in both models.
const Tangible *s_actor = tangible_get(actor_id);
const Tangible *m_actor = master->tangible_get(actor_id);
assert(s_actor != nullptr);
assert(m_actor != nullptr);
// Pass one: update the actor essentials.
assert(tsb.at_eof());
diff_actor_essentials(m_actor, s_actor, &tsb);
tsb.copy_into(sb); tsb.copy_into(sb);
patch_actor_essentials(&tsb); patch_actor_essentials(&tsb);
diff_visible_animations(actor_id, master, &tsb); // Get the list of tangibles visible in either model.
// Some tangibles may be missing in the master, some may be missing in the sync.
util::IdVector visible = PlaneMap::sort_union_id_vectors(
master->get_near(actor_id, 100.0, true),
this->get_near(actor_id, 100.0, true));
TanVector m_visible = tangible_get_all(visible);
TanVector s_visible = tangible_get_all(visible);
assert(m_visible.size() == s_visible.size());
// Pass two: update visible animations.
assert(tsb.at_eof());
diff_visible_animations(m_visible, s_visible, &tsb);
tsb.copy_into(sb); tsb.copy_into(sb);
patch_visible_animations(&tsb); patch_visible_animations(&tsb);
// Copy the version number from master animqueue to synch animqueue.
for (int i = 0; i < int(m_visible.size()); i++) {
const Tangible *m_tan = m_visible[i];
if (m_tan != nullptr) {
Tangible *s_tan = const_cast<Tangible *>(s_visible[i]);
if (s_tan == nullptr) {
s_tan = tangible_get(m_tan->id());
assert(s_tan != nullptr);
}
s_tan->anim_queue_.update_version(m_tan->anim_queue_);
}
}
assert(tsb.at_eof());
} }
void World::diff_actor_essentials(int64_t actor_id, World *master, StreamBuffer *sb) { void World::diff_actor_essentials(const Tangible *m_actor, const Tangible *s_actor, StreamBuffer *sb) {
Tangible *s_actor = tangible_get(actor_id); sb->write_int64(m_actor->id());
Tangible *m_actor = master->tangible_get(actor_id);
assert(s_actor != nullptr);
assert(m_actor != nullptr);
sb->write_int64(actor_id);
s_actor->id_player_pool_.diff(m_actor->id_player_pool_, sb); s_actor->id_player_pool_.diff(m_actor->id_player_pool_, sb);
s_actor->anim_queue_.diff(m_actor->anim_queue_, sb); s_actor->anim_queue_.diff(m_actor->anim_queue_, sb);
} }
@@ -524,29 +563,19 @@ void World::patch_actor_essentials(StreamBuffer *sb) {
s_actor->update_plane_item(); s_actor->update_plane_item();
} }
void World::diff_visible_animations(int64_t actor_id, World *master, StreamBuffer *sb) { void World::diff_visible_animations(const TanVector &mvis, const TanVector &svis, StreamBuffer *sb) {
// Get the list of tangibles visible in either model.
util::IdVector visible = PlaneMap::sort_union_id_vectors(
master->get_near(actor_id, 100.0, true),
this->get_near(actor_id, 100.0, true));
// Some tangibles may be missing in the master, some may be missing in the sync.
std::vector<Tangible *> m_visible = tangible_get_all(visible);
std::vector<Tangible *> s_visible = tangible_get_all(visible);
assert(m_visible.size() == s_visible.size());
// For each tangible missing in the synchronous model, send the // For each tangible missing in the synchronous model, send the
// necessary information to create the tangible. // necessary information to create the tangible.
sb->write_int32(0); sb->write_int32(0);
int count_pos = sb->total_writes(); int count_pos = sb->total_writes();
int count = 0; int count = 0;
for (int i = 0; i < int(s_visible.size()); i++) { for (int i = 0; i < int(svis.size()); i++) {
Tangible *mt = m_visible[i]; const Tangible *mt = mvis[i];
Tangible *st = s_visible[i]; const Tangible *st = svis[i];
if (st == nullptr) { if (st == nullptr) {
count += 1; count += 1;
sb->write_int64(mt->id()); sb->write_int64(mt->id());
mt->anim_queue_.serialize(sb); mt->anim_queue_.serialize_size_and_steps(sb);
} }
} }
sb->overwrite_int32(count_pos, count); sb->overwrite_int32(count_pos, count);
@@ -556,9 +585,9 @@ void World::diff_visible_animations(int64_t actor_id, World *master, StreamBuffe
sb->write_int32(0); sb->write_int32(0);
count_pos = sb->total_writes(); count_pos = sb->total_writes();
count = 0; count = 0;
for (int i = 0; i < int(s_visible.size()); i++) { for (int i = 0; i < int(svis.size()); i++) {
Tangible *mt = m_visible[i]; const Tangible *mt = mvis[i];
Tangible *st = s_visible[i]; const Tangible *st = svis[i];
if (mt == nullptr) { if (mt == nullptr) {
count += 1; count += 1;
sb->write_int64(st->id()); sb->write_int64(st->id());
@@ -571,9 +600,9 @@ void World::diff_visible_animations(int64_t actor_id, World *master, StreamBuffe
sb->write_int32(0); sb->write_int32(0);
count_pos = sb->total_writes(); count_pos = sb->total_writes();
count = 0; count = 0;
for (int i = 0; i < int(s_visible.size()); i++) { for (int i = 0; i < int(svis.size()); i++) {
Tangible *mt = m_visible[i]; const Tangible *mt = mvis[i];
Tangible *st = s_visible[i]; const Tangible *st = svis[i];
if ((mt != nullptr) && (st != nullptr)) { if ((mt != nullptr) && (st != nullptr)) {
if (st->anim_queue_.need_patch(mt->anim_queue_)) { if (st->anim_queue_.need_patch(mt->anim_queue_)) {
count++; count++;
@@ -591,7 +620,7 @@ void World::patch_visible_animations(StreamBuffer *sb) {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
int64_t id = sb->read_int64(); int64_t id = sb->read_int64();
Tangible *t = tangible_make(state(), id, false); Tangible *t = tangible_make(state(), id, false);
t->anim_queue_.deserialize(sb); t->anim_queue_.deserialize_size_and_steps(sb);
t->update_plane_item(); t->update_plane_item();
} }

View File

@@ -68,7 +68,7 @@ public:
// Get the ID // Get the ID
// //
int64_t id() { return plane_item_.id(); } int64_t id() const { return plane_item_.id(); }
void update_plane_item(); void update_plane_item();
bool is_an_actor() { return (id_player_pool_.get_fifo_capacity() > 0); } bool is_an_actor() { return (id_player_pool_.get_fifo_capacity() > 0); }
@@ -78,7 +78,7 @@ public:
class World { class World {
public: public:
using IdVector = util::IdVector; using IdVector = util::IdVector;
using TangibleVector = std::vector<Tangible*>; using TanVector = std::vector<const Tangible*>;
using Redirects = std::map<int64_t, int64_t>; using Redirects = std::map<int64_t, int64_t>;
// Constructor. // Constructor.
@@ -107,7 +107,7 @@ public:
// Get a list of the tangibles that are near the player. If 'exclude_nowhere' is // Get a list of the tangibles that are near the player. If 'exclude_nowhere' is
// true, exclude any tangibles on the nowhere plane. // true, exclude any tangibles on the nowhere plane.
// //
IdVector get_near(int64_t player_id, float radius, bool exclude_nowhere); IdVector get_near(int64_t player_id, float radius, bool exclude_nowhere) const;
// Make a tangible. // Make a tangible.
// //
@@ -120,10 +120,7 @@ public:
// Get a pointer to the specified tangible. // Get a pointer to the specified tangible.
// //
Tangible *tangible_get(int64_t id); Tangible *tangible_get(int64_t id);
const Tangible *tangible_get(int64_t id) const;
// Get pointers to many tangibles.
//
TangibleVector tangible_get_all(const IdVector &ids);
// Get a pointer to the specified tangible. // Get a pointer to the specified tangible.
// //
@@ -132,6 +129,10 @@ public:
// //
Tangible *tangible_get(lua_State *L, int idx); Tangible *tangible_get(lua_State *L, int idx);
// Get pointers to many tangibles.
//
TanVector tangible_get_all(const IdVector &ids) const;
// Delete the specified tangible. // Delete the specified tangible.
// //
void tangible_delete(lua_State *L, int64_t id); void tangible_delete(lua_State *L, int64_t id);
@@ -182,7 +183,7 @@ public:
// so that they can be sent to the client. It also applies the diffs // so that they can be sent to the client. It also applies the diffs
// to this model. // to this model.
// //
void difference_transmit(int64_t actor, World *master, StreamBuffer *sb); void difference_transmit(int64_t actor, const World *master, StreamBuffer *sb);
// Apply differences. // Apply differences.
// //
@@ -213,7 +214,7 @@ private:
// Before we do anything else, we need to get the actor in the right place. // Before we do anything else, we need to get the actor in the right place.
// We also update the actor's ID allocation pipeline. // We also update the actor's ID allocation pipeline.
// //
void diff_actor_essentials(int64_t actor_id, World *master, StreamBuffer *sb); static void diff_actor_essentials(const Tangible *mactor, const Tangible *sactor, StreamBuffer *sb);
void patch_actor_essentials(StreamBuffer *sb); void patch_actor_essentials(StreamBuffer *sb);
// Pass 2 of difference transmission: visible animations. // Pass 2 of difference transmission: visible animations.
@@ -221,7 +222,7 @@ private:
// Synchronizes the animation status of every tangible inside the visibility // Synchronizes the animation status of every tangible inside the visibility
// radius of either model. Creates missing tangibles and deletes excess tangibles. // radius of either model. Creates missing tangibles and deletes excess tangibles.
// //
void diff_visible_animations(int64_t actor_id, World *master, StreamBuffer *sb); static void diff_visible_animations(const TanVector &mvis, const TanVector &svis, StreamBuffer *sb);
void patch_visible_animations(StreamBuffer *sb); void patch_visible_animations(StreamBuffer *sb);
private: private: