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

@@ -98,8 +98,17 @@ Tangible *World::tangible_get(int64_t id) {
}
}
std::vector<Tangible*> World::tangible_get_all(const std::vector<int64_t> &ids) {
std::vector<Tangible*> result(ids.size());
const Tangible *World::tangible_get(int64_t id) const {
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++) {
result[i] = tangible_get(ids[i]);
}
@@ -152,8 +161,8 @@ void World::tangible_delete(lua_State *L, int64_t id) {
LS.result();
}
util::IdVector World::get_near(int64_t player_id, float radius, bool exclude_nowhere) {
Tangible *player = tangible_get(player_id);
util::IdVector World::get_near(int64_t player_id, float radius, bool exclude_nowhere) const {
const Tangible *player = tangible_get(player_id);
if (player == nullptr) {
return IdVector();
}
@@ -494,24 +503,54 @@ void World::rollback() {
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;
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);
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);
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) {
Tangible *s_actor = tangible_get(actor_id);
Tangible *m_actor = master->tangible_get(actor_id);
assert(s_actor != nullptr);
assert(m_actor != nullptr);
sb->write_int64(actor_id);
void World::diff_actor_essentials(const Tangible *m_actor, const Tangible *s_actor, StreamBuffer *sb) {
sb->write_int64(m_actor->id());
s_actor->id_player_pool_.diff(m_actor->id_player_pool_, 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();
}
void World::diff_visible_animations(int64_t actor_id, World *master, 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());
void World::diff_visible_animations(const TanVector &mvis, const TanVector &svis, StreamBuffer *sb) {
// For each tangible missing in the synchronous model, send the
// necessary information to create the tangible.
sb->write_int32(0);
int count_pos = sb->total_writes();
int count = 0;
for (int i = 0; i < int(s_visible.size()); i++) {
Tangible *mt = m_visible[i];
Tangible *st = s_visible[i];
for (int i = 0; i < int(svis.size()); i++) {
const Tangible *mt = mvis[i];
const Tangible *st = svis[i];
if (st == nullptr) {
count += 1;
sb->write_int64(mt->id());
mt->anim_queue_.serialize(sb);
mt->anim_queue_.serialize_size_and_steps(sb);
}
}
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);
count_pos = sb->total_writes();
count = 0;
for (int i = 0; i < int(s_visible.size()); i++) {
Tangible *mt = m_visible[i];
Tangible *st = s_visible[i];
for (int i = 0; i < int(svis.size()); i++) {
const Tangible *mt = mvis[i];
const Tangible *st = svis[i];
if (mt == nullptr) {
count += 1;
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);
count_pos = sb->total_writes();
count = 0;
for (int i = 0; i < int(s_visible.size()); i++) {
Tangible *mt = m_visible[i];
Tangible *st = s_visible[i];
for (int i = 0; i < int(svis.size()); i++) {
const Tangible *mt = mvis[i];
const Tangible *st = svis[i];
if ((mt != nullptr) && (st != nullptr)) {
if (st->anim_queue_.need_patch(mt->anim_queue_)) {
count++;
@@ -591,7 +620,7 @@ void World::patch_visible_animations(StreamBuffer *sb) {
for (int i = 0; i < count; i++) {
int64_t id = sb->read_int64();
Tangible *t = tangible_make(state(), id, false);
t->anim_queue_.deserialize(sb);
t->anim_queue_.deserialize_size_and_steps(sb);
t->update_plane_item();
}