Move tool stuff to tool.{h,cpp}
[oweals/minetest.git] / src / game.cpp
index e3380ace401da06211dceab384d822c2ca63dee0..bd84593b8d94a786088bd83e4faa2f2b28f7a6b9 100644 (file)
@@ -43,13 +43,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "gettext.h"
 #include "log.h"
 #include "filesys.h"
-
-/*
-       TODO: Move content-aware stuff to separate file by adding properties
-             and virtual interfaces
-*/
-#include "content_mapnode.h"
-#include "content_nodemeta.h"
+// Needed for determining pointing to nodes
+#include "mapnode_contentfeatures.h"
+#include "nodemetadata.h"
 
 /*
        Setting this to 1 enables a special camera mode that forces
@@ -114,9 +110,9 @@ struct TextDestChat : public TextDest
        Client *m_client;
 };
 
-struct TextDestSignNode : public TextDest
+struct TextDestNodeMetadata : public TextDest
 {
-       TextDestSignNode(v3s16 p, Client *client)
+       TextDestNodeMetadata(v3s16 p, Client *client)
        {
                m_p = p;
                m_client = client;
@@ -342,43 +338,15 @@ void getPointedNode(Client *client, v3f player_position,
                        v3s16(-1,0,0), // left
                };
                
-               /*
-                       Meta-objects
-               */
-               if(n.getContent() == CONTENT_TORCH)
+               ContentFeatures &f = content_features(n);
+               
+               if(f.selection_box.type == NODEBOX_FIXED)
                {
-                       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();
+                       f32 distance = (npf - camera_position).getLength();
 
-                       core::aabbox3d<f32> box;
-                       
-                       // bottom
-                       if(dir == v3s16(0,-1,0))
-                       {
-                               box = core::aabbox3d<f32>(
-                                       npf - v3f(BS/6, BS/2, BS/6),
-                                       npf + v3f(BS/6, -BS/2+BS/3*2, BS/6)
-                               );
-                       }
-                       // top
-                       else if(dir == v3s16(0,1,0))
-                       {
-                               box = core::aabbox3d<f32>(
-                                       npf - v3f(BS/6, -BS/2+BS/3*2, BS/6),
-                                       npf + v3f(BS/6, BS/2, BS/6)
-                               );
-                       }
-                       // side
-                       else
-                       {
-                               box = core::aabbox3d<f32>(
-                                       cpf - v3f(BS/6, BS/3, BS/6),
-                                       cpf + v3f(BS/6, BS/3, BS/6)
-                               );
-                       }
+                       core::aabbox3d<f32> box = f.selection_box.fixed;
+                       box.MinEdge += npf;
+                       box.MaxEdge += npf;
 
                        if(distance < mindistance)
                        {
@@ -392,7 +360,7 @@ void getPointedNode(Client *client, v3f player_position,
                                }
                        }
                }
-               else if(n.getContent() == CONTENT_SIGN_WALL)
+               else if(f.selection_box.type == NODEBOX_WALLMOUNTED)
                {
                        v3s16 dir = unpackDir(n.param2);
                        v3f dir_f = v3f(dir.X, dir.Y, dir.Z);
@@ -400,122 +368,43 @@ void getPointedNode(Client *client, v3f player_position,
                        v3f cpf = npf + dir_f;
                        f32 distance = (cpf - camera_position).getLength();
 
-                       v3f vertices[4] =
-                       {
-                               v3f(BS*0.42,-BS*0.35,-BS*0.4),
-                               v3f(BS*0.49, BS*0.35, BS*0.4),
-                       };
-
-                       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;
-                               }
+                       
+                       // top
+                       if(dir == v3s16(0,1,0)){
+                               box = f.selection_box.wall_top;
                        }
-               }
-
-               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;
+                       // bottom
+                       else if(dir == v3s16(0,-1,0)){
+                               box = f.selection_box.wall_bottom;
                        }
+                       // side
+                       else{
+                               v3f vertices[2] =
+                               {
+                                       f.selection_box.wall_side.MinEdge,
+                                       f.selection_box.wall_side.MaxEdge
+                               };
 
-                       core::aabbox3d<f32> box;
-
-                       box = core::aabbox3d<f32>(vertices[0]);
-                       box.addInternalPoint(vertices[1]);
-
-                       if(distance < mindistance)
-                       {
-                               if(box.intersectsWithLine(shootline))
+                               for(s32 i=0; i<2; i++)
                                {
-                                       nodefound = true;
-                                       nodepos = np;
-                                       neighbourpos = np;
-                                       mindistance = distance;
-                                       nodehilightbox = box;
+                                       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);
                                }
-                       }
-               }
-               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;
+                               box = core::aabbox3d<f32>(vertices[0]);
+                               box.addInternalPoint(vertices[1]);
                        }
 
-                       core::aabbox3d<f32> box;
-
-                       box = core::aabbox3d<f32>(vertices[0]);
-                       box.addInternalPoint(vertices[1]);
-
+                       box.MinEdge += npf;
+                       box.MaxEdge += npf;
+                       
                        if(distance < mindistance)
                        {
                                if(box.intersectsWithLine(shootline))
@@ -528,10 +417,7 @@ void getPointedNode(Client *client, v3f player_position,
                                }
                        }
                }
