]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/test.cpp
.
[dragonfireclient.git] / src / test.cpp
index 8cdd84426855f6539cdd5af14ae1c2ab674022d6..1a172e4f5e0f736b8250d57d10dffe5a961f932f 100644 (file)
@@ -1,6 +1,24 @@
+/*
+Minetest-c55
+Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
 #include "test.h"
 #include "common_irrlicht.h"
-
 #include "debug.h"
 #include "map.h"
 #include "player.h"
 #include "connection.h"
 #include "utility.h"
 #include "serialization.h"
+#include "voxel.h"
 #include <sstream>
-
-#ifdef _WIN32
-       #include <windows.h>
-       #define sleep_ms(x) Sleep(x)
-#else
-       #include <unistd.h>
-       #define sleep_ms(x) usleep(x*1000)
-#endif
+#include "porting.h"
 
 /*
        Asserts that the exception occurs
@@ -53,6 +65,8 @@ struct TestCompress
 {
        void Run()
        {
+               { // ver 0
+
                SharedBuffer<u8> fromdata(4);
                fromdata[0]=1;
                fromdata[1]=5;
@@ -104,6 +118,64 @@ struct TestCompress
                {
                        assert(str_out2[i] == fromdata[i]);
                }
+
+               }
+
+               { // ver HIGHEST
+
+               SharedBuffer<u8> fromdata(4);
+               fromdata[0]=1;
+               fromdata[1]=5;
+               fromdata[2]=5;
+               fromdata[3]=1;
+               
+               std::ostringstream os(std::ios_base::binary);
+               compress(fromdata, os, SER_FMT_VER_HIGHEST);
+
+               std::string str_out = os.str();
+               
+               dstream<<"str_out.size()="<<str_out.size()<<std::endl;
+               dstream<<"TestCompress: 1,5,5,1 -> ";
+               for(u32 i=0; i<str_out.size(); i++)
+               {
+                       dstream<<(u32)str_out[i]<<",";
+               }
+               dstream<<std::endl;
+
+               /*assert(str_out.size() == 10);
+
+               assert(str_out[0] == 0);
+               assert(str_out[1] == 0);
+               assert(str_out[2] == 0);
+               assert(str_out[3] == 4);
+               assert(str_out[4] == 0);
+               assert(str_out[5] == 1);
+               assert(str_out[6] == 1);
+               assert(str_out[7] == 5);
+               assert(str_out[8] == 0);
+               assert(str_out[9] == 1);*/
+
+               std::istringstream is(str_out, std::ios_base::binary);
+               std::ostringstream os2(std::ios_base::binary);
+
+               decompress(is, os2, SER_FMT_VER_HIGHEST);
+               std::string str_out2 = os2.str();
+
+               dstream<<"decompress: ";
+               for(u32 i=0; i<str_out2.size(); i++)
+               {
+                       dstream<<(u32)str_out2[i]<<",";
+               }
+               dstream<<std::endl;
+
+               assert(str_out2.size() == fromdata.getSize());
+
+               for(u32 i=0; i<str_out2.size(); i++)
+               {
+                       assert(str_out2[i] == fromdata[i]);
+               }
+
+               }
        }
 };
 
@@ -114,17 +186,157 @@ struct TestMapNode
                MapNode n;
 
                // Default values
-               assert(n.d == MATERIAL_AIR);
-               assert(n.getLight() == 0);
+               assert(n.d == CONTENT_AIR);
+               assert(n.getLight(LIGHTBANK_DAY) == 0);
+               assert(n.getLight(LIGHTBANK_NIGHT) == 0);
                
                // Transparency
-               n.d = MATERIAL_AIR;
+               n.d = CONTENT_AIR;
                assert(n.light_propagates() == true);
                n.d = 0;
                assert(n.light_propagates() == false);
        }
 };
 
