X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fclient%2Fclient.cpp;h=8b532633ab0cd85dc2926f2dbda72eb3ed7762e3;hb=f21a21b285c5b54e3abcc757c6715d22520dd190;hp=4a48b45afc91eb5179f9a6d038c88b19244f213d;hpb=8507332e2d0c54aec4045fb6f0021bdc3bd57750;p=blank.git diff --git a/src/client/client.cpp b/src/client/client.cpp index 4a48b45..8b53263 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -4,8 +4,10 @@ #include "../app/Environment.hpp" #include "../app/init.hpp" -#include "../app/TextureIndex.hpp" -#include "../model/CompositeModel.hpp" +#include "../model/Model.hpp" +#include "../io/WorldSave.hpp" +#include "../world/ChunkIndex.hpp" +#include "../world/ChunkStore.hpp" #include #include @@ -45,27 +47,37 @@ void InitialState::Render(Viewport &viewport) { // TODO: this clutter is a giant mess InteractiveState::InteractiveState(MasterState &master, uint32_t player_id) : master(master) -, block_types() -, save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetClientConf().host)) -, world(block_types, master.GetWorldConf()) -, interface( - master.GetInterfaceConf(), - master.GetEnv(), - world, - world.AddPlayer(master.GetInterfaceConf().player_name, player_id) -) -, chunk_renderer(*interface.GetPlayer().chunks) -, skeletons() -, update_timer(16) -, player_hist() { - TextureIndex tex_index; - master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index); - chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index); +, res() +, sounds() +, save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host)) +, world(res.block_types, master.GetWorldConf()) +, player(*world.AddPlayer(master.GetConfig().player.name, player_id)) +, hud(master.GetEnv(), master.GetConfig(), player) +, 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()) +, loop_timer(16) +, sky(master.GetEnv().loader.LoadCubeMap("skybox")) +, update_status() +, chat(master.GetEnv(), *this, *this) { + if (!save.Exists()) { + save.Write(master.GetWorldConf()); + } + 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); - skeletons.Load(); - // TODO: better solution for initializing HUD - interface.SelectNext(); - update_timer.Start(); + loop_timer.Start(); + if (save.Exists(player)) { + save.Read(player); + } } void InteractiveState::OnEnter() { @@ -75,7 +87,13 @@ void InteractiveState::OnEnter() { 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) { + master.GetEnv().state.Push(&chat); + hud.KeepMessages(true); + } else { + interface.HandlePress(event.key); + } break; case SDL_KEYUP: interface.HandleRelease(event.key); @@ -93,7 +111,7 @@ void InteractiveState::Handle(const SDL_Event &event) { interface.Handle(event.wheel); break; case SDL_QUIT: - master.Quit(); + Exit(); break; default: break; @@ -101,85 +119,228 @@ 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(res.block_types[player.GetInventorySlot() + 1]); + loop_timer.Update(dt); master.Update(dt); - - interface.Update(dt); - world.Update(dt); + chunk_receiver.Update(dt); + + hud.Update(dt); + int world_dt = 0; + while (loop_timer.HitOnce()) { + world.Update(loop_timer.Interval()); + world_dt += loop_timer.Interval(); + loop_timer.PopIteration(); + } chunk_renderer.Update(dt); - update_timer.Update(dt); - - Entity &player = *interface.GetPlayer().entity; - - if (update_timer.Hit()) { - PushPlayerUpdate(player); + if (world_dt > 0) { + input.PushPlayerUpdate(world_dt); } - glm::mat4 trans = player.Transform(player.ChunkCoords()); + glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords()); glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)); glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)); - master.GetEnv().audio.Position(player.Position()); - master.GetEnv().audio.Velocity(player.Velocity()); + master.GetEnv().audio.Position(player.GetEntity().Position()); + master.GetEnv().audio.Velocity(player.GetEntity().Velocity()); master.GetEnv().audio.Orientation(dir, up); } -void InteractiveState::PushPlayerUpdate(const Entity &player) { - std::uint16_t packet = master.GetClient().SendPlayerUpdate(player); - if (player_hist.size() < 16) { - player_hist.emplace_back(player.GetState(), update_timer.Elapsed(), packet); +void InteractiveState::Render(Viewport &viewport) { + viewport.WorldPosition( + player.GetEntity().Transform(player.GetEntity().ChunkCoords()) + * player.GetEntity().GetModel().EyesTransform()); + if (master.GetConfig().video.world) { + chunk_renderer.Render(viewport); + world.Render(viewport); + sky.Render(viewport); + } + hud.Render(viewport); +} + +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()); + } + cout << "spawned entity #" << entity_id << " (" << entity.Name() + << ") at " << entity.AbsolutePosition() << endl; +} + +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(); + cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl; + 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 { - auto entry = player_hist.begin(); - entry->state = player.GetState(); - entry->timestamp = update_timer.Elapsed(); - entry->packet = packet; - player_hist.splice(player_hist.end(), player_hist, entry); + return false; } } -void InteractiveState::MergePlayerCorrection(uint16_t seq, const EntityState &corrected_state) { - if (player_hist.empty()) return; +void InteractiveState::ClearEntity(uint32_t entity_id) { + update_status.erase(entity_id); +} - auto entry = player_hist.begin(); - auto end = player_hist.end(); +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); +} - // drop anything older than the fix - while (entry != end) { - int pack_diff = int16_t(seq) - int16_t(entry->packet); - if (pack_diff < 0) { - entry = player_hist.erase(entry); - } else { - break; +void InteractiveState::Handle(const Packet::BlockUpdate &pack) { + glm::ivec3 pos; + pack.ReadChunkCoords(pos); + Chunk *chunk = player.GetChunks().Get(pos); + if (!chunk) { + // this change doesn't concern us + return; + } + uint32_t count = 0; + pack.ReadBlockCount(count); + for (uint32_t i = 0; i < count; ++i) { + uint16_t index; + Block block; + pack.ReadIndex(index, i); + pack.ReadBlock(block, i); + if (index < Chunk::size && block.type < res.block_types.size()) { + manip.SetBlock(*chunk, index, block); } } - if (entry == end) return; } -void InteractiveState::Render(Viewport &viewport) { - Entity &player = *interface.GetPlayer().entity; - viewport.WorldPosition(player.Transform(player.ChunkCoords())); - chunk_renderer.Render(viewport); - world.Render(viewport); - interface.Render(viewport); +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) { + hud.PostMessage("Audio enabled"); + } else { + hud.PostMessage("Audio disabled"); + } +} + +void InteractiveState::SetVideo(bool b) { + master.GetConfig().video.world = b; + if (b) { + hud.PostMessage("World rendering enabled"); + } else { + hud.PostMessage("World rendering disabled"); + } +} + +void InteractiveState::SetHUD(bool b) { + master.GetConfig().video.hud = b; + if (b) { + hud.PostMessage("HUD rendering enabled"); + } else { + hud.PostMessage("HUD rendering disabled"); + } +} + +void InteractiveState::SetDebug(bool b) { + master.GetConfig().video.debug = b; + if (b) { + hud.PostMessage("Debug rendering enabled"); + } else { + hud.PostMessage("Debug rendering disabled"); + } +} + +void InteractiveState::Exit() { + save.Write(player); + master.Quit(); +} + +void InteractiveState::OnLineSubmit(const string &line) { + if (!line.empty()) { + master.GetClient().SendMessage(1, 0, line); + } } MasterState::MasterState( Environment &env, - const World::Config &wc, - const Interface::Config &ic, - const Client::Config &cc) + Config &config, + const World::Config &wc) : env(env) +, config(config) , world_conf(wc) -, intf_conf(ic) -, client_conf(cc) , state() -, client(cc) +, 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() { @@ -191,7 +352,7 @@ void MasterState::Quit() { void MasterState::OnEnter() { - login_packet = client.SendLogin(intf_conf.player_name); + login_packet = client.SendLogin(config.player.name); env.state.Push(&init_state); } @@ -202,7 +363,6 @@ void MasterState::Handle(const SDL_Event &event) { void MasterState::Update(int dt) { - update_timer.Update(dt); client.Handle(); client.Update(dt); } @@ -215,15 +375,14 @@ void MasterState::Render(Viewport &) { void MasterState::OnPacketLost(uint16_t id) { if (id == login_packet) { - login_packet = client.SendLogin(intf_conf.player_name); + login_packet = client.SendLogin(config.player.name); } } void MasterState::OnTimeout() { if (client.GetConnection().Closed()) { - // TODO: push disconnected message - cout << "connection timed out" << endl; Quit(); + env.ShowMessage("connection timed out"); } } @@ -244,127 +403,91 @@ void MasterState::On(const Packet::Join &pack) { pack.ReadPlayerID(player_id); state.reset(new InteractiveState(*this, player_id)); - pack.ReadPlayerState(state->GetInterface().GetPlayer().entity->GetState()); + EntityState player_state; + pack.ReadPlayerState(player_state); + state->GetPlayer().GetEntity().SetState(player_state); env.state.PopAfter(this); env.state.Push(state.get()); } void MasterState::On(const Packet::Part &pack) { + Quit(); if (state) { // kicked - cout << "kicked by server" << endl; + env.ShowMessage("kicked by server"); } else { // join refused - cout << "login refused by server" << endl; + env.ShowMessage("login refused by server"); } - Quit(); } 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.Name() << " at " << entity.AbsolutePosition() << endl; + state->Handle(pack); } 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.Name() << " at " << entity.AbsolutePosition() << endl; - return; - } - } + state->Handle(pack); } 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); - } - } - } + state->Handle(pack); } -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; +void MasterState::On(const Packet::PlayerCorrection &pack) { + if (!state) { + cout << "got player correction without a player :S" << endl; + return; } + state->Handle(pack); +} - int 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::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::ClearEntity(uint32_t entity_id) { - update_status.erase(entity_id); +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::PlayerCorrection &pack) { +void MasterState::On(const Packet::BlockUpdate &pack) { if (!state) { - cout << "got player correction without a player :S" << endl; - Quit(); + cout << "received block update, but the world has not been created yet" << 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::Message &pack) { + if (state) { + state->Handle(pack); + } else { + string msg; + pack.ReadMessage(msg); + cout << "got message before interface was created: " << msg << endl; + } } }