コード例 #1
0
ファイル: TestNailsMap.cs プロジェクト: 1upD/Nails
        public void TestGetTwoCubes_OtherLocation()
        {
            // Setup: Create a NailsMap
            NailsMap map = new NailsMap();

            // Mark a location
            map.MarkLocation("TestStyle", 0, 0, 0);
            map.MarkLocation("TestStyle", 0, 1, 0);

            // If there is no cube at the location
            NailsCube cube = map.GetCube(0, -1, 0);

            Assert.IsNull(cube);

            cube = map.GetCube(1, 0, 0);
            Assert.IsNull(cube);

            cube = map.GetCube(-1, 0, 0);
            Assert.IsNull(cube);

            cube = map.GetCube(0, -1, 0);
            Assert.IsNull(cube);

            cube = map.GetCube(0, 0, -1);
            Assert.IsNull(cube);

            cube = map.GetCube(0, 0, 1);
            Assert.IsNull(cube);
        }
コード例 #2
0
        public void TestVMFImportExport()
        {
            // Create a new JSON adapter
            VMFAdapter adapter = new VMFAdapter("testinput.vmf", 64, 64);

            // Setup: Create a NailsMap
            NailsMap map = adapter.Import();

            // Export the map
            adapter.Export(map);

            // Import the map
            var newMap = adapter.Import();

            // Compare
            Assert.IsNotNull(newMap);
            NailsCube cube = newMap.GetCube(0, 0, 0);

            Assert.IsNotNull(cube);
            Assert.AreEqual("TestStyle", cube.StyleName);
            cube = newMap.GetCube(0, 1, 0);
            Assert.IsNotNull(cube);
            Assert.AreEqual("TestStyle", cube.StyleName);
            cube = newMap.GetCube(1, 1, 0);
            Assert.IsNotNull(cube);
            Assert.AreEqual("TestStyle", cube.StyleName);
        }
コード例 #3
0
ファイル: JSONAdapter.cs プロジェクト: 1upD/Nails
 /**
  * Exports the given NailsMap to a JSON file. Uses the file name passed in when this adapter was constructed.
  * <author>1upD</author>
  */
 public void Export(NailsMap map)
 {
     using (TextWriter w = new StreamWriter(new FileStream(this._filename, FileMode.OpenOrCreate)))
         using (JsonWriter writer = new JsonTextWriter(w))
         {
             this._serializer.Serialize(writer, map);
         }
 }
コード例 #4
0
ファイル: TestNailsMap.cs プロジェクト: 1upD/Nails
        public void TestGetCube_NothingAtLocation()
        {
            // Setup: Create a NailsMap
            NailsMap map = new NailsMap();

            // If there is no cube at the location
            NailsCube cube = map.GetCube(0, 0, 0);

            Assert.IsNull(cube);
        }
コード例 #5
0
ファイル: TestNailsMap.cs プロジェクト: 1upD/Nails
        public void TestGetCube()
        {
            // Setup: Create a NailsMap
            NailsMap map = new NailsMap();

            // Mark a location
            map.MarkLocation("TestStyle", 0, 0, 0);

            // If there is no cube at the location
            NailsCube cube = map.GetCube(0, 0, 0);

            Assert.IsNotNull(cube);
            Assert.AreEqual(cube.StyleName, "TestStyle");
        }
コード例 #6
0
ファイル: TestNailsMap.cs プロジェクト: 1upD/Nails
        public void TestCubeMarkedMultipleTimes()
        {
            // Setup: Create a NailsMap
            NailsMap map = new NailsMap();

            // Mark a location
            map.MarkLocation("TestStyle", 0, 0, 0);
            map.MarkLocation("TestStyle", 0, 0, 0);

            // Get the cube
            NailsCube cube = map.GetCube(0, 0, 0);

            // Should act as though there is only one cube
            Assert.IsNotNull(cube);
            Assert.AreEqual(cube.StyleName, "TestStyle");
        }
