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