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 if (res.models.size() < 1) {
69 throw std::runtime_error("need at least one model to run");
71 res.models[0].Instantiate(player.GetEntity().GetModel());
72 sounds.Load(master.GetEnv().loader, res.snd_index);
73 interface.SetInventorySlots(res.block_types.size() - 1);
74 chunk_renderer.LoadTextures(master.GetEnv().loader, res.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(res.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(
151 player.GetEntity().Transform(player.GetEntity().ChunkCoords())
152 * player.GetEntity().GetModel().EyesTransform());
153 if (master.GetConfig().video.world) {
154 chunk_renderer.Render(viewport);
155 world.Render(viewport);
156 sky.Render(viewport);
158 hud.Render(viewport);
161 void InteractiveState::Handle(const Packet::SpawnEntity &pack) {
163 pack.ReadEntityID(entity_id);
164 Entity &entity = world.ForceAddEntity(entity_id);
165 UpdateEntity(entity_id, pack.Seq());
166 pack.ReadEntity(entity);
168 pack.ReadSkeletonID(skel_id);
169 if (skel_id > 0 && skel_id <= res.models.size()) {
170 Model &skel = res.models.Get(skel_id);
171 skel.Instantiate(entity.GetModel());
173 cout << "spawned entity #" << entity_id << " (" << entity.Name()
174 << ") at " << entity.AbsolutePosition() << endl;
177 void InteractiveState::Handle(const Packet::DespawnEntity &pack) {
179 pack.ReadEntityID(entity_id);
180 ClearEntity(entity_id);
181 for (Entity &entity : world.Entities()) {
182 if (entity.ID() == entity_id) {
184 cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl;
190 void InteractiveState::Handle(const Packet::EntityUpdate &pack) {
191 auto world_iter = world.Entities().begin();
192 auto world_end = world.Entities().end();
195 pack.ReadEntityCount(count);
197 for (uint32_t i = 0; i < count; ++i) {
198 uint32_t entity_id = 0;
199 pack.ReadEntityID(entity_id, i);
201 while (world_iter != world_end && world_iter->ID() < entity_id) {
204 if (world_iter == world_end) {
205 // nothing can be done from here
208 if (world_iter->ID() == entity_id) {
209 if (UpdateEntity(entity_id, pack.Seq())) {
210 pack.ReadEntityState(world_iter->GetState(), i);
216 bool InteractiveState::UpdateEntity(uint32_t entity_id, uint16_t seq) {
217 auto entry = update_status.find(entity_id);
218 if (entry == update_status.end()) {
219 update_status.emplace(entity_id, UpdateStatus{ seq, loop_timer.Elapsed() });
223 int16_t pack_diff = int16_t(seq) - int16_t(entry->second.last_packet);
224 int time_diff = loop_timer.Elapsed() - entry->second.last_update;
225 entry->second.last_update = loop_timer.Elapsed();
227 if (pack_diff > 0 || time_diff > 1500) {
228 entry->second.last_packet = seq;
235 void InteractiveState::ClearEntity(uint32_t entity_id) {
236 update_status.erase(entity_id);
239 void InteractiveState::Handle(const Packet::PlayerCorrection &pack) {
241 EntityState corrected_state;
242 pack.ReadPacketSeq(pack_seq);
243 pack.ReadPlayerState(corrected_state);
244 input.MergePlayerCorrection(pack_seq, corrected_state);
247 void InteractiveState::Handle(const Packet::BlockUpdate &pack) {
249 pack.ReadChunkCoords(pos);
250 Chunk *chunk = player.GetChunks().Get(pos);
252 // this change doesn't concern us
256 pack.ReadBlockCount(count);
257 for (uint32_t i = 0; i < count; ++i) {
260 pack.ReadIndex(index, i);
261 pack.ReadBlock(block, i);
262 if (index < Chunk::size && block.type < res.block_types.size()) {
263 manip.SetBlock(*chunk, index, block);
268 void InteractiveState::SetAudio(bool b) {
269 master.GetConfig().audio.enabled = b;
271 hud.PostMessage("Audio enabled");
273 hud.PostMessage("Audio disabled");
277 void InteractiveState::SetVideo(bool b) {
278 master.GetConfig().video.world = b;
280 hud.PostMessage("World rendering enabled");
282 hud.PostMessage("World rendering disabled");
286 void InteractiveState::SetHUD(bool b) {
287 master.GetConfig().video.hud = b;
289 hud.PostMessage("HUD rendering enabled");
291 hud.PostMessage("HUD rendering disabled");
295 void InteractiveState::SetDebug(bool b) {
296 master.GetConfig().video.debug = b;
298 hud.PostMessage("Debug rendering enabled");
300 hud.PostMessage("Debug rendering disabled");
304 void InteractiveState::Exit() {
310 MasterState::MasterState(
313 const World::Config &wc)
321 client.GetConnection().SetHandler(this);
324 void MasterState::Quit() {
325 if (!client.GetConnection().Closed()) {
328 env.state.PopUntil(this);
332 void MasterState::OnEnter() {
333 login_packet = client.SendLogin(config.player.name);
334 env.state.Push(&init_state);
338 void MasterState::Handle(const SDL_Event &event) {
343 void MasterState::Update(int dt) {
349 void MasterState::Render(Viewport &) {
354 void MasterState::OnPacketLost(uint16_t id) {
355 if (id == login_packet) {
356 login_packet = client.SendLogin(config.player.name);
360 void MasterState::OnTimeout() {
361 if (client.GetConnection().Closed()) {
363 env.ShowMessage("connection timed out");
367 void MasterState::On(const Packet::Join &pack) {
368 pack.ReadWorldName(world_conf.name);
372 cout << "server changing worlds to \"" << world_conf.name << '"' << endl;
375 cout << "joined game \"" << world_conf.name << '"' << endl;
376 // server received our login
381 pack.ReadPlayerID(player_id);
382 state.reset(new InteractiveState(*this, player_id));
384 pack.ReadPlayerState(state->GetPlayer().GetEntity().GetState());
385 glm::vec3 orient(glm::eulerAngles(state->GetPlayer().GetEntity().Orientation()));
386 state->GetPlayerController().TurnHead(orient.x, orient.y);
388 env.state.PopAfter(this);
389 env.state.Push(state.get());
392 void MasterState::On(const Packet::Part &pack) {
396 env.ShowMessage("kicked by server");
399 env.ShowMessage("login refused by server");
403 void MasterState::On(const Packet::SpawnEntity &pack) {
405 cout << "got entity spawn before world was created" << endl;
411 void MasterState::On(const Packet::DespawnEntity &pack) {
413 cout << "got entity despawn before world was created" << endl;
419 void MasterState::On(const Packet::EntityUpdate &pack) {
421 cout << "got entity update before world was created" << endl;
427 void MasterState::On(const Packet::PlayerCorrection &pack) {
429 cout << "got player correction without a player :S" << endl;
435 void MasterState::On(const Packet::ChunkBegin &pack) {
437 cout << "got chunk data, but the world has not been created yet" << endl;
438 cout << "great, this will totally screw up everything :(" << endl;
441 state->GetChunkReceiver().Handle(pack);
444 void MasterState::On(const Packet::ChunkData &pack) {
446 cout << "got chunk data, but the world has not been created yet" << endl;
447 cout << "great, this will totally screw up everything :(" << endl;
450 state->GetChunkReceiver().Handle(pack);
453 void MasterState::On(const Packet::BlockUpdate &pack) {
455 cout << "received block update, but the world has not been created yet" << endl;