コード例 #7
0
ファイル: TestNailsMap.cs プロジェクト: 1upD/Nails
        public void TestFacesTwoCubes_FrontBack()
        {
            // Setup: Create a NailsMap
            NailsMap map = new NailsMap();

            // Mark a location
            map.MarkLocation("TestStyle", 0, 0, 0);
            map.MarkLocation("TestStyle", 1, 0, 0);

            // If there is no cube at the location
            NailsCube cube = map.GetCube(0, 0, 0);

            Assert.IsNotNull(cube);
            Assert.AreEqual(cube.StyleName, "TestStyle");

            // Check the second cube
            cube = map.GetCube(1, 0, 0);

            // Look at faces
            var faces = cube.GetFaces();

            Assert.IsTrue(faces.Contains(NailsCubeFace.Floor));
            Assert.IsTrue(faces.Contains(NailsCubeFace.Front));
            Assert.IsTrue(faces.Contains(NailsCubeFace.Left));
            Assert.IsFalse(faces.Contains(NailsCubeFace.Back));
            Assert.IsTrue(faces.Contains(NailsCubeFace.Right));
            Assert.IsTrue(faces.Contains(NailsCubeFace.Ceiling));

            // Check the first cube
            cube = map.GetCube(0, 0, 0);

            // Look at faces
            faces = cube.GetFaces();
            Assert.IsTrue(faces.Contains(NailsCubeFace.Floor));
            Assert.IsFalse(faces.Contains(NailsCubeFace.Front));
            Assert.IsTrue(faces.Contains(NailsCubeFace.Left));
            Assert.IsTrue(faces.Contains(NailsCubeFace.Back));
            Assert.IsTrue(faces.Contains(NailsCubeFace.Right));
            Assert.IsTrue(faces.Contains(NailsCubeFace.Ceiling));
        }
コード例 #8
0
        /**
         * Main method given command line options.
         * <author>1upD</author>
         */
        private static void RunOptionsAndReturnExitCode(Options opts)
        {
            try
            {
                // Read agents from XML file
                NailsConfig config = NailsConfig.ReadConfiguration(opts.InputFileName);

                // Create a new nails map
                NailsMap nailsMap = new NailsMap();
                AlifeMap alifeMap = new NailsAlifeMap(nailsMap);
                alifeMap.Agents = config.Agents;

                // Run the simulation
                AlifeSimulation.Simulate(ref alifeMap, opts.Lifetime);

                // Write out to a file
                VMFAdapter vmfAdapter = new VMFAdapter(filename: opts.OutputFileName, horizontal_scale: opts.HorizontalScale, vertical_scale: opts.VerticalScale, config: config);
                vmfAdapter.Export(nailsMap);
            } catch (Exception e)
            {
                log.Fatal("NailsCmd caught fatal exception: ", e);
            }
        }
コード例 #9
0
ファイル: JSONAdapterTest.cs プロジェクト: 1upD/Nails
        public void TestImportExport()
        {
            // Setup: Create a NailsMap
            NailsMap map = new NailsMap();

            // Mark a location
            map.MarkLocation("TestStyle", 0, 1, 0);

            // Create a new JSON adapter
            JSONAdapter adapter = new JSONAdapter("testoutput.json");

            // Export the map
            adapter.Export(map);

            // Import the map
            var newMap = adapter.Import();

            // Compare
            Assert.IsNotNull(newMap);
            NailsCube cube = newMap.GetCube(0, 1, 0);

            Assert.IsNotNull(cube);
            Assert.AreEqual("TestStyle", cube.StyleName);
        }
コード例 #10
0
ファイル: VMFAdapter.cs プロジェクト: 1upD/Nails
        /**
         * Exports the given NailsMap to a VMF file. Uses the file name passed in when this adapter was constructed.
         * <author>1upD</author>
         */
        public void Export(NailsMap map)
        {
            try
            {
                log.Info(string.Format("Exporting Nails map data to VMF file: {0}", this._filename));

                // Make a deep copy of the stored VMF to add instances to
                VMF output_vmf = new VMFParser.VMF(this._vmf.ToVMFStrings());
                int i          = 0;

                foreach (NailsCube cube in map.NailsCubes)
                {
                    int x_pos = cube.X * this._horizontal_scale;
                    int y_pos = cube.Y * this._horizontal_scale;
                    int z_pos = cube.Z * this._vertical_scale;

                    var faces = cube.GetFaces();

                    if (faces.Contains(NailsCubeFace.Floor))
                    {
                        instanceData instance = this.MakeInstance("floor", cube.StyleName, 0, i++, x_pos, y_pos, z_pos);
                        insertInstanceIntoVMF(instance, output_vmf);
                    }

                    if (faces.Contains(NailsCubeFace.Front))
                    {
                        instanceData instance = this.MakeInstance("wall", cube.StyleName, 0, i++, x_pos, y_pos, z_pos);
                        insertInstanceIntoVMF(instance, output_vmf);
                    }

                    if (faces.Contains(NailsCubeFace.Left))
                    {
                        instanceData instance = this.MakeInstance("wall", cube.StyleName, 90, i++, x_pos, y_pos, z_pos);
                        insertInstanceIntoVMF(instance, output_vmf);
                    }

                    if (faces.Contains(NailsCubeFace.Back))
                    {
                        instanceData instance = this.MakeInstance("wall", cube.StyleName, 180, i++, x_pos, y_pos, z_pos);
                        insertInstanceIntoVMF(instance, output_vmf);
                    }

                    if (faces.Contains(NailsCubeFace.Right))
                    {
                        instanceData instance = this.MakeInstance("wall", cube.StyleName, 270, i++, x_pos, y_pos, z_pos);
                        insertInstanceIntoVMF(instance, output_vmf);
                    }

                    if (faces.Contains(NailsCubeFace.Ceiling))
                    {
                        instanceData instance = this.MakeInstance("ceiling", cube.StyleName, 0, i++, x_pos, y_pos, z_pos);
                        insertInstanceIntoVMF(instance, output_vmf);
                    }
                }


                using (StreamWriter sw = new StreamWriter(new FileStream(this._filename, FileMode.Create)))
                {
                    var vmfStrings = output_vmf.ToVMFStrings();
                    foreach (string line in vmfStrings)
                    {
                        sw.WriteLine(line);
                    }
                }

                log.Info(string.Format("Wrote to VMF file: {0}", this._filename));
            } catch (Exception e)
            {
                log.Error("VMFAdapter.Export(): Caught exception: ", e);
                log.Info(string.Format("Failed to write to VMF file: {0}", this._filename));
            }
        }
