1 #include "ServerState.hpp"
3 #include "../app/Environment.hpp"
4 #include "../app/TextureIndex.hpp"
5 #include "../io/WorldSave.hpp"
6 #include "../net/io.hpp"
14 ServerState::ServerState(
15 HeadlessEnvironment &env,
16 const Generator::Config &gc,
17 const World::Config &wc,
25 , world(block_types, wc)
27 , chunk_loader(world.Chunks(), generator, ws)
28 , spawner(world, models, env.rng)
29 , server(config.net, world, wc, ws)
31 TextureIndex tex_index;
32 env.loader.LoadShapes("default", shapes);
33 env.loader.LoadBlockTypes("default", block_types, tex_index, shapes);
34 env.loader.LoadModels("default", models, tex_index, shapes);
35 if (models.size() < 2) {
36 throw std::runtime_error("need at least two models to run");
38 generator.LoadTypes(block_types);
39 spawner.LimitModels(1, models.size());
40 server.SetPlayerModel(models[0]);
44 std::cout << "listening on UDP port " << config.net.port << std::endl;
47 ServerState::~ServerState() {
52 void ServerState::Handle(const SDL_Event &event) {
53 if (event.type == SDL_QUIT) {
54 std::cout << "saving remaining chunks" << std::endl;
55 for (Chunk &chunk : world.Chunks()) {
56 if (chunk.ShouldUpdateSave()) {
57 chunk_loader.SaveFile().Write(chunk);
65 void ServerState::Update(int dt) {
66 loop_timer.Update(dt);
69 while (loop_timer.HitOnce()) {
70 spawner.Update(loop_timer.Interval());
71 world.Update(loop_timer.Interval());
72 world_dt += loop_timer.Interval();
73 loop_timer.PopIteration();
75 chunk_loader.Update(dt);
77 server.Update(world_dt);
82 void ServerState::Render(Viewport &viewport) {