3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 #include "common_irrlicht.h"
22 #include <IGUICheckBox.h>
23 #include <IGUIEditBox.h>
24 #include <IGUIButton.h>
25 #include <IGUIStaticText.h>
29 #include "guiPauseMenu.h"
30 #include "guiPasswordChange.h"
31 #include "guiInventoryMenu.h"
32 #include "guiTextInputMenu.h"
33 #include "guiDeathScreen.h"
34 #include "materials.h"
42 #include "mainmenumanager.h"
46 // Needed for some special cases for CONTENT_TORCH and CONTENT_SIGN_WALL
47 // TODO: A generic way for handling such should be created
48 #include "content_mapnode.h"
49 // Needed for sign text input
50 // TODO: A generic way for handling such should be created
51 #include "content_nodemeta.h"
52 // Needed for determining pointing to nodes
53 #include "mapnode_contentfeatures.h"
56 Setting this to 1 enables a special camera mode that forces
57 the renderers to think that the camera statically points from
58 the starting place to a static direction.
60 This allows one to move around with the player and see what
61 is actually drawn behind solid things and behind the player.
63 #define FIELD_OF_VIEW_TEST 0
73 ChatLine(const std::wstring &a_text):
86 // Inventory actions from the menu are buffered here before sending
87 Queue<InventoryAction*> inventory_action_queue;
88 // This is a copy of the inventory that the client's environment has
89 Inventory local_inventory;
91 u16 g_selected_item = 0;
97 struct TextDestChat : public TextDest
99 TextDestChat(Client *client)
103 void gotText(std::wstring text)
105 // Discard empty line
110 m_client->sendChatMessage(text);
112 m_client->addChatMessage(text);
118 struct TextDestSignNode : public TextDest
120 TextDestSignNode(v3s16 p, Client *client)
125 void gotText(std::wstring text)
127 std::string ntext = wide_to_narrow(text);
128 infostream<<"Changing text of a sign node: "
130 m_client->sendSignNodeText(m_p, ntext);
137 /* Respawn menu callback */
139 class MainRespawnInitiator: public IRespawnInitiator
142 MainRespawnInitiator(bool *active, Client *client):
143 m_active(active), m_client(client)
150 m_client->sendRespawn();
160 void draw_hotbar(video::IVideoDriver *driver, gui::IGUIFont *font,
161 v2s32 centerlowerpos, s32 imgsize, s32 itemcount,
162 Inventory *inventory, s32 halfheartcount)
164 InventoryList *mainlist = inventory->getList("main");
167 errorstream<<"draw_hotbar(): mainlist == NULL"<<std::endl;
171 s32 padding = imgsize/12;
172 //s32 height = imgsize + padding*2;
173 s32 width = itemcount*(imgsize+padding*2);
175 // Position of upper left corner of bar
176 v2s32 pos = centerlowerpos - v2s32(width/2, imgsize+padding*2);
178 // Draw background color
179 /*core::rect<s32> barrect(0,0,width,height);
181 video::SColor bgcolor(255,128,128,128);
182 driver->draw2DRectangle(bgcolor, barrect, NULL);*/
184 core::rect<s32> imgrect(0,0,imgsize,imgsize);
186 for(s32 i=0; i<itemcount; i++)
188 InventoryItem *item = mainlist->getItem(i);
190 core::rect<s32> rect = imgrect + pos
191 + v2s32(padding+i*(imgsize+padding*2), padding);
193 if(g_selected_item == i)
195 video::SColor c_outside(255,255,0,0);
196 //video::SColor c_outside(255,0,0,0);
197 //video::SColor c_inside(255,192,192,192);
198 s32 x1 = rect.UpperLeftCorner.X;
199 s32 y1 = rect.UpperLeftCorner.Y;
200 s32 x2 = rect.LowerRightCorner.X;
201 s32 y2 = rect.LowerRightCorner.Y;
202 // Black base borders
203 driver->draw2DRectangle(c_outside,
205 v2s32(x1 - padding, y1 - padding),
206 v2s32(x2 + padding, y1)
208 driver->draw2DRectangle(c_outside,
210 v2s32(x1 - padding, y2),
211 v2s32(x2 + padding, y2 + padding)
213 driver->draw2DRectangle(c_outside,
215 v2s32(x1 - padding, y1),
218 driver->draw2DRectangle(c_outside,
221 v2s32(x2 + padding, y2)
223 /*// Light inside borders
224 driver->draw2DRectangle(c_inside,
226 v2s32(x1 - padding/2, y1 - padding/2),
227 v2s32(x2 + padding/2, y1)
229 driver->draw2DRectangle(c_inside,
231 v2s32(x1 - padding/2, y2),
232 v2s32(x2 + padding/2, y2 + padding/2)
234 driver->draw2DRectangle(c_inside,
236 v2s32(x1 - padding/2, y1),
239 driver->draw2DRectangle(c_inside,
242 v2s32(x2 + padding/2, y2)
247 video::SColor bgcolor2(128,0,0,0);
248 driver->draw2DRectangle(bgcolor2, rect, NULL);
252 drawInventoryItem(driver, font, item, rect, NULL);
260 video::ITexture *heart_texture =
261 driver->getTexture(getTexturePath("heart.png").c_str());
262 v2s32 p = pos + v2s32(0, -20);
263 for(s32 i=0; i<halfheartcount/2; i++)
265 const video::SColor color(255,255,255,255);
266 const video::SColor colors[] = {color,color,color,color};
267 core::rect<s32> rect(0,0,16,16);
269 driver->draw2DImage(heart_texture, rect,
270 core::rect<s32>(core::position2d<s32>(0,0),
271 core::dimension2di(heart_texture->getOriginalSize())),
275 if(halfheartcount % 2 == 1)
277 const video::SColor color(255,255,255,255);
278 const video::SColor colors[] = {color,color,color,color};
279 core::rect<s32> rect(0,0,16/2,16);
281 core::dimension2di srcd(heart_texture->getOriginalSize());
283 driver->draw2DImage(heart_texture, rect,
284 core::rect<s32>(core::position2d<s32>(0,0), srcd),
292 Find what the player is pointing at
294 void getPointedNode(Client *client, v3f player_position,
295 v3f camera_direction, v3f camera_position,
296 bool &nodefound, core::line3d<f32> shootline,
297 v3s16 &nodepos, v3s16 &neighbourpos,
298 core::aabbox3d<f32> &nodehilightbox,
301 f32 mindistance = BS * 1001;
303 v3s16 pos_i = floatToInt(player_position, BS);
305 /*infostream<<"pos_i=("<<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z<<")"
309 s16 ystart = pos_i.Y + 0 - (camera_direction.Y<0 ? a : 1);
310 s16 zstart = pos_i.Z - (camera_direction.Z<0 ? a : 1);
311 s16 xstart = pos_i.X - (camera_direction.X<0 ? a : 1);
312 s16 yend = pos_i.Y + 1 + (camera_direction.Y>0 ? a : 1);
313 s16 zend = pos_i.Z + (camera_direction.Z>0 ? a : 1);
314 s16 xend = pos_i.X + (camera_direction.X>0 ? a : 1);
316 for(s16 y = ystart; y <= yend; y++)
317 for(s16 z = zstart; z <= zend; z++)
318 for(s16 x = xstart; x <= xend; x++)
323 n = client->getNode(v3s16(x,y,z));
324 if(content_pointable(n.getContent()) == false)
327 catch(InvalidPositionException &e)
333 v3f npf = intToFloat(np, BS);
338 v3s16(0,0,1), // back
340 v3s16(1,0,0), // right
341 v3s16(0,0,-1), // front
342 v3s16(0,-1,0), // bottom
343 v3s16(-1,0,0), // left
349 if(n.getContent() == CONTENT_TORCH)
351 v3s16 dir = unpackDir(n.param2);
352 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
353 dir_f *= BS/2 - BS/6 - BS/20;
354 v3f cpf = npf + dir_f;
355 f32 distance = (cpf - camera_position).getLength();
357 core::aabbox3d<f32> box;
360 if(dir == v3s16(0,-1,0))
362 box = core::aabbox3d<f32>(
363 npf - v3f(BS/6, BS/2, BS/6),
364 npf + v3f(BS/6, -BS/2+BS/3*2, BS/6)
368 else if(dir == v3s16(0,1,0))
370 box = core::aabbox3d<f32>(
371 npf - v3f(BS/6, -BS/2+BS/3*2, BS/6),
372 npf + v3f(BS/6, BS/2, BS/6)
378 box = core::aabbox3d<f32>(
379 cpf - v3f(BS/6, BS/3, BS/6),
380 cpf + v3f(BS/6, BS/3, BS/6)
384 if(distance < mindistance)
386 if(box.intersectsWithLine(shootline))
391 mindistance = distance;
392 nodehilightbox = box;
396 else if(n.getContent() == CONTENT_SIGN_WALL)
398 v3s16 dir = unpackDir(n.param2);
399 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
400 dir_f *= BS/2 - BS/6 - BS/20;
401 v3f cpf = npf + dir_f;
402 f32 distance = (cpf - camera_position).getLength();
406 v3f(BS*0.42,-BS*0.35,-BS*0.4),
407 v3f(BS*0.49, BS*0.35, BS*0.4),
410 for(s32 i=0; i<2; i++)
412 if(dir == v3s16(1,0,0))
413 vertices[i].rotateXZBy(0);
414 if(dir == v3s16(-1,0,0))
415 vertices[i].rotateXZBy(180);
416 if(dir == v3s16(0,0,1))
417 vertices[i].rotateXZBy(90);
418 if(dir == v3s16(0,0,-1))
419 vertices[i].rotateXZBy(-90);
420 if(dir == v3s16(0,-1,0))
421 vertices[i].rotateXYBy(-90);
422 if(dir == v3s16(0,1,0))
423 vertices[i].rotateXYBy(90);
428 core::aabbox3d<f32> box;
430 box = core::aabbox3d<f32>(vertices[0]);
431 box.addInternalPoint(vertices[1]);
433 if(distance < mindistance)
435 if(box.intersectsWithLine(shootline))
440 mindistance = distance;
441 nodehilightbox = box;
446 else if(n.getContent() == CONTENT_LADDER)
448 v3s16 dir = unpackDir(n.param2);
449 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
450 dir_f *= BS/2 - BS/6 - BS/20;
451 v3f cpf = npf + dir_f;
452 f32 distance = (cpf - camera_position).getLength();
456 v3f(BS*0.42,-BS/2,-BS/2),
457 v3f(BS*0.49, BS/2, BS/2),
460 for(s32 i=0; i<2; i++)
462 if(dir == v3s16(1,0,0))
463 vertices[i].rotateXZBy(0);
464 if(dir == v3s16(-1,0,0))
465 vertices[i].rotateXZBy(180);
466 if(dir == v3s16(0,0,1))
467 vertices[i].rotateXZBy(90);
468 if(dir == v3s16(0,0,-1))
469 vertices[i].rotateXZBy(-90);
470 if(dir == v3s16(0,-1,0))
471 vertices[i].rotateXYBy(-90);
472 if(dir == v3s16(0,1,0))
473 vertices[i].rotateXYBy(90);
478 core::aabbox3d<f32> box;
480 box = core::aabbox3d<f32>(vertices[0]);
481 box.addInternalPoint(vertices[1]);
483 if(distance < mindistance)
485 if(box.intersectsWithLine(shootline))
490 mindistance = distance;
491 nodehilightbox = box;
495 else if(n.getContent() == CONTENT_RAIL)
497 v3s16 dir = unpackDir(n.param0);
498 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
499 dir_f *= BS/2 - BS/6 - BS/20;
500 v3f cpf = npf + dir_f;
501 f32 distance = (cpf - camera_position).getLength();
503 float d = (float)BS/16;
506 v3f(BS/2, -BS/2+d, -BS/2),
507 v3f(-BS/2, -BS/2, BS/2),
510 for(s32 i=0; i<2; i++)
515 core::aabbox3d<f32> box;
517 box = core::aabbox3d<f32>(vertices[0]);
518 box.addInternalPoint(vertices[1]);
520 if(distance < mindistance)
522 if(box.intersectsWithLine(shootline))
527 mindistance = distance;
528 nodehilightbox = box;
537 for(u16 i=0; i<6; i++)
539 v3f dir_f = v3f(dirs[i].X,
540 dirs[i].Y, dirs[i].Z);
541 v3f centerpoint = npf + dir_f * BS/2;
543 (centerpoint - camera_position).getLength();
545 if(distance < mindistance)
547 core::CMatrix4<f32> m;
548 m.buildRotateFromTo(v3f(0,0,1), dir_f);
550 // This is the back face
552 v3f(BS/2, BS/2, BS/2),
553 v3f(-BS/2, -BS/2, BS/2+d)
556 for(u16 j=0; j<2; j++)
558 m.rotateVect(corners[j]);
562 core::aabbox3d<f32> facebox(corners[0]);
563 facebox.addInternalPoint(corners[1]);
565 if(facebox.intersectsWithLine(shootline))
569 neighbourpos = np + dirs[i];
570 mindistance = distance;
572 //nodehilightbox = facebox;
574 const float d = 0.502;
575 core::aabbox3d<f32> nodebox
576 (-BS*d, -BS*d, -BS*d, BS*d, BS*d, BS*d);
577 v3f nodepos_f = intToFloat(nodepos, BS);
578 nodebox.MinEdge += nodepos_f;
579 nodebox.MaxEdge += nodepos_f;
580 nodehilightbox = nodebox;
582 } // if distance < mindistance
588 void update_skybox(video::IVideoDriver* driver,
589 scene::ISceneManager* smgr, scene::ISceneNode* &skybox,
597 /*// Disable skybox if FarMesh is enabled
598 if(g_settings->getBool("enable_farmesh"))
601 if(brightness >= 0.5)
603 skybox = smgr->addSkyBoxSceneNode(
604 driver->getTexture(getTexturePath("skybox2.png").c_str()),
605 driver->getTexture(getTexturePath("skybox3.png").c_str()),
606 driver->getTexture(getTexturePath("skybox1.png").c_str()),
607 driver->getTexture(getTexturePath("skybox1.png").c_str()),
608 driver->getTexture(getTexturePath("skybox1.png").c_str()),
609 driver->getTexture(getTexturePath("skybox1.png").c_str()));
611 else if(brightness >= 0.2)
613 skybox = smgr->addSkyBoxSceneNode(
614 driver->getTexture(getTexturePath("skybox2_dawn.png").c_str()),
615 driver->getTexture(getTexturePath("skybox3_dawn.png").c_str()),
616 driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
617 driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
618 driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
619 driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()));
623 skybox = smgr->addSkyBoxSceneNode(
624 driver->getTexture(getTexturePath("skybox2_night.png").c_str()),
625 driver->getTexture(getTexturePath("skybox3_night.png").c_str()),
626 driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
627 driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
628 driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
629 driver->getTexture(getTexturePath("skybox1_night.png").c_str()));
634 Draws a screen with a single text on it.
635 Text will be removed when the screen is drawn the next time.
637 /*gui::IGUIStaticText **/
638 void draw_load_screen(const std::wstring &text,
639 video::IVideoDriver* driver, gui::IGUIFont* font)
641 v2u32 screensize = driver->getScreenSize();
642 const wchar_t *loadingtext = text.c_str();
643 core::vector2d<u32> textsize_u = font->getDimension(loadingtext);
644 core::vector2d<s32> textsize(textsize_u.X,textsize_u.Y);
645 core::vector2d<s32> center(screensize.X/2, screensize.Y/2);
646 core::rect<s32> textrect(center - textsize/2, center + textsize/2);
648 gui::IGUIStaticText *guitext = guienv->addStaticText(
649 loadingtext, textrect, false, false);
650 guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
652 driver->beginScene(true, true, video::SColor(255,0,0,0));
665 IrrlichtDevice *device,
668 std::string playername,
669 std::string password,
672 std::wstring &error_message,
673 std::string configpath
676 video::IVideoDriver* driver = device->getVideoDriver();
677 scene::ISceneManager* smgr = device->getSceneManager();
679 // Calculate text height using the font
680 u32 text_height = font->getDimension(L"Random test string").Height;
682 v2u32 screensize(0,0);
683 v2u32 last_screensize(0,0);
684 screensize = driver->getScreenSize();
686 const s32 hotbar_itemcount = 8;
687 //const s32 hotbar_imagesize = 36;
688 //const s32 hotbar_imagesize = 64;
689 s32 hotbar_imagesize = 48;
691 // The color of the sky
693 //video::SColor skycolor = video::SColor(255,140,186,250);
695 video::SColor bgcolor_bright = video::SColor(255,170,200,230);
698 Draw "Loading" screen
700 /*gui::IGUIStaticText *gui_loadingtext = */
701 //draw_load_screen(L"Loading and connecting...", driver, font);
703 draw_load_screen(L"Loading...", driver, font);
707 SharedPtr will delete it when it goes out of scope.
709 SharedPtr<Server> server;
711 draw_load_screen(L"Creating server...", driver, font);
712 infostream<<"Creating server"<<std::endl;
713 server = new Server(map_dir, configpath);
721 draw_load_screen(L"Creating client...", driver, font);
722 infostream<<"Creating client"<<std::endl;
723 MapDrawControl draw_control;
724 Client client(device, playername.c_str(), password, draw_control);
726 draw_load_screen(L"Resolving address...", driver, font);
727 Address connect_address(0,0,0,0, port);
730 //connect_address.Resolve("localhost");
731 connect_address.setAddress(127,0,0,1);
733 connect_address.Resolve(address.c_str());
735 catch(ResolveError &e)
737 errorstream<<"Couldn't resolve address"<<std::endl;
739 error_message = L"Couldn't resolve address";
740 //gui_loadingtext->remove();
745 Attempt to connect to the server
748 infostream<<"Connecting to server at ";
749 connect_address.print(&infostream);
750 infostream<<std::endl;
751 client.connect(connect_address);
753 bool could_connect = false;
756 float time_counter = 0.0;
759 if(client.connectedAndInitialized())
761 could_connect = true;
764 if(client.accessDenied())
768 // Wait for 10 seconds
769 if(time_counter >= 10.0)
774 std::wostringstream ss;
775 ss<<L"Connecting to server... (timeout in ";
776 ss<<(int)(10.0 - time_counter + 1.0);
778 draw_load_screen(ss.str(), driver, font);
781 driver->beginScene(true, true, video::SColor(255,0,0,0));
783 driver->endScene();*/
785 // Update client and server
797 catch(con::PeerNotFoundException &e)
800 if(could_connect == false)
802 if(client.accessDenied())
804 error_message = L"Access denied. Reason: "
805 +client.accessDeniedReason();
806 errorstream<<wide_to_narrow(error_message)<<std::endl;
810 error_message = L"Connection timed out.";
811 errorstream<<"Timed out."<<std::endl;
813 //gui_loadingtext->remove();
820 float old_brightness = 1.0;
821 scene::ISceneNode* skybox = NULL;
822 update_skybox(driver, smgr, skybox, 1.0);
825 Create the camera node
827 Camera camera(smgr, draw_control);
828 if (!camera.successfullyCreated(error_message))
831 f32 camera_yaw = 0; // "right/left"
832 f32 camera_pitch = 0; // "up/down"
838 float cloud_height = BS*100;
839 Clouds *clouds = NULL;
840 if(g_settings->getBool("enable_clouds"))
842 clouds = new Clouds(smgr->getRootSceneNode(), smgr, -1,
843 cloud_height, time(0));
850 FarMesh *farmesh = NULL;
851 if(g_settings->getBool("enable_farmesh"))
853 farmesh = new FarMesh(smgr->getRootSceneNode(), smgr, -1, client.getMapSeed(), &client);
860 //gui_loadingtext->remove();
866 // First line of debug text
867 gui::IGUIStaticText *guitext = guienv->addStaticText(
869 core::rect<s32>(5, 5, 795, 5+text_height),
871 // Second line of debug text
872 gui::IGUIStaticText *guitext2 = guienv->addStaticText(
874 core::rect<s32>(5, 5+(text_height+5)*1, 795, (5+text_height)*2),
876 // At the middle of the screen
877 // Object infos are shown in this
878 gui::IGUIStaticText *guitext_info = guienv->addStaticText(
880 core::rect<s32>(0,0,400,text_height+5) + v2s32(100,200),
884 gui::IGUIStaticText *guitext_chat = guienv->addStaticText(
886 core::rect<s32>(0,0,0,0),
887 //false, false); // Disable word wrap as of now
889 //guitext_chat->setBackgroundColor(video::SColor(96,0,0,0));
890 core::list<ChatLine> chat_lines;
892 // Profiler text (size is updated when text is updated)
893 gui::IGUIStaticText *guitext_profiler = guienv->addStaticText(
895 core::rect<s32>(6, 4+(text_height+5)*2, 400,
896 (text_height+5)*2 + text_height*35),
898 guitext_profiler->setBackgroundColor(video::SColor(80,0,0,0));
899 guitext_profiler->setVisible(false);
901 /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
902 (guienv, NULL, v2s32(10, 70), 5, &local_inventory);*/
903 /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
904 (guienv, NULL, v2s32(0, 0), quickinv_itemcount, &local_inventory);*/
906 // Test the text input system
907 /*(new GUITextInputMenu(guienv, guiroot, -1, &g_menumgr,
909 /*GUIMessageMenu *menu =
910 new GUIMessageMenu(guienv, guiroot, -1,
916 (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
917 &g_menumgr))->drop();
920 /*guitext2->setVisible(true);
921 guitext_info->setVisible(true);
922 guitext_chat->setVisible(true);*/
924 //s32 guitext_chat_pad_bottom = 70;
927 Some statistics are collected in these
930 u32 beginscenetime = 0;
932 u32 endscenetime = 0;
935 //throw con::PeerNotFoundException("lol");
937 core::list<float> frametime_log;
939 float nodig_delay_counter = 0.0;
940 float dig_time = 0.0;
942 v3s16 nodepos_old(-32768,-32768,-32768);
944 float damage_flash_timer = 0;
945 s16 farmesh_range = 20*MAP_BLOCKSIZE;
947 const float object_hit_delay = 0.5;
948 float object_hit_delay_timer = 0.0;
950 bool invert_mouse = g_settings->getBool("invert_mouse");
952 bool respawn_menu_active = false;
954 bool show_profiler = false;
955 bool force_fog_off = false;
956 bool disable_camera_update = false;
962 bool first_loop_after_window_activation = true;
964 // TODO: Convert the static interval timers to these
965 // Interval limiter for profiler
966 IntervalLimiter m_profiler_interval;
968 // Time is in milliseconds
969 // NOTE: getRealTime() causes strange problems in wine (imprecision?)
970 // NOTE: So we have to use getTime() and call run()s between them
971 u32 lasttime = device->getTimer()->getTime();
973 while(device->run() && kill == false)
975 //std::cerr<<"frame"<<std::endl;
977 if(client.accessDenied())
979 error_message = L"Access denied. Reason: "
980 +client.accessDeniedReason();
981 errorstream<<wide_to_narrow(error_message)<<std::endl;
985 if(g_gamecallback->disconnect_requested)
987 g_gamecallback->disconnect_requested = false;
991 if(g_gamecallback->changepassword_requested)
993 (new GUIPasswordChange(guienv, guiroot, -1,
994 &g_menumgr, &client))->drop();
995 g_gamecallback->changepassword_requested = false;
999 Process TextureSource's queue
1001 ((TextureSource*)g_texturesource)->processQueue();
1006 last_screensize = screensize;
1007 screensize = driver->getScreenSize();
1008 v2s32 displaycenter(screensize.X/2,screensize.Y/2);
1009 //bool screensize_changed = screensize != last_screensize;
1012 if(screensize.Y <= 800)
1013 hotbar_imagesize = 32;
1014 else if(screensize.Y <= 1280)
1015 hotbar_imagesize = 48;
1017 hotbar_imagesize = 64;
1019 // Hilight boxes collected during the loop and displayed
1020 core::list< core::aabbox3d<f32> > hilightboxes;
1023 std::wstring infotext;
1025 // When screen size changes, update positions and sizes of stuff
1026 /*if(screensize_changed)
1028 v2s32 pos(displaycenter.X-((quickinv_itemcount-1)*quickinv_spacing+quickinv_size)/2, screensize.Y-quickinv_spacing);
1029 quick_inventory->updatePosition(pos);
1032 //TimeTaker //timer1("//timer1");
1034 // Time of frame without fps limit
1038 // not using getRealTime is necessary for wine
1039 u32 time = device->getTimer()->getTime();
1041 busytime_u32 = time - lasttime;
1044 busytime = busytime_u32 / 1000.0;
1047 //infostream<<"busytime_u32="<<busytime_u32<<std::endl;
1049 // Necessary for device->getTimer()->getTime()
1057 float fps_max = g_settings->getFloat("fps_max");
1058 u32 frametime_min = 1000./fps_max;
1060 if(busytime_u32 < frametime_min)
1062 u32 sleeptime = frametime_min - busytime_u32;
1063 device->sleep(sleeptime);
1067 // Necessary for device->getTimer()->getTime()
1071 Time difference calculation
1073 f32 dtime; // in seconds
1075 u32 time = device->getTimer()->getTime();
1077 dtime = (time - lasttime) / 1000.0;
1084 object_hit_delay_timer -= dtime;
1086 g_profiler->add("Elapsed time", dtime);
1087 g_profiler->avg("FPS", 1./dtime);
1090 Log frametime for visualization
1092 frametime_log.push_back(dtime);
1093 if(frametime_log.size() > 100)
1095 core::list<float>::Iterator i = frametime_log.begin();
1096 frametime_log.erase(i);
1100 Visualize frametime in terminal
1102 /*for(u32 i=0; i<dtime*400; i++)
1104 infostream<<std::endl;*/
1107 Time average and jitter calculation
1110 static f32 dtime_avg1 = 0.0;
1111 dtime_avg1 = dtime_avg1 * 0.96 + dtime * 0.04;
1112 f32 dtime_jitter1 = dtime - dtime_avg1;
1114 static f32 dtime_jitter1_max_sample = 0.0;
1115 static f32 dtime_jitter1_max_fraction = 0.0;
1117 static f32 jitter1_max = 0.0;
1118 static f32 counter = 0.0;
1119 if(dtime_jitter1 > jitter1_max)
1120 jitter1_max = dtime_jitter1;
1125 dtime_jitter1_max_sample = jitter1_max;
1126 dtime_jitter1_max_fraction
1127 = dtime_jitter1_max_sample / (dtime_avg1+0.001);
1133 Busytime average and jitter calculation
1136 static f32 busytime_avg1 = 0.0;
1137 busytime_avg1 = busytime_avg1 * 0.98 + busytime * 0.02;
1138 f32 busytime_jitter1 = busytime - busytime_avg1;
1140 static f32 busytime_jitter1_max_sample = 0.0;
1141 static f32 busytime_jitter1_min_sample = 0.0;
1143 static f32 jitter1_max = 0.0;
1144 static f32 jitter1_min = 0.0;
1145 static f32 counter = 0.0;
1146 if(busytime_jitter1 > jitter1_max)
1147 jitter1_max = busytime_jitter1;
1148 if(busytime_jitter1 < jitter1_min)
1149 jitter1_min = busytime_jitter1;
1153 busytime_jitter1_max_sample = jitter1_max;
1154 busytime_jitter1_min_sample = jitter1_min;
1161 Debug info for client
1164 static float counter = 0.0;
1169 client.printDebugInfo(infostream);
1176 float profiler_print_interval =
1177 g_settings->getFloat("profiler_print_interval");
1178 bool print_to_log = true;
1179 if(profiler_print_interval == 0){
1180 print_to_log = false;
1181 profiler_print_interval = 5;
1183 if(m_profiler_interval.step(dtime, profiler_print_interval))
1186 infostream<<"Profiler:"<<std::endl;
1187 g_profiler->print(infostream);
1190 std::ostringstream os(std::ios_base::binary);
1191 g_profiler->print(os);
1192 std::wstring text = narrow_to_wide(os.str());
1193 guitext_profiler->setText(text.c_str());
1195 g_profiler->clear();
1197 s32 w = font->getDimension(text.c_str()).Width;
1200 core::rect<s32> rect(6, 4+(text_height+5)*2, 12+w,
1201 8+(text_height+5)*2 +
1202 font->getDimension(text.c_str()).Height);
1203 guitext_profiler->setRelativePosition(rect);
1207 Direct handling of user input
1210 // Reset input if window not active or some menu is active
1211 if(device->isWindowActive() == false || noMenuActive() == false)
1216 // Input handler step() (used by the random input generator)
1220 Launch menus according to keys
1222 if(input->wasKeyDown(getKeySetting("keymap_inventory")))
1224 infostream<<"the_game: "
1225 <<"Launching inventory"<<std::endl;
1227 GUIInventoryMenu *menu =
1228 new GUIInventoryMenu(guienv, guiroot, -1,
1229 &g_menumgr, v2s16(8,7),
1230 client.getInventoryContext(),
1233 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
1234 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1235 "list", "current_player", "main",
1236 v2s32(0, 3), v2s32(8, 4)));
1237 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1238 "list", "current_player", "craft",
1239 v2s32(3, 0), v2s32(3, 3)));
1240 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1241 "list", "current_player", "craftresult",
1242 v2s32(7, 1), v2s32(1, 1)));
1244 menu->setDrawSpec(draw_spec);
1248 else if(input->wasKeyDown(EscapeKey))
1250 infostream<<"the_game: "
1251 <<"Launching pause menu"<<std::endl;
1252 // It will delete itself by itself
1253 (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
1254 &g_menumgr))->drop();
1256 // Move mouse cursor on top of the disconnect button
1257 input->setMousePos(displaycenter.X, displaycenter.Y+25);
1259 else if(input->wasKeyDown(getKeySetting("keymap_chat")))
1261 TextDest *dest = new TextDestChat(&client);
1263 (new GUITextInputMenu(guienv, guiroot, -1,
1267 else if(input->wasKeyDown(getKeySetting("keymap_cmd")))
1269 TextDest *dest = new TextDestChat(&client);
1271 (new GUITextInputMenu(guienv, guiroot, -1,
1275 else if(input->wasKeyDown(getKeySetting("keymap_freemove")))
1277 if(g_settings->getBool("free_move"))
1279 g_settings->set("free_move","false");
1280 chat_lines.push_back(ChatLine(L"free_move disabled"));
1284 g_settings->set("free_move","true");
1285 chat_lines.push_back(ChatLine(L"free_move enabled"));
1288 else if(input->wasKeyDown(getKeySetting("keymap_fastmove")))
1290 if(g_settings->getBool("fast_move"))
1292 g_settings->set("fast_move","false");
1293 chat_lines.push_back(ChatLine(L"fast_move disabled"));
1297 g_settings->set("fast_move","true");
1298 chat_lines.push_back(ChatLine(L"fast_move enabled"));
1301 else if(input->wasKeyDown(getKeySetting("keymap_frametime_graph")))
1303 if(g_settings->getBool("frametime_graph"))
1305 g_settings->set("frametime_graph","false");
1306 chat_lines.push_back(ChatLine(L"frametime_graph disabled"));
1310 g_settings->set("frametime_graph","true");
1311 chat_lines.push_back(ChatLine(L"frametime_graph enabled"));
1314 else if(input->wasKeyDown(getKeySetting("keymap_screenshot")))
1316 irr::video::IImage* const image = driver->createScreenShot();
1318 irr::c8 filename[256];
1319 snprintf(filename, 256, "%s" DIR_DELIM "screenshot_%u.png",
1320 g_settings->get("screenshot_path").c_str(),
1321 device->getTimer()->getRealTime());
1322 if (driver->writeImageToFile(image, filename)) {
1323 std::wstringstream sstr;
1324 sstr<<"Saved screenshot to '"<<filename<<"'";
1325 infostream<<"Saved screenshot to '"<<filename<<"'"<<std::endl;
1326 chat_lines.push_back(ChatLine(sstr.str()));
1328 infostream<<"Failed to save screenshot '"<<filename<<"'"<<std::endl;
1333 else if(input->wasKeyDown(getKeySetting("keymap_toggle_profiler")))
1335 show_profiler = !show_profiler;
1336 guitext_profiler->setVisible(show_profiler);
1338 chat_lines.push_back(ChatLine(L"Profiler disabled"));
1340 chat_lines.push_back(ChatLine(L"Profiler enabled"));
1342 else if(input->wasKeyDown(getKeySetting("keymap_toggle_force_fog_off")))
1344 force_fog_off = !force_fog_off;
1346 chat_lines.push_back(ChatLine(L"Fog disabled"));
1348 chat_lines.push_back(ChatLine(L"Fog enabled"));
1350 else if(input->wasKeyDown(getKeySetting("keymap_toggle_update_camera")))
1352 disable_camera_update = !disable_camera_update;
1353 if(disable_camera_update)
1354 chat_lines.push_back(ChatLine(L"Camera update disabled"));
1356 chat_lines.push_back(ChatLine(L"Camera update enabled"));
1359 // Item selection with mouse wheel
1361 s32 wheel = input->getMouseWheel();
1362 u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1,
1363 hotbar_itemcount-1);
1367 if(g_selected_item < max_item)
1370 g_selected_item = 0;
1374 if(g_selected_item > 0)
1377 g_selected_item = max_item;
1382 for(u16 i=0; i<10; i++)
1384 const KeyPress *kp = NumberKey + (i + 1) % 10;
1385 if(input->wasKeyDown(*kp))
1387 if(i < PLAYER_INVENTORY_SIZE && i < hotbar_itemcount)
1389 g_selected_item = i;
1391 infostream<<"Selected item: "
1392 <<g_selected_item<<std::endl;
1397 // Viewing range selection
1398 if(input->wasKeyDown(getKeySetting("keymap_rangeselect")))
1400 if(draw_control.range_all)
1402 draw_control.range_all = false;
1403 infostream<<"Disabled full viewing range"<<std::endl;
1407 draw_control.range_all = true;
1408 infostream<<"Enabled full viewing range"<<std::endl;
1412 // Print debug stacks
1413 if(input->wasKeyDown(getKeySetting("keymap_print_debug_stacks")))
1415 dstream<<"-----------------------------------------"
1417 dstream<<DTIME<<"Printing debug stacks:"<<std::endl;
1418 dstream<<"-----------------------------------------"
1420 debug_stacks_print();
1424 Mouse and camera control
1425 NOTE: Do this before client.setPlayerControl() to not cause a camera lag of one frame
1428 if((device->isWindowActive() && noMenuActive()) || random_input)
1432 // Mac OSX gets upset if this is set every frame
1433 if(device->getCursorControl()->isVisible())
1434 device->getCursorControl()->setVisible(false);
1437 if(first_loop_after_window_activation){
1438 //infostream<<"window active, first loop"<<std::endl;
1439 first_loop_after_window_activation = false;
1442 s32 dx = input->getMousePos().X - displaycenter.X;
1443 s32 dy = input->getMousePos().Y - displaycenter.Y;
1446 //infostream<<"window active, pos difference "<<dx<<","<<dy<<std::endl;
1448 /*const float keyspeed = 500;
1449 if(input->isKeyDown(irr::KEY_UP))
1450 dy -= dtime * keyspeed;
1451 if(input->isKeyDown(irr::KEY_DOWN))
1452 dy += dtime * keyspeed;
1453 if(input->isKeyDown(irr::KEY_LEFT))
1454 dx -= dtime * keyspeed;
1455 if(input->isKeyDown(irr::KEY_RIGHT))
1456 dx += dtime * keyspeed;*/
1458 camera_yaw -= dx*0.2;
1459 camera_pitch += dy*0.2;
1460 if(camera_pitch < -89.5) camera_pitch = -89.5;
1461 if(camera_pitch > 89.5) camera_pitch = 89.5;
1463 input->setMousePos(displaycenter.X, displaycenter.Y);
1466 // Mac OSX gets upset if this is set every frame
1467 if(device->getCursorControl()->isVisible() == false)
1468 device->getCursorControl()->setVisible(true);
1470 //infostream<<"window inactive"<<std::endl;
1471 first_loop_after_window_activation = true;
1475 Player speed control
1478 if(!noMenuActive() || !device->isWindowActive())
1480 PlayerControl control(
1491 client.setPlayerControl(control);
1504 PlayerControl control(
1505 input->isKeyDown(getKeySetting("keymap_forward")),
1506 input->isKeyDown(getKeySetting("keymap_backward")),
1507 input->isKeyDown(getKeySetting("keymap_left")),
1508 input->isKeyDown(getKeySetting("keymap_right")),
1509 input->isKeyDown(getKeySetting("keymap_jump")),
1510 input->isKeyDown(getKeySetting("keymap_special1")),
1511 input->isKeyDown(getKeySetting("keymap_sneak")),
1515 client.setPlayerControl(control);
1524 //TimeTaker timer("server->step(dtime)");
1525 server->step(dtime);
1533 //TimeTaker timer("client.step(dtime)");
1535 //client.step(dtime_avg1);
1539 // Read client events
1542 ClientEvent event = client.getClientEvent();
1543 if(event.type == CE_NONE)
1547 else if(event.type == CE_PLAYER_DAMAGE)
1549 //u16 damage = event.player_damage.amount;
1550 //infostream<<"Player damage: "<<damage<<std::endl;
1551 damage_flash_timer = 0.05;
1552 if(event.player_damage.amount >= 2){
1553 damage_flash_timer += 0.05 * event.player_damage.amount;
1556 else if(event.type == CE_PLAYER_FORCE_MOVE)
1558 camera_yaw = event.player_force_move.yaw;
1559 camera_pitch = event.player_force_move.pitch;
1561 else if(event.type == CE_DEATHSCREEN)
1563 if(respawn_menu_active)
1566 /*bool set_camera_point_target =
1567 event.deathscreen.set_camera_point_target;
1568 v3f camera_point_target;
1569 camera_point_target.X = event.deathscreen.camera_point_target_x;
1570 camera_point_target.Y = event.deathscreen.camera_point_target_y;
1571 camera_point_target.Z = event.deathscreen.camera_point_target_z;*/
1572 MainRespawnInitiator *respawner =
1573 new MainRespawnInitiator(
1574 &respawn_menu_active, &client);
1575 GUIDeathScreen *menu =
1576 new GUIDeathScreen(guienv, guiroot, -1,
1577 &g_menumgr, respawner);
1580 /* Handle visualization */
1582 damage_flash_timer = 0;
1584 /*LocalPlayer* player = client.getLocalPlayer();
1585 player->setPosition(player->getPosition() + v3f(0,-BS,0));
1586 camera.update(player, busytime, screensize);*/
1591 //TimeTaker //timer2("//timer2");
1593 LocalPlayer* player = client.getLocalPlayer();
1594 camera.update(player, busytime, screensize);
1597 v3f player_position = player->getPosition();
1598 v3f camera_position = camera.getPosition();
1599 v3f camera_direction = camera.getDirection();
1600 f32 camera_fov = camera.getFovMax();
1602 if(!disable_camera_update){
1603 client.updateCamera(camera_position,
1604 camera_direction, camera_fov);
1608 //TimeTaker //timer3("//timer3");
1611 Calculate what block is the crosshair pointing to
1614 //u32 t1 = device->getTimer()->getRealTime();
1616 //f32 d = 4; // max. distance
1617 f32 d = 4; // max. distance
1618 core::line3d<f32> shootline(camera_position,
1619 camera_position + camera_direction * BS * (d+1));
1621 ClientActiveObject *selected_active_object
1622 = client.getSelectedActiveObject
1623 (d*BS, camera_position, shootline);
1625 bool left_punch = false;
1626 bool left_punch_muted = false;
1628 if(selected_active_object != NULL)
1630 /* Clear possible cracking animation */
1631 if(nodepos_old != v3s16(-32768,-32768,-32768))
1633 client.clearTempMod(nodepos_old);
1635 nodepos_old = v3s16(-32768,-32768,-32768);
1638 //infostream<<"Client returned selected_active_object != NULL"<<std::endl;
1640 core::aabbox3d<f32> *selection_box
1641 = selected_active_object->getSelectionBox();
1642 // Box should exist because object was returned in the
1644 assert(selection_box);
1646 v3f pos = selected_active_object->getPosition();
1648 core::aabbox3d<f32> box_on_map(
1649 selection_box->MinEdge + pos,
1650 selection_box->MaxEdge + pos
1653 if(selected_active_object->doShowSelectionBox())
1654 hilightboxes.push_back(box_on_map);
1656 //infotext = narrow_to_wide("A ClientActiveObject");
1657 infotext = narrow_to_wide(selected_active_object->infoText());
1659 //if(input->getLeftClicked())
1660 if(input->getLeftState())
1662 bool do_punch = false;
1663 bool do_punch_damage = false;
1664 if(object_hit_delay_timer <= 0.0){
1666 do_punch_damage = true;
1667 object_hit_delay_timer = object_hit_delay;
1669 if(input->getLeftClicked()){
1673 infostream<<"Left-clicked object"<<std::endl;
1676 if(do_punch_damage){
1677 client.clickActiveObject(0,
1678 selected_active_object->getId(), g_selected_item);
1681 else if(input->getRightClicked())
1683 infostream<<"Right-clicked object"<<std::endl;
1684 client.clickActiveObject(1,
1685 selected_active_object->getId(), g_selected_item);
1688 else // selected_object == NULL
1692 Find out which node we are pointing at
1695 bool nodefound = false;
1698 core::aabbox3d<f32> nodehilightbox;
1700 getPointedNode(&client, player_position,
1701 camera_direction, camera_position,
1702 nodefound, shootline,
1703 nodepos, neighbourpos,
1707 if(nodepos_old != v3s16(-32768,-32768,-32768))
1709 client.clearTempMod(nodepos_old);
1711 nodepos_old = v3s16(-32768,-32768,-32768);
1718 hilightboxes.push_back(nodehilightbox);
1721 Check information text of node
1724 NodeMetadata *meta = client.getNodeMetadata(nodepos);
1727 infotext = narrow_to_wide(meta->infoText());
1730 //MapNode node = client.getNode(nodepos);
1736 if(input->getLeftReleased())
1738 client.clearTempMod(nodepos);
1742 if(nodig_delay_counter > 0.0)
1744 nodig_delay_counter -= dtime;
1748 if(nodepos != nodepos_old)
1750 infostream<<"Pointing at ("<<nodepos.X<<","
1751 <<nodepos.Y<<","<<nodepos.Z<<")"<<std::endl;
1753 if(nodepos_old != v3s16(-32768,-32768,-32768))
1755 client.clearTempMod(nodepos_old);
1757 nodepos_old = v3s16(-32768,-32768,-32768);
1761 if(input->getLeftClicked() ||
1762 (input->getLeftState() && nodepos != nodepos_old))
1764 infostream<<"Started digging"<<std::endl;
1765 client.groundAction(0, nodepos, neighbourpos, g_selected_item);
1767 if(input->getLeftClicked())
1769 client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, 0));
1771 if(input->getLeftState())
1773 MapNode n = client.getNode(nodepos);
1775 // Get tool name. Default is "" = bare hands
1776 std::string toolname = "";
1777 InventoryList *mlist = local_inventory.getList("main");
1780 InventoryItem *item = mlist->getItem(g_selected_item);
1781 if(item && (std::string)item->getName() == "ToolItem")
1783 ToolItem *titem = (ToolItem*)item;
1784 toolname = titem->getToolName();
1788 // Get digging properties for material and tool
1789 content_t material = n.getContent();
1790 DiggingProperties prop =
1791 getDiggingProperties(material, toolname);
1793 float dig_time_complete = 0.0;
1795 if(prop.diggable == false)
1797 /*infostream<<"Material "<<(int)material
1798 <<" not diggable with \""
1799 <<toolname<<"\""<<std::endl;*/
1800 // I guess nobody will wait for this long
1801 dig_time_complete = 10000000.0;
1805 dig_time_complete = prop.time;
1808 if(dig_time_complete >= 0.001)
1810 dig_index = (u16)((float)CRACK_ANIMATION_LENGTH
1811 * dig_time/dig_time_complete);
1813 // This is for torches
1816 dig_index = CRACK_ANIMATION_LENGTH;
1819 if(dig_index < CRACK_ANIMATION_LENGTH)
1821 //TimeTaker timer("client.setTempMod");
1822 //infostream<<"dig_index="<<dig_index<<std::endl;
1823 client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, dig_index));
1827 infostream<<"Digging completed"<<std::endl;
1828 client.groundAction(3, nodepos, neighbourpos, g_selected_item);
1829 client.clearTempMod(nodepos);
1830 client.removeNode(nodepos);
1834 nodig_delay_counter = dig_time_complete
1835 / (float)CRACK_ANIMATION_LENGTH;
1837 // We don't want a corresponding delay to
1838 // very time consuming nodes
1839 if(nodig_delay_counter > 0.5)
1841 nodig_delay_counter = 0.5;
1843 // We want a slight delay to very little
1844 // time consuming nodes
1845 float mindelay = 0.15;
1846 if(nodig_delay_counter < mindelay)
1848 nodig_delay_counter = mindelay;
1854 camera.setDigging(0); // left click animation
1859 if(input->getRightClicked())
1861 infostream<<"Ground right-clicked"<<std::endl;
1863 // If metadata provides an inventory view, activate it
1864 if(meta && meta->getInventoryDrawSpecString() != "" && !random_input)
1866 infostream<<"Launching custom inventory view"<<std::endl;
1868 Construct the unique identification string of the node
1870 std::string current_name;
1871 current_name += "nodemeta:";
1872 current_name += itos(nodepos.X);
1873 current_name += ",";
1874 current_name += itos(nodepos.Y);
1875 current_name += ",";
1876 current_name += itos(nodepos.Z);
1882 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
1884 GUIInventoryMenu::makeDrawSpecArrayFromString(
1886 meta->getInventoryDrawSpecString(),
1889 GUIInventoryMenu *menu =
1890 new GUIInventoryMenu(guienv, guiroot, -1,
1891 &g_menumgr, invsize,
1892 client.getInventoryContext(),
1894 menu->setDrawSpec(draw_spec);
1897 else if(meta && meta->typeId() == CONTENT_SIGN_WALL && !random_input)
1899 infostream<<"Sign node right-clicked"<<std::endl;
1901 SignNodeMetadata *signmeta = (SignNodeMetadata*)meta;
1903 // Get a new text for it
1905 TextDest *dest = new TextDestSignNode(nodepos, &client);
1907 std::wstring wtext =
1908 narrow_to_wide(signmeta->getText());
1910 (new GUITextInputMenu(guienv, guiroot, -1,
1916 client.groundAction(1, nodepos, neighbourpos, g_selected_item);
1917 camera.setDigging(1); // right click animation
1921 nodepos_old = nodepos;
1924 } // selected_object == NULL
1926 if(left_punch || (input->getLeftClicked() && !left_punch_muted))
1928 camera.setDigging(0); // left click animation
1931 input->resetLeftClicked();
1932 input->resetRightClicked();
1934 if(input->getLeftReleased())
1936 infostream<<"Left button released (stopped digging)"
1938 client.groundAction(2, v3s16(0,0,0), v3s16(0,0,0), 0);
1940 if(input->getRightReleased())
1942 //inostream<<DTIME<<"Right released"<<std::endl;
1946 input->resetLeftReleased();
1947 input->resetRightReleased();
1950 Calculate stuff for drawing
1953 u32 daynight_ratio = client.getDayNightRatio();
1954 u8 l = decode_light((daynight_ratio * LIGHT_SUN) / 1000);
1955 video::SColor bgcolor = video::SColor(
1957 bgcolor_bright.getRed() * l / 255,
1958 bgcolor_bright.getGreen() * l / 255,
1959 bgcolor_bright.getBlue() * l / 255);
1960 /*skycolor.getRed() * l / 255,
1961 skycolor.getGreen() * l / 255,
1962 skycolor.getBlue() * l / 255);*/
1964 float brightness = (float)l/255.0;
1969 if(fabs(brightness - old_brightness) > 0.01)
1970 update_skybox(driver, smgr, skybox, brightness);
1977 clouds->step(dtime);
1978 clouds->update(v2f(player_position.X, player_position.Z),
1979 0.05+brightness*0.95);
1987 farmesh_range = draw_control.wanted_range * 10;
1988 if(draw_control.range_all && farmesh_range < 500)
1989 farmesh_range = 500;
1990 if(farmesh_range > 1000)
1991 farmesh_range = 1000;
1993 farmesh->step(dtime);
1994 farmesh->update(v2f(player_position.X, player_position.Z),
1995 0.05+brightness*0.95, farmesh_range);
1998 // Store brightness value
1999 old_brightness = brightness;
2005 if(g_settings->getBool("enable_fog") == true && !force_fog_off)
2010 range = BS*farmesh_range;
2014 range = draw_control.wanted_range*BS + MAP_BLOCKSIZE*BS*1.5;
2016 if(draw_control.range_all)
2019 range = range * 0.5 + 25*BS;*/
2024 video::EFT_FOG_LINEAR,
2036 video::EFT_FOG_LINEAR,
2046 Update gui stuff (0ms)
2049 //TimeTaker guiupdatetimer("Gui updating");
2052 static float drawtime_avg = 0;
2053 drawtime_avg = drawtime_avg * 0.95 + (float)drawtime*0.05;
2054 static float beginscenetime_avg = 0;
2055 beginscenetime_avg = beginscenetime_avg * 0.95 + (float)beginscenetime*0.05;
2056 static float scenetime_avg = 0;
2057 scenetime_avg = scenetime_avg * 0.95 + (float)scenetime*0.05;
2058 static float endscenetime_avg = 0;
2059 endscenetime_avg = endscenetime_avg * 0.95 + (float)endscenetime*0.05;
2062 snprintf(temptext, 300, "Minetest-c55 %s ("
2065 " drawtime=%.0f, beginscenetime=%.0f"
2066 ", scenetime=%.0f, endscenetime=%.0f",
2068 draw_control.range_all,
2075 guitext->setText(narrow_to_wide(temptext).c_str());
2080 snprintf(temptext, 300,
2081 "(% .1f, % .1f, % .1f)"
2082 " (% .3f < btime_jitter < % .3f"
2083 ", dtime_jitter = % .1f %%"
2084 ", v_range = %.1f, RTT = %.3f)",
2085 player_position.X/BS,
2086 player_position.Y/BS,
2087 player_position.Z/BS,
2088 busytime_jitter1_min_sample,
2089 busytime_jitter1_max_sample,
2090 dtime_jitter1_max_fraction * 100.0,
2091 draw_control.wanted_range,
2095 guitext2->setText(narrow_to_wide(temptext).c_str());
2099 guitext_info->setText(infotext.c_str());
2103 Get chat messages from client
2107 std::wstring message;
2108 while(client.getChatMessage(message))
2110 chat_lines.push_back(ChatLine(message));
2111 /*if(chat_lines.size() > 6)
2113 core::list<ChatLine>::Iterator
2114 i = chat_lines.begin();
2115 chat_lines.erase(i);
2118 // Append them to form the whole static text and throw
2119 // it to the gui element
2121 // This will correspond to the line number counted from
2122 // top to bottom, from size-1 to 0
2123 s16 line_number = chat_lines.size();
2124 // Count of messages to be removed from the top
2125 u16 to_be_removed_count = 0;
2126 for(core::list<ChatLine>::Iterator
2127 i = chat_lines.begin();
2128 i != chat_lines.end(); i++)
2130 // After this, line number is valid for this loop
2135 This results in a maximum age of 60*6 to the
2136 lowermost line and a maximum of 6 lines
2138 float allowed_age = (6-line_number) * 60.0;
2140 if((*i).age > allowed_age)
2142 to_be_removed_count++;
2145 whole += (*i).text + L'\n';
2147 for(u16 i=0; i<to_be_removed_count; i++)
2149 core::list<ChatLine>::Iterator
2150 it = chat_lines.begin();
2151 chat_lines.erase(it);
2153 guitext_chat->setText(whole.c_str());
2155 // Update gui element size and position
2157 /*core::rect<s32> rect(
2159 screensize.Y - guitext_chat_pad_bottom
2160 - text_height*chat_lines.size(),
2162 screensize.Y - guitext_chat_pad_bottom
2164 core::rect<s32> rect(
2168 50 + guitext_chat->getTextHeight()
2171 guitext_chat->setRelativePosition(rect);
2173 // Don't show chat if empty or profiler is enabled
2174 if(chat_lines.size() == 0 || show_profiler)
2175 guitext_chat->setVisible(false);
2177 guitext_chat->setVisible(true);
2184 static u16 old_selected_item = 65535;
2185 if(client.getLocalInventoryUpdated()
2186 || g_selected_item != old_selected_item)
2188 client.selectPlayerItem(g_selected_item);
2189 old_selected_item = g_selected_item;
2190 //infostream<<"Updating local inventory"<<std::endl;
2191 client.getLocalInventory(local_inventory);
2193 // Update wielded tool
2194 InventoryList *mlist = local_inventory.getList("main");
2195 InventoryItem *item = NULL;
2197 item = mlist->getItem(g_selected_item);
2202 Send actions returned by the inventory menu
2204 while(inventory_action_queue.size() != 0)
2206 InventoryAction *a = inventory_action_queue.pop_front();
2208 client.sendInventoryAction(a);
2217 TimeTaker drawtimer("Drawing");
2221 TimeTaker timer("beginScene");
2222 driver->beginScene(true, true, bgcolor);
2223 //driver->beginScene(false, true, bgcolor);
2224 beginscenetime = timer.stop(true);
2229 //infostream<<"smgr->drawAll()"<<std::endl;
2232 TimeTaker timer("smgr");
2234 scenetime = timer.stop(true);
2238 //TimeTaker timer9("auxiliary drawings");
2242 //TimeTaker //timer10("//timer10");
2248 driver->setMaterial(m);
2250 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
2252 for(core::list< core::aabbox3d<f32> >::Iterator i=hilightboxes.begin();
2253 i != hilightboxes.end(); i++)
2255 /*infostream<<"hilightbox min="
2256 <<"("<<i->MinEdge.X<<","<<i->MinEdge.Y<<","<<i->MinEdge.Z<<")"
2258 <<"("<<i->MaxEdge.X<<","<<i->MaxEdge.Y<<","<<i->MaxEdge.Z<<")"
2260 driver->draw3DBox(*i, video::SColor(255,0,0,0));
2267 // Warning: This clears the Z buffer.
2268 camera.drawWieldedTool();
2275 client.renderPostFx();
2281 if(g_settings->getBool("frametime_graph") == true)
2284 for(core::list<float>::Iterator
2285 i = frametime_log.begin();
2286 i != frametime_log.end();
2289 driver->draw2DLine(v2s32(x,50),
2290 v2s32(x,50+(*i)*1000),
2291 video::SColor(255,255,255,255));
2299 driver->draw2DLine(displaycenter - core::vector2d<s32>(10,0),
2300 displaycenter + core::vector2d<s32>(10,0),
2301 video::SColor(255,255,255,255));
2302 driver->draw2DLine(displaycenter - core::vector2d<s32>(0,10),
2303 displaycenter + core::vector2d<s32>(0,10),
2304 video::SColor(255,255,255,255));
2309 //TimeTaker //timer11("//timer11");
2321 draw_hotbar(driver, font, v2s32(displaycenter.X, screensize.Y),
2322 hotbar_imagesize, hotbar_itemcount, &local_inventory,
2329 if(damage_flash_timer > 0.0)
2331 damage_flash_timer -= dtime;
2333 video::SColor color(128,255,0,0);
2334 driver->draw2DRectangle(color,
2335 core::rect<s32>(0,0,screensize.X,screensize.Y),
2343 TimeTaker timer("endScene");
2345 endscenetime = timer.stop(true);
2348 drawtime = drawtimer.stop(true);
2354 static s16 lastFPS = 0;
2355 //u16 fps = driver->getFPS();
2356 u16 fps = (1.0/dtime_avg1);
2360 core::stringw str = L"Minetest [";
2361 str += driver->getName();
2365 device->setWindowCaption(str.c_str());
2377 Draw a "shutting down" screen, which will be shown while the map
2378 generator and other stuff quits
2381 /*gui::IGUIStaticText *gui_shuttingdowntext = */
2382 draw_load_screen(L"Shutting down stuff...", driver, font);
2383 /*driver->beginScene(true, true, video::SColor(255,0,0,0));
2386 gui_shuttingdowntext->remove();*/