コード例 #11
0
ファイル: VMFAdapter.cs プロジェクト: 1upD/Nails
        /**
         * Imports a VMF file to create a NailsMap. Uses the file name passed in when this adapter was constructed.
         * <author>1upD</author>
         * TODO Add more error checking
         */
        public NailsMap Import()
        {
            try {
                // Reset adapter VMF
                this._vmf = new VMF();


                NailsMap map = new NailsMap();

                List <string> lines = new List <string>();

                using (StreamReader sr = new StreamReader(new FileStream(this._filename, FileMode.OpenOrCreate)))
                {
                    while (!sr.EndOfStream)
                    {
                        lines.Add(sr.ReadLine());
                    }
                }


                VMF input_vmf = new VMF(lines.ToArray());

                for (int blockIndex = 0; blockIndex < input_vmf.Body.Count; blockIndex++)
                {
                    // Should this object be included when the VMF is output?
                    bool includeThisBlock = true;
                    // Get the next object from the VMF
                    var obj = input_vmf.Body[blockIndex];

                    try
                    {
                        // Try to cast to block
                        VMFParser.VBlock block = (VMFParser.VBlock)obj;


                        // If this block is an entity
                        if (block.Name == "entity")
                        {
                            var body = block.Body;
                            foreach (var innerObj in body)
                            {
                                try
                                {
                                    VMFParser.VProperty prop = (VMFParser.VProperty)innerObj;

                                    // If this block is an instance
                                    if (prop.Name == "classname" && prop.Value == "func_instance")
                                    {
                                        VProperty fileProperty  = (VProperty)body.Where(p => p.Name == "file").ToList()[0];
                                        var       filePathParts = fileProperty.Value.Split('/');

                                        // If this is a nails instance
                                        if (filePathParts[0] == "Nails")
                                        {
                                            // Get position
                                            VProperty originProperty = (VProperty)body.Where(p => p.Name == "origin").ToList()[0];
                                            var       originParts    = originProperty.Value.Split(' ');
                                            int       x_pos          = int.Parse(originParts[0]) / this._horizontal_scale;
                                            int       y_pos          = int.Parse(originParts[1]) / this._horizontal_scale;
                                            int       z_pos          = int.Parse(originParts[2]) / this._vertical_scale;
                                            string    style          = filePathParts[1];
                                            map.MarkLocation(style, x_pos, y_pos, z_pos);

                                            // Remove this block from the vmf
                                            includeThisBlock = false;
                                        }

                                        break;
                                    }
                                } catch (InvalidCastException e)
                                {
                                    log.Error("Invalid cast exception. VMF Object is not a VProperty.", e);
                                }
                            }
                        }
                    } catch (InvalidCastException e)
                    {
                        log.Error("Invalid cast exception. VMF object is not a VBlock.", e);
                    }
                    // If this object is not a Nails block, add it to the adapter's VMF to be output later
                    if (includeThisBlock)
                    {
                        this._vmf.Body.Add(obj);
                    }
                }

                return(map);
            } catch (Exception e)
            {
                log.Error("VMFAdapter.Import(): Caught exception: ", e);
                log.Info(string.Format("Failed to read from VMF file: {0}", this._filename));
            }

            return(null);
        }
コード例 #12
0
ファイル: NailsAlifeMap.cs プロジェクト: 1upD/Nails
 public NailsAlifeMap(NailsMap map)
 {
     this.Agents = new List <BaseAgent>();
     this.map    = map;
 }
コード例 #13
0
ファイル: NailsAlifeMap.cs プロジェクト: 1upD/Nails
 public NailsAlifeMap()
 {
     this.Agents = new List <BaseAgent>();
     map         = new NailsMap();
 }