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