1 #include "InitialState.hpp"
2 #include "InteractiveState.hpp"
3 #include "MasterState.hpp"
5 #include "../app/Environment.hpp"
6 #include "../app/init.hpp"
7 #include "../app/TextureIndex.hpp"
8 #include "../model/Model.hpp"
9 #include "../io/WorldSave.hpp"
10 #include "../world/ChunkIndex.hpp"
11 #include "../world/ChunkStore.hpp"
14 #include <glm/gtx/io.hpp>
22 InitialState::InitialState(MasterState &master)
25 message.Position(glm::vec3(0.0f), Gravity::CENTER);
26 message.Set(master.GetEnv().assets.large_ui_font, "logging in");
29 void InitialState::OnEnter() {
33 void InitialState::Handle(const SDL_Event &evt) {
34 if (evt.type == SDL_QUIT) {
39 void InitialState::Update(int dt) {
43 void InitialState::Render(Viewport &viewport) {
44 message.Render(viewport);
48 // TODO: this clutter is a giant mess
49 InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
52 , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host))
53 , world(block_types, master.GetWorldConf())
54 , player(*world.AddPlayer(master.GetConfig().player.name))
55 , hud(master.GetEnv(), master.GetConfig(), player)
56 , manip(master.GetEnv(), player.GetEntity())
57 , input(world, player, master.GetClient())
58 , interface(master.GetConfig(), master.GetEnv().keymap, input, *this)
59 , chunk_receiver(world.Chunks(), save)
60 , chunk_renderer(player.GetChunks())
63 , sky(master.GetEnv().loader.LoadCubeMap("skybox")) {
65 save.Write(master.GetWorldConf());
67 TextureIndex tex_index;
68 master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index);
69 interface.SetInventorySlots(block_types.size() - 1);
70 chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index);
71 chunk_renderer.FogDensity(master.GetWorldConf().fog_density);
74 if (save.Exists(player)) {
79 void InteractiveState::OnEnter() {
80 master.GetEnv().window.GrabMouse();
83 void InteractiveState::Handle(const SDL_Event &event) {
86 interface.HandlePress(event.key);
89 interface.HandleRelease(event.key);
91 case SDL_MOUSEBUTTONDOWN:
92 interface.HandlePress(event.button);
94 case SDL_MOUSEBUTTONUP:
95 interface.HandleRelease(event.button);
98 interface.Handle(event.motion);
101 interface.Handle(event.wheel);
111 void InteractiveState::Update(int dt) {
113 if (input.BlockFocus()) {
114 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
115 } else if (input.EntityFocus()) {
116 hud.FocusEntity(*input.EntityFocus().entity);
120 hud.Display(block_types[player.GetInventorySlot() + 1]);
121 loop_timer.Update(dt);
123 chunk_receiver.Update(dt);
127 while (loop_timer.HitOnce()) {
128 world.Update(loop_timer.Interval());
129 world_dt += loop_timer.Interval();
130 loop_timer.PopIteration();
132 chunk_renderer.Update(dt);
135 input.PushPlayerUpdate(world_dt);
138 glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
139 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
140 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
141 master.GetEnv().audio.Position(player.GetEntity().Position());
142 master.GetEnv().audio.Velocity(player.GetEntity().Velocity());
143 master.GetEnv().audio.Orientation(dir, up);
146 void InteractiveState::Render(Viewport &viewport) {
147 viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords()));
148 if (master.GetConfig().video.world) {
149 chunk_renderer.Render(viewport);
150 world.Render(viewport);
151 sky.Render(viewport);
153 hud.Render(viewport);
156 void InteractiveState::MergePlayerCorrection(std::uint16_t pack, const EntityState &state) {
157 input.MergePlayerCorrection(pack, state);
160 void InteractiveState::Handle(const Packet::BlockUpdate &pack) {
162 pack.ReadChunkCoords(pos);
163 Chunk *chunk = player.GetChunks().Get(pos);
165 // this change doesn't concern us
169 pack.ReadBlockCount(count);
170 for (uint32_t i = 0; i < count; ++i) {
173 pack.ReadIndex(index, i);
174 pack.ReadBlock(block, i);
175 if (index < Chunk::size && block.type < block_types.size()) {
176 manip.SetBlock(*chunk, index, block);
181 void InteractiveState::SetAudio(bool b) {
182 master.GetConfig().audio.enabled = b;
184 hud.PostMessage("Audio enabled");
186 hud.PostMessage("Audio disabled");
190 void InteractiveState::SetVideo(bool b) {
191 master.GetConfig().video.world = b;
193 hud.PostMessage("World rendering enabled");
195 hud.PostMessage("World rendering disabled");
199 void InteractiveState::SetHUD(bool b) {
200 master.GetConfig().video.hud = b;
202 hud.PostMessage("HUD rendering enabled");
204 hud.PostMessage("HUD rendering disabled");
208 void InteractiveState::SetDebug(bool b) {
209 master.GetConfig().video.debug = b;
211 hud.PostMessage("Debug rendering enabled");
213 hud.PostMessage("Debug rendering disabled");
217 void InteractiveState::Exit() {
223 MasterState::MasterState(
226 const World::Config &wc)
236 client.GetConnection().SetHandler(this);
237 update_timer.Start();
240 void MasterState::Quit() {
241 if (!client.GetConnection().Closed()) {
244 env.state.PopUntil(this);
248 void MasterState::OnEnter() {
249 login_packet = client.SendLogin(config.player.name);
250 env.state.Push(&init_state);
254 void MasterState::Handle(const SDL_Event &event) {
259 void MasterState::Update(int dt) {
260 update_timer.Update(dt);
266 void MasterState::Render(Viewport &) {
271 void MasterState::OnPacketLost(uint16_t id) {
272 if (id == login_packet) {
273 login_packet = client.SendLogin(config.player.name);
277 void MasterState::OnTimeout() {
278 if (client.GetConnection().Closed()) {
280 env.ShowMessage("connection timed out");
284 void MasterState::On(const Packet::Join &pack) {
285 pack.ReadWorldName(world_conf.name);
289 cout << "server changing worlds to \"" << world_conf.name << '"' << endl;
292 cout << "joined game \"" << world_conf.name << '"' << endl;
293 // server received our login
298 pack.ReadPlayerID(player_id);
299 state.reset(new InteractiveState(*this, player_id));
301 pack.ReadPlayerState(state->GetPlayer().GetEntity().GetState());
303 env.state.PopAfter(this);
304 env.state.Push(state.get());
307 void MasterState::On(const Packet::Part &pack) {
311 env.ShowMessage("kicked by server");
314 env.ShowMessage("login refused by server");
318 void MasterState::On(const Packet::SpawnEntity &pack) {
320 cout << "got entity spawn before world was created" << endl;
324 pack.ReadEntityID(entity_id);
325 Entity &entity = state->GetWorld().ForceAddEntity(entity_id);
326 UpdateEntity(entity_id, pack.Seq());
327 pack.ReadEntity(entity);
329 pack.ReadSkeletonID(skel_id);
330 Model *skel = state->GetSkeletons().ByID(skel_id);
332 skel->Instantiate(entity.GetModel());
334 cout << "spawned entity #" << entity_id << " (" << entity.Name()
335 << ") at " << entity.AbsolutePosition() << endl;
338 void MasterState::On(const Packet::DespawnEntity &pack) {
340 cout << "got entity despawn before world was created" << endl;
344 pack.ReadEntityID(entity_id);
345 ClearEntity(entity_id);
346 for (Entity &entity : state->GetWorld().Entities()) {
347 if (entity.ID() == entity_id) {
349 cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl;
355 void MasterState::On(const Packet::EntityUpdate &pack) {
357 cout << "got entity update before world was created" << endl;
361 auto world_iter = state->GetWorld().Entities().begin();
362 auto world_end = state->GetWorld().Entities().end();
365 pack.ReadEntityCount(count);
367 for (uint32_t i = 0; i < count; ++i) {
368 uint32_t entity_id = 0;
369 pack.ReadEntityID(entity_id, i);
371 while (world_iter != world_end && world_iter->ID() < entity_id) {
374 if (world_iter == world_end) {
375 // nothing can be done from here
378 if (world_iter->ID() == entity_id) {
379 if (UpdateEntity(entity_id, pack.Seq())) {
380 pack.ReadEntityState(world_iter->GetState(), i);
386 bool MasterState::UpdateEntity(uint32_t entity_id, uint16_t seq) {
387 auto entry = update_status.find(entity_id);
388 if (entry == update_status.end()) {
389 update_status.emplace(entity_id, UpdateStatus{ seq, update_timer.Elapsed() });
393 int16_t pack_diff = int16_t(seq) - int16_t(entry->second.last_packet);
394 int time_diff = update_timer.Elapsed() - entry->second.last_update;
395 entry->second.last_update = update_timer.Elapsed();
397 if (pack_diff > 0 || time_diff > 1500) {
398 entry->second.last_packet = seq;
405 void MasterState::ClearEntity(uint32_t entity_id) {
406 update_status.erase(entity_id);
409 void MasterState::On(const Packet::PlayerCorrection &pack) {
411 cout << "got player correction without a player :S" << endl;
415 EntityState corrected_state;
416 pack.ReadPacketSeq(pack_seq);
417 pack.ReadPlayerState(corrected_state);
418 state->MergePlayerCorrection(pack_seq, corrected_state);
421 void MasterState::On(const Packet::ChunkBegin &pack) {
423 cout << "got chunk data, but the world has not been created yet" << endl;
424 cout << "great, this will totally screw up everything :(" << endl;
427 state->GetChunkReceiver().Handle(pack);
430 void MasterState::On(const Packet::ChunkData &pack) {
432 cout << "got chunk data, but the world has not been created yet" << endl;
433 cout << "great, this will totally screw up everything :(" << endl;
436 state->GetChunkReceiver().Handle(pack);
439 void MasterState::On(const Packet::BlockUpdate &pack) {
441 cout << "received block update, but the world has not been created yet" << endl;