+void MasterState::On(const Packet::SpawnEntity &pack) {
+ if (!state) {
+ cout << "got entity spawn before world was created" << endl;
+ Quit();
+ return;
+ }
+ uint32_t entity_id;
+ pack.ReadEntityID(entity_id);
+ Entity &entity = state->GetWorld().ForceAddEntity(entity_id);
+ UpdateEntity(entity_id, pack.Seq());
+ pack.ReadEntity(entity);
+ uint32_t skel_id;
+ pack.ReadSkeletonID(skel_id);
+ CompositeModel *skel = state->GetSkeletons().ByID(skel_id);
+ if (skel) {
+ skel->Instantiate(entity.GetModel());
+ }
+ cout << "spawned entity #" << entity_id << " (" << entity.Name()
+ << ") at " << entity.AbsolutePosition() << endl;
+}
+
+void MasterState::On(const Packet::DespawnEntity &pack) {
+ if (!state) {
+ cout << "got entity despawn before world was created" << endl;
+ Quit();
+ return;
+ }
+ uint32_t entity_id;
+ pack.ReadEntityID(entity_id);
+ ClearEntity(entity_id);
+ for (Entity &entity : state->GetWorld().Entities()) {
+ if (entity.ID() == entity_id) {
+ entity.Kill();
+ cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl;
+ return;
+ }
+ }
+}
+
+void MasterState::On(const Packet::EntityUpdate &pack) {
+ if (!state) {
+ cout << "got entity update before world was created" << endl;
+ Quit();
+ return;
+ }
+
+ auto world_iter = state->GetWorld().Entities().begin();
+ auto world_end = state->GetWorld().Entities().end();
+
+ uint32_t count = 0;
+ pack.ReadEntityCount(count);
+
+ for (uint32_t i = 0; i < count; ++i) {
+ uint32_t entity_id = 0;
+ pack.ReadEntityID(entity_id, i);
+
+ while (world_iter != world_end && world_iter->ID() < entity_id) {
+ ++world_iter;
+ }
+ if (world_iter == world_end) {
+ // nothing can be done from here
+ return;
+ }
+ if (world_iter->ID() == entity_id) {
+ if (UpdateEntity(entity_id, pack.Seq())) {
+ pack.ReadEntityState(world_iter->GetState(), i);
+ }
+ }
+ }
+}
+
+bool MasterState::UpdateEntity(uint32_t entity_id, uint16_t seq) {
+ auto entry = update_status.find(entity_id);
+ if (entry == update_status.end()) {
+ update_status.emplace(entity_id, UpdateStatus{ seq, update_timer.Elapsed() });
+ return true;
+ }
+
+ int16_t pack_diff = int16_t(seq) - int16_t(entry->second.last_packet);
+ int time_diff = update_timer.Elapsed() - entry->second.last_update;
+ entry->second.last_update = update_timer.Elapsed();
+
+ if (pack_diff > 0 || time_diff > 1500) {
+ entry->second.last_packet = seq;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void MasterState::ClearEntity(uint32_t entity_id) {
+ update_status.erase(entity_id);
+}
+
+void MasterState::On(const Packet::PlayerCorrection &pack) {
+ if (!state) {
+ cout << "got player correction without a player :S" << endl;
+ Quit();
+ return;
+ }
+ uint16_t pack_seq;
+ EntityState corrected_state;
+ pack.ReadPacketSeq(pack_seq);
+ pack.ReadPlayerState(corrected_state);
+ state->MergePlayerCorrection(pack_seq, corrected_state);
+}
+
+void MasterState::On(const Packet::ChunkBegin &pack) {
+ if (!state) {
+ cout << "got chunk data, but the world has not been created yet" << endl;
+ cout << "great, this will totally screw up everything :(" << endl;
+ return;
+ }
+ state->GetChunkReceiver().Handle(pack);
+}
+
+void MasterState::On(const Packet::ChunkData &pack) {
+ if (!state) {
+ cout << "got chunk data, but the world has not been created yet" << endl;
+ cout << "great, this will totally screw up everything :(" << endl;
+ return;
+ }
+ state->GetChunkReceiver().Handle(pack);
+}
+
+void MasterState::On(const Packet::BlockUpdate &pack) {
+ if (!state) {
+ cout << "received block update, but the world has not been created yet" << endl;
+ return;
+ }
+ state->Handle(pack);
+}
+