X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fclient%2Fclient.cpp;h=8bd0ae3db1a6c76d7546cd568d823e2ef616f709;hb=52154f309e569c913520dd004e9fafcbc4671e6f;hp=6a2dd32993b1fb45031ef9485428ce256ae0f4a7;hpb=ba55bf4293f3abc742eef710545a4b207ba2c820;p=blank.git diff --git a/src/client/client.cpp b/src/client/client.cpp index 6a2dd32..8bd0ae3 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -4,7 +4,6 @@ #include "../app/Environment.hpp" #include "../app/init.hpp" -#include "../app/TextureIndex.hpp" #include "../model/Model.hpp" #include "../io/WorldSave.hpp" #include "../world/ChunkIndex.hpp" @@ -48,44 +47,78 @@ void InitialState::Render(Viewport &viewport) { // TODO: this clutter is a giant mess InteractiveState::InteractiveState(MasterState &master, uint32_t player_id) : master(master) -, shapes() -, block_types() +, res() +, sounds() , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host)) -, world(block_types, master.GetWorldConf()) -, player(*world.AddPlayer(master.GetConfig().player.name)) +, world(res.block_types, master.GetWorldConf()) +, player(*world.AddPlayer(master.GetConfig().player.name, player_id)) , hud(master.GetEnv(), master.GetConfig(), player) -, manip(master.GetEnv(), player.GetEntity()) +, manip(master.GetEnv().audio, sounds, player.GetEntity()) , input(world, player, master.GetClient()) , interface(master.GetConfig(), master.GetEnv().keymap, input, *this) , chunk_receiver(world.Chunks(), save) , chunk_renderer(player.GetChunks()) -, skeletons() , loop_timer(16) -, sky(master.GetEnv().loader.LoadCubeMap("skybox")) { +, stat_timer(1000) +, sky(master.GetEnv().loader.LoadCubeMap("skybox")) +, update_status() +, chat(master.GetEnv(), *this, *this) +, time_skipped(0) +, packets_skipped(0) { if (!save.Exists()) { save.Write(master.GetWorldConf()); } - TextureIndex tex_index; - master.GetEnv().loader.LoadShapes("default", shapes); - master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index, shapes); - skeletons.Load(shapes, tex_index); - interface.SetInventorySlots(block_types.size() - 1); - chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index); + res.Load(master.GetEnv().loader, "default"); + if (res.models.size() < 1) { + throw std::runtime_error("need at least one model to run"); + } + res.models[0].Instantiate(player.GetEntity().GetModel()); + sounds.Load(master.GetEnv().loader, res.snd_index); + interface.SetInventorySlots(res.block_types.size() - 1); + chunk_renderer.LoadTextures(master.GetEnv().loader, res.tex_index); chunk_renderer.FogDensity(master.GetWorldConf().fog_density); loop_timer.Start(); + stat_timer.Start(); if (save.Exists(player)) { save.Read(player); } } -void InteractiveState::OnEnter() { - master.GetEnv().window.GrabMouse(); +void InteractiveState::OnResume() { + OnFocus(); +} + +void InteractiveState::OnPause() { + OnBlur(); +} + +void InteractiveState::OnFocus() { + if (master.GetConfig().input.mouse) { + master.GetEnv().window.GrabMouse(); + } + interface.Unlock(); +} + +void InteractiveState::OnBlur() { + master.GetEnv().window.ReleaseMouse(); + interface.Lock(); } void InteractiveState::Handle(const SDL_Event &event) { switch (event.type) { case SDL_KEYDOWN: - interface.HandlePress(event.key); + // TODO: move to interface + if (event.key.keysym.sym == SDLK_RETURN) { + chat.Clear(); + master.GetEnv().state.Push(&chat); + hud.KeepMessages(true); + } else if (event.key.keysym.sym == SDLK_SLASH) { + chat.Preset("/"); + master.GetEnv().state.Push(&chat); + hud.KeepMessages(true); + } else { + interface.HandlePress(event.key); + } break; case SDL_KEYUP: interface.HandleRelease(event.key); @@ -111,20 +144,11 @@ void InteractiveState::Handle(const SDL_Event &event) { } void InteractiveState::Update(int dt) { - input.Update(dt); - if (input.BlockFocus()) { - hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block); - } else if (input.EntityFocus()) { - hud.FocusEntity(*input.EntityFocus().entity); - } else { - hud.FocusNone(); - } - hud.Display(block_types[player.GetInventorySlot() + 1]); loop_timer.Update(dt); + stat_timer.Update(dt); master.Update(dt); chunk_receiver.Update(dt); - hud.Update(dt); int world_dt = 0; while (loop_timer.HitOnce()) { world.Update(loop_timer.Interval()); @@ -133,9 +157,28 @@ void InteractiveState::Update(int dt) { } chunk_renderer.Update(dt); + if (input.BlockFocus()) { + hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block); + } else if (input.EntityFocus()) { + hud.FocusEntity(*input.EntityFocus().entity); + } else { + hud.FocusNone(); + } if (world_dt > 0) { - input.PushPlayerUpdate(world_dt); + if (input.UpdateImportant() || packets_skipped >= master.NetStat().SuggestedPacketSkip()) { + input.PushPlayerUpdate(time_skipped + world_dt); + time_skipped = 0; + packets_skipped = 0; + } else { + time_skipped += world_dt; + ++packets_skipped; + } + } + hud.Display(res.block_types[player.GetInventorySlot() + 1]); + if (stat_timer.Hit()) { + hud.UpdateNetStats(master.NetStat()); } + hud.Update(dt); glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords()); glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)); @@ -146,7 +189,7 @@ void InteractiveState::Update(int dt) { } void InteractiveState::Render(Viewport &viewport) { - viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords())); + viewport.WorldPosition(player.GetEntity().ViewTransform(player.GetEntity().ChunkCoords())); if (master.GetConfig().video.world) { chunk_renderer.Render(viewport); world.Render(viewport); @@ -155,8 +198,90 @@ void InteractiveState::Render(Viewport &viewport) { hud.Render(viewport); } -void InteractiveState::MergePlayerCorrection(std::uint16_t pack, const EntityState &state) { - input.MergePlayerCorrection(pack, state); +void InteractiveState::Handle(const Packet::SpawnEntity &pack) { + uint32_t entity_id; + pack.ReadEntityID(entity_id); + Entity &entity = world.ForceAddEntity(entity_id); + UpdateEntity(entity_id, pack.Seq()); + pack.ReadEntity(entity); + uint32_t model_id; + pack.ReadModelID(model_id); + if (model_id > 0 && model_id <= res.models.size()) { + res.models.Get(model_id).Instantiate(entity.GetModel()); + } +} + +void InteractiveState::Handle(const Packet::DespawnEntity &pack) { + uint32_t entity_id; + pack.ReadEntityID(entity_id); + ClearEntity(entity_id); + for (Entity &entity : world.Entities()) { + if (entity.ID() == entity_id) { + entity.Kill(); + return; + } + } +} + +void InteractiveState::Handle(const Packet::EntityUpdate &pack) { + auto world_iter = world.Entities().begin(); + auto world_end = world.Entities().end(); + + uint32_t count = 0; + glm::ivec3 base; + pack.ReadEntityCount(count); + pack.ReadChunkBase(base); + EntityState state; + + 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(state, base, i); + world_iter->SetState(state); + } + } + } +} + +bool InteractiveState::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, loop_timer.Elapsed() }); + return true; + } + + int16_t pack_diff = int16_t(seq) - int16_t(entry->second.last_packet); + int time_diff = loop_timer.Elapsed() - entry->second.last_update; + entry->second.last_update = loop_timer.Elapsed(); + + if (pack_diff > 0 || time_diff > 1500) { + entry->second.last_packet = seq; + return true; + } else { + return false; + } +} + +void InteractiveState::ClearEntity(uint32_t entity_id) { + update_status.erase(entity_id); +} + +void InteractiveState::Handle(const Packet::PlayerCorrection &pack) { + uint16_t pack_seq; + EntityState corrected_state; + pack.ReadPacketSeq(pack_seq); + pack.ReadPlayerState(corrected_state); + input.MergePlayerCorrection(pack_seq, corrected_state); } void InteractiveState::Handle(const Packet::BlockUpdate &pack) { @@ -174,12 +299,18 @@ void InteractiveState::Handle(const Packet::BlockUpdate &pack) { Block block; pack.ReadIndex(index, i); pack.ReadBlock(block, i); - if (index < Chunk::size && block.type < block_types.size()) { + if (index < Chunk::size && block.type < res.block_types.size()) { manip.SetBlock(*chunk, index, block); } } } +void InteractiveState::Handle(const Packet::Message &pack) { + string msg; + pack.ReadMessage(msg); + hud.PostMessage(msg); +} + void InteractiveState::SetAudio(bool b) { master.GetConfig().audio.enabled = b; if (b) { @@ -221,6 +352,12 @@ void InteractiveState::Exit() { master.Quit(); } +void InteractiveState::OnLineSubmit(const string &line) { + if (!line.empty()) { + master.GetClient().SendMessage(1, 0, line); + } +} + MasterState::MasterState( Environment &env, @@ -232,11 +369,8 @@ MasterState::MasterState( , state() , client(config.net) , init_state(*this) -, login_packet(-1) -, update_status() -, update_timer(16) { +, login_packet(-1) { client.GetConnection().SetHandler(this); - update_timer.Start(); } void MasterState::Quit() { @@ -259,7 +393,6 @@ void MasterState::Handle(const SDL_Event &event) { void MasterState::Update(int dt) { - update_timer.Update(dt); client.Handle(); client.Update(dt); } @@ -300,7 +433,9 @@ void MasterState::On(const Packet::Join &pack) { pack.ReadPlayerID(player_id); state.reset(new InteractiveState(*this, player_id)); - pack.ReadPlayerState(state->GetPlayer().GetEntity().GetState()); + EntityState player_state; + pack.ReadPlayerState(player_state); + state->GetPlayer().GetEntity().SetState(player_state); env.state.PopAfter(this); env.state.Push(state.get()); @@ -322,19 +457,7 @@ void MasterState::On(const Packet::SpawnEntity &pack) { cout << "got entity spawn before world was created" << endl; 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); - Model *skel = state->GetSkeletons().ByID(skel_id); - if (skel) { - skel->Instantiate(entity.GetModel()); - } - cout << "spawned entity #" << entity_id << " (" << entity.Name() - << ") at " << entity.AbsolutePosition() << endl; + state->Handle(pack); } void MasterState::On(const Packet::DespawnEntity &pack) { @@ -342,16 +465,7 @@ void MasterState::On(const Packet::DespawnEntity &pack) { cout << "got entity despawn before world was created" << endl; 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; - } - } + state->Handle(pack); } void MasterState::On(const Packet::EntityUpdate &pack) { @@ -359,53 +473,7 @@ void MasterState::On(const Packet::EntityUpdate &pack) { cout << "got entity update before world was created" << endl; 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); + state->Handle(pack); } void MasterState::On(const Packet::PlayerCorrection &pack) { @@ -413,11 +481,7 @@ void MasterState::On(const Packet::PlayerCorrection &pack) { cout << "got player correction without a player :S" << endl; return; } - uint16_t pack_seq; - EntityState corrected_state; - pack.ReadPacketSeq(pack_seq); - pack.ReadPlayerState(corrected_state); - state->MergePlayerCorrection(pack_seq, corrected_state); + state->Handle(pack); } void MasterState::On(const Packet::ChunkBegin &pack) { @@ -446,5 +510,15 @@ void MasterState::On(const Packet::BlockUpdate &pack) { state->Handle(pack); } +void MasterState::On(const Packet::Message &pack) { + if (state) { + state->Handle(pack); + } else { + string msg; + pack.ReadMessage(msg); + cout << "got message before interface was created: " << msg << endl; + } +} + } }