]> git.lizzy.rs Git - minetest.git/blobdiff - src/mapblock_mesh.cpp
Create node metadata when placing nodes again
[minetest.git] / src / mapblock_mesh.cpp
index 5d8c0b737474cf322a9c0b88d43839a53e361bf3..b843448c484c1cb70461d80593d4d27f433f6b1e 100644 (file)
@@ -25,7 +25,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "settings.h"
 #include "profiler.h"
 #include "nodedef.h"
-#include "tile.h"
 #include "gamedef.h"
 #include "content_mapblock.h"
 
@@ -82,6 +81,39 @@ void MeshMakeData::fill(u32 daynight_ratio, MapBlock *block)
        }
 }
 
+void MeshMakeData::fillSingleNode(u32 daynight_ratio, MapNode *node)
+{
+       m_daynight_ratio = daynight_ratio;
+       m_blockpos = v3s16(0,0,0);
+       m_temp_mods.clear();
+       
+       v3s16 blockpos_nodes = v3s16(0,0,0);
+       VoxelArea area(blockpos_nodes-v3s16(1,1,1)*MAP_BLOCKSIZE,
+                       blockpos_nodes+v3s16(1,1,1)*MAP_BLOCKSIZE*2-v3s16(1,1,1));
+       s32 volume = area.getVolume();
+       s32 our_node_index = area.index(1,1,1);
+
+       // Allocate this block + neighbors
+       m_vmanip.clear();
+       m_vmanip.addArea(area);
+
+       // Fill in data
+       MapNode *data = new MapNode[volume];
+       for(s32 i = 0; i < volume; i++)
+       {
+               if(i == our_node_index)
+               {
+                       data[i] = *node;
+               }
+               else
+               {
+                       data[i] = MapNode(CONTENT_AIR, LIGHT_MAX, 0);
+               }
+       }
+       m_vmanip.copyFrom(data, area, area.MinEdge, area.MinEdge, area.getExtent());
+       delete[] data;
+}
+
 /*
        vertex_dirs: v3s16[4]
 */
