ServerRemotePlayer implements ServerActiveObject
[oweals/minetest.git] / src / tile.cpp
index 4e441329ab335c4e9ec9dc0b70777a68640fbbce..f328ab766d74d0273752c775689f9f9b3f51ddce 100644 (file)
@@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "settings.h"
 #include <ICameraSceneNode.h>
 #include "log.h"
+#include "mapnode.h" // For texture atlas making
+#include "mineral.h" // For texture atlas making
 
 /*
        A cache from texture name to texture path
@@ -128,7 +130,8 @@ std::string getTexturePath(const std::string &filename)
        */
        if(fullpath == "")
        {
-               std::string testpath = porting::getDataPath(filename.c_str());
+               std::string rel_path = std::string("textures")+DIR_DELIM+filename;
+               std::string testpath = porting::path_data + DIR_DELIM + rel_path;
                // Check all filename extensions. Returns "" if not found.
                fullpath = getImagePath(testpath);
        }
@@ -507,52 +510,62 @@ void TextureSource::buildMainAtlas()
        }
 
        /*
-               A list of stuff to include in the texture atlas.
-
-               It is a single-dimensional texture atlas due to the need to tile
-               textures.
-               
-               It should contain as much of the stuff shown in game as possible,
-               to minimize texture changes.
-
-               It fills up quickly, so do not add anything that isn't contained
-               in most MapBlocks. E.g. mese isn't suitable but stone is.
+               Grab list of stuff to include in the texture atlas from the
+               main content features
        */
 
-       core::array<std::string> sourcelist;
-
-       sourcelist.push_back("stone.png");
-       sourcelist.push_back("mud.png");
-       sourcelist.push_back("sand.png");
-       sourcelist.push_back("grass.png");
-       sourcelist.push_back("grass_footsteps.png");
-       sourcelist.push_back("tree.png");
-       sourcelist.push_back("tree_top.png");
-       sourcelist.push_back("water.png");
-       sourcelist.push_back("leaves.png");
-       sourcelist.push_back("glass.png");
-       sourcelist.push_back("mud.png^grass_side.png");
-       sourcelist.push_back("cobble.png");
-       sourcelist.push_back("mossycobble.png");
-       sourcelist.push_back("gravel.png");
-       sourcelist.push_back("jungletree.png");
-       
-       sourcelist.push_back("stone.png^mineral_coal.png");
-       sourcelist.push_back("stone.png^mineral_iron.png");
+       core::map<std::string, bool> sourcelist;
+
+       for(u16 j=0; j<MAX_CONTENT+1; j++)
+       {
+               if(j == CONTENT_IGNORE || j == CONTENT_AIR)
+                       continue;
+               ContentFeatures *f = &content_features(j);
+               for(core::map<std::string, bool>::Iterator
+                               i = f->used_texturenames.getIterator();
+                               i.atEnd() == false; i++)
+               {
+                       std::string name = i.getNode()->getKey();
+                       sourcelist[name] = true;
+
+                       if(f->often_contains_mineral){
+                               for(int k=1; k<MINERAL_COUNT; k++){
+                                       std::string mineraltexture = mineral_block_texture(k);
+                                       std::string fulltexture = name + "^" + mineraltexture;
+                                       sourcelist[fulltexture] = true;
+                               }
+                       }
+               }
+       }
        
+       infostream<<"Creating texture atlas out of textures: ";
+       for(core::map<std::string, bool>::Iterator
+                       i = sourcelist.getIterator();
+                       i.atEnd() == false; i++)
+       {
+               std::string name = i.getNode()->getKey();
+               infostream<<"\""<<name<<"\" ";
+       }
+       infostream<<std::endl;
+
        // Padding to disallow texture bleeding
        s32 padding = 16;
 
+       s32 column_width = 256;
+       s32 column_padding = 16;
+
        /*
                First pass: generate almost everything
        */
        core::position2d<s32> pos_in_atlas(0,0);
        
-       pos_in_atlas.Y += padding;
+       pos_in_atlas.Y = padding;
 
