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(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 <= res.models.size()) {
168 Model &skel = res.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 < res.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());
383 glm::vec3 orient(glm::eulerAngles(state->GetPlayer().GetEntity().Orientation()));
384 state->GetPlayerController().TurnHead(orient.x, orient.y);
386 env.state.PopAfter(this);
387 env.state.Push(state.get());
390 void MasterState::On(const Packet::Part &pack) {
394 env.ShowMessage("kicked by server");
397 env.ShowMessage("login refused by server");
401 void MasterState::On(const Packet::SpawnEntity &pack) {
403 cout << "got entity spawn before world was created" << endl;
409 void MasterState::On(const Packet::DespawnEntity &pack) {
411 cout << "got entity despawn before world was created" << endl;
417 void MasterState::On(const Packet::EntityUpdate &pack) {
419 cout << "got entity update before world was created" << endl;
425 void MasterState::On(const Packet::PlayerCorrection &pack) {
427 cout << "got player correction without a player :S" << endl;
433 void MasterState::On(const Packet::ChunkBegin &pack) {
435 cout << "got chunk data, but the world has not been created yet" << endl;
436 cout << "great, this will totally screw up everything :(" << endl;
439 state->GetChunkReceiver().Handle(pack);
442 void MasterState::On(const Packet::ChunkData &pack) {
444 cout << "got chunk data, but the world has not been created yet" << endl;
445 cout << "great, this will totally screw up everything :(" << endl;
448 state->GetChunkReceiver().Handle(pack);
451 void MasterState::On(const Packet::BlockUpdate &pack) {
453 cout << "received block update, but the world has not been created yet" << endl;