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)
54 , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host))
55 , world(block_types, master.GetWorldConf())
56 , player(*world.AddPlayer(master.GetConfig().player.name))
57 , hud(master.GetEnv(), master.GetConfig(), player)
58 , manip(master.GetEnv(), player.GetEntity())
59 , input(world, player, master.GetClient())
60 , interface(master.GetConfig(), master.GetEnv().keymap, input, *this)
61 , chunk_receiver(world.Chunks(), save)
62 , chunk_renderer(player.GetChunks())
64 , sky(master.GetEnv().loader.LoadCubeMap("skybox"))
67 save.Write(master.GetWorldConf());
69 TextureIndex tex_index;
70 master.GetEnv().loader.LoadShapes("default", shapes);
71 master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index, shapes);
72 master.GetEnv().loader.LoadModels("default", models, tex_index, shapes);
73 interface.SetInventorySlots(block_types.size() - 1);
74 chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index);
75 chunk_renderer.FogDensity(master.GetWorldConf().fog_density);
77 if (save.Exists(player)) {
82 void InteractiveState::OnEnter() {
83 master.GetEnv().window.GrabMouse();
86 void InteractiveState::Handle(const SDL_Event &event) {
89 interface.HandlePress(event.key);
92 interface.HandleRelease(event.key);
94 case SDL_MOUSEBUTTONDOWN:
95 interface.HandlePress(event.button);
97 case SDL_MOUSEBUTTONUP:
98 interface.HandleRelease(event.button);
100 case SDL_MOUSEMOTION:
101 interface.Handle(event.motion);
104 interface.Handle(event.wheel);
114 void InteractiveState::Update(int dt) {
116 if (input.BlockFocus()) {
117 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
118 } else if (input.EntityFocus()) {
119 hud.FocusEntity(*input.EntityFocus().entity);
123 hud.Display(block_types[player.GetInventorySlot() + 1]);
124 loop_timer.Update(dt);
126 chunk_receiver.Update(dt);
130 while (loop_timer.HitOnce()) {
131 world.Update(loop_timer.Interval());
132 world_dt += loop_timer.Interval();
133 loop_timer.PopIteration();
135 chunk_renderer.Update(dt);
138 input.PushPlayerUpdate(world_dt);
141 glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
142 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
143 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
144 master.GetEnv().audio.Position(player.GetEntity().Position());
145 master.GetEnv().audio.Velocity(player.GetEntity().Velocity());
146 master.GetEnv().audio.Orientation(dir, up);
149 void InteractiveState::Render(Viewport &viewport) {
150 viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords()));
151 if (master.GetConfig().video.world) {
152 chunk_renderer.Render(viewport);
153 world.Render(viewport);
154 sky.Render(viewport);
156 hud.Render(viewport);
159 void InteractiveState::Handle(const Packet::SpawnEntity &pack) {
161 pack.ReadEntityID(entity_id);
162 Entity &entity = world.ForceAddEntity(entity_id);
163 UpdateEntity(entity_id, pack.Seq());
164 pack.ReadEntity(entity);
166 pack.ReadSkeletonID(skel_id);
167 if (skel_id > 0 && skel_id <= models.size()) {
168 Model &skel = models.Get(skel_id);
169 skel.Instantiate(entity.GetModel());
171 cout << "spawned entity #" << entity_id << " (" << entity.Name()
172 << ") at " << entity.AbsolutePosition() << endl;
175 void InteractiveState::Handle(const Packet::DespawnEntity &pack) {
177 pack.ReadEntityID(entity_id);
178 ClearEntity(entity_id);
179 for (Entity &entity : world.Entities()) {
180 if (entity.ID() == entity_id) {
182 cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl;
188 void InteractiveState::Handle(const Packet::EntityUpdate &pack) {
189 auto world_iter = world.Entities().begin();
190 auto world_end = world.Entities().end();
193 pack.ReadEntityCount(count);
195 for (uint32_t i = 0; i < count; ++i) {
196 uint32_t entity_id = 0;
197 pack.ReadEntityID(entity_id, i);
199 while (world_iter != world_end && world_iter->ID() < entity_id) {
202 if (world_iter == world_end) {
203 // nothing can be done from here
206 if (world_iter->ID() == entity_id) {
207 if (UpdateEntity(entity_id, pack.Seq())) {
208 pack.ReadEntityState(world_iter->GetState(), i);
214 bool InteractiveState::UpdateEntity(uint32_t entity_id, uint16_t seq) {
215 auto entry = update_status.find(entity_id);
216 if (entry == update_status.end()) {
217 update_status.emplace(entity_id, UpdateStatus{ seq, loop_timer.Elapsed() });
221 int16_t pack_diff = int16_t(seq) - int16_t(entry->second.last_packet);
222 int time_diff = loop_timer.Elapsed() - entry->second.last_update;
223 entry->second.last_update = loop_timer.Elapsed();
225 if (pack_diff > 0 || time_diff > 1500) {
226 entry->second.last_packet = seq;
233 void InteractiveState::ClearEntity(uint32_t entity_id) {
234 update_status.erase(entity_id);
237 void InteractiveState::Handle(const Packet::PlayerCorrection &pack) {
239 EntityState corrected_state;
240 pack.ReadPacketSeq(pack_seq);
241 pack.ReadPlayerState(corrected_state);
242 input.MergePlayerCorrection(pack_seq, corrected_state);
245 void InteractiveState::Handle(const Packet::BlockUpdate &pack) {
247 pack.ReadChunkCoords(pos);
248 Chunk *chunk = player.GetChunks().Get(pos);
250 // this change doesn't concern us
254 pack.ReadBlockCount(count);
255 for (uint32_t i = 0; i < count; ++i) {
258 pack.ReadIndex(index, i);
259 pack.ReadBlock(block, i);
260 if (index < Chunk::size && block.type < block_types.size()) {
261 manip.SetBlock(*chunk, index, block);
266 void InteractiveState::SetAudio(bool b) {
267 master.GetConfig().audio.enabled = b;
269 hud.PostMessage("Audio enabled");
271 hud.PostMessage("Audio disabled");
275 void InteractiveState::SetVideo(bool b) {
276 master.GetConfig().video.world = b;
278 hud.PostMessage("World rendering enabled");
280 hud.PostMessage("World rendering disabled");
284 void InteractiveState::SetHUD(bool b) {
285 master.GetConfig().video.hud = b;
287 hud.PostMessage("HUD rendering enabled");
289 hud.PostMessage("HUD rendering disabled");
293 void InteractiveState::SetDebug(bool b) {
294 master.GetConfig().video.debug = b;
296 hud.PostMessage("Debug rendering enabled");
298 hud.PostMessage("Debug rendering disabled");
302 void InteractiveState::Exit() {
308 MasterState::MasterState(
311 const World::Config &wc)
319 client.GetConnection().SetHandler(this);
322 void MasterState::Quit() {
323 if (!client.GetConnection().Closed()) {
326 env.state.PopUntil(this);
330 void MasterState::OnEnter() {
331 login_packet = client.SendLogin(config.player.name);
332 env.state.Push(&init_state);
336 void MasterState::Handle(const SDL_Event &event) {
341 void MasterState::Update(int dt) {
347 void MasterState::Render(Viewport &) {
352 void MasterState::OnPacketLost(uint16_t id) {
353 if (id == login_packet) {
354 login_packet = client.SendLogin(config.player.name);
358 void MasterState::OnTimeout() {
359 if (client.GetConnection().Closed()) {
361 env.ShowMessage("connection timed out");
365 void MasterState::On(const Packet::Join &pack) {
366 pack.ReadWorldName(world_conf.name);
370 cout << "server changing worlds to \"" << world_conf.name << '"' << endl;
373 cout << "joined game \"" << world_conf.name << '"' << endl;
374 // server received our login
379 pack.ReadPlayerID(player_id);
380 state.reset(new InteractiveState(*this, player_id));
382 pack.ReadPlayerState(state->GetPlayer().GetEntity().GetState());
384 env.state.PopAfter(this);
385 env.state.Push(state.get());
388 void MasterState::On(const Packet::Part &pack) {
392 env.ShowMessage("kicked by server");
395 env.ShowMessage("login refused by server");
399 void MasterState::On(const Packet::SpawnEntity &pack) {
401 cout << "got entity spawn before world was created" << endl;
407 void MasterState::On(const Packet::DespawnEntity &pack) {
409 cout << "got entity despawn before world was created" << endl;
415 void MasterState::On(const Packet::EntityUpdate &pack) {
417 cout << "got entity update before world was created" << endl;
423 void MasterState::On(const Packet::PlayerCorrection &pack) {
425 cout << "got player correction without a player :S" << endl;
431 void MasterState::On(const Packet::ChunkBegin &pack) {
433 cout << "got chunk data, but the world has not been created yet" << endl;
434 cout << "great, this will totally screw up everything :(" << endl;
437 state->GetChunkReceiver().Handle(pack);
440 void MasterState::On(const Packet::ChunkData &pack) {
442 cout << "got chunk data, but the world has not been created yet" << endl;
443 cout << "great, this will totally screw up everything :(" << endl;
446 state->GetChunkReceiver().Handle(pack);
449 void MasterState::On(const Packet::BlockUpdate &pack) {
451 cout << "received block update, but the world has not been created yet" << endl;