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"
48 TODO: Move content-aware stuff to separate file by adding properties
49 and virtual interfaces
51 #include "content_mapnode.h"
52 #include "content_nodemeta.h"
55 Setting this to 1 enables a special camera mode that forces
56 the renderers to think that the camera statically points from
57 the starting place to a static direction.
59 This allows one to move around with the player and see what
60 is actually drawn behind solid things and behind the player.
62 #define FIELD_OF_VIEW_TEST 0
72 ChatLine(const std::wstring &a_text):
85 // Inventory actions from the menu are buffered here before sending
86 Queue<InventoryAction*> inventory_action_queue;
87 // This is a copy of the inventory that the client's environment has
88 Inventory local_inventory;
90 u16 g_selected_item = 0;
96 struct TextDestChat : public TextDest
98 TextDestChat(Client *client)
102 void gotText(std::wstring text)
104 // Discard empty line
109 m_client->sendChatMessage(text);
111 m_client->addChatMessage(text);
117 struct TextDestSignNode : public TextDest
119 TextDestSignNode(v3s16 p, Client *client)
124 void gotText(std::wstring text)
126 std::string ntext = wide_to_narrow(text);
127 infostream<<"Changing text of a sign node: "
129 m_client->sendSignNodeText(m_p, ntext);
136 /* Respawn menu callback */
138 class MainRespawnInitiator: public IRespawnInitiator
141 MainRespawnInitiator(bool *active, Client *client):
142 m_active(active), m_client(client)
149 m_client->sendRespawn();
159 void draw_hotbar(video::IVideoDriver *driver, gui::IGUIFont *font,
160 v2s32 centerlowerpos, s32 imgsize, s32 itemcount,
161 Inventory *inventory, s32 halfheartcount)
163 InventoryList *mainlist = inventory->getList("main");
166 errorstream<<"draw_hotbar(): mainlist == NULL"<<std::endl;
170 s32 padding = imgsize/12;
171 //s32 height = imgsize + padding*2;
172 s32 width = itemcount*(imgsize+padding*2);
174 // Position of upper left corner of bar
175 v2s32 pos = centerlowerpos - v2s32(width/2, imgsize+padding*2);
177 // Draw background color
178 /*core::rect<s32> barrect(0,0,width,height);
180 video::SColor bgcolor(255,128,128,128);
181 driver->draw2DRectangle(bgcolor, barrect, NULL);*/
183 core::rect<s32> imgrect(0,0,imgsize,imgsize);
185 for(s32 i=0; i<itemcount; i++)
187 InventoryItem *item = mainlist->getItem(i);
189 core::rect<s32> rect = imgrect + pos
190 + v2s32(padding+i*(imgsize+padding*2), padding);
192 if(g_selected_item == i)
194 video::SColor c_outside(255,255,0,0);
195 //video::SColor c_outside(255,0,0,0);
196 //video::SColor c_inside(255,192,192,192);
197 s32 x1 = rect.UpperLeftCorner.X;
198 s32 y1 = rect.UpperLeftCorner.Y;
199 s32 x2 = rect.LowerRightCorner.X;
200 s32 y2 = rect.LowerRightCorner.Y;
201 // Black base borders
202 driver->draw2DRectangle(c_outside,
204 v2s32(x1 - padding, y1 - padding),
205 v2s32(x2 + padding, y1)
207 driver->draw2DRectangle(c_outside,
209 v2s32(x1 - padding, y2),
210 v2s32(x2 + padding, y2 + padding)
212 driver->draw2DRectangle(c_outside,
214 v2s32(x1 - padding, y1),
217 driver->draw2DRectangle(c_outside,
220 v2s32(x2 + padding, y2)
222 /*// Light inside borders
223 driver->draw2DRectangle(c_inside,
225 v2s32(x1 - padding/2, y1 - padding/2),
226 v2s32(x2 + padding/2, y1)
228 driver->draw2DRectangle(c_inside,
230 v2s32(x1 - padding/2, y2),
231 v2s32(x2 + padding/2, y2 + padding/2)
233 driver->draw2DRectangle(c_inside,
235 v2s32(x1 - padding/2, y1),
238 driver->draw2DRectangle(c_inside,
241 v2s32(x2 + padding/2, y2)
246 video::SColor bgcolor2(128,0,0,0);
247 driver->draw2DRectangle(bgcolor2, rect, NULL);
251 drawInventoryItem(driver, font, item, rect, NULL);
259 video::ITexture *heart_texture =
260 driver->getTexture(getTexturePath("heart.png").c_str());
261 v2s32 p = pos + v2s32(0, -20);
262 for(s32 i=0; i<halfheartcount/2; i++)
264 const video::SColor color(255,255,255,255);
265 const video::SColor colors[] = {color,color,color,color};
266 core::rect<s32> rect(0,0,16,16);
268 driver->draw2DImage(heart_texture, rect,
269 core::rect<s32>(core::position2d<s32>(0,0),
270 core::dimension2di(heart_texture->getOriginalSize())),
274 if(halfheartcount % 2 == 1)
276 const video::SColor color(255,255,255,255);
277 const video::SColor colors[] = {color,color,color,color};
278 core::rect<s32> rect(0,0,16/2,16);
280 core::dimension2di srcd(heart_texture->getOriginalSize());
282 driver->draw2DImage(heart_texture, rect,
283 core::rect<s32>(core::position2d<s32>(0,0), srcd),
291 Find what the player is pointing at
293 void getPointedNode(Client *client, v3f player_position,
294 v3f camera_direction, v3f camera_position,
295 bool &nodefound, core::line3d<f32> shootline,
296 v3s16 &nodepos, v3s16 &neighbourpos,
297 core::aabbox3d<f32> &nodehilightbox,
300 f32 mindistance = BS * 1001;
302 v3s16 pos_i = floatToInt(player_position, BS);
304 /*infostream<<"pos_i=("<<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z<<")"
308 s16 ystart = pos_i.Y + 0 - (camera_direction.Y<0 ? a : 1);
309 s16 zstart = pos_i.Z - (camera_direction.Z<0 ? a : 1);
310 s16 xstart = pos_i.X - (camera_direction.X<0 ? a : 1);
311 s16 yend = pos_i.Y + 1 + (camera_direction.Y>0 ? a : 1);
312 s16 zend = pos_i.Z + (camera_direction.Z>0 ? a : 1);
313 s16 xend = pos_i.X + (camera_direction.X>0 ? a : 1);
315 for(s16 y = ystart; y <= yend; y++)
316 for(s16 z = zstart; z <= zend; z++)
317 for(s16 x = xstart; x <= xend; x++)
322 n = client->getNode(v3s16(x,y,z));
323 if(content_pointable(n.getContent()) == false)
326 catch(InvalidPositionException &e)
332 v3f npf = intToFloat(np, BS);
337 v3s16(0,0,1), // back
339 v3s16(1,0,0), // right
340 v3s16(0,0,-1), // front
341 v3s16(0,-1,0), // bottom
342 v3s16(-1,0,0), // left
348 if(n.getContent() == CONTENT_TORCH)
350 v3s16 dir = unpackDir(n.param2);
351 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
352 dir_f *= BS/2 - BS/6 - BS/20;
353 v3f cpf = npf + dir_f;
354 f32 distance = (cpf - camera_position).getLength();
356 core::aabbox3d<f32> box;
359 if(dir == v3s16(0,-1,0))
361 box = core::aabbox3d<f32>(
362 npf - v3f(BS/6, BS/2, BS/6),
363 npf + v3f(BS/6, -BS/2+BS/3*2, BS/6)
367 else if(dir == v3s16(0,1,0))
369 box = core::aabbox3d<f32>(
370 npf - v3f(BS/6, -BS/2+BS/3*2, BS/6),
371 npf + v3f(BS/6, BS/2, BS/6)
377 box = core::aabbox3d<f32>(
378 cpf - v3f(BS/6, BS/3, BS/6),
379 cpf + v3f(BS/6, BS/3, BS/6)
383 if(distance < mindistance)
385 if(box.intersectsWithLine(shootline))
390 mindistance = distance;
391 nodehilightbox = box;
395 else if(n.getContent() == CONTENT_SIGN_WALL)
397 v3s16 dir = unpackDir(n.param2);
398 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
399 dir_f *= BS/2 - BS/6 - BS/20;
400 v3f cpf = npf + dir_f;
401 f32 distance = (cpf - camera_position).getLength();
405 v3f(BS*0.42,-BS*0.35,-BS*0.4),
406 v3f(BS*0.49, BS*0.35, BS*0.4),
409 for(s32 i=0; i<2; i++)
411 if(dir == v3s16(1,0,0))
412 vertices[i].rotateXZBy(0);
413 if(dir == v3s16(-1,0,0))
414 vertices[i].rotateXZBy(180);
415 if(dir == v3s16(0,0,1))
416 vertices[i].rotateXZBy(90);
417 if(dir == v3s16(0,0,-1))
418 vertices[i].rotateXZBy(-90);
419 if(dir == v3s16(0,-1,0))
420 vertices[i].rotateXYBy(-90);
421 if(dir == v3s16(0,1,0))
422 vertices[i].rotateXYBy(90);
427 core::aabbox3d<f32> box;
429 box = core::aabbox3d<f32>(vertices[0]);
430 box.addInternalPoint(vertices[1]);
432 if(distance < mindistance)
434 if(box.intersectsWithLine(shootline))
439 mindistance = distance;
440 nodehilightbox = box;
445 else if(n.getContent() == CONTENT_LADDER)
447 v3s16 dir = unpackDir(n.param2);
448 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
449 dir_f *= BS/2 - BS/6 - BS/20;
450 v3f cpf = npf + dir_f;
451 f32 distance = (cpf - camera_position).getLength();
455 v3f(BS*0.42,-BS/2,-BS/2),
456 v3f(BS*0.49, BS/2, BS/2),
459 for(s32 i=0; i<2; i++)
461 if(dir == v3s16(1,0,0))
462 vertices[i].rotateXZBy(0);
463 if(dir == v3s16(-1,0,0))
464 vertices[i].rotateXZBy(180);
465 if(dir == v3s16(0,0,1))
466 vertices[i].rotateXZBy(90);
467 if(dir == v3s16(0,0,-1))
468 vertices[i].rotateXZBy(-90);
469 if(dir == v3s16(0,-1,0))
470 vertices[i].rotateXYBy(-90);
471 if(dir == v3s16(0,1,0))
472 vertices[i].rotateXYBy(90);
477 core::aabbox3d<f32> box;
479 box = core::aabbox3d<f32>(vertices[0]);
480 box.addInternalPoint(vertices[1]);
482 if(distance < mindistance)
484 if(box.intersectsWithLine(shootline))
489 mindistance = distance;
490 nodehilightbox = box;
494 else if(n.getContent() == CONTENT_RAIL)
496 v3s16 dir = unpackDir(n.param0);
497 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
498 dir_f *= BS/2 - BS/6 - BS/20;
499 v3f cpf = npf + dir_f;
500 f32 distance = (cpf - camera_position).getLength();
502 float d = (float)BS/16;
505 v3f(BS/2, -BS/2+d, -BS/2),
506 v3f(-BS/2, -BS/2, BS/2),
509 for(s32 i=0; i<2; i++)
514 core::aabbox3d<f32> box;
516 box = core::aabbox3d<f32>(vertices[0]);
517 box.addInternalPoint(vertices[1]);
519 if(distance < mindistance)
521 if(box.intersectsWithLine(shootline))
526 mindistance = distance;
527 nodehilightbox = box;
536 for(u16 i=0; i<6; i++)
538 v3f dir_f = v3f(dirs[i].X,
539 dirs[i].Y, dirs[i].Z);
540 v3f centerpoint = npf + dir_f * BS/2;
542 (centerpoint - camera_position).getLength();
544 if(distance < mindistance)
546 core::CMatrix4<f32> m;
547 m.buildRotateFromTo(v3f(0,0,1), dir_f);
549 // This is the back face
551 v3f(BS/2, BS/2, BS/2),
552 v3f(-BS/2, -BS/2, BS/2+d)
555 for(u16 j=0; j<2; j++)
557 m.rotateVect(corners[j]);
561 core::aabbox3d<f32> facebox(corners[0]);
562 facebox.addInternalPoint(corners[1]);
564 if(facebox.intersectsWithLine(shootline))
568 neighbourpos = np + dirs[i];
569 mindistance = distance;
571 //nodehilightbox = facebox;
573 const float d = 0.502;
574 core::aabbox3d<f32> nodebox
575 (-BS*d, -BS*d, -BS*d, BS*d, BS*d, BS*d);
576 v3f nodepos_f = intToFloat(nodepos, BS);
577 nodebox.MinEdge += nodepos_f;
578 nodebox.MaxEdge += nodepos_f;
579 nodehilightbox = nodebox;
581 } // if distance < mindistance
587 void update_skybox(video::IVideoDriver* driver,
588 scene::ISceneManager* smgr, scene::ISceneNode* &skybox,
596 /*// Disable skybox if FarMesh is enabled
597 if(g_settings->getBool("enable_farmesh"))
600 if(brightness >= 0.5)
602 skybox = smgr->addSkyBoxSceneNode(
603 driver->getTexture(getTexturePath("skybox2.png").c_str()),
604 driver->getTexture(getTexturePath("skybox3.png").c_str()),
605 driver->getTexture(getTexturePath("skybox1.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()));
610 else if(brightness >= 0.2)
612 skybox = smgr->addSkyBoxSceneNode(
613 driver->getTexture(getTexturePath("skybox2_dawn.png").c_str()),
614 driver->getTexture(getTexturePath("skybox3_dawn.png").c_str()),
615 driver->getTexture(getTexturePath("skybox1_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()));
622 skybox = smgr->addSkyBoxSceneNode(
623 driver->getTexture(getTexturePath("skybox2_night.png").c_str()),
624 driver->getTexture(getTexturePath("skybox3_night.png").c_str()),
625 driver->getTexture(getTexturePath("skybox1_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()));
633 Draws a screen with a single text on it.
634 Text will be removed when the screen is drawn the next time.
636 /*gui::IGUIStaticText **/
637 void draw_load_screen(const std::wstring &text,
638 video::IVideoDriver* driver, gui::IGUIFont* font)
640 v2u32 screensize = driver->getScreenSize();
641 const wchar_t *loadingtext = text.c_str();
642 core::vector2d<u32> textsize_u = font->getDimension(loadingtext);
643 core::vector2d<s32> textsize(textsize_u.X,textsize_u.Y);
644 core::vector2d<s32> center(screensize.X/2, screensize.Y/2);
645 core::rect<s32> textrect(center - textsize/2, center + textsize/2);
647 gui::IGUIStaticText *guitext = guienv->addStaticText(
648 loadingtext, textrect, false, false);
649 guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
651 driver->beginScene(true, true, video::SColor(255,0,0,0));
664 IrrlichtDevice *device,
667 std::string playername,
668 std::string password,
671 std::wstring &error_message,
672 std::string configpath
675 video::IVideoDriver* driver = device->getVideoDriver();
676 scene::ISceneManager* smgr = device->getSceneManager();
678 // Calculate text height using the font
679 u32 text_height = font->getDimension(L"Random test string").Height;
681 v2u32 screensize(0,0);
682 v2u32 last_screensize(0,0);
683 screensize = driver->getScreenSize();
685 const s32 hotbar_itemcount = 8;
686 //const s32 hotbar_imagesize = 36;
687 //const s32 hotbar_imagesize = 64;
688 s32 hotbar_imagesize = 48;
690 // The color of the sky
692 //video::SColor skycolor = video::SColor(255,140,186,250);
694 video::SColor bgcolor_bright = video::SColor(255,170,200,230);
697 Draw "Loading" screen
699 /*gui::IGUIStaticText *gui_loadingtext = */
700 //draw_load_screen(L"Loading and connecting...", driver, font);
702 draw_load_screen(L"Loading...", driver, font);
706 SharedPtr will delete it when it goes out of scope.
708 SharedPtr<Server> server;
710 draw_load_screen(L"Creating server...", driver, font);
711 infostream<<"Creating server"<<std::endl;
712 server = new Server(map_dir, configpath);
720 draw_load_screen(L"Creating client...", driver, font);
721 infostream<<"Creating client"<<std::endl;
722 MapDrawControl draw_control;
723 Client client(device, playername.c_str(), password, draw_control);
725 draw_load_screen(L"Resolving address...", driver, font);
726 Address connect_address(0,0,0,0, port);
729 //connect_address.Resolve("localhost");
730 connect_address.setAddress(127,0,0,1);
732 connect_address.Resolve(address.c_str());
734 catch(ResolveError &e)
736 errorstream<<"Couldn't resolve address"<<std::endl;
738 error_message = L"Couldn't resolve address";
739 //gui_loadingtext->remove();
744 Attempt to connect to the server
747 infostream<<"Connecting to server at ";
748 connect_address.print(&infostream);
749 infostream<<std::endl;
750 client.connect(connect_address);
752 bool could_connect = false;
755 float time_counter = 0.0;
758 if(client.connectedAndInitialized())
760 could_connect = true;
763 if(client.accessDenied())
767 // Wait for 10 seconds
768 if(time_counter >= 10.0)
773 std::wostringstream ss;
774 ss<<L"Connecting to server... (timeout in ";
775 ss<<(int)(10.0 - time_counter + 1.0);
777 draw_load_screen(ss.str(), driver, font);
780 driver->beginScene(true, true, video::SColor(255,0,0,0));
782 driver->endScene();*/
784 // Update client and server
796 catch(con::PeerNotFoundException &e)
799 if(could_connect == false)
801 if(client.accessDenied())
803 error_message = L"Access denied. Reason: "
804 +client.accessDeniedReason();
805 errorstream<<wide_to_narrow(error_message)<<std::endl;
809 error_message = L"Connection timed out.";
810 errorstream<<"Timed out."<<std::endl;
812 //gui_loadingtext->remove();
819 float old_brightness = 1.0;
820 scene::ISceneNode* skybox = NULL;
821 update_skybox(driver, smgr, skybox, 1.0);
824 Create the camera node
826 Camera camera(smgr, draw_control);
827 if (!camera.successfullyCreated(error_message))
830 f32 camera_yaw = 0; // "right/left"
831 f32 camera_pitch = 0; // "up/down"
837 float cloud_height = BS*100;
838 Clouds *clouds = NULL;
839 if(g_settings->getBool("enable_clouds"))
841 clouds = new Clouds(smgr->getRootSceneNode(), smgr, -1,
842 cloud_height, time(0));
849 FarMesh *farmesh = NULL;
850 if(g_settings->getBool("enable_farmesh"))
852 farmesh = new FarMesh(smgr->getRootSceneNode(), smgr, -1, client.getMapSeed(), &client);
859 //gui_loadingtext->remove();
865 // First line of debug text
866 gui::IGUIStaticText *guitext = guienv->addStaticText(
868 core::rect<s32>(5, 5, 795, 5+text_height),
870 // Second line of debug text
871 gui::IGUIStaticText *guitext2 = guienv->addStaticText(
873 core::rect<s32>(5, 5+(text_height+5)*1, 795, (5+text_height)*2),
875 // At the middle of the screen
876 // Object infos are shown in this
877 gui::IGUIStaticText *guitext_info = guienv->addStaticText(
879 core::rect<s32>(0,0,400,text_height+5) + v2s32(100,200),
883 gui::IGUIStaticText *guitext_chat = guienv->addStaticText(
885 core::rect<s32>(0,0,0,0),
886 //false, false); // Disable word wrap as of now
888 //guitext_chat->setBackgroundColor(video::SColor(96,0,0,0));
889 core::list<ChatLine> chat_lines;
891 // Profiler text (size is updated when text is updated)
892 gui::IGUIStaticText *guitext_profiler = guienv->addStaticText(
894 core::rect<s32>(6, 4+(text_height+5)*2, 400,
895 (text_height+5)*2 + text_height*35),
897 guitext_profiler->setBackgroundColor(video::SColor(80,0,0,0));
898 guitext_profiler->setVisible(false);
900 /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
901 (guienv, NULL, v2s32(10, 70), 5, &local_inventory);*/
902 /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
903 (guienv, NULL, v2s32(0, 0), quickinv_itemcount, &local_inventory);*/
905 // Test the text input system
906 /*(new GUITextInputMenu(guienv, guiroot, -1, &g_menumgr,
908 /*GUIMessageMenu *menu =
909 new GUIMessageMenu(guienv, guiroot, -1,
915 (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
916 &g_menumgr))->drop();
919 /*guitext2->setVisible(true);
920 guitext_info->setVisible(true);
921 guitext_chat->setVisible(true);*/
923 //s32 guitext_chat_pad_bottom = 70;
926 Some statistics are collected in these
929 u32 beginscenetime = 0;
931 u32 endscenetime = 0;
934 //throw con::PeerNotFoundException("lol");
936 core::list<float> frametime_log;
938 float nodig_delay_counter = 0.0;
939 float dig_time = 0.0;
941 v3s16 nodepos_old(-32768,-32768,-32768);
943 float damage_flash_timer = 0;
944 s16 farmesh_range = 20*MAP_BLOCKSIZE;
946 const float object_hit_delay = 0.5;
947 float object_hit_delay_timer = 0.0;
949 bool invert_mouse = g_settings->getBool("invert_mouse");
951 bool respawn_menu_active = false;
953 bool show_profiler = false;
954 bool force_fog_off = false;
955 bool disable_camera_update = false;
961 bool first_loop_after_window_activation = true;
963 // TODO: Convert the static interval timers to these
964 // Interval limiter for profiler
965 IntervalLimiter m_profiler_interval;
967 // Time is in milliseconds
968 // NOTE: getRealTime() causes strange problems in wine (imprecision?)
969 // NOTE: So we have to use getTime() and call run()s between them
970 u32 lasttime = device->getTimer()->getTime();
972 while(device->run() && kill == false)
974 //std::cerr<<"frame"<<std::endl;
976 if(client.accessDenied())
978 error_message = L"Access denied. Reason: "
979 +client.accessDeniedReason();
980 errorstream<<wide_to_narrow(error_message)<<std::endl;
984 if(g_gamecallback->disconnect_requested)
986 g_gamecallback->disconnect_requested = false;
990 if(g_gamecallback->changepassword_requested)
992 (new GUIPasswordChange(guienv, guiroot, -1,
993 &g_menumgr, &client))->drop();
994 g_gamecallback->changepassword_requested = false;
998 Process TextureSource's queue
1000 ((TextureSource*)g_texturesource)->processQueue();
1005 last_screensize = screensize;
1006 screensize = driver->getScreenSize();
1007 v2s32 displaycenter(screensize.X/2,screensize.Y/2);
1008 //bool screensize_changed = screensize != last_screensize;
1011 if(screensize.Y <= 800)
1012 hotbar_imagesize = 32;
1013 else if(screensize.Y <= 1280)
1014 hotbar_imagesize = 48;
1016 hotbar_imagesize = 64;
1018 // Hilight boxes collected during the loop and displayed
1019 core::list< core::aabbox3d<f32> > hilightboxes;
1022 std::wstring infotext;
1024 // When screen size changes, update positions and sizes of stuff
1025 /*if(screensize_changed)
1027 v2s32 pos(displaycenter.X-((quickinv_itemcount-1)*quickinv_spacing+quickinv_size)/2, screensize.Y-quickinv_spacing);
1028 quick_inventory->updatePosition(pos);
1031 //TimeTaker //timer1("//timer1");
1033 // Time of frame without fps limit
1037 // not using getRealTime is necessary for wine
1038 u32 time = device->getTimer()->getTime();
1040 busytime_u32 = time - lasttime;
1043 busytime = busytime_u32 / 1000.0;
1046 //infostream<<"busytime_u32="<<busytime_u32<<std::endl;
1048 // Necessary for device->getTimer()->getTime()
1056 float fps_max = g_settings->getFloat("fps_max");
1057 u32 frametime_min = 1000./fps_max;
1059 if(busytime_u32 < frametime_min)
1061 u32 sleeptime = frametime_min - busytime_u32;
1062 device->sleep(sleeptime);
1066 // Necessary for device->getTimer()->getTime()
1070 Time difference calculation
1072 f32 dtime; // in seconds
1074 u32 time = device->getTimer()->getTime();
1076 dtime = (time - lasttime) / 1000.0;
1083 object_hit_delay_timer -= dtime;
1085 g_profiler->add("Elapsed time", dtime);
1086 g_profiler->avg("FPS", 1./dtime);
1089 Log frametime for visualization
1091 frametime_log.push_back(dtime);
1092 if(frametime_log.size() > 100)
1094 core::list<float>::Iterator i = frametime_log.begin();
1095 frametime_log.erase(i);
1099 Visualize frametime in terminal
1101 /*for(u32 i=0; i<dtime*400; i++)
1103 infostream<<std::endl;*/
1106 Time average and jitter calculation
1109 static f32 dtime_avg1 = 0.0;
1110 dtime_avg1 = dtime_avg1 * 0.96 + dtime * 0.04;
1111 f32 dtime_jitter1 = dtime - dtime_avg1;
1113 static f32 dtime_jitter1_max_sample = 0.0;
1114 static f32 dtime_jitter1_max_fraction = 0.0;
1116 static f32 jitter1_max = 0.0;
1117 static f32 counter = 0.0;
1118 if(dtime_jitter1 > jitter1_max)
1119 jitter1_max = dtime_jitter1;
1124 dtime_jitter1_max_sample = jitter1_max;
1125 dtime_jitter1_max_fraction
1126 = dtime_jitter1_max_sample / (dtime_avg1+0.001);
1132 Busytime average and jitter calculation
1135 static f32 busytime_avg1 = 0.0;
1136 busytime_avg1 = busytime_avg1 * 0.98 + busytime * 0.02;
1137 f32 busytime_jitter1 = busytime - busytime_avg1;
1139 static f32 busytime_jitter1_max_sample = 0.0;
1140 static f32 busytime_jitter1_min_sample = 0.0;
1142 static f32 jitter1_max = 0.0;
1143 static f32 jitter1_min = 0.0;
1144 static f32 counter = 0.0;
1145 if(busytime_jitter1 > jitter1_max)
1146 jitter1_max = busytime_jitter1;
1147 if(busytime_jitter1 < jitter1_min)
1148 jitter1_min = busytime_jitter1;
1152 busytime_jitter1_max_sample = jitter1_max;
1153 busytime_jitter1_min_sample = jitter1_min;
1160 Debug info for client
1163 static float counter = 0.0;
1168 client.printDebugInfo(infostream);
1175 float profiler_print_interval =
1176 g_settings->getFloat("profiler_print_interval");
1177 bool print_to_log = true;
1178 if(profiler_print_interval == 0){
1179 print_to_log = false;
1180 profiler_print_interval = 5;
1182 if(m_profiler_interval.step(dtime, profiler_print_interval))
1185 infostream<<"Profiler:"<<std::endl;
1186 g_profiler->print(infostream);
1189 std::ostringstream os(std::ios_base::binary);
1190 g_profiler->print(os);
1191 std::wstring text = narrow_to_wide(os.str());
1192 guitext_profiler->setText(text.c_str());
1194 g_profiler->clear();
1196 s32 w = font->getDimension(text.c_str()).Width;
1199 core::rect<s32> rect(6, 4+(text_height+5)*2, 12+w,
1200 8+(text_height+5)*2 +
1201 font->getDimension(text.c_str()).Height);
1202 guitext_profiler->setRelativePosition(rect);
1206 Direct handling of user input
1209 // Reset input if window not active or some menu is active
1210 if(device->isWindowActive() == false || noMenuActive() == false)
1215 // Input handler step() (used by the random input generator)
1219 Launch menus according to keys
1221 if(input->wasKeyDown(getKeySetting("keymap_inventory")))
1223 infostream<<"the_game: "
1224 <<"Launching inventory"<<std::endl;
1226 GUIInventoryMenu *menu =
1227 new GUIInventoryMenu(guienv, guiroot, -1,
1228 &g_menumgr, v2s16(8,7),
1229 client.getInventoryContext(),
1232 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
1233 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1234 "list", "current_player", "main",
1235 v2s32(0, 3), v2s32(8, 4)));
1236 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1237 "list", "current_player", "craft",
1238 v2s32(3, 0), v2s32(3, 3)));
1239 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1240 "list", "current_player", "craftresult",
1241 v2s32(7, 1), v2s32(1, 1)));
1243 menu->setDrawSpec(draw_spec);
1247 else if(input->wasKeyDown(EscapeKey))
1249 infostream<<"the_game: "
1250 <<"Launching pause menu"<<std::endl;
1251 // It will delete itself by itself
1252 (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
1253 &g_menumgr))->drop();
1255 // Move mouse cursor on top of the disconnect button
1256 input->setMousePos(displaycenter.X, displaycenter.Y+25);
1258 else if(input->wasKeyDown(getKeySetting("keymap_chat")))
1260 TextDest *dest = new TextDestChat(&client);
1262 (new GUITextInputMenu(guienv, guiroot, -1,
1266 else if(input->wasKeyDown(getKeySetting("keymap_cmd")))
1268 TextDest *dest = new TextDestChat(&client);
1270 (new GUITextInputMenu(guienv, guiroot, -1,
1274 else if(input->wasKeyDown(getKeySetting("keymap_freemove")))
1276 if(g_settings->getBool("free_move"))
1278 g_settings->set("free_move","false");
1279 chat_lines.push_back(ChatLine(L"free_move disabled"));
1283 g_settings->set("free_move","true");
1284 chat_lines.push_back(ChatLine(L"free_move enabled"));
1287 else if(input->wasKeyDown(getKeySetting("keymap_fastmove")))
1289 if(g_settings->getBool("fast_move"))
1291 g_settings->set("fast_move","false");
1292 chat_lines.push_back(ChatLine(L"fast_move disabled"));
1296 g_settings->set("fast_move","true");
1297 chat_lines.push_back(ChatLine(L"fast_move enabled"));
1300 else if(input->wasKeyDown(getKeySetting("keymap_frametime_graph")))
1302 if(g_settings->getBool("frametime_graph"))
1304 g_settings->set("frametime_graph","false");
1305 chat_lines.push_back(ChatLine(L"frametime_graph disabled"));
1309 g_settings->set("frametime_graph","true");
1310 chat_lines.push_back(ChatLine(L"frametime_graph enabled"));
1313 else if(input->wasKeyDown(getKeySetting("keymap_screenshot")))
1315 irr::video::IImage* const image = driver->createScreenShot();
1317 irr::c8 filename[256];
1318 snprintf(filename, 256, "%s" DIR_DELIM "screenshot_%u.png",
1319 g_settings->get("screenshot_path").c_str(),
1320 device->getTimer()->getRealTime());
1321 if (driver->writeImageToFile(image, filename)) {
1322 std::wstringstream sstr;
1323 sstr<<"Saved screenshot to '"<<filename<<"'";
1324 infostream<<"Saved screenshot to '"<<filename<<"'"<<std::endl;
1325 chat_lines.push_back(ChatLine(sstr.str()));
1327 infostream<<"Failed to save screenshot '"<<filename<<"'"<<std::endl;
1332 else if(input->wasKeyDown(getKeySetting("keymap_toggle_profiler")))
1334 show_profiler = !show_profiler;
1335 guitext_profiler->setVisible(show_profiler);
1337 chat_lines.push_back(ChatLine(L"Profiler disabled"));
1339 chat_lines.push_back(ChatLine(L"Profiler enabled"));
1341 else if(input->wasKeyDown(getKeySetting("keymap_toggle_force_fog_off")))
1343 force_fog_off = !force_fog_off;
1345 chat_lines.push_back(ChatLine(L"Fog disabled"));
1347 chat_lines.push_back(ChatLine(L"Fog enabled"));
1349 else if(input->wasKeyDown(getKeySetting("keymap_toggle_update_camera")))
1351 disable_camera_update = !disable_camera_update;
1352 if(disable_camera_update)
1353 chat_lines.push_back(ChatLine(L"Camera update disabled"));
1355 chat_lines.push_back(ChatLine(L"Camera update enabled"));
1358 // Item selection with mouse wheel
1360 s32 wheel = input->getMouseWheel();
1361 u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1,
1362 hotbar_itemcount-1);
1366 if(g_selected_item < max_item)
1369 g_selected_item = 0;
1373 if(g_selected_item > 0)
1376 g_selected_item = max_item;
1381 for(u16 i=0; i<10; i++)
1383 const KeyPress *kp = NumberKey + (i + 1) % 10;
1384 if(input->wasKeyDown(*kp))
1386 if(i < PLAYER_INVENTORY_SIZE && i < hotbar_itemcount)
1388 g_selected_item = i;
1390 infostream<<"Selected item: "
1391 <<g_selected_item<<std::endl;
1396 // Viewing range selection
1397 if(input->wasKeyDown(getKeySetting("keymap_rangeselect")))
1399 if(draw_control.range_all)
1401 draw_control.range_all = false;
1402 infostream<<"Disabled full viewing range"<<std::endl;
1406 draw_control.range_all = true;
1407 infostream<<"Enabled full viewing range"<<std::endl;
1411 // Print debug stacks
1412 if(input->wasKeyDown(getKeySetting("keymap_print_debug_stacks")))
1414 dstream<<"-----------------------------------------"
1416 dstream<<DTIME<<"Printing debug stacks:"<<std::endl;
1417 dstream<<"-----------------------------------------"
1419 debug_stacks_print();
1423 Mouse and camera control
1424 NOTE: Do this before client.setPlayerControl() to not cause a camera lag of one frame
1427 if((device->isWindowActive() && noMenuActive()) || random_input)
1431 // Mac OSX gets upset if this is set every frame
1432 if(device->getCursorControl()->isVisible())
1433 device->getCursorControl()->setVisible(false);
1436 if(first_loop_after_window_activation){
1437 //infostream<<"window active, first loop"<<std::endl;
1438 first_loop_after_window_activation = false;
1441 s32 dx = input->getMousePos().X - displaycenter.X;
1442 s32 dy = input->getMousePos().Y - displaycenter.Y;
1445 //infostream<<"window active, pos difference "<<dx<<","<<dy<<std::endl;
1447 /*const float keyspeed = 500;
1448 if(input->isKeyDown(irr::KEY_UP))
1449 dy -= dtime * keyspeed;
1450 if(input->isKeyDown(irr::KEY_DOWN))
1451 dy += dtime * keyspeed;
1452 if(input->isKeyDown(irr::KEY_LEFT))
1453 dx -= dtime * keyspeed;
1454 if(input->isKeyDown(irr::KEY_RIGHT))
1455 dx += dtime * keyspeed;*/
1457 camera_yaw -= dx*0.2;
1458 camera_pitch += dy*0.2;
1459 if(camera_pitch < -89.5) camera_pitch = -89.5;
1460 if(camera_pitch > 89.5) camera_pitch = 89.5;
1462 input->setMousePos(displaycenter.X, displaycenter.Y);
1465 // Mac OSX gets upset if this is set every frame
1466 if(device->getCursorControl()->isVisible() == false)
1467 device->getCursorControl()->setVisible(true);
1469 //infostream<<"window inactive"<<std::endl;
1470 first_loop_after_window_activation = true;
1474 Player speed control
1477 if(!noMenuActive() || !device->isWindowActive())
1479 PlayerControl control(
1490 client.setPlayerControl(control);
1503 PlayerControl control(
1504 input->isKeyDown(getKeySetting("keymap_forward")),
1505 input->isKeyDown(getKeySetting("keymap_backward")),
1506 input->isKeyDown(getKeySetting("keymap_left")),
1507 input->isKeyDown(getKeySetting("keymap_right")),
1508 input->isKeyDown(getKeySetting("keymap_jump")),
1509 input->isKeyDown(getKeySetting("keymap_special1")),
1510 input->isKeyDown(getKeySetting("keymap_sneak")),
1514 client.setPlayerControl(control);
1523 //TimeTaker timer("server->step(dtime)");
1524 server->step(dtime);
1532 //TimeTaker timer("client.step(dtime)");
1534 //client.step(dtime_avg1);
1538 // Read client events
1541 ClientEvent event = client.getClientEvent();
1542 if(event.type == CE_NONE)
1546 else if(event.type == CE_PLAYER_DAMAGE)
1548 //u16 damage = event.player_damage.amount;
1549 //infostream<<"Player damage: "<<damage<<std::endl;
1550 damage_flash_timer = 0.05;
1551 if(event.player_damage.amount >= 2){
1552 damage_flash_timer += 0.05 * event.player_damage.amount;
1555 else if(event.type == CE_PLAYER_FORCE_MOVE)
1557 camera_yaw = event.player_force_move.yaw;
1558 camera_pitch = event.player_force_move.pitch;
1560 else if(event.type == CE_DEATHSCREEN)
1562 if(respawn_menu_active)
1565 /*bool set_camera_point_target =
1566 event.deathscreen.set_camera_point_target;
1567 v3f camera_point_target;
1568 camera_point_target.X = event.deathscreen.camera_point_target_x;
1569 camera_point_target.Y = event.deathscreen.camera_point_target_y;
1570 camera_point_target.Z = event.deathscreen.camera_point_target_z;*/
1571 MainRespawnInitiator *respawner =
1572 new MainRespawnInitiator(
1573 &respawn_menu_active, &client);
1574 GUIDeathScreen *menu =
1575 new GUIDeathScreen(guienv, guiroot, -1,
1576 &g_menumgr, respawner);
1579 /* Handle visualization */
1581 damage_flash_timer = 0;
1583 /*LocalPlayer* player = client.getLocalPlayer();
1584 player->setPosition(player->getPosition() + v3f(0,-BS,0));
1585 camera.update(player, busytime, screensize);*/
1590 //TimeTaker //timer2("//timer2");
1592 LocalPlayer* player = client.getLocalPlayer();
1593 camera.update(player, busytime, screensize);
1596 v3f player_position = player->getPosition();
1597 v3f camera_position = camera.getPosition();
1598 v3f camera_direction = camera.getDirection();
1599 f32 camera_fov = camera.getFovMax();
1601 if(!disable_camera_update){
1602 client.updateCamera(camera_position,
1603 camera_direction, camera_fov);
1607 //TimeTaker //timer3("//timer3");
1610 Calculate what block is the crosshair pointing to
1613 //u32 t1 = device->getTimer()->getRealTime();
1615 //f32 d = 4; // max. distance
1616 f32 d = 4; // max. distance
1617 core::line3d<f32> shootline(camera_position,
1618 camera_position + camera_direction * BS * (d+1));
1620 ClientActiveObject *selected_active_object
1621 = client.getSelectedActiveObject
1622 (d*BS, camera_position, shootline);
1624 bool left_punch = false;
1625 bool left_punch_muted = false;
1627 if(selected_active_object != NULL)
1629 /* Clear possible cracking animation */
1630 if(nodepos_old != v3s16(-32768,-32768,-32768))
1632 client.clearTempMod(nodepos_old);
1634 nodepos_old = v3s16(-32768,-32768,-32768);
1637 //infostream<<"Client returned selected_active_object != NULL"<<std::endl;
1639 core::aabbox3d<f32> *selection_box
1640 = selected_active_object->getSelectionBox();
1641 // Box should exist because object was returned in the
1643 assert(selection_box);
1645 v3f pos = selected_active_object->getPosition();
1647 core::aabbox3d<f32> box_on_map(
1648 selection_box->MinEdge + pos,
1649 selection_box->MaxEdge + pos
1652 if(selected_active_object->doShowSelectionBox())
1653 hilightboxes.push_back(box_on_map);
1655 //infotext = narrow_to_wide("A ClientActiveObject");
1656 infotext = narrow_to_wide(selected_active_object->infoText());
1658 //if(input->getLeftClicked())
1659 if(input->getLeftState())
1661 bool do_punch = false;
1662 bool do_punch_damage = false;
1663 if(object_hit_delay_timer <= 0.0){
1665 do_punch_damage = true;
1666 object_hit_delay_timer = object_hit_delay;
1668 if(input->getLeftClicked()){
1672 infostream<<"Left-clicked object"<<std::endl;
1675 if(do_punch_damage){
1676 client.clickActiveObject(0,
1677 selected_active_object->getId(), g_selected_item);
1680 else if(input->getRightClicked())
1682 infostream<<"Right-clicked object"<<std::endl;
1683 client.clickActiveObject(1,
1684 selected_active_object->getId(), g_selected_item);
1687 else // selected_object == NULL
1691 Find out which node we are pointing at
1694 bool nodefound = false;
1697 core::aabbox3d<f32> nodehilightbox;
1699 getPointedNode(&client, player_position,
1700 camera_direction, camera_position,
1701 nodefound, shootline,
1702 nodepos, neighbourpos,
1706 if(nodepos_old != v3s16(-32768,-32768,-32768))
1708 client.clearTempMod(nodepos_old);
1710 nodepos_old = v3s16(-32768,-32768,-32768);
1717 hilightboxes.push_back(nodehilightbox);
1720 Check information text of node
1723 NodeMetadata *meta = client.getNodeMetadata(nodepos);
1726 infotext = narrow_to_wide(meta->infoText());
1729 //MapNode node = client.getNode(nodepos);
1735 if(input->getLeftReleased())
1737 client.clearTempMod(nodepos);
1741 if(nodig_delay_counter > 0.0)
1743 nodig_delay_counter -= dtime;
1747 if(nodepos != nodepos_old)
1749 infostream<<"Pointing at ("<<nodepos.X<<","
1750 <<nodepos.Y<<","<<nodepos.Z<<")"<<std::endl;
1752 if(nodepos_old != v3s16(-32768,-32768,-32768))
1754 client.clearTempMod(nodepos_old);
1756 nodepos_old = v3s16(-32768,-32768,-32768);
1760 if(input->getLeftClicked() ||
1761 (input->getLeftState() && nodepos != nodepos_old))
1763 infostream<<"Started digging"<<std::endl;
1764 client.groundAction(0, nodepos, neighbourpos, g_selected_item);
1766 if(input->getLeftClicked())
1768 client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, 0));
1770 if(input->getLeftState())
1772 MapNode n = client.getNode(nodepos);
1774 // Get tool name. Default is "" = bare hands
1775 std::string toolname = "";
1776 InventoryList *mlist = local_inventory.getList("main");
1779 InventoryItem *item = mlist->getItem(g_selected_item);
1780 if(item && (std::string)item->getName() == "ToolItem")
1782 ToolItem *titem = (ToolItem*)item;
1783 toolname = titem->getToolName();
1787 // Get digging properties for material and tool
1788 content_t material = n.getContent();
1789 DiggingProperties prop =
1790 getDiggingProperties(material, toolname);
1792 float dig_time_complete = 0.0;
1794 if(prop.diggable == false)
1796 /*infostream<<"Material "<<(int)material
1797 <<" not diggable with \""
1798 <<toolname<<"\""<<std::endl;*/
1799 // I guess nobody will wait for this long
1800 dig_time_complete = 10000000.0;
1804 dig_time_complete = prop.time;
1807 if(dig_time_complete >= 0.001)
1809 dig_index = (u16)((float)CRACK_ANIMATION_LENGTH
1810 * dig_time/dig_time_complete);
1812 // This is for torches
1815 dig_index = CRACK_ANIMATION_LENGTH;
1818 if(dig_index < CRACK_ANIMATION_LENGTH)
1820 //TimeTaker timer("client.setTempMod");
1821 //infostream<<"dig_index="<<dig_index<<std::endl;
1822 client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, dig_index));
1826 infostream<<"Digging completed"<<std::endl;
1827 client.groundAction(3, nodepos, neighbourpos, g_selected_item);
1828 client.clearTempMod(nodepos);
1829 client.removeNode(nodepos);
1833 nodig_delay_counter = dig_time_complete
1834 / (float)CRACK_ANIMATION_LENGTH;
1836 // We don't want a corresponding delay to
1837 // very time consuming nodes
1838 if(nodig_delay_counter > 0.5)
1840 nodig_delay_counter = 0.5;
1842 // We want a slight delay to very little
1843 // time consuming nodes
1844 float mindelay = 0.15;
1845 if(nodig_delay_counter < mindelay)
1847 nodig_delay_counter = mindelay;
1853 camera.setDigging(0); // left click animation
1858 if(input->getRightClicked())
1860 infostream<<"Ground right-clicked"<<std::endl;
1862 // If metadata provides an inventory view, activate it
1863 if(meta && meta->getInventoryDrawSpecString() != "" && !random_input)
1865 infostream<<"Launching custom inventory view"<<std::endl;
1867 Construct the unique identification string of the node
1869 std::string current_name;
1870 current_name += "nodemeta:";
1871 current_name += itos(nodepos.X);
1872 current_name += ",";
1873 current_name += itos(nodepos.Y);
1874 current_name += ",";
1875 current_name += itos(nodepos.Z);
1881 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
1883 GUIInventoryMenu::makeDrawSpecArrayFromString(
1885 meta->getInventoryDrawSpecString(),
1888 GUIInventoryMenu *menu =
1889 new GUIInventoryMenu(guienv, guiroot, -1,
1890 &g_menumgr, invsize,
1891 client.getInventoryContext(),
1893 menu->setDrawSpec(draw_spec);
1896 else if(meta && meta->typeId() == CONTENT_SIGN_WALL && !random_input)
1898 infostream<<"Sign node right-clicked"<<std::endl;
1900 SignNodeMetadata *signmeta = (SignNodeMetadata*)meta;
1902 // Get a new text for it
1904 TextDest *dest = new TextDestSignNode(nodepos, &client);
1906 std::wstring wtext =
1907 narrow_to_wide(signmeta->getText());
1909 (new GUITextInputMenu(guienv, guiroot, -1,
1915 client.groundAction(1, nodepos, neighbourpos, g_selected_item);
1916 camera.setDigging(1); // right click animation
1920 nodepos_old = nodepos;
1923 } // selected_object == NULL
1925 if(left_punch || (input->getLeftClicked() && !left_punch_muted))
1927 camera.setDigging(0); // left click animation
1930 input->resetLeftClicked();
1931 input->resetRightClicked();
1933 if(input->getLeftReleased())
1935 infostream<<"Left button released (stopped digging)"
1937 client.groundAction(2, v3s16(0,0,0), v3s16(0,0,0), 0);
1939 if(input->getRightReleased())
1941 //inostream<<DTIME<<"Right released"<<std::endl;
1945 input->resetLeftReleased();
1946 input->resetRightReleased();
1949 Calculate stuff for drawing
1952 u32 daynight_ratio = client.getDayNightRatio();
1953 u8 l = decode_light((daynight_ratio * LIGHT_SUN) / 1000);
1954 video::SColor bgcolor = video::SColor(
1956 bgcolor_bright.getRed() * l / 255,
1957 bgcolor_bright.getGreen() * l / 255,
1958 bgcolor_bright.getBlue() * l / 255);
1959 /*skycolor.getRed() * l / 255,
1960 skycolor.getGreen() * l / 255,
1961 skycolor.getBlue() * l / 255);*/
1963 float brightness = (float)l/255.0;
1968 if(fabs(brightness - old_brightness) > 0.01)
1969 update_skybox(driver, smgr, skybox, brightness);
1976 clouds->step(dtime);
1977 clouds->update(v2f(player_position.X, player_position.Z),
1978 0.05+brightness*0.95);
1986 farmesh_range = draw_control.wanted_range * 10;
1987 if(draw_control.range_all && farmesh_range < 500)
1988 farmesh_range = 500;
1989 if(farmesh_range > 1000)
1990 farmesh_range = 1000;
1992 farmesh->step(dtime);
1993 farmesh->update(v2f(player_position.X, player_position.Z),
1994 0.05+brightness*0.95, farmesh_range);
1997 // Store brightness value
1998 old_brightness = brightness;
2004 if(g_settings->getBool("enable_fog") == true && !force_fog_off)
2009 range = BS*farmesh_range;
2013 range = draw_control.wanted_range*BS + MAP_BLOCKSIZE*BS*1.5;
2015 if(draw_control.range_all)
2018 range = range * 0.5 + 25*BS;*/
2023 video::EFT_FOG_LINEAR,
2035 video::EFT_FOG_LINEAR,
2045 Update gui stuff (0ms)
2048 //TimeTaker guiupdatetimer("Gui updating");
2051 static float drawtime_avg = 0;
2052 drawtime_avg = drawtime_avg * 0.95 + (float)drawtime*0.05;
2053 static float beginscenetime_avg = 0;
2054 beginscenetime_avg = beginscenetime_avg * 0.95 + (float)beginscenetime*0.05;
2055 static float scenetime_avg = 0;
2056 scenetime_avg = scenetime_avg * 0.95 + (float)scenetime*0.05;
2057 static float endscenetime_avg = 0;
2058 endscenetime_avg = endscenetime_avg * 0.95 + (float)endscenetime*0.05;
2061 snprintf(temptext, 300, "Minetest-c55 %s ("
2064 " drawtime=%.0f, beginscenetime=%.0f"
2065 ", scenetime=%.0f, endscenetime=%.0f",
2067 draw_control.range_all,
2074 guitext->setText(narrow_to_wide(temptext).c_str());
2079 snprintf(temptext, 300,
2080 "(% .1f, % .1f, % .1f)"
2081 " (% .3f < btime_jitter < % .3f"
2082 ", dtime_jitter = % .1f %%"
2083 ", v_range = %.1f, RTT = %.3f)",
2084 player_position.X/BS,
2085 player_position.Y/BS,
2086 player_position.Z/BS,
2087 busytime_jitter1_min_sample,
2088 busytime_jitter1_max_sample,
2089 dtime_jitter1_max_fraction * 100.0,
2090 draw_control.wanted_range,
2094 guitext2->setText(narrow_to_wide(temptext).c_str());
2098 guitext_info->setText(infotext.c_str());
2102 Get chat messages from client
2106 std::wstring message;
2107 while(client.getChatMessage(message))
2109 chat_lines.push_back(ChatLine(message));
2110 /*if(chat_lines.size() > 6)
2112 core::list<ChatLine>::Iterator
2113 i = chat_lines.begin();
2114 chat_lines.erase(i);
2117 // Append them to form the whole static text and throw
2118 // it to the gui element
2120 // This will correspond to the line number counted from
2121 // top to bottom, from size-1 to 0
2122 s16 line_number = chat_lines.size();
2123 // Count of messages to be removed from the top
2124 u16 to_be_removed_count = 0;
2125 for(core::list<ChatLine>::Iterator
2126 i = chat_lines.begin();
2127 i != chat_lines.end(); i++)
2129 // After this, line number is valid for this loop
2134 This results in a maximum age of 60*6 to the
2135 lowermost line and a maximum of 6 lines
2137 float allowed_age = (6-line_number) * 60.0;
2139 if((*i).age > allowed_age)
2141 to_be_removed_count++;
2144 whole += (*i).text + L'\n';
2146 for(u16 i=0; i<to_be_removed_count; i++)
2148 core::list<ChatLine>::Iterator
2149 it = chat_lines.begin();
2150 chat_lines.erase(it);
2152 guitext_chat->setText(whole.c_str());
2154 // Update gui element size and position
2156 /*core::rect<s32> rect(
2158 screensize.Y - guitext_chat_pad_bottom
2159 - text_height*chat_lines.size(),
2161 screensize.Y - guitext_chat_pad_bottom
2163 core::rect<s32> rect(
2167 50 + guitext_chat->getTextHeight()
2170 guitext_chat->setRelativePosition(rect);
2172 // Don't show chat if empty or profiler is enabled
2173 if(chat_lines.size() == 0 || show_profiler)
2174 guitext_chat->setVisible(false);
2176 guitext_chat->setVisible(true);
2183 static u16 old_selected_item = 65535;
2184 if(client.getLocalInventoryUpdated()
2185 || g_selected_item != old_selected_item)
2187 client.selectPlayerItem(g_selected_item);
2188 old_selected_item = g_selected_item;
2189 //infostream<<"Updating local inventory"<<std::endl;
2190 client.getLocalInventory(local_inventory);
2192 // Update wielded tool
2193 InventoryList *mlist = local_inventory.getList("main");
2194 InventoryItem *item = NULL;
2196 item = mlist->getItem(g_selected_item);
2201 Send actions returned by the inventory menu
2203 while(inventory_action_queue.size() != 0)
2205 InventoryAction *a = inventory_action_queue.pop_front();
2207 client.sendInventoryAction(a);
2216 TimeTaker drawtimer("Drawing");
2220 TimeTaker timer("beginScene");
2221 driver->beginScene(true, true, bgcolor);
2222 //driver->beginScene(false, true, bgcolor);
2223 beginscenetime = timer.stop(true);
2228 //infostream<<"smgr->drawAll()"<<std::endl;
2231 TimeTaker timer("smgr");
2233 scenetime = timer.stop(true);
2237 //TimeTaker timer9("auxiliary drawings");
2241 //TimeTaker //timer10("//timer10");
2247 driver->setMaterial(m);
2249 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
2251 for(core::list< core::aabbox3d<f32> >::Iterator i=hilightboxes.begin();
2252 i != hilightboxes.end(); i++)
2254 /*infostream<<"hilightbox min="
2255 <<"("<<i->MinEdge.X<<","<<i->MinEdge.Y<<","<<i->MinEdge.Z<<")"
2257 <<"("<<i->MaxEdge.X<<","<<i->MaxEdge.Y<<","<<i->MaxEdge.Z<<")"
2259 driver->draw3DBox(*i, video::SColor(255,0,0,0));
2266 // Warning: This clears the Z buffer.
2267 camera.drawWieldedTool();
2274 client.renderPostFx();
2280 if(g_settings->getBool("frametime_graph") == true)
2283 for(core::list<float>::Iterator
2284 i = frametime_log.begin();
2285 i != frametime_log.end();
2288 driver->draw2DLine(v2s32(x,50),
2289 v2s32(x,50+(*i)*1000),
2290 video::SColor(255,255,255,255));
2298 driver->draw2DLine(displaycenter - core::vector2d<s32>(10,0),
2299 displaycenter + core::vector2d<s32>(10,0),
2300 video::SColor(255,255,255,255));
2301 driver->draw2DLine(displaycenter - core::vector2d<s32>(0,10),
2302 displaycenter + core::vector2d<s32>(0,10),
2303 video::SColor(255,255,255,255));
2308 //TimeTaker //timer11("//timer11");
2320 draw_hotbar(driver, font, v2s32(displaycenter.X, screensize.Y),
2321 hotbar_imagesize, hotbar_itemcount, &local_inventory,
2328 if(damage_flash_timer > 0.0)
2330 damage_flash_timer -= dtime;
2332 video::SColor color(128,255,0,0);
2333 driver->draw2DRectangle(color,
2334 core::rect<s32>(0,0,screensize.X,screensize.Y),
2342 TimeTaker timer("endScene");
2344 endscenetime = timer.stop(true);
2347 drawtime = drawtimer.stop(true);
2353 static s16 lastFPS = 0;
2354 //u16 fps = driver->getFPS();
2355 u16 fps = (1.0/dtime_avg1);
2359 core::stringw str = L"Minetest [";
2360 str += driver->getName();
2364 device->setWindowCaption(str.c_str());
2376 Draw a "shutting down" screen, which will be shown while the map
2377 generator and other stuff quits
2380 /*gui::IGUIStaticText *gui_shuttingdowntext = */
2381 draw_load_screen(L"Shutting down stuff...", driver, font);
2382 /*driver->beginScene(true, true, video::SColor(255,0,0,0));
2385 gui_shuttingdowntext->remove();*/