1 #include "InitialState.hpp"
2 #include "InteractiveState.hpp"
3 #include "MasterState.hpp"
5 #include "../app/Environment.hpp"
6 #include "../app/init.hpp"
7 #include "../model/Model.hpp"
8 #include "../io/WorldSave.hpp"
9 #include "../world/ChunkIndex.hpp"
10 #include "../world/ChunkStore.hpp"
13 #include <glm/gtx/io.hpp>
21 InitialState::InitialState(MasterState &master)
24 message.Position(glm::vec3(0.0f), Gravity::CENTER);
25 message.Set(master.GetEnv().assets.large_ui_font, "logging in");
28 void InitialState::OnEnter() {
32 void InitialState::Handle(const SDL_Event &evt) {
33 if (evt.type == SDL_QUIT) {
38 void InitialState::Update(int dt) {
42 void InitialState::Render(Viewport &viewport) {
43 message.Render(viewport);
47 // TODO: this clutter is a giant mess
48 InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
52 , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host))
53 , world(res.block_types, master.GetWorldConf())
54 , player(*world.AddPlayer(master.GetConfig().player.name))
55 , hud(master.GetEnv(), master.GetConfig(), player)
56 , manip(master.GetEnv().audio, sounds, 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())
62 , sky(master.GetEnv().loader.LoadCubeMap("skybox"))
65 save.Write(master.GetWorldConf());
67 res.Load(master.GetEnv().loader, "default");
68 sounds.Load(master.GetEnv().loader, res.snd_index);
69 interface.SetInventorySlots(res.block_types.size() - 1);
70 chunk_renderer.LoadTextures(master.GetEnv().loader, res.tex_index);
71 chunk_renderer.FogDensity(master.GetWorldConf().fog_density);
73 if (save.Exists(player)) {
78 void InteractiveState::OnEnter() {
79 master.GetEnv().window.GrabMouse();
82 void InteractiveState::Handle(const SDL_Event &event) {
85 interface.HandlePress(event.key);
88 interface.HandleRelease(event.key);
90 case SDL_MOUSEBUTTONDOWN:
91 interface.HandlePress(event.button);
93 case SDL_MOUSEBUTTONUP:
94 interface.HandleRelease(event.button);
97 interface.Handle(event.motion);
100 interface.Handle(event.wheel);
110 void InteractiveState::Update(int dt) {
112 if (input.BlockFocus()) {
113 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
114 } else if (input.EntityFocus()) {
115 hud.FocusEntity(*input.EntityFocus().entity);
119 hud.Display(res.block_types[player.GetInventorySlot() + 1]);
120 loop_timer.Update(dt);
122 chunk_receiver.Update(dt);
126 while (loop_timer.HitOnce()) {
127 world.Update(loop_timer.Interval());
128 world_dt += loop_timer.Interval();
129 loop_timer.PopIteration();
131 chunk_renderer.Update(dt);
134 input.PushPlayerUpdate(world_dt);
137 glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
138 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
139 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
140 master.GetEnv().audio.Position(player.GetEntity().Position());
141 master.GetEnv().audio.Velocity(player.GetEntity().Velocity());
142 master.GetEnv().audio.Orientation(dir, up);
145 void InteractiveState::Render(Viewport &viewport) {
146 viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords()));
147 if (master.GetConfig().video.world) {
148 chunk_renderer.Render(viewport);
149 world.Render(viewport);
150 sky.Render(viewport);
152 hud.Render(viewport);
155 void InteractiveState::Handle(const Packet::SpawnEntity &pack) {
157 pack.ReadEntityID(entity_id);
158 Entity &entity = world.ForceAddEntity(entity_id);
159 UpdateEntity(entity_id, pack.Seq());
160 pack.ReadEntity(entity);
162 pack.ReadSkeletonID(skel_id);
163 if (skel_id > 0 && skel_id <= res.models.size()) {
164 Model &skel = res.models.Get(skel_id);
165 skel.Instantiate(entity.GetModel());
167 cout << "spawned entity #" << entity_id << " (" << entity.Name()
168 << ") at " << entity.AbsolutePosition() << endl;
171 void InteractiveState::Handle(const Packet::DespawnEntity &pack) {
173 pack.ReadEntityID(entity_id);
174 ClearEntity(entity_id);
175 for (Entity &entity : world.Entities()) {
176 if (entity.ID() == entity_id) {
178 cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl;
184 void InteractiveState::Handle(const Packet::EntityUpdate &pack) {
185 auto world_iter = world.Entities().begin();
186 auto world_end = world.Entities().end();
189 pack.ReadEntityCount(count);
191 for (uint32_t i = 0; i < count; ++i) {
192 uint32_t entity_id = 0;
193 pack.ReadEntityID(entity_id, i);
195 while (world_iter != world_end && world_iter->ID() < entity_id) {
198 if (world_iter == world_end) {
199 // nothing can be done from here
202 if (world_iter->ID() == entity_id) {
203 if (UpdateEntity(entity_id, pack.Seq())) {
204 pack.ReadEntityState(world_iter->GetState(), i);
210 bool InteractiveState::UpdateEntity(uint32_t entity_id, uint16_t seq) {
211 auto entry = update_status.find(entity_id);
212 if (entry == update_status.end()) {
213 update_status.emplace(entity_id, UpdateStatus{ seq, loop_timer.Elapsed() });
217 int16_t pack_diff = int16_t(seq) - int16_t(entry->second.last_packet);
218 int time_diff = loop_timer.Elapsed() - entry->second.last_update;
219 entry->second.last_update = loop_timer.Elapsed();
221 if (pack_diff > 0 || time_diff > 1500) {
222 entry->second.last_packet = seq;
229 void InteractiveState::ClearEntity(uint32_t entity_id) {
230 update_status.erase(entity_id);
233 void InteractiveState::Handle(const Packet::PlayerCorrection &pack) {
235 EntityState corrected_state;
236 pack.ReadPacketSeq(pack_seq);
237 pack.ReadPlayerState(corrected_state);
238 input.MergePlayerCorrection(pack_seq, corrected_state);
241 void InteractiveState::Handle(const Packet::BlockUpdate &pack) {
243 pack.ReadChunkCoords(pos);
244 Chunk *chunk = player.GetChunks().Get(pos);
246 // this change doesn't concern us
250 pack.ReadBlockCount(count);
251 for (uint32_t i = 0; i < count; ++i) {
254 pack.ReadIndex(index, i);
255 pack.ReadBlock(block, i);
256 if (index < Chunk::size && block.type < res.block_types.size()) {
257 manip.SetBlock(*chunk, index, block);
262 void InteractiveState::SetAudio(bool b) {
263 master.GetConfig().audio.enabled = b;
265 hud.PostMessage("Audio enabled");
267 hud.PostMessage("Audio disabled");
271 void InteractiveState::SetVideo(bool b) {
272 master.GetConfig().video.world = b;
274 hud.PostMessage("World rendering enabled");
276 hud.PostMessage("World rendering disabled");
280 void InteractiveState::SetHUD(bool b) {
281 master.GetConfig().video.hud = b;
283 hud.PostMessage("HUD rendering enabled");
285 hud.PostMessage("HUD rendering disabled");
289 void InteractiveState::SetDebug(bool b) {
290 master.GetConfig().video.debug = b;
292 hud.PostMessage("Debug rendering enabled");
294 hud.PostMessage("Debug rendering disabled");
298 void InteractiveState::Exit() {
304 MasterState::MasterState(
307 const World::Config &wc)
315 client.GetConnection().SetHandler(this);
318 void MasterState::Quit() {
319 if (!client.GetConnection().Closed()) {
322 env.state.PopUntil(this);
326 void MasterState::OnEnter() {
327 login_packet = client.SendLogin(config.player.name);
328 env.state.Push(&init_state);
332 void MasterState::Handle(const SDL_Event &event) {
337 void MasterState::Update(int dt) {
343 void MasterState::Render(Viewport &) {
348 void MasterState::OnPacketLost(uint16_t id) {
349 if (id == login_packet) {
350 login_packet = client.SendLogin(config.player.name);
354 void MasterState::OnTimeout() {
355 if (client.GetConnection().Closed()) {
357 env.ShowMessage("connection timed out");
361 void MasterState::On(const Packet::Join &pack) {
362 pack.ReadWorldName(world_conf.name);
366 cout << "server changing worlds to \"" << world_conf.name << '"' << endl;
369 cout << "joined game \"" << world_conf.name << '"' << endl;
370 // server received our login
375 pack.ReadPlayerID(player_id);
376 state.reset(new InteractiveState(*this, player_id));
378 pack.ReadPlayerState(state->GetPlayer().GetEntity().GetState());
380 env.state.PopAfter(this);
381 env.state.Push(state.get());
384 void MasterState::On(const Packet::Part &pack) {
388 env.ShowMessage("kicked by server");
391 env.ShowMessage("login refused by server");
395 void MasterState::On(const Packet::SpawnEntity &pack) {
397 cout << "got entity spawn before world was created" << endl;
403 void MasterState::On(const Packet::DespawnEntity &pack) {
405 cout << "got entity despawn before world was created" << endl;
411 void MasterState::On(const Packet::EntityUpdate &pack) {
413 cout << "got entity update before world was created" << endl;
419 void MasterState::On(const Packet::PlayerCorrection &pack) {
421 cout << "got player correction without a player :S" << endl;
427 void MasterState::On(const Packet::ChunkBegin &pack) {
429 cout << "got chunk data, but the world has not been created yet" << endl;
430 cout << "great, this will totally screw up everything :(" << endl;
433 state->GetChunkReceiver().Handle(pack);
436 void MasterState::On(const Packet::ChunkData &pack) {
438 cout << "got chunk data, but the world has not been created yet" << endl;
439 cout << "great, this will totally screw up everything :(" << endl;
442 state->GetChunkReceiver().Handle(pack);
445 void MasterState::On(const Packet::BlockUpdate &pack) {
447 cout << "received block update, but the world has not been created yet" << endl;