]> git.lizzy.rs Git - minetest.git/blob - src/game.cpp
Added an experimental "far view" thing. Doesn't work exactly like it should and not...
[minetest.git] / src / game.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "common_irrlicht.h"
21 #include "game.h"
22 #include "client.h"
23 #include "server.h"
24 #include "guiPauseMenu.h"
25 #include "guiPasswordChange.h"
26 #include "guiInventoryMenu.h"
27 #include "guiTextInputMenu.h"
28 #include "guiFurnaceMenu.h"
29 #include "materials.h"
30 #include "config.h"
31 #include "clouds.h"
32 #include "keycode.h"
33 #include "farmesh.h"
34
35 /*
36         Setting this to 1 enables a special camera mode that forces
37         the renderers to think that the camera statically points from
38         the starting place to a static direction.
39
40         This allows one to move around with the player and see what
41         is actually drawn behind solid things and behind the player.
42 */
43 #define FIELD_OF_VIEW_TEST 0
44
45
46 MapDrawControl draw_control;
47
48 // Chat data
49 struct ChatLine
50 {
51         ChatLine():
52                 age(0.0)
53         {
54         }
55         ChatLine(const std::wstring &a_text):
56                 age(0.0),
57                 text(a_text)
58         {
59         }
60         float age;
61         std::wstring text;
62 };
63
64 /*
65         Inventory stuff
66 */
67
68 // Inventory actions from the menu are buffered here before sending
69 Queue<InventoryAction*> inventory_action_queue;
70 // This is a copy of the inventory that the client's environment has
71 Inventory local_inventory;
72
73 u16 g_selected_item = 0;
74
75 /*
76         Text input system
77 */
78
79 struct TextDestSign : public TextDest
80 {
81         TextDestSign(v3s16 blockpos, s16 id, Client *client)
82         {
83                 m_blockpos = blockpos;
84                 m_id = id;
85                 m_client = client;
86         }
87         void gotText(std::wstring text)
88         {
89                 std::string ntext = wide_to_narrow(text);
90                 dstream<<"Changing text of a sign object: "
91                                 <<ntext<<std::endl;
92                 m_client->sendSignText(m_blockpos, m_id, ntext);
93         }
94
95         v3s16 m_blockpos;
96         s16 m_id;
97         Client *m_client;
98 };
99
100 struct TextDestChat : public TextDest
101 {
102         TextDestChat(Client *client)
103         {
104                 m_client = client;
105         }
106         void gotText(std::wstring text)
107         {
108                 // Discard empty line
109                 if(text == L"")
110                         return;
111                 
112                 // Parse command (server command starts with "/#")
113                 if(text[0] == L'/' && text[1] != L'#')
114                 {
115                         std::wstring reply = L"Local: ";
116
117                         reply += L"Local commands not yet supported. "
118                                         L"Server prefix is \"/#\".";
119                         
120                         m_client->addChatMessage(reply);
121                         return;
122                 }
123
124                 // Send to others
125                 m_client->sendChatMessage(text);
126                 // Show locally
127                 m_client->addChatMessage(text);
128         }
129
130         Client *m_client;
131 };
132
133 struct TextDestSignNode : public TextDest
134 {
135         TextDestSignNode(v3s16 p, Client *client)
136         {
137                 m_p = p;
138                 m_client = client;
139         }
140         void gotText(std::wstring text)
141         {
142                 std::string ntext = wide_to_narrow(text);
143                 dstream<<"Changing text of a sign node: "
144                                 <<ntext<<std::endl;
145                 m_client->sendSignNodeText(m_p, ntext);
146         }
147
148         v3s16 m_p;
149         Client *m_client;
150 };
151
152 /*
153         Render distance feedback loop
154 */
155 void updateViewingRange(f32 frametime_in, Client *client)
156 {
157         if(draw_control.range_all == true)
158                 return;
159         
160         static f32 added_frametime = 0;
161         static s16 added_frames = 0;
162
163         added_frametime += frametime_in;
164         added_frames += 1;
165
166         // Actually this counter kind of sucks because frametime is busytime
167         static f32 counter = 0;
168         counter -= frametime_in;
169         if(counter > 0)
170                 return;
171         //counter = 0.1;
172         counter = 0.2;
173
174         /*dstream<<__FUNCTION_NAME
175                         <<": Collected "<<added_frames<<" frames, total of "
176                         <<added_frametime<<"s."<<std::endl;*/
177         
178         /*dstream<<"draw_control.blocks_drawn="
179                         <<draw_control.blocks_drawn
180                         <<", draw_control.blocks_would_have_drawn="
181                         <<draw_control.blocks_would_have_drawn
182                         <<std::endl;*/
183         
184         float range_min = g_settings.getS16("viewing_range_nodes_min");
185         float range_max = g_settings.getS16("viewing_range_nodes_max");
186         
187         // Limit minimum to keep the feedback loop stable
188         if(range_min < 5)
189                 range_min = 5;
190         
191         draw_control.wanted_min_range = range_min;
192         //draw_control.wanted_max_blocks = (1.5*draw_control.blocks_drawn)+1;
193         draw_control.wanted_max_blocks = (1.5*draw_control.blocks_would_have_drawn)+1;
194         if(draw_control.wanted_max_blocks < 10)
195                 draw_control.wanted_max_blocks = 10;
196         
197         float block_draw_ratio = 1.0;
198         if(draw_control.blocks_would_have_drawn != 0)
199         {
200                 block_draw_ratio = (float)draw_control.blocks_drawn
201                         / (float)draw_control.blocks_would_have_drawn;
202         }
203
204         // Calculate the average frametime in the case that all wanted
205         // blocks had been drawn
206         f32 frametime = added_frametime / added_frames / block_draw_ratio;
207         
208         added_frametime = 0.0;
209         added_frames = 0;
210         
211         float wanted_fps = g_settings.getFloat("wanted_fps");
212         float wanted_frametime = 1.0 / wanted_fps;
213         
214         f32 wanted_frametime_change = wanted_frametime - frametime;
215         //dstream<<"wanted_frametime_change="<<wanted_frametime_change<<std::endl;
216         
217         // If needed frametime change is small, just return
218         if(fabs(wanted_frametime_change) < wanted_frametime*0.4)
219         {
220                 //dstream<<"ignoring small wanted_frametime_change"<<std::endl;
221                 return;
222         }
223
224         float range = draw_control.wanted_range;
225         float new_range = range;
226
227         static s16 range_old = 0;
228         static f32 frametime_old = 0;
229         
230         float d_range = range - range_old;
231         f32 d_frametime = frametime - frametime_old;
232         // A sane default of 30ms per 50 nodes of range
233         static f32 time_per_range = 30. / 50;
234         if(d_range != 0)
235         {
236                 time_per_range = d_frametime / d_range;
237         }
238         
239         // The minimum allowed calculated frametime-range derivative:
240         // Practically this sets the maximum speed of changing the range.
241         // The lower this value, the higher the maximum changing speed.
242         // A low value here results in wobbly range (0.001)
243         // A high value here results in slow changing range (0.0025)
244         // SUGG: This could be dynamically adjusted so that when
245         //       the camera is turning, this is lower
246         //float min_time_per_range = 0.0015;
247         float min_time_per_range = 0.0010;
248         //float min_time_per_range = 0.05 / range;
249         if(time_per_range < min_time_per_range)
250         {
251                 time_per_range = min_time_per_range;
252                 //dstream<<"time_per_range="<<time_per_range<<" (min)"<<std::endl;
253         }
254         else
255         {
256                 //dstream<<"time_per_range="<<time_per_range<<std::endl;
257         }
258
259         f32 wanted_range_change = wanted_frametime_change / time_per_range;
260         // Dampen the change a bit to kill oscillations
261         //wanted_range_change *= 0.9;
262         //wanted_range_change *= 0.75;
263         wanted_range_change *= 0.5;
264         //dstream<<"wanted_range_change="<<wanted_range_change<<std::endl;
265
266         // If needed range change is very small, just return
267         if(fabs(wanted_range_change) < 0.001)
268         {
269                 //dstream<<"ignoring small wanted_range_change"<<std::endl;
270                 return;
271         }
272
273         new_range += wanted_range_change;
274         
275         //float new_range_unclamped = new_range;
276         if(new_range < range_min)
277                 new_range = range_min;
278         if(new_range > range_max)
279                 new_range = range_max;
280         
281         /*dstream<<"new_range="<<new_range_unclamped
282                         <<", clamped to "<<new_range<<std::endl;*/
283
284         draw_control.wanted_range = new_range;
285
286         range_old = new_range;
287         frametime_old = frametime;
288 }
289
290 /*
291         Hotbar draw routine
292 */
293 void draw_hotbar(video::IVideoDriver *driver, gui::IGUIFont *font,
294                 v2s32 centerlowerpos, s32 imgsize, s32 itemcount,
295                 Inventory *inventory, s32 halfheartcount)
296 {
297         InventoryList *mainlist = inventory->getList("main");
298         if(mainlist == NULL)
299         {
300                 dstream<<"WARNING: draw_hotbar(): mainlist == NULL"<<std::endl;
301                 return;
302         }
303         
304         s32 padding = imgsize/12;
305         //s32 height = imgsize + padding*2;
306         s32 width = itemcount*(imgsize+padding*2);
307         
308         // Position of upper left corner of bar
309         v2s32 pos = centerlowerpos - v2s32(width/2, imgsize+padding*2);
310         
311         // Draw background color
312         /*core::rect<s32> barrect(0,0,width,height);
313         barrect += pos;
314         video::SColor bgcolor(255,128,128,128);
315         driver->draw2DRectangle(bgcolor, barrect, NULL);*/
316
317         core::rect<s32> imgrect(0,0,imgsize,imgsize);
318
319         for(s32 i=0; i<itemcount; i++)
320         {
321                 InventoryItem *item = mainlist->getItem(i);
322                 
323                 core::rect<s32> rect = imgrect + pos
324                                 + v2s32(padding+i*(imgsize+padding*2), padding);
325                 
326                 if(g_selected_item == i)
327                 {
328                         driver->draw2DRectangle(video::SColor(255,255,0,0),
329                                         core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*padding,
330                                                         rect.LowerRightCorner + v2s32(1,1)*padding),
331                                         NULL);
332                 }
333                 else
334                 {
335                         video::SColor bgcolor2(128,0,0,0);
336                         driver->draw2DRectangle(bgcolor2, rect, NULL);
337                 }
338
339                 if(item != NULL)
340                 {
341                         drawInventoryItem(driver, font, item, rect, NULL);
342                 }
343         }
344         
345         /*
346                 Draw hearts
347         */
348         {
349                 video::ITexture *heart_texture =
350                                 driver->getTexture(getTexturePath("heart.png").c_str());
351                 v2s32 p = pos + v2s32(0, -20);
352                 for(s32 i=0; i<halfheartcount/2; i++)
353                 {
354                         const video::SColor color(255,255,255,255);
355                         const video::SColor colors[] = {color,color,color,color};
356                         core::rect<s32> rect(0,0,16,16);
357                         rect += p;
358                         driver->draw2DImage(heart_texture, rect,
359                                 core::rect<s32>(core::position2d<s32>(0,0),
360                                 core::dimension2di(heart_texture->getOriginalSize())),
361                                 NULL, colors, true);
362                         p += v2s32(20,0);
363                 }
364                 if(halfheartcount % 2 == 1)
365                 {
366                         const video::SColor color(255,255,255,255);
367                         const video::SColor colors[] = {color,color,color,color};
368                         core::rect<s32> rect(0,0,16/2,16);
369                         rect += p;
370                         core::dimension2di srcd(heart_texture->getOriginalSize());
371                         srcd.Width /= 2;
372                         driver->draw2DImage(heart_texture, rect,
373                                 core::rect<s32>(core::position2d<s32>(0,0), srcd),
374                                 NULL, colors, true);
375                         p += v2s32(20,0);
376                 }
377         }
378 }
379
380 /*
381         Find what the player is pointing at
382 */
383 void getPointedNode(Client *client, v3f player_position,
384                 v3f camera_direction, v3f camera_position,
385                 bool &nodefound, core::line3d<f32> shootline,
386                 v3s16 &nodepos, v3s16 &neighbourpos,
387                 core::aabbox3d<f32> &nodehilightbox,
388                 f32 d)
389 {
390         f32 mindistance = BS * 1001;
391         
392         v3s16 pos_i = floatToInt(player_position, BS);
393
394         /*std::cout<<"pos_i=("<<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z<<")"
395                         <<std::endl;*/
396
397         s16 a = d;
398         s16 ystart = pos_i.Y + 0 - (camera_direction.Y<0 ? a : 1);
399         s16 zstart = pos_i.Z - (camera_direction.Z<0 ? a : 1);
400         s16 xstart = pos_i.X - (camera_direction.X<0 ? a : 1);
401         s16 yend = pos_i.Y + 1 + (camera_direction.Y>0 ? a : 1);
402         s16 zend = pos_i.Z + (camera_direction.Z>0 ? a : 1);
403         s16 xend = pos_i.X + (camera_direction.X>0 ? a : 1);
404         
405         for(s16 y = ystart; y <= yend; y++)
406         for(s16 z = zstart; z <= zend; z++)
407         for(s16 x = xstart; x <= xend; x++)
408         {
409                 MapNode n;
410                 try
411                 {
412                         n = client->getNode(v3s16(x,y,z));
413                         if(content_pointable(n.d) == false)
414                                 continue;
415                 }
416                 catch(InvalidPositionException &e)
417                 {
418                         continue;
419                 }
420
421                 v3s16 np(x,y,z);
422                 v3f npf = intToFloat(np, BS);
423                 
424                 f32 d = 0.01;
425                 
426                 v3s16 dirs[6] = {
427                         v3s16(0,0,1), // back
428                         v3s16(0,1,0), // top
429                         v3s16(1,0,0), // right
430                         v3s16(0,0,-1), // front
431                         v3s16(0,-1,0), // bottom
432                         v3s16(-1,0,0), // left
433                 };
434                 
435                 /*
436                         Meta-objects
437                 */
438                 if(n.d == CONTENT_TORCH)
439                 {
440                         v3s16 dir = unpackDir(n.dir);
441                         v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
442                         dir_f *= BS/2 - BS/6 - BS/20;
443                         v3f cpf = npf + dir_f;
444                         f32 distance = (cpf - camera_position).getLength();
445
446                         core::aabbox3d<f32> box;
447                         
448                         // bottom
449                         if(dir == v3s16(0,-1,0))
450                         {
451                                 box = core::aabbox3d<f32>(
452                                         npf - v3f(BS/6, BS/2, BS/6),
453                                         npf + v3f(BS/6, -BS/2+BS/3*2, BS/6)
454                                 );
455                         }
456                         // top
457                         else if(dir == v3s16(0,1,0))
458                         {
459                                 box = core::aabbox3d<f32>(
460                                         npf - v3f(BS/6, -BS/2+BS/3*2, BS/6),
461                                         npf + v3f(BS/6, BS/2, BS/6)
462                                 );
463                         }
464                         // side
465                         else
466                         {
467                                 box = core::aabbox3d<f32>(
468                                         cpf - v3f(BS/6, BS/3, BS/6),
469                                         cpf + v3f(BS/6, BS/3, BS/6)
470                                 );
471                         }
472
473                         if(distance < mindistance)
474                         {
475                                 if(box.intersectsWithLine(shootline))
476                                 {
477                                         nodefound = true;
478                                         nodepos = np;
479                                         neighbourpos = np;
480                                         mindistance = distance;
481                                         nodehilightbox = box;
482                                 }
483                         }
484                 }
485                 else if(n.d == CONTENT_SIGN_WALL)
486                 {
487                         v3s16 dir = unpackDir(n.dir);
488                         v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
489                         dir_f *= BS/2 - BS/6 - BS/20;
490                         v3f cpf = npf + dir_f;
491                         f32 distance = (cpf - camera_position).getLength();
492
493                         v3f vertices[4] =
494                         {
495                                 v3f(BS*0.42,-BS*0.35,-BS*0.4),
496                                 v3f(BS*0.49, BS*0.35, BS*0.4),
497                         };
498
499                         for(s32 i=0; i<2; i++)
500                         {
501                                 if(dir == v3s16(1,0,0))
502                                         vertices[i].rotateXZBy(0);
503                                 if(dir == v3s16(-1,0,0))
504                                         vertices[i].rotateXZBy(180);
505                                 if(dir == v3s16(0,0,1))
506                                         vertices[i].rotateXZBy(90);
507                                 if(dir == v3s16(0,0,-1))
508                                         vertices[i].rotateXZBy(-90);
509                                 if(dir == v3s16(0,-1,0))
510                                         vertices[i].rotateXYBy(-90);
511                                 if(dir == v3s16(0,1,0))
512                                         vertices[i].rotateXYBy(90);
513
514                                 vertices[i] += npf;
515                         }
516
517                         core::aabbox3d<f32> box;
518
519                         box = core::aabbox3d<f32>(vertices[0]);
520                         box.addInternalPoint(vertices[1]);
521
522                         if(distance < mindistance)
523                         {
524                                 if(box.intersectsWithLine(shootline))
525                                 {
526                                         nodefound = true;
527                                         nodepos = np;
528                                         neighbourpos = np;
529                                         mindistance = distance;
530                                         nodehilightbox = box;
531                                 }
532                         }
533                 }
534                 /*
535                         Regular blocks
536                 */
537                 else
538                 {
539                         for(u16 i=0; i<6; i++)
540                         {
541                                 v3f dir_f = v3f(dirs[i].X,
542                                                 dirs[i].Y, dirs[i].Z);
543                                 v3f centerpoint = npf + dir_f * BS/2;
544                                 f32 distance =
545                                                 (centerpoint - camera_position).getLength();
546                                 
547                                 if(distance < mindistance)
548                                 {
549                                         core::CMatrix4<f32> m;
550                                         m.buildRotateFromTo(v3f(0,0,1), dir_f);
551
552                                         // This is the back face
553                                         v3f corners[2] = {
554                                                 v3f(BS/2, BS/2, BS/2),
555                                                 v3f(-BS/2, -BS/2, BS/2+d)
556                                         };
557                                         
558                                         for(u16 j=0; j<2; j++)
559                                         {
560                                                 m.rotateVect(corners[j]);
561                                                 corners[j] += npf;
562                                         }
563
564                                         core::aabbox3d<f32> facebox(corners[0]);
565                                         facebox.addInternalPoint(corners[1]);
566
567                                         if(facebox.intersectsWithLine(shootline))
568                                         {
569                                                 nodefound = true;
570                                                 nodepos = np;
571                                                 neighbourpos = np + dirs[i];
572                                                 mindistance = distance;
573
574                                                 //nodehilightbox = facebox;
575
576                                                 const float d = 0.502;
577                                                 core::aabbox3d<f32> nodebox
578                                                                 (-BS*d, -BS*d, -BS*d, BS*d, BS*d, BS*d);
579                                                 v3f nodepos_f = intToFloat(nodepos, BS);
580                                                 nodebox.MinEdge += nodepos_f;
581                                                 nodebox.MaxEdge += nodepos_f;
582                                                 nodehilightbox = nodebox;
583                                         }
584                                 } // if distance < mindistance
585                         } // for dirs
586                 } // regular block
587         } // for coords
588 }
589
590 void update_skybox(video::IVideoDriver* driver,
591                 scene::ISceneManager* smgr, scene::ISceneNode* &skybox,
592                 float brightness)
593 {
594         if(skybox)
595         {
596                 skybox->remove();
597         }
598         
599         // Disable skybox if FarMesh is enabled
600         if(g_settings.getBool("enable_farmesh"))
601                 return;
602         
603         if(brightness >= 0.5)
604         {
605                 skybox = smgr->addSkyBoxSceneNode(
606                         driver->getTexture(getTexturePath("skybox2.png").c_str()),
607                         driver->getTexture(getTexturePath("skybox3.png").c_str()),
608                         driver->getTexture(getTexturePath("skybox1.png").c_str()),
609                         driver->getTexture(getTexturePath("skybox1.png").c_str()),
610                         driver->getTexture(getTexturePath("skybox1.png").c_str()),
611                         driver->getTexture(getTexturePath("skybox1.png").c_str()));
612         }
613         else if(brightness >= 0.2)
614         {
615                 skybox = smgr->addSkyBoxSceneNode(
616                         driver->getTexture(getTexturePath("skybox2_dawn.png").c_str()),
617                         driver->getTexture(getTexturePath("skybox3_dawn.png").c_str()),
618                         driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
619                         driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
620                         driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
621                         driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()));
622         }
623         else
624         {
625                 skybox = smgr->addSkyBoxSceneNode(
626                         driver->getTexture(getTexturePath("skybox2_night.png").c_str()),
627                         driver->getTexture(getTexturePath("skybox3_night.png").c_str()),
628                         driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
629                         driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
630                         driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
631                         driver->getTexture(getTexturePath("skybox1_night.png").c_str()));
632         }
633 }
634
635 void the_game(
636         bool &kill,
637         bool random_input,
638         InputHandler *input,
639         IrrlichtDevice *device,
640         gui::IGUIFont* font,
641         std::string map_dir,
642         std::string playername,
643         std::string password,
644         std::string address,
645         u16 port,
646         std::wstring &error_message
647 )
648 {
649         video::IVideoDriver* driver = device->getVideoDriver();
650         scene::ISceneManager* smgr = device->getSceneManager();
651
652         v2u32 screensize(0,0);
653         v2u32 last_screensize(0,0);
654         screensize = driver->getScreenSize();
655
656         const s32 hotbar_itemcount = 8;
657         const s32 hotbar_imagesize = 36;
658         
659         // The color of the sky
660
661         //video::SColor skycolor = video::SColor(255,140,186,250);
662
663         video::SColor bgcolor_bright = video::SColor(255,170,200,230);
664
665         /*
666                 Draw "Loading" screen
667         */
668         const wchar_t *loadingtext = L"Loading and connecting...";
669         u32 text_height = font->getDimension(loadingtext).Height;
670         core::vector2d<s32> center(screensize.X/2, screensize.Y/2);
671         core::vector2d<s32> textsize(300, text_height);
672         core::rect<s32> textrect(center - textsize/2, center + textsize/2);
673
674         gui::IGUIStaticText *gui_loadingtext = guienv->addStaticText(
675                         loadingtext, textrect, false, false);
676         gui_loadingtext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
677
678         driver->beginScene(true, true, video::SColor(255,0,0,0));
679         guienv->drawAll();
680         driver->endScene();
681
682         
683         /*
684                 Create server.
685                 SharedPtr will delete it when it goes out of scope.
686         */
687         SharedPtr<Server> server;
688         if(address == ""){
689                 std::cout<<DTIME<<"Creating server"<<std::endl;
690                 server = new Server(map_dir);
691                 server->start(port);
692         }
693         
694         /*
695                 Create client
696         */
697
698         std::cout<<DTIME<<"Creating client"<<std::endl;
699         Client client(device, playername.c_str(), password, draw_control);
700                         
701         Address connect_address(0,0,0,0, port);
702         try{
703                 if(address == "")
704                         //connect_address.Resolve("localhost");
705                         connect_address.setAddress(127,0,0,1);
706                 else
707                         connect_address.Resolve(address.c_str());
708         }
709         catch(ResolveError &e)
710         {
711                 std::cout<<DTIME<<"Couldn't resolve address"<<std::endl;
712                 //return 0;
713                 error_message = L"Couldn't resolve address";
714                 gui_loadingtext->remove();
715                 return;
716         }
717
718         /*
719                 Attempt to connect to the server
720         */
721         
722         dstream<<DTIME<<"Connecting to server at ";
723         connect_address.print(&dstream);
724         dstream<<std::endl;
725         client.connect(connect_address);
726
727         bool could_connect = false;
728         
729         try{
730                 float time_counter = 0.0;
731                 for(;;)
732                 {
733                         if(client.connectedAndInitialized())
734                         {
735                                 could_connect = true;
736                                 break;
737                         }
738                         if(client.accessDenied())
739                         {
740                                 break;
741                         }
742                         // Wait for 10 seconds
743                         if(time_counter >= 10.0)
744                         {
745                                 break;
746                         }
747
748                         // Update screen
749                         driver->beginScene(true, true, video::SColor(255,0,0,0));
750                         guienv->drawAll();
751                         driver->endScene();
752
753                         // Update client and server
754
755                         client.step(0.1);
756
757                         if(server != NULL)
758                                 server->step(0.1);
759                         
760                         // Delay a bit
761                         sleep_ms(100);
762                         time_counter += 0.1;
763                 }
764         }
765         catch(con::PeerNotFoundException &e)
766         {}
767
768         if(could_connect == false)
769         {
770                 if(client.accessDenied())
771                 {
772                         error_message = L"Access denied. Reason: "
773                                         +client.accessDeniedReason();
774                         std::cout<<DTIME<<wide_to_narrow(error_message)<<std::endl;
775                 }
776                 else
777                 {
778                         error_message = L"Connection timed out.";
779                         std::cout<<DTIME<<"Timed out."<<std::endl;
780                 }
781                 gui_loadingtext->remove();
782                 return;
783         }
784
785         /*
786                 Create skybox
787         */
788         float old_brightness = 1.0;
789         scene::ISceneNode* skybox = NULL;
790         update_skybox(driver, smgr, skybox, 1.0);
791         
792         /*
793                 Create the camera node
794         */
795
796         scene::ICameraSceneNode* camera = smgr->addCameraSceneNode(
797                 0, // Camera parent
798                 v3f(BS*100, BS*2, BS*100), // Look from
799                 v3f(BS*100+1, BS*2, BS*100), // Look to
800                 -1 // Camera ID
801         );
802
803         if(camera == NULL)
804         {
805                 error_message = L"Failed to create the camera node";
806                 return;
807         }
808
809         camera->setFOV(FOV_ANGLE);
810
811         // Just so big a value that everything rendered is visible
812         camera->setFarValue(100000*BS);
813         
814         f32 camera_yaw = 0; // "right/left"
815         f32 camera_pitch = 0; // "up/down"
816
817         /*
818                 Clouds
819         */
820         
821         float cloud_height = BS*100;
822         Clouds *clouds = NULL;
823         if(g_settings.getBool("enable_clouds"))
824         {
825                 clouds = new Clouds(smgr->getRootSceneNode(), smgr, -1,
826                                 cloud_height, time(0));
827         }
828         
829         /*
830                 FarMesh
831         */
832
833         FarMesh *farmesh = NULL;
834         if(g_settings.getBool("enable_farmesh"))
835         {
836                 farmesh = new FarMesh(smgr->getRootSceneNode(), smgr, -1, client.getMapSeed());
837         }
838
839         /*
840                 Move into game
841         */
842         
843         gui_loadingtext->remove();
844
845         /*
846                 Add some gui stuff
847         */
848
849         // First line of debug text
850         gui::IGUIStaticText *guitext = guienv->addStaticText(
851                         L"Minetest-c55",
852                         core::rect<s32>(5, 5, 795, 5+text_height),
853                         false, false);
854         // Second line of debug text
855         gui::IGUIStaticText *guitext2 = guienv->addStaticText(
856                         L"",
857                         core::rect<s32>(5, 5+(text_height+5)*1, 795, (5+text_height)*2),
858                         false, false);
859         
860         // At the middle of the screen
861         // Object infos are shown in this
862         gui::IGUIStaticText *guitext_info = guienv->addStaticText(
863                         L"",
864                         core::rect<s32>(0,0,400,text_height+5) + v2s32(100,200),
865                         false, false);
866         
867         // Chat text
868         gui::IGUIStaticText *guitext_chat = guienv->addStaticText(
869                         L"",
870                         core::rect<s32>(0,0,0,0),
871                         false, false); // Disable word wrap as of now
872                         //false, true);
873         //guitext_chat->setBackgroundColor(video::SColor(96,0,0,0));
874         core::list<ChatLine> chat_lines;
875         
876         /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
877                         (guienv, NULL, v2s32(10, 70), 5, &local_inventory);*/
878         /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
879                         (guienv, NULL, v2s32(0, 0), quickinv_itemcount, &local_inventory);*/
880         
881         // Test the text input system
882         /*(new GUITextInputMenu(guienv, guiroot, -1, &g_menumgr,
883                         NULL))->drop();*/
884         /*GUIMessageMenu *menu =
885                         new GUIMessageMenu(guienv, guiroot, -1, 
886                                 &g_menumgr,
887                                 L"Asd");
888         menu->drop();*/
889         
890         // Launch pause menu
891         (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
892                         &g_menumgr))->drop();
893         
894         // Enable texts
895         /*guitext2->setVisible(true);
896         guitext_info->setVisible(true);
897         guitext_chat->setVisible(true);*/
898
899         //s32 guitext_chat_pad_bottom = 70;
900
901         /*
902                 Some statistics are collected in these
903         */
904         u32 drawtime = 0;
905         u32 beginscenetime = 0;
906         u32 scenetime = 0;
907         u32 endscenetime = 0;
908         
909         // A test
910         //throw con::PeerNotFoundException("lol");
911
912         core::list<float> frametime_log;
913
914         float damage_flash_timer = 0;
915         
916         bool invert_mouse = g_settings.getBool("invert_mouse");
917
918         /*
919                 Main loop
920         */
921
922         bool first_loop_after_window_activation = true;
923
924         // TODO: Convert the static interval timers to these
925         // Interval limiter for profiler
926         IntervalLimiter m_profiler_interval;
927
928         // Time is in milliseconds
929         // NOTE: getRealTime() causes strange problems in wine (imprecision?)
930         // NOTE: So we have to use getTime() and call run()s between them
931         u32 lasttime = device->getTimer()->getTime();
932
933         while(device->run() && kill == false)
934         {
935                 if(g_gamecallback->disconnect_requested)
936                 {
937                         g_gamecallback->disconnect_requested = false;
938                         break;
939                 }
940
941                 if(g_gamecallback->changepassword_requested)
942                 {
943                         (new GUIPasswordChange(guienv, guiroot, -1,
944                                 &g_menumgr, &client))->drop();
945                         g_gamecallback->changepassword_requested = false;
946                 }
947
948                 /*
949                         Process TextureSource's queue
950                 */
951                 ((TextureSource*)g_texturesource)->processQueue();
952
953                 /*
954                         Random calculations
955                 */
956                 last_screensize = screensize;
957                 screensize = driver->getScreenSize();
958                 v2s32 displaycenter(screensize.X/2,screensize.Y/2);
959                 //bool screensize_changed = screensize != last_screensize;
960                 
961                 // Hilight boxes collected during the loop and displayed
962                 core::list< core::aabbox3d<f32> > hilightboxes;
963                 
964                 // Info text
965                 std::wstring infotext;
966
967                 // When screen size changes, update positions and sizes of stuff
968                 /*if(screensize_changed)
969                 {
970                         v2s32 pos(displaycenter.X-((quickinv_itemcount-1)*quickinv_spacing+quickinv_size)/2, screensize.Y-quickinv_spacing);
971                         quick_inventory->updatePosition(pos);
972                 }*/
973
974                 //TimeTaker //timer1("//timer1");
975                 
976                 // Time of frame without fps limit
977                 float busytime;
978                 u32 busytime_u32;
979                 {
980                         // not using getRealTime is necessary for wine
981                         u32 time = device->getTimer()->getTime();
982                         if(time > lasttime)
983                                 busytime_u32 = time - lasttime;
984                         else
985                                 busytime_u32 = 0;
986                         busytime = busytime_u32 / 1000.0;
987                 }
988
989                 //std::cout<<"busytime_u32="<<busytime_u32<<std::endl;
990         
991                 // Necessary for device->getTimer()->getTime()
992                 device->run();
993
994                 /*
995                         Viewing range
996                 */
997                 
998                 updateViewingRange(busytime, &client);
999                 
1000                 /*
1001                         FPS limiter
1002                 */
1003
1004                 {
1005                         float fps_max = g_settings.getFloat("fps_max");
1006                         u32 frametime_min = 1000./fps_max;
1007                         
1008                         if(busytime_u32 < frametime_min)
1009                         {
1010                                 u32 sleeptime = frametime_min - busytime_u32;
1011                                 device->sleep(sleeptime);
1012                         }
1013                 }
1014
1015                 // Necessary for device->getTimer()->getTime()
1016                 device->run();
1017
1018                 /*
1019                         Time difference calculation
1020                 */
1021                 f32 dtime; // in seconds
1022                 
1023                 u32 time = device->getTimer()->getTime();
1024                 if(time > lasttime)
1025                         dtime = (time - lasttime) / 1000.0;
1026                 else
1027                         dtime = 0;
1028                 lasttime = time;
1029
1030                 /*
1031                         Log frametime for visualization
1032                 */
1033                 frametime_log.push_back(dtime);
1034                 if(frametime_log.size() > 100)
1035                 {
1036                         core::list<float>::Iterator i = frametime_log.begin();
1037                         frametime_log.erase(i);
1038                 }
1039
1040                 /*
1041                         Visualize frametime in terminal
1042                 */
1043                 /*for(u32 i=0; i<dtime*400; i++)
1044                         std::cout<<"X";
1045                 std::cout<<std::endl;*/
1046
1047                 /*
1048                         Time average and jitter calculation
1049                 */
1050
1051                 static f32 dtime_avg1 = 0.0;
1052                 dtime_avg1 = dtime_avg1 * 0.98 + dtime * 0.02;
1053                 f32 dtime_jitter1 = dtime - dtime_avg1;
1054
1055                 static f32 dtime_jitter1_max_sample = 0.0;
1056                 static f32 dtime_jitter1_max_fraction = 0.0;
1057                 {
1058                         static f32 jitter1_max = 0.0;
1059                         static f32 counter = 0.0;
1060                         if(dtime_jitter1 > jitter1_max)
1061                                 jitter1_max = dtime_jitter1;
1062                         counter += dtime;
1063                         if(counter > 0.0)
1064                         {
1065                                 counter -= 3.0;
1066                                 dtime_jitter1_max_sample = jitter1_max;
1067                                 dtime_jitter1_max_fraction
1068                                                 = dtime_jitter1_max_sample / (dtime_avg1+0.001);
1069                                 jitter1_max = 0.0;
1070                         }
1071                 }
1072                 
1073                 /*
1074                         Busytime average and jitter calculation
1075                 */
1076
1077                 static f32 busytime_avg1 = 0.0;
1078                 busytime_avg1 = busytime_avg1 * 0.98 + busytime * 0.02;
1079                 f32 busytime_jitter1 = busytime - busytime_avg1;
1080                 
1081                 static f32 busytime_jitter1_max_sample = 0.0;
1082                 static f32 busytime_jitter1_min_sample = 0.0;
1083                 {
1084                         static f32 jitter1_max = 0.0;
1085                         static f32 jitter1_min = 0.0;
1086                         static f32 counter = 0.0;
1087                         if(busytime_jitter1 > jitter1_max)
1088                                 jitter1_max = busytime_jitter1;
1089                         if(busytime_jitter1 < jitter1_min)
1090                                 jitter1_min = busytime_jitter1;
1091                         counter += dtime;
1092                         if(counter > 0.0){
1093                                 counter -= 3.0;
1094                                 busytime_jitter1_max_sample = jitter1_max;
1095                                 busytime_jitter1_min_sample = jitter1_min;
1096                                 jitter1_max = 0.0;
1097                                 jitter1_min = 0.0;
1098                         }
1099                 }
1100                 
1101                 /*
1102                         Debug info for client
1103                 */
1104                 {
1105                         static float counter = 0.0;
1106                         counter -= dtime;
1107                         if(counter < 0)
1108                         {
1109                                 counter = 30.0;
1110                                 client.printDebugInfo(std::cout);
1111                         }
1112                 }
1113
1114                 /*
1115                         Profiler
1116                 */
1117                 float profiler_print_interval =
1118                                 g_settings.getFloat("profiler_print_interval");
1119                 if(profiler_print_interval != 0)
1120                 {
1121                         if(m_profiler_interval.step(0.030, profiler_print_interval))
1122                         {
1123                                 dstream<<"Profiler:"<<std::endl;
1124                                 g_profiler.print(dstream);
1125                                 g_profiler.clear();
1126                         }
1127                 }
1128
1129                 /*
1130                         Direct handling of user input
1131                 */
1132                 
1133                 // Reset input if window not active or some menu is active
1134                 if(device->isWindowActive() == false || noMenuActive() == false)
1135                 {
1136                         input->clear();
1137                 }
1138
1139                 // Input handler step() (used by the random input generator)
1140                 input->step(dtime);
1141
1142                 /*
1143                         Launch menus according to keys
1144                 */
1145                 if(input->wasKeyDown(getKeySetting("keymap_inventory")))
1146                 {
1147                         dstream<<DTIME<<"the_game: "
1148                                         <<"Launching inventory"<<std::endl;
1149                         
1150                         GUIInventoryMenu *menu =
1151                                 new GUIInventoryMenu(guienv, guiroot, -1,
1152                                         &g_menumgr, v2s16(8,7),
1153                                         client.getInventoryContext(),
1154                                         &client);
1155
1156                         core::array<GUIInventoryMenu::DrawSpec> draw_spec;
1157                         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1158                                         "list", "current_player", "main",
1159                                         v2s32(0, 3), v2s32(8, 4)));
1160                         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1161                                         "list", "current_player", "craft",
1162                                         v2s32(3, 0), v2s32(3, 3)));
1163                         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1164                                         "list", "current_player", "craftresult",
1165                                         v2s32(7, 1), v2s32(1, 1)));
1166
1167                         menu->setDrawSpec(draw_spec);
1168
1169                         menu->drop();
1170                 }
1171                 else if(input->wasKeyDown(KEY_ESCAPE))
1172                 {
1173                         dstream<<DTIME<<"the_game: "
1174                                         <<"Launching pause menu"<<std::endl;
1175                         // It will delete itself by itself
1176                         (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
1177                                         &g_menumgr))->drop();
1178
1179                         // Move mouse cursor on top of the disconnect button
1180                         input->setMousePos(displaycenter.X, displaycenter.Y+25);
1181                 }
1182                 else if(input->wasKeyDown(getKeySetting("keymap_chat")))
1183                 {
1184                         TextDest *dest = new TextDestChat(&client);
1185
1186                         (new GUITextInputMenu(guienv, guiroot, -1,
1187                                         &g_menumgr, dest,
1188                                         L""))->drop();
1189                 }
1190
1191                 // Item selection with mouse wheel
1192                 {
1193                         s32 wheel = input->getMouseWheel();
1194                         u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1,
1195                                         hotbar_itemcount-1);
1196
1197                         if(wheel < 0)
1198                         {
1199                                 if(g_selected_item < max_item)
1200                                         g_selected_item++;
1201                                 else
1202                                         g_selected_item = 0;
1203                         }
1204                         else if(wheel > 0)
1205                         {
1206                                 if(g_selected_item > 0)
1207                                         g_selected_item--;
1208                                 else
1209                                         g_selected_item = max_item;
1210                         }
1211                 }
1212                 
1213                 // Item selection
1214                 for(u16 i=0; i<10; i++)
1215                 {
1216                         s32 keycode = irr::KEY_KEY_1 + i;
1217                         if(i == 9)
1218                                 keycode = irr::KEY_KEY_0;
1219                         if(input->wasKeyDown((irr::EKEY_CODE)keycode))
1220                         {
1221                                 if(i < PLAYER_INVENTORY_SIZE && i < hotbar_itemcount)
1222                                 {
1223                                         g_selected_item = i;
1224
1225                                         dstream<<DTIME<<"Selected item: "
1226                                                         <<g_selected_item<<std::endl;
1227                                 }
1228                         }
1229                 }
1230
1231                 // Viewing range selection
1232                 if(input->wasKeyDown(getKeySetting("keymap_rangeselect")))
1233                 {
1234                         if(draw_control.range_all)
1235                         {
1236                                 draw_control.range_all = false;
1237                                 dstream<<DTIME<<"Disabled full viewing range"<<std::endl;
1238                         }
1239                         else
1240                         {
1241                                 draw_control.range_all = true;
1242                                 dstream<<DTIME<<"Enabled full viewing range"<<std::endl;
1243                         }
1244                 }
1245
1246                 // Print debug stacks
1247                 if(input->wasKeyDown(getKeySetting("keymap_print_debug_stacks")))
1248                 {
1249                         dstream<<"-----------------------------------------"
1250                                         <<std::endl;
1251                         dstream<<DTIME<<"Printing debug stacks:"<<std::endl;
1252                         dstream<<"-----------------------------------------"
1253                                         <<std::endl;
1254                         debug_stacks_print();
1255                 }
1256
1257                 /*
1258                         Player speed control
1259                         TODO: Cache the keycodes from getKeySetting
1260                 */
1261                 
1262                 {
1263                         /*bool a_up,
1264                         bool a_down,
1265                         bool a_left,
1266                         bool a_right,
1267                         bool a_jump,
1268                         bool a_superspeed,
1269                         bool a_sneak,
1270                         float a_pitch,
1271                         float a_yaw*/
1272                         PlayerControl control(
1273                                 input->isKeyDown(getKeySetting("keymap_forward")),
1274                                 input->isKeyDown(getKeySetting("keymap_backward")),
1275                                 input->isKeyDown(getKeySetting("keymap_left")),
1276                                 input->isKeyDown(getKeySetting("keymap_right")),
1277                                 input->isKeyDown(getKeySetting("keymap_jump")),
1278                                 input->isKeyDown(getKeySetting("keymap_special1")),
1279                                 input->isKeyDown(getKeySetting("keymap_sneak")),
1280                                 camera_pitch,
1281                                 camera_yaw
1282                         );
1283                         client.setPlayerControl(control);
1284                 }
1285                 
1286                 /*
1287                         Run server
1288                 */
1289
1290                 if(server != NULL)
1291                 {
1292                         //TimeTaker timer("server->step(dtime)");
1293                         server->step(dtime);
1294                 }
1295
1296                 /*
1297                         Process environment
1298                 */
1299                 
1300                 {
1301                         //TimeTaker timer("client.step(dtime)");
1302                         client.step(dtime);
1303                         //client.step(dtime_avg1);
1304                 }
1305
1306                 // Read client events
1307                 for(;;)
1308                 {
1309                         ClientEvent event = client.getClientEvent();
1310                         if(event.type == CE_NONE)
1311                         {
1312                                 break;
1313                         }
1314                         else if(event.type == CE_PLAYER_DAMAGE)
1315                         {
1316                                 //u16 damage = event.player_damage.amount;
1317                                 //dstream<<"Player damage: "<<damage<<std::endl;
1318                                 damage_flash_timer = 0.05;
1319                         }
1320                         else if(event.type == CE_PLAYER_FORCE_MOVE)
1321                         {
1322                                 camera_yaw = event.player_force_move.yaw;
1323                                 camera_pitch = event.player_force_move.pitch;
1324                         }
1325                 }
1326                 
1327                 // Get player position
1328                 v3f player_position = client.getPlayerPosition();
1329
1330                 //TimeTaker //timer2("//timer2");
1331
1332                 /*
1333                         Mouse and camera control
1334                 */
1335                 
1336                 if((device->isWindowActive() && noMenuActive()) || random_input)
1337                 {
1338                         if(!random_input)
1339                                 device->getCursorControl()->setVisible(false);
1340
1341                         if(first_loop_after_window_activation){
1342                                 //std::cout<<"window active, first loop"<<std::endl;
1343                                 first_loop_after_window_activation = false;
1344                         }
1345                         else{
1346                                 s32 dx = input->getMousePos().X - displaycenter.X;
1347                                 s32 dy = input->getMousePos().Y - displaycenter.Y;
1348                                 if(invert_mouse)
1349                                         dy = -dy;
1350                                 //std::cout<<"window active, pos difference "<<dx<<","<<dy<<std::endl;
1351                                 
1352                                 /*const float keyspeed = 500;
1353                                 if(input->isKeyDown(irr::KEY_UP))
1354                                         dy -= dtime * keyspeed;
1355                                 if(input->isKeyDown(irr::KEY_DOWN))
1356                                         dy += dtime * keyspeed;
1357                                 if(input->isKeyDown(irr::KEY_LEFT))
1358                                         dx -= dtime * keyspeed;
1359                                 if(input->isKeyDown(irr::KEY_RIGHT))
1360                                         dx += dtime * keyspeed;*/
1361
1362                                 camera_yaw -= dx*0.2;
1363                                 camera_pitch += dy*0.2;
1364                                 if(camera_pitch < -89.5) camera_pitch = -89.5;
1365                                 if(camera_pitch > 89.5) camera_pitch = 89.5;
1366                         }
1367                         input->setMousePos(displaycenter.X, displaycenter.Y);
1368                 }
1369                 else{
1370                         device->getCursorControl()->setVisible(true);
1371
1372                         //std::cout<<"window inactive"<<std::endl;
1373                         first_loop_after_window_activation = true;
1374                 }
1375
1376                 camera_yaw = wrapDegrees(camera_yaw);
1377                 camera_pitch = wrapDegrees(camera_pitch);
1378                 
1379                 v3f camera_direction = v3f(0,0,1);
1380                 camera_direction.rotateYZBy(camera_pitch);
1381                 camera_direction.rotateXZBy(camera_yaw);
1382                 
1383                 // This is at the height of the eyes of the current figure
1384                 //v3f camera_position = player_position + v3f(0, BS+BS/2, 0);
1385                 // This is more like in minecraft
1386                 v3f camera_position = player_position + v3f(0, BS+BS*0.625, 0);
1387
1388                 camera->setPosition(camera_position);
1389                 // *100.0 helps in large map coordinates
1390                 camera->setTarget(camera_position + camera_direction * 100.0);
1391
1392                 if(FIELD_OF_VIEW_TEST){
1393                         client.updateCamera(v3f(0,0,0), v3f(0,0,1));
1394                 }
1395                 else{
1396                         //TimeTaker timer("client.updateCamera");
1397                         client.updateCamera(camera_position, camera_direction);
1398                 }
1399                 
1400                 //timer2.stop();
1401                 //TimeTaker //timer3("//timer3");
1402
1403                 /*
1404                         Calculate what block is the crosshair pointing to
1405                 */
1406                 
1407                 //u32 t1 = device->getTimer()->getRealTime();
1408                 
1409                 //f32 d = 4; // max. distance
1410                 f32 d = 4; // max. distance
1411                 core::line3d<f32> shootline(camera_position,
1412                                 camera_position + camera_direction * BS * (d+1));
1413
1414                 MapBlockObject *selected_object = client.getSelectedObject
1415                                 (d*BS, camera_position, shootline);
1416
1417                 ClientActiveObject *selected_active_object
1418                                 = client.getSelectedActiveObject
1419                                         (d*BS, camera_position, shootline);
1420
1421                 if(selected_object != NULL)
1422                 {
1423                         //dstream<<"Client returned selected_object != NULL"<<std::endl;
1424
1425                         core::aabbox3d<f32> box_on_map
1426                                         = selected_object->getSelectionBoxOnMap();
1427
1428                         hilightboxes.push_back(box_on_map);
1429
1430                         infotext = narrow_to_wide(selected_object->infoText());
1431
1432                         if(input->getLeftClicked())
1433                         {
1434                                 std::cout<<DTIME<<"Left-clicked object"<<std::endl;
1435                                 client.clickObject(0, selected_object->getBlock()->getPos(),
1436                                                 selected_object->getId(), g_selected_item);
1437                         }
1438                         else if(input->getRightClicked())
1439                         {
1440                                 std::cout<<DTIME<<"Right-clicked object"<<std::endl;
1441                                 /*
1442                                         Check if we want to modify the object ourselves
1443                                 */
1444                                 if(selected_object->getTypeId() == MAPBLOCKOBJECT_TYPE_SIGN)
1445                                 {
1446                                         dstream<<"Sign object right-clicked"<<std::endl;
1447                                         
1448                                         if(random_input == false)
1449                                         {
1450                                                 // Get a new text for it
1451
1452                                                 TextDest *dest = new TextDestSign(
1453                                                                 selected_object->getBlock()->getPos(),
1454                                                                 selected_object->getId(),
1455                                                                 &client);
1456
1457                                                 SignObject *sign_object = (SignObject*)selected_object;
1458
1459                                                 std::wstring wtext =
1460                                                                 narrow_to_wide(sign_object->getText());
1461
1462                                                 (new GUITextInputMenu(guienv, guiroot, -1,
1463                                                                 &g_menumgr, dest,
1464                                                                 wtext))->drop();
1465                                         }
1466                                 }
1467                                 /*
1468                                         Otherwise pass the event to the server as-is
1469                                 */
1470                                 else
1471                                 {
1472                                         client.clickObject(1, selected_object->getBlock()->getPos(),
1473                                                         selected_object->getId(), g_selected_item);
1474                                 }
1475                         }
1476                 }
1477                 else if(selected_active_object != NULL)
1478                 {
1479                         //dstream<<"Client returned selected_active_object != NULL"<<std::endl;
1480                         
1481                         core::aabbox3d<f32> *selection_box
1482                                         = selected_active_object->getSelectionBox();
1483                         // Box should exist because object was returned in the
1484                         // first place
1485                         assert(selection_box);
1486
1487                         v3f pos = selected_active_object->getPosition();
1488
1489                         core::aabbox3d<f32> box_on_map(
1490                                         selection_box->MinEdge + pos,
1491                                         selection_box->MaxEdge + pos
1492                         );
1493
1494                         hilightboxes.push_back(box_on_map);
1495
1496                         //infotext = narrow_to_wide("A ClientActiveObject");
1497                         infotext = narrow_to_wide(selected_active_object->infoText());
1498
1499                         if(input->getLeftClicked())
1500                         {
1501                                 std::cout<<DTIME<<"Left-clicked object"<<std::endl;
1502                                 client.clickActiveObject(0,
1503                                                 selected_active_object->getId(), g_selected_item);
1504                         }
1505                         else if(input->getRightClicked())
1506                         {
1507                                 std::cout<<DTIME<<"Right-clicked object"<<std::endl;
1508                         }
1509                 }
1510                 else // selected_object == NULL
1511                 {
1512
1513                 /*
1514                         Find out which node we are pointing at
1515                 */
1516                 
1517                 bool nodefound = false;
1518                 v3s16 nodepos;
1519                 v3s16 neighbourpos;
1520                 core::aabbox3d<f32> nodehilightbox;
1521
1522                 getPointedNode(&client, player_position,
1523                                 camera_direction, camera_position,
1524                                 nodefound, shootline,
1525                                 nodepos, neighbourpos,
1526                                 nodehilightbox, d);
1527         
1528                 static float nodig_delay_counter = 0.0;
1529
1530                 if(nodefound)
1531                 {
1532                         static v3s16 nodepos_old(-32768,-32768,-32768);
1533
1534                         static float dig_time = 0.0;
1535                         static u16 dig_index = 0;
1536                         
1537                         /*
1538                                 Visualize selection
1539                         */
1540
1541                         hilightboxes.push_back(nodehilightbox);
1542
1543                         /*
1544                                 Check information text of node
1545                         */
1546
1547                         NodeMetadata *meta = client.getNodeMetadata(nodepos);
1548                         if(meta)
1549                         {
1550                                 infotext = narrow_to_wide(meta->infoText());
1551                         }
1552                         
1553                         //MapNode node = client.getNode(nodepos);
1554
1555                         /*
1556                                 Handle digging
1557                         */
1558                         
1559                         if(input->getLeftReleased())
1560                         {
1561                                 client.clearTempMod(nodepos);
1562                                 dig_time = 0.0;
1563                         }
1564                         
1565                         if(nodig_delay_counter > 0.0)
1566                         {
1567                                 nodig_delay_counter -= dtime;
1568                         }
1569                         else
1570                         {
1571                                 if(nodepos != nodepos_old)
1572                                 {
1573                                         std::cout<<DTIME<<"Pointing at ("<<nodepos.X<<","
1574                                                         <<nodepos.Y<<","<<nodepos.Z<<")"<<std::endl;
1575
1576                                         if(nodepos_old != v3s16(-32768,-32768,-32768))
1577                                         {
1578                                                 client.clearTempMod(nodepos_old);
1579                                                 dig_time = 0.0;
1580                                         }
1581                                 }
1582
1583                                 if(input->getLeftClicked() ||
1584                                                 (input->getLeftState() && nodepos != nodepos_old))
1585                                 {
1586                                         dstream<<DTIME<<"Started digging"<<std::endl;
1587                                         client.groundAction(0, nodepos, neighbourpos, g_selected_item);
1588                                 }
1589                                 if(input->getLeftClicked())
1590                                 {
1591                                         client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, 0));
1592                                 }
1593                                 if(input->getLeftState())
1594                                 {
1595                                         MapNode n = client.getNode(nodepos);
1596                                 
1597                                         // Get tool name. Default is "" = bare hands
1598                                         std::string toolname = "";
1599                                         InventoryList *mlist = local_inventory.getList("main");
1600                                         if(mlist != NULL)
1601                                         {
1602                                                 InventoryItem *item = mlist->getItem(g_selected_item);
1603                                                 if(item && (std::string)item->getName() == "ToolItem")
1604                                                 {
1605                                                         ToolItem *titem = (ToolItem*)item;
1606                                                         toolname = titem->getToolName();
1607                                                 }
1608                                         }
1609
1610                                         // Get digging properties for material and tool
1611                                         u8 material = n.d;
1612                                         DiggingProperties prop =
1613                                                         getDiggingProperties(material, toolname);
1614                                         
1615                                         float dig_time_complete = 0.0;
1616
1617                                         if(prop.diggable == false)
1618                                         {
1619                                                 /*dstream<<"Material "<<(int)material
1620                                                                 <<" not diggable with \""
1621                                                                 <<toolname<<"\""<<std::endl;*/
1622                                                 // I guess nobody will wait for this long
1623                                                 dig_time_complete = 10000000.0;
1624                                         }
1625                                         else
1626                                         {
1627                                                 dig_time_complete = prop.time;
1628                                         }
1629                                         
1630                                         if(dig_time_complete >= 0.001)
1631                                         {
1632                                                 dig_index = (u16)((float)CRACK_ANIMATION_LENGTH
1633                                                                 * dig_time/dig_time_complete);
1634                                         }
1635                                         // This is for torches
1636                                         else
1637                                         {
1638                                                 dig_index = CRACK_ANIMATION_LENGTH;
1639                                         }
1640
1641                                         if(dig_index < CRACK_ANIMATION_LENGTH)
1642                                         {
1643                                                 //TimeTaker timer("client.setTempMod");
1644                                                 //dstream<<"dig_index="<<dig_index<<std::endl;
1645                                                 client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, dig_index));
1646                                         }
1647                                         else
1648                                         {
1649                                                 dstream<<DTIME<<"Digging completed"<<std::endl;
1650                                                 client.groundAction(3, nodepos, neighbourpos, g_selected_item);
1651                                                 client.clearTempMod(nodepos);
1652                                                 client.removeNode(nodepos);
1653
1654                                                 dig_time = 0;
1655
1656                                                 nodig_delay_counter = dig_time_complete
1657                                                                 / (float)CRACK_ANIMATION_LENGTH;
1658
1659                                                 // We don't want a corresponding delay to
1660                                                 // very time consuming nodes
1661                                                 if(nodig_delay_counter > 0.5)
1662                                                 {
1663                                                         nodig_delay_counter = 0.5;
1664                                                 }
1665                                                 // We want a slight delay to very little
1666                                                 // time consuming nodes
1667                                                 float mindelay = 0.15;
1668                                                 if(nodig_delay_counter < mindelay)
1669                                                 {
1670                                                         nodig_delay_counter = mindelay;
1671                                                 }
1672                                         }
1673
1674                                         dig_time += dtime;
1675                                 }
1676                         }
1677                         
1678                         if(input->getRightClicked())
1679                         {
1680                                 std::cout<<DTIME<<"Ground right-clicked"<<std::endl;
1681                                 
1682                                 if(meta && meta->typeId() == CONTENT_SIGN_WALL && !random_input)
1683                                 {
1684                                         dstream<<"Sign node right-clicked"<<std::endl;
1685                                         
1686                                         SignNodeMetadata *signmeta = (SignNodeMetadata*)meta;
1687                                         
1688                                         // Get a new text for it
1689
1690                                         TextDest *dest = new TextDestSignNode(nodepos, &client);
1691
1692                                         std::wstring wtext =
1693                                                         narrow_to_wide(signmeta->getText());
1694
1695                                         (new GUITextInputMenu(guienv, guiroot, -1,
1696                                                         &g_menumgr, dest,
1697                                                         wtext))->drop();
1698                                 }
1699                                 else if(meta && meta->typeId() == CONTENT_CHEST && !random_input)
1700                                 {
1701                                         dstream<<"Chest node right-clicked"<<std::endl;
1702                                         
1703                                         //ChestNodeMetadata *chestmeta = (ChestNodeMetadata*)meta;
1704
1705                                         std::string chest_inv_id;
1706                                         chest_inv_id += "nodemeta:";
1707                                         chest_inv_id += itos(nodepos.X);
1708                                         chest_inv_id += ",";
1709                                         chest_inv_id += itos(nodepos.Y);
1710                                         chest_inv_id += ",";
1711                                         chest_inv_id += itos(nodepos.Z);
1712                                         
1713                                         GUIInventoryMenu *menu =
1714                                                 new GUIInventoryMenu(guienv, guiroot, -1,
1715                                                         &g_menumgr, v2s16(8,9),
1716                                                         client.getInventoryContext(),
1717                                                         &client);
1718
1719                                         core::array<GUIInventoryMenu::DrawSpec> draw_spec;
1720                                         
1721                                         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1722                                                         "list", chest_inv_id, "0",
1723                                                         v2s32(0, 0), v2s32(8, 4)));
1724                                         draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1725                                                         "list", "current_player", "main",
1726                                                         v2s32(0, 5), v2s32(8, 4)));
1727
1728                                         menu->setDrawSpec(draw_spec);
1729
1730                                         menu->drop();
1731
1732                                 }
1733                                 else if(meta && meta->typeId() == CONTENT_FURNACE && !random_input)
1734                                 {
1735                                         dstream<<"Furnace node right-clicked"<<std::endl;
1736                                         
1737                                         GUIFurnaceMenu *menu =
1738                                                 new GUIFurnaceMenu(guienv, guiroot, -1,
1739                                                         &g_menumgr, nodepos, &client);
1740
1741                                         menu->drop();
1742
1743                                 }
1744                                 else
1745                                 {
1746                                         client.groundAction(1, nodepos, neighbourpos, g_selected_item);
1747                                 }
1748                         }
1749                         
1750                         nodepos_old = nodepos;
1751                 }
1752                 else{
1753                 }
1754
1755                 } // selected_object == NULL
1756                 
1757                 input->resetLeftClicked();
1758                 input->resetRightClicked();
1759                 
1760                 if(input->getLeftReleased())
1761                 {
1762                         std::cout<<DTIME<<"Left button released (stopped digging)"
1763                                         <<std::endl;
1764                         client.groundAction(2, v3s16(0,0,0), v3s16(0,0,0), 0);
1765                 }
1766                 if(input->getRightReleased())
1767                 {
1768                         //std::cout<<DTIME<<"Right released"<<std::endl;
1769                         // Nothing here
1770                 }
1771                 
1772                 input->resetLeftReleased();
1773                 input->resetRightReleased();
1774                 
1775                 /*
1776                         Calculate stuff for drawing
1777                 */
1778
1779                 camera->setAspectRatio((f32)screensize.X / (f32)screensize.Y);
1780                 
1781                 u32 daynight_ratio = client.getDayNightRatio();
1782                 u8 l = decode_light((daynight_ratio * LIGHT_SUN) / 1000);
1783                 video::SColor bgcolor = video::SColor(
1784                                 255,
1785                                 bgcolor_bright.getRed() * l / 255,
1786                                 bgcolor_bright.getGreen() * l / 255,
1787                                 bgcolor_bright.getBlue() * l / 255);
1788                                 /*skycolor.getRed() * l / 255,
1789                                 skycolor.getGreen() * l / 255,
1790                                 skycolor.getBlue() * l / 255);*/
1791
1792                 float brightness = (float)l/255.0;
1793
1794                 /*
1795                         Update skybox
1796                 */
1797                 if(fabs(brightness - old_brightness) > 0.01)
1798                         update_skybox(driver, smgr, skybox, brightness);
1799
1800                 /*
1801                         Update coulds
1802                 */
1803                 if(clouds)
1804                 {
1805                         clouds->step(dtime);
1806                         clouds->update(v2f(player_position.X, player_position.Z),
1807                                         0.05+brightness*0.95);
1808                 }
1809                 
1810                 /*
1811                         Update farmesh (TODO: Remove from here)
1812                 */
1813                 if(farmesh)
1814                 {
1815                         farmesh->step(dtime);
1816                         farmesh->update(v2f(player_position.X, player_position.Z),
1817                                         0.05+brightness*0.95);
1818                 }
1819                 
1820                 // Store brightness value
1821                 old_brightness = brightness;
1822
1823                 /*
1824                         Fog
1825                 */
1826                 
1827                 if(g_settings.getBool("enable_fog") == true)
1828                 {
1829                         f32 range = draw_control.wanted_range*BS + MAP_BLOCKSIZE*BS*1.5;
1830                         if(draw_control.range_all)
1831                                 range = 100000*BS;
1832                         if(range < 50*BS)
1833                                 range = range * 0.5 + 25*BS;
1834
1835                         driver->setFog(
1836                                 bgcolor,
1837                                 video::EFT_FOG_LINEAR,
1838                                 range*0.4,
1839                                 range*1.0,
1840                                 0.01,
1841                                 false, // pixel fog
1842                                 false // range fog
1843                         );
1844                 }
1845                 else
1846                 {
1847                         driver->setFog(
1848                                 bgcolor,
1849                                 video::EFT_FOG_LINEAR,
1850                                 100000*BS,
1851                                 110000*BS,
1852                                 0.01,
1853                                 false, // pixel fog
1854                                 false // range fog
1855                         );
1856                 }
1857
1858
1859                 /*
1860                         Update gui stuff (0ms)
1861                 */
1862
1863                 //TimeTaker guiupdatetimer("Gui updating");
1864                 
1865                 {
1866                         static float drawtime_avg = 0;
1867                         drawtime_avg = drawtime_avg * 0.95 + (float)drawtime*0.05;
1868                         static float beginscenetime_avg = 0;
1869                         beginscenetime_avg = beginscenetime_avg * 0.95 + (float)beginscenetime*0.05;
1870                         static float scenetime_avg = 0;
1871                         scenetime_avg = scenetime_avg * 0.95 + (float)scenetime*0.05;
1872                         static float endscenetime_avg = 0;
1873                         endscenetime_avg = endscenetime_avg * 0.95 + (float)endscenetime*0.05;
1874                         
1875                         char temptext[300];
1876                         snprintf(temptext, 300, "Minetest-c55 %s ("
1877                                         "R: range_all=%i"
1878                                         ")"
1879                                         " drawtime=%.0f, beginscenetime=%.0f"
1880                                         ", scenetime=%.0f, endscenetime=%.0f",
1881                                         VERSION_STRING,
1882                                         draw_control.range_all,
1883                                         drawtime_avg,
1884                                         beginscenetime_avg,
1885                                         scenetime_avg,
1886                                         endscenetime_avg
1887                                         );
1888                         
1889                         guitext->setText(narrow_to_wide(temptext).c_str());
1890                 }
1891                 
1892                 {
1893                         char temptext[300];
1894                         snprintf(temptext, 300,
1895                                         "(% .1f, % .1f, % .1f)"
1896                                         " (% .3f < btime_jitter < % .3f"
1897                                         ", dtime_jitter = % .1f %%"
1898                                         ", v_range = %.1f)",
1899                                         player_position.X/BS,
1900                                         player_position.Y/BS,
1901                                         player_position.Z/BS,
1902                                         busytime_jitter1_min_sample,
1903                                         busytime_jitter1_max_sample,
1904                                         dtime_jitter1_max_fraction * 100.0,
1905                                         draw_control.wanted_range
1906                                         );
1907
1908                         guitext2->setText(narrow_to_wide(temptext).c_str());
1909                 }
1910                 
1911                 {
1912                         guitext_info->setText(infotext.c_str());
1913                 }
1914                 
1915                 /*
1916                         Get chat messages from client
1917                 */
1918                 {
1919                         // Get new messages
1920                         std::wstring message;
1921                         while(client.getChatMessage(message))
1922                         {
1923                                 chat_lines.push_back(ChatLine(message));
1924                                 /*if(chat_lines.size() > 6)
1925                                 {
1926                                         core::list<ChatLine>::Iterator
1927                                                         i = chat_lines.begin();
1928                                         chat_lines.erase(i);
1929                                 }*/
1930                         }
1931                         // Append them to form the whole static text and throw
1932                         // it to the gui element
1933                         std::wstring whole;
1934                         // This will correspond to the line number counted from
1935                         // top to bottom, from size-1 to 0
1936                         s16 line_number = chat_lines.size();
1937                         // Count of messages to be removed from the top
1938                         u16 to_be_removed_count = 0;
1939                         for(core::list<ChatLine>::Iterator
1940                                         i = chat_lines.begin();
1941                                         i != chat_lines.end(); i++)
1942                         {
1943                                 // After this, line number is valid for this loop
1944                                 line_number--;
1945                                 // Increment age
1946                                 (*i).age += dtime;
1947                                 /*
1948                                         This results in a maximum age of 60*6 to the
1949                                         lowermost line and a maximum of 6 lines
1950                                 */
1951                                 float allowed_age = (6-line_number) * 60.0;
1952
1953                                 if((*i).age > allowed_age)
1954                                 {
1955                                         to_be_removed_count++;
1956                                         continue;
1957                                 }
1958                                 whole += (*i).text + L'\n';
1959                         }
1960                         for(u16 i=0; i<to_be_removed_count; i++)
1961                         {
1962                                 core::list<ChatLine>::Iterator
1963                                                 it = chat_lines.begin();
1964                                 chat_lines.erase(it);
1965                         }
1966                         guitext_chat->setText(whole.c_str());
1967
1968                         // Update gui element size and position
1969
1970                         /*core::rect<s32> rect(
1971                                         10,
1972                                         screensize.Y - guitext_chat_pad_bottom
1973                                                         - text_height*chat_lines.size(),
1974                                         screensize.X - 10,
1975                                         screensize.Y - guitext_chat_pad_bottom
1976                         );*/
1977                         core::rect<s32> rect(
1978                                         10,
1979                                         50,
1980                                         screensize.X - 10,
1981                                         50 + text_height*chat_lines.size()
1982                         );
1983
1984                         guitext_chat->setRelativePosition(rect);
1985
1986                         if(chat_lines.size() == 0)
1987                                 guitext_chat->setVisible(false);
1988                         else
1989                                 guitext_chat->setVisible(true);
1990                 }
1991
1992                 /*
1993                         Inventory
1994                 */
1995                 
1996                 static u16 old_selected_item = 65535;
1997                 if(client.getLocalInventoryUpdated()
1998                                 || g_selected_item != old_selected_item)
1999                 {
2000                         old_selected_item = g_selected_item;
2001                         //std::cout<<"Updating local inventory"<<std::endl;
2002                         client.getLocalInventory(local_inventory);
2003                 }
2004                 
2005                 /*
2006                         Send actions returned by the inventory menu
2007                 */
2008                 while(inventory_action_queue.size() != 0)
2009                 {
2010                         InventoryAction *a = inventory_action_queue.pop_front();
2011
2012                         client.sendInventoryAction(a);
2013                         // Eat it
2014                         delete a;
2015                 }
2016
2017                 /*
2018                         Drawing begins
2019                 */
2020
2021                 TimeTaker drawtimer("Drawing");
2022
2023                 
2024                 {
2025                         TimeTaker timer("beginScene");
2026                         driver->beginScene(true, true, bgcolor);
2027                         //driver->beginScene(false, true, bgcolor);
2028                         beginscenetime = timer.stop(true);
2029                 }
2030                 
2031                 /*
2032                         Draw farmesh before everything else
2033                 */
2034                 {
2035                         //farmesh->render();
2036                 }
2037
2038                 //timer3.stop();
2039                 
2040                 //std::cout<<DTIME<<"smgr->drawAll()"<<std::endl;
2041                 
2042                 {
2043                         TimeTaker timer("smgr");
2044                         smgr->drawAll();
2045                         scenetime = timer.stop(true);
2046                 }
2047                 
2048                 {
2049                 //TimeTaker timer9("auxiliary drawings");
2050                 // 0ms
2051                 
2052                 //timer9.stop();
2053                 //TimeTaker //timer10("//timer10");
2054                 
2055                 video::SMaterial m;
2056                 //m.Thickness = 10;
2057                 m.Thickness = 3;
2058                 m.Lighting = false;
2059                 driver->setMaterial(m);
2060
2061                 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
2062
2063                 for(core::list< core::aabbox3d<f32> >::Iterator i=hilightboxes.begin();
2064                                 i != hilightboxes.end(); i++)
2065                 {
2066                         /*std::cout<<"hilightbox min="
2067                                         <<"("<<i->MinEdge.X<<","<<i->MinEdge.Y<<","<<i->MinEdge.Z<<")"
2068                                         <<" max="
2069                                         <<"("<<i->MaxEdge.X<<","<<i->MaxEdge.Y<<","<<i->MaxEdge.Z<<")"
2070                                         <<std::endl;*/
2071                         driver->draw3DBox(*i, video::SColor(255,0,0,0));
2072                 }
2073
2074                 /*
2075                         Frametime log
2076                 */
2077                 if(g_settings.getBool("frametime_graph") == true)
2078                 {
2079                         s32 x = 10;
2080                         for(core::list<float>::Iterator
2081                                         i = frametime_log.begin();
2082                                         i != frametime_log.end();
2083                                         i++)
2084                         {
2085                                 driver->draw2DLine(v2s32(x,50),
2086                                                 v2s32(x,50+(*i)*1000),
2087                                                 video::SColor(255,255,255,255));
2088                                 x++;
2089                         }
2090                 }
2091
2092                 /*
2093                         Draw crosshair
2094                 */
2095                 driver->draw2DLine(displaycenter - core::vector2d<s32>(10,0),
2096                                 displaycenter + core::vector2d<s32>(10,0),
2097                                 video::SColor(255,255,255,255));
2098                 driver->draw2DLine(displaycenter - core::vector2d<s32>(0,10),
2099                                 displaycenter + core::vector2d<s32>(0,10),
2100                                 video::SColor(255,255,255,255));
2101
2102                 } // timer
2103
2104                 //timer10.stop();
2105                 //TimeTaker //timer11("//timer11");
2106
2107                 /*
2108                         Draw gui
2109                 */
2110                 // 0-1ms
2111                 guienv->drawAll();
2112
2113                 /*
2114                         Draw hotbar
2115                 */
2116                 {
2117                         draw_hotbar(driver, font, v2s32(displaycenter.X, screensize.Y),
2118                                         hotbar_imagesize, hotbar_itemcount, &local_inventory,
2119                                         client.getHP());
2120                 }
2121
2122                 /*
2123                         Damage flash
2124                 */
2125                 if(damage_flash_timer > 0.0)
2126                 {
2127                         damage_flash_timer -= dtime;
2128                         
2129                         video::SColor color(128,255,0,0);
2130                         driver->draw2DRectangle(color,
2131                                         core::rect<s32>(0,0,screensize.X,screensize.Y),
2132                                         NULL);
2133                 }
2134                 
2135                 /*
2136                         End scene
2137                 */
2138                 {
2139                         TimeTaker timer("endScene");
2140                         endSceneX(driver);
2141                         endscenetime = timer.stop(true);
2142                 }
2143
2144                 drawtime = drawtimer.stop(true);
2145
2146                 /*
2147                         End of drawing
2148                 */
2149
2150                 static s16 lastFPS = 0;
2151                 //u16 fps = driver->getFPS();
2152                 u16 fps = (1.0/dtime_avg1);
2153
2154                 if (lastFPS != fps)
2155                 {
2156                         core::stringw str = L"Minetest [";
2157                         str += driver->getName();
2158                         str += "] FPS=";
2159                         str += fps;
2160
2161                         device->setWindowCaption(str.c_str());
2162                         lastFPS = fps;
2163                 }
2164         }
2165
2166         /*
2167                 Drop stuff
2168         */
2169         if(clouds)
2170                 clouds->drop();
2171         
2172         /*
2173                 Draw a "shutting down" screen, which will be shown while the map
2174                 generator and other stuff quits
2175         */
2176         {
2177                 const wchar_t *shuttingdowntext = L"Shutting down stuff...";
2178                 gui::IGUIStaticText *gui_shuttingdowntext = guienv->addStaticText(
2179                                 shuttingdowntext, textrect, false, false);
2180                 gui_shuttingdowntext->setTextAlignment(gui::EGUIA_CENTER,
2181                                 gui::EGUIA_UPPERLEFT);
2182                 driver->beginScene(true, true, video::SColor(255,0,0,0));
2183                 guienv->drawAll();
2184                 driver->endScene();
2185                 gui_shuttingdowntext->remove();
2186         }
2187 }
2188
2189