-       for(u32 i=0; i<sourcelist.size(); i++)
+       for(core::map<std::string, bool>::Iterator
+                       i = sourcelist.getIterator();
+                       i.atEnd() == false; i++)
        {
-               std::string name = sourcelist[i];
+               std::string name = i.getNode()->getKey();
 
                /*video::IImage *img = driver->createImageFromFile(
                                getTexturePath(name.c_str()).c_str());
@@ -586,20 +599,26 @@ void TextureSource::buildMainAtlas()
                        continue;
                }
 
-               // Stop making atlas if atlas is full
+               // Wrap columns and stop making atlas if atlas is full
                if(pos_in_atlas.Y + dim.Height > atlas_dim.Height)
                {
-                       infostream<<"TextureSource::buildMainAtlas(): "
-                                       <<"Atlas is full, not adding more textures."
-                                       <<std::endl;
-                       break;
+                       if(pos_in_atlas.X > (s32)atlas_dim.Width - 256 - padding){
+                               errorstream<<"TextureSource::buildMainAtlas(): "
+                                               <<"Atlas is full, not adding more textures."
+                                               <<std::endl;
+                               break;
+                       }
+                       pos_in_atlas.Y = padding;
+                       pos_in_atlas.X += column_width + column_padding;
                }
                
         infostream<<"TextureSource::buildMainAtlas(): Adding \""<<name
                 <<"\" to texture atlas"<<std::endl;
 
                // Tile it a few times in the X direction
-               u16 xwise_tiling = 16;
+               u16 xwise_tiling = column_width / dim.Width;
+               if(xwise_tiling > 16) // Limit to 16 (more gives no benefit)
+                       xwise_tiling = 16;
                for(u32 j=0; j<xwise_tiling; j++)
                {
                        // Copy the copy to the atlas
@@ -627,7 +646,7 @@ void TextureSource::buildMainAtlas()
                                dst_y = -y0 + pos_in_atlas.Y-1;
                                src_y = pos_in_atlas.Y;
                        }
-                       s32 x = x0 + pos_in_atlas.X * dim.Width;
+                       s32 x = x0 + pos_in_atlas.X;
                        video::SColor c = atlas_img->getPixel(x, src_y);
                        atlas_img->setPixel(x,dst_y,c);
                }
@@ -668,9 +687,11 @@ void TextureSource::buildMainAtlas()
        /*
                Second pass: set texture pointer in generated AtlasPointers
        */
-       for(u32 i=0; i<sourcelist.size(); i++)
+       for(core::map<std::string, bool>::Iterator
+                       i = sourcelist.getIterator();
+                       i.atEnd() == false; i++)
        {
-               std::string name = sourcelist[i];
+               std::string name = i.getNode()->getKey();
                if(m_name_to_id.find(name) == NULL)
                        continue;
                u32 id = m_name_to_id[name];
@@ -681,8 +702,12 @@ void TextureSource::buildMainAtlas()
        /*
                Write image to file so that it can be inspected
        */
-       /*driver->writeImageToFile(atlas_img, 
-                       getTexturePath("main_atlas.png").c_str());*/
+       /*std::string atlaspath = porting::path_userdata
+                       + DIR_DELIM + "generated_texture_atlas.png";
+       infostream<<"Removing and writing texture atlas for inspection to "
+                       <<atlaspath<<std::endl;
+       fs::RecursiveDelete(atlaspath);
+       driver->writeImageToFile(atlas_img, atlaspath.c_str());*/
 }
 
 video::IImage* generate_image_from_scratch(std::string name,
@@ -1092,22 +1117,23 @@ bool generate_image(std::string part_of_name, video::IImage *& baseimg,
                                core::dimension2d<u32> dim = image->getDimension();
                                baseimg = driver->createImage(video::ECF_A8R8G8B8, dim);
                                
+                               // Blit
+                               image->copyTo(baseimg);
+
+                               image->drop();
+
                                for(u32 y=0; y<dim.Height; y++)
                                for(u32 x=0; x<dim.Width; x++)
                                {
-                                       video::SColor c = image->getPixel(x,y);
+                                       video::SColor c = baseimg->getPixel(x,y);
                                        u32 r = c.getRed();
                                        u32 g = c.getGreen();
                                        u32 b = c.getBlue();
                                        if(!(r == r1 && g == g1 && b == b1))
                                                continue;
                                        c.setAlpha(0);
-                                       image->setPixel(x,y,c);
+                                       baseimg->setPixel(x,y,c);
                                }
-                               // Blit
-                               image->copyTo(baseimg);
-
-                               image->drop();
                        }
                }
                /*
@@ -1149,11 +1175,16 @@ bool generate_image(std::string part_of_name, video::IImage *& baseimg,
                        {
                                core::dimension2d<u32> dim = image->getDimension();
                                baseimg = driver->createImage(video::ECF_A8R8G8B8, dim);
+
+                               // Blit
+                               image->copyTo(baseimg);
+
+                               image->drop();
                                
                                for(u32 y=0; y<dim.Height; y++)
                                for(u32 x=0; x<dim.Width; x++)
                                {
-                                       video::SColor c = image->getPixel(x,y);
+                                       video::SColor c = baseimg->getPixel(x,y);
                                        u32 r = c.getRed();
                                        u32 g = c.getGreen();
                                        u32 b = c.getBlue();
@@ -1161,12 +1192,8 @@ bool generate_image(std::string part_of_name, video::IImage *& baseimg,
                                           !(r == r2 && g == g2 && b == b2))
                                                continue;
                                        c.setAlpha(0);
-                                       image->setPixel(x,y,c);
+                                       baseimg->setPixel(x,y,c);
                                }
-                               // Blit
-                               image->copyTo(baseimg);
-
-                               image->drop();
                        }
                }
                /*