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)
53 , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host))
54 , world(block_types, master.GetWorldConf())
55 , player(*world.AddPlayer(master.GetConfig().player.name))
56 , hud(master.GetEnv(), master.GetConfig(), player)
57 , manip(master.GetEnv(), player.GetEntity())
58 , input(world, player, master.GetClient())
59 , interface(master.GetConfig(), master.GetEnv().keymap, input, *this)
60 , chunk_receiver(world.Chunks(), save)
61 , chunk_renderer(player.GetChunks())
64 , sky(master.GetEnv().loader.LoadCubeMap("skybox")) {
66 save.Write(master.GetWorldConf());
68 TextureIndex tex_index;
69 master.GetEnv().loader.LoadShapes("default", shapes);
70 master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index, shapes);
71 skeletons.Load(shapes, tex_index);
72 interface.SetInventorySlots(block_types.size() - 1);
73 chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index);
74 chunk_renderer.FogDensity(master.GetWorldConf().fog_density);
76 if (save.Exists(player)) {
81 void InteractiveState::OnEnter() {
82 master.GetEnv().window.GrabMouse();
85 void InteractiveState::Handle(const SDL_Event &event) {
88 interface.HandlePress(event.key);
91 interface.HandleRelease(event.key);
93 case SDL_MOUSEBUTTONDOWN:
94 interface.HandlePress(event.button);
96 case SDL_MOUSEBUTTONUP:
97 interface.HandleRelease(event.button);
100 interface.Handle(event.motion);
103 interface.Handle(event.wheel);
113 void InteractiveState::Update(int dt) {
115 if (input.BlockFocus()) {
116 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
117 } else if (input.EntityFocus()) {
118 hud.FocusEntity(*input.EntityFocus().entity);
122 hud.Display(block_types[player.GetInventorySlot() + 1]);
123 loop_timer.Update(dt);
125 chunk_receiver.Update(dt);
129 while (loop_timer.HitOnce()) {
130 world.Update(loop_timer.Interval());
131 world_dt += loop_timer.Interval();
132 loop_timer.PopIteration();
134 chunk_renderer.Update(dt);
137 input.PushPlayerUpdate(world_dt);
140 glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
141 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
142 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
143 master.GetEnv().audio.Position(player.GetEntity().Position());
144 master.GetEnv().audio.Velocity(player.GetEntity().Velocity());
145 master.GetEnv().audio.Orientation(dir, up);
148 void InteractiveState::Render(Viewport &viewport) {
149 viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords()));
150 if (master.GetConfig().video.world) {
151 chunk_renderer.Render(viewport);
152 world.Render(viewport);
153 sky.Render(viewport);
155 hud.Render(viewport);
158 void InteractiveState::MergePlayerCorrection(std::uint16_t pack, const EntityState &state) {
159 input.MergePlayerCorrection(pack, state);
162 void InteractiveState::Handle(const Packet::BlockUpdate &pack) {
164 pack.ReadChunkCoords(pos);
165 Chunk *chunk = player.GetChunks().Get(pos);
167 // this change doesn't concern us
171 pack.ReadBlockCount(count);
172 for (uint32_t i = 0; i < count; ++i) {
175 pack.ReadIndex(index, i);
176 pack.ReadBlock(block, i);
177 if (index < Chunk::size && block.type < block_types.size()) {
178 manip.SetBlock(*chunk, index, block);
183 void InteractiveState::SetAudio(bool b) {
184 master.GetConfig().audio.enabled = b;
186 hud.PostMessage("Audio enabled");
188 hud.PostMessage("Audio disabled");
192 void InteractiveState::SetVideo(bool b) {
193 master.GetConfig().video.world = b;
195 hud.PostMessage("World rendering enabled");
197 hud.PostMessage("World rendering disabled");
201 void InteractiveState::SetHUD(bool b) {
202 master.GetConfig().video.hud = b;
204 hud.PostMessage("HUD rendering enabled");
206 hud.PostMessage("HUD rendering disabled");
210 void InteractiveState::SetDebug(bool b) {
211 master.GetConfig().video.debug = b;
213 hud.PostMessage("Debug rendering enabled");
215 hud.PostMessage("Debug rendering disabled");
219 void InteractiveState::Exit() {
225 MasterState::MasterState(
228 const World::Config &wc)
238 client.GetConnection().SetHandler(this);
239 update_timer.Start();
242 void MasterState::Quit() {
243 if (!client.GetConnection().Closed()) {
246 env.state.PopUntil(this);
250 void MasterState::OnEnter() {
251 login_packet = client.SendLogin(config.player.name);
252 env.state.Push(&init_state);
256 void MasterState::Handle(const SDL_Event &event) {
261 void MasterState::Update(int dt) {
262 update_timer.Update(dt);
268 void MasterState::Render(Viewport &) {
273 void MasterState::OnPacketLost(uint16_t id) {
274 if (id == login_packet) {
275 login_packet = client.SendLogin(config.player.name);
279 void MasterState::OnTimeout() {
280 if (client.GetConnection().Closed()) {
282 env.ShowMessage("connection timed out");
286 void MasterState::On(const Packet::Join &pack) {
287 pack.ReadWorldName(world_conf.name);
291 cout << "server changing worlds to \"" << world_conf.name << '"' << endl;
294 cout << "joined game \"" << world_conf.name << '"' << endl;
295 // server received our login
300 pack.ReadPlayerID(player_id);
301 state.reset(new InteractiveState(*this, player_id));
303 pack.ReadPlayerState(state->GetPlayer().GetEntity().GetState());
305 env.state.PopAfter(this);
306 env.state.Push(state.get());
309 void MasterState::On(const Packet::Part &pack) {
313 env.ShowMessage("kicked by server");
316 env.ShowMessage("login refused by server");
320 void MasterState::On(const Packet::SpawnEntity &pack) {
322 cout << "got entity spawn before world was created" << endl;
326 pack.ReadEntityID(entity_id);
327 Entity &entity = state->GetWorld().ForceAddEntity(entity_id);
328 UpdateEntity(entity_id, pack.Seq());
329 pack.ReadEntity(entity);
331 pack.ReadSkeletonID(skel_id);
332 Model *skel = state->GetSkeletons().ByID(skel_id);
334 skel->Instantiate(entity.GetModel());
336 cout << "spawned entity #" << entity_id << " (" << entity.Name()
337 << ") at " << entity.AbsolutePosition() << endl;
340 void MasterState::On(const Packet::DespawnEntity &pack) {
342 cout << "got entity despawn before world was created" << endl;
346 pack.ReadEntityID(entity_id);
347 ClearEntity(entity_id);
348 for (Entity &entity : state->GetWorld().Entities()) {
349 if (entity.ID() == entity_id) {
351 cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl;
357 void MasterState::On(const Packet::EntityUpdate &pack) {
359 cout << "got entity update before world was created" << endl;
363 auto world_iter = state->GetWorld().Entities().begin();
364 auto world_end = state->GetWorld().Entities().end();
367 pack.ReadEntityCount(count);
369 for (uint32_t i = 0; i < count; ++i) {
370 uint32_t entity_id = 0;
371 pack.ReadEntityID(entity_id, i);
373 while (world_iter != world_end && world_iter->ID() < entity_id) {
376 if (world_iter == world_end) {
377 // nothing can be done from here
380 if (world_iter->ID() == entity_id) {
381 if (UpdateEntity(entity_id, pack.Seq())) {
382 pack.ReadEntityState(world_iter->GetState(), i);
388 bool MasterState::UpdateEntity(uint32_t entity_id, uint16_t seq) {
389 auto entry = update_status.find(entity_id);
390 if (entry == update_status.end()) {
391 update_status.emplace(entity_id, UpdateStatus{ seq, update_timer.Elapsed() });
395 int16_t pack_diff = int16_t(seq) - int16_t(entry->second.last_packet);
396 int time_diff = update_timer.Elapsed() - entry->second.last_update;
397 entry->second.last_update = update_timer.Elapsed();
399 if (pack_diff > 0 || time_diff > 1500) {
400 entry->second.last_packet = seq;
407 void MasterState::ClearEntity(uint32_t entity_id) {
408 update_status.erase(entity_id);
411 void MasterState::On(const Packet::PlayerCorrection &pack) {
413 cout << "got player correction without a player :S" << endl;
417 EntityState corrected_state;
418 pack.ReadPacketSeq(pack_seq);
419 pack.ReadPlayerState(corrected_state);
420 state->MergePlayerCorrection(pack_seq, corrected_state);
423 void MasterState::On(const Packet::ChunkBegin &pack) {
425 cout << "got chunk data, but the world has not been created yet" << endl;
426 cout << "great, this will totally screw up everything :(" << endl;
429 state->GetChunkReceiver().Handle(pack);
432 void MasterState::On(const Packet::ChunkData &pack) {
434 cout << "got chunk data, but the world has not been created yet" << endl;
435 cout << "great, this will totally screw up everything :(" << endl;
438 state->GetChunkReceiver().Handle(pack);
441 void MasterState::On(const Packet::BlockUpdate &pack) {
443 cout << "received block update, but the world has not been created yet" << endl;