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