tuned lava/universal damage code
[oweals/minetest.git] / src / game.cpp
index 337879cb8f2302e4a2b982023511eb5666b173bb..c740ed252c2c949ba6abbffd57923dba8b0af215 100644 (file)
@@ -417,7 +417,7 @@ void getPointedNode(Client *client, v3f player_position,
                try
                {
                        n = client->getNode(v3s16(x,y,z));
-                       if(content_pointable(n.d) == false)
+                       if(content_pointable(n.getContent()) == false)
                                continue;
                }
                catch(InvalidPositionException &e)
@@ -442,9 +442,9 @@ void getPointedNode(Client *client, v3f player_position,
                /*
                        Meta-objects
                */
-               if(n.d == CONTENT_TORCH)
+               if(n.getContent() == CONTENT_TORCH)
                {
-                       v3s16 dir = unpackDir(n.dir);
+                       v3s16 dir = unpackDir(n.param2);
                        v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
                        dir_f *= BS/2 - BS/6 - BS/20;
                        v3f cpf = npf + dir_f;
@@ -489,9 +489,9 @@ void getPointedNode(Client *client, v3f player_position,
                                }
                        }
                }
-               else if(n.d == CONTENT_SIGN_WALL)
+               else if(n.getContent() == CONTENT_SIGN_WALL)
                {
-                       v3s16 dir = unpackDir(n.dir);
+                       v3s16 dir = unpackDir(n.param2);
                        v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
                        dir_f *= BS/2 - BS/6 - BS/20;
                        v3f cpf = npf + dir_f;
@@ -538,6 +538,93 @@ void getPointedNode(Client *client, v3f player_position,
                                }
                        }
                }
+
+               else if(n.getContent() == CONTENT_LADDER)
+               {
+                       v3s16 dir = unpackDir(n.param2);
+                       v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
+                       dir_f *= BS/2 - BS/6 - BS/20;
+                       v3f cpf = npf + dir_f;
+                       f32 distance = (cpf - camera_position).getLength();
+
+                       v3f vertices[4] =
+                       {
+                               v3f(BS*0.42,-BS/2,-BS/2),
+                               v3f(BS*0.49, BS/2, BS/2),
+                       };
+
+                       for(s32 i=0; i<2; i++)
+                       {
+                               if(dir == v3s16(1,0,0))
+                                       vertices[i].rotateXZBy(0);
+                               if(dir == v3s16(-1,0,0))
+                                       vertices[i].rotateXZBy(180);
+                               if(dir == v3s16(0,0,1))
+                                       vertices[i].rotateXZBy(90);
+                               if(dir == v3s16(0,0,-1))
+                                       vertices[i].rotateXZBy(-90);
+                               if(dir == v3s16(0,-1,0))
+                                       vertices[i].rotateXYBy(-90);
+                               if(dir == v3s16(0,1,0))
+                                       vertices[i].rotateXYBy(90);
+
+                               vertices[i] += npf;
+                       }
+
+                       core::aabbox3d<f32> box;
+
+                       box = core::aabbox3d<f32>(vertices[0]);
+                       box.addInternalPoint(vertices[1]);
+
+                       if(distance < mindistance)
+                       {
+                               if(box.intersectsWithLine(shootline))
+                               {
+                                       nodefound = true;
+                                       nodepos = np;
+                                       neighbourpos = np;
+                                       mindistance = distance;
+                                       nodehilightbox = box;
+                               }
+                       }
+               }
+               else if(n.getContent() == CONTENT_RAIL)
+               {
+                       v3s16 dir = unpackDir(n.param0);
+                       v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
+                       dir_f *= BS/2 - BS/6 - BS/20;
+                       v3f cpf = npf + dir_f;
+                       f32 distance = (cpf - camera_position).getLength();
+
+                       float d = (float)BS/16;
+                       v3f vertices[4] =
+                       {
+                               v3f(BS/2, -BS/2+d, -BS/2),
+                               v3f(-BS/2, -BS/2, BS/2),
+                       };
+
+                       for(s32 i=0; i<2; i++)
+                       {
+                               vertices[i] += npf;
+                       }
+
+                       core::aabbox3d<f32> box;
+
+                       box = core::aabbox3d<f32>(vertices[0]);
+                       box.addInternalPoint(vertices[1]);
+
+                       if(distance < mindistance)
+                       {
+                               if(box.intersectsWithLine(shootline))
+                               {
+                                       nodefound = true;
+                                       nodepos = np;
+                                       neighbourpos = np;
+                                       mindistance = distance;
+                                       nodehilightbox = box;
+                               }
+                       }
+               }
                /*
                        Regular blocks
                */
