]> git.localhorst.tv Git - blank.git/blob - src/server/ServerState.cpp
linear model instance state
[blank.git] / src / server / ServerState.cpp
1 #include "ServerState.hpp"
2
3 #include "../app/Environment.hpp"
4 #include "../app/TextureIndex.hpp"
5 #include "../io/WorldSave.hpp"
6 #include "../net/io.hpp"
7
8 #include <iostream>
9
10
11 namespace blank {
12 namespace server {
13
14 ServerState::ServerState(
15         HeadlessEnvironment &env,
16         const Generator::Config &gc,
17         const World::Config &wc,
18         const WorldSave &ws,
19         const Config &config
20 )
21 : env(env)
22 , shapes()
23 , block_types()
24 , world(block_types, wc)
25 , generator(gc)
26 , chunk_loader(world.Chunks(), generator, ws)
27 , skeletons()
28 , spawner(world, skeletons, env.rng)
29 , server(config.net, world, wc, ws)
30 , loop_timer(16) {
31         TextureIndex tex_index;
32         env.loader.LoadShapes("default", shapes);
33         env.loader.LoadBlockTypes("default", block_types, tex_index, shapes);
34         generator.LoadTypes(block_types);
35         skeletons.LoadHeadless();
36         spawner.LimitSkeletons(1, skeletons.size());
37         server.SetPlayerModel(skeletons[0]);
38
39         loop_timer.Start();
40
41         std::cout << "listening on UDP port " << config.net.port << std::endl;
42 }
43
44 ServerState::~ServerState() {
45
46 }
47
48
49 void ServerState::Handle(const SDL_Event &event) {
50         if (event.type == SDL_QUIT) {
51                 std::cout << "saving remaining chunks" << std::endl;
52                 for (Chunk &chunk : world.Chunks()) {
53                         if (chunk.ShouldUpdateSave()) {
54                                 chunk_loader.SaveFile().Write(chunk);
55                         }
56                 }
57                 env.state.PopAll();
58         }
59 }
60
61
62 void ServerState::Update(int dt) {
63         loop_timer.Update(dt);
64         server.Handle();
65         int world_dt = 0;
66         while (loop_timer.HitOnce()) {
67                 spawner.Update(loop_timer.Interval());
68                 world.Update(loop_timer.Interval());
69                 world_dt += loop_timer.Interval();
70                 loop_timer.PopIteration();
71         }
72         chunk_loader.Update(dt);
73         if (world_dt > 0) {
74                 server.Update(world_dt);
75         }
76 }
77
78
79 void ServerState::Render(Viewport &viewport) {
80
81 }
82
83 }
84 }