-               /*
-                       Regular blocks
-               */
-               else
+               else // NODEBOX_REGULAR
                {
                        for(u16 i=0; i<6; i++)
                        {
@@ -888,11 +774,11 @@ void the_game(
        //guitext_chat->setBackgroundColor(video::SColor(96,0,0,0));
        core::list<ChatLine> chat_lines;
        
-       // Profiler text
+       // Profiler text (size is updated when text is updated)
        gui::IGUIStaticText *guitext_profiler = guienv->addStaticText(
                        L"<Profiler>",
-                       core::rect<s32>(6, 4+(text_height+5)*3, 400,
-                       (text_height+5)*3 + text_height*35),
+                       core::rect<s32>(6, 4+(text_height+5)*2, 400,
+                       (text_height+5)*2 + text_height*35),
                        false, false);
        guitext_profiler->setBackgroundColor(video::SColor(80,0,0,0));
        guitext_profiler->setVisible(false);
@@ -951,6 +837,8 @@ void the_game(
        bool respawn_menu_active = false;
 
        bool show_profiler = false;
+       bool force_fog_off = false;
+       bool disable_camera_update = false;
 
        /*
                Main loop
@@ -1186,9 +1074,18 @@ void the_game(
 
                        std::ostringstream os(std::ios_base::binary);
                        g_profiler->print(os);
-                       guitext_profiler->setText(narrow_to_wide(os.str()).c_str());
+                       std::wstring text = narrow_to_wide(os.str());
+                       guitext_profiler->setText(text.c_str());
 
                        g_profiler->clear();
+                       
+                       s32 w = font->getDimension(text.c_str()).Width;
+                       if(w < 400)
+                               w = 400;
+                       core::rect<s32> rect(6, 4+(text_height+5)*2, 12+w,
+                                       8+(text_height+5)*2 +
+                                       font->getDimension(text.c_str()).Height);
+                       guitext_profiler->setRelativePosition(rect);
                }
 
                /*
@@ -1322,6 +1219,26 @@ void the_game(
                {
                        show_profiler = !show_profiler;
                        guitext_profiler->setVisible(show_profiler);
+                       if(show_profiler)
+                               chat_lines.push_back(ChatLine(L"Profiler disabled"));
+                       else
+                               chat_lines.push_back(ChatLine(L"Profiler enabled"));
+               }
+               else if(input->wasKeyDown(getKeySetting("keymap_toggle_force_fog_off")))
+               {
+                       force_fog_off = !force_fog_off;
+                       if(force_fog_off)
+                               chat_lines.push_back(ChatLine(L"Fog disabled"));
+                       else
+                               chat_lines.push_back(ChatLine(L"Fog enabled"));
+               }
+               else if(input->wasKeyDown(getKeySetting("keymap_toggle_update_camera")))
+               {
+                       disable_camera_update = !disable_camera_update;
+                       if(disable_camera_update)
+                               chat_lines.push_back(ChatLine(L"Camera update disabled"));
+                       else
+                               chat_lines.push_back(ChatLine(L"Camera update enabled"));
                }
 
                // Item selection with mouse wheel
@@ -1567,11 +1484,10 @@ void the_game(
                v3f camera_direction = camera.getDirection();
                f32 camera_fov = camera.getFovMax();
                
-               if(FIELD_OF_VIEW_TEST)
-                       client.updateCamera(v3f(0,0,0), v3f(0,0,1), camera_fov);
-               else
+               if(!disable_camera_update){
                        client.updateCamera(camera_position,
                                camera_direction, camera_fov);
+               }
 
                //timer2.stop();
                //TimeTaker //timer3("//timer3");
@@ -1863,23 +1779,22 @@ void the_game(
                                        menu->setDrawSpec(draw_spec);
                                        menu->drop();
                                }
-                               else if(meta && meta->typeId() == CONTENT_SIGN_WALL && !random_input)
+                               // If metadata provides text input, activate text input
+                               else if(meta && meta->allowsTextInput() && !random_input)
                                {
-                                       infostream<<"Sign node right-clicked"<<std::endl;
-                                       
-                                       SignNodeMetadata *signmeta = (SignNodeMetadata*)meta;
+                                       infostream<<"Launching metadata text input"<<std::endl;
                                        
                                        // Get a new text for it
 
-                                       TextDest *dest = new TextDestSignNode(nodepos, &client);
+                                       TextDest *dest = new TextDestNodeMetadata(nodepos, &client);
 
-                                       std::wstring wtext =
-                                                       narrow_to_wide(signmeta->getText());
+                                       std::wstring wtext = narrow_to_wide(meta->getText());
 
                                        (new GUITextInputMenu(guienv, guiroot, -1,
                                                        &g_menumgr, dest,
                                                        wtext))->drop();
                                }
+                               // Otherwise report right click to server
                                else
                                {
                                        client.groundAction(1, nodepos, neighbourpos, g_selected_item);
@@ -1971,7 +1886,7 @@ void the_game(
                        Fog
                */
                
-               if(g_settings->getBool("enable_fog") == true)
+               if(g_settings->getBool("enable_fog") == true && !force_fog_off)
                {
                        f32 range;
                        if(farmesh)
@@ -1981,12 +1896,11 @@ void the_game(
                        else
                        {
                                range = draw_control.wanted_range*BS + MAP_BLOCKSIZE*BS*1.5;
+                               range *= 0.9;
                                if(draw_control.range_all)
                                        range = 100000*BS;
                                /*if(range < 50*BS)
                                        range = range * 0.5 + 25*BS;*/
-                               // Move the invisible limit a bit further
-                               //range *= 1.2;
                        }
 
                        driver->setFog(