@@ -639,6 +726,34 @@ void update_skybox(video::IVideoDriver* driver,
        }
 }
 
+/*
+       Draws a screen with a single text on it.
+       Text will be removed when the screen is drawn the next time.
+*/
+/*gui::IGUIStaticText **/
+void draw_load_screen(const std::wstring &text,
+               video::IVideoDriver* driver, gui::IGUIFont* font)
+{
+       v2u32 screensize = driver->getScreenSize();
+       const wchar_t *loadingtext = text.c_str();
+       core::vector2d<u32> textsize_u = font->getDimension(loadingtext);
+       core::vector2d<s32> textsize(textsize_u.X,textsize_u.Y);
+       core::vector2d<s32> center(screensize.X/2, screensize.Y/2);
+       core::rect<s32> textrect(center - textsize/2, center + textsize/2);
+
+       gui::IGUIStaticText *guitext = guienv->addStaticText(
+                       loadingtext, textrect, false, false);
+       guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
+
+       driver->beginScene(true, true, video::SColor(255,0,0,0));
+       guienv->drawAll();
+       driver->endScene();
+       
+       guitext->remove();
+       
+       //return guitext;
+}
+
 void the_game(
        bool &kill,
        bool random_input,
@@ -650,18 +765,24 @@ void the_game(
        std::string password,
        std::string address,
        u16 port,
-       std::wstring &error_message
+       std::wstring &error_message,
+    std::string configpath
 )
 {
        video::IVideoDriver* driver = device->getVideoDriver();
        scene::ISceneManager* smgr = device->getSceneManager();
+       
+       // Calculate text height using the font
+       u32 text_height = font->getDimension(L"Random test string").Height;
 
        v2u32 screensize(0,0);
        v2u32 last_screensize(0,0);
        screensize = driver->getScreenSize();
 
        const s32 hotbar_itemcount = 8;
-       const s32 hotbar_imagesize = 36;
+       //const s32 hotbar_imagesize = 36;
+       //const s32 hotbar_imagesize = 64;
+       s32 hotbar_imagesize = 48;
        
        // The color of the sky
 
@@ -672,20 +793,10 @@ void the_game(
        /*
                Draw "Loading" screen
        */
-       const wchar_t *loadingtext = L"Loading and connecting...";
-       u32 text_height = font->getDimension(loadingtext).Height;
-       core::vector2d<s32> center(screensize.X/2, screensize.Y/2);
-       core::vector2d<s32> textsize(300, text_height);
-       core::rect<s32> textrect(center - textsize/2, center + textsize/2);
-
-       gui::IGUIStaticText *gui_loadingtext = guienv->addStaticText(
-                       loadingtext, textrect, false, false);
-       gui_loadingtext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
-
-       driver->beginScene(true, true, video::SColor(255,0,0,0));
-       guienv->drawAll();
-       driver->endScene();
+       /*gui::IGUIStaticText *gui_loadingtext = */
+       //draw_load_screen(L"Loading and connecting...", driver, font);
 
+       draw_load_screen(L"Loading...", driver, font);
        
        /*
                Create server.
@@ -693,8 +804,9 @@ void the_game(
        */
        SharedPtr<Server> server;
        if(address == ""){
+               draw_load_screen(L"Creating server...", driver, font);
                std::cout<<DTIME<<"Creating server"<<std::endl;
-               server = new Server(map_dir);
+               server = new Server(map_dir, configpath);
                server->start(port);
        }
        
@@ -702,9 +814,11 @@ void the_game(
                Create client
        */
 
+       draw_load_screen(L"Creating client...", driver, font);
        std::cout<<DTIME<<"Creating client"<<std::endl;
        Client client(device, playername.c_str(), password, draw_control);
                        
+       draw_load_screen(L"Resolving address...", driver, font);
        Address connect_address(0,0,0,0, port);
        try{
                if(address == "")
@@ -718,7 +832,7 @@ void the_game(
                std::cout<<DTIME<<"Couldn't resolve address"<<std::endl;
                //return 0;
                error_message = L"Couldn't resolve address";
-               gui_loadingtext->remove();
+               //gui_loadingtext->remove();
                return;
        }
 
@@ -751,11 +865,17 @@ void the_game(
                        {
                                break;
                        }
+                       
+                       std::wostringstream ss;
+                       ss<<L"Connecting to server... (timeout in ";
+                       ss<<(int)(10.0 - time_counter + 1.0);
+                       ss<<L" seconds)";
+                       draw_load_screen(ss.str(), driver, font);
 
-                       // Update screen
+                       /*// Update screen
                        driver->beginScene(true, true, video::SColor(255,0,0,0));
                        guienv->drawAll();
-                       driver->endScene();
+                       driver->endScene();*/
 
                        // Update client and server
 
@@ -785,7 +905,7 @@ void the_game(
                        error_message = L"Connection timed out.";
                        std::cout<<DTIME<<"Timed out."<<std::endl;
                }
-               gui_loadingtext->remove();
+               //gui_loadingtext->remove();
                return;
        }
 
@@ -847,7 +967,7 @@ void the_game(
                Move into game
        */
        
-       gui_loadingtext->remove();
+       //gui_loadingtext->remove();
 
        /*
                Add some gui stuff
@@ -940,6 +1060,16 @@ void the_game(
 
        while(device->run() && kill == false)
        {
+               //std::cerr<<"frame"<<std::endl;
+
+               if(client.accessDenied())
+               {
+                       error_message = L"Access denied. Reason: "
+                                       +client.accessDeniedReason();
+                       std::cout<<DTIME<<wide_to_narrow(error_message)<<std::endl;
+                       break;
+               }
+
                if(g_gamecallback->disconnect_requested)
                {
                        g_gamecallback->disconnect_requested = false;
@@ -965,6 +1095,14 @@ void the_game(
                screensize = driver->getScreenSize();
                v2s32 displaycenter(screensize.X/2,screensize.Y/2);
                //bool screensize_changed = screensize != last_screensize;
+
+               // Resize hotbar
+               if(screensize.Y <= 800)
+                       hotbar_imagesize = 32;
+               else if(screensize.Y <= 1280)
+                       hotbar_imagesize = 48;
+               else
+                       hotbar_imagesize = 64;
                
                // Hilight boxes collected during the loop and displayed
                core::list< core::aabbox3d<f32> > hilightboxes;
@@ -1057,7 +1195,7 @@ void the_game(
                */
 
                static f32 dtime_avg1 = 0.0;
-               dtime_avg1 = dtime_avg1 * 0.98 + dtime * 0.02;
+               dtime_avg1 = dtime_avg1 * 0.96 + dtime * 0.04;
                f32 dtime_jitter1 = dtime - dtime_avg1;
 
                static f32 dtime_jitter1_max_sample = 0.0;
@@ -1221,6 +1359,38 @@ void the_game(
                                chat_lines.push_back(ChatLine(L"fast_move enabled"));
                        }
                }
+               else if(input->wasKeyDown(getKeySetting("keymap_frametime_graph")))
+               {
+                       if(g_settings.getBool("frametime_graph"))
+                       {
+                               g_settings.set("frametime_graph","false");
+                               chat_lines.push_back(ChatLine(L"frametime_graph disabled"));
+                       }
+                       else
+                       {
+                               g_settings.set("frametime_graph","true");
+                               chat_lines.push_back(ChatLine(L"frametime_graph enabled"));
+                       }
+               }
+               else if(input->wasKeyDown(getKeySetting("keymap_screenshot")))
+               {
+                       irr::video::IImage* const image = driver->createScreenShot(); 
+                       if (image) { 
+                               irr::c8 filename[256]; 
+                               snprintf(filename, 256, "%s/screenshot_%u.png", 
+                                                g_settings.get("screenshot_path").c_str(),
+                                                device->getTimer()->getRealTime()); 
+                               if (driver->writeImageToFile(image, filename)) {
+                                       std::wstringstream sstr;
+                                       sstr<<"Saved screenshot to '"<<filename<<"'";
+                                       dstream<<"Saved screenshot to '"<<filename<<"'"<<std::endl;
+                                       chat_lines.push_back(ChatLine(sstr.str()));
+                               } else{
+                                       dstream<<"Failed to save screenshot '"<<filename<<"'"<<std::endl;
+                               }
+                               image->drop(); 
+                       }                        
+               }
 
                // Item selection with mouse wheel
                {
@@ -1359,7 +1529,8 @@ void the_game(
                }
                
                // Get player position
-               v3f player_position = client.getPlayerPosition();
+               v3f camera_position;
+               v3f player_position = client.getPlayerPosition(&camera_position);
 
                //TimeTaker //timer2("//timer2");
 
@@ -1419,11 +1590,6 @@ void the_game(
                v3f camera_direction = v3f(0,0,1);
                camera_direction.rotateYZBy(camera_pitch);
                camera_direction.rotateXZBy(camera_yaw);
-               
-               // This is at the height of the eyes of the current figure
-               //v3f camera_position = player_position + v3f(0, BS+BS/2, 0);
-               // This is more like in minecraft
-               v3f camera_position = player_position + v3f(0, BS+BS*0.625, 0);
 
                camera->setPosition(camera_position);
                // *100.0 helps in large map coordinates
@@ -1545,6 +1711,8 @@ void the_game(
                        else if(input->getRightClicked())
                        {
                                std::cout<<DTIME<<"Right-clicked object"<<std::endl;
+                               client.clickActiveObject(1,
+                                               selected_active_object->getId(), g_selected_item);
                        }
                }
                else // selected_object == NULL
@@ -1648,7 +1816,7 @@ void the_game(
                                        }
 
                                        // Get digging properties for material and tool
-                                       u8 material = n.d;
+                                       content_t material = n.getContent();
                                        DiggingProperties prop =
                                                        getDiggingProperties(material, toolname);
                                        
@@ -2040,6 +2208,7 @@ void the_game(
                if(client.getLocalInventoryUpdated()
                                || g_selected_item != old_selected_item)
                {
+                       client.selectPlayerItem(g_selected_item);
                        old_selected_item = g_selected_item;
                        //std::cout<<"Updating local inventory"<<std::endl;
                        client.getLocalInventory(local_inventory);
@@ -2146,6 +2315,13 @@ void the_game(
                // 0-1ms
                guienv->drawAll();
 
+               /*
+                       Environment post fx
+               */
+               {
+                       client.getEnv()->drawPostFx(driver, camera_position);
+               }
+               
                /*
                        Draw hotbar
                */
@@ -2167,7 +2343,7 @@ void the_game(
                                        core::rect<s32>(0,0,screensize.X,screensize.Y),
                                        NULL);
                }
-               
+
                /*
                        End scene
                */
@@ -2210,15 +2386,12 @@ void the_game(
                generator and other stuff quits
        */
        {
-               const wchar_t *shuttingdowntext = L"Shutting down stuff...";
-               gui::IGUIStaticText *gui_shuttingdowntext = guienv->addStaticText(
-                               shuttingdowntext, textrect, false, false);
-               gui_shuttingdowntext->setTextAlignment(gui::EGUIA_CENTER,
-                               gui::EGUIA_UPPERLEFT);
-               driver->beginScene(true, true, video::SColor(255,0,0,0));
+               /*gui::IGUIStaticText *gui_shuttingdowntext = */
+               draw_load_screen(L"Shutting down stuff...", driver, font);
+               /*driver->beginScene(true, true, video::SColor(255,0,0,0));
                guienv->drawAll();
                driver->endScene();
-               gui_shuttingdowntext->remove();
+               gui_shuttingdowntext->remove();*/
        }
 }