@@ -206,8 +238,8 @@ static void makeFastFace(TileSpec tile, u8 li0, u8 li1, u8 li2, u8 li3, v3f p,
        else if(scale.Y < 0.999 || scale.Y > 1.001) abs_scale = scale.Y;
        else if(scale.Z < 0.999 || scale.Z > 1.001) abs_scale = scale.Z;
 
-       v3f zerovector = v3f(0,0,0);
-       
+       v3f normal(dir.X, dir.Y, dir.Z);
+
        u8 alpha = tile.alpha;
        /*u8 alpha = 255;
        if(tile.id == TILE_WATER)
@@ -229,16 +261,16 @@ static void makeFastFace(TileSpec tile, u8 li0, u8 li1, u8 li2, u8 li3, v3f p,
        face.vertices[3] = video::S3DVertex(vertex_pos[3], v3f(0,1,0), c,
                        core::vector2d<f32>(x0+w*abs_scale, y0));*/
 
-       face.vertices[0] = video::S3DVertex(vertex_pos[0], v3f(0,1,0),
+       face.vertices[0] = video::S3DVertex(vertex_pos[0], normal,
                        MapBlock_LightColor(alpha, li0),
                        core::vector2d<f32>(x0+w*abs_scale, y0+h));
-       face.vertices[1] = video::S3DVertex(vertex_pos[1], v3f(0,1,0),
+       face.vertices[1] = video::S3DVertex(vertex_pos[1], normal,
                        MapBlock_LightColor(alpha, li1),
                        core::vector2d<f32>(x0, y0+h));
-       face.vertices[2] = video::S3DVertex(vertex_pos[2], v3f(0,1,0),
+       face.vertices[2] = video::S3DVertex(vertex_pos[2], normal,
                        MapBlock_LightColor(alpha, li2),
                        core::vector2d<f32>(x0, y0));
-       face.vertices[3] = video::S3DVertex(vertex_pos[3], v3f(0,1,0),
+       face.vertices[3] = video::S3DVertex(vertex_pos[3], normal,
                        MapBlock_LightColor(alpha, li3),
                        core::vector2d<f32>(x0+w*abs_scale, y0));
 
@@ -249,15 +281,48 @@ static void makeFastFace(TileSpec tile, u8 li0, u8 li1, u8 li2, u8 li3, v3f p,
        dest.push_back(face);
 }
        
+static TileSpec getTile(const MapNode &node, v3s16 dir, INodeDefManager *nodemgr)
+{
+       // Direction must be (1,0,0), (-1,0,0), (0,1,0), (0,-1,0),
+       // (0,0,1), (0,0,-1) or (0,0,0)
+       assert(dir.X * dir.X + dir.Y * dir.Y + dir.Z * dir.Z <= 1);
+
+       // Convert direction to single integer for table lookup
+       //  0 = (0,0,0)
+       //  1 = (1,0,0)
+       //  2 = (0,1,0)
+       //  3 = (0,0,1)
+       //  4 = invalid, treat as (0,0,0)
+       //  5 = (0,0,-1)
+       //  6 = (0,-1,0)
+       //  7 = (-1,0,0)
+       u8 dir_i = (dir.X + 2 * dir.Y + 3 * dir.Z) & 7;
+
+       // Get rotation for things like chests
+       u8 facedir = node.getFaceDir(nodemgr);
+       assert(facedir <= 3);
+       
+       static const u8 dir_to_tile[4 * 8] =
+       {
+               // 0  +X  +Y  +Z   0  -Z  -Y  -X
+                  0,  2,  0,  4,  0,  5,  1,  3,  // facedir = 0
+                  0,  4,  0,  3,  0,  2,  1,  5,  // facedir = 1
+                  0,  3,  0,  5,  0,  4,  1,  2,  // facedir = 2
+                  0,  5,  0,  2,  0,  3,  1,  4,  // facedir = 3
+       };
+
+       return nodemgr->get(node).tiles[dir_to_tile[facedir*8 + dir_i]];
+}
+
 /*
        Gets node tile from any place relative to block.
        Returns TILE_NODE if doesn't exist or should not be drawn.
 */
-static TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir,
-               NodeModMap &temp_mods, ITextureSource *tsrc, INodeDefManager *ndef)
+TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir,
+               NodeModMap *temp_mods, ITextureSource *tsrc, INodeDefManager *ndef)
 {
        TileSpec spec;
-       spec = mn.getTile(face_dir, tsrc, ndef);
+       spec = getTile(mn, face_dir, ndef);
        
        /*
                Check temporary modifications on this node
@@ -269,13 +334,15 @@ static TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir,
        {
                struct NodeMod mod = n->getValue();*/
        NodeMod mod;
-       if(temp_mods.get(p, &mod))
+       if(temp_mods && temp_mods->get(p, &mod))
        {
+               #if 0  // NODEMOD_CHANGECONTENT isn't used at the moment
                if(mod.type == NODEMOD_CHANGECONTENT)
                {
                        MapNode mn2(mod.param);
-                       spec = mn2.getTile(face_dir, tsrc, ndef);
+                       spec = getTile(mn2, face_dir, ndef);
                }
+               #endif
                if(mod.type == NODEMOD_CRACK)
                {
                        /*
@@ -305,19 +372,14 @@ static TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir,
        return spec;
 }
 
-static content_t getNodeContent(v3s16 p, MapNode mn, NodeModMap &temp_mods)
+static content_t getNodeContent(v3s16 p, MapNode mn, NodeModMap *temp_mods)
 {
        /*
                Check temporary modifications on this node
        */
-       /*core::map<v3s16, NodeMod>::Node *n;
-       n = m_temp_mods.find(p);
-       // If modified
-       if(n != NULL)
-       {
-               struct NodeMod mod = n->getValue();*/
+       #if 0  // NODEMOD_CHANGECONTENT isn't used at the moment
        NodeMod mod;
-       if(temp_mods.get(p, &mod))
+       if(temp_mods && temp_mods->get(p, &mod))
        {
                if(mod.type == NODEMOD_CHANGECONTENT)
                {
@@ -339,6 +401,7 @@ static content_t getNodeContent(v3s16 p, MapNode mn, NodeModMap &temp_mods)
                        */
                }
        }
+       #endif
 
        return mn.getContent();
 }
@@ -413,7 +476,7 @@ static void getTileInfo(
                v3s16 face_dir,
                u32 daynight_ratio,
                VoxelManipulator &vmanip,
-               NodeModMap &temp_mods,
+               NodeModMap *temp_mods,
                bool smooth_lighting,
                IGameDef *gamedef,
                // Output:
@@ -497,7 +560,7 @@ static void updateFastFaceRow(
                v3s16 face_dir,
                v3f face_dir_f,
                core::array<FastFace> &dest,
-               NodeModMap &temp_mods,
+               NodeModMap *temp_mods,
                VoxelManipulator &vmanip,
                v3s16 blockpos_nodes,
                bool smooth_lighting,
@@ -693,7 +756,7 @@ scene::SMesh* makeMapBlockMesh(MeshMakeData *data, IGameDef *gamedef)
                                                v3s16(0,1,0), //face dir
                                                v3f  (0,1,0),
                                                fastfaces_new,
-                                               data->m_temp_mods,
+                                               &data->m_temp_mods,
                                                data->m_vmanip,
                                                blockpos_nodes,
                                                smooth_lighting,
@@ -712,7 +775,7 @@ scene::SMesh* makeMapBlockMesh(MeshMakeData *data, IGameDef *gamedef)
                                                v3s16(1,0,0),
                                                v3f  (1,0,0),
                                                fastfaces_new,
-                                               data->m_temp_mods,
+                                               &data->m_temp_mods,
                                                data->m_vmanip,
                                                blockpos_nodes,
                                                smooth_lighting,
@@ -731,7 +794,7 @@ scene::SMesh* makeMapBlockMesh(MeshMakeData *data, IGameDef *gamedef)
                                                v3s16(0,0,1),
                                                v3f  (0,0,1),
                                                fastfaces_new,
-                                               data->m_temp_mods,
+                                               &data->m_temp_mods,
                                                data->m_vmanip,
                                                blockpos_nodes,
                                                smooth_lighting,