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