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