]> git.localhorst.tv Git - blank.git/blob - src/standalone/standalone.cpp
make gcc nag more
[blank.git] / src / standalone / standalone.cpp
1 #include "DirectCLIFeedback.hpp"
2 #include "MasterState.hpp"
3 #include "PreloadState.hpp"
4 #include "UnloadState.hpp"
5
6 #include "../app/Config.hpp"
7 #include "../app/Environment.hpp"
8 #include "../app/init.hpp"
9 #include "../geometry/distance.hpp"
10 #include "../io/WorldSave.hpp"
11 #include "../world/ChunkLoader.hpp"
12 #include "../world/ChunkRenderer.hpp"
13
14 #include <SDL.h>
15
16
17 namespace blank {
18 namespace standalone {
19
20 DirectCLIFeedback::DirectCLIFeedback(Player &p, HUD &h)
21 : CLIContext(p)
22 , hud(h) {
23
24 }
25
26 void DirectCLIFeedback::Error(const std::string &msg) {
27         hud.PostMessage(msg);
28 }
29
30 void DirectCLIFeedback::Message(const std::string &msg) {
31         hud.PostMessage(msg);
32 }
33
34 void DirectCLIFeedback::Broadcast(const std::string &msg) {
35         hud.PostMessage(msg);
36 }
37
38
39 MasterState::MasterState(
40         Environment &env,
41         Config &config,
42         const Generator::Config &gc,
43         const World::Config &wc,
44         const WorldSave &save
45 )
46 : config(config)
47 , env(env)
48 , res()
49 , sounds()
50 , save(save)
51 , world(res.block_types, wc)
52 , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
53 , player(*world.AddPlayer(config.player.name))
54 , spawn_player(false)
55 , hud(env, config, player)
56 , manip(env.audio, sounds, player.GetEntity())
57 , input(world, player, manip)
58 , interface(config, env.keymap, input, *this)
59 , generator(gc)
60 , chunk_loader(world.Chunks(), generator, save)
61 , chunk_renderer(player.GetChunks())
62 , spawner(world, res.models)
63 , sky(env.loader.LoadCubeMap("skybox"))
64 , cli(world)
65 , cli_ctx(player, hud)
66 , preload(env, chunk_loader, chunk_renderer)
67 , unload(env, world.Chunks(), save)
68 , chat(env, *this, *this) {
69         res.Load(env.loader, "default");
70         if (res.models.size() < 2) {
71                 throw std::runtime_error("need at least two models to run");
72         }
73         res.models[0].Instantiate(player.GetEntity().GetModel());
74         sounds.Load(env.loader, res.snd_index);
75         spawner.LimitModels(1, res.models.size());
76         interface.SetInventorySlots(res.block_types.size() - 1);
77         generator.LoadTypes(res.block_types);
78         chunk_renderer.LoadTextures(env.loader, res.tex_index);
79         chunk_renderer.FogDensity(wc.fog_density);
80         if (save.Exists(player)) {
81                 save.Read(player);
82         } else {
83                 spawn_player = true;
84         }
85 }
86
87 MasterState::~MasterState() {
88         world.Chunks().UnregisterIndex(spawn_index);
89 }
90
91
92 void MasterState::OnResume() {
93         if (spawn_index.MissingChunks() > 0) {
94                 env.state.Push(&preload);
95                 return;
96         }
97         if (spawn_player) {
98                 // TODO: spawn
99                 spawn_player = false;
100         }
101         hud.KeepMessages(false);
102         OnFocus();
103 }
104
105 void MasterState::OnPause() {
106         OnBlur();
107 }
108
109 void MasterState::OnFocus() {
110         if (config.input.mouse) {
111                 env.window.GrabMouse();
112         }
113         interface.Unlock();
114 }
115
116 void MasterState::OnBlur() {
117         env.window.ReleaseMouse();
118         interface.Lock();
119 }
120
121
122 void MasterState::Handle(const SDL_Event &event) {
123         switch (event.type) {
124                 case SDL_KEYDOWN:
125                         // TODO: move to interface
126                         if (event.key.keysym.sym == SDLK_RETURN) {
127                                 chat.Clear();
128                                 env.state.Push(&chat);
129                                 hud.KeepMessages(true);
130                         } else if (event.key.keysym.sym == SDLK_SLASH) {
131                                 chat.Preset("/");
132                                 env.state.Push(&chat);
133                                 hud.KeepMessages(true);
134                         } else {
135                                 interface.HandlePress(event.key);
136                         }
137                         break;
138                 case SDL_KEYUP:
139                         interface.HandleRelease(event.key);
140                         break;
141                 case SDL_MOUSEBUTTONDOWN:
142                         interface.HandlePress(event.button);
143                         break;
144                 case SDL_MOUSEBUTTONUP:
145                         interface.HandleRelease(event.button);
146                         break;
147                 case SDL_MOUSEMOTION:
148                         interface.Handle(event.motion);
149                         break;
150                 case SDL_MOUSEWHEEL:
151                         interface.Handle(event.wheel);
152                         break;
153                 case SDL_QUIT:
154                         Exit();
155                         break;
156                 default:
157                         break;
158         }
159 }
160
161 void MasterState::Update(int dt) {
162         spawner.Update(dt);
163         world.Update(dt);
164         if (input.BlockFocus()) {
165                 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
166         } else if (input.EntityFocus()) {
167                 hud.FocusEntity(input.EntityFocus().GetEntity());
168         } else {
169                 hud.FocusNone();
170         }
171         hud.Display(res.block_types[player.GetInventorySlot() + 1]);
172         hud.Update(dt);
173         chunk_loader.Update(dt);
174         chunk_renderer.Update(dt);
175
176         glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
177         glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
178         glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
179         env.audio.Position(player.GetEntity().Position());
180         env.audio.Velocity(player.GetEntity().Velocity());
181         env.audio.Orientation(dir, up);
182 }
183
184 void MasterState::Render(Viewport &viewport) {
185         viewport.WorldPosition(player.GetEntity().ViewTransform(player.GetEntity().ChunkCoords()));
186         if (config.video.world) {
187                 chunk_renderer.Render(viewport);
188                 world.Render(viewport);
189                 if (config.video.debug) {
190                         world.RenderDebug(viewport);
191                 }
192                 sky.Render(viewport);
193         }
194         hud.Render(viewport);
195 }
196
197
198 void MasterState::SetAudio(bool b) {
199         config.audio.enabled = b;
200         if (b) {
201                 hud.PostMessage("Audio enabled");
202         } else {
203                 hud.PostMessage("Audio disabled");
204         }
205 }
206
207 void MasterState::SetVideo(bool b) {
208         config.video.world = b;
209         if (b) {
210                 hud.PostMessage("World rendering enabled");
211         } else {
212                 hud.PostMessage("World rendering disabled");
213         }
214 }
215
216 void MasterState::SetHUD(bool b) {
217         config.video.hud = b;
218         if (b) {
219                 hud.PostMessage("HUD rendering enabled");
220         } else {
221                 hud.PostMessage("HUD rendering disabled");
222         }
223 }
224
225 void MasterState::SetDebug(bool b) {
226         config.video.debug = b;
227         if (b) {
228                 hud.PostMessage("Debug rendering enabled");
229         } else {
230                 hud.PostMessage("Debug rendering disabled");
231         }
232 }
233
234 void MasterState::NextCamera() {
235         if (iszero(env.viewport.CameraOffset())) {
236                 env.viewport.OffsetCamera(glm::vec3(0.0f, 0.0f, -5.0f));
237         } else {
238                 env.viewport.OffsetCamera(glm::vec3(0.0f, 0.0f, 0.0f));
239         }
240 }
241
242 void MasterState::Exit() {
243         save.Write(player);
244         env.state.Switch(&unload);
245 }
246
247 void MasterState::OnLineSubmit(const std::string &line) {
248         if (line.empty()) {
249                 return;
250         }
251         if (line[0] == '/' && line.size() > 1 && line[1] != '/') {
252                 cli.Execute(cli_ctx, line.substr(1));
253         } else {
254                 hud.PostMessage(line);
255         }
256 }
257
258
259 PreloadState::PreloadState(Environment &env, ChunkLoader &loader, ChunkRenderer &render)
260 : ProgressState(env, "Preloading chunks: %d/%d (%d%%)")
261 , env(env)
262 , loader(loader)
263 , render(render)
264 , total(loader.ToLoad())
265 , per_update(64) {
266
267 }
268
269 void PreloadState::Update(int) {
270         loader.LoadN(per_update);
271         if (loader.ToLoad() <= 0) {
272                 env.state.Pop();
273                 render.Update(render.MissingChunks());
274         } else {
275                 SetProgress(total - loader.ToLoad(), total);
276         }
277 }
278
279
280 UnloadState::UnloadState(
281         Environment &env,
282         ChunkStore &chunks,
283         const WorldSave &save)
284 : ProgressState(env, "Unloading chunks: %d/%d (%d%%)")
285 , env(env)
286 , chunks(chunks)
287 , save(save)
288 , cur(chunks.begin())
289 , end(chunks.end())
290 , done(0)
291 , total(chunks.NumLoaded())
292 , per_update(64) {
293
294 }
295
296
297 void UnloadState::OnResume() {
298         cur = chunks.begin();
299         end = chunks.end();
300         done = 0;
301         total = chunks.NumLoaded();
302 }
303
304
305 void UnloadState::Handle(const SDL_Event &) {
306         // ignore everything
307 }
308
309 void UnloadState::Update(int) {
310         for (std::size_t i = 0; i < per_update && cur != end; ++i, ++cur, ++done) {
311                 if (cur->ShouldUpdateSave()) {
312                         save.Write(*cur);
313                 }
314         }
315         if (cur == end) {
316                 env.state.Pop();
317         } else {
318                 SetProgress(done, total);
319         }
320 }
321
322 }
323 }