]> git.lizzy.rs Git - dragonfireclient.git/blob - src/game.cpp
eb59ee5ae9e30f9e44284ae3dd06426661c7699f
[dragonfireclient.git] / src / game.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-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 #include "game.h"
21
22 #include <iomanip>
23 #include "camera.h"
24 #include "client.h"
25 #include "client/tile.h"     // For TextureSource
26 #include "client/keys.h"
27 #include "client/joystick_controller.h"
28 #include "clientmap.h"
29 #include "clouds.h"
30 #include "config.h"
31 #include "content_cao.h"
32 #include "drawscene.h"
33 #include "event_manager.h"
34 #include "fontengine.h"
35 #include "itemdef.h"
36 #include "log.h"
37 #include "filesys.h"
38 #include "gettext.h"
39 #include "guiChatConsole.h"
40 #include "guiFormSpecMenu.h"
41 #include "guiKeyChangeMenu.h"
42 #include "guiPasswordChange.h"
43 #include "guiVolumeChange.h"
44 #include "mainmenumanager.h"
45 #include "mapblock.h"
46 #include "nodedef.h"         // Needed for determining pointing to nodes
47 #include "nodemetadata.h"
48 #include "particles.h"
49 #include "profiler.h"
50 #include "quicktune_shortcutter.h"
51 #include "server.h"
52 #include "settings.h"
53 #include "shader.h"          // For ShaderSource
54 #include "sky.h"
55 #include "subgame.h"
56 #include "tool.h"
57 #include "util/directiontables.h"
58 #include "util/pointedthing.h"
59 #include "irrlicht_changes/static_text.h"
60 #include "version.h"
61 #include "minimap.h"
62 #include "mapblock_mesh.h"
63 #include "script/scripting_client.h"
64
65 #include "sound.h"
66
67 #if USE_SOUND
68         #include "sound_openal.h"
69 #endif
70
71 #ifdef HAVE_TOUCHSCREENGUI
72         #include "touchscreengui.h"
73 #endif
74
75 extern Settings *g_settings;
76 extern Profiler *g_profiler;
77
78 /*
79         Text input system
80 */
81
82 struct TextDestNodeMetadata : public TextDest
83 {
84         TextDestNodeMetadata(v3s16 p, Client *client)
85         {
86                 m_p = p;
87                 m_client = client;
88         }
89         // This is deprecated I guess? -celeron55
90         void gotText(const std::wstring &text)
91         {
92                 std::string ntext = wide_to_utf8(text);
93                 infostream << "Submitting 'text' field of node at (" << m_p.X << ","
94                            << m_p.Y << "," << m_p.Z << "): " << ntext << std::endl;
95                 StringMap fields;
96                 fields["text"] = ntext;
97                 m_client->sendNodemetaFields(m_p, "", fields);
98         }
99         void gotText(const StringMap &fields)
100         {
101                 m_client->sendNodemetaFields(m_p, "", fields);
102         }
103
104         v3s16 m_p;
105         Client *m_client;
106 };
107
108 struct TextDestPlayerInventory : public TextDest
109 {
110         TextDestPlayerInventory(Client *client)
111         {
112                 m_client = client;
113                 m_formname = "";
114         }
115         TextDestPlayerInventory(Client *client, const std::string &formname)
116         {
117                 m_client = client;
118                 m_formname = formname;
119         }
120         void gotText(const StringMap &fields)
121         {
122                 m_client->sendInventoryFields(m_formname, fields);
123         }
124
125         Client *m_client;
126 };
127
128 struct LocalFormspecHandler : public TextDest
129 {
130         LocalFormspecHandler(const std::string &formname):
131                 m_client(NULL)
132         {
133                 m_formname = formname;
134         }
135
136         LocalFormspecHandler(const std::string &formname, Client *client):
137                 m_client(client)
138         {
139                 m_formname = formname;
140         }
141
142         void gotText(const StringMap &fields)
143         {
144                 if (m_formname == "MT_PAUSE_MENU") {
145                         if (fields.find("btn_sound") != fields.end()) {
146                                 g_gamecallback->changeVolume();
147                                 return;
148                         }
149
150                         if (fields.find("btn_key_config") != fields.end()) {
151                                 g_gamecallback->keyConfig();
152                                 return;
153                         }
154
155                         if (fields.find("btn_exit_menu") != fields.end()) {
156                                 g_gamecallback->disconnect();
157                                 return;
158                         }
159
160                         if (fields.find("btn_exit_os") != fields.end()) {
161                                 g_gamecallback->exitToOS();
162                                 return;
163                         }
164
165                         if (fields.find("btn_change_password") != fields.end()) {
166                                 g_gamecallback->changePassword();
167                                 return;
168                         }
169
170                         if (fields.find("quit") != fields.end()) {
171                                 return;
172                         }
173
174                         if (fields.find("btn_continue") != fields.end()) {
175                                 return;
176                         }
177                 }
178
179                 // Don't disable this part when modding is disabled, it's used in builtin
180                 m_client->getScript()->on_formspec_input(m_formname, fields);
181         }
182
183         Client *m_client;
184 };
185
186 /* Form update callback */
187
188 class NodeMetadataFormSource: public IFormSource
189 {
190 public:
191         NodeMetadataFormSource(ClientMap *map, v3s16 p):
192                 m_map(map),
193                 m_p(p)
194         {
195         }
196         std::string getForm()
197         {
198                 NodeMetadata *meta = m_map->getNodeMetadata(m_p);
199
200                 if (!meta)
201                         return "";
202
203                 return meta->getString("formspec");
204         }
205
206         virtual std::string resolveText(const std::string &str)
207         {
208                 NodeMetadata *meta = m_map->getNodeMetadata(m_p);
209
210                 if (!meta)
211                         return str;
212
213                 return meta->resolveString(str);
214         }
215
216         ClientMap *m_map;
217         v3s16 m_p;
218 };
219
220 class PlayerInventoryFormSource: public IFormSource
221 {
222 public:
223         PlayerInventoryFormSource(Client *client):
224                 m_client(client)
225         {
226         }
227         std::string getForm()
228         {
229                 LocalPlayer *player = m_client->getEnv().getLocalPlayer();
230                 return player->inventory_formspec;
231         }
232
233         Client *m_client;
234 };
235
236 /* Profiler display */
237
238 void update_profiler_gui(gui::IGUIStaticText *guitext_profiler, FontEngine *fe,
239                 u32 show_profiler, u32 show_profiler_max, s32 screen_height)
240 {
241         if (show_profiler == 0) {
242                 guitext_profiler->setVisible(false);
243         } else {
244
245                 std::ostringstream os(std::ios_base::binary);
246                 g_profiler->printPage(os, show_profiler, show_profiler_max);
247                 std::wstring text = utf8_to_wide(os.str());
248                 setStaticText(guitext_profiler, text.c_str());
249                 guitext_profiler->setVisible(true);
250
251                 s32 w = fe->getTextWidth(text.c_str());
252
253                 if (w < 400)
254                         w = 400;
255
256                 unsigned text_height = fe->getTextHeight();
257
258                 core::position2di upper_left, lower_right;
259
260                 upper_left.X  = 6;
261                 upper_left.Y  = (text_height + 5) * 2;
262                 lower_right.X = 12 + w;
263                 lower_right.Y = upper_left.Y + (text_height + 1) * MAX_PROFILER_TEXT_ROWS;
264
265                 if (lower_right.Y > screen_height * 2 / 3)
266                         lower_right.Y = screen_height * 2 / 3;
267
268                 core::rect<s32> rect(upper_left, lower_right);
269
270                 guitext_profiler->setRelativePosition(rect);
271                 guitext_profiler->setVisible(true);
272         }
273 }
274
275 class ProfilerGraph
276 {
277 private:
278         struct Piece {
279                 Profiler::GraphValues values;
280         };
281         struct Meta {
282                 float min;
283                 float max;
284                 video::SColor color;
285                 Meta(float initial = 0,
286                         video::SColor color = video::SColor(255, 255, 255, 255)):
287                         min(initial),
288                         max(initial),
289                         color(color)
290                 {}
291         };
292         std::deque<Piece> m_log;
293 public:
294         u32 m_log_max_size;
295
296         ProfilerGraph():
297                 m_log_max_size(200)
298         {}
299
300         void put(const Profiler::GraphValues &values)
301         {
302                 Piece piece;
303                 piece.values = values;
304                 m_log.push_back(piece);
305
306                 while (m_log.size() > m_log_max_size)
307                         m_log.erase(m_log.begin());
308         }
309
310         void draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
311                   gui::IGUIFont *font) const
312         {
313                 // Do *not* use UNORDERED_MAP here as the order needs
314                 // to be the same for each call to prevent flickering
315                 std::map<std::string, Meta> m_meta;
316
317                 for (std::deque<Piece>::const_iterator k = m_log.begin();
318                                 k != m_log.end(); ++k) {
319                         const Piece &piece = *k;
320
321                         for (Profiler::GraphValues::const_iterator i = piece.values.begin();
322                                         i != piece.values.end(); ++i) {
323                                 const std::string &id = i->first;
324                                 const float &value = i->second;
325                                 std::map<std::string, Meta>::iterator j = m_meta.find(id);
326
327                                 if (j == m_meta.end()) {
328                                         m_meta[id] = Meta(value);
329                                         continue;
330                                 }
331
332                                 if (value < j->second.min)
333                                         j->second.min = value;
334
335                                 if (value > j->second.max)
336                                         j->second.max = value;
337                         }
338                 }
339
340                 // Assign colors
341                 static const video::SColor usable_colors[] = {
342                         video::SColor(255, 255, 100, 100),
343                         video::SColor(255, 90, 225, 90),
344                         video::SColor(255, 100, 100, 255),
345                         video::SColor(255, 255, 150, 50),
346                         video::SColor(255, 220, 220, 100)
347                 };
348                 static const u32 usable_colors_count =
349                         sizeof(usable_colors) / sizeof(*usable_colors);
350                 u32 next_color_i = 0;
351
352                 for (std::map<std::string, Meta>::iterator i = m_meta.begin();
353                                 i != m_meta.end(); ++i) {
354                         Meta &meta = i->second;
355                         video::SColor color(255, 200, 200, 200);
356
357                         if (next_color_i < usable_colors_count)
358                                 color = usable_colors[next_color_i++];
359
360                         meta.color = color;
361                 }
362
363                 s32 graphh = 50;
364                 s32 textx = x_left + m_log_max_size + 15;
365                 s32 textx2 = textx + 200 - 15;
366                 s32 meta_i = 0;
367
368                 for (std::map<std::string, Meta>::const_iterator i = m_meta.begin();
369                                 i != m_meta.end(); ++i) {
370                         const std::string &id = i->first;
371                         const Meta &meta = i->second;
372                         s32 x = x_left;
373                         s32 y = y_bottom - meta_i * 50;
374                         float show_min = meta.min;
375                         float show_max = meta.max;
376
377                         if (show_min >= -0.0001 && show_max >= -0.0001) {
378                                 if (show_min <= show_max * 0.5)
379                                         show_min = 0;
380                         }
381
382                         s32 texth = 15;
383                         char buf[10];
384                         snprintf(buf, 10, "%.3g", show_max);
385                         font->draw(utf8_to_wide(buf).c_str(),
386                                         core::rect<s32>(textx, y - graphh,
387                                                    textx2, y - graphh + texth),
388                                         meta.color);
389                         snprintf(buf, 10, "%.3g", show_min);
390                         font->draw(utf8_to_wide(buf).c_str(),
391                                         core::rect<s32>(textx, y - texth,
392                                                    textx2, y),
393                                         meta.color);
394                         font->draw(utf8_to_wide(id).c_str(),
395                                         core::rect<s32>(textx, y - graphh / 2 - texth / 2,
396                                                    textx2, y - graphh / 2 + texth / 2),
397                                         meta.color);
398                         s32 graph1y = y;
399                         s32 graph1h = graphh;
400                         bool relativegraph = (show_min != 0 && show_min != show_max);
401                         float lastscaledvalue = 0.0;
402                         bool lastscaledvalue_exists = false;
403
404                         for (std::deque<Piece>::const_iterator j = m_log.begin();
405                                         j != m_log.end(); ++j) {
406                                 const Piece &piece = *j;
407                                 float value = 0;
408                                 bool value_exists = false;
409                                 Profiler::GraphValues::const_iterator k =
410                                         piece.values.find(id);
411
412                                 if (k != piece.values.end()) {
413                                         value = k->second;
414                                         value_exists = true;
415                                 }
416
417                                 if (!value_exists) {
418                                         x++;
419                                         lastscaledvalue_exists = false;
420                                         continue;
421                                 }
422
423                                 float scaledvalue = 1.0;
424
425                                 if (show_max != show_min)
426                                         scaledvalue = (value - show_min) / (show_max - show_min);
427
428                                 if (scaledvalue == 1.0 && value == 0) {
429                                         x++;
430                                         lastscaledvalue_exists = false;
431                                         continue;
432                                 }
433
434                                 if (relativegraph) {
435                                         if (lastscaledvalue_exists) {
436                                                 s32 ivalue1 = lastscaledvalue * graph1h;
437                                                 s32 ivalue2 = scaledvalue * graph1h;
438                                                 driver->draw2DLine(v2s32(x - 1, graph1y - ivalue1),
439                                                                    v2s32(x, graph1y - ivalue2), meta.color);
440                                         }
441
442                                         lastscaledvalue = scaledvalue;
443                                         lastscaledvalue_exists = true;
444                                 } else {
445                                         s32 ivalue = scaledvalue * graph1h;
446                                         driver->draw2DLine(v2s32(x, graph1y),
447                                                            v2s32(x, graph1y - ivalue), meta.color);
448                                 }
449
450                                 x++;
451                         }
452
453                         meta_i++;
454                 }
455         }
456 };
457
458 class NodeDugEvent: public MtEvent
459 {
460 public:
461         v3s16 p;
462         MapNode n;
463
464         NodeDugEvent(v3s16 p, MapNode n):
465                 p(p),
466                 n(n)
467         {}
468         const char *getType() const
469         {
470                 return "NodeDug";
471         }
472 };
473
474 class SoundMaker
475 {
476         ISoundManager *m_sound;
477         INodeDefManager *m_ndef;
478 public:
479         float m_player_step_timer;
480
481         SimpleSoundSpec m_player_step_sound;
482         SimpleSoundSpec m_player_leftpunch_sound;
483         SimpleSoundSpec m_player_rightpunch_sound;
484
485         SoundMaker(ISoundManager *sound, INodeDefManager *ndef):
486                 m_sound(sound),
487                 m_ndef(ndef),
488                 m_player_step_timer(0)
489         {
490         }
491
492         void playPlayerStep()
493         {
494                 if (m_player_step_timer <= 0 && m_player_step_sound.exists()) {
495                         m_player_step_timer = 0.03;
496                         m_sound->playSound(m_player_step_sound, false);
497                 }
498         }
499
500         static void viewBobbingStep(MtEvent *e, void *data)
501         {
502                 SoundMaker *sm = (SoundMaker *)data;
503                 sm->playPlayerStep();
504         }
505
506         static void playerRegainGround(MtEvent *e, void *data)
507         {
508                 SoundMaker *sm = (SoundMaker *)data;
509                 sm->playPlayerStep();
510         }
511
512         static void playerJump(MtEvent *e, void *data)
513         {
514                 //SoundMaker *sm = (SoundMaker*)data;
515         }
516
517         static void cameraPunchLeft(MtEvent *e, void *data)
518         {
519                 SoundMaker *sm = (SoundMaker *)data;
520                 sm->m_sound->playSound(sm->m_player_leftpunch_sound, false);
521         }
522
523         static void cameraPunchRight(MtEvent *e, void *data)
524         {
525                 SoundMaker *sm = (SoundMaker *)data;
526                 sm->m_sound->playSound(sm->m_player_rightpunch_sound, false);
527         }
528
529         static void nodeDug(MtEvent *e, void *data)
530         {
531                 SoundMaker *sm = (SoundMaker *)data;
532                 NodeDugEvent *nde = (NodeDugEvent *)e;
533                 sm->m_sound->playSound(sm->m_ndef->get(nde->n).sound_dug, false);
534         }
535
536         static void playerDamage(MtEvent *e, void *data)
537         {
538                 SoundMaker *sm = (SoundMaker *)data;
539                 sm->m_sound->playSound(SimpleSoundSpec("player_damage", 0.5), false);
540         }
541
542         static void playerFallingDamage(MtEvent *e, void *data)
543         {
544                 SoundMaker *sm = (SoundMaker *)data;
545                 sm->m_sound->playSound(SimpleSoundSpec("player_falling_damage", 0.5), false);
546         }
547
548         void registerReceiver(MtEventManager *mgr)
549         {
550                 mgr->reg("ViewBobbingStep", SoundMaker::viewBobbingStep, this);
551                 mgr->reg("PlayerRegainGround", SoundMaker::playerRegainGround, this);
552                 mgr->reg("PlayerJump", SoundMaker::playerJump, this);
553                 mgr->reg("CameraPunchLeft", SoundMaker::cameraPunchLeft, this);
554                 mgr->reg("CameraPunchRight", SoundMaker::cameraPunchRight, this);
555                 mgr->reg("NodeDug", SoundMaker::nodeDug, this);
556                 mgr->reg("PlayerDamage", SoundMaker::playerDamage, this);
557                 mgr->reg("PlayerFallingDamage", SoundMaker::playerFallingDamage, this);
558         }
559
560         void step(float dtime)
561         {
562                 m_player_step_timer -= dtime;
563         }
564 };
565
566 // Locally stored sounds don't need to be preloaded because of this
567 class GameOnDemandSoundFetcher: public OnDemandSoundFetcher
568 {
569         std::set<std::string> m_fetched;
570 private:
571         void paths_insert(std::set<std::string> &dst_paths,
572                 const std::string &base,
573                 const std::string &name)
574         {
575                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".ogg");
576                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".0.ogg");
577                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".1.ogg");
578                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".2.ogg");
579                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".3.ogg");
580                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".4.ogg");
581                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".5.ogg");
582                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".6.ogg");
583                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".7.ogg");
584                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".8.ogg");
585                 dst_paths.insert(base + DIR_DELIM + "sounds" + DIR_DELIM + name + ".9.ogg");
586         }
587 public:
588         void fetchSounds(const std::string &name,
589                 std::set<std::string> &dst_paths,
590                 std::set<std::string> &dst_datas)
591         {
592                 if (m_fetched.count(name))
593                         return;
594
595                 m_fetched.insert(name);
596
597                 paths_insert(dst_paths, porting::path_share, name);
598                 paths_insert(dst_paths, porting::path_user,  name);
599         }
600 };
601
602
603 // before 1.8 there isn't a "integer interface", only float
604 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
605 typedef f32 SamplerLayer_t;
606 #else
607 typedef s32 SamplerLayer_t;
608 #endif
609
610
611 class GameGlobalShaderConstantSetter : public IShaderConstantSetter
612 {
613         Sky *m_sky;
614         bool *m_force_fog_off;
615         f32 *m_fog_range;
616         bool m_fog_enabled;
617         CachedPixelShaderSetting<float, 4> m_sky_bg_color;
618         CachedPixelShaderSetting<float> m_fog_distance;
619         CachedVertexShaderSetting<float> m_animation_timer_vertex;
620         CachedPixelShaderSetting<float> m_animation_timer_pixel;
621         CachedPixelShaderSetting<float, 3> m_day_light;
622         CachedPixelShaderSetting<float, 3> m_eye_position_pixel;
623         CachedVertexShaderSetting<float, 3> m_eye_position_vertex;
624         CachedPixelShaderSetting<float, 3> m_minimap_yaw;
625         CachedPixelShaderSetting<SamplerLayer_t> m_base_texture;
626         CachedPixelShaderSetting<SamplerLayer_t> m_normal_texture;
627         CachedPixelShaderSetting<SamplerLayer_t> m_texture_flags;
628         Client *m_client;
629
630 public:
631         void onSettingsChange(const std::string &name)
632         {
633                 if (name == "enable_fog")
634                         m_fog_enabled = g_settings->getBool("enable_fog");
635         }
636
637         static void settingsCallback(const std::string &name, void *userdata)
638         {
639                 reinterpret_cast<GameGlobalShaderConstantSetter*>(userdata)->onSettingsChange(name);
640         }
641
642         void setSky(Sky *sky) { m_sky = sky; }
643
644         GameGlobalShaderConstantSetter(Sky *sky, bool *force_fog_off,
645                         f32 *fog_range, Client *client) :
646                 m_sky(sky),
647                 m_force_fog_off(force_fog_off),
648                 m_fog_range(fog_range),
649                 m_sky_bg_color("skyBgColor"),
650                 m_fog_distance("fogDistance"),
651                 m_animation_timer_vertex("animationTimer"),
652                 m_animation_timer_pixel("animationTimer"),
653                 m_day_light("dayLight"),
654                 m_eye_position_pixel("eyePosition"),
655                 m_eye_position_vertex("eyePosition"),
656                 m_minimap_yaw("yawVec"),
657                 m_base_texture("baseTexture"),
658                 m_normal_texture("normalTexture"),
659                 m_texture_flags("textureFlags"),
660                 m_client(client)
661         {
662                 g_settings->registerChangedCallback("enable_fog", settingsCallback, this);
663                 m_fog_enabled = g_settings->getBool("enable_fog");
664         }
665
666         ~GameGlobalShaderConstantSetter()
667         {
668                 g_settings->deregisterChangedCallback("enable_fog", settingsCallback, this);
669         }
670
671         virtual void onSetConstants(video::IMaterialRendererServices *services,
672                         bool is_highlevel)
673         {
674                 if (!is_highlevel)
675                         return;
676
677                 // Background color
678                 video::SColor bgcolor = m_sky->getBgColor();
679                 video::SColorf bgcolorf(bgcolor);
680                 float bgcolorfa[4] = {
681                         bgcolorf.r,
682                         bgcolorf.g,
683                         bgcolorf.b,
684                         bgcolorf.a,
685                 };
686                 m_sky_bg_color.set(bgcolorfa, services);
687
688                 // Fog distance
689                 float fog_distance = 10000 * BS;
690
691                 if (m_fog_enabled && !*m_force_fog_off)
692                         fog_distance = *m_fog_range;
693
694                 m_fog_distance.set(&fog_distance, services);
695
696                 u32 daynight_ratio = (float)m_client->getEnv().getDayNightRatio();
697                 video::SColorf sunlight;
698                 get_sunlight_color(&sunlight, daynight_ratio);
699                 float dnc[3] = {
700                         sunlight.r,
701                         sunlight.g,
702                         sunlight.b };
703                 m_day_light.set(dnc, services);
704
705                 u32 animation_timer = porting::getTimeMs() % 100000;
706                 float animation_timer_f = (float)animation_timer / 100000.f;
707                 m_animation_timer_vertex.set(&animation_timer_f, services);
708                 m_animation_timer_pixel.set(&animation_timer_f, services);
709
710                 float eye_position_array[3];
711                 v3f epos = m_client->getEnv().getLocalPlayer()->getEyePosition();
712 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
713                 eye_position_array[0] = epos.X;
714                 eye_position_array[1] = epos.Y;
715                 eye_position_array[2] = epos.Z;
716 #else
717                 epos.getAs3Values(eye_position_array);
718 #endif
719                 m_eye_position_pixel.set(eye_position_array, services);
720                 m_eye_position_vertex.set(eye_position_array, services);
721
722                 float minimap_yaw_array[3];
723                 v3f minimap_yaw = m_client->getMinimap()->getYawVec();
724 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
725                 minimap_yaw_array[0] = minimap_yaw.X;
726                 minimap_yaw_array[1] = minimap_yaw.Y;
727                 minimap_yaw_array[2] = minimap_yaw.Z;
728 #else
729                 minimap_yaw.getAs3Values(minimap_yaw_array);
730 #endif
731                 m_minimap_yaw.set(minimap_yaw_array, services);
732
733                 SamplerLayer_t base_tex = 0,
734                                 normal_tex = 1,
735                                 flags_tex = 2;
736                 m_base_texture.set(&base_tex, services);
737                 m_normal_texture.set(&normal_tex, services);
738                 m_texture_flags.set(&flags_tex, services);
739         }
740 };
741
742
743 class GameGlobalShaderConstantSetterFactory : public IShaderConstantSetterFactory
744 {
745         Sky *m_sky;
746         bool *m_force_fog_off;
747         f32 *m_fog_range;
748         Client *m_client;
749         std::vector<GameGlobalShaderConstantSetter *> created_nosky;
750 public:
751         GameGlobalShaderConstantSetterFactory(bool *force_fog_off,
752                         f32 *fog_range, Client *client) :
753                 m_sky(NULL),
754                 m_force_fog_off(force_fog_off),
755                 m_fog_range(fog_range),
756                 m_client(client)
757         {}
758
759         void setSky(Sky *sky) {
760                 m_sky = sky;
761                 for (size_t i = 0; i < created_nosky.size(); ++i) {
762                         created_nosky[i]->setSky(m_sky);
763                 }
764                 created_nosky.clear();
765         }
766
767         virtual IShaderConstantSetter* create()
768         {
769                 GameGlobalShaderConstantSetter *scs = new GameGlobalShaderConstantSetter(
770                                 m_sky, m_force_fog_off, m_fog_range, m_client);
771                 if (!m_sky)
772                         created_nosky.push_back(scs);
773                 return scs;
774         }
775 };
776
777
778 bool nodePlacementPrediction(Client &client,
779                 const ItemDefinition &playeritem_def, v3s16 nodepos, v3s16 neighbourpos)
780 {
781         std::string prediction = playeritem_def.node_placement_prediction;
782         INodeDefManager *nodedef = client.ndef();
783         ClientMap &map = client.getEnv().getClientMap();
784         MapNode node;
785         bool is_valid_position;
786
787         node = map.getNodeNoEx(nodepos, &is_valid_position);
788         if (!is_valid_position)
789                 return false;
790
791         if (prediction != "" && !nodedef->get(node).rightclickable) {
792                 verbosestream << "Node placement prediction for "
793                               << playeritem_def.name << " is "
794                               << prediction << std::endl;
795                 v3s16 p = neighbourpos;
796
797                 // Place inside node itself if buildable_to
798                 MapNode n_under = map.getNodeNoEx(nodepos, &is_valid_position);
799                 if (is_valid_position)
800                 {
801                         if (nodedef->get(n_under).buildable_to)
802                                 p = nodepos;
803                         else {
804                                 node = map.getNodeNoEx(p, &is_valid_position);
805                                 if (is_valid_position &&!nodedef->get(node).buildable_to)
806                                         return false;
807                         }
808                 }
809
810                 // Find id of predicted node
811                 content_t id;
812                 bool found = nodedef->getId(prediction, id);
813
814                 if (!found) {
815                         errorstream << "Node placement prediction failed for "
816                                     << playeritem_def.name << " (places "
817                                     << prediction
818                                     << ") - Name not known" << std::endl;
819                         return false;
820                 }
821
822                 // Predict param2 for facedir and wallmounted nodes
823                 u8 param2 = 0;
824
825                 if (nodedef->get(id).param_type_2 == CPT2_WALLMOUNTED ||
826                                 nodedef->get(id).param_type_2 == CPT2_COLORED_WALLMOUNTED) {
827                         v3s16 dir = nodepos - neighbourpos;
828
829                         if (abs(dir.Y) > MYMAX(abs(dir.X), abs(dir.Z))) {
830                                 param2 = dir.Y < 0 ? 1 : 0;
831                         } else if (abs(dir.X) > abs(dir.Z)) {
832                                 param2 = dir.X < 0 ? 3 : 2;
833                         } else {
834                                 param2 = dir.Z < 0 ? 5 : 4;
835                         }
836                 }
837
838                 if (nodedef->get(id).param_type_2 == CPT2_FACEDIR ||
839                                 nodedef->get(id).param_type_2 == CPT2_COLORED_FACEDIR) {
840                         v3s16 dir = nodepos - floatToInt(client.getEnv().getLocalPlayer()->getPosition(), BS);
841
842                         if (abs(dir.X) > abs(dir.Z)) {
843                                 param2 = dir.X < 0 ? 3 : 1;
844                         } else {
845                                 param2 = dir.Z < 0 ? 2 : 0;
846                         }
847                 }
848
849                 assert(param2 <= 5);
850
851                 //Check attachment if node is in group attached_node
852                 if (((ItemGroupList) nodedef->get(id).groups)["attached_node"] != 0) {
853                         static v3s16 wallmounted_dirs[8] = {
854                                 v3s16(0, 1, 0),
855                                 v3s16(0, -1, 0),
856                                 v3s16(1, 0, 0),
857                                 v3s16(-1, 0, 0),
858                                 v3s16(0, 0, 1),
859                                 v3s16(0, 0, -1),
860                         };
861                         v3s16 pp;
862
863                         if (nodedef->get(id).param_type_2 == CPT2_WALLMOUNTED ||
864                                         nodedef->get(id).param_type_2 == CPT2_COLORED_WALLMOUNTED)
865                                 pp = p + wallmounted_dirs[param2];
866                         else
867                                 pp = p + v3s16(0, -1, 0);
868
869                         if (!nodedef->get(map.getNodeNoEx(pp)).walkable)
870                                 return false;
871                 }
872
873                 // Add node to client map
874                 MapNode n(id, 0, param2);
875
876                 try {
877                         LocalPlayer *player = client.getEnv().getLocalPlayer();
878
879                         // Dont place node when player would be inside new node
880                         // NOTE: This is to be eventually implemented by a mod as client-side Lua
881                         if (!nodedef->get(n).walkable ||
882                                         g_settings->getBool("enable_build_where_you_stand") ||
883                                         (client.checkPrivilege("noclip") && g_settings->getBool("noclip")) ||
884                                         (nodedef->get(n).walkable &&
885                                          neighbourpos != player->getStandingNodePos() + v3s16(0, 1, 0) &&
886                                          neighbourpos != player->getStandingNodePos() + v3s16(0, 2, 0))) {
887
888                                 // This triggers the required mesh update too
889                                 client.addNode(p, n);
890                                 return true;
891                         }
892                 } catch (InvalidPositionException &e) {
893                         errorstream << "Node placement prediction failed for "
894                                     << playeritem_def.name << " (places "
895                                     << prediction
896                                     << ") - Position not loaded" << std::endl;
897                 }
898         }
899
900         return false;
901 }
902
903 static inline void create_formspec_menu(GUIFormSpecMenu **cur_formspec,
904                 Client *client, IrrlichtDevice *device, JoystickController *joystick,
905                 IFormSource *fs_src, TextDest *txt_dest)
906 {
907
908         if (*cur_formspec == 0) {
909                 *cur_formspec = new GUIFormSpecMenu(device, joystick,
910                         guiroot, -1, &g_menumgr, client, client->getTextureSource(),
911                         fs_src, txt_dest);
912                 (*cur_formspec)->doPause = false;
913
914                 /*
915                         Caution: do not call (*cur_formspec)->drop() here --
916                         the reference might outlive the menu, so we will
917                         periodically check if *cur_formspec is the only
918                         remaining reference (i.e. the menu was removed)
919                         and delete it in that case.
920                 */
921
922         } else {
923                 (*cur_formspec)->setFormSource(fs_src);
924                 (*cur_formspec)->setTextDest(txt_dest);
925         }
926
927 }
928
929 #ifdef __ANDROID__
930 #define SIZE_TAG "size[11,5.5]"
931 #else
932 #define SIZE_TAG "size[11,5.5,true]" // Fixed size on desktop
933 #endif
934
935 /******************************************************************************/
936 static void updateChat(Client &client, f32 dtime, bool show_debug,
937                 const v2u32 &screensize, bool show_chat, u32 show_profiler,
938                 ChatBackend &chat_backend, gui::IGUIStaticText *guitext_chat)
939 {
940         // Add chat log output for errors to be shown in chat
941         static LogOutputBuffer chat_log_error_buf(g_logger, LL_ERROR);
942
943         // Get new messages from error log buffer
944         while (!chat_log_error_buf.empty()) {
945                 std::wstring error_message = utf8_to_wide(chat_log_error_buf.get());
946                 if (!g_settings->getBool("disable_escape_sequences")) {
947                         error_message = L"\x1b(c@red)" + error_message + L"\x1b(c@white)";
948                 }
949                 chat_backend.addMessage(L"", error_message);
950         }
951
952         // Get new messages from client
953         std::wstring message;
954
955         while (client.getChatMessage(message)) {
956                 chat_backend.addUnparsedMessage(message);
957         }
958
959         // Remove old messages
960         chat_backend.step(dtime);
961
962         // Display all messages in a static text element
963         unsigned int recent_chat_count = chat_backend.getRecentBuffer().getLineCount();
964         EnrichedString recent_chat     = chat_backend.getRecentChat();
965         unsigned int line_height       = g_fontengine->getLineHeight();
966
967         setStaticText(guitext_chat, recent_chat);
968
969         // Update gui element size and position
970         s32 chat_y = 5;
971
972         if (show_debug)
973                 chat_y += 2 * line_height;
974
975         // first pass to calculate height of text to be set
976         s32 width = std::min(g_fontengine->getTextWidth(recent_chat.c_str()) + 10,
977                              porting::getWindowSize().X - 20);
978         core::rect<s32> rect(10, chat_y, width, chat_y + porting::getWindowSize().Y);
979         guitext_chat->setRelativePosition(rect);
980
981         //now use real height of text and adjust rect according to this size
982         rect = core::rect<s32>(10, chat_y, width,
983                                chat_y + guitext_chat->getTextHeight());
984
985
986         guitext_chat->setRelativePosition(rect);
987         // Don't show chat if disabled or empty or profiler is enabled
988         guitext_chat->setVisible(
989                 show_chat && recent_chat_count != 0 && !show_profiler);
990 }
991
992
993 /****************************************************************************
994  Fast key cache for main game loop
995  ****************************************************************************/
996
997 /* This is faster than using getKeySetting with the tradeoff that functions
998  * using it must make sure that it's initialised before using it and there is
999  * no error handling (for example bounds checking). This is really intended for
1000  * use only in the main running loop of the client (the_game()) where the faster
1001  * (up to 10x faster) key lookup is an asset. Other parts of the codebase
1002  * (e.g. formspecs) should continue using getKeySetting().
1003  */
1004 struct KeyCache {
1005
1006         KeyCache()
1007         {
1008                 handler = NULL;
1009                 populate();
1010                 populate_nonchanging();
1011         }
1012
1013         void populate();
1014
1015         // Keys that are not settings dependent
1016         void populate_nonchanging();
1017
1018         KeyPress key[KeyType::INTERNAL_ENUM_COUNT];
1019         InputHandler *handler;
1020 };
1021
1022 void KeyCache::populate_nonchanging()
1023 {
1024         key[KeyType::ESC] = EscapeKey;
1025 }
1026
1027 void KeyCache::populate()
1028 {
1029         key[KeyType::FORWARD]      = getKeySetting("keymap_forward");
1030         key[KeyType::BACKWARD]     = getKeySetting("keymap_backward");
1031         key[KeyType::LEFT]         = getKeySetting("keymap_left");
1032         key[KeyType::RIGHT]        = getKeySetting("keymap_right");
1033         key[KeyType::JUMP]         = getKeySetting("keymap_jump");
1034         key[KeyType::SPECIAL1]     = getKeySetting("keymap_special1");
1035         key[KeyType::SNEAK]        = getKeySetting("keymap_sneak");
1036
1037         key[KeyType::AUTORUN]      = getKeySetting("keymap_autorun");
1038
1039         key[KeyType::DROP]         = getKeySetting("keymap_drop");
1040         key[KeyType::INVENTORY]    = getKeySetting("keymap_inventory");
1041         key[KeyType::CHAT]         = getKeySetting("keymap_chat");
1042         key[KeyType::CMD]          = getKeySetting("keymap_cmd");
1043         key[KeyType::CMD_LOCAL]    = getKeySetting("keymap_cmd_local");
1044         key[KeyType::CONSOLE]      = getKeySetting("keymap_console");
1045         key[KeyType::MINIMAP]      = getKeySetting("keymap_minimap");
1046         key[KeyType::FREEMOVE]     = getKeySetting("keymap_freemove");
1047         key[KeyType::FASTMOVE]     = getKeySetting("keymap_fastmove");
1048         key[KeyType::NOCLIP]       = getKeySetting("keymap_noclip");
1049         key[KeyType::CINEMATIC]    = getKeySetting("keymap_cinematic");
1050         key[KeyType::SCREENSHOT]   = getKeySetting("keymap_screenshot");
1051         key[KeyType::TOGGLE_HUD]   = getKeySetting("keymap_toggle_hud");
1052         key[KeyType::TOGGLE_CHAT]  = getKeySetting("keymap_toggle_chat");
1053         key[KeyType::TOGGLE_FORCE_FOG_OFF]
1054                         = getKeySetting("keymap_toggle_force_fog_off");
1055         key[KeyType::TOGGLE_UPDATE_CAMERA]
1056                         = getKeySetting("keymap_toggle_update_camera");
1057         key[KeyType::TOGGLE_DEBUG]
1058                         = getKeySetting("keymap_toggle_debug");
1059         key[KeyType::TOGGLE_PROFILER]
1060                         = getKeySetting("keymap_toggle_profiler");
1061         key[KeyType::CAMERA_MODE]
1062                         = getKeySetting("keymap_camera_mode");
1063         key[KeyType::INCREASE_VIEWING_RANGE]
1064                         = getKeySetting("keymap_increase_viewing_range_min");
1065         key[KeyType::DECREASE_VIEWING_RANGE]
1066                         = getKeySetting("keymap_decrease_viewing_range_min");
1067         key[KeyType::RANGESELECT]
1068                         = getKeySetting("keymap_rangeselect");
1069         key[KeyType::ZOOM] = getKeySetting("keymap_zoom");
1070
1071         key[KeyType::QUICKTUNE_NEXT] = getKeySetting("keymap_quicktune_next");
1072         key[KeyType::QUICKTUNE_PREV] = getKeySetting("keymap_quicktune_prev");
1073         key[KeyType::QUICKTUNE_INC]  = getKeySetting("keymap_quicktune_inc");
1074         key[KeyType::QUICKTUNE_DEC]  = getKeySetting("keymap_quicktune_dec");
1075
1076         key[KeyType::DEBUG_STACKS]   = getKeySetting("keymap_print_debug_stacks");
1077
1078         if (handler) {
1079                 // First clear all keys, then re-add the ones we listen for
1080                 handler->dontListenForKeys();
1081                 for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
1082                         handler->listenForKey(key[i]);
1083                 }
1084                 handler->listenForKey(EscapeKey);
1085                 handler->listenForKey(CancelKey);
1086                 for (size_t i = 0; i < 10; i++) {
1087                         handler->listenForKey(NumberKey[i]);
1088                 }
1089         }
1090 }
1091
1092
1093 /****************************************************************************
1094
1095  ****************************************************************************/
1096
1097 const float object_hit_delay = 0.2;
1098
1099 struct FpsControl {
1100         u32 last_time, busy_time, sleep_time;
1101 };
1102
1103
1104 /* The reason the following structs are not anonymous structs within the
1105  * class is that they are not used by the majority of member functions and
1106  * many functions that do require objects of thse types do not modify them
1107  * (so they can be passed as a const qualified parameter)
1108  */
1109 struct CameraOrientation {
1110         f32 camera_yaw;    // "right/left"
1111         f32 camera_pitch;  // "up/down"
1112 };
1113
1114 struct GameRunData {
1115         u16 dig_index;
1116         u16 new_playeritem;
1117         PointedThing pointed_old;
1118         bool digging;
1119         bool ldown_for_dig;
1120         bool dig_instantly;
1121         bool left_punch;
1122         bool update_wielded_item_trigger;
1123         bool reset_jump_timer;
1124         float nodig_delay_timer;
1125         float dig_time;
1126         float dig_time_complete;
1127         float repeat_rightclick_timer;
1128         float object_hit_delay_timer;
1129         float time_from_last_punch;
1130         ClientActiveObject *selected_object;
1131
1132         float jump_timer;
1133         float damage_flash;
1134         float update_draw_list_timer;
1135         float statustext_time;
1136
1137         f32 fog_range;
1138
1139         v3f update_draw_list_last_cam_dir;
1140
1141         u32 profiler_current_page;
1142         u32 profiler_max_page;     // Number of pages
1143
1144         float time_of_day;
1145         float time_of_day_smooth;
1146 };
1147
1148 struct Jitter {
1149         f32 max, min, avg, counter, max_sample, min_sample, max_fraction;
1150 };
1151
1152 struct RunStats {
1153         u32 drawtime;
1154
1155         Jitter dtime_jitter, busy_time_jitter;
1156 };
1157
1158 /****************************************************************************
1159  THE GAME
1160  ****************************************************************************/
1161
1162 /* This is not intended to be a public class. If a public class becomes
1163  * desirable then it may be better to create another 'wrapper' class that
1164  * hides most of the stuff in this class (nothing in this class is required
1165  * by any other file) but exposes the public methods/data only.
1166  */
1167 class Game {
1168 public:
1169         Game();
1170         ~Game();
1171
1172         bool startup(bool *kill,
1173                         bool random_input,
1174                         InputHandler *input,
1175                         IrrlichtDevice *device,
1176                         const std::string &map_dir,
1177                         const std::string &playername,
1178                         const std::string &password,
1179                         // If address is "", local server is used and address is updated
1180                         std::string *address,
1181                         u16 port,
1182                         std::string &error_message,
1183                         bool *reconnect,
1184                         ChatBackend *chat_backend,
1185                         const SubgameSpec &gamespec,    // Used for local game
1186                         bool simple_singleplayer_mode);
1187
1188         void run();
1189         void shutdown();
1190
1191 protected:
1192
1193         void extendedResourceCleanup();
1194
1195         // Basic initialisation
1196         bool init(const std::string &map_dir, std::string *address,
1197                         u16 port,
1198                         const SubgameSpec &gamespec);
1199         bool initSound();
1200         bool createSingleplayerServer(const std::string &map_dir,
1201                         const SubgameSpec &gamespec, u16 port, std::string *address);
1202
1203         // Client creation
1204         bool createClient(const std::string &playername,
1205                         const std::string &password, std::string *address, u16 port);
1206         bool initGui();
1207
1208         // Client connection
1209         bool connectToServer(const std::string &playername,
1210                         const std::string &password, std::string *address, u16 port,
1211                         bool *connect_ok, bool *aborted);
1212         bool getServerContent(bool *aborted);
1213
1214         // Main loop
1215
1216         void updateInteractTimers(f32 dtime);
1217         bool checkConnection();
1218         bool handleCallbacks();
1219         void processQueues();
1220         void updateProfilers(const RunStats &stats, const FpsControl &draw_times, f32 dtime);
1221         void addProfilerGraphs(const RunStats &stats, const FpsControl &draw_times, f32 dtime);
1222         void updateStats(RunStats *stats, const FpsControl &draw_times, f32 dtime);
1223
1224         // Input related
1225         void processUserInput(f32 dtime);
1226         void processKeyInput();
1227         void processItemSelection(u16 *new_playeritem);
1228
1229         void dropSelectedItem();
1230         void openInventory();
1231         void openConsole(float scale, const wchar_t *line=NULL);
1232         void toggleFreeMove();
1233         void toggleFreeMoveAlt();
1234         void toggleFast();
1235         void toggleNoClip();
1236         void toggleCinematic();
1237         void toggleAutorun();
1238
1239         void toggleChat();
1240         void toggleHud();
1241         void toggleMinimap(bool shift_pressed);
1242         void toggleFog();
1243         void toggleDebug();
1244         void toggleUpdateCamera();
1245         void toggleProfiler();
1246
1247         void increaseViewRange();
1248         void decreaseViewRange();
1249         void toggleFullViewRange();
1250
1251         void updateCameraDirection(CameraOrientation *cam, float dtime);
1252         void updateCameraOrientation(CameraOrientation *cam, float dtime);
1253         void updatePlayerControl(const CameraOrientation &cam);
1254         void step(f32 *dtime);
1255         void processClientEvents(CameraOrientation *cam);
1256         void updateCamera(u32 busy_time, f32 dtime);
1257         void updateSound(f32 dtime);
1258         void processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug);
1259         /*!
1260          * Returns the object or node the player is pointing at.
1261          * Also updates the selected thing in the Hud.
1262          *
1263          * @param[in]  shootline         the shootline, starting from
1264          * the camera position. This also gives the maximal distance
1265          * of the search.
1266          * @param[in]  liquids_pointable if false, liquids are ignored
1267          * @param[in]  look_for_object   if false, objects are ignored
1268          * @param[in]  camera_offset     offset of the camera
1269          * @param[out] selected_object   the selected object or
1270          * NULL if not found
1271          */
1272         PointedThing updatePointedThing(
1273                         const core::line3d<f32> &shootline, bool liquids_pointable,
1274                         bool look_for_object, const v3s16 &camera_offset);
1275         void handlePointingAtNothing(const ItemStack &playerItem);
1276         void handlePointingAtNode(const PointedThing &pointed, const ItemDefinition &playeritem_def,
1277                         const ToolCapabilities &playeritem_toolcap, f32 dtime);
1278         void handlePointingAtObject(const PointedThing &pointed, const ItemStack &playeritem,
1279                         const v3f &player_position, bool show_debug);
1280         void handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
1281                         const ToolCapabilities &playeritem_toolcap, f32 dtime);
1282         void updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
1283                         const CameraOrientation &cam);
1284         void updateGui(const RunStats &stats, f32 dtime, const CameraOrientation &cam);
1285         void updateProfilerGraphs(ProfilerGraph *graph);
1286
1287         // Misc
1288         void limitFps(FpsControl *fps_timings, f32 *dtime);
1289
1290         void showOverlayMessage(const wchar_t *msg, float dtime, int percent,
1291                         bool draw_clouds = true);
1292
1293         static void settingChangedCallback(const std::string &setting_name, void *data);
1294         void readSettings();
1295
1296         inline bool getLeftClicked()
1297         {
1298                 return input->getLeftClicked() ||
1299                         input->joystick.getWasKeyDown(KeyType::MOUSE_L);
1300         }
1301         inline bool getRightClicked()
1302         {
1303                 return input->getRightClicked() ||
1304                         input->joystick.getWasKeyDown(KeyType::MOUSE_R);
1305         }
1306         inline bool isLeftPressed()
1307         {
1308                 return input->getLeftState() ||
1309                         input->joystick.isKeyDown(KeyType::MOUSE_L);
1310         }
1311         inline bool isRightPressed()
1312         {
1313                 return input->getRightState() ||
1314                         input->joystick.isKeyDown(KeyType::MOUSE_R);
1315         }
1316         inline bool getLeftReleased()
1317         {
1318                 return input->getLeftReleased() ||
1319                         input->joystick.wasKeyReleased(KeyType::MOUSE_L);
1320         }
1321
1322         inline bool isKeyDown(GameKeyType k)
1323         {
1324                 return input->isKeyDown(keycache.key[k]) || input->joystick.isKeyDown(k);
1325         }
1326         inline bool wasKeyDown(GameKeyType k)
1327         {
1328                 return input->wasKeyDown(keycache.key[k]) || input->joystick.wasKeyDown(k);
1329         }
1330
1331 #ifdef __ANDROID__
1332         void handleAndroidChatInput();
1333 #endif
1334
1335 private:
1336         void showPauseMenu();
1337
1338         InputHandler *input;
1339
1340         Client *client;
1341         Server *server;
1342
1343         IWritableTextureSource *texture_src;
1344         IWritableShaderSource *shader_src;
1345
1346         // When created, these will be filled with data received from the server
1347         IWritableItemDefManager *itemdef_manager;
1348         IWritableNodeDefManager *nodedef_manager;
1349
1350         GameOnDemandSoundFetcher soundfetcher; // useful when testing
1351         ISoundManager *sound;
1352         bool sound_is_dummy;
1353         SoundMaker *soundmaker;
1354
1355         ChatBackend *chat_backend;
1356
1357         GUIFormSpecMenu *current_formspec;
1358         //default: "". If other than "", empty show_formspec packets will only close the formspec when the formname matches
1359         std::string cur_formname;
1360
1361         EventManager *eventmgr;
1362         QuicktuneShortcutter *quicktune;
1363
1364         GUIChatConsole *gui_chat_console; // Free using ->Drop()
1365         MapDrawControl *draw_control;
1366         Camera *camera;
1367         Clouds *clouds;                   // Free using ->Drop()
1368         Sky *sky;                         // Free using ->Drop()
1369         Inventory *local_inventory;
1370         Hud *hud;
1371         Minimap *mapper;
1372
1373         GameRunData runData;
1374         GameUIFlags flags;
1375
1376         /* 'cache'
1377            This class does take ownership/responsibily for cleaning up etc of any of
1378            these items (e.g. device)
1379         */
1380         IrrlichtDevice *device;
1381         video::IVideoDriver *driver;
1382         scene::ISceneManager *smgr;
1383         bool *kill;
1384         std::string *error_message;
1385         bool *reconnect_requested;
1386         scene::ISceneNode *skybox;
1387
1388         bool random_input;
1389         bool simple_singleplayer_mode;
1390         /* End 'cache' */
1391
1392         /* Pre-calculated values
1393          */
1394         int crack_animation_length;
1395
1396         /* GUI stuff
1397          */
1398         gui::IGUIStaticText *guitext;          // First line of debug text
1399         gui::IGUIStaticText *guitext2;         // Second line of debug text
1400         gui::IGUIStaticText *guitext_info;     // At the middle of the screen
1401         gui::IGUIStaticText *guitext_status;
1402         gui::IGUIStaticText *guitext_chat;         // Chat text
1403         gui::IGUIStaticText *guitext_profiler; // Profiler text
1404
1405         std::wstring infotext;
1406         std::wstring m_statustext;
1407
1408         KeyCache keycache;
1409
1410         IntervalLimiter profiler_interval;
1411
1412         /*
1413          * TODO: Local caching of settings is not optimal and should at some stage
1414          *       be updated to use a global settings object for getting thse values
1415          *       (as opposed to the this local caching). This can be addressed in
1416          *       a later release.
1417          */
1418         bool m_cache_doubletap_jump;
1419         bool m_cache_enable_clouds;
1420         bool m_cache_enable_joysticks;
1421         bool m_cache_enable_particles;
1422         bool m_cache_enable_fog;
1423         bool m_cache_enable_noclip;
1424         bool m_cache_enable_free_move;
1425         f32  m_cache_mouse_sensitivity;
1426         f32  m_cache_joystick_frustum_sensitivity;
1427         f32  m_repeat_right_click_time;
1428         f32  m_cache_cam_smoothing;
1429         f32  m_cache_fog_start;
1430
1431         bool m_invert_mouse;
1432         bool m_first_loop_after_window_activation;
1433         bool m_camera_offset_changed;
1434
1435 #ifdef __ANDROID__
1436         bool m_cache_hold_aux1;
1437         bool m_android_chat_open;
1438 #endif
1439 };
1440
1441 Game::Game() :
1442         client(NULL),
1443         server(NULL),
1444         texture_src(NULL),
1445         shader_src(NULL),
1446         itemdef_manager(NULL),
1447         nodedef_manager(NULL),
1448         sound(NULL),
1449         sound_is_dummy(false),
1450         soundmaker(NULL),
1451         chat_backend(NULL),
1452         current_formspec(NULL),
1453         cur_formname(""),
1454         eventmgr(NULL),
1455         quicktune(NULL),
1456         gui_chat_console(NULL),
1457         draw_control(NULL),
1458         camera(NULL),
1459         clouds(NULL),
1460         sky(NULL),
1461         local_inventory(NULL),
1462         hud(NULL),
1463         mapper(NULL),
1464         m_invert_mouse(false),
1465         m_first_loop_after_window_activation(false),
1466         m_camera_offset_changed(false)
1467 {
1468         g_settings->registerChangedCallback("doubletap_jump",
1469                 &settingChangedCallback, this);
1470         g_settings->registerChangedCallback("enable_clouds",
1471                 &settingChangedCallback, this);
1472         g_settings->registerChangedCallback("doubletap_joysticks",
1473                 &settingChangedCallback, this);
1474         g_settings->registerChangedCallback("enable_particles",
1475                 &settingChangedCallback, this);
1476         g_settings->registerChangedCallback("enable_fog",
1477                 &settingChangedCallback, this);
1478         g_settings->registerChangedCallback("mouse_sensitivity",
1479                 &settingChangedCallback, this);
1480         g_settings->registerChangedCallback("joystick_frustum_sensitivity",
1481                 &settingChangedCallback, this);
1482         g_settings->registerChangedCallback("repeat_rightclick_time",
1483                 &settingChangedCallback, this);
1484         g_settings->registerChangedCallback("noclip",
1485                 &settingChangedCallback, this);
1486         g_settings->registerChangedCallback("free_move",
1487                 &settingChangedCallback, this);
1488         g_settings->registerChangedCallback("cinematic",
1489                 &settingChangedCallback, this);
1490         g_settings->registerChangedCallback("cinematic_camera_smoothing",
1491                 &settingChangedCallback, this);
1492         g_settings->registerChangedCallback("camera_smoothing",
1493                 &settingChangedCallback, this);
1494
1495         readSettings();
1496
1497 #ifdef __ANDROID__
1498         m_cache_hold_aux1 = false;      // This is initialised properly later
1499 #endif
1500
1501 }
1502
1503
1504
1505 /****************************************************************************
1506  MinetestApp Public
1507  ****************************************************************************/
1508
1509 Game::~Game()
1510 {
1511         delete client;
1512         delete soundmaker;
1513         if (!sound_is_dummy)
1514                 delete sound;
1515
1516         delete server; // deleted first to stop all server threads
1517
1518         delete hud;
1519         delete local_inventory;
1520         delete camera;
1521         delete quicktune;
1522         delete eventmgr;
1523         delete texture_src;
1524         delete shader_src;
1525         delete nodedef_manager;
1526         delete itemdef_manager;
1527         delete draw_control;
1528
1529         extendedResourceCleanup();
1530
1531         g_settings->deregisterChangedCallback("doubletap_jump",
1532                 &settingChangedCallback, this);
1533         g_settings->deregisterChangedCallback("enable_clouds",
1534                 &settingChangedCallback, this);
1535         g_settings->deregisterChangedCallback("enable_particles",
1536                 &settingChangedCallback, this);
1537         g_settings->deregisterChangedCallback("enable_fog",
1538                 &settingChangedCallback, this);
1539         g_settings->deregisterChangedCallback("mouse_sensitivity",
1540                 &settingChangedCallback, this);
1541         g_settings->deregisterChangedCallback("repeat_rightclick_time",
1542                 &settingChangedCallback, this);
1543         g_settings->deregisterChangedCallback("noclip",
1544                 &settingChangedCallback, this);
1545         g_settings->deregisterChangedCallback("free_move",
1546                 &settingChangedCallback, this);
1547         g_settings->deregisterChangedCallback("cinematic",
1548                 &settingChangedCallback, this);
1549         g_settings->deregisterChangedCallback("cinematic_camera_smoothing",
1550                 &settingChangedCallback, this);
1551         g_settings->deregisterChangedCallback("camera_smoothing",
1552                 &settingChangedCallback, this);
1553 }
1554
1555 bool Game::startup(bool *kill,
1556                 bool random_input,
1557                 InputHandler *input,
1558                 IrrlichtDevice *device,
1559                 const std::string &map_dir,
1560                 const std::string &playername,
1561                 const std::string &password,
1562                 std::string *address,     // can change if simple_singleplayer_mode
1563                 u16 port,
1564                 std::string &error_message,
1565                 bool *reconnect,
1566                 ChatBackend *chat_backend,
1567                 const SubgameSpec &gamespec,
1568                 bool simple_singleplayer_mode)
1569 {
1570         // "cache"
1571         this->device              = device;
1572         this->kill                = kill;
1573         this->error_message       = &error_message;
1574         this->reconnect_requested = reconnect;
1575         this->random_input        = random_input;
1576         this->input               = input;
1577         this->chat_backend        = chat_backend;
1578         this->simple_singleplayer_mode = simple_singleplayer_mode;
1579
1580         keycache.handler = input;
1581         keycache.populate();
1582
1583         driver              = device->getVideoDriver();
1584         smgr                = device->getSceneManager();
1585
1586         smgr->getParameters()->setAttribute(scene::OBJ_LOADER_IGNORE_MATERIAL_FILES, true);
1587
1588         memset(&runData, 0, sizeof(runData));
1589         runData.time_from_last_punch = 10.0;
1590         runData.profiler_max_page = 3;
1591         runData.update_wielded_item_trigger = true;
1592
1593         memset(&flags, 0, sizeof(flags));
1594         flags.show_chat = true;
1595         flags.show_hud = true;
1596         flags.show_debug = g_settings->getBool("show_debug");
1597         m_invert_mouse = g_settings->getBool("invert_mouse");
1598         m_first_loop_after_window_activation = true;
1599
1600         if (!init(map_dir, address, port, gamespec))
1601                 return false;
1602
1603         if (!createClient(playername, password, address, port))
1604                 return false;
1605
1606         return true;
1607 }
1608
1609
1610 void Game::run()
1611 {
1612         ProfilerGraph graph;
1613         RunStats stats              = { 0 };
1614         CameraOrientation cam_view_target  = { 0 };
1615         CameraOrientation cam_view  = { 0 };
1616         FpsControl draw_times       = { 0 };
1617         f32 dtime; // in seconds
1618
1619         /* Clear the profiler */
1620         Profiler::GraphValues dummyvalues;
1621         g_profiler->graphGet(dummyvalues);
1622
1623         draw_times.last_time = device->getTimer()->getTime();
1624
1625         set_light_table(g_settings->getFloat("display_gamma"));
1626
1627 #ifdef __ANDROID__
1628         m_cache_hold_aux1 = g_settings->getBool("fast_move")
1629                         && client->checkPrivilege("fast");
1630 #endif
1631
1632         while (device->run()
1633                         && !(*kill || g_gamecallback->shutdown_requested
1634                         || (server && server->getShutdownRequested()))) {
1635
1636                 /* Must be called immediately after a device->run() call because it
1637                  * uses device->getTimer()->getTime()
1638                  */
1639                 limitFps(&draw_times, &dtime);
1640
1641                 updateStats(&stats, draw_times, dtime);
1642                 updateInteractTimers(dtime);
1643
1644                 if (!checkConnection())
1645                         break;
1646                 if (!handleCallbacks())
1647                         break;
1648
1649                 processQueues();
1650
1651                 infotext = L"";
1652                 hud->resizeHotbar();
1653
1654                 updateProfilers(stats, draw_times, dtime);
1655                 processUserInput(dtime);
1656                 // Update camera before player movement to avoid camera lag of one frame
1657                 updateCameraDirection(&cam_view_target, dtime);
1658                 cam_view.camera_yaw += (cam_view_target.camera_yaw -
1659                                 cam_view.camera_yaw) * m_cache_cam_smoothing;
1660                 cam_view.camera_pitch += (cam_view_target.camera_pitch -
1661                                 cam_view.camera_pitch) * m_cache_cam_smoothing;
1662                 updatePlayerControl(cam_view);
1663                 step(&dtime);
1664                 processClientEvents(&cam_view_target);
1665                 updateCamera(draw_times.busy_time, dtime);
1666                 updateSound(dtime);
1667                 processPlayerInteraction(dtime, flags.show_hud, flags.show_debug);
1668                 updateFrame(&graph, &stats, dtime, cam_view);
1669                 updateProfilerGraphs(&graph);
1670
1671                 // Update if minimap has been disabled by the server
1672                 flags.show_minimap &= client->shouldShowMinimap();
1673         }
1674 }
1675
1676
1677 void Game::shutdown()
1678 {
1679 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8
1680         if (g_settings->get("3d_mode") == "pageflip") {
1681                 driver->setRenderTarget(irr::video::ERT_STEREO_BOTH_BUFFERS);
1682         }
1683 #endif
1684
1685         showOverlayMessage(wgettext("Shutting down..."), 0, 0, false);
1686
1687         if (clouds)
1688                 clouds->drop();
1689
1690         if (gui_chat_console)
1691                 gui_chat_console->drop();
1692
1693         if (sky)
1694                 sky->drop();
1695
1696         /* cleanup menus */
1697         while (g_menumgr.menuCount() > 0) {
1698                 g_menumgr.m_stack.front()->setVisible(false);
1699                 g_menumgr.deletingMenu(g_menumgr.m_stack.front());
1700         }
1701
1702         if (current_formspec) {
1703                 current_formspec->drop();
1704                 current_formspec = NULL;
1705         }
1706
1707         chat_backend->addMessage(L"", L"# Disconnected.");
1708         chat_backend->addMessage(L"", L"");
1709
1710         if (client) {
1711                 client->Stop();
1712                 while (!client->isShutdown()) {
1713                         assert(texture_src != NULL);
1714                         assert(shader_src != NULL);
1715                         texture_src->processQueue();
1716                         shader_src->processQueue();
1717                         sleep_ms(100);
1718                 }
1719         }
1720 }
1721
1722
1723 /****************************************************************************/
1724 /****************************************************************************
1725  Startup
1726  ****************************************************************************/
1727 /****************************************************************************/
1728
1729 bool Game::init(
1730                 const std::string &map_dir,
1731                 std::string *address,
1732                 u16 port,
1733                 const SubgameSpec &gamespec)
1734 {
1735         texture_src = createTextureSource(device);
1736
1737         showOverlayMessage(wgettext("Loading..."), 0, 0);
1738
1739         shader_src = createShaderSource(device);
1740
1741         itemdef_manager = createItemDefManager();
1742         nodedef_manager = createNodeDefManager();
1743
1744         eventmgr = new EventManager();
1745         quicktune = new QuicktuneShortcutter();
1746
1747         if (!(texture_src && shader_src && itemdef_manager && nodedef_manager
1748                         && eventmgr && quicktune))
1749                 return false;
1750
1751         if (!initSound())
1752                 return false;
1753
1754         // Create a server if not connecting to an existing one
1755         if (*address == "") {
1756                 if (!createSingleplayerServer(map_dir, gamespec, port, address))
1757                         return false;
1758         }
1759
1760         return true;
1761 }
1762
1763 bool Game::initSound()
1764 {
1765 #if USE_SOUND
1766         if (g_settings->getBool("enable_sound")) {
1767                 infostream << "Attempting to use OpenAL audio" << std::endl;
1768                 sound = createOpenALSoundManager(&soundfetcher);
1769                 if (!sound)
1770                         infostream << "Failed to initialize OpenAL audio" << std::endl;
1771         } else
1772                 infostream << "Sound disabled." << std::endl;
1773 #endif
1774
1775         if (!sound) {
1776                 infostream << "Using dummy audio." << std::endl;
1777                 sound = &dummySoundManager;
1778                 sound_is_dummy = true;
1779         }
1780
1781         soundmaker = new SoundMaker(sound, nodedef_manager);
1782         if (!soundmaker)
1783                 return false;
1784
1785         soundmaker->registerReceiver(eventmgr);
1786
1787         return true;
1788 }
1789
1790 bool Game::createSingleplayerServer(const std::string &map_dir,
1791                 const SubgameSpec &gamespec, u16 port, std::string *address)
1792 {
1793         showOverlayMessage(wgettext("Creating server..."), 0, 5);
1794
1795         std::string bind_str = g_settings->get("bind_address");
1796         Address bind_addr(0, 0, 0, 0, port);
1797
1798         if (g_settings->getBool("ipv6_server")) {
1799                 bind_addr.setAddress((IPv6AddressBytes *) NULL);
1800         }
1801
1802         try {
1803                 bind_addr.Resolve(bind_str.c_str());
1804         } catch (ResolveError &e) {
1805                 infostream << "Resolving bind address \"" << bind_str
1806                            << "\" failed: " << e.what()
1807                            << " -- Listening on all addresses." << std::endl;
1808         }
1809
1810         if (bind_addr.isIPv6() && !g_settings->getBool("enable_ipv6")) {
1811                 *error_message = "Unable to listen on " +
1812                                 bind_addr.serializeString() +
1813                                 " because IPv6 is disabled";
1814                 errorstream << *error_message << std::endl;
1815                 return false;
1816         }
1817
1818         server = new Server(map_dir, gamespec, simple_singleplayer_mode,
1819                             bind_addr.isIPv6(), false);
1820
1821         server->start(bind_addr);
1822
1823         return true;
1824 }
1825
1826 bool Game::createClient(const std::string &playername,
1827                 const std::string &password, std::string *address, u16 port)
1828 {
1829         showOverlayMessage(wgettext("Creating client..."), 0, 10);
1830
1831         draw_control = new MapDrawControl;
1832         if (!draw_control)
1833                 return false;
1834
1835         bool could_connect, connect_aborted;
1836
1837         if (!connectToServer(playername, password, address, port,
1838                         &could_connect, &connect_aborted))
1839                 return false;
1840
1841         if (!could_connect) {
1842                 if (error_message->empty() && !connect_aborted) {
1843                         // Should not happen if error messages are set properly
1844                         *error_message = "Connection failed for unknown reason";
1845                         errorstream << *error_message << std::endl;
1846                 }
1847                 return false;
1848         }
1849
1850         if (!getServerContent(&connect_aborted)) {
1851                 if (error_message->empty() && !connect_aborted) {
1852                         // Should not happen if error messages are set properly
1853                         *error_message = "Connection failed for unknown reason";
1854                         errorstream << *error_message << std::endl;
1855                 }
1856                 return false;
1857         }
1858
1859         GameGlobalShaderConstantSetterFactory *scsf = new GameGlobalShaderConstantSetterFactory(
1860                         &flags.force_fog_off, &runData.fog_range, client);
1861         shader_src->addShaderConstantSetterFactory(scsf);
1862
1863         // Update cached textures, meshes and materials
1864         client->afterContentReceived(device);
1865
1866         /* Camera
1867          */
1868         camera = new Camera(smgr, *draw_control, client);
1869         if (!camera || !camera->successfullyCreated(*error_message))
1870                 return false;
1871         client->setCamera(camera);
1872
1873         /* Clouds
1874          */
1875         if (m_cache_enable_clouds) {
1876                 clouds = new Clouds(smgr->getRootSceneNode(), smgr, -1, time(0));
1877                 if (!clouds) {
1878                         *error_message = "Memory allocation error (clouds)";
1879                         errorstream << *error_message << std::endl;
1880                         return false;
1881                 }
1882         }
1883
1884         /* Skybox
1885          */
1886         sky = new Sky(smgr->getRootSceneNode(), smgr, -1, texture_src);
1887         scsf->setSky(sky);
1888         skybox = NULL;  // This is used/set later on in the main run loop
1889
1890         local_inventory = new Inventory(itemdef_manager);
1891
1892         if (!(sky && local_inventory)) {
1893                 *error_message = "Memory allocation error (sky or local inventory)";
1894                 errorstream << *error_message << std::endl;
1895                 return false;
1896         }
1897
1898         /* Pre-calculated values
1899          */
1900         video::ITexture *t = texture_src->getTexture("crack_anylength.png");
1901         if (t) {
1902                 v2u32 size = t->getOriginalSize();
1903                 crack_animation_length = size.Y / size.X;
1904         } else {
1905                 crack_animation_length = 5;
1906         }
1907
1908         if (!initGui())
1909                 return false;
1910
1911         /* Set window caption
1912          */
1913         std::wstring str = utf8_to_wide(PROJECT_NAME_C);
1914         str += L" ";
1915         str += utf8_to_wide(g_version_hash);
1916         str += L" [";
1917         str += driver->getName();
1918         str += L"]";
1919         device->setWindowCaption(str.c_str());
1920
1921         LocalPlayer *player = client->getEnv().getLocalPlayer();
1922         player->hurt_tilt_timer = 0;
1923         player->hurt_tilt_strength = 0;
1924
1925         hud = new Hud(driver, smgr, guienv, client, player, local_inventory);
1926
1927         if (!hud) {
1928                 *error_message = "Memory error: could not create HUD";
1929                 errorstream << *error_message << std::endl;
1930                 return false;
1931         }
1932
1933         mapper = client->getMinimap();
1934         mapper->setMinimapMode(MINIMAP_MODE_OFF);
1935
1936         return true;
1937 }
1938
1939 bool Game::initGui()
1940 {
1941         // First line of debug text
1942         guitext = addStaticText(guienv,
1943                         utf8_to_wide(PROJECT_NAME_C).c_str(),
1944                         core::rect<s32>(0, 0, 0, 0),
1945                         false, false, guiroot);
1946
1947         // Second line of debug text
1948         guitext2 = addStaticText(guienv,
1949                         L"",
1950                         core::rect<s32>(0, 0, 0, 0),
1951                         false, false, guiroot);
1952
1953         // At the middle of the screen
1954         // Object infos are shown in this
1955         guitext_info = addStaticText(guienv,
1956                         L"",
1957                         core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5) + v2s32(100, 200),
1958                         false, true, guiroot);
1959
1960         // Status text (displays info when showing and hiding GUI stuff, etc.)
1961         guitext_status = addStaticText(guienv,
1962                         L"<Status>",
1963                         core::rect<s32>(0, 0, 0, 0),
1964                         false, false, guiroot);
1965         guitext_status->setVisible(false);
1966
1967         // Chat text
1968         guitext_chat = addStaticText(
1969                         guienv,
1970                         L"",
1971                         core::rect<s32>(0, 0, 0, 0),
1972                         //false, false); // Disable word wrap as of now
1973                         false, true, guiroot);
1974
1975         // Remove stale "recent" chat messages from previous connections
1976         chat_backend->clearRecentChat();
1977
1978         // Chat backend and console
1979         gui_chat_console = new GUIChatConsole(guienv, guienv->getRootGUIElement(),
1980                         -1, chat_backend, client, &g_menumgr);
1981         if (!gui_chat_console) {
1982                 *error_message = "Could not allocate memory for chat console";
1983                 errorstream << *error_message << std::endl;
1984                 return false;
1985         }
1986
1987         // Profiler text (size is updated when text is updated)
1988         guitext_profiler = addStaticText(guienv,
1989                         L"<Profiler>",
1990                         core::rect<s32>(0, 0, 0, 0),
1991                         false, false, guiroot);
1992         guitext_profiler->setBackgroundColor(video::SColor(120, 0, 0, 0));
1993         guitext_profiler->setVisible(false);
1994         guitext_profiler->setWordWrap(true);
1995
1996 #ifdef HAVE_TOUCHSCREENGUI
1997
1998         if (g_touchscreengui)
1999                 g_touchscreengui->init(texture_src);
2000
2001 #endif
2002
2003         return true;
2004 }
2005
2006 bool Game::connectToServer(const std::string &playername,
2007                 const std::string &password, std::string *address, u16 port,
2008                 bool *connect_ok, bool *aborted)
2009 {
2010         *connect_ok = false;    // Let's not be overly optimistic
2011         *aborted = false;
2012         bool local_server_mode = false;
2013
2014         showOverlayMessage(wgettext("Resolving address..."), 0, 15);
2015
2016         Address connect_address(0, 0, 0, 0, port);
2017
2018         try {
2019                 connect_address.Resolve(address->c_str());
2020
2021                 if (connect_address.isZero()) { // i.e. INADDR_ANY, IN6ADDR_ANY
2022                         //connect_address.Resolve("localhost");
2023                         if (connect_address.isIPv6()) {
2024                                 IPv6AddressBytes addr_bytes;
2025                                 addr_bytes.bytes[15] = 1;
2026                                 connect_address.setAddress(&addr_bytes);
2027                         } else {
2028                                 connect_address.setAddress(127, 0, 0, 1);
2029                         }
2030                         local_server_mode = true;
2031                 }
2032         } catch (ResolveError &e) {
2033                 *error_message = std::string("Couldn't resolve address: ") + e.what();
2034                 errorstream << *error_message << std::endl;
2035                 return false;
2036         }
2037
2038         if (connect_address.isIPv6() && !g_settings->getBool("enable_ipv6")) {
2039                 *error_message = "Unable to connect to " +
2040                                 connect_address.serializeString() +
2041                                 " because IPv6 is disabled";
2042                 errorstream << *error_message << std::endl;
2043                 return false;
2044         }
2045
2046         client = new Client(device,
2047                         playername.c_str(), password,
2048                         *draw_control, texture_src, shader_src,
2049                         itemdef_manager, nodedef_manager, sound, eventmgr,
2050                         connect_address.isIPv6(), &flags);
2051
2052         if (!client)
2053                 return false;
2054
2055         infostream << "Connecting to server at ";
2056         connect_address.print(&infostream);
2057         infostream << std::endl;
2058
2059         client->connect(connect_address, *address,
2060                 simple_singleplayer_mode || local_server_mode);
2061
2062         /*
2063                 Wait for server to accept connection
2064         */
2065
2066         try {
2067                 input->clear();
2068
2069                 FpsControl fps_control = { 0 };
2070                 f32 dtime;
2071                 f32 wait_time = 0; // in seconds
2072
2073                 fps_control.last_time = device->getTimer()->getTime();
2074
2075                 client->initMods();
2076
2077                 while (device->run()) {
2078
2079                         limitFps(&fps_control, &dtime);
2080
2081                         // Update client and server
2082                         client->step(dtime);
2083
2084                         if (server != NULL)
2085                                 server->step(dtime);
2086
2087                         // End condition
2088                         if (client->getState() == LC_Init) {
2089                                 *connect_ok = true;
2090                                 break;
2091                         }
2092
2093                         // Break conditions
2094                         if (client->accessDenied()) {
2095                                 *error_message = "Access denied. Reason: "
2096                                                 + client->accessDeniedReason();
2097                                 *reconnect_requested = client->reconnectRequested();
2098                                 errorstream << *error_message << std::endl;
2099                                 break;
2100                         }
2101
2102                         if (wasKeyDown(KeyType::ESC) || input->wasKeyDown(CancelKey)) {
2103                                 *aborted = true;
2104                                 infostream << "Connect aborted [Escape]" << std::endl;
2105                                 break;
2106                         }
2107
2108                         wait_time += dtime;
2109                         // Only time out if we aren't waiting for the server we started
2110                         if ((*address != "") && (wait_time > 10)) {
2111                                 bool sent_old_init = g_settings->getFlag("send_pre_v25_init");
2112                                 // If no pre v25 init was sent, and no answer was received,
2113                                 // but the low level connection could be established
2114                                 // (meaning that we have a peer id), then we probably wanted
2115                                 // to connect to a legacy server. In this case, tell the user
2116                                 // to enable the option to be able to connect.
2117                                 if (!sent_old_init &&
2118                                                 (client->getProtoVersion() == 0) &&
2119                                                 client->connectedToServer()) {
2120                                         *error_message = "Connection failure: init packet not "
2121                                         "recognized by server.\n"
2122                                         "Most likely the server uses an old protocol version (<v25).\n"
2123                                         "Please ask the server owner to update to 0.4.13 or later.\n"
2124                                         "To still connect to the server in the meantime,\n"
2125                                         "you can enable the 'send_pre_v25_init' setting by editing minetest.conf,\n"
2126                                         "or by enabling the 'Client -> Network -> Support older Servers'\n"
2127                                         "entry in the advanced settings menu.";
2128                                 } else {
2129                                         *error_message = "Connection timed out.";
2130                                 }
2131                                 errorstream << *error_message << std::endl;
2132                                 break;
2133                         }
2134
2135                         // Update status
2136                         showOverlayMessage(wgettext("Connecting to server..."), dtime, 20);
2137                 }
2138         } catch (con::PeerNotFoundException &e) {
2139                 // TODO: Should something be done here? At least an info/error
2140                 // message?
2141                 return false;
2142         }
2143
2144         return true;
2145 }
2146
2147 bool Game::getServerContent(bool *aborted)
2148 {
2149         input->clear();
2150
2151         FpsControl fps_control = { 0 };
2152         f32 dtime; // in seconds
2153
2154         fps_control.last_time = device->getTimer()->getTime();
2155
2156         while (device->run()) {
2157
2158                 limitFps(&fps_control, &dtime);
2159
2160                 // Update client and server
2161                 client->step(dtime);
2162
2163                 if (server != NULL)
2164                         server->step(dtime);
2165
2166                 // End condition
2167                 if (client->mediaReceived() && client->itemdefReceived() &&
2168                                 client->nodedefReceived()) {
2169                         break;
2170                 }
2171
2172                 // Error conditions
2173                 if (!checkConnection())
2174                         return false;
2175
2176                 if (client->getState() < LC_Init) {
2177                         *error_message = "Client disconnected";
2178                         errorstream << *error_message << std::endl;
2179                         return false;
2180                 }
2181
2182                 if (wasKeyDown(KeyType::ESC) || input->wasKeyDown(CancelKey)) {
2183                         *aborted = true;
2184                         infostream << "Connect aborted [Escape]" << std::endl;
2185                         return false;
2186                 }
2187
2188                 // Display status
2189                 int progress = 25;
2190
2191                 if (!client->itemdefReceived()) {
2192                         const wchar_t *text = wgettext("Item definitions...");
2193                         progress = 25;
2194                         draw_load_screen(text, device, guienv, texture_src,
2195                                 dtime, progress);
2196                         delete[] text;
2197                 } else if (!client->nodedefReceived()) {
2198                         const wchar_t *text = wgettext("Node definitions...");
2199                         progress = 30;
2200                         draw_load_screen(text, device, guienv, texture_src,
2201                                 dtime, progress);
2202                         delete[] text;
2203                 } else {
2204                         std::stringstream message;
2205                         std::fixed(message);
2206                         message.precision(0);
2207                         message << gettext("Media...") << " " << (client->mediaReceiveProgress()*100) << "%";
2208                         message.precision(2);
2209
2210                         if ((USE_CURL == 0) ||
2211                                         (!g_settings->getBool("enable_remote_media_server"))) {
2212                                 float cur = client->getCurRate();
2213                                 std::string cur_unit = gettext("KiB/s");
2214
2215                                 if (cur > 900) {
2216                                         cur /= 1024.0;
2217                                         cur_unit = gettext("MiB/s");
2218                                 }
2219
2220                                 message << " (" << cur << ' ' << cur_unit << ")";
2221                         }
2222
2223                         progress = 30 + client->mediaReceiveProgress() * 35 + 0.5;
2224                         draw_load_screen(utf8_to_wide(message.str()), device,
2225                                         guienv, texture_src, dtime, progress);
2226                 }
2227         }
2228
2229         return true;
2230 }
2231
2232
2233 /****************************************************************************/
2234 /****************************************************************************
2235  Run
2236  ****************************************************************************/
2237 /****************************************************************************/
2238
2239 inline void Game::updateInteractTimers(f32 dtime)
2240 {
2241         if (runData.nodig_delay_timer >= 0)
2242                 runData.nodig_delay_timer -= dtime;
2243
2244         if (runData.object_hit_delay_timer >= 0)
2245                 runData.object_hit_delay_timer -= dtime;
2246
2247         runData.time_from_last_punch += dtime;
2248 }
2249
2250
2251 /* returns false if game should exit, otherwise true
2252  */
2253 inline bool Game::checkConnection()
2254 {
2255         if (client->accessDenied()) {
2256                 *error_message = "Access denied. Reason: "
2257                                 + client->accessDeniedReason();
2258                 *reconnect_requested = client->reconnectRequested();
2259                 errorstream << *error_message << std::endl;
2260                 return false;
2261         }
2262
2263         return true;
2264 }
2265
2266
2267 /* returns false if game should exit, otherwise true
2268  */
2269 inline bool Game::handleCallbacks()
2270 {
2271         if (g_gamecallback->disconnect_requested) {
2272                 g_gamecallback->disconnect_requested = false;
2273                 return false;
2274         }
2275
2276         if (g_gamecallback->changepassword_requested) {
2277                 (new GUIPasswordChange(guienv, guiroot, -1,
2278                                        &g_menumgr, client))->drop();
2279                 g_gamecallback->changepassword_requested = false;
2280         }
2281
2282         if (g_gamecallback->changevolume_requested) {
2283                 (new GUIVolumeChange(guienv, guiroot, -1,
2284                                      &g_menumgr))->drop();
2285                 g_gamecallback->changevolume_requested = false;
2286         }
2287
2288         if (g_gamecallback->keyconfig_requested) {
2289                 (new GUIKeyChangeMenu(guienv, guiroot, -1,
2290                                       &g_menumgr))->drop();
2291                 g_gamecallback->keyconfig_requested = false;
2292         }
2293
2294         if (g_gamecallback->keyconfig_changed) {
2295                 keycache.populate(); // update the cache with new settings
2296                 g_gamecallback->keyconfig_changed = false;
2297         }
2298
2299         return true;
2300 }
2301
2302
2303 void Game::processQueues()
2304 {
2305         texture_src->processQueue();
2306         itemdef_manager->processQueue(client);
2307         shader_src->processQueue();
2308 }
2309
2310
2311 void Game::updateProfilers(const RunStats &stats, const FpsControl &draw_times, f32 dtime)
2312 {
2313         float profiler_print_interval =
2314                         g_settings->getFloat("profiler_print_interval");
2315         bool print_to_log = true;
2316
2317         if (profiler_print_interval == 0) {
2318                 print_to_log = false;
2319                 profiler_print_interval = 5;
2320         }
2321
2322         if (profiler_interval.step(dtime, profiler_print_interval)) {
2323                 if (print_to_log) {
2324                         infostream << "Profiler:" << std::endl;
2325                         g_profiler->print(infostream);
2326                 }
2327
2328                 update_profiler_gui(guitext_profiler, g_fontengine,
2329                                 runData.profiler_current_page, runData.profiler_max_page,
2330                                 driver->getScreenSize().Height);
2331
2332                 g_profiler->clear();
2333         }
2334
2335         addProfilerGraphs(stats, draw_times, dtime);
2336 }
2337
2338
2339 void Game::addProfilerGraphs(const RunStats &stats,
2340                 const FpsControl &draw_times, f32 dtime)
2341 {
2342         g_profiler->graphAdd("mainloop_other",
2343                         draw_times.busy_time / 1000.0f - stats.drawtime / 1000.0f);
2344
2345         if (draw_times.sleep_time != 0)
2346                 g_profiler->graphAdd("mainloop_sleep", draw_times.sleep_time / 1000.0f);
2347         g_profiler->graphAdd("mainloop_dtime", dtime);
2348
2349         g_profiler->add("Elapsed time", dtime);
2350         g_profiler->avg("FPS", 1. / dtime);
2351 }
2352
2353
2354 void Game::updateStats(RunStats *stats, const FpsControl &draw_times,
2355                 f32 dtime)
2356 {
2357
2358         f32 jitter;
2359         Jitter *jp;
2360
2361         /* Time average and jitter calculation
2362          */
2363         jp = &stats->dtime_jitter;
2364         jp->avg = jp->avg * 0.96 + dtime * 0.04;
2365
2366         jitter = dtime - jp->avg;
2367
2368         if (jitter > jp->max)
2369                 jp->max = jitter;
2370
2371         jp->counter += dtime;
2372
2373         if (jp->counter > 0.0) {
2374                 jp->counter -= 3.0;
2375                 jp->max_sample = jp->max;
2376                 jp->max_fraction = jp->max_sample / (jp->avg + 0.001);
2377                 jp->max = 0.0;
2378         }
2379
2380         /* Busytime average and jitter calculation
2381          */
2382         jp = &stats->busy_time_jitter;
2383         jp->avg = jp->avg + draw_times.busy_time * 0.02;
2384
2385         jitter = draw_times.busy_time - jp->avg;
2386
2387         if (jitter > jp->max)
2388                 jp->max = jitter;
2389         if (jitter < jp->min)
2390                 jp->min = jitter;
2391
2392         jp->counter += dtime;
2393
2394         if (jp->counter > 0.0) {
2395                 jp->counter -= 3.0;
2396                 jp->max_sample = jp->max;
2397                 jp->min_sample = jp->min;
2398                 jp->max = 0.0;
2399                 jp->min = 0.0;
2400         }
2401 }
2402
2403
2404
2405 /****************************************************************************
2406  Input handling
2407  ****************************************************************************/
2408
2409 void Game::processUserInput(f32 dtime)
2410 {
2411         // Reset input if window not active or some menu is active
2412         if (!device->isWindowActive() || !noMenuActive() || guienv->hasFocus(gui_chat_console)) {
2413                 input->clear();
2414 #ifdef HAVE_TOUCHSCREENGUI
2415                 g_touchscreengui->hide();
2416 #endif
2417         }
2418 #ifdef HAVE_TOUCHSCREENGUI
2419         else if (g_touchscreengui) {
2420                 /* on touchscreengui step may generate own input events which ain't
2421                  * what we want in case we just did clear them */
2422                 g_touchscreengui->step(dtime);
2423         }
2424 #endif
2425
2426         if (!guienv->hasFocus(gui_chat_console) && gui_chat_console->isOpen()) {
2427                 gui_chat_console->closeConsoleAtOnce();
2428         }
2429
2430         // Input handler step() (used by the random input generator)
2431         input->step(dtime);
2432
2433 #ifdef __ANDROID__
2434         if (current_formspec != NULL)
2435                 current_formspec->getAndroidUIInput();
2436         else
2437                 handleAndroidChatInput();
2438 #endif
2439
2440         // Increase timer for double tap of "keymap_jump"
2441         if (m_cache_doubletap_jump && runData.jump_timer <= 0.2f)
2442                 runData.jump_timer += dtime;
2443
2444         processKeyInput();
2445         processItemSelection(&runData.new_playeritem);
2446 }
2447
2448
2449 void Game::processKeyInput()
2450 {
2451         if (wasKeyDown(KeyType::DROP)) {
2452                 dropSelectedItem();
2453         } else if (wasKeyDown(KeyType::AUTORUN)) {
2454                 toggleAutorun();
2455         } else if (wasKeyDown(KeyType::INVENTORY)) {
2456                 openInventory();
2457         } else if (wasKeyDown(KeyType::ESC) || input->wasKeyDown(CancelKey)) {
2458                 if (!gui_chat_console->isOpenInhibited()) {
2459                         showPauseMenu();
2460                 }
2461         } else if (wasKeyDown(KeyType::CHAT)) {
2462                 openConsole(0.2, L"");
2463         } else if (wasKeyDown(KeyType::CMD)) {
2464                 openConsole(0.2, L"/");
2465         } else if (wasKeyDown(KeyType::CMD_LOCAL)) {
2466                 openConsole(0.2, L".");
2467         } else if (wasKeyDown(KeyType::CONSOLE)) {
2468                 openConsole(core::clamp(g_settings->getFloat("console_height"), 0.1f, 1.0f));
2469         } else if (wasKeyDown(KeyType::FREEMOVE)) {
2470                 toggleFreeMove();
2471         } else if (wasKeyDown(KeyType::JUMP)) {
2472                 toggleFreeMoveAlt();
2473         } else if (wasKeyDown(KeyType::FASTMOVE)) {
2474                 toggleFast();
2475         } else if (wasKeyDown(KeyType::NOCLIP)) {
2476                 toggleNoClip();
2477         } else if (wasKeyDown(KeyType::CINEMATIC)) {
2478                 toggleCinematic();
2479         } else if (wasKeyDown(KeyType::SCREENSHOT)) {
2480                 client->makeScreenshot(device);
2481         } else if (wasKeyDown(KeyType::TOGGLE_HUD)) {
2482                 toggleHud();
2483         } else if (wasKeyDown(KeyType::MINIMAP)) {
2484                 toggleMinimap(isKeyDown(KeyType::SNEAK));
2485         } else if (wasKeyDown(KeyType::TOGGLE_CHAT)) {
2486                 toggleChat();
2487         } else if (wasKeyDown(KeyType::TOGGLE_FORCE_FOG_OFF)) {
2488                 toggleFog();
2489         } else if (wasKeyDown(KeyType::TOGGLE_UPDATE_CAMERA)) {
2490                 toggleUpdateCamera();
2491         } else if (wasKeyDown(KeyType::TOGGLE_DEBUG)) {
2492                 toggleDebug();
2493         } else if (wasKeyDown(KeyType::TOGGLE_PROFILER)) {
2494                 toggleProfiler();
2495         } else if (wasKeyDown(KeyType::INCREASE_VIEWING_RANGE)) {
2496                 increaseViewRange();
2497         } else if (wasKeyDown(KeyType::DECREASE_VIEWING_RANGE)) {
2498                 decreaseViewRange();
2499         } else if (wasKeyDown(KeyType::RANGESELECT)) {
2500                 toggleFullViewRange();
2501         } else if (wasKeyDown(KeyType::QUICKTUNE_NEXT)) {
2502                 quicktune->next();
2503         } else if (wasKeyDown(KeyType::QUICKTUNE_PREV)) {
2504                 quicktune->prev();
2505         } else if (wasKeyDown(KeyType::QUICKTUNE_INC)) {
2506                 quicktune->inc();
2507         } else if (wasKeyDown(KeyType::QUICKTUNE_DEC)) {
2508                 quicktune->dec();
2509         } else if (wasKeyDown(KeyType::DEBUG_STACKS)) {
2510                 // Print debug stacks
2511                 dstream << "-----------------------------------------"
2512                         << std::endl;
2513                 dstream << "Printing debug stacks:" << std::endl;
2514                 dstream << "-----------------------------------------"
2515                         << std::endl;
2516                 debug_stacks_print();
2517         }
2518
2519         if (!isKeyDown(KeyType::JUMP) && runData.reset_jump_timer) {
2520                 runData.reset_jump_timer = false;
2521                 runData.jump_timer = 0.0f;
2522         }
2523
2524         if (quicktune->hasMessage()) {
2525                 m_statustext = utf8_to_wide(quicktune->getMessage());
2526                 runData.statustext_time = 0.0f;
2527         }
2528 }
2529
2530 void Game::processItemSelection(u16 *new_playeritem)
2531 {
2532         LocalPlayer *player = client->getEnv().getLocalPlayer();
2533
2534         /* Item selection using mouse wheel
2535          */
2536         *new_playeritem = client->getPlayerItem();
2537
2538         s32 wheel = input->getMouseWheel();
2539         u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE - 1,
2540                     player->hud_hotbar_itemcount - 1);
2541
2542         s32 dir = wheel;
2543
2544         if (input->joystick.wasKeyDown(KeyType::SCROLL_DOWN)) {
2545                 dir = -1;
2546         }
2547
2548         if (input->joystick.wasKeyDown(KeyType::SCROLL_UP)) {
2549                 dir = 1;
2550         }
2551
2552         if (dir < 0)
2553                 *new_playeritem = *new_playeritem < max_item ? *new_playeritem + 1 : 0;
2554         else if (dir > 0)
2555                 *new_playeritem = *new_playeritem > 0 ? *new_playeritem - 1 : max_item;
2556         // else dir == 0
2557
2558         /* Item selection using keyboard
2559          */
2560         for (u16 i = 0; i < 10; i++) {
2561                 static const KeyPress *item_keys[10] = {
2562                         NumberKey + 1, NumberKey + 2, NumberKey + 3, NumberKey + 4,
2563                         NumberKey + 5, NumberKey + 6, NumberKey + 7, NumberKey + 8,
2564                         NumberKey + 9, NumberKey + 0,
2565                 };
2566
2567                 if (input->wasKeyDown(*item_keys[i])) {
2568                         if (i < PLAYER_INVENTORY_SIZE && i < player->hud_hotbar_itemcount) {
2569                                 *new_playeritem = i;
2570                                 infostream << "Selected item: " << new_playeritem << std::endl;
2571                         }
2572                         break;
2573                 }
2574         }
2575 }
2576
2577
2578 void Game::dropSelectedItem()
2579 {
2580         IDropAction *a = new IDropAction();
2581         a->count = 0;
2582         a->from_inv.setCurrentPlayer();
2583         a->from_list = "main";
2584         a->from_i = client->getPlayerItem();
2585         client->inventoryAction(a);
2586 }
2587
2588
2589 void Game::openInventory()
2590 {
2591         /*
2592          * Don't permit to open inventory is CAO or player doesn't exists.
2593          * This prevent showing an empty inventory at player load
2594          */
2595
2596         LocalPlayer *player = client->getEnv().getLocalPlayer();
2597         if (player == NULL || player->getCAO() == NULL)
2598                 return;
2599
2600         infostream << "the_game: " << "Launching inventory" << std::endl;
2601
2602         PlayerInventoryFormSource *fs_src = new PlayerInventoryFormSource(client);
2603         TextDest *txt_dst = new TextDestPlayerInventory(client);
2604
2605         create_formspec_menu(&current_formspec, client, device, &input->joystick, fs_src, txt_dst);
2606         cur_formname = "";
2607
2608         InventoryLocation inventoryloc;
2609         inventoryloc.setCurrentPlayer();
2610         current_formspec->setFormSpec(fs_src->getForm(), inventoryloc);
2611 }
2612
2613
2614 void Game::openConsole(float scale, const wchar_t *line)
2615 {
2616         assert(scale > 0.0f && scale <= 1.0f);
2617
2618 #ifdef __ANDROID__
2619         porting::showInputDialog(gettext("ok"), "", "", 2);
2620         m_android_chat_open = true;
2621 #else
2622         if (gui_chat_console->isOpenInhibited())
2623                 return;
2624         gui_chat_console->openConsole(scale);
2625         if (line) {
2626                 gui_chat_console->setCloseOnEnter(true);
2627                 gui_chat_console->replaceAndAddToHistory(line);
2628         }
2629 #endif
2630 }
2631
2632 #ifdef __ANDROID__
2633 void Game::handleAndroidChatInput()
2634 {
2635         if (m_android_chat_open && porting::getInputDialogState() == 0) {
2636                 std::string text = porting::getInputDialogValue();
2637                 client->typeChatMessage(utf8_to_wide(text));
2638         }
2639 }
2640 #endif
2641
2642
2643 void Game::toggleFreeMove()
2644 {
2645         static const wchar_t *msg[] = { L"free_move disabled", L"free_move enabled" };
2646
2647         bool free_move = !g_settings->getBool("free_move");
2648         g_settings->set("free_move", bool_to_cstr(free_move));
2649
2650         runData.statustext_time = 0;
2651         m_statustext = msg[free_move];
2652         if (free_move && !client->checkPrivilege("fly"))
2653                 m_statustext += L" (note: no 'fly' privilege)";
2654 }
2655
2656
2657 void Game::toggleFreeMoveAlt()
2658 {
2659         if (m_cache_doubletap_jump && runData.jump_timer < 0.2f)
2660                 toggleFreeMove();
2661
2662         runData.reset_jump_timer = true;
2663 }
2664
2665
2666 void Game::toggleFast()
2667 {
2668         static const wchar_t *msg[] = { L"fast_move disabled", L"fast_move enabled" };
2669         bool fast_move = !g_settings->getBool("fast_move");
2670         g_settings->set("fast_move", bool_to_cstr(fast_move));
2671
2672         runData.statustext_time = 0;
2673         m_statustext = msg[fast_move];
2674
2675         bool has_fast_privs = client->checkPrivilege("fast");
2676
2677         if (fast_move && !has_fast_privs)
2678                 m_statustext += L" (note: no 'fast' privilege)";
2679
2680 #ifdef __ANDROID__
2681         m_cache_hold_aux1 = fast_move && has_fast_privs;
2682 #endif
2683 }
2684
2685
2686 void Game::toggleNoClip()
2687 {
2688         static const wchar_t *msg[] = { L"noclip disabled", L"noclip enabled" };
2689         bool noclip = !g_settings->getBool("noclip");
2690         g_settings->set("noclip", bool_to_cstr(noclip));
2691
2692         runData.statustext_time = 0;
2693         m_statustext = msg[noclip];
2694
2695         if (noclip && !client->checkPrivilege("noclip"))
2696                 m_statustext += L" (note: no 'noclip' privilege)";
2697 }
2698
2699 void Game::toggleCinematic()
2700 {
2701         static const wchar_t *msg[] = { L"cinematic disabled", L"cinematic enabled" };
2702         bool cinematic = !g_settings->getBool("cinematic");
2703         g_settings->set("cinematic", bool_to_cstr(cinematic));
2704
2705         runData.statustext_time = 0;
2706         m_statustext = msg[cinematic];
2707 }
2708
2709 // Add WoW-style autorun by toggling continuous forward.
2710 void Game::toggleAutorun()
2711 {
2712         static const wchar_t *msg[] = { L"autorun disabled", L"autorun enabled" };
2713         bool autorun_enabled = !g_settings->getBool("continuous_forward");
2714         g_settings->set("continuous_forward", bool_to_cstr(autorun_enabled));
2715
2716         runData.statustext_time = 0;
2717         m_statustext = msg[autorun_enabled ? 1 : 0];
2718 }
2719
2720 void Game::toggleChat()
2721 {
2722         static const wchar_t *msg[] = { L"Chat hidden", L"Chat shown" };
2723
2724         flags.show_chat = !flags.show_chat;
2725         runData.statustext_time = 0;
2726         m_statustext = msg[flags.show_chat];
2727 }
2728
2729
2730 void Game::toggleHud()
2731 {
2732         static const wchar_t *msg[] = { L"HUD hidden", L"HUD shown" };
2733
2734         flags.show_hud = !flags.show_hud;
2735         runData.statustext_time = 0;
2736         m_statustext = msg[flags.show_hud];
2737 }
2738
2739 void Game::toggleMinimap(bool shift_pressed)
2740 {
2741         if (!flags.show_hud || !g_settings->getBool("enable_minimap"))
2742                 return;
2743
2744         if (shift_pressed) {
2745                 mapper->toggleMinimapShape();
2746                 return;
2747         }
2748
2749         u32 hud_flags = client->getEnv().getLocalPlayer()->hud_flags;
2750
2751         MinimapMode mode = MINIMAP_MODE_OFF;
2752         if (hud_flags & HUD_FLAG_MINIMAP_VISIBLE) {
2753                 mode = mapper->getMinimapMode();
2754                 mode = (MinimapMode)((int)mode + 1);
2755         }
2756
2757         flags.show_minimap = true;
2758         switch (mode) {
2759                 case MINIMAP_MODE_SURFACEx1:
2760                         m_statustext = L"Minimap in surface mode, Zoom x1";
2761                         break;
2762                 case MINIMAP_MODE_SURFACEx2:
2763                         m_statustext = L"Minimap in surface mode, Zoom x2";
2764                         break;
2765                 case MINIMAP_MODE_SURFACEx4:
2766                         m_statustext = L"Minimap in surface mode, Zoom x4";
2767                         break;
2768                 case MINIMAP_MODE_RADARx1:
2769                         m_statustext = L"Minimap in radar mode, Zoom x1";
2770                         break;
2771                 case MINIMAP_MODE_RADARx2:
2772                         m_statustext = L"Minimap in radar mode, Zoom x2";
2773                         break;
2774                 case MINIMAP_MODE_RADARx4:
2775                         m_statustext = L"Minimap in radar mode, Zoom x4";
2776                         break;
2777                 default:
2778                         mode = MINIMAP_MODE_OFF;
2779                         flags.show_minimap = false;
2780                         m_statustext = (hud_flags & HUD_FLAG_MINIMAP_VISIBLE) ?
2781                                 L"Minimap hidden" : L"Minimap disabled by server";
2782         }
2783
2784         runData.statustext_time = 0;
2785         mapper->setMinimapMode(mode);
2786 }
2787
2788 void Game::toggleFog()
2789 {
2790         static const wchar_t *msg[] = { L"Fog enabled", L"Fog disabled" };
2791
2792         flags.force_fog_off = !flags.force_fog_off;
2793         runData.statustext_time = 0;
2794         m_statustext = msg[flags.force_fog_off];
2795 }
2796
2797
2798 void Game::toggleDebug()
2799 {
2800         // Initial / 4x toggle: Chat only
2801         // 1x toggle: Debug text with chat
2802         // 2x toggle: Debug text with profiler graph
2803         // 3x toggle: Debug text and wireframe
2804         if (!flags.show_debug) {
2805                 flags.show_debug = true;
2806                 flags.show_profiler_graph = false;
2807                 draw_control->show_wireframe = false;
2808                 m_statustext = L"Debug info shown";
2809         } else if (!flags.show_profiler_graph && !draw_control->show_wireframe) {
2810                 flags.show_profiler_graph = true;
2811                 m_statustext = L"Profiler graph shown";
2812         } else if (!draw_control->show_wireframe && client->checkPrivilege("debug")) {
2813                 flags.show_profiler_graph = false;
2814                 draw_control->show_wireframe = true;
2815                 m_statustext = L"Wireframe shown";
2816         } else {
2817                 flags.show_debug = false;
2818                 flags.show_profiler_graph = false;
2819                 draw_control->show_wireframe = false;
2820                 if (client->checkPrivilege("debug")) {
2821                         m_statustext = L"Debug info, profiler graph, and wireframe hidden";
2822                 } else {
2823                         m_statustext = L"Debug info and profiler graph hidden";
2824                 }
2825         }
2826         runData.statustext_time = 0;
2827 }
2828
2829
2830 void Game::toggleUpdateCamera()
2831 {
2832         static const wchar_t *msg[] = {
2833                 L"Camera update enabled",
2834                 L"Camera update disabled"
2835         };
2836
2837         flags.disable_camera_update = !flags.disable_camera_update;
2838         runData.statustext_time = 0;
2839         m_statustext = msg[flags.disable_camera_update];
2840 }
2841
2842
2843 void Game::toggleProfiler()
2844 {
2845         runData.profiler_current_page =
2846                 (runData.profiler_current_page + 1) % (runData.profiler_max_page + 1);
2847
2848         // FIXME: This updates the profiler with incomplete values
2849         update_profiler_gui(guitext_profiler, g_fontengine, runData.profiler_current_page,
2850                 runData.profiler_max_page, driver->getScreenSize().Height);
2851
2852         if (runData.profiler_current_page != 0) {
2853                 std::wstringstream sstr;
2854                 sstr << "Profiler shown (page " << runData.profiler_current_page
2855                      << " of " << runData.profiler_max_page << ")";
2856                 m_statustext = sstr.str();
2857         } else {
2858                 m_statustext = L"Profiler hidden";
2859         }
2860         runData.statustext_time = 0;
2861 }
2862
2863
2864 void Game::increaseViewRange()
2865 {
2866         s16 range = g_settings->getS16("viewing_range");
2867         s16 range_new = range + 10;
2868
2869         if (range_new > 4000) {
2870                 range_new = 4000;
2871                 m_statustext = utf8_to_wide("Viewing range is at maximum: "
2872                                 + itos(range_new));
2873         } else {
2874                 m_statustext = utf8_to_wide("Viewing range changed to "
2875                                 + itos(range_new));
2876         }
2877         g_settings->set("viewing_range", itos(range_new));
2878         runData.statustext_time = 0;
2879 }
2880
2881
2882 void Game::decreaseViewRange()
2883 {
2884         s16 range = g_settings->getS16("viewing_range");
2885         s16 range_new = range - 10;
2886
2887         if (range_new < 20) {
2888                 range_new = 20;
2889                 m_statustext = utf8_to_wide("Viewing range is at minimum: "
2890                                 + itos(range_new));
2891         } else {
2892                 m_statustext = utf8_to_wide("Viewing range changed to "
2893                                 + itos(range_new));
2894         }
2895         g_settings->set("viewing_range", itos(range_new));
2896         runData.statustext_time = 0;
2897 }
2898
2899
2900 void Game::toggleFullViewRange()
2901 {
2902         static const wchar_t *msg[] = {
2903                 L"Disabled full viewing range",
2904                 L"Enabled full viewing range"
2905         };
2906
2907         draw_control->range_all = !draw_control->range_all;
2908         infostream << msg[draw_control->range_all] << std::endl;
2909         m_statustext = msg[draw_control->range_all];
2910         runData.statustext_time = 0;
2911 }
2912
2913
2914 void Game::updateCameraDirection(CameraOrientation *cam, float dtime)
2915 {
2916         if ((device->isWindowActive() && noMenuActive()) || random_input) {
2917
2918 #ifndef __ANDROID__
2919                 if (!random_input) {
2920                         // Mac OSX gets upset if this is set every frame
2921                         if (device->getCursorControl()->isVisible())
2922                                 device->getCursorControl()->setVisible(false);
2923                 }
2924 #endif
2925
2926                 if (m_first_loop_after_window_activation)
2927                         m_first_loop_after_window_activation = false;
2928                 else
2929                         updateCameraOrientation(cam, dtime);
2930
2931                 input->setMousePos((driver->getScreenSize().Width / 2),
2932                                 (driver->getScreenSize().Height / 2));
2933         } else {
2934
2935 #ifndef ANDROID
2936                 // Mac OSX gets upset if this is set every frame
2937                 if (!device->getCursorControl()->isVisible())
2938                         device->getCursorControl()->setVisible(true);
2939 #endif
2940
2941                 if (!m_first_loop_after_window_activation)
2942                         m_first_loop_after_window_activation = true;
2943
2944         }
2945 }
2946
2947 void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
2948 {
2949 #ifdef HAVE_TOUCHSCREENGUI
2950         if (g_touchscreengui) {
2951                 cam->camera_yaw   += g_touchscreengui->getYawChange();
2952                 cam->camera_pitch  = g_touchscreengui->getPitch();
2953         } else {
2954 #endif
2955
2956                 s32 dx = input->getMousePos().X - (driver->getScreenSize().Width / 2);
2957                 s32 dy = input->getMousePos().Y - (driver->getScreenSize().Height / 2);
2958
2959                 if (m_invert_mouse || camera->getCameraMode() == CAMERA_MODE_THIRD_FRONT) {
2960                         dy = -dy;
2961                 }
2962
2963                 cam->camera_yaw   -= dx * m_cache_mouse_sensitivity;
2964                 cam->camera_pitch += dy * m_cache_mouse_sensitivity;
2965
2966 #ifdef HAVE_TOUCHSCREENGUI
2967         }
2968 #endif
2969
2970         if (m_cache_enable_joysticks) {
2971                 f32 c = m_cache_joystick_frustum_sensitivity * (1.f / 32767.f) * dtime;
2972                 cam->camera_yaw -= input->joystick.getAxisWithoutDead(JA_FRUSTUM_HORIZONTAL) * c;
2973                 cam->camera_pitch += input->joystick.getAxisWithoutDead(JA_FRUSTUM_VERTICAL) * c;
2974         }
2975
2976         cam->camera_pitch = rangelim(cam->camera_pitch, -89.5, 89.5);
2977 }
2978
2979
2980 void Game::updatePlayerControl(const CameraOrientation &cam)
2981 {
2982         //TimeTaker tt("update player control", NULL, PRECISION_NANO);
2983
2984         // DO NOT use the isKeyDown method for the forward, backward, left, right
2985         // buttons, as the code that uses the controls needs to be able to
2986         // distinguish between the two in order to know when to use joysticks.
2987
2988         PlayerControl control(
2989                 input->isKeyDown(keycache.key[KeyType::FORWARD]),
2990                 input->isKeyDown(keycache.key[KeyType::BACKWARD]),
2991                 input->isKeyDown(keycache.key[KeyType::LEFT]),
2992                 input->isKeyDown(keycache.key[KeyType::RIGHT]),
2993                 isKeyDown(KeyType::JUMP),
2994                 isKeyDown(KeyType::SPECIAL1),
2995                 isKeyDown(KeyType::SNEAK),
2996                 isKeyDown(KeyType::ZOOM),
2997                 isLeftPressed(),
2998                 isRightPressed(),
2999                 cam.camera_pitch,
3000                 cam.camera_yaw,
3001                 input->joystick.getAxisWithoutDead(JA_SIDEWARD_MOVE),
3002                 input->joystick.getAxisWithoutDead(JA_FORWARD_MOVE)
3003         );
3004
3005         u32 keypress_bits =
3006                         ( (u32)(isKeyDown(KeyType::FORWARD)                       & 0x1) << 0) |
3007                         ( (u32)(isKeyDown(KeyType::BACKWARD)                      & 0x1) << 1) |
3008                         ( (u32)(isKeyDown(KeyType::LEFT)                          & 0x1) << 2) |
3009                         ( (u32)(isKeyDown(KeyType::RIGHT)                         & 0x1) << 3) |
3010                         ( (u32)(isKeyDown(KeyType::JUMP)                          & 0x1) << 4) |
3011                         ( (u32)(isKeyDown(KeyType::SPECIAL1)                      & 0x1) << 5) |
3012                         ( (u32)(isKeyDown(KeyType::SNEAK)                         & 0x1) << 6) |
3013                         ( (u32)(isLeftPressed()                                   & 0x1) << 7) |
3014                         ( (u32)(isRightPressed()                                  & 0x1) << 8
3015                 );
3016
3017 #ifdef ANDROID
3018         /* For Android, simulate holding down AUX1 (fast move) if the user has
3019          * the fast_move setting toggled on. If there is an aux1 key defined for
3020          * Android then its meaning is inverted (i.e. holding aux1 means walk and
3021          * not fast)
3022          */
3023         if (m_cache_hold_aux1) {
3024                 control.aux1 = control.aux1 ^ true;
3025                 keypress_bits ^= ((u32)(1U << 5));
3026         }
3027 #endif
3028
3029         client->setPlayerControl(control);
3030         LocalPlayer *player = client->getEnv().getLocalPlayer();
3031         player->keyPressed = keypress_bits;
3032
3033         //tt.stop();
3034 }
3035
3036
3037 inline void Game::step(f32 *dtime)
3038 {
3039         bool can_be_and_is_paused =
3040                         (simple_singleplayer_mode && g_menumgr.pausesGame());
3041
3042         if (can_be_and_is_paused) {     // This is for a singleplayer server
3043                 *dtime = 0;             // No time passes
3044         } else {
3045                 if (server != NULL) {
3046                         //TimeTaker timer("server->step(dtime)");
3047                         server->step(*dtime);
3048                 }
3049
3050                 //TimeTaker timer("client.step(dtime)");
3051                 client->step(*dtime);
3052         }
3053 }
3054
3055
3056 void Game::processClientEvents(CameraOrientation *cam)
3057 {
3058         ClientEvent event = client->getClientEvent();
3059
3060         LocalPlayer *player = client->getEnv().getLocalPlayer();
3061
3062         for ( ; event.type != CE_NONE; event = client->getClientEvent()) {
3063
3064                 switch (event.type) {
3065                 case CE_PLAYER_DAMAGE:
3066                         if (client->getHP() == 0)
3067                                 break;
3068                         if (client->moddingEnabled()) {
3069                                 client->getScript()->on_damage_taken(event.player_damage.amount);
3070                         }
3071
3072                         runData.damage_flash += 95.0 + 3.2 * event.player_damage.amount;
3073                         runData.damage_flash = MYMIN(runData.damage_flash, 127.0);
3074
3075                         player->hurt_tilt_timer = 1.5;
3076                         player->hurt_tilt_strength =
3077                                 rangelim(event.player_damage.amount / 4, 1.0, 4.0);
3078
3079                         client->event()->put(new SimpleTriggerEvent("PlayerDamage"));
3080                         break;
3081
3082                 case CE_PLAYER_FORCE_MOVE:
3083                         cam->camera_yaw = event.player_force_move.yaw;
3084                         cam->camera_pitch = event.player_force_move.pitch;
3085                         break;
3086
3087                 case CE_DEATHSCREEN:
3088                         // This should be enabled for death formspec in builtin
3089                         client->getScript()->on_death();
3090
3091                         /* Handle visualization */
3092                         runData.damage_flash = 0;
3093                         player->hurt_tilt_timer = 0;
3094                         player->hurt_tilt_strength = 0;
3095                         break;
3096
3097                 case CE_SHOW_FORMSPEC:
3098                         if (*(event.show_formspec.formspec) == "") {
3099                                 if (current_formspec && ( *(event.show_formspec.formname) == "" || *(event.show_formspec.formname) == cur_formname) ){
3100                                         current_formspec->quitMenu();
3101                                 }
3102                         } else {
3103                                 FormspecFormSource *fs_src =
3104                                         new FormspecFormSource(*(event.show_formspec.formspec));
3105                                 TextDestPlayerInventory *txt_dst =
3106                                         new TextDestPlayerInventory(client, *(event.show_formspec.formname));
3107
3108                                 create_formspec_menu(&current_formspec, client, device, &input->joystick,
3109                                         fs_src, txt_dst);
3110                                 cur_formname = *(event.show_formspec.formname);
3111                         }
3112
3113                         delete event.show_formspec.formspec;
3114                         delete event.show_formspec.formname;
3115                         break;
3116
3117                 case CE_SHOW_LOCAL_FORMSPEC:
3118                         {
3119                                 FormspecFormSource *fs_src = new FormspecFormSource(*event.show_formspec.formspec);
3120                                 LocalFormspecHandler *txt_dst = new LocalFormspecHandler(*event.show_formspec.formname, client);
3121                                 create_formspec_menu(&current_formspec, client, device, &input->joystick,
3122                                         fs_src, txt_dst);
3123                         }
3124                         delete event.show_formspec.formspec;
3125                         delete event.show_formspec.formname;
3126                         break;
3127
3128                 case CE_SPAWN_PARTICLE:
3129                 case CE_ADD_PARTICLESPAWNER:
3130                 case CE_DELETE_PARTICLESPAWNER:
3131                         client->getParticleManager()->handleParticleEvent(&event, client,
3132                                         smgr, player);
3133                         break;
3134
3135                 case CE_HUDADD:
3136                         {
3137                                 u32 id = event.hudadd.id;
3138
3139                                 HudElement *e = player->getHud(id);
3140
3141                                 if (e != NULL) {
3142                                         delete event.hudadd.pos;
3143                                         delete event.hudadd.name;
3144                                         delete event.hudadd.scale;
3145                                         delete event.hudadd.text;
3146                                         delete event.hudadd.align;
3147                                         delete event.hudadd.offset;
3148                                         delete event.hudadd.world_pos;
3149                                         delete event.hudadd.size;
3150                                         continue;
3151                                 }
3152
3153                                 e = new HudElement;
3154                                 e->type   = (HudElementType)event.hudadd.type;
3155                                 e->pos    = *event.hudadd.pos;
3156                                 e->name   = *event.hudadd.name;
3157                                 e->scale  = *event.hudadd.scale;
3158                                 e->text   = *event.hudadd.text;
3159                                 e->number = event.hudadd.number;
3160                                 e->item   = event.hudadd.item;
3161                                 e->dir    = event.hudadd.dir;
3162                                 e->align  = *event.hudadd.align;
3163                                 e->offset = *event.hudadd.offset;
3164                                 e->world_pos = *event.hudadd.world_pos;
3165                                 e->size = *event.hudadd.size;
3166
3167                                 u32 new_id = player->addHud(e);
3168                                 //if this isn't true our huds aren't consistent
3169                                 sanity_check(new_id == id);
3170                         }
3171
3172                         delete event.hudadd.pos;
3173                         delete event.hudadd.name;
3174                         delete event.hudadd.scale;
3175                         delete event.hudadd.text;
3176                         delete event.hudadd.align;
3177                         delete event.hudadd.offset;
3178                         delete event.hudadd.world_pos;
3179                         delete event.hudadd.size;
3180                         break;
3181
3182                 case CE_HUDRM:
3183                         {
3184                                 HudElement *e = player->removeHud(event.hudrm.id);
3185
3186                                 if (e != NULL)
3187                                         delete e;
3188                         }
3189                         break;
3190
3191                 case CE_HUDCHANGE:
3192                         {
3193                                 u32 id = event.hudchange.id;
3194                                 HudElement *e = player->getHud(id);
3195
3196                                 if (e == NULL) {
3197                                         delete event.hudchange.v3fdata;
3198                                         delete event.hudchange.v2fdata;
3199                                         delete event.hudchange.sdata;
3200                                         delete event.hudchange.v2s32data;
3201                                         continue;
3202                                 }
3203
3204                                 switch (event.hudchange.stat) {
3205                                 case HUD_STAT_POS:
3206                                         e->pos = *event.hudchange.v2fdata;
3207                                         break;
3208
3209                                 case HUD_STAT_NAME:
3210                                         e->name = *event.hudchange.sdata;
3211                                         break;
3212
3213                                 case HUD_STAT_SCALE:
3214                                         e->scale = *event.hudchange.v2fdata;
3215                                         break;
3216
3217                                 case HUD_STAT_TEXT:
3218                                         e->text = *event.hudchange.sdata;
3219                                         break;
3220
3221                                 case HUD_STAT_NUMBER:
3222                                         e->number = event.hudchange.data;
3223                                         break;
3224
3225                                 case HUD_STAT_ITEM:
3226                                         e->item = event.hudchange.data;
3227                                         break;
3228
3229                                 case HUD_STAT_DIR:
3230                                         e->dir = event.hudchange.data;
3231                                         break;
3232
3233                                 case HUD_STAT_ALIGN:
3234                                         e->align = *event.hudchange.v2fdata;
3235                                         break;
3236
3237                                 case HUD_STAT_OFFSET:
3238                                         e->offset = *event.hudchange.v2fdata;
3239                                         break;
3240
3241                                 case HUD_STAT_WORLD_POS:
3242                                         e->world_pos = *event.hudchange.v3fdata;
3243                                         break;
3244
3245                                 case HUD_STAT_SIZE:
3246                                         e->size = *event.hudchange.v2s32data;
3247                                         break;
3248                                 }
3249                         }
3250
3251                         delete event.hudchange.v3fdata;
3252                         delete event.hudchange.v2fdata;
3253                         delete event.hudchange.sdata;
3254                         delete event.hudchange.v2s32data;
3255                         break;
3256
3257                 case CE_SET_SKY:
3258                         sky->setVisible(false);
3259
3260                         if (skybox) {
3261                                 skybox->remove();
3262                                 skybox = NULL;
3263                         }
3264
3265                         // Handle according to type
3266                         if (*event.set_sky.type == "regular") {
3267                                 sky->setVisible(true);
3268                         } else if (*event.set_sky.type == "skybox" &&
3269                                         event.set_sky.params->size() == 6) {
3270                                 sky->setFallbackBgColor(*event.set_sky.bgcolor);
3271                                 skybox = smgr->addSkyBoxSceneNode(
3272                                                  texture_src->getTextureForMesh((*event.set_sky.params)[0]),
3273                                                  texture_src->getTextureForMesh((*event.set_sky.params)[1]),
3274                                                  texture_src->getTextureForMesh((*event.set_sky.params)[2]),
3275                                                  texture_src->getTextureForMesh((*event.set_sky.params)[3]),
3276                                                  texture_src->getTextureForMesh((*event.set_sky.params)[4]),
3277                                                  texture_src->getTextureForMesh((*event.set_sky.params)[5]));
3278                         }
3279                         // Handle everything else as plain color
3280                         else {
3281                                 if (*event.set_sky.type != "plain")
3282                                         infostream << "Unknown sky type: "
3283                                                    << (*event.set_sky.type) << std::endl;
3284
3285                                 sky->setFallbackBgColor(*event.set_sky.bgcolor);
3286                         }
3287
3288                         delete event.set_sky.bgcolor;
3289                         delete event.set_sky.type;
3290                         delete event.set_sky.params;
3291                         break;
3292
3293                 case CE_OVERRIDE_DAY_NIGHT_RATIO:
3294                         client->getEnv().setDayNightRatioOverride(
3295                                         event.override_day_night_ratio.do_override,
3296                                         event.override_day_night_ratio.ratio_f * 1000);
3297                         break;
3298
3299                 default:
3300                         // unknown or unhandled type
3301                         break;
3302
3303                 }
3304         }
3305 }
3306
3307
3308 void Game::updateCamera(u32 busy_time, f32 dtime)
3309 {
3310         LocalPlayer *player = client->getEnv().getLocalPlayer();
3311
3312         /*
3313                 For interaction purposes, get info about the held item
3314                 - What item is it?
3315                 - Is it a usable item?
3316                 - Can it point to liquids?
3317         */
3318         ItemStack playeritem;
3319         {
3320                 InventoryList *mlist = local_inventory->getList("main");
3321
3322                 if (mlist && client->getPlayerItem() < mlist->getSize())
3323                         playeritem = mlist->getItem(client->getPlayerItem());
3324         }
3325
3326         if (playeritem.getDefinition(itemdef_manager).name.empty()) { // override the hand
3327                 InventoryList *hlist = local_inventory->getList("hand");
3328                 if (hlist)
3329                         playeritem = hlist->getItem(0);
3330         }
3331
3332
3333         ToolCapabilities playeritem_toolcap =
3334                 playeritem.getToolCapabilities(itemdef_manager);
3335
3336         v3s16 old_camera_offset = camera->getOffset();
3337
3338         if (wasKeyDown(KeyType::CAMERA_MODE)) {
3339                 GenericCAO *playercao = player->getCAO();
3340
3341                 // If playercao not loaded, don't change camera
3342                 if (playercao == NULL)
3343                         return;
3344
3345                 camera->toggleCameraMode();
3346
3347                 playercao->setVisible(camera->getCameraMode() > CAMERA_MODE_FIRST);
3348                 playercao->setChildrenVisible(camera->getCameraMode() > CAMERA_MODE_FIRST);
3349         }
3350
3351         float full_punch_interval = playeritem_toolcap.full_punch_interval;
3352         float tool_reload_ratio = runData.time_from_last_punch / full_punch_interval;
3353
3354         tool_reload_ratio = MYMIN(tool_reload_ratio, 1.0);
3355         camera->update(player, dtime, busy_time / 1000.0f, tool_reload_ratio,
3356                       client->getEnv());
3357         camera->step(dtime);
3358
3359         v3f camera_position = camera->getPosition();
3360         v3f camera_direction = camera->getDirection();
3361         f32 camera_fov = camera->getFovMax();
3362         v3s16 camera_offset = camera->getOffset();
3363
3364         m_camera_offset_changed = (camera_offset != old_camera_offset);
3365
3366         if (!flags.disable_camera_update) {
3367                 client->getEnv().getClientMap().updateCamera(camera_position,
3368                                 camera_direction, camera_fov, camera_offset);
3369
3370                 if (m_camera_offset_changed) {
3371                         client->updateCameraOffset(camera_offset);
3372                         client->getEnv().updateCameraOffset(camera_offset);
3373
3374                         if (clouds)
3375                                 clouds->updateCameraOffset(camera_offset);
3376                 }
3377         }
3378 }
3379
3380
3381 void Game::updateSound(f32 dtime)
3382 {
3383         // Update sound listener
3384         v3s16 camera_offset = camera->getOffset();
3385         sound->updateListener(camera->getCameraNode()->getPosition() + intToFloat(camera_offset, BS),
3386                               v3f(0, 0, 0), // velocity
3387                               camera->getDirection(),
3388                               camera->getCameraNode()->getUpVector());
3389         sound->setListenerGain(g_settings->getFloat("sound_volume"));
3390
3391
3392         //      Update sound maker
3393         soundmaker->step(dtime);
3394
3395         LocalPlayer *player = client->getEnv().getLocalPlayer();
3396
3397         ClientMap &map = client->getEnv().getClientMap();
3398         MapNode n = map.getNodeNoEx(player->getFootstepNodePos());
3399         soundmaker->m_player_step_sound = nodedef_manager->get(n).sound_footstep;
3400 }
3401
3402
3403 void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
3404 {
3405         LocalPlayer *player = client->getEnv().getLocalPlayer();
3406
3407         ItemStack playeritem;
3408         {
3409                 InventoryList *mlist = local_inventory->getList("main");
3410
3411                 if (mlist && client->getPlayerItem() < mlist->getSize())
3412                         playeritem = mlist->getItem(client->getPlayerItem());
3413         }
3414
3415         const ItemDefinition &playeritem_def =
3416                         playeritem.getDefinition(itemdef_manager);
3417         InventoryList *hlist = local_inventory->getList("hand");
3418         const ItemDefinition &hand_def =
3419                 hlist ? hlist->getItem(0).getDefinition(itemdef_manager) : itemdef_manager->get("");
3420
3421         v3f player_position  = player->getPosition();
3422         v3f camera_position  = camera->getPosition();
3423         v3f camera_direction = camera->getDirection();
3424         v3s16 camera_offset  = camera->getOffset();
3425
3426
3427         /*
3428                 Calculate what block is the crosshair pointing to
3429         */
3430
3431         f32 d = playeritem_def.range; // max. distance
3432         f32 d_hand = hand_def.range;
3433
3434         if (d < 0 && d_hand >= 0)
3435                 d = d_hand;
3436         else if (d < 0)
3437                 d = 4.0;
3438
3439         core::line3d<f32> shootline;
3440
3441         if (camera->getCameraMode() != CAMERA_MODE_THIRD_FRONT) {
3442                 shootline = core::line3d<f32>(camera_position,
3443                         camera_position + camera_direction * BS * d);
3444         } else {
3445             // prevent player pointing anything in front-view
3446                 shootline = core::line3d<f32>(camera_position,camera_position);
3447         }
3448
3449 #ifdef HAVE_TOUCHSCREENGUI
3450
3451         if ((g_settings->getBool("touchtarget")) && (g_touchscreengui)) {
3452                 shootline = g_touchscreengui->getShootline();
3453                 shootline.start += intToFloat(camera_offset, BS);
3454                 shootline.end += intToFloat(camera_offset, BS);
3455         }
3456
3457 #endif
3458
3459         PointedThing pointed = updatePointedThing(shootline,
3460                         playeritem_def.liquids_pointable,
3461                         !runData.ldown_for_dig,
3462                         camera_offset);
3463
3464         if (pointed != runData.pointed_old) {
3465                 infostream << "Pointing at " << pointed.dump() << std::endl;
3466                 hud->updateSelectionMesh(camera_offset);
3467         }
3468
3469         /*
3470                 Stop digging when
3471                 - releasing left mouse button
3472                 - pointing away from node
3473         */
3474         if (runData.digging) {
3475                 if (getLeftReleased()) {
3476                         infostream << "Left button released"
3477                                    << " (stopped digging)" << std::endl;
3478                         runData.digging = false;
3479                 } else if (pointed != runData.pointed_old) {
3480                         if (pointed.type == POINTEDTHING_NODE
3481                                         && runData.pointed_old.type == POINTEDTHING_NODE
3482                                         && pointed.node_undersurface
3483                                                         == runData.pointed_old.node_undersurface) {
3484                                 // Still pointing to the same node, but a different face.
3485                                 // Don't reset.
3486                         } else {
3487                                 infostream << "Pointing away from node"
3488                                            << " (stopped digging)" << std::endl;
3489                                 runData.digging = false;
3490                                 hud->updateSelectionMesh(camera_offset);
3491                         }
3492                 }
3493
3494                 if (!runData.digging) {
3495                         client->interact(1, runData.pointed_old);
3496                         client->setCrack(-1, v3s16(0, 0, 0));
3497                         runData.dig_time = 0.0;
3498                 }
3499         } else if (runData.dig_instantly && getLeftReleased()) {
3500                 // Remove e.g. torches faster when clicking instead of holding LMB
3501                 runData.nodig_delay_timer = 0;
3502                 runData.dig_instantly = false;
3503         }
3504
3505         if (!runData.digging && runData.ldown_for_dig && !isLeftPressed()) {
3506                 runData.ldown_for_dig = false;
3507         }
3508
3509         runData.left_punch = false;
3510
3511         soundmaker->m_player_leftpunch_sound.name = "";
3512
3513         if (isRightPressed())
3514                 runData.repeat_rightclick_timer += dtime;
3515         else
3516                 runData.repeat_rightclick_timer = 0;
3517
3518         if (playeritem_def.usable && isLeftPressed()) {
3519                 if (getLeftClicked())
3520                         client->interact(4, pointed);
3521         } else if (pointed.type == POINTEDTHING_NODE) {
3522                 ToolCapabilities playeritem_toolcap =
3523                                 playeritem.getToolCapabilities(itemdef_manager);
3524                 if (playeritem.name.empty()) {
3525                         playeritem_toolcap = *hand_def.tool_capabilities;
3526                 }
3527                 handlePointingAtNode(pointed, playeritem_def, playeritem_toolcap, dtime);
3528         } else if (pointed.type == POINTEDTHING_OBJECT) {
3529                 handlePointingAtObject(pointed, playeritem, player_position, show_debug);
3530         } else if (isLeftPressed()) {
3531                 // When button is held down in air, show continuous animation
3532                 runData.left_punch = true;
3533         } else if (getRightClicked()) {
3534                 handlePointingAtNothing(playeritem);
3535         }
3536
3537         runData.pointed_old = pointed;
3538
3539         if (runData.left_punch || getLeftClicked())
3540                 camera->setDigging(0); // left click animation
3541
3542         input->resetLeftClicked();
3543         input->resetRightClicked();
3544
3545         input->joystick.clearWasKeyDown(KeyType::MOUSE_L);
3546         input->joystick.clearWasKeyDown(KeyType::MOUSE_R);
3547
3548         input->resetLeftReleased();
3549         input->resetRightReleased();
3550
3551         input->joystick.clearWasKeyReleased(KeyType::MOUSE_L);
3552         input->joystick.clearWasKeyReleased(KeyType::MOUSE_R);
3553 }
3554
3555
3556 PointedThing Game::updatePointedThing(
3557         const core::line3d<f32> &shootline,
3558         bool liquids_pointable,
3559         bool look_for_object,
3560         const v3s16 &camera_offset)
3561 {
3562         std::vector<aabb3f> *selectionboxes = hud->getSelectionBoxes();
3563         selectionboxes->clear();
3564         hud->setSelectedFaceNormal(v3f(0.0, 0.0, 0.0));
3565         static const bool show_entity_selectionbox = g_settings->getBool(
3566                 "show_entity_selectionbox");
3567
3568         ClientMap &map = client->getEnv().getClientMap();
3569         INodeDefManager *nodedef=client->getNodeDefManager();
3570
3571         runData.selected_object = NULL;
3572
3573         PointedThing result=client->getEnv().getPointedThing(
3574                 shootline, liquids_pointable, look_for_object);
3575         if (result.type == POINTEDTHING_OBJECT) {
3576                 runData.selected_object = client->getEnv().getActiveObject(result.object_id);
3577                 if (show_entity_selectionbox && runData.selected_object->doShowSelectionBox()) {
3578                         aabb3f *selection_box = runData.selected_object->getSelectionBox();
3579
3580                         // Box should exist because object was
3581                         // returned in the first place
3582
3583                         assert(selection_box);
3584
3585                         v3f pos = runData.selected_object->getPosition();
3586                         selectionboxes->push_back(aabb3f(
3587                                 selection_box->MinEdge, selection_box->MaxEdge));
3588                         selectionboxes->push_back(
3589                                 aabb3f(selection_box->MinEdge, selection_box->MaxEdge));
3590                         hud->setSelectionPos(pos, camera_offset);
3591                 }
3592         } else if (result.type == POINTEDTHING_NODE) {
3593                 // Update selection boxes
3594                 MapNode n = map.getNodeNoEx(result.node_undersurface);
3595                 std::vector<aabb3f> boxes;
3596                 n.getSelectionBoxes(nodedef, &boxes,
3597                         n.getNeighbors(result.node_undersurface, &map));
3598
3599                 f32 d = 0.002 * BS;
3600                 for (std::vector<aabb3f>::const_iterator i = boxes.begin();
3601                         i != boxes.end(); ++i) {
3602                         aabb3f box = *i;
3603                         box.MinEdge -= v3f(d, d, d);
3604                         box.MaxEdge += v3f(d, d, d);
3605                         selectionboxes->push_back(box);
3606                 }
3607                 hud->setSelectionPos(intToFloat(result.node_undersurface, BS),
3608                         camera_offset);
3609                 hud->setSelectedFaceNormal(v3f(
3610                         result.intersection_normal.X,
3611                         result.intersection_normal.Y,
3612                         result.intersection_normal.Z));
3613         }
3614
3615         // Update selection mesh light level and vertex colors
3616         if (selectionboxes->size() > 0) {
3617                 v3f pf = hud->getSelectionPos();
3618                 v3s16 p = floatToInt(pf, BS);
3619
3620                 // Get selection mesh light level
3621                 MapNode n = map.getNodeNoEx(p);
3622                 u16 node_light = getInteriorLight(n, -1, nodedef);
3623                 u16 light_level = node_light;
3624
3625                 for (u8 i = 0; i < 6; i++) {
3626                         n = map.getNodeNoEx(p + g_6dirs[i]);
3627                         node_light = getInteriorLight(n, -1, nodedef);
3628                         if (node_light > light_level)
3629                                 light_level = node_light;
3630                 }
3631
3632                 u32 daynight_ratio = client->getEnv().getDayNightRatio();
3633                 video::SColor c;
3634                 final_color_blend(&c, light_level, daynight_ratio);
3635
3636                 // Modify final color a bit with time
3637                 u32 timer = porting::getTimeMs() % 5000;
3638                 float timerf = (float) (irr::core::PI * ((timer / 2500.0) - 0.5));
3639                 float sin_r = 0.08 * sin(timerf);
3640                 float sin_g = 0.08 * sin(timerf + irr::core::PI * 0.5);
3641                 float sin_b = 0.08 * sin(timerf + irr::core::PI);
3642                 c.setRed(
3643                         core::clamp(core::round32(c.getRed() * (0.8 + sin_r)), 0, 255));
3644                 c.setGreen(
3645                         core::clamp(core::round32(c.getGreen() * (0.8 + sin_g)), 0, 255));
3646                 c.setBlue(
3647                         core::clamp(core::round32(c.getBlue() * (0.8 + sin_b)), 0, 255));
3648
3649                 // Set mesh final color
3650                 hud->setSelectionMeshColor(c);
3651         }
3652         return result;
3653 }
3654
3655
3656 void Game::handlePointingAtNothing(const ItemStack &playerItem)
3657 {
3658         infostream << "Right Clicked in Air" << std::endl;
3659         PointedThing fauxPointed;
3660         fauxPointed.type = POINTEDTHING_NOTHING;
3661         client->interact(5, fauxPointed);
3662 }
3663
3664
3665 void Game::handlePointingAtNode(const PointedThing &pointed, const ItemDefinition &playeritem_def,
3666                 const ToolCapabilities &playeritem_toolcap, f32 dtime)
3667 {
3668         v3s16 nodepos = pointed.node_undersurface;
3669         v3s16 neighbourpos = pointed.node_abovesurface;
3670
3671         /*
3672                 Check information text of node
3673         */
3674
3675         ClientMap &map = client->getEnv().getClientMap();
3676         NodeMetadata *meta = map.getNodeMetadata(nodepos);
3677
3678         if (meta) {
3679                 infotext = unescape_enriched(utf8_to_wide(meta->getString("infotext")));
3680         } else {
3681                 MapNode n = map.getNodeNoEx(nodepos);
3682
3683                 if (nodedef_manager->get(n).tiledef[0].name == "unknown_node.png") {
3684                         infotext = L"Unknown node: ";
3685                         infotext += utf8_to_wide(nodedef_manager->get(n).name);
3686                 }
3687         }
3688
3689         if (runData.nodig_delay_timer <= 0.0 && isLeftPressed()
3690                         && client->checkPrivilege("interact")) {
3691                 handleDigging(pointed, nodepos, playeritem_toolcap, dtime);
3692         }
3693
3694         if ((getRightClicked() ||
3695                         runData.repeat_rightclick_timer >= m_repeat_right_click_time) &&
3696                         client->checkPrivilege("interact")) {
3697                 runData.repeat_rightclick_timer = 0;
3698                 infostream << "Ground right-clicked" << std::endl;
3699
3700                 if (meta && meta->getString("formspec") != "" && !random_input
3701                                 && !isKeyDown(KeyType::SNEAK)) {
3702                         infostream << "Launching custom inventory view" << std::endl;
3703
3704                         InventoryLocation inventoryloc;
3705                         inventoryloc.setNodeMeta(nodepos);
3706
3707                         NodeMetadataFormSource *fs_src = new NodeMetadataFormSource(
3708                                 &client->getEnv().getClientMap(), nodepos);
3709                         TextDest *txt_dst = new TextDestNodeMetadata(nodepos, client);
3710
3711                         create_formspec_menu(&current_formspec, client,
3712                                         device, &input->joystick, fs_src, txt_dst);
3713                         cur_formname = "";
3714
3715                         current_formspec->setFormSpec(meta->getString("formspec"), inventoryloc);
3716                 } else {
3717                         // Report right click to server
3718
3719                         camera->setDigging(1);  // right click animation (always shown for feedback)
3720
3721                         // If the wielded item has node placement prediction,
3722                         // make that happen
3723                         bool placed = nodePlacementPrediction(*client,
3724                                         playeritem_def,
3725                                         nodepos, neighbourpos);
3726
3727                         if (placed) {
3728                                 // Report to server
3729                                 client->interact(3, pointed);
3730                                 // Read the sound
3731                                 soundmaker->m_player_rightpunch_sound =
3732                                                 playeritem_def.sound_place;
3733                         } else {
3734                                 soundmaker->m_player_rightpunch_sound =
3735                                                 SimpleSoundSpec();
3736
3737                                 if (playeritem_def.node_placement_prediction == "" ||
3738                                                 nodedef_manager->get(map.getNodeNoEx(nodepos)).rightclickable) {
3739                                         client->interact(3, pointed); // Report to server
3740                                 } else {
3741                                         soundmaker->m_player_rightpunch_sound =
3742                                                 playeritem_def.sound_place_failed;
3743                                 }
3744                         }
3745                 }
3746         }
3747 }
3748
3749
3750 void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack &playeritem,
3751                 const v3f &player_position, bool show_debug)
3752 {
3753         infotext = unescape_enriched(
3754                 utf8_to_wide(runData.selected_object->infoText()));
3755
3756         if (show_debug) {
3757                 if (infotext != L"") {
3758                         infotext += L"\n";
3759                 }
3760                 infotext += unescape_enriched(utf8_to_wide(
3761                         runData.selected_object->debugInfoText()));
3762         }
3763
3764         if (isLeftPressed()) {
3765                 bool do_punch = false;
3766                 bool do_punch_damage = false;
3767
3768                 if (runData.object_hit_delay_timer <= 0.0) {
3769                         do_punch = true;
3770                         do_punch_damage = true;
3771                         runData.object_hit_delay_timer = object_hit_delay;
3772                 }
3773
3774                 if (getLeftClicked())
3775                         do_punch = true;
3776
3777                 if (do_punch) {
3778                         infostream << "Left-clicked object" << std::endl;
3779                         runData.left_punch = true;
3780                 }
3781
3782                 if (do_punch_damage) {
3783                         // Report direct punch
3784                         v3f objpos = runData.selected_object->getPosition();
3785                         v3f dir = (objpos - player_position).normalize();
3786                         ItemStack item = playeritem;
3787                         if (playeritem.name.empty()) {
3788                                 InventoryList *hlist = local_inventory->getList("hand");
3789                                 if (hlist) {
3790                                         item = hlist->getItem(0);
3791                                 }
3792                         }
3793
3794                         bool disable_send = runData.selected_object->directReportPunch(
3795                                         dir, &item, runData.time_from_last_punch);
3796                         runData.time_from_last_punch = 0;
3797
3798                         if (!disable_send)
3799                                 client->interact(0, pointed);
3800                 }
3801         } else if (getRightClicked()) {
3802                 infostream << "Right-clicked object" << std::endl;
3803                 client->interact(3, pointed);  // place
3804         }
3805 }
3806
3807
3808 void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
3809                 const ToolCapabilities &playeritem_toolcap, f32 dtime)
3810 {
3811         LocalPlayer *player = client->getEnv().getLocalPlayer();
3812         ClientMap &map = client->getEnv().getClientMap();
3813         MapNode n = client->getEnv().getClientMap().getNodeNoEx(nodepos);
3814
3815         // NOTE: Similar piece of code exists on the server side for
3816         // cheat detection.
3817         // Get digging parameters
3818         DigParams params = getDigParams(nodedef_manager->get(n).groups,
3819                         &playeritem_toolcap);
3820
3821         // If can't dig, try hand
3822         if (!params.diggable) {
3823                 InventoryList *hlist = local_inventory->getList("hand");
3824                 const ItemDefinition &hand =
3825                         hlist ? hlist->getItem(0).getDefinition(itemdef_manager) : itemdef_manager->get("");
3826                 const ToolCapabilities *tp = hand.tool_capabilities;
3827
3828                 if (tp)
3829                         params = getDigParams(nodedef_manager->get(n).groups, tp);
3830         }
3831
3832         if (!runData.digging) {
3833                 infostream << "Started digging" << std::endl;
3834                 runData.dig_instantly = params.time == 0;
3835                 if (client->moddingEnabled() && client->getScript()->on_punchnode(nodepos, n))
3836                         return;
3837                 client->interact(0, pointed);
3838                 runData.digging = true;
3839                 runData.ldown_for_dig = true;
3840         }
3841
3842         if (!params.diggable) {
3843                 // I guess nobody will wait for this long
3844                 runData.dig_time_complete = 10000000.0;
3845         } else {
3846                 runData.dig_time_complete = params.time;
3847
3848                 if (m_cache_enable_particles) {
3849                         const ContentFeatures &features =
3850                                         client->getNodeDefManager()->get(n);
3851                         client->getParticleManager()->addPunchingParticles(client, smgr,
3852                                         player, nodepos, n, features);
3853                 }
3854         }
3855
3856         if (!runData.dig_instantly) {
3857                 runData.dig_index = (float)crack_animation_length
3858                                 * runData.dig_time
3859                                 / runData.dig_time_complete;
3860         } else {
3861                 // This is for e.g. torches
3862                 runData.dig_index = crack_animation_length;
3863         }
3864
3865         SimpleSoundSpec sound_dig = nodedef_manager->get(n).sound_dig;
3866
3867         if (sound_dig.exists() && params.diggable) {
3868                 if (sound_dig.name == "__group") {
3869                         if (params.main_group != "") {
3870                                 soundmaker->m_player_leftpunch_sound.gain = 0.5;
3871                                 soundmaker->m_player_leftpunch_sound.name =
3872                                                 std::string("default_dig_") +
3873                                                 params.main_group;
3874                         }
3875                 } else {
3876                         soundmaker->m_player_leftpunch_sound = sound_dig;
3877                 }
3878         }
3879
3880         // Don't show cracks if not diggable
3881         if (runData.dig_time_complete >= 100000.0) {
3882         } else if (runData.dig_index < crack_animation_length) {
3883                 //TimeTaker timer("client.setTempMod");
3884                 //infostream<<"dig_index="<<dig_index<<std::endl;
3885                 client->setCrack(runData.dig_index, nodepos);
3886         } else {
3887                 infostream << "Digging completed" << std::endl;
3888                 client->setCrack(-1, v3s16(0, 0, 0));
3889
3890                 runData.dig_time = 0;
3891                 runData.digging = false;
3892
3893                 runData.nodig_delay_timer =
3894                                 runData.dig_time_complete / (float)crack_animation_length;
3895
3896                 // We don't want a corresponding delay to very time consuming nodes
3897                 // and nodes without digging time (e.g. torches) get a fixed delay.
3898                 if (runData.nodig_delay_timer > 0.3)
3899                         runData.nodig_delay_timer = 0.3;
3900                 else if (runData.dig_instantly)
3901                         runData.nodig_delay_timer = 0.15;
3902
3903                 bool is_valid_position;
3904                 MapNode wasnode = map.getNodeNoEx(nodepos, &is_valid_position);
3905                 if (is_valid_position) {
3906                         if (client->moddingEnabled()) {
3907                                 if (client->getScript()->on_dignode(nodepos, wasnode)) {
3908                                         return;
3909                                 }
3910                         }
3911                         client->removeNode(nodepos);
3912                 }
3913
3914                 client->interact(2, pointed);
3915
3916                 if (m_cache_enable_particles) {
3917                         const ContentFeatures &features =
3918                                 client->getNodeDefManager()->get(wasnode);
3919                         client->getParticleManager()->addDiggingParticles(client, smgr,
3920                                 player, nodepos, wasnode, features);
3921                 }
3922
3923
3924                 // Send event to trigger sound
3925                 MtEvent *e = new NodeDugEvent(nodepos, wasnode);
3926                 client->event()->put(e);
3927         }
3928
3929         if (runData.dig_time_complete < 100000.0) {
3930                 runData.dig_time += dtime;
3931         } else {
3932                 runData.dig_time = 0;
3933                 client->setCrack(-1, nodepos);
3934         }
3935
3936         camera->setDigging(0);  // left click animation
3937 }
3938
3939
3940 void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
3941                 const CameraOrientation &cam)
3942 {
3943         LocalPlayer *player = client->getEnv().getLocalPlayer();
3944
3945         /*
3946                 Fog range
3947         */
3948
3949         if (draw_control->range_all) {
3950                 runData.fog_range = 100000 * BS;
3951         } else {
3952                 runData.fog_range = draw_control->wanted_range * BS;
3953         }
3954
3955         /*
3956                 Calculate general brightness
3957         */
3958         u32 daynight_ratio = client->getEnv().getDayNightRatio();
3959         float time_brightness = decode_light_f((float)daynight_ratio / 1000.0);
3960         float direct_brightness;
3961         bool sunlight_seen;
3962
3963         if (m_cache_enable_noclip && m_cache_enable_free_move) {
3964                 direct_brightness = time_brightness;
3965                 sunlight_seen = true;
3966         } else {
3967                 ScopeProfiler sp(g_profiler, "Detecting background light", SPT_AVG);
3968                 float old_brightness = sky->getBrightness();
3969                 direct_brightness = client->getEnv().getClientMap()
3970                                 .getBackgroundBrightness(MYMIN(runData.fog_range * 1.2, 60 * BS),
3971                                         daynight_ratio, (int)(old_brightness * 255.5), &sunlight_seen)
3972                                     / 255.0;
3973         }
3974
3975         float time_of_day_smooth = runData.time_of_day_smooth;
3976         float time_of_day = client->getEnv().getTimeOfDayF();
3977
3978         static const float maxsm = 0.05;
3979         static const float todsm = 0.05;
3980
3981         if (fabs(time_of_day - time_of_day_smooth) > maxsm &&
3982                         fabs(time_of_day - time_of_day_smooth + 1.0) > maxsm &&
3983                         fabs(time_of_day - time_of_day_smooth - 1.0) > maxsm)
3984                 time_of_day_smooth = time_of_day;
3985
3986         if (time_of_day_smooth > 0.8 && time_of_day < 0.2)
3987                 time_of_day_smooth = time_of_day_smooth * (1.0 - todsm)
3988                                 + (time_of_day + 1.0) * todsm;
3989         else
3990                 time_of_day_smooth = time_of_day_smooth * (1.0 - todsm)
3991                                 + time_of_day * todsm;
3992
3993         runData.time_of_day = time_of_day;
3994         runData.time_of_day_smooth = time_of_day_smooth;
3995
3996         sky->update(time_of_day_smooth, time_brightness, direct_brightness,
3997                         sunlight_seen, camera->getCameraMode(), player->getYaw(),
3998                         player->getPitch());
3999
4000         /*
4001                 Update clouds
4002         */
4003         if (clouds) {
4004                 v3f player_position = player->getPosition();
4005                 if (sky->getCloudsVisible()) {
4006                         clouds->setVisible(true);
4007                         clouds->step(dtime);
4008                         clouds->update(v2f(player_position.X, player_position.Z),
4009                                        sky->getCloudColor());
4010                 } else {
4011                         clouds->setVisible(false);
4012                 }
4013         }
4014
4015         /*
4016                 Update particles
4017         */
4018         client->getParticleManager()->step(dtime);
4019
4020         /*
4021                 Fog
4022         */
4023
4024         if (m_cache_enable_fog && !flags.force_fog_off) {
4025                 driver->setFog(
4026                                 sky->getBgColor(),
4027                                 video::EFT_FOG_LINEAR,
4028                                 runData.fog_range * m_cache_fog_start,
4029                                 runData.fog_range * 1.0,
4030                                 0.01,
4031                                 false, // pixel fog
4032                                 true // range fog
4033                 );
4034         } else {
4035                 driver->setFog(
4036                                 sky->getBgColor(),
4037                                 video::EFT_FOG_LINEAR,
4038                                 100000 * BS,
4039                                 110000 * BS,
4040                                 0.01,
4041                                 false, // pixel fog
4042                                 false // range fog
4043                 );
4044         }
4045
4046         /*
4047                 Get chat messages from client
4048         */
4049
4050         v2u32 screensize = driver->getScreenSize();
4051
4052         updateChat(*client, dtime, flags.show_debug, screensize,
4053                         flags.show_chat, runData.profiler_current_page,
4054                         *chat_backend, guitext_chat);
4055
4056         /*
4057                 Inventory
4058         */
4059
4060         if (client->getPlayerItem() != runData.new_playeritem)
4061                 client->selectPlayerItem(runData.new_playeritem);
4062
4063         // Update local inventory if it has changed
4064         if (client->getLocalInventoryUpdated()) {
4065                 //infostream<<"Updating local inventory"<<std::endl;
4066                 client->getLocalInventory(*local_inventory);
4067                 runData.update_wielded_item_trigger = true;
4068         }
4069
4070         if (runData.update_wielded_item_trigger) {
4071                 // Update wielded tool
4072                 InventoryList *mlist = local_inventory->getList("main");
4073
4074                 if (mlist && (client->getPlayerItem() < mlist->getSize())) {
4075                         ItemStack item = mlist->getItem(client->getPlayerItem());
4076                         if (item.getDefinition(itemdef_manager).name.empty()) { // override the hand
4077                                 InventoryList *hlist = local_inventory->getList("hand");
4078                                 if (hlist)
4079                                         item = hlist->getItem(0);
4080                         }
4081                         camera->wield(item);
4082                 }
4083
4084                 runData.update_wielded_item_trigger = false;
4085         }
4086
4087         /*
4088                 Update block draw list every 200ms or when camera direction has
4089                 changed much
4090         */
4091         runData.update_draw_list_timer += dtime;
4092
4093         v3f camera_direction = camera->getDirection();
4094         if (runData.update_draw_list_timer >= 0.2
4095                         || runData.update_draw_list_last_cam_dir.getDistanceFrom(camera_direction) > 0.2
4096                         || m_camera_offset_changed) {
4097                 runData.update_draw_list_timer = 0;
4098                 client->getEnv().getClientMap().updateDrawList(driver);
4099                 runData.update_draw_list_last_cam_dir = camera_direction;
4100         }
4101
4102         updateGui(*stats, dtime, cam);
4103
4104         /*
4105            make sure menu is on top
4106            1. Delete formspec menu reference if menu was removed
4107            2. Else, make sure formspec menu is on top
4108         */
4109         if (current_formspec) {
4110                 if (current_formspec->getReferenceCount() == 1) {
4111                         current_formspec->drop();
4112                         current_formspec = NULL;
4113                 } else if (!noMenuActive()) {
4114                         guiroot->bringToFront(current_formspec);
4115                 }
4116         }
4117
4118         /*
4119                 Drawing begins
4120         */
4121
4122         const video::SColor &skycolor = sky->getSkyColor();
4123
4124         TimeTaker tt_draw("mainloop: draw");
4125         driver->beginScene(true, true, skycolor);
4126
4127         draw_scene(driver, smgr, *camera, *client, player, *hud, *mapper,
4128                         guienv, screensize, skycolor, flags.show_hud,
4129                         flags.show_minimap);
4130
4131         /*
4132                 Profiler graph
4133         */
4134         if (flags.show_profiler_graph)
4135                 graph->draw(10, screensize.Y - 10, driver, g_fontengine->getFont());
4136
4137         /*
4138                 Damage flash
4139         */
4140         if (runData.damage_flash > 0.0) {
4141                 video::SColor color(runData.damage_flash, 180, 0, 0);
4142                 driver->draw2DRectangle(color,
4143                                         core::rect<s32>(0, 0, screensize.X, screensize.Y),
4144                                         NULL);
4145
4146                 runData.damage_flash -= 100.0 * dtime;
4147         }
4148
4149         /*
4150                 Damage camera tilt
4151         */
4152         if (player->hurt_tilt_timer > 0.0) {
4153                 player->hurt_tilt_timer -= dtime * 5;
4154
4155                 if (player->hurt_tilt_timer < 0)
4156                         player->hurt_tilt_strength = 0;
4157         }
4158
4159         /*
4160                 Update minimap pos and rotation
4161         */
4162         if (flags.show_minimap && flags.show_hud) {
4163                 mapper->setPos(floatToInt(player->getPosition(), BS));
4164                 mapper->setAngle(player->getYaw());
4165         }
4166
4167         /*
4168                 End scene
4169         */
4170         driver->endScene();
4171
4172         stats->drawtime = tt_draw.stop(true);
4173         g_profiler->graphAdd("mainloop_draw", stats->drawtime / 1000.0f);
4174 }
4175
4176
4177 inline static const char *yawToDirectionString(int yaw)
4178 {
4179         static const char *direction[4] = {"North [+Z]", "West [-X]", "South [-Z]", "East [+X]"};
4180
4181         yaw = wrapDegrees_0_360(yaw);
4182         yaw = (yaw + 45) % 360 / 90;
4183
4184         return direction[yaw];
4185 }
4186
4187
4188 void Game::updateGui(const RunStats &stats, f32 dtime, const CameraOrientation &cam)
4189 {
4190         v2u32 screensize = driver->getScreenSize();
4191         LocalPlayer *player = client->getEnv().getLocalPlayer();
4192         v3f player_position = player->getPosition();
4193
4194         if (flags.show_debug) {
4195                 static float drawtime_avg = 0;
4196                 drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
4197
4198                 u16 fps = 1.0 / stats.dtime_jitter.avg;
4199
4200                 std::ostringstream os(std::ios_base::binary);
4201                 os << std::fixed
4202                    << PROJECT_NAME_C " " << g_version_hash
4203                    << " FPS = " << fps
4204                    << " (R: range_all=" << draw_control->range_all << ")"
4205                    << std::setprecision(0)
4206                    << " drawtime = " << drawtime_avg
4207                    << std::setprecision(1)
4208                    << ", dtime_jitter = "
4209                    << (stats.dtime_jitter.max_fraction * 100.0) << " %"
4210                    << std::setprecision(1)
4211                    << ", v_range = " << draw_control->wanted_range
4212                    << std::setprecision(3)
4213                    << ", RTT = " << client->getRTT();
4214                 setStaticText(guitext, utf8_to_wide(os.str()).c_str());
4215                 guitext->setVisible(true);
4216         } else {
4217                 guitext->setVisible(false);
4218         }
4219
4220         if (guitext->isVisible()) {
4221                 core::rect<s32> rect(
4222                                 5,              5,
4223                                 screensize.X,   5 + g_fontengine->getTextHeight()
4224                 );
4225                 guitext->setRelativePosition(rect);
4226         }
4227
4228         if (flags.show_debug) {
4229                 std::ostringstream os(std::ios_base::binary);
4230                 os << std::setprecision(1) << std::fixed
4231                    << "(" << (player_position.X / BS)
4232                    << ", " << (player_position.Y / BS)
4233                    << ", " << (player_position.Z / BS)
4234                    << ") (yaw=" << (wrapDegrees_0_360(cam.camera_yaw))
4235                    << " " << yawToDirectionString(cam.camera_yaw)
4236                    << ") (seed = " << ((u64)client->getMapSeed())
4237                    << ")";
4238
4239                 if (runData.pointed_old.type == POINTEDTHING_NODE) {
4240                         ClientMap &map = client->getEnv().getClientMap();
4241                         const INodeDefManager *nodedef = client->getNodeDefManager();
4242                         MapNode n = map.getNodeNoEx(runData.pointed_old.node_undersurface);
4243                         if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
4244                                 const ContentFeatures &features = nodedef->get(n);
4245                                 os << " (pointing_at = " << nodedef->get(n).name
4246                                    << " - " << features.tiledef[0].name.c_str()
4247                                    << ")";
4248                         }
4249                 }
4250
4251                 setStaticText(guitext2, utf8_to_wide(os.str()).c_str());
4252                 guitext2->setVisible(true);
4253
4254                 core::rect<s32> rect(
4255                                 5,             5 + g_fontengine->getTextHeight(),
4256                                 screensize.X,  5 + g_fontengine->getTextHeight() * 2
4257                 );
4258                 guitext2->setRelativePosition(rect);
4259         } else {
4260                 guitext2->setVisible(false);
4261         }
4262
4263         setStaticText(guitext_info, infotext.c_str());
4264         guitext_info->setVisible(flags.show_hud && g_menumgr.menuCount() == 0);
4265
4266         float statustext_time_max = 1.5;
4267
4268         if (!m_statustext.empty()) {
4269                 runData.statustext_time += dtime;
4270
4271                 if (runData.statustext_time >= statustext_time_max) {
4272                         m_statustext = L"";
4273                         runData.statustext_time = 0;
4274                 }
4275         }
4276
4277         setStaticText(guitext_status, m_statustext.c_str());
4278         guitext_status->setVisible(!m_statustext.empty());
4279
4280         if (!m_statustext.empty()) {
4281                 s32 status_width  = guitext_status->getTextWidth();
4282                 s32 status_height = guitext_status->getTextHeight();
4283                 s32 status_y = screensize.Y - 150;
4284                 s32 status_x = (screensize.X - status_width) / 2;
4285                 core::rect<s32> rect(
4286                                 status_x , status_y - status_height,
4287                                 status_x + status_width, status_y
4288                 );
4289                 guitext_status->setRelativePosition(rect);
4290
4291                 // Fade out
4292                 video::SColor initial_color(255, 0, 0, 0);
4293
4294                 if (guienv->getSkin())
4295                         initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
4296
4297                 video::SColor final_color = initial_color;
4298                 final_color.setAlpha(0);
4299                 video::SColor fade_color = initial_color.getInterpolated_quadratic(
4300                                 initial_color, final_color,
4301                                 pow(runData.statustext_time / statustext_time_max, 2.0f));
4302                 guitext_status->setOverrideColor(fade_color);
4303                 guitext_status->enableOverrideColor(true);
4304         }
4305 }
4306
4307
4308 /* Log times and stuff for visualization */
4309 inline void Game::updateProfilerGraphs(ProfilerGraph *graph)
4310 {
4311         Profiler::GraphValues values;
4312         g_profiler->graphGet(values);
4313         graph->put(values);
4314 }
4315
4316
4317
4318 /****************************************************************************
4319  Misc
4320  ****************************************************************************/
4321
4322 /* On some computers framerate doesn't seem to be automatically limited
4323  */
4324 inline void Game::limitFps(FpsControl *fps_timings, f32 *dtime)
4325 {
4326         // not using getRealTime is necessary for wine
4327         device->getTimer()->tick(); // Maker sure device time is up-to-date
4328         u32 time = device->getTimer()->getTime();
4329         u32 last_time = fps_timings->last_time;
4330
4331         if (time > last_time)  // Make sure time hasn't overflowed
4332                 fps_timings->busy_time = time - last_time;
4333         else
4334                 fps_timings->busy_time = 0;
4335
4336         u32 frametime_min = 1000 / (g_menumgr.pausesGame()
4337                         ? g_settings->getFloat("pause_fps_max")
4338                         : g_settings->getFloat("fps_max"));
4339
4340         if (fps_timings->busy_time < frametime_min) {
4341                 fps_timings->sleep_time = frametime_min - fps_timings->busy_time;
4342                 device->sleep(fps_timings->sleep_time);
4343         } else {
4344                 fps_timings->sleep_time = 0;
4345         }
4346
4347         /* Get the new value of the device timer. Note that device->sleep() may
4348          * not sleep for the entire requested time as sleep may be interrupted and
4349          * therefore it is arguably more accurate to get the new time from the
4350          * device rather than calculating it by adding sleep_time to time.
4351          */
4352
4353         device->getTimer()->tick(); // Update device timer
4354         time = device->getTimer()->getTime();
4355
4356         if (time > last_time)  // Make sure last_time hasn't overflowed
4357                 *dtime = (time - last_time) / 1000.0;
4358         else
4359                 *dtime = 0;
4360
4361         fps_timings->last_time = time;
4362 }
4363
4364 // Note: This will free (using delete[])! \p msg. If you want to use it later,
4365 // pass a copy of it to this function
4366 // Note: \p msg must be allocated using new (not malloc())
4367 void Game::showOverlayMessage(const wchar_t *msg, float dtime,
4368                 int percent, bool draw_clouds)
4369 {
4370         draw_load_screen(msg, device, guienv, texture_src, dtime, percent,
4371                 draw_clouds);
4372         delete[] msg;
4373 }
4374
4375 void Game::settingChangedCallback(const std::string &setting_name, void *data)
4376 {
4377         ((Game *)data)->readSettings();
4378 }
4379
4380 void Game::readSettings()
4381 {
4382         m_cache_doubletap_jump               = g_settings->getBool("doubletap_jump");
4383         m_cache_enable_clouds                = g_settings->getBool("enable_clouds");
4384         m_cache_enable_joysticks             = g_settings->getBool("enable_joysticks");
4385         m_cache_enable_particles             = g_settings->getBool("enable_particles");
4386         m_cache_enable_fog                   = g_settings->getBool("enable_fog");
4387         m_cache_mouse_sensitivity            = g_settings->getFloat("mouse_sensitivity");
4388         m_cache_joystick_frustum_sensitivity = g_settings->getFloat("joystick_frustum_sensitivity");
4389         m_repeat_right_click_time            = g_settings->getFloat("repeat_rightclick_time");
4390
4391         m_cache_enable_noclip                = g_settings->getBool("noclip");
4392         m_cache_enable_free_move             = g_settings->getBool("free_move");
4393
4394         m_cache_fog_start                    = g_settings->getFloat("fog_start");
4395
4396         m_cache_cam_smoothing = 0;
4397         if (g_settings->getBool("cinematic"))
4398                 m_cache_cam_smoothing = 1 - g_settings->getFloat("cinematic_camera_smoothing");
4399         else
4400                 m_cache_cam_smoothing = 1 - g_settings->getFloat("camera_smoothing");
4401
4402         m_cache_fog_start = rangelim(m_cache_fog_start, 0.0f, 0.99f);
4403         m_cache_cam_smoothing = rangelim(m_cache_cam_smoothing, 0.01f, 1.0f);
4404         m_cache_mouse_sensitivity = rangelim(m_cache_mouse_sensitivity, 0.001, 100.0);
4405
4406 }
4407
4408 /****************************************************************************/
4409 /****************************************************************************
4410  Shutdown / cleanup
4411  ****************************************************************************/
4412 /****************************************************************************/
4413
4414 void Game::extendedResourceCleanup()
4415 {
4416         // Extended resource accounting
4417         infostream << "Irrlicht resources after cleanup:" << std::endl;
4418         infostream << "\tRemaining meshes   : "
4419                    << device->getSceneManager()->getMeshCache()->getMeshCount() << std::endl;
4420         infostream << "\tRemaining textures : "
4421                    << driver->getTextureCount() << std::endl;
4422
4423         for (unsigned int i = 0; i < driver->getTextureCount(); i++) {
4424                 irr::video::ITexture *texture = driver->getTextureByIndex(i);
4425                 infostream << "\t\t" << i << ":" << texture->getName().getPath().c_str()
4426                            << std::endl;
4427         }
4428
4429         clearTextureNameCache();
4430         infostream << "\tRemaining materials: "
4431                << driver-> getMaterialRendererCount()
4432                        << " (note: irrlicht doesn't support removing renderers)" << std::endl;
4433 }
4434
4435 void Game::showPauseMenu()
4436 {
4437 #ifdef __ANDROID__
4438         static const std::string control_text = strgettext("Default Controls:\n"
4439                 "No menu visible:\n"
4440                 "- single tap: button activate\n"
4441                 "- double tap: place/use\n"
4442                 "- slide finger: look around\n"
4443                 "Menu/Inventory visible:\n"
4444                 "- double tap (outside):\n"
4445                 " -->close\n"
4446                 "- touch stack, touch slot:\n"
4447                 " --> move stack\n"
4448                 "- touch&drag, tap 2nd finger\n"
4449                 " --> place single item to slot\n"
4450                 );
4451 #else
4452         static const std::string control_text = strgettext("Default Controls:\n"
4453                 "- WASD: move\n"
4454                 "- Space: jump/climb\n"
4455                 "- Shift: sneak/go down\n"
4456                 "- Q: drop item\n"
4457                 "- I: inventory\n"
4458                 "- Mouse: turn/look\n"
4459                 "- Mouse left: dig/punch\n"
4460                 "- Mouse right: place/use\n"
4461                 "- Mouse wheel: select item\n"
4462                 "- T: chat\n"
4463         );
4464 #endif
4465
4466         float ypos = simple_singleplayer_mode ? 0.5 : 0.1;
4467         std::ostringstream os;
4468
4469         os << FORMSPEC_VERSION_STRING  << SIZE_TAG
4470                 << "button_exit[4," << (ypos++) << ";3,0.5;btn_continue;"
4471                 << strgettext("Continue") << "]";
4472
4473         if (!simple_singleplayer_mode) {
4474                 os << "button_exit[4," << (ypos++) << ";3,0.5;btn_change_password;"
4475                         << strgettext("Change Password") << "]";
4476         }
4477
4478 #ifndef __ANDROID__
4479         os              << "button_exit[4," << (ypos++) << ";3,0.5;btn_sound;"
4480                 << strgettext("Sound Volume") << "]";
4481         os              << "button_exit[4," << (ypos++) << ";3,0.5;btn_key_config;"
4482                 << strgettext("Change Keys")  << "]";
4483 #endif
4484         os              << "button_exit[4," << (ypos++) << ";3,0.5;btn_exit_menu;"
4485                 << strgettext("Exit to Menu") << "]";
4486         os              << "button_exit[4," << (ypos++) << ";3,0.5;btn_exit_os;"
4487                 << strgettext("Exit to OS")   << "]"
4488                 << "textarea[7.5,0.25;3.9,6.25;;" << control_text << ";]"
4489                 << "textarea[0.4,0.25;3.5,6;;" << PROJECT_NAME_C "\n"
4490                 << g_build_info << "\n"
4491                 << "path_user = " << wrap_rows(porting::path_user, 20)
4492                 << "\n;]";
4493
4494         /* Create menu */
4495         /* Note: FormspecFormSource and LocalFormspecHandler  *
4496          * are deleted by guiFormSpecMenu                     */
4497         FormspecFormSource *fs_src = new FormspecFormSource(os.str());
4498         LocalFormspecHandler *txt_dst = new LocalFormspecHandler("MT_PAUSE_MENU");
4499
4500         create_formspec_menu(&current_formspec, client, device, &input->joystick, fs_src, txt_dst);
4501         current_formspec->setFocus("btn_continue");
4502         current_formspec->doPause = true;
4503 }
4504
4505 /****************************************************************************/
4506 /****************************************************************************
4507  extern function for launching the game
4508  ****************************************************************************/
4509 /****************************************************************************/
4510
4511 void the_game(bool *kill,
4512                 bool random_input,
4513                 InputHandler *input,
4514                 IrrlichtDevice *device,
4515
4516                 const std::string &map_dir,
4517                 const std::string &playername,
4518                 const std::string &password,
4519                 const std::string &address,         // If empty local server is created
4520                 u16 port,
4521
4522                 std::string &error_message,
4523                 ChatBackend &chat_backend,
4524                 bool *reconnect_requested,
4525                 const SubgameSpec &gamespec,        // Used for local game
4526                 bool simple_singleplayer_mode)
4527 {
4528         Game game;
4529
4530         /* Make a copy of the server address because if a local singleplayer server
4531          * is created then this is updated and we don't want to change the value
4532          * passed to us by the calling function
4533          */
4534         std::string server_address = address;
4535
4536         try {
4537
4538                 if (game.startup(kill, random_input, input, device, map_dir,
4539                                 playername, password, &server_address, port, error_message,
4540                                 reconnect_requested, &chat_backend, gamespec,
4541                                 simple_singleplayer_mode)) {
4542                         game.run();
4543                         game.shutdown();
4544                 }
4545
4546         } catch (SerializationError &e) {
4547                 error_message = std::string("A serialization error occurred:\n")
4548                                 + e.what() + "\n\nThe server is probably "
4549                                 " running a different version of " PROJECT_NAME_C ".";
4550                 errorstream << error_message << std::endl;
4551         } catch (ServerError &e) {
4552                 error_message = e.what();
4553                 errorstream << "ServerError: " << error_message << std::endl;
4554         } catch (ModError &e) {
4555                 error_message = e.what() + strgettext("\nCheck debug.txt for details.");
4556                 errorstream << "ModError: " << error_message << std::endl;
4557         }
4558 }