Ejemplo n.º 1
0
 public override void ToggleVoxel(Voxel targetVoxel)
 {
     MatterSystem.ExecuteCommand(1, new CommandFill(MatterSystem.Bots[1].Position.Offset(targetVoxel)));
     Octree.Remove(targetVoxel);
 }
Ejemplo n.º 2
0
        public void Solve(string outputFile)
        {
            if (outputFile != null)
            {
                Name = Path.GetFileNameWithoutExtension(outputFile);
            }

            // get all the target voxels
            RemainingVoxels = new HashSet <Voxel>(MatterSystem.Matrix.Storage.Where(x => x.Filled != x.Target));

            var lastCompletePercent = 0;
            var startCount          = RemainingVoxels.Count;

            Log.Info($"{Name} Total targets: {RemainingVoxels.Count:N0}");

            while (RemainingVoxels.Count > 0)
            {
                var(targetVoxel, moveTarget) = ChooseNextTarget();

                // move bot to a nearby if we're not there
                if (MatterSystem.Bots[1].Position != moveTarget)
                {
                    var route = AstarMatrixPather.GetRouteTo(MatterSystem.Bots[1].Position, moveTarget);

                    MatterSystem.ExecuteCommands(CommandOptimizer.Compress(route));
                }

                // fill/void it
                ToggleVoxel(targetVoxel);

                // remove it from targets
                RemainingVoxels.Remove(targetVoxel);

                var complete = (int)(100 - ((float)RemainingVoxels.Count / startCount * 100));
                if (complete != lastCompletePercent)
                {
                    lastCompletePercent = complete;
                    Log.Info($"{Name} {complete:N0}% complete ({RemainingVoxels.Count:N0} remaining)");
                }
            }

            // move to home
            var homeRoute = AstarMatrixPather.GetRouteTo(MatterSystem.Bots[1].Position, MatterSystem.Matrix.Get(0, 0, 0));

            if (homeRoute == null)
            {
                TraceFile.WriteTraceFile(outputFile + ".failed", MatterSystem.Trace);
                Log.Error($"{Path.GetFileNameWithoutExtension(outputFile)} Failed with {RemainingVoxels.Count:N0} targets left");
                return;
            }

            MatterSystem.ExecuteCommands(CommandOptimizer.Compress(homeRoute));

            if (outputFile != null)
            {
                // halt
                MatterSystem.ExecuteCommand(1, new CommandHalt());

                // save trace file
                TraceFile.WriteTraceFile(outputFile, MatterSystem.Trace);
                Log.Debug($"{Path.GetFileNameWithoutExtension(outputFile)} Finished solution ");
            }
        }