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 TODO: Move content-aware stuff to separate file by adding properties
47 and virtual interfaces
49 #include "content_mapnode.h"
50 #include "content_nodemeta.h"
53 Setting this to 1 enables a special camera mode that forces
54 the renderers to think that the camera statically points from
55 the starting place to a static direction.
57 This allows one to move around with the player and see what
58 is actually drawn behind solid things and behind the player.
60 #define FIELD_OF_VIEW_TEST 0
70 ChatLine(const std::wstring &a_text):
83 // Inventory actions from the menu are buffered here before sending
84 Queue<InventoryAction*> inventory_action_queue;
85 // This is a copy of the inventory that the client's environment has
86 Inventory local_inventory;
88 u16 g_selected_item = 0;
94 struct TextDestChat : public TextDest
96 TextDestChat(Client *client)
100 void gotText(std::wstring text)
102 // Discard empty line
107 m_client->sendChatMessage(text);
109 m_client->addChatMessage(text);
115 struct TextDestSignNode : public TextDest
117 TextDestSignNode(v3s16 p, Client *client)
122 void gotText(std::wstring text)
124 std::string ntext = wide_to_narrow(text);
125 dstream<<"Changing text of a sign node: "
127 m_client->sendSignNodeText(m_p, ntext);
134 /* Respawn menu callback */
136 class MainRespawnInitiator: public IRespawnInitiator
139 MainRespawnInitiator(bool *active, Client *client):
140 m_active(active), m_client(client)
147 m_client->sendRespawn();
157 void draw_hotbar(video::IVideoDriver *driver, gui::IGUIFont *font,
158 v2s32 centerlowerpos, s32 imgsize, s32 itemcount,
159 Inventory *inventory, s32 halfheartcount)
161 InventoryList *mainlist = inventory->getList("main");
164 dstream<<"WARNING: draw_hotbar(): mainlist == NULL"<<std::endl;
168 s32 padding = imgsize/12;
169 //s32 height = imgsize + padding*2;
170 s32 width = itemcount*(imgsize+padding*2);
172 // Position of upper left corner of bar
173 v2s32 pos = centerlowerpos - v2s32(width/2, imgsize+padding*2);
175 // Draw background color
176 /*core::rect<s32> barrect(0,0,width,height);
178 video::SColor bgcolor(255,128,128,128);
179 driver->draw2DRectangle(bgcolor, barrect, NULL);*/
181 core::rect<s32> imgrect(0,0,imgsize,imgsize);
183 for(s32 i=0; i<itemcount; i++)
185 InventoryItem *item = mainlist->getItem(i);
187 core::rect<s32> rect = imgrect + pos
188 + v2s32(padding+i*(imgsize+padding*2), padding);
190 if(g_selected_item == i)
192 video::SColor c_outside(255,255,0,0);
193 //video::SColor c_outside(255,0,0,0);
194 //video::SColor c_inside(255,192,192,192);
195 s32 x1 = rect.UpperLeftCorner.X;
196 s32 y1 = rect.UpperLeftCorner.Y;
197 s32 x2 = rect.LowerRightCorner.X;
198 s32 y2 = rect.LowerRightCorner.Y;
199 // Black base borders
200 driver->draw2DRectangle(c_outside,
202 v2s32(x1 - padding, y1 - padding),
203 v2s32(x2 + padding, y1)
205 driver->draw2DRectangle(c_outside,
207 v2s32(x1 - padding, y2),
208 v2s32(x2 + padding, y2 + padding)
210 driver->draw2DRectangle(c_outside,
212 v2s32(x1 - padding, y1),
215 driver->draw2DRectangle(c_outside,
218 v2s32(x2 + padding, y2)
220 /*// Light inside borders
221 driver->draw2DRectangle(c_inside,
223 v2s32(x1 - padding/2, y1 - padding/2),
224 v2s32(x2 + padding/2, y1)
226 driver->draw2DRectangle(c_inside,
228 v2s32(x1 - padding/2, y2),
229 v2s32(x2 + padding/2, y2 + padding/2)
231 driver->draw2DRectangle(c_inside,
233 v2s32(x1 - padding/2, y1),
236 driver->draw2DRectangle(c_inside,
239 v2s32(x2 + padding/2, y2)
244 video::SColor bgcolor2(128,0,0,0);
245 driver->draw2DRectangle(bgcolor2, rect, NULL);
249 drawInventoryItem(driver, font, item, rect, NULL);
257 video::ITexture *heart_texture =
258 driver->getTexture(getTexturePath("heart.png").c_str());
259 v2s32 p = pos + v2s32(0, -20);
260 for(s32 i=0; i<halfheartcount/2; i++)
262 const video::SColor color(255,255,255,255);
263 const video::SColor colors[] = {color,color,color,color};
264 core::rect<s32> rect(0,0,16,16);
266 driver->draw2DImage(heart_texture, rect,
267 core::rect<s32>(core::position2d<s32>(0,0),
268 core::dimension2di(heart_texture->getOriginalSize())),
272 if(halfheartcount % 2 == 1)
274 const video::SColor color(255,255,255,255);
275 const video::SColor colors[] = {color,color,color,color};
276 core::rect<s32> rect(0,0,16/2,16);
278 core::dimension2di srcd(heart_texture->getOriginalSize());
280 driver->draw2DImage(heart_texture, rect,
281 core::rect<s32>(core::position2d<s32>(0,0), srcd),
289 Find what the player is pointing at
291 void getPointedNode(Client *client, v3f player_position,
292 v3f camera_direction, v3f camera_position,
293 bool &nodefound, core::line3d<f32> shootline,
294 v3s16 &nodepos, v3s16 &neighbourpos,
295 core::aabbox3d<f32> &nodehilightbox,
298 f32 mindistance = BS * 1001;
300 v3s16 pos_i = floatToInt(player_position, BS);
302 /*dstream<<"pos_i=("<<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z<<")"
306 s16 ystart = pos_i.Y + 0 - (camera_direction.Y<0 ? a : 1);
307 s16 zstart = pos_i.Z - (camera_direction.Z<0 ? a : 1);
308 s16 xstart = pos_i.X - (camera_direction.X<0 ? a : 1);
309 s16 yend = pos_i.Y + 1 + (camera_direction.Y>0 ? a : 1);
310 s16 zend = pos_i.Z + (camera_direction.Z>0 ? a : 1);
311 s16 xend = pos_i.X + (camera_direction.X>0 ? a : 1);
313 for(s16 y = ystart; y <= yend; y++)
314 for(s16 z = zstart; z <= zend; z++)
315 for(s16 x = xstart; x <= xend; x++)
320 n = client->getNode(v3s16(x,y,z));
321 if(content_pointable(n.getContent()) == false)
324 catch(InvalidPositionException &e)
330 v3f npf = intToFloat(np, BS);
335 v3s16(0,0,1), // back
337 v3s16(1,0,0), // right
338 v3s16(0,0,-1), // front
339 v3s16(0,-1,0), // bottom
340 v3s16(-1,0,0), // left
346 if(n.getContent() == CONTENT_TORCH)
348 v3s16 dir = unpackDir(n.param2);
349 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
350 dir_f *= BS/2 - BS/6 - BS/20;
351 v3f cpf = npf + dir_f;
352 f32 distance = (cpf - camera_position).getLength();
354 core::aabbox3d<f32> box;
357 if(dir == v3s16(0,-1,0))
359 box = core::aabbox3d<f32>(
360 npf - v3f(BS/6, BS/2, BS/6),
361 npf + v3f(BS/6, -BS/2+BS/3*2, BS/6)
365 else if(dir == v3s16(0,1,0))
367 box = core::aabbox3d<f32>(
368 npf - v3f(BS/6, -BS/2+BS/3*2, BS/6),
369 npf + v3f(BS/6, BS/2, BS/6)
375 box = core::aabbox3d<f32>(
376 cpf - v3f(BS/6, BS/3, BS/6),
377 cpf + v3f(BS/6, BS/3, BS/6)
381 if(distance < mindistance)
383 if(box.intersectsWithLine(shootline))
388 mindistance = distance;
389 nodehilightbox = box;
393 else if(n.getContent() == CONTENT_SIGN_WALL)
395 v3s16 dir = unpackDir(n.param2);
396 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
397 dir_f *= BS/2 - BS/6 - BS/20;
398 v3f cpf = npf + dir_f;
399 f32 distance = (cpf - camera_position).getLength();
403 v3f(BS*0.42,-BS*0.35,-BS*0.4),
404 v3f(BS*0.49, BS*0.35, BS*0.4),
407 for(s32 i=0; i<2; i++)
409 if(dir == v3s16(1,0,0))
410 vertices[i].rotateXZBy(0);
411 if(dir == v3s16(-1,0,0))
412 vertices[i].rotateXZBy(180);
413 if(dir == v3s16(0,0,1))
414 vertices[i].rotateXZBy(90);
415 if(dir == v3s16(0,0,-1))
416 vertices[i].rotateXZBy(-90);
417 if(dir == v3s16(0,-1,0))
418 vertices[i].rotateXYBy(-90);
419 if(dir == v3s16(0,1,0))
420 vertices[i].rotateXYBy(90);
425 core::aabbox3d<f32> box;
427 box = core::aabbox3d<f32>(vertices[0]);
428 box.addInternalPoint(vertices[1]);
430 if(distance < mindistance)
432 if(box.intersectsWithLine(shootline))
437 mindistance = distance;
438 nodehilightbox = box;
443 else if(n.getContent() == CONTENT_LADDER)
445 v3s16 dir = unpackDir(n.param2);
446 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
447 dir_f *= BS/2 - BS/6 - BS/20;
448 v3f cpf = npf + dir_f;
449 f32 distance = (cpf - camera_position).getLength();
453 v3f(BS*0.42,-BS/2,-BS/2),
454 v3f(BS*0.49, BS/2, BS/2),
457 for(s32 i=0; i<2; i++)
459 if(dir == v3s16(1,0,0))
460 vertices[i].rotateXZBy(0);
461 if(dir == v3s16(-1,0,0))
462 vertices[i].rotateXZBy(180);
463 if(dir == v3s16(0,0,1))
464 vertices[i].rotateXZBy(90);
465 if(dir == v3s16(0,0,-1))
466 vertices[i].rotateXZBy(-90);
467 if(dir == v3s16(0,-1,0))
468 vertices[i].rotateXYBy(-90);
469 if(dir == v3s16(0,1,0))
470 vertices[i].rotateXYBy(90);
475 core::aabbox3d<f32> box;
477 box = core::aabbox3d<f32>(vertices[0]);
478 box.addInternalPoint(vertices[1]);
480 if(distance < mindistance)
482 if(box.intersectsWithLine(shootline))
487 mindistance = distance;
488 nodehilightbox = box;
492 else if(n.getContent() == CONTENT_RAIL)
494 v3s16 dir = unpackDir(n.param0);
495 v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
496 dir_f *= BS/2 - BS/6 - BS/20;
497 v3f cpf = npf + dir_f;
498 f32 distance = (cpf - camera_position).getLength();
500 float d = (float)BS/16;
503 v3f(BS/2, -BS/2+d, -BS/2),
504 v3f(-BS/2, -BS/2, BS/2),
507 for(s32 i=0; i<2; i++)
512 core::aabbox3d<f32> box;
514 box = core::aabbox3d<f32>(vertices[0]);
515 box.addInternalPoint(vertices[1]);
517 if(distance < mindistance)
519 if(box.intersectsWithLine(shootline))
524 mindistance = distance;
525 nodehilightbox = box;
534 for(u16 i=0; i<6; i++)
536 v3f dir_f = v3f(dirs[i].X,
537 dirs[i].Y, dirs[i].Z);
538 v3f centerpoint = npf + dir_f * BS/2;
540 (centerpoint - camera_position).getLength();
542 if(distance < mindistance)
544 core::CMatrix4<f32> m;
545 m.buildRotateFromTo(v3f(0,0,1), dir_f);
547 // This is the back face
549 v3f(BS/2, BS/2, BS/2),
550 v3f(-BS/2, -BS/2, BS/2+d)
553 for(u16 j=0; j<2; j++)
555 m.rotateVect(corners[j]);
559 core::aabbox3d<f32> facebox(corners[0]);
560 facebox.addInternalPoint(corners[1]);
562 if(facebox.intersectsWithLine(shootline))
566 neighbourpos = np + dirs[i];
567 mindistance = distance;
569 //nodehilightbox = facebox;
571 const float d = 0.502;
572 core::aabbox3d<f32> nodebox
573 (-BS*d, -BS*d, -BS*d, BS*d, BS*d, BS*d);
574 v3f nodepos_f = intToFloat(nodepos, BS);
575 nodebox.MinEdge += nodepos_f;
576 nodebox.MaxEdge += nodepos_f;
577 nodehilightbox = nodebox;
579 } // if distance < mindistance
585 void update_skybox(video::IVideoDriver* driver,
586 scene::ISceneManager* smgr, scene::ISceneNode* &skybox,
594 /*// Disable skybox if FarMesh is enabled
595 if(g_settings->getBool("enable_farmesh"))
598 if(brightness >= 0.5)
600 skybox = smgr->addSkyBoxSceneNode(
601 driver->getTexture(getTexturePath("skybox2.png").c_str()),
602 driver->getTexture(getTexturePath("skybox3.png").c_str()),
603 driver->getTexture(getTexturePath("skybox1.png").c_str()),
604 driver->getTexture(getTexturePath("skybox1.png").c_str()),
605 driver->getTexture(getTexturePath("skybox1.png").c_str()),
606 driver->getTexture(getTexturePath("skybox1.png").c_str()));
608 else if(brightness >= 0.2)
610 skybox = smgr->addSkyBoxSceneNode(
611 driver->getTexture(getTexturePath("skybox2_dawn.png").c_str()),
612 driver->getTexture(getTexturePath("skybox3_dawn.png").c_str()),
613 driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
614 driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
615 driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()),
616 driver->getTexture(getTexturePath("skybox1_dawn.png").c_str()));
620 skybox = smgr->addSkyBoxSceneNode(
621 driver->getTexture(getTexturePath("skybox2_night.png").c_str()),
622 driver->getTexture(getTexturePath("skybox3_night.png").c_str()),
623 driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
624 driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
625 driver->getTexture(getTexturePath("skybox1_night.png").c_str()),
626 driver->getTexture(getTexturePath("skybox1_night.png").c_str()));
631 Draws a screen with a single text on it.
632 Text will be removed when the screen is drawn the next time.
634 /*gui::IGUIStaticText **/
635 void draw_load_screen(const std::wstring &text,
636 video::IVideoDriver* driver, gui::IGUIFont* font)
638 v2u32 screensize = driver->getScreenSize();
639 const wchar_t *loadingtext = text.c_str();
640 core::vector2d<u32> textsize_u = font->getDimension(loadingtext);
641 core::vector2d<s32> textsize(textsize_u.X,textsize_u.Y);
642 core::vector2d<s32> center(screensize.X/2, screensize.Y/2);
643 core::rect<s32> textrect(center - textsize/2, center + textsize/2);
645 gui::IGUIStaticText *guitext = guienv->addStaticText(
646 loadingtext, textrect, false, false);
647 guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
649 driver->beginScene(true, true, video::SColor(255,0,0,0));
662 IrrlichtDevice *device,
665 std::string playername,
666 std::string password,
669 std::wstring &error_message,
670 std::string configpath
673 video::IVideoDriver* driver = device->getVideoDriver();
674 scene::ISceneManager* smgr = device->getSceneManager();
676 // Calculate text height using the font
677 u32 text_height = font->getDimension(L"Random test string").Height;
679 v2u32 screensize(0,0);
680 v2u32 last_screensize(0,0);
681 screensize = driver->getScreenSize();
683 const s32 hotbar_itemcount = 8;
684 //const s32 hotbar_imagesize = 36;
685 //const s32 hotbar_imagesize = 64;
686 s32 hotbar_imagesize = 48;
688 // The color of the sky
690 //video::SColor skycolor = video::SColor(255,140,186,250);
692 video::SColor bgcolor_bright = video::SColor(255,170,200,230);
695 Draw "Loading" screen
697 /*gui::IGUIStaticText *gui_loadingtext = */
698 //draw_load_screen(L"Loading and connecting...", driver, font);
700 draw_load_screen(L"Loading...", driver, font);
704 SharedPtr will delete it when it goes out of scope.
706 SharedPtr<Server> server;
708 draw_load_screen(L"Creating server...", driver, font);
709 dstream<<DTIME<<"Creating server"<<std::endl;
710 server = new Server(map_dir, configpath);
718 draw_load_screen(L"Creating client...", driver, font);
719 dstream<<DTIME<<"Creating client"<<std::endl;
720 MapDrawControl draw_control;
721 Client client(device, playername.c_str(), password, draw_control);
723 draw_load_screen(L"Resolving address...", driver, font);
724 Address connect_address(0,0,0,0, port);
727 //connect_address.Resolve("localhost");
728 connect_address.setAddress(127,0,0,1);
730 connect_address.Resolve(address.c_str());
732 catch(ResolveError &e)
734 dstream<<DTIME<<"Couldn't resolve address"<<std::endl;
736 error_message = L"Couldn't resolve address";
737 //gui_loadingtext->remove();
742 Attempt to connect to the server
745 dstream<<DTIME<<"Connecting to server at ";
746 connect_address.print(&dstream);
748 client.connect(connect_address);
750 bool could_connect = false;
753 float time_counter = 0.0;
756 if(client.connectedAndInitialized())
758 could_connect = true;
761 if(client.accessDenied())
765 // Wait for 10 seconds
766 if(time_counter >= 10.0)
771 std::wostringstream ss;
772 ss<<L"Connecting to server... (timeout in ";
773 ss<<(int)(10.0 - time_counter + 1.0);
775 draw_load_screen(ss.str(), driver, font);
778 driver->beginScene(true, true, video::SColor(255,0,0,0));
780 driver->endScene();*/
782 // Update client and server
794 catch(con::PeerNotFoundException &e)
797 if(could_connect == false)
799 if(client.accessDenied())
801 error_message = L"Access denied. Reason: "
802 +client.accessDeniedReason();
803 dstream<<DTIME<<wide_to_narrow(error_message)<<std::endl;
807 error_message = L"Connection timed out.";
808 dstream<<DTIME<<"Timed out."<<std::endl;
810 //gui_loadingtext->remove();
817 float old_brightness = 1.0;
818 scene::ISceneNode* skybox = NULL;
819 update_skybox(driver, smgr, skybox, 1.0);
822 Create the camera node
824 Camera camera(smgr, draw_control);
825 if (!camera.successfullyCreated(error_message))
828 f32 camera_yaw = 0; // "right/left"
829 f32 camera_pitch = 0; // "up/down"
835 float cloud_height = BS*100;
836 Clouds *clouds = NULL;
837 if(g_settings->getBool("enable_clouds"))
839 clouds = new Clouds(smgr->getRootSceneNode(), smgr, -1,
840 cloud_height, time(0));
847 FarMesh *farmesh = NULL;
848 if(g_settings->getBool("enable_farmesh"))
850 farmesh = new FarMesh(smgr->getRootSceneNode(), smgr, -1, client.getMapSeed(), &client);
857 //gui_loadingtext->remove();
863 // First line of debug text
864 gui::IGUIStaticText *guitext = guienv->addStaticText(
866 core::rect<s32>(5, 5, 795, 5+text_height),
868 // Second line of debug text
869 gui::IGUIStaticText *guitext2 = guienv->addStaticText(
871 core::rect<s32>(5, 5+(text_height+5)*1, 795, (5+text_height)*2),
874 // At the middle of the screen
875 // Object infos are shown in this
876 gui::IGUIStaticText *guitext_info = guienv->addStaticText(
878 core::rect<s32>(0,0,400,text_height+5) + v2s32(100,200),
882 gui::IGUIStaticText *guitext_chat = guienv->addStaticText(
884 core::rect<s32>(0,0,0,0),
885 //false, false); // Disable word wrap as of now
887 //guitext_chat->setBackgroundColor(video::SColor(96,0,0,0));
888 core::list<ChatLine> chat_lines;
890 /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
891 (guienv, NULL, v2s32(10, 70), 5, &local_inventory);*/
892 /*GUIQuickInventory *quick_inventory = new GUIQuickInventory
893 (guienv, NULL, v2s32(0, 0), quickinv_itemcount, &local_inventory);*/
895 // Test the text input system
896 /*(new GUITextInputMenu(guienv, guiroot, -1, &g_menumgr,
898 /*GUIMessageMenu *menu =
899 new GUIMessageMenu(guienv, guiroot, -1,
905 (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
906 &g_menumgr))->drop();
909 /*guitext2->setVisible(true);
910 guitext_info->setVisible(true);
911 guitext_chat->setVisible(true);*/
913 //s32 guitext_chat_pad_bottom = 70;
916 Some statistics are collected in these
919 u32 beginscenetime = 0;
921 u32 endscenetime = 0;
924 //throw con::PeerNotFoundException("lol");
926 core::list<float> frametime_log;
928 float nodig_delay_counter = 0.0;
929 float dig_time = 0.0;
931 v3s16 nodepos_old(-32768,-32768,-32768);
933 float damage_flash_timer = 0;
934 s16 farmesh_range = 20*MAP_BLOCKSIZE;
936 const float object_hit_delay = 0.5;
937 float object_hit_delay_timer = 0.0;
939 bool invert_mouse = g_settings->getBool("invert_mouse");
941 bool respawn_menu_active = false;
947 bool first_loop_after_window_activation = true;
949 // TODO: Convert the static interval timers to these
950 // Interval limiter for profiler
951 IntervalLimiter m_profiler_interval;
953 // Time is in milliseconds
954 // NOTE: getRealTime() causes strange problems in wine (imprecision?)
955 // NOTE: So we have to use getTime() and call run()s between them
956 u32 lasttime = device->getTimer()->getTime();
958 while(device->run() && kill == false)
960 //std::cerr<<"frame"<<std::endl;
962 if(client.accessDenied())
964 error_message = L"Access denied. Reason: "
965 +client.accessDeniedReason();
966 dstream<<DTIME<<wide_to_narrow(error_message)<<std::endl;
970 if(g_gamecallback->disconnect_requested)
972 g_gamecallback->disconnect_requested = false;
976 if(g_gamecallback->changepassword_requested)
978 (new GUIPasswordChange(guienv, guiroot, -1,
979 &g_menumgr, &client))->drop();
980 g_gamecallback->changepassword_requested = false;
984 Process TextureSource's queue
986 ((TextureSource*)g_texturesource)->processQueue();
991 last_screensize = screensize;
992 screensize = driver->getScreenSize();
993 v2s32 displaycenter(screensize.X/2,screensize.Y/2);
994 //bool screensize_changed = screensize != last_screensize;
997 if(screensize.Y <= 800)
998 hotbar_imagesize = 32;
999 else if(screensize.Y <= 1280)
1000 hotbar_imagesize = 48;
1002 hotbar_imagesize = 64;
1004 // Hilight boxes collected during the loop and displayed
1005 core::list< core::aabbox3d<f32> > hilightboxes;
1008 std::wstring infotext;
1010 // When screen size changes, update positions and sizes of stuff
1011 /*if(screensize_changed)
1013 v2s32 pos(displaycenter.X-((quickinv_itemcount-1)*quickinv_spacing+quickinv_size)/2, screensize.Y-quickinv_spacing);
1014 quick_inventory->updatePosition(pos);
1017 //TimeTaker //timer1("//timer1");
1019 // Time of frame without fps limit
1023 // not using getRealTime is necessary for wine
1024 u32 time = device->getTimer()->getTime();
1026 busytime_u32 = time - lasttime;
1029 busytime = busytime_u32 / 1000.0;
1032 //dstream<<"busytime_u32="<<busytime_u32<<std::endl;
1034 // Necessary for device->getTimer()->getTime()
1042 float fps_max = g_settings->getFloat("fps_max");
1043 u32 frametime_min = 1000./fps_max;
1045 if(busytime_u32 < frametime_min)
1047 u32 sleeptime = frametime_min - busytime_u32;
1048 device->sleep(sleeptime);
1052 // Necessary for device->getTimer()->getTime()
1056 Time difference calculation
1058 f32 dtime; // in seconds
1060 u32 time = device->getTimer()->getTime();
1062 dtime = (time - lasttime) / 1000.0;
1069 object_hit_delay_timer -= dtime;
1072 Log frametime for visualization
1074 frametime_log.push_back(dtime);
1075 if(frametime_log.size() > 100)
1077 core::list<float>::Iterator i = frametime_log.begin();
1078 frametime_log.erase(i);
1082 Visualize frametime in terminal
1084 /*for(u32 i=0; i<dtime*400; i++)
1086 dstream<<std::endl;*/
1089 Time average and jitter calculation
1092 static f32 dtime_avg1 = 0.0;
1093 dtime_avg1 = dtime_avg1 * 0.96 + dtime * 0.04;
1094 f32 dtime_jitter1 = dtime - dtime_avg1;
1096 static f32 dtime_jitter1_max_sample = 0.0;
1097 static f32 dtime_jitter1_max_fraction = 0.0;
1099 static f32 jitter1_max = 0.0;
1100 static f32 counter = 0.0;
1101 if(dtime_jitter1 > jitter1_max)
1102 jitter1_max = dtime_jitter1;
1107 dtime_jitter1_max_sample = jitter1_max;
1108 dtime_jitter1_max_fraction
1109 = dtime_jitter1_max_sample / (dtime_avg1+0.001);
1115 Busytime average and jitter calculation
1118 static f32 busytime_avg1 = 0.0;
1119 busytime_avg1 = busytime_avg1 * 0.98 + busytime * 0.02;
1120 f32 busytime_jitter1 = busytime - busytime_avg1;
1122 static f32 busytime_jitter1_max_sample = 0.0;
1123 static f32 busytime_jitter1_min_sample = 0.0;
1125 static f32 jitter1_max = 0.0;
1126 static f32 jitter1_min = 0.0;
1127 static f32 counter = 0.0;
1128 if(busytime_jitter1 > jitter1_max)
1129 jitter1_max = busytime_jitter1;
1130 if(busytime_jitter1 < jitter1_min)
1131 jitter1_min = busytime_jitter1;
1135 busytime_jitter1_max_sample = jitter1_max;
1136 busytime_jitter1_min_sample = jitter1_min;
1143 Debug info for client
1146 static float counter = 0.0;
1151 client.printDebugInfo(dstream);
1158 float profiler_print_interval =
1159 g_settings->getFloat("profiler_print_interval");
1160 if(profiler_print_interval != 0)
1162 if(m_profiler_interval.step(0.030, profiler_print_interval))
1164 dstream<<"Profiler:"<<std::endl;
1165 g_profiler->print(dstream);
1166 g_profiler->clear();
1171 Direct handling of user input
1174 // Reset input if window not active or some menu is active
1175 if(device->isWindowActive() == false || noMenuActive() == false)
1180 // Input handler step() (used by the random input generator)
1184 Launch menus according to keys
1186 if(input->wasKeyDown(getKeySetting("keymap_inventory")))
1188 dstream<<DTIME<<"the_game: "
1189 <<"Launching inventory"<<std::endl;
1191 GUIInventoryMenu *menu =
1192 new GUIInventoryMenu(guienv, guiroot, -1,
1193 &g_menumgr, v2s16(8,7),
1194 client.getInventoryContext(),
1197 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
1198 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1199 "list", "current_player", "main",
1200 v2s32(0, 3), v2s32(8, 4)));
1201 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1202 "list", "current_player", "craft",
1203 v2s32(3, 0), v2s32(3, 3)));
1204 draw_spec.push_back(GUIInventoryMenu::DrawSpec(
1205 "list", "current_player", "craftresult",
1206 v2s32(7, 1), v2s32(1, 1)));
1208 menu->setDrawSpec(draw_spec);
1212 else if(input->wasKeyDown(EscapeKey))
1214 dstream<<DTIME<<"the_game: "
1215 <<"Launching pause menu"<<std::endl;
1216 // It will delete itself by itself
1217 (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
1218 &g_menumgr))->drop();
1220 // Move mouse cursor on top of the disconnect button
1221 input->setMousePos(displaycenter.X, displaycenter.Y+25);
1223 else if(input->wasKeyDown(getKeySetting("keymap_chat")))
1225 TextDest *dest = new TextDestChat(&client);
1227 (new GUITextInputMenu(guienv, guiroot, -1,
1231 else if(input->wasKeyDown(getKeySetting("keymap_cmd")))
1233 TextDest *dest = new TextDestChat(&client);
1235 (new GUITextInputMenu(guienv, guiroot, -1,
1239 else if(input->wasKeyDown(getKeySetting("keymap_freemove")))
1241 if(g_settings->getBool("free_move"))
1243 g_settings->set("free_move","false");
1244 chat_lines.push_back(ChatLine(L"free_move disabled"));
1248 g_settings->set("free_move","true");
1249 chat_lines.push_back(ChatLine(L"free_move enabled"));
1252 else if(input->wasKeyDown(getKeySetting("keymap_fastmove")))
1254 if(g_settings->getBool("fast_move"))
1256 g_settings->set("fast_move","false");
1257 chat_lines.push_back(ChatLine(L"fast_move disabled"));
1261 g_settings->set("fast_move","true");
1262 chat_lines.push_back(ChatLine(L"fast_move enabled"));
1265 else if(input->wasKeyDown(getKeySetting("keymap_frametime_graph")))
1267 if(g_settings->getBool("frametime_graph"))
1269 g_settings->set("frametime_graph","false");
1270 chat_lines.push_back(ChatLine(L"frametime_graph disabled"));
1274 g_settings->set("frametime_graph","true");
1275 chat_lines.push_back(ChatLine(L"frametime_graph enabled"));
1278 else if(input->wasKeyDown(getKeySetting("keymap_screenshot")))
1280 irr::video::IImage* const image = driver->createScreenShot();
1282 irr::c8 filename[256];
1283 snprintf(filename, 256, "%s/screenshot_%u.png",
1284 g_settings->get("screenshot_path").c_str(),
1285 device->getTimer()->getRealTime());
1286 if (driver->writeImageToFile(image, filename)) {
1287 std::wstringstream sstr;
1288 sstr<<"Saved screenshot to '"<<filename<<"'";
1289 dstream<<"Saved screenshot to '"<<filename<<"'"<<std::endl;
1290 chat_lines.push_back(ChatLine(sstr.str()));
1292 dstream<<"Failed to save screenshot '"<<filename<<"'"<<std::endl;
1298 // Item selection with mouse wheel
1300 s32 wheel = input->getMouseWheel();
1301 u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1,
1302 hotbar_itemcount-1);
1306 if(g_selected_item < max_item)
1309 g_selected_item = 0;
1313 if(g_selected_item > 0)
1316 g_selected_item = max_item;
1321 for(u16 i=0; i<10; i++)
1323 const KeyPress *kp = NumberKey + (i + 1) % 10;
1324 if(input->wasKeyDown(*kp))
1326 if(i < PLAYER_INVENTORY_SIZE && i < hotbar_itemcount)
1328 g_selected_item = i;
1330 dstream<<DTIME<<"Selected item: "
1331 <<g_selected_item<<std::endl;
1336 // Viewing range selection
1337 if(input->wasKeyDown(getKeySetting("keymap_rangeselect")))
1339 if(draw_control.range_all)
1341 draw_control.range_all = false;
1342 dstream<<DTIME<<"Disabled full viewing range"<<std::endl;
1346 draw_control.range_all = true;
1347 dstream<<DTIME<<"Enabled full viewing range"<<std::endl;
1351 // Print debug stacks
1352 if(input->wasKeyDown(getKeySetting("keymap_print_debug_stacks")))
1354 dstream<<"-----------------------------------------"
1356 dstream<<DTIME<<"Printing debug stacks:"<<std::endl;
1357 dstream<<"-----------------------------------------"
1359 debug_stacks_print();
1363 Mouse and camera control
1364 NOTE: Do this before client.setPlayerControl() to not cause a camera lag of one frame
1367 if((device->isWindowActive() && noMenuActive()) || random_input)
1371 // Mac OSX gets upset if this is set every frame
1372 if(device->getCursorControl()->isVisible())
1373 device->getCursorControl()->setVisible(false);
1376 if(first_loop_after_window_activation){
1377 //dstream<<"window active, first loop"<<std::endl;
1378 first_loop_after_window_activation = false;
1381 s32 dx = input->getMousePos().X - displaycenter.X;
1382 s32 dy = input->getMousePos().Y - displaycenter.Y;
1385 //dstream<<"window active, pos difference "<<dx<<","<<dy<<std::endl;
1387 /*const float keyspeed = 500;
1388 if(input->isKeyDown(irr::KEY_UP))
1389 dy -= dtime * keyspeed;
1390 if(input->isKeyDown(irr::KEY_DOWN))
1391 dy += dtime * keyspeed;
1392 if(input->isKeyDown(irr::KEY_LEFT))
1393 dx -= dtime * keyspeed;
1394 if(input->isKeyDown(irr::KEY_RIGHT))
1395 dx += dtime * keyspeed;*/
1397 camera_yaw -= dx*0.2;
1398 camera_pitch += dy*0.2;
1399 if(camera_pitch < -89.5) camera_pitch = -89.5;
1400 if(camera_pitch > 89.5) camera_pitch = 89.5;
1402 input->setMousePos(displaycenter.X, displaycenter.Y);
1405 // Mac OSX gets upset if this is set every frame
1406 if(device->getCursorControl()->isVisible() == false)
1407 device->getCursorControl()->setVisible(true);
1409 //dstream<<"window inactive"<<std::endl;
1410 first_loop_after_window_activation = true;
1414 Player speed control
1417 if(!noMenuActive() || !device->isWindowActive())
1419 PlayerControl control(
1430 client.setPlayerControl(control);
1443 PlayerControl control(
1444 input->isKeyDown(getKeySetting("keymap_forward")),
1445 input->isKeyDown(getKeySetting("keymap_backward")),
1446 input->isKeyDown(getKeySetting("keymap_left")),
1447 input->isKeyDown(getKeySetting("keymap_right")),
1448 input->isKeyDown(getKeySetting("keymap_jump")),
1449 input->isKeyDown(getKeySetting("keymap_special1")),
1450 input->isKeyDown(getKeySetting("keymap_sneak")),
1454 client.setPlayerControl(control);
1463 //TimeTaker timer("server->step(dtime)");
1464 server->step(dtime);
1472 //TimeTaker timer("client.step(dtime)");
1474 //client.step(dtime_avg1);
1478 // Read client events
1481 ClientEvent event = client.getClientEvent();
1482 if(event.type == CE_NONE)
1486 else if(event.type == CE_PLAYER_DAMAGE)
1488 //u16 damage = event.player_damage.amount;
1489 //dstream<<"Player damage: "<<damage<<std::endl;
1490 damage_flash_timer = 0.05;
1491 if(event.player_damage.amount >= 2){
1492 damage_flash_timer += 0.05 * event.player_damage.amount;
1495 else if(event.type == CE_PLAYER_FORCE_MOVE)
1497 camera_yaw = event.player_force_move.yaw;
1498 camera_pitch = event.player_force_move.pitch;
1500 else if(event.type == CE_DEATHSCREEN)
1502 if(respawn_menu_active)
1505 /*bool set_camera_point_target =
1506 event.deathscreen.set_camera_point_target;
1507 v3f camera_point_target;
1508 camera_point_target.X = event.deathscreen.camera_point_target_x;
1509 camera_point_target.Y = event.deathscreen.camera_point_target_y;
1510 camera_point_target.Z = event.deathscreen.camera_point_target_z;*/
1511 MainRespawnInitiator *respawner =
1512 new MainRespawnInitiator(
1513 &respawn_menu_active, &client);
1514 GUIDeathScreen *menu =
1515 new GUIDeathScreen(guienv, guiroot, -1,
1516 &g_menumgr, respawner);
1519 /* Handle visualization */
1521 damage_flash_timer = 0;
1523 /*LocalPlayer* player = client.getLocalPlayer();
1524 player->setPosition(player->getPosition() + v3f(0,-BS,0));
1525 camera.update(player, busytime, screensize);*/
1530 //TimeTaker //timer2("//timer2");
1532 LocalPlayer* player = client.getLocalPlayer();
1533 camera.update(player, busytime, screensize);
1536 v3f player_position = player->getPosition();
1537 v3f camera_position = camera.getPosition();
1538 v3f camera_direction = camera.getDirection();
1539 f32 camera_fov = camera.getFovMax();
1541 if(FIELD_OF_VIEW_TEST)
1542 client.updateCamera(v3f(0,0,0), v3f(0,0,1), camera_fov);
1544 client.updateCamera(camera_position,
1545 camera_direction, camera_fov);
1548 //TimeTaker //timer3("//timer3");
1551 Calculate what block is the crosshair pointing to
1554 //u32 t1 = device->getTimer()->getRealTime();
1556 //f32 d = 4; // max. distance
1557 f32 d = 4; // max. distance
1558 core::line3d<f32> shootline(camera_position,
1559 camera_position + camera_direction * BS * (d+1));
1561 ClientActiveObject *selected_active_object
1562 = client.getSelectedActiveObject
1563 (d*BS, camera_position, shootline);
1565 bool left_punch = false;
1566 bool left_punch_muted = false;
1568 if(selected_active_object != NULL)
1570 /* Clear possible cracking animation */
1571 if(nodepos_old != v3s16(-32768,-32768,-32768))
1573 client.clearTempMod(nodepos_old);
1575 nodepos_old = v3s16(-32768,-32768,-32768);
1578 //dstream<<"Client returned selected_active_object != NULL"<<std::endl;
1580 core::aabbox3d<f32> *selection_box
1581 = selected_active_object->getSelectionBox();
1582 // Box should exist because object was returned in the
1584 assert(selection_box);
1586 v3f pos = selected_active_object->getPosition();
1588 core::aabbox3d<f32> box_on_map(
1589 selection_box->MinEdge + pos,
1590 selection_box->MaxEdge + pos
1593 if(selected_active_object->doShowSelectionBox())
1594 hilightboxes.push_back(box_on_map);
1596 //infotext = narrow_to_wide("A ClientActiveObject");
1597 infotext = narrow_to_wide(selected_active_object->infoText());
1599 //if(input->getLeftClicked())
1600 if(input->getLeftState())
1602 bool do_punch = false;
1603 bool do_punch_damage = false;
1604 if(object_hit_delay_timer <= 0.0){
1606 do_punch_damage = true;
1607 object_hit_delay_timer = object_hit_delay;
1609 if(input->getLeftClicked()){
1613 dstream<<DTIME<<"Left-clicked object"<<std::endl;
1616 if(do_punch_damage){
1617 client.clickActiveObject(0,
1618 selected_active_object->getId(), g_selected_item);
1621 else if(input->getRightClicked())
1623 dstream<<DTIME<<"Right-clicked object"<<std::endl;
1624 client.clickActiveObject(1,
1625 selected_active_object->getId(), g_selected_item);
1628 else // selected_object == NULL
1632 Find out which node we are pointing at
1635 bool nodefound = false;
1638 core::aabbox3d<f32> nodehilightbox;
1640 getPointedNode(&client, player_position,
1641 camera_direction, camera_position,
1642 nodefound, shootline,
1643 nodepos, neighbourpos,
1647 if(nodepos_old != v3s16(-32768,-32768,-32768))
1649 client.clearTempMod(nodepos_old);
1651 nodepos_old = v3s16(-32768,-32768,-32768);
1658 hilightboxes.push_back(nodehilightbox);
1661 Check information text of node
1664 NodeMetadata *meta = client.getNodeMetadata(nodepos);
1667 infotext = narrow_to_wide(meta->infoText());
1670 //MapNode node = client.getNode(nodepos);
1676 if(input->getLeftReleased())
1678 client.clearTempMod(nodepos);
1682 if(nodig_delay_counter > 0.0)
1684 nodig_delay_counter -= dtime;
1688 if(nodepos != nodepos_old)
1690 dstream<<DTIME<<"Pointing at ("<<nodepos.X<<","
1691 <<nodepos.Y<<","<<nodepos.Z<<")"<<std::endl;
1693 if(nodepos_old != v3s16(-32768,-32768,-32768))
1695 client.clearTempMod(nodepos_old);
1697 nodepos_old = v3s16(-32768,-32768,-32768);
1701 if(input->getLeftClicked() ||
1702 (input->getLeftState() && nodepos != nodepos_old))
1704 dstream<<DTIME<<"Started digging"<<std::endl;
1705 client.groundAction(0, nodepos, neighbourpos, g_selected_item);
1707 if(input->getLeftClicked())
1709 client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, 0));
1711 if(input->getLeftState())
1713 MapNode n = client.getNode(nodepos);
1715 // Get tool name. Default is "" = bare hands
1716 std::string toolname = "";
1717 InventoryList *mlist = local_inventory.getList("main");
1720 InventoryItem *item = mlist->getItem(g_selected_item);
1721 if(item && (std::string)item->getName() == "ToolItem")
1723 ToolItem *titem = (ToolItem*)item;
1724 toolname = titem->getToolName();
1728 // Get digging properties for material and tool
1729 content_t material = n.getContent();
1730 DiggingProperties prop =
1731 getDiggingProperties(material, toolname);
1733 float dig_time_complete = 0.0;
1735 if(prop.diggable == false)
1737 /*dstream<<"Material "<<(int)material
1738 <<" not diggable with \""
1739 <<toolname<<"\""<<std::endl;*/
1740 // I guess nobody will wait for this long
1741 dig_time_complete = 10000000.0;
1745 dig_time_complete = prop.time;
1748 if(dig_time_complete >= 0.001)
1750 dig_index = (u16)((float)CRACK_ANIMATION_LENGTH
1751 * dig_time/dig_time_complete);
1753 // This is for torches
1756 dig_index = CRACK_ANIMATION_LENGTH;
1759 if(dig_index < CRACK_ANIMATION_LENGTH)
1761 //TimeTaker timer("client.setTempMod");
1762 //dstream<<"dig_index="<<dig_index<<std::endl;
1763 client.setTempMod(nodepos, NodeMod(NODEMOD_CRACK, dig_index));
1767 dstream<<DTIME<<"Digging completed"<<std::endl;
1768 client.groundAction(3, nodepos, neighbourpos, g_selected_item);
1769 client.clearTempMod(nodepos);
1770 client.removeNode(nodepos);
1774 nodig_delay_counter = dig_time_complete
1775 / (float)CRACK_ANIMATION_LENGTH;
1777 // We don't want a corresponding delay to
1778 // very time consuming nodes
1779 if(nodig_delay_counter > 0.5)
1781 nodig_delay_counter = 0.5;
1783 // We want a slight delay to very little
1784 // time consuming nodes
1785 float mindelay = 0.15;
1786 if(nodig_delay_counter < mindelay)
1788 nodig_delay_counter = mindelay;
1794 camera.setDigging(0); // left click animation
1799 if(input->getRightClicked())
1801 dstream<<DTIME<<"Ground right-clicked"<<std::endl;
1803 // If metadata provides an inventory view, activate it
1804 if(meta && meta->getInventoryDrawSpecString() != "" && !random_input)
1806 dstream<<DTIME<<"Launching custom inventory view"<<std::endl;
1808 Construct the unique identification string of the node
1810 std::string current_name;
1811 current_name += "nodemeta:";
1812 current_name += itos(nodepos.X);
1813 current_name += ",";
1814 current_name += itos(nodepos.Y);
1815 current_name += ",";
1816 current_name += itos(nodepos.Z);
1822 core::array<GUIInventoryMenu::DrawSpec> draw_spec;
1824 GUIInventoryMenu::makeDrawSpecArrayFromString(
1826 meta->getInventoryDrawSpecString(),
1829 GUIInventoryMenu *menu =
1830 new GUIInventoryMenu(guienv, guiroot, -1,
1831 &g_menumgr, invsize,
1832 client.getInventoryContext(),
1834 menu->setDrawSpec(draw_spec);
1837 else if(meta && meta->typeId() == CONTENT_SIGN_WALL && !random_input)
1839 dstream<<"Sign node right-clicked"<<std::endl;
1841 SignNodeMetadata *signmeta = (SignNodeMetadata*)meta;
1843 // Get a new text for it
1845 TextDest *dest = new TextDestSignNode(nodepos, &client);
1847 std::wstring wtext =
1848 narrow_to_wide(signmeta->getText());
1850 (new GUITextInputMenu(guienv, guiroot, -1,
1856 client.groundAction(1, nodepos, neighbourpos, g_selected_item);
1857 camera.setDigging(1); // right click animation
1861 nodepos_old = nodepos;
1864 } // selected_object == NULL
1866 if(left_punch || (input->getLeftClicked() && !left_punch_muted))
1868 camera.setDigging(0); // left click animation
1871 input->resetLeftClicked();
1872 input->resetRightClicked();
1874 if(input->getLeftReleased())
1876 dstream<<DTIME<<"Left button released (stopped digging)"
1878 client.groundAction(2, v3s16(0,0,0), v3s16(0,0,0), 0);
1880 if(input->getRightReleased())
1882 //dstream<<DTIME<<"Right released"<<std::endl;
1886 input->resetLeftReleased();
1887 input->resetRightReleased();
1890 Calculate stuff for drawing
1893 u32 daynight_ratio = client.getDayNightRatio();
1894 u8 l = decode_light((daynight_ratio * LIGHT_SUN) / 1000);
1895 video::SColor bgcolor = video::SColor(
1897 bgcolor_bright.getRed() * l / 255,
1898 bgcolor_bright.getGreen() * l / 255,
1899 bgcolor_bright.getBlue() * l / 255);
1900 /*skycolor.getRed() * l / 255,
1901 skycolor.getGreen() * l / 255,
1902 skycolor.getBlue() * l / 255);*/
1904 float brightness = (float)l/255.0;
1909 if(fabs(brightness - old_brightness) > 0.01)
1910 update_skybox(driver, smgr, skybox, brightness);
1917 clouds->step(dtime);
1918 clouds->update(v2f(player_position.X, player_position.Z),
1919 0.05+brightness*0.95);
1927 farmesh_range = draw_control.wanted_range * 10;
1928 if(draw_control.range_all && farmesh_range < 500)
1929 farmesh_range = 500;
1930 if(farmesh_range > 1000)
1931 farmesh_range = 1000;
1933 farmesh->step(dtime);
1934 farmesh->update(v2f(player_position.X, player_position.Z),
1935 0.05+brightness*0.95, farmesh_range);
1938 // Store brightness value
1939 old_brightness = brightness;
1945 if(g_settings->getBool("enable_fog") == true)
1950 range = BS*farmesh_range;
1954 range = draw_control.wanted_range*BS + MAP_BLOCKSIZE*BS*1.5;
1955 if(draw_control.range_all)
1958 range = range * 0.5 + 25*BS;
1963 video::EFT_FOG_LINEAR,
1975 video::EFT_FOG_LINEAR,
1985 Update gui stuff (0ms)
1988 //TimeTaker guiupdatetimer("Gui updating");
1991 static float drawtime_avg = 0;
1992 drawtime_avg = drawtime_avg * 0.95 + (float)drawtime*0.05;
1993 static float beginscenetime_avg = 0;
1994 beginscenetime_avg = beginscenetime_avg * 0.95 + (float)beginscenetime*0.05;
1995 static float scenetime_avg = 0;
1996 scenetime_avg = scenetime_avg * 0.95 + (float)scenetime*0.05;
1997 static float endscenetime_avg = 0;
1998 endscenetime_avg = endscenetime_avg * 0.95 + (float)endscenetime*0.05;
2001 snprintf(temptext, 300, "Minetest-c55 %s ("
2004 " drawtime=%.0f, beginscenetime=%.0f"
2005 ", scenetime=%.0f, endscenetime=%.0f",
2007 draw_control.range_all,
2014 guitext->setText(narrow_to_wide(temptext).c_str());
2019 snprintf(temptext, 300,
2020 "(% .1f, % .1f, % .1f)"
2021 " (% .3f < btime_jitter < % .3f"
2022 ", dtime_jitter = % .1f %%"
2023 ", v_range = %.1f)",
2024 player_position.X/BS,
2025 player_position.Y/BS,
2026 player_position.Z/BS,
2027 busytime_jitter1_min_sample,
2028 busytime_jitter1_max_sample,
2029 dtime_jitter1_max_fraction * 100.0,
2030 draw_control.wanted_range
2033 guitext2->setText(narrow_to_wide(temptext).c_str());
2037 guitext_info->setText(infotext.c_str());
2041 Get chat messages from client
2045 std::wstring message;
2046 while(client.getChatMessage(message))
2048 chat_lines.push_back(ChatLine(message));
2049 /*if(chat_lines.size() > 6)
2051 core::list<ChatLine>::Iterator
2052 i = chat_lines.begin();
2053 chat_lines.erase(i);
2056 // Append them to form the whole static text and throw
2057 // it to the gui element
2059 // This will correspond to the line number counted from
2060 // top to bottom, from size-1 to 0
2061 s16 line_number = chat_lines.size();
2062 // Count of messages to be removed from the top
2063 u16 to_be_removed_count = 0;
2064 for(core::list<ChatLine>::Iterator
2065 i = chat_lines.begin();
2066 i != chat_lines.end(); i++)
2068 // After this, line number is valid for this loop
2073 This results in a maximum age of 60*6 to the
2074 lowermost line and a maximum of 6 lines
2076 float allowed_age = (6-line_number) * 60.0;
2078 if((*i).age > allowed_age)
2080 to_be_removed_count++;
2083 whole += (*i).text + L'\n';
2085 for(u16 i=0; i<to_be_removed_count; i++)
2087 core::list<ChatLine>::Iterator
2088 it = chat_lines.begin();
2089 chat_lines.erase(it);
2091 guitext_chat->setText(whole.c_str());
2093 // Update gui element size and position
2095 /*core::rect<s32> rect(
2097 screensize.Y - guitext_chat_pad_bottom
2098 - text_height*chat_lines.size(),
2100 screensize.Y - guitext_chat_pad_bottom
2102 core::rect<s32> rect(
2106 50 + guitext_chat->getTextHeight()
2109 guitext_chat->setRelativePosition(rect);
2111 if(chat_lines.size() == 0)
2112 guitext_chat->setVisible(false);
2114 guitext_chat->setVisible(true);
2121 static u16 old_selected_item = 65535;
2122 if(client.getLocalInventoryUpdated()
2123 || g_selected_item != old_selected_item)
2125 client.selectPlayerItem(g_selected_item);
2126 old_selected_item = g_selected_item;
2127 //dstream<<"Updating local inventory"<<std::endl;
2128 client.getLocalInventory(local_inventory);
2130 // Update wielded tool
2131 InventoryList *mlist = local_inventory.getList("main");
2132 InventoryItem *item = NULL;
2134 item = mlist->getItem(g_selected_item);
2139 Send actions returned by the inventory menu
2141 while(inventory_action_queue.size() != 0)
2143 InventoryAction *a = inventory_action_queue.pop_front();
2145 client.sendInventoryAction(a);
2154 TimeTaker drawtimer("Drawing");
2158 TimeTaker timer("beginScene");
2159 driver->beginScene(true, true, bgcolor);
2160 //driver->beginScene(false, true, bgcolor);
2161 beginscenetime = timer.stop(true);
2166 //dstream<<DTIME<<"smgr->drawAll()"<<std::endl;
2169 TimeTaker timer("smgr");
2171 scenetime = timer.stop(true);
2175 //TimeTaker timer9("auxiliary drawings");
2179 //TimeTaker //timer10("//timer10");
2185 driver->setMaterial(m);
2187 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
2189 for(core::list< core::aabbox3d<f32> >::Iterator i=hilightboxes.begin();
2190 i != hilightboxes.end(); i++)
2192 /*dstream<<"hilightbox min="
2193 <<"("<<i->MinEdge.X<<","<<i->MinEdge.Y<<","<<i->MinEdge.Z<<")"
2195 <<"("<<i->MaxEdge.X<<","<<i->MaxEdge.Y<<","<<i->MaxEdge.Z<<")"
2197 driver->draw3DBox(*i, video::SColor(255,0,0,0));
2204 // Warning: This clears the Z buffer.
2205 camera.drawWieldedTool();
2212 client.renderPostFx();
2218 if(g_settings->getBool("frametime_graph") == true)
2221 for(core::list<float>::Iterator
2222 i = frametime_log.begin();
2223 i != frametime_log.end();
2226 driver->draw2DLine(v2s32(x,50),
2227 v2s32(x,50+(*i)*1000),
2228 video::SColor(255,255,255,255));
2236 driver->draw2DLine(displaycenter - core::vector2d<s32>(10,0),
2237 displaycenter + core::vector2d<s32>(10,0),
2238 video::SColor(255,255,255,255));
2239 driver->draw2DLine(displaycenter - core::vector2d<s32>(0,10),
2240 displaycenter + core::vector2d<s32>(0,10),
2241 video::SColor(255,255,255,255));
2246 //TimeTaker //timer11("//timer11");
2258 draw_hotbar(driver, font, v2s32(displaycenter.X, screensize.Y),
2259 hotbar_imagesize, hotbar_itemcount, &local_inventory,
2266 if(damage_flash_timer > 0.0)
2268 damage_flash_timer -= dtime;
2270 video::SColor color(128,255,0,0);
2271 driver->draw2DRectangle(color,
2272 core::rect<s32>(0,0,screensize.X,screensize.Y),
2280 TimeTaker timer("endScene");
2282 endscenetime = timer.stop(true);
2285 drawtime = drawtimer.stop(true);
2291 static s16 lastFPS = 0;
2292 //u16 fps = driver->getFPS();
2293 u16 fps = (1.0/dtime_avg1);
2297 core::stringw str = L"Minetest [";
2298 str += driver->getName();
2302 device->setWindowCaption(str.c_str());
2314 Draw a "shutting down" screen, which will be shown while the map
2315 generator and other stuff quits
2318 /*gui::IGUIStaticText *gui_shuttingdowntext = */
2319 draw_load_screen(L"Shutting down stuff...", driver, font);
2320 /*driver->beginScene(true, true, video::SColor(255,0,0,0));
2323 gui_shuttingdowntext->remove();*/