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)
51 , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host))
52 , world(res.block_types, master.GetWorldConf())
53 , player(*world.AddPlayer(master.GetConfig().player.name))
54 , hud(master.GetEnv(), master.GetConfig(), player)
55 , manip(master.GetEnv(), player.GetEntity())
56 , input(world, player, master.GetClient())
57 , interface(master.GetConfig(), master.GetEnv().keymap, input, *this)
58 , chunk_receiver(world.Chunks(), save)
59 , chunk_renderer(player.GetChunks())
61 , sky(master.GetEnv().loader.LoadCubeMap("skybox"))
64 save.Write(master.GetWorldConf());
66 res.Load(master.GetEnv().loader, "default");
67 interface.SetInventorySlots(res.block_types.size() - 1);
68 chunk_renderer.LoadTextures(master.GetEnv().loader, res.tex_index);
69 chunk_renderer.FogDensity(master.GetWorldConf().fog_density);
71 if (save.Exists(player)) {
76 void InteractiveState::OnEnter() {
77 master.GetEnv().window.GrabMouse();
80 void InteractiveState::Handle(const SDL_Event &event) {
83 interface.HandlePress(event.key);
86 interface.HandleRelease(event.key);
88 case SDL_MOUSEBUTTONDOWN:
89 interface.HandlePress(event.button);
91 case SDL_MOUSEBUTTONUP:
92 interface.HandleRelease(event.button);
95 interface.Handle(event.motion);
98 interface.Handle(event.wheel);
108 void InteractiveState::Update(int dt) {
110 if (input.BlockFocus()) {
111 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
112 } else if (input.EntityFocus()) {
113 hud.FocusEntity(*input.EntityFocus().entity);
117 hud.Display(res.block_types[player.GetInventorySlot() + 1]);
118 loop_timer.Update(dt);
120 chunk_receiver.Update(dt);
124 while (loop_timer.HitOnce()) {
125 world.Update(loop_timer.Interval());
126 world_dt += loop_timer.Interval();
127 loop_timer.PopIteration();
129 chunk_renderer.Update(dt);
132 input.PushPlayerUpdate(world_dt);
135 glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
136 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
137 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
138 master.GetEnv().audio.Position(player.GetEntity().Position());
139 master.GetEnv().audio.Velocity(player.GetEntity().Velocity());
140 master.GetEnv().audio.Orientation(dir, up);
143 void InteractiveState::Render(Viewport &viewport) {
144 viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords()));
145 if (master.GetConfig().video.world) {
146 chunk_renderer.Render(viewport);
147 world.Render(viewport);
148 sky.Render(viewport);
150 hud.Render(viewport);
153 void InteractiveState::Handle(const Packet::SpawnEntity &pack) {
155 pack.ReadEntityID(entity_id);
156 Entity &entity = world.ForceAddEntity(entity_id);
157 UpdateEntity(entity_id, pack.Seq());
158 pack.ReadEntity(entity);
160 pack.ReadSkeletonID(skel_id);
161 if (skel_id > 0 && skel_id <= res.models.size()) {
162 Model &skel = res.models.Get(skel_id);
163 skel.Instantiate(entity.GetModel());
165 cout << "spawned entity #" << entity_id << " (" << entity.Name()
166 << ") at " << entity.AbsolutePosition() << endl;
169 void InteractiveState::Handle(const Packet::DespawnEntity &pack) {
171 pack.ReadEntityID(entity_id);
172 ClearEntity(entity_id);
173 for (Entity &entity : world.Entities()) {
174 if (entity.ID() == entity_id) {
176 cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl;
182 void InteractiveState::Handle(const Packet::EntityUpdate &pack) {
183 auto world_iter = world.Entities().begin();
184 auto world_end = world.Entities().end();
187 pack.ReadEntityCount(count);
189 for (uint32_t i = 0; i < count; ++i) {
190 uint32_t entity_id = 0;
191 pack.ReadEntityID(entity_id, i);
193 while (world_iter != world_end && world_iter->ID() < entity_id) {
196 if (world_iter == world_end) {
197 // nothing can be done from here
200 if (world_iter->ID() == entity_id) {
201 if (UpdateEntity(entity_id, pack.Seq())) {
202 pack.ReadEntityState(world_iter->GetState(), i);
208 bool InteractiveState::UpdateEntity(uint32_t entity_id, uint16_t seq) {
209 auto entry = update_status.find(entity_id);
210 if (entry == update_status.end()) {
211 update_status.emplace(entity_id, UpdateStatus{ seq, loop_timer.Elapsed() });
215 int16_t pack_diff = int16_t(seq) - int16_t(entry->second.last_packet);
216 int time_diff = loop_timer.Elapsed() - entry->second.last_update;
217 entry->second.last_update = loop_timer.Elapsed();
219 if (pack_diff > 0 || time_diff > 1500) {
220 entry->second.last_packet = seq;
227 void InteractiveState::ClearEntity(uint32_t entity_id) {
228 update_status.erase(entity_id);
231 void InteractiveState::Handle(const Packet::PlayerCorrection &pack) {
233 EntityState corrected_state;
234 pack.ReadPacketSeq(pack_seq);
235 pack.ReadPlayerState(corrected_state);
236 input.MergePlayerCorrection(pack_seq, corrected_state);
239 void InteractiveState::Handle(const Packet::BlockUpdate &pack) {
241 pack.ReadChunkCoords(pos);
242 Chunk *chunk = player.GetChunks().Get(pos);
244 // this change doesn't concern us
248 pack.ReadBlockCount(count);
249 for (uint32_t i = 0; i < count; ++i) {
252 pack.ReadIndex(index, i);
253 pack.ReadBlock(block, i);
254 if (index < Chunk::size && block.type < res.block_types.size()) {
255 manip.SetBlock(*chunk, index, block);
260 void InteractiveState::SetAudio(bool b) {
261 master.GetConfig().audio.enabled = b;
263 hud.PostMessage("Audio enabled");
265 hud.PostMessage("Audio disabled");
269 void InteractiveState::SetVideo(bool b) {
270 master.GetConfig().video.world = b;
272 hud.PostMessage("World rendering enabled");
274 hud.PostMessage("World rendering disabled");
278 void InteractiveState::SetHUD(bool b) {
279 master.GetConfig().video.hud = b;
281 hud.PostMessage("HUD rendering enabled");
283 hud.PostMessage("HUD rendering disabled");
287 void InteractiveState::SetDebug(bool b) {
288 master.GetConfig().video.debug = b;
290 hud.PostMessage("Debug rendering enabled");
292 hud.PostMessage("Debug rendering disabled");
296 void InteractiveState::Exit() {
302 MasterState::MasterState(
305 const World::Config &wc)
313 client.GetConnection().SetHandler(this);
316 void MasterState::Quit() {
317 if (!client.GetConnection().Closed()) {
320 env.state.PopUntil(this);
324 void MasterState::OnEnter() {
325 login_packet = client.SendLogin(config.player.name);
326 env.state.Push(&init_state);
330 void MasterState::Handle(const SDL_Event &event) {
335 void MasterState::Update(int dt) {
341 void MasterState::Render(Viewport &) {
346 void MasterState::OnPacketLost(uint16_t id) {
347 if (id == login_packet) {
348 login_packet = client.SendLogin(config.player.name);
352 void MasterState::OnTimeout() {
353 if (client.GetConnection().Closed()) {
355 env.ShowMessage("connection timed out");
359 void MasterState::On(const Packet::Join &pack) {
360 pack.ReadWorldName(world_conf.name);
364 cout << "server changing worlds to \"" << world_conf.name << '"' << endl;
367 cout << "joined game \"" << world_conf.name << '"' << endl;
368 // server received our login
373 pack.ReadPlayerID(player_id);
374 state.reset(new InteractiveState(*this, player_id));
376 pack.ReadPlayerState(state->GetPlayer().GetEntity().GetState());
378 env.state.PopAfter(this);
379 env.state.Push(state.get());
382 void MasterState::On(const Packet::Part &pack) {
386 env.ShowMessage("kicked by server");
389 env.ShowMessage("login refused by server");
393 void MasterState::On(const Packet::SpawnEntity &pack) {
395 cout << "got entity spawn before world was created" << endl;
401 void MasterState::On(const Packet::DespawnEntity &pack) {
403 cout << "got entity despawn before world was created" << endl;
409 void MasterState::On(const Packet::EntityUpdate &pack) {
411 cout << "got entity update before world was created" << endl;
417 void MasterState::On(const Packet::PlayerCorrection &pack) {
419 cout << "got player correction without a player :S" << endl;
425 void MasterState::On(const Packet::ChunkBegin &pack) {
427 cout << "got chunk data, but the world has not been created yet" << endl;
428 cout << "great, this will totally screw up everything :(" << endl;
431 state->GetChunkReceiver().Handle(pack);
434 void MasterState::On(const Packet::ChunkData &pack) {
436 cout << "got chunk data, but the world has not been created yet" << endl;
437 cout << "great, this will totally screw up everything :(" << endl;
440 state->GetChunkReceiver().Handle(pack);
443 void MasterState::On(const Packet::BlockUpdate &pack) {
445 cout << "received block update, but the world has not been created yet" << endl;