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