]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/game.h
Forcefully place items when minetest.place_node is used
[dragonfireclient.git] / src / client / game.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include <iomanip>
23 #include <cmath>
24 #include "client/renderingengine.h"
25 #include "camera.h"
26 #include "client.h"
27 #include "client/clientevent.h"
28 //#include "client/gameui.h"
29 #include "client/inputhandler.h"
30 #include "client/sound.h"
31 #include "client/tile.h"     // For TextureSource
32 #include "client/keys.h"
33 #include "client/joystick_controller.h"
34 #include "clientmap.h"
35 #include "clouds.h"
36 #include "config.h"
37 #include "content_cao.h"
38 #include "client/event_manager.h"
39 #include "fontengine.h"
40 #include "itemdef.h"
41 #include "log.h"
42 #include "filesys.h"
43 #include "gettext.h"
44 #include "gui/cheatMenu.h"
45 #include "gui/guiChatConsole.h"
46 #include "gui/guiConfirmRegistration.h"
47 #include "gui/guiFormSpecMenu.h"
48 #include "gui/guiKeyChangeMenu.h"
49 #include "gui/guiPasswordChange.h"
50 #include "gui/guiVolumeChange.h"
51 #include "gui/mainmenumanager.h"
52 #include "gui/profilergraph.h"
53 #include "mapblock.h"
54 #include "minimap.h"
55 #include "nodedef.h"         // Needed for determining pointing to nodes
56 #include "nodemetadata.h"
57 #include "particles.h"
58 #include "porting.h"
59 #include "profiler.h"
60 #include "raycast.h"
61 #include "server.h"
62 #include "settings.h"
63 #include "shader.h"
64 #include "sky.h"
65 #include "translation.h"
66 #include "util/basic_macros.h"
67 #include "util/directiontables.h"
68 #include "util/pointedthing.h"
69 #include "util/quicktune_shortcutter.h"
70 #include "irrlicht_changes/static_text.h"
71 #include "version.h"
72 #include "script/scripting_client.h"
73 #include "hud.h"
74 #include "irrlichttypes.h"
75 #include <string>
76
77 class InputHandler;
78 class ChatBackend;  /* to avoid having to include chat.h */
79 struct SubgameSpec;
80 struct GameStartData;
81
82 struct Jitter {
83         f32 max, min, avg, counter, max_sample, min_sample, max_fraction;
84 };
85
86 struct RunStats {
87         u32 drawtime;
88
89         Jitter dtime_jitter, busy_time_jitter;
90 };
91
92 struct CameraOrientation {
93         f32 camera_yaw;    // "right/left"
94         f32 camera_pitch;  // "up/down"
95 };
96
97 /*
98         Text input system
99 */
100
101 struct TextDestNodeMetadata : public TextDest
102 {
103         TextDestNodeMetadata(v3s16 p, Client *client)
104         {
105                 m_p = p;
106                 m_client = client;
107         }
108         // This is deprecated I guess? -celeron55
109         void gotText(const std::wstring &text)
110         {
111                 std::string ntext = wide_to_utf8(text);
112                 infostream << "Submitting 'text' field of node at (" << m_p.X << ","
113                            << m_p.Y << "," << m_p.Z << "): " << ntext << std::endl;
114                 StringMap fields;
115                 fields["text"] = ntext;
116                 m_client->sendNodemetaFields(m_p, "", fields);
117         }
118         void gotText(const StringMap &fields)
119         {
120                 m_client->sendNodemetaFields(m_p, "", fields);
121         }
122
123         v3s16 m_p;
124         Client *m_client;
125 };
126
127 struct TextDestPlayerInventory : public TextDest
128 {
129         TextDestPlayerInventory(Client *client)
130         {
131                 m_client = client;
132                 m_formname = "";
133         }
134         TextDestPlayerInventory(Client *client, const std::string &formname)
135         {
136                 m_client = client;
137                 m_formname = formname;
138         }
139         void gotText(const StringMap &fields)
140         {
141                 m_client->sendInventoryFields(m_formname, fields);
142         }
143
144         Client *m_client;
145 };
146
147 struct LocalFormspecHandler : public TextDest
148 {
149         LocalFormspecHandler(const std::string &formname)
150         {
151                 m_formname = formname;
152         }
153
154         LocalFormspecHandler(const std::string &formname, Client *client):
155                 m_client(client)
156         {
157                 m_formname = formname;
158         }
159
160         void gotText(const StringMap &fields)
161         {
162                 if (m_formname == "MT_PAUSE_MENU") {
163                         if (fields.find("btn_sound") != fields.end()) {
164                                 g_gamecallback->changeVolume();
165                                 return;
166                         }
167
168                         if (fields.find("btn_key_config") != fields.end()) {
169                                 g_gamecallback->keyConfig();
170                                 return;
171                         }
172
173                         if (fields.find("btn_exit_menu") != fields.end()) {
174                                 g_gamecallback->disconnect();
175                                 return;
176                         }
177
178                         if (fields.find("btn_exit_os") != fields.end()) {
179                                 g_gamecallback->exitToOS();
180 #ifndef __ANDROID__
181                                 RenderingEngine::get_raw_device()->closeDevice();
182 #endif
183                                 return;
184                         }
185
186                         if (fields.find("btn_change_password") != fields.end()) {
187                                 g_gamecallback->changePassword();
188                                 return;
189                         }
190
191                         return;
192                 }
193
194                 if (m_formname == "MT_DEATH_SCREEN") {
195                         assert(m_client != 0);
196                         m_client->sendRespawn();
197                         return;
198                 }
199
200                 if (m_client->modsLoaded())
201                         m_client->getScript()->on_formspec_input(m_formname, fields);
202         }
203
204         Client *m_client = nullptr;
205 };
206
207 /* Form update callback */
208
209 class NodeMetadataFormSource: public IFormSource
210 {
211 public:
212         NodeMetadataFormSource(ClientMap *map, v3s16 p):
213                 m_map(map),
214                 m_p(p)
215         {
216         }
217         const std::string &getForm() const
218         {
219                 static const std::string empty_string = "";
220                 NodeMetadata *meta = m_map->getNodeMetadata(m_p);
221
222                 if (!meta)
223                         return empty_string;
224
225                 return meta->getString("formspec");
226         }
227
228         virtual std::string resolveText(const std::string &str)
229         {
230                 NodeMetadata *meta = m_map->getNodeMetadata(m_p);
231
232                 if (!meta)
233                         return str;
234
235                 return meta->resolveString(str);
236         }
237
238         ClientMap *m_map;
239         v3s16 m_p;
240 };
241
242 class PlayerInventoryFormSource: public IFormSource
243 {
244 public:
245         PlayerInventoryFormSource(Client *client):
246                 m_client(client)
247         {
248         }
249
250         const std::string &getForm() const
251         {
252                 LocalPlayer *player = m_client->getEnv().getLocalPlayer();
253                 return player->inventory_formspec;
254         }
255
256         Client *m_client;
257 };
258
259 class NodeDugEvent: public MtEvent
260 {
261 public:
262         v3s16 p;
263         MapNode n;
264
265         NodeDugEvent(v3s16 p, MapNode n):
266                 p(p),
267                 n(n)
268         {}
269         MtEvent::Type getType() const
270         {
271                 return MtEvent::NODE_DUG;
272         }
273 };
274
275 class SoundMaker
276 {
277         ISoundManager *m_sound;
278         const NodeDefManager *m_ndef;
279 public:
280         bool makes_footstep_sound;
281         float m_player_step_timer;
282         float m_player_jump_timer;
283
284         SimpleSoundSpec m_player_step_sound;
285         SimpleSoundSpec m_player_leftpunch_sound;
286         SimpleSoundSpec m_player_rightpunch_sound;
287
288         SoundMaker(ISoundManager *sound, const NodeDefManager *ndef):
289                 m_sound(sound),
290                 m_ndef(ndef),
291                 makes_footstep_sound(true),
292                 m_player_step_timer(0.0f),
293                 m_player_jump_timer(0.0f)
294         {
295         }
296
297         void playPlayerStep()
298         {
299                 if (m_player_step_timer <= 0 && m_player_step_sound.exists()) {
300                         m_player_step_timer = 0.03;
301                         if (makes_footstep_sound)
302                                 m_sound->playSound(m_player_step_sound, false);
303                 }
304         }
305
306         void playPlayerJump()
307         {
308                 if (m_player_jump_timer <= 0.0f) {
309                         m_player_jump_timer = 0.2f;
310                         m_sound->playSound(SimpleSoundSpec("player_jump", 0.5f), false);
311                 }
312         }
313
314         static void viewBobbingStep(MtEvent *e, void *data)
315         {
316                 SoundMaker *sm = (SoundMaker *)data;
317                 sm->playPlayerStep();
318         }
319
320         static void playerRegainGround(MtEvent *e, void *data)
321         {
322                 SoundMaker *sm = (SoundMaker *)data;
323                 sm->playPlayerStep();
324         }
325
326         static void playerJump(MtEvent *e, void *data)
327         {
328                 SoundMaker *sm = (SoundMaker *)data;
329                 sm->playPlayerJump();
330         }
331
332         static void cameraPunchLeft(MtEvent *e, void *data)
333         {
334                 SoundMaker *sm = (SoundMaker *)data;
335                 sm->m_sound->playSound(sm->m_player_leftpunch_sound, false);
336         }
337
338         static void cameraPunchRight(MtEvent *e, void *data)
339         {
340                 SoundMaker *sm = (SoundMaker *)data;
341                 sm->m_sound->playSound(sm->m_player_rightpunch_sound, false);
342         }
343
344         static void nodeDug(MtEvent *e, void *data)
345         {
346                 SoundMaker *sm = (SoundMaker *)data;
347                 NodeDugEvent *nde = (NodeDugEvent *)e;
348                 sm->m_sound->playSound(sm->m_ndef->get(nde->n).sound_dug, false);
349         }
350
351         static void playerDamage(MtEvent *e, void *data)
352         {
353                 SoundMaker *sm = (SoundMaker *)data;
354                 sm->m_sound->playSound(SimpleSoundSpec("player_damage", 0.5), false);
355         }
356
357         static void playerFallingDamage(MtEvent *e, void *data)
358         {
359                 SoundMaker *sm = (SoundMaker *)data;
360                 sm->m_sound->playSound(SimpleSoundSpec("player_falling_damage", 0.5), false);
361         }
362
363         void registerReceiver(MtEventManager *mgr)
364         {
365                 mgr->reg(MtEvent::VIEW_BOBBING_STEP, SoundMaker::viewBobbingStep, this);
366                 mgr->reg(MtEvent::PLAYER_REGAIN_GROUND, SoundMaker::playerRegainGround, this);
367                 mgr->reg(MtEvent::PLAYER_JUMP, SoundMaker::playerJump, this);
368                 mgr->reg(MtEvent::CAMERA_PUNCH_LEFT, SoundMaker::cameraPunchLeft, this);
369                 mgr->reg(MtEvent::CAMERA_PUNCH_RIGHT, SoundMaker::cameraPunchRight, this);
370                 mgr->reg(MtEvent::NODE_DUG, SoundMaker::nodeDug, this);
371                 mgr->reg(MtEvent::PLAYER_DAMAGE, SoundMaker::playerDamage, this);
372                 mgr->reg(MtEvent::PLAYER_FALLING_DAMAGE, SoundMaker::playerFallingDamage, this);
373         }
374
375         void step(float dtime)
376         {
377                 m_player_step_timer -= dtime;
378                 m_player_jump_timer -= dtime;
379         }
380 };
381
382 // Locally stored sounds don't need to be preloaded because of this
383 class GameOnDemandSoundFetcher: public OnDemandSoundFetcher
384 {
385         std::set<std::string> m_fetched;
386 private:
387         void paths_insert(std::set<std::string> &dst_paths,
388                 const std::string &base,
389                 const std::string &name)
390         {
391                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".ogg");
392                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".0.ogg");
393                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".1.ogg");
394                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".2.ogg");
395                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".3.ogg");
396                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".4.ogg");
397                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".5.ogg");
398                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".6.ogg");
399                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".7.ogg");
400                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".8.ogg");
401                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".9.ogg");
402         }
403 public:
404         void fetchSounds(const std::string &name,
405                 std::set<std::string> &dst_paths,
406                 std::set<std::string> &dst_datas)
407         {
408                 if (m_fetched.count(name))
409                         return;
410
411                 m_fetched.insert(name);
412
413                 paths_insert(dst_paths, porting::path_share, name);
414                 paths_insert(dst_paths, porting::path_user,  name);
415         }
416 };
417
418
419 // before 1.8 there isn't a "integer interface", only float
420 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
421 typedef f32 SamplerLayer_t;
422 #else
423 typedef s32 SamplerLayer_t;
424 #endif
425
426
427 class GameGlobalShaderConstantSetter : public IShaderConstantSetter
428 {
429         Sky *m_sky;
430         bool *m_force_fog_off;
431         f32 *m_fog_range;
432         bool m_fog_enabled;
433         CachedPixelShaderSetting<float, 4> m_sky_bg_color;
434         CachedPixelShaderSetting<float> m_fog_distance;
435         CachedVertexShaderSetting<float> m_animation_timer_vertex;
436         CachedPixelShaderSetting<float> m_animation_timer_pixel;
437         CachedPixelShaderSetting<float, 3> m_day_light;
438         CachedPixelShaderSetting<float, 4> m_star_color;
439         CachedPixelShaderSetting<float, 3> m_eye_position_pixel;
440         CachedVertexShaderSetting<float, 3> m_eye_position_vertex;
441         CachedPixelShaderSetting<float, 3> m_minimap_yaw;
442         CachedPixelShaderSetting<float, 3> m_camera_offset_pixel;
443         CachedPixelShaderSetting<float, 3> m_camera_offset_vertex;
444         CachedPixelShaderSetting<SamplerLayer_t> m_base_texture;
445         Client *m_client;
446
447 public:
448         void onSettingsChange(const std::string &name)
449         {
450                 if (name == "enable_fog")
451                         m_fog_enabled = g_settings->getBool("enable_fog");
452         }
453
454         static void settingsCallback(const std::string &name, void *userdata)
455         {
456                 reinterpret_cast<GameGlobalShaderConstantSetter*>(userdata)->onSettingsChange(name);
457         }
458
459         void setSky(Sky *sky) { m_sky = sky; }
460
461         GameGlobalShaderConstantSetter(Sky *sky, bool *force_fog_off,
462                         f32 *fog_range, Client *client) :
463                 m_sky(sky),
464                 m_force_fog_off(force_fog_off),
465                 m_fog_range(fog_range),
466                 m_sky_bg_color("skyBgColor"),
467                 m_fog_distance("fogDistance"),
468                 m_animation_timer_vertex("animationTimer"),
469                 m_animation_timer_pixel("animationTimer"),
470                 m_day_light("dayLight"),
471                 m_star_color("starColor"),
472                 m_eye_position_pixel("eyePosition"),
473                 m_eye_position_vertex("eyePosition"),
474                 m_minimap_yaw("yawVec"),
475                 m_camera_offset_pixel("cameraOffset"),
476                 m_camera_offset_vertex("cameraOffset"),
477                 m_base_texture("baseTexture"),
478                 m_client(client)
479         {
480                 g_settings->registerChangedCallback("enable_fog", settingsCallback, this);
481                 m_fog_enabled = g_settings->getBool("enable_fog");
482         }
483
484         ~GameGlobalShaderConstantSetter()
485         {
486                 g_settings->deregisterChangedCallback("enable_fog", settingsCallback, this);
487         }
488
489         void onSetConstants(video::IMaterialRendererServices *services) override
490         {
491                 // Background color
492                 video::SColor bgcolor = m_sky->getBgColor();
493                 video::SColorf bgcolorf(bgcolor);
494                 float bgcolorfa[4] = {
495                         bgcolorf.r,
496                         bgcolorf.g,
497                         bgcolorf.b,
498                         bgcolorf.a,
499                 };
500                 m_sky_bg_color.set(bgcolorfa, services);
501
502                 // Fog distance
503                 float fog_distance = 10000 * BS;
504
505                 if (m_fog_enabled && !*m_force_fog_off)
506                         fog_distance = *m_fog_range;
507
508                 m_fog_distance.set(&fog_distance, services);
509
510                 u32 daynight_ratio = (float)m_client->getEnv().getDayNightRatio();
511                 video::SColorf sunlight;
512                 get_sunlight_color(&sunlight, daynight_ratio);
513                 float dnc[3] = {
514                         sunlight.r,
515                         sunlight.g,
516                         sunlight.b };
517                 m_day_light.set(dnc, services);
518
519                 video::SColorf star_color = m_sky->getCurrentStarColor();
520                 float clr[4] = {star_color.r, star_color.g, star_color.b, star_color.a};
521                 m_star_color.set(clr, services);
522
523                 u32 animation_timer = porting::getTimeMs() % 1000000;
524                 float animation_timer_f = (float)animation_timer / 100000.f;
525                 m_animation_timer_vertex.set(&animation_timer_f, services);
526                 m_animation_timer_pixel.set(&animation_timer_f, services);
527
528                 float eye_position_array[3];
529                 v3f epos = m_client->getEnv().getLocalPlayer()->getEyePosition();
530 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
531                 eye_position_array[0] = epos.X;
532                 eye_position_array[1] = epos.Y;
533                 eye_position_array[2] = epos.Z;
534 #else
535                 epos.getAs3Values(eye_position_array);
536 #endif
537                 m_eye_position_pixel.set(eye_position_array, services);
538                 m_eye_position_vertex.set(eye_position_array, services);
539
540                 if (m_client->getMinimap()) {
541                         float minimap_yaw_array[3];
542                         v3f minimap_yaw = m_client->getMinimap()->getYawVec();
543 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
544                         minimap_yaw_array[0] = minimap_yaw.X;
545                         minimap_yaw_array[1] = minimap_yaw.Y;
546                         minimap_yaw_array[2] = minimap_yaw.Z;
547 #else
548                         minimap_yaw.getAs3Values(minimap_yaw_array);
549 #endif
550                         m_minimap_yaw.set(minimap_yaw_array, services);
551                 }
552
553                 float camera_offset_array[3];
554                 v3f offset = intToFloat(m_client->getCamera()->getOffset(), BS);
555 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
556                 camera_offset_array[0] = offset.X;
557                 camera_offset_array[1] = offset.Y;
558                 camera_offset_array[2] = offset.Z;
559 #else
560                 offset.getAs3Values(camera_offset_array);
561 #endif
562                 m_camera_offset_pixel.set(camera_offset_array, services);
563                 m_camera_offset_vertex.set(camera_offset_array, services);
564
565                 SamplerLayer_t base_tex = 0;
566                 m_base_texture.set(&base_tex, services);
567         }
568 };
569
570
571 class GameGlobalShaderConstantSetterFactory : public IShaderConstantSetterFactory
572 {
573         Sky *m_sky;
574         bool *m_force_fog_off;
575         f32 *m_fog_range;
576         Client *m_client;
577         std::vector<GameGlobalShaderConstantSetter *> created_nosky;
578 public:
579         GameGlobalShaderConstantSetterFactory(bool *force_fog_off,
580                         f32 *fog_range, Client *client) :
581                 m_sky(NULL),
582                 m_force_fog_off(force_fog_off),
583                 m_fog_range(fog_range),
584                 m_client(client)
585         {}
586
587         void setSky(Sky *sky) {
588                 m_sky = sky;
589                 for (GameGlobalShaderConstantSetter *ggscs : created_nosky) {
590                         ggscs->setSky(m_sky);
591                 }
592                 created_nosky.clear();
593         }
594
595         virtual IShaderConstantSetter* create()
596         {
597                 auto *scs = new GameGlobalShaderConstantSetter(
598                                 m_sky, m_force_fog_off, m_fog_range, m_client);
599                 if (!m_sky)
600                         created_nosky.push_back(scs);
601                 return scs;
602         }
603 };
604
605 #ifdef __ANDROID__
606 #define SIZE_TAG "size[11,5.5]"
607 #else
608 #define SIZE_TAG "size[11,5.5,true]" // Fixed size on desktop
609 #endif
610
611 /****************************************************************************
612  ****************************************************************************/
613
614 const float object_hit_delay = 0.2;
615
616 struct FpsControl {
617         u32 last_time, busy_time, sleep_time;
618 };
619
620
621 /* The reason the following structs are not anonymous structs within the
622  * class is that they are not used by the majority of member functions and
623  * many functions that do require objects of thse types do not modify them
624  * (so they can be passed as a const qualified parameter)
625  */
626
627 struct GameRunData {
628         u16 dig_index;
629         u16 new_playeritem;
630         PointedThing pointed_old;
631         bool digging;
632         bool punching;
633         bool btn_down_for_dig;
634         bool dig_instantly;
635         bool digging_blocked;
636         bool reset_jump_timer;
637         float nodig_delay_timer;
638         float dig_time;
639         float dig_time_complete;
640         float repeat_place_timer;
641         float object_hit_delay_timer;
642         float time_from_last_punch;
643         ClientActiveObject *selected_object;
644
645         float jump_timer;
646         float damage_flash;
647         float update_draw_list_timer;
648
649         f32 fog_range;
650
651         v3f update_draw_list_last_cam_dir;
652
653         float time_of_day_smooth;
654 };
655
656 class Game;
657
658 struct ClientEventHandler
659 {
660         void (Game::*handler)(ClientEvent *, CameraOrientation *);
661 };
662
663 class Game {
664 public:
665         Game();
666         ~Game();
667
668         bool startup(bool *kill,
669                         InputHandler *input,
670                         const GameStartData &game_params,
671                         std::string &error_message,
672                         bool *reconnect,
673                         ChatBackend *chat_backend);
674
675
676         void run();
677         void shutdown();
678
679         void extendedResourceCleanup();
680
681         // Basic initialisation
682         bool init(const std::string &map_dir, const std::string &address,
683                         u16 port, const SubgameSpec &gamespec);
684         bool initSound();
685         bool createSingleplayerServer(const std::string &map_dir,
686                         const SubgameSpec &gamespec, u16 port);
687
688         // Client creation
689         bool createClient(const GameStartData &start_data);
690         bool initGui();
691
692         // Client connection
693         bool connectToServer(const GameStartData &start_data,
694                         bool *connect_ok, bool *aborted);
695         bool getServerContent(bool *aborted);
696
697         // Main loop
698
699         void updateInteractTimers(f32 dtime);
700         bool checkConnection();
701         bool handleCallbacks();
702         void processQueues();
703         void updateProfilers(const RunStats &stats, const FpsControl &draw_times, f32 dtime);
704         void updateStats(RunStats *stats, const FpsControl &draw_times, f32 dtime);
705         void updateProfilerGraphs(ProfilerGraph *graph);
706
707         // Input related
708         void processUserInput(f32 dtime);
709         void processKeyInput();
710         void processItemSelection(u16 *new_playeritem);
711
712         void dropSelectedItem(bool single_item = false);
713         void openInventory();
714         void openEnderchest();
715         void openConsole(float scale, const wchar_t *line=NULL);
716         void toggleFreeMove();
717         void toggleFreeMoveAlt();
718         void togglePitchMove();
719         void toggleFast();
720         void toggleNoClip();
721         void toggleKillaura();
722         void toggleFreecam();
723         void toggleScaffold();
724         void toggleNextItem();
725         void toggleCinematic();
726         void toggleAutoforward();
727
728         void toggleMinimap(bool shift_pressed);
729         void toggleFog();
730         void toggleDebug();
731         void toggleUpdateCamera();
732         void updatePlayerCAOVisibility();
733
734         void increaseViewRange();
735         void decreaseViewRange();
736         void toggleFullViewRange();
737         void checkZoomEnabled();
738
739         void updateCameraDirection(CameraOrientation *cam, float dtime);
740         void updateCameraOrientation(CameraOrientation *cam, float dtime);
741         void updatePlayerControl(const CameraOrientation &cam);
742         void step(f32 *dtime);
743         void processClientEvents(CameraOrientation *cam);
744         void updateCamera(u32 busy_time, f32 dtime);
745         void updateSound(f32 dtime);
746         void processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug);
747         /*!
748          * Returns the object or node the player is pointing at.
749          * Also updates the selected thing in the Hud.
750          *
751          * @param[in]  shootline         the shootline, starting from
752          * the camera position. This also gives the maximal distance
753          * of the search.
754          * @param[in]  liquids_pointable if false, liquids are ignored
755          * @param[in]  look_for_object   if false, objects are ignored
756          * @param[in]  camera_offset     offset of the camera
757          * @param[out] selected_object   the selected object or
758          * NULL if not found
759          */
760         PointedThing updatePointedThing(
761                         const core::line3d<f32> &shootline, bool liquids_pointable,
762                         bool look_for_object, const v3s16 &camera_offset);
763         void handlePointingAtNothing(const ItemStack &playerItem);
764         void handlePointingAtNode(const PointedThing &pointed,
765                         const ItemStack &selected_item, const ItemStack &hand_item, f32 dtime);
766         void handlePointingAtObject(const PointedThing &pointed, const ItemStack &playeritem,
767                         const v3f &player_position, bool show_debug);
768         void handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
769                         const ItemStack &selected_item, const ItemStack &hand_item, f32 dtime);
770         void updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
771                         const CameraOrientation &cam);
772
773         // Misc
774         void limitFps(FpsControl *fps_timings, f32 *dtime);
775
776         void showOverlayMessage(const char *msg, float dtime, int percent,
777                         bool draw_clouds = true);
778
779         static void freecamChangedCallback(const std::string &setting_name, void *data);
780         static void settingChangedCallback(const std::string &setting_name, void *data);
781         static void updateAllMapBlocksCallback(const std::string &setting_name, void *data);
782         void readSettings();
783
784         bool isKeyDown(GameKeyType k);
785         bool wasKeyDown(GameKeyType k);
786         bool wasKeyPressed(GameKeyType k);
787         bool wasKeyReleased(GameKeyType k);
788
789 #ifdef __ANDROID__
790         void handleAndroidChatInput();
791 #endif
792
793         struct Flags {
794                 bool force_fog_off = false;
795                 bool disable_camera_update = false;
796         };
797
798         void showDeathFormspec();
799         void showPauseMenu();
800
801         // ClientEvent handlers
802         void handleClientEvent_None(ClientEvent *event, CameraOrientation *cam);
803         void handleClientEvent_PlayerDamage(ClientEvent *event, CameraOrientation *cam);
804         void handleClientEvent_PlayerForceMove(ClientEvent *event, CameraOrientation *cam);
805         void handleClientEvent_Deathscreen(ClientEvent *event, CameraOrientation *cam);
806         void handleClientEvent_ShowFormSpec(ClientEvent *event, CameraOrientation *cam);
807         void handleClientEvent_ShowLocalFormSpec(ClientEvent *event, CameraOrientation *cam);
808         void handleClientEvent_HandleParticleEvent(ClientEvent *event,
809                 CameraOrientation *cam);
810         void handleClientEvent_HudAdd(ClientEvent *event, CameraOrientation *cam);
811         void handleClientEvent_HudRemove(ClientEvent *event, CameraOrientation *cam);
812         void handleClientEvent_HudChange(ClientEvent *event, CameraOrientation *cam);
813         void handleClientEvent_SetSky(ClientEvent *event, CameraOrientation *cam);
814         void handleClientEvent_SetSun(ClientEvent *event, CameraOrientation *cam);
815         void handleClientEvent_SetMoon(ClientEvent *event, CameraOrientation *cam);
816         void handleClientEvent_SetStars(ClientEvent *event, CameraOrientation *cam);
817         void handleClientEvent_OverrideDayNigthRatio(ClientEvent *event,
818                 CameraOrientation *cam);
819         void handleClientEvent_CloudParams(ClientEvent *event, CameraOrientation *cam);
820
821         void updateChat(f32 dtime, const v2u32 &screensize);
822
823         bool nodePlacement(const ItemDefinition &selected_def, const ItemStack &selected_item,
824                 const v3s16 &nodepos, const v3s16 &neighbourpos, const PointedThing &pointed,
825                 const NodeMetadata *meta, bool force = false);
826         static const ClientEventHandler clientEventHandler[CLIENTEVENT_MAX];
827
828         InputHandler *input = nullptr;
829
830         Client *client = nullptr;
831         Server *server = nullptr;
832
833         IWritableTextureSource *texture_src = nullptr;
834         IWritableShaderSource *shader_src = nullptr;
835
836         // When created, these will be filled with data received from the server
837         IWritableItemDefManager *itemdef_manager = nullptr;
838         NodeDefManager *nodedef_manager = nullptr;
839
840         GameOnDemandSoundFetcher soundfetcher; // useful when testing
841         ISoundManager *sound = nullptr;
842         bool sound_is_dummy = false;
843         SoundMaker *soundmaker = nullptr;
844
845         ChatBackend *chat_backend = nullptr;
846         LogOutputBuffer m_chat_log_buf;
847
848         EventManager *eventmgr = nullptr;
849         QuicktuneShortcutter *quicktune = nullptr;
850         bool registration_confirmation_shown = false;
851
852         std::unique_ptr<GameUI> m_game_ui;
853         GUIChatConsole *gui_chat_console = nullptr; // Free using ->Drop()
854         CheatMenu *m_cheat_menu = nullptr;
855         MapDrawControl *draw_control = nullptr;
856         Camera *camera = nullptr;
857         Clouds *clouds = nullptr;                         // Free using ->Drop()
858         Sky *sky = nullptr;                         // Free using ->Drop()
859         Hud *hud = nullptr;
860         Minimap *mapper = nullptr;
861
862         GameRunData runData;
863         Flags m_flags;
864
865         /* 'cache'
866            This class does take ownership/responsibily for cleaning up etc of any of
867            these items (e.g. device)
868         */
869         IrrlichtDevice *device;
870         video::IVideoDriver *driver;
871         scene::ISceneManager *smgr;
872         bool *kill;
873         std::string *error_message;
874         bool *reconnect_requested;
875         scene::ISceneNode *skybox;
876
877         bool simple_singleplayer_mode;
878         /* End 'cache' */
879
880         /* Pre-calculated values
881          */
882         int crack_animation_length;
883
884         IntervalLimiter profiler_interval;
885
886         /*
887          * TODO: Local caching of settings is not optimal and should at some stage
888          *       be updated to use a global settings object for getting thse values
889          *       (as opposed to the this local caching). This can be addressed in
890          *       a later release.
891          */
892         bool m_cache_doubletap_jump;
893         bool m_cache_enable_clouds;
894         bool m_cache_enable_joysticks;
895         bool m_cache_enable_particles;
896         bool m_cache_enable_fog;
897         bool m_cache_enable_noclip;
898         bool m_cache_enable_free_move;
899         f32  m_cache_mouse_sensitivity;
900         f32  m_cache_joystick_frustum_sensitivity;
901         f32  m_repeat_place_time;
902         f32  m_cache_cam_smoothing;
903         f32  m_cache_fog_start;
904
905         bool m_invert_mouse = false;
906         bool m_first_loop_after_window_activation = false;
907         bool m_camera_offset_changed = false;
908
909         bool m_does_lost_focus_pause_game = false;
910
911         CameraOrientation cam_view_target  = { 0 };
912         CameraOrientation cam_view  = { 0 };
913
914         int m_reset_HW_buffer_counter = 0;
915 #ifdef __ANDROID__
916         bool m_cache_hold_aux1;
917         bool m_android_chat_open;
918 #endif
919 };
920 extern Game *g_game;
921
922 void the_game(bool *kill,
923                 InputHandler *input,
924                 const GameStartData &start_data,
925                 std::string &error_message,
926                 ChatBackend &chat_backend,
927                 bool *reconnect_requested);