Exemple #1
0
 public RenderForm(CompletedSimulation demoResult, IAiFactory demoAi)
 {
     DemoResult      = demoResult ?? throw new ArgumentNullException(nameof(demoResult));
     DemoAi          = demoAi ?? throw new ArgumentNullException(nameof(demoAi));
     OutputDirectory = Path.Combine(Directory.GetCurrentDirectory(), DemoAi.Name);
     InitializeComponent();
     RenderControl.VisibleState = VisibleState.GenerateBlank(DemoResult.Parameters);
 }
Exemple #2
0
        private void BeginRender_Click(object sender, EventArgs e)
        {
            IAi ai = DemoAi.Create(DemoResult.Parameters);
            Level level = DemoResult.ProtoLevel.Generate();

            _rover = new Rover(level, DemoResult.Parameters);
            _stats = RoverStats.Create(DemoResult.Parameters);
            _state = VisibleState.GenerateBlank(level.BottomRight, _rover.Position);
            _actionEnumerator = ai.Simulate(_rover.Accessor).GetEnumerator();
            beginRender.Enabled = false;
            UpdateTimer.Start();
        }
Exemple #3
0
        private static void ExportFrames(CompletedSimulation sim, IAiFactory aiFactory, String outputDir)
        {
            const Int32 tileSize = 10;

            IAi          ai    = aiFactory.Create(sim.Parameters);
            Level        level = sim.ProtoLevel.Generate();
            Rover        rover = new Rover(level, sim.Parameters);
            VisibleState state = VisibleState.GenerateBlank(level.BottomRight, rover.Position);

            using var actionEnumerator = ai.Simulate(rover.Accessor).GetEnumerator();

            Int32 maxFrameCount  = sim.Parameters.InitialMovesLeft;
            Int32 filenameDigits = (Int32)Math.Ceiling(Math.Log10(maxFrameCount)) + 1;

            String        fileBase = Path.Combine(outputDir, $"frame-{sim.ProtoLevel.Seed}-");
            DirectoryInfo dir      = new DirectoryInfo(outputDir);

            if (dir.Exists)
            {
                foreach (var file in dir.EnumerateFiles())
                {
                    file.Delete();
                }
            }
            else
            {
                dir.Create();
            }

            Int32 width  = level.Width * tileSize;
            Int32 height = level.Height * tileSize;

            using Bitmap bitmap    = new Bitmap(width, height);
            using Graphics surface = Graphics.FromImage(bitmap);

            Update update     = new Update(terrain: rover.Adjacent);
            Int32  frameIndex = 0;

            do
            {
                if (!state.Apply(update))
                {
                    continue;
                }

                GdiRenderer.Draw(surface, width, height, state);
                String suffix   = frameIndex.ToString().PadLeft(filenameDigits, '0');
                String filename = fileBase + suffix + ".png";
                bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
                frameIndex++;
            }while (actionEnumerator.MoveNext() && rover.Perform(actionEnumerator.Current, out update));
        }
Exemple #4
0
        public RenderViewModel(IAiFactory aiFactory, CompletedSimulation simulation)
        {
            Ai         = aiFactory ?? throw new ArgumentNullException(nameof(aiFactory));
            Simulation = simulation ?? throw new ArgumentNullException(nameof(simulation));

            Start = ReactiveCommand.CreateFromObservable(() =>
            {
                IAi ai      = Ai.Create(Simulation.Parameters);
                Level level = Simulation.ProtoLevel.Generate();
                Rover rover = new Rover(level, Simulation.Parameters);

                return(Observable.Create <(RoverAction action, Update update)>(obs =>
                {
                    return Task.Run(async() =>
                    {
                        var actionEnumerator = ai.Simulate(rover.Accessor).GetEnumerator();
                        while (actionEnumerator.MoveNext() && rover.Perform(actionEnumerator.Current, out Update update))
                        {
                            obs.OnNext((actionEnumerator.Current, update));
                            Int32 delay = actionEnumerator.Current.Instruction switch
                            {
                                Instruction.Move => 75,
                                Instruction.CollectSample => 50,
                                _ => 0
                            };
                            if (delay != 0)
                            {
                                await Task.Delay(delay);
                            }
                        }
                    });
                }));
            });

            Stats = Start.Aggregate(RoverStats.Create(Simulation.Parameters), (s, update) => s.Add(update.action, update.update));

            State = this
                    .WhenAnyObservable(m => m.Start)
                    .Select(update => update.update)
                    .Scan(VisibleState.GenerateBlank(Simulation.Parameters), (state, update) =>
            {
                state.Apply(update);
                return(state);
            });
        }