+struct TestVoxelManipulator
+{
+       void Run()
+       {
+               /*
+                       VoxelArea
+               */
+
+               VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
+               assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
+               assert(a.index(-1,-1,-1) == 0);
+               
+               VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
+               // An area that is 1 bigger in x+ and z-
+               VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
+               
+               core::list<VoxelArea> aa;
+               d.diff(c, aa);
+               
+               // Correct results
+               core::array<VoxelArea> results;
+               results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
+               results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
+
+               assert(aa.size() == results.size());
+               
+               dstream<<"Result of diff:"<<std::endl;
+               for(core::list<VoxelArea>::Iterator
+                               i = aa.begin(); i != aa.end(); i++)
+               {
+                       i->print(dstream);
+                       dstream<<std::endl;
+                       
+                       s32 j = results.linear_search(*i);
+                       assert(j != -1);
+                       results.erase(j, 1);
+               }
+
+
+               /*
+                       VoxelManipulator
+               */
+               
+               VoxelManipulator v;
+
+               v.print(dstream);
+
+               dstream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
+               
+               v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
+
+               v.print(dstream);
+
+               assert(v.getNode(v3s16(-1,0,-1)).d == 2);
+
+               dstream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
+
+               EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
+
+               v.print(dstream);
+
+               dstream<<"*** Adding area ***"<<std::endl;
+
+               v.addArea(a);
+               
+               v.print(dstream);
+
+               assert(v.getNode(v3s16(-1,0,-1)).d == 2);
+               EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
+
+               /*
+                       Water stuff
+               */
+
+               v.clear();
+
+               const char *content =
+                       "#...######  "
+                       "#...##..##  "
+                       "#........ .."
+                       "############"
+
+                       "#...######  "
+                       "#...##..##  "
+                       "#........#  "
+                       "############"
+               ;
+
+               v3s16 size(12, 4, 2);
+               VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
+               
+               const char *p = content;
+               for(s16 z=0; z<size.Z; z++)
+               for(s16 y=size.Y-1; y>=0; y--)
+               for(s16 x=0; x<size.X; x++)
+               {
+                       MapNode n;
+                       //n.pressure = size.Y - y;
+                       if(*p == '#')
+                               n.d = CONTENT_STONE;
+                       else if(*p == '.')
+                               n.d = CONTENT_WATER;
+                       else if(*p == ' ')
+                               n.d = CONTENT_AIR;
+                       else
+                               assert(0);
+                       v.setNode(v3s16(x,y,z), n);
+                       p++;
+               }
+
+               v.print(dstream, VOXELPRINT_WATERPRESSURE);
+               
+               core::map<v3s16, u8> active_nodes;
+               v.updateAreaWaterPressure(area, active_nodes);
+
+               v.print(dstream, VOXELPRINT_WATERPRESSURE);
+               
+               //s16 highest_y = -32768;
+               /*
+                       NOTE: These are commented out because this behaviour is changed
+                             all the time
+               */
+               //assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
+               //assert(highest_y == 3);
+               /*assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == 3);
+               //assert(highest_y == 3);*/
+               
+               active_nodes.clear();
+               active_nodes[v3s16(9,1,0)] = 1;
+               //v.flowWater(active_nodes, 0, true, 1000);
+               v.flowWater(active_nodes, 0, false, 1000);
+               
+               dstream<<"Final result of flowWater:"<<std::endl;
+               v.print(dstream, VOXELPRINT_WATERPRESSURE);
+               
+               //assert(0);
+       }
+};
+
 struct TestMapBlock
 {
        class TC : public NodeContainer
@@ -211,13 +423,15 @@ struct TestMapBlock
                assert(b.getChangedFlag() == false);
 
                // All nodes should have been set to
-               // .d=MATERIAL_AIR and .getLight() = 0
+               // .d=CONTENT_AIR and .getLight() = 0
                for(u16 z=0; z<MAP_BLOCKSIZE; z++)
-                       for(u16 y=0; y<MAP_BLOCKSIZE; y++)
-                               for(u16 x=0; x<MAP_BLOCKSIZE; x++){
-                                       assert(b.getNode(v3s16(x,y,z)).d == MATERIAL_AIR);
-                                       assert(b.getNode(v3s16(x,y,z)).getLight() == 0);
-                               }
+               for(u16 y=0; y<MAP_BLOCKSIZE; y++)
+               for(u16 x=0; x<MAP_BLOCKSIZE; x++)
+               {
+                       assert(b.getNode(v3s16(x,y,z)).d == CONTENT_AIR);
+                       assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
+                       assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
+               }
                
                /*
                        Parent fetch functions
@@ -231,7 +445,7 @@ struct TestMapBlock
                assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
                assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
                n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
-               assert(n.d == MATERIAL_AIR);
+               assert(n.d == CONTENT_AIR);
 
                // ...but outside the block they should be invalid
                assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
@@ -266,8 +480,9 @@ struct TestMapBlock
                n.d = 4;
                b.setNode(p, n);
                assert(b.getNode(p).d == 4);
-               assert(b.getNodeMaterial(p) == 4);
-               assert(b.getNodeMaterial(v3s16(-1,-1,0)) == 5);
+               //TODO: Update to new system
+               /*assert(b.getNodeTile(p) == 4);
+               assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
                
                /*
                        propagateSunlight()
@@ -277,7 +492,8 @@ struct TestMapBlock
                        for(u16 y=0; y<MAP_BLOCKSIZE; y++){
                                for(u16 x=0; x<MAP_BLOCKSIZE; x++){
                                        MapNode n = b.getNode(v3s16(x,y,z));
-                                       n.setLight(0);
+                                       n.setLight(LIGHTBANK_DAY, 0);
+                                       n.setLight(LIGHTBANK_NIGHT, 0);
                                        b.setNode(v3s16(x,y,z), n);
                                }
                        }
@@ -288,22 +504,26 @@ struct TestMapBlock
                        */
                        parent.position_valid = true;
                        b.setIsUnderground(false);
-                       parent.node.d = MATERIAL_AIR;
-                       parent.node.setLight(LIGHT_SUN);
+                       parent.node.d = CONTENT_AIR;
+                       parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
+                       parent.node.setLight(LIGHTBANK_NIGHT, 0);
                        core::map<v3s16, bool> light_sources;
                        // The bottom block is invalid, because we have a shadowing node
                        assert(b.propagateSunlight(light_sources) == false);
-                       assert(b.getNode(v3s16(1,4,0)).getLight() == LIGHT_SUN);
-                       assert(b.getNode(v3s16(1,3,0)).getLight() == LIGHT_SUN);
-                       assert(b.getNode(v3s16(1,2,0)).getLight() == 0);
-                       assert(b.getNode(v3s16(1,1,0)).getLight() == 0);
-                       assert(b.getNode(v3s16(1,0,0)).getLight() == 0);
-                       assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
-                       assert(b.getFaceLight(p, v3s16(0,1,0)) == LIGHT_SUN);
-                       assert(b.getFaceLight(p, v3s16(0,-1,0)) == 0);
+                       assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
+                       assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
+                       assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
+                       assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
+                       assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
+                       assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
+                       assert(b.getFaceLight(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
+                       assert(b.getFaceLight(1000, p, v3s16(0,-1,0)) == 0);
+                       assert(b.getFaceLight(0, p, v3s16(0,-1,0)) == 0);
                        // According to MapBlock::getFaceLight,
                        // The face on the z+ side should have double-diminished light
-                       assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
+                       //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
+                       // The face on the z+ side should have diminished light
+                       assert(b.getFaceLight(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
                }
                /*
                        Check how the block handles being in between blocks with some non-sunlight
@@ -313,15 +533,15 @@ struct TestMapBlock
                        // Make neighbours to exist and set some non-sunlight to them
                        parent.position_valid = true;
                        b.setIsUnderground(true);
-                       parent.node.setLight(LIGHT_MAX/2);
+                       parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
                        core::map<v3s16, bool> light_sources;
                        // The block below should be valid because there shouldn't be
                        // sunlight in there either
-                       assert(b.propagateSunlight(light_sources) == true);
+                       assert(b.propagateSunlight(light_sources, true) == true);
                        // Should not touch nodes that are not affected (that is, all of them)
                        //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
                        // Should set light of non-sunlighted blocks to 0.
-                       assert(b.getNode(v3s16(1,2,3)).getLight() == 0);
+                       assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
                }
                /*
                        Set up a situation where:
@@ -339,8 +559,8 @@ struct TestMapBlock
                                for(u16 y=0; y<MAP_BLOCKSIZE; y++){
                                        for(u16 x=0; x<MAP_BLOCKSIZE; x++){
                                                MapNode n;
-                                               n.d = MATERIAL_AIR;
-                                               n.setLight(0);
+                                               n.d = CONTENT_AIR;
+                                               n.setLight(LIGHTBANK_DAY, 0);
                                                b.setNode(v3s16(x,y,z), n);
                                        }
                                }
@@ -354,7 +574,7 @@ struct TestMapBlock
                                parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
                        }
                        // Lighting value for the valid nodes
-                       parent.node.setLight(LIGHT_MAX/2);
+                       parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
                        core::map<v3s16, bool> light_sources;
                        // Bottom block is not valid
                        assert(b.propagateSunlight(light_sources) == false);
@@ -475,10 +695,12 @@ struct TestHeightmap
        {
                //g_heightmap_debugprint = true;
                const s16 BS1 = 4;
-               UnlimitedHeightmap hm1(BS1,
+               /*UnlimitedHeightmap hm1(BS1,
                                new ConstantGenerator(0.0),
                                new ConstantGenerator(0.0),
-                               new ConstantGenerator(5.0));
+                               new ConstantGenerator(5.0));*/
+               PointAttributeDatabase padb;
+               UnlimitedHeightmap hm1(BS1, &padb);
                // Go through it so it generates itself
                for(s16 y=0; y<=BS1; y++){
                        for(s16 x=0; x<=BS1; x++){
@@ -503,16 +725,32 @@ struct TestHeightmap
        void Random()
        {
                dstream<<"Running random code (get a human to check this)"<<std::endl;
-               dstream<<"rand() values: ";
+               dstream<<"myrand() values: ";
                for(u16 i=0; i<5; i++)
-                       dstream<<(u16)rand()<<" ";
+                       dstream<<(u16)myrand()<<" ";
                dstream<<std::endl;
 
                const s16 BS1 = 8;
-               UnlimitedHeightmap hm1(BS1,
+               /*UnlimitedHeightmap hm1(BS1,
                                new ConstantGenerator(10.0),
                                new ConstantGenerator(0.3),
-                               new ConstantGenerator(0.0));
+                               new ConstantGenerator(0.0));*/
+
+               PointAttributeDatabase padb;
+
+               padb.getList("hm_baseheight")->addPoint(v2s16(-BS1,0), Attribute(0));
+               padb.getList("hm_randmax")->addPoint(v2s16(-BS1,0), Attribute(0));
+               padb.getList("hm_randfactor")->addPoint(v2s16(-BS1,0), Attribute(0.0));
+
+               padb.getList("hm_baseheight")->addPoint(v2s16(0,0), Attribute(-20));
+               padb.getList("hm_randmax")->addPoint(v2s16(0,0), Attribute(0));
+               padb.getList("hm_randfactor")->addPoint(v2s16(0,0), Attribute(0.5));
+
+               padb.getList("hm_baseheight")->addPoint(v2s16(BS1*2,BS1), Attribute(0));
+               padb.getList("hm_randmax")->addPoint(v2s16(BS1*2,BS1), Attribute(30));
+               padb.getList("hm_randfactor")->addPoint(v2s16(BS1*2,BS1), Attribute(0.63));
+
+               UnlimitedHeightmap hm1(BS1, &padb);
 
                // Force hm1 to generate a some heightmap
                hm1.getGroundHeight(v2s16(0,0));
@@ -850,8 +1088,8 @@ struct TestConnection
 
                        dstream<<"Sending data (size="<<1100<<"):";
                        for(int i=0; i<1100 && i<20; i++){
-                               if(i%2==0) printf(" ");
-                               printf("%.2X", ((int)((const char*)*data1)[i])&0xff);
+                               if(i%2==0) DEBUGPRINT(" ");
+                               DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
                        }
                        if(1100>20)
                                dstream<<"...";
@@ -871,8 +1109,8 @@ struct TestConnection
 
                        dstream<<"Received data (size="<<size<<"):";
                        for(int i=0; i<size && i<20; i++){
-                               if(i%2==0) printf(" ");
-                               printf("%.2X", ((int)((const char*)recvdata)[i])&0xff);
+                               if(i%2==0) DEBUGPRINT(" ");
+                               DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
                        }
                        if(size>20)
                                dstream<<"...";
@@ -906,6 +1144,7 @@ void run_tests()
        TEST(TestUtilities);
        TEST(TestCompress);
        TEST(TestMapNode);
+       TEST(TestVoxelManipulator);
        TEST(TestMapBlock);
        TEST(TestMapSector);
        TEST(TestHeightmap);