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