示例#1
0
 private void _realtimeSimulation_OnCollision(RealtimeSimulation sender, Cell cell1, Cell cell2)
 {
     propertiesChanged();
     if (OnCollision != null)
     {
         OnCollision(sender, cell1, cell2);
     }
 }
示例#2
0
 private void _simulation_OnObjectAdded(RealtimeSimulation sender, Cell cell)
 {
     //Dispatcher.BeginInvoke(new Action(() =>
     //{
     //    fillUniverseProperties(_simulation);
     //    fillCellsProperties(_simulation);
     //}));
 }
示例#3
0
 private void _simulation_OnCollision(RealtimeSimulation sender, Cell cell1, Cell cell2)
 {
     Dispatcher.BeginInvoke(new Action(() =>
     {
         fillUniverseProperties(_simulation);
         fillCellsProperties(_simulation);
     }));
 }
        /// <summary>
        /// Generates Random Simulation
        /// </summary>
        /// <param name="stopWhenCompleted">Stop Simulation when a critical mass reached by any cell or all smart cells has died)</param>
        /// <param name="totalCycle">Stops when Total Cycle Hits (0 for infinite)</param>
        /// <param name="width">Observation Are Widht</param>
        /// <param name="height">Observation Are Height</param>
        /// <param name="dummyCellCount">dummy Cell Count</param>
        /// <param name="maxVX">Max Horizontal Velocity for Dummy</param>
        /// <param name="maxVY">Max Vertical Velocity for Dummy</param>
        /// <param name="maxRadius">Max Radius for Dummy</param>
        /// <param name="minRadius">Min Radius for Dummy</param>
        /// <param name="smartCellCount">smart CellC ount</param>
        /// <param name="SmaxVX">Max Horizontal Velocity for Smart</param>
        /// <param name="SmaxVY">Max Vertical Velocity for Smart</param>
        /// <param name="SmaxRadius">Max Radius for Smart</param>
        /// <param name="SminRadius">Min Radius for Smart</param>
        /// <returns></returns>
        public static RealtimeSimulation GenerateSimulation(
            bool stopWhenCompleted, int totalCycle,
            double width, double height,
            int dummyCellCount, double maxVX, double maxVY, double maxRadius, double minRadius,
            int smartCellCount, double SmaxVX, double SmaxVY, double SmaxRadius, double SminRadius)
        {
            var s = new RealtimeSimulation();

            s.Boundry           = new Rect(0, 0, width, height);
            s.TotalCycle        = totalCycle;
            s.StopWhenCompleted = stopWhenCompleted;
            s.AddRandomSmartCells(smartCellCount, new Vector2D(SmaxVX, SmaxVY), SmaxRadius, SminRadius);
            s.AddRandomDummyCells(dummyCellCount, new Vector2D(maxVX, maxVY), maxRadius, minRadius);
            return(s);
        }
示例#5
0
        private void sldMemory_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e)
        {
            _cycleIndex = (int)sldMemory.Value;
            if (_memorySimulation == null)
            {
                return;
            }
            if (_cycleIndex < _memorySimulation.Cycles.Count)
            {
                if (_simulation == null)
                {
                    _simulation = new RealtimeSimulation();
                }
                _simulation.Paused           = true;
                _simulation.Cycle            = _cycleIndex;
                _simulation.CancelationToken = true;
                var cycle = _memorySimulation.Cycles[_cycleIndex];
                _simulation.Cells.RemoveAll(x => !cycle.Cells.Contains(x));
                foreach (var cCell in cycle.Cells)
                {
                    var sCell = _simulation.Cells.SingleOrDefault(x => x.Equals(cCell));
                    if (sCell != null)
                    {
                        sCell.Position        = cCell.Position;
                        sCell.Radius          = cCell.Radius;
                        sCell.Mass            = cCell.Mass;
                        sCell.Velocity        = cCell.Velocity;
                        sCell.Accerelation    = cCell.Accerelation;
                        sCell.Texture         = null;
                        sCell.SelectedTexture = null;
                    }
                    else
                    {
                        _simulation.Cells.Add(cCell);
                    }
                }
                ;
                _simulation.CancelationToken = false;

                if (!_memoryPlayTimer.IsEnabled)
                {
                    fillCellsProperties(_simulation);
                    fillUniverseProperties(_simulation);
                }
            }
        }
示例#6
0
 private void fillUniverseProperties(RealtimeSimulation simulation)
 {
     if (simulation == null)
     {
         return;
     }
     txtCycle.Text               = string.Format("Cycle : {0}", simulation.Cycle);
     txtTotalMass.Text           = string.Format("Mass : {0}", simulation.Mass);
     txtTotalEnergy.Text         = string.Format("Energy : {0}", simulation.Energy);
     txtTotalSpeed.Text          = string.Format("Velocity : {0}", simulation.Velocity);
     txtTotalMomentum.Text       = string.Format("Momentum : {0}", simulation.Momentum);
     txtTotalCells.Text          = string.Format("Cells : {0}", simulation.Cells.Count);
     txtTotalDummyCells.Text     = string.Format("Smarts : {0}", simulation.SmartCellCount);
     txtTotalSmartCells.Text     = string.Format("Dummys : {0}", simulation.DummyCellCount);
     txtSmartCellCharacters.Text = "";
     foreach (var c in Enum.GetValues(typeof(SmartCell.CharacterType)).Cast <SmartCell.CharacterType>())
     {
         txtSmartCellCharacters.Text += Enum.GetName(typeof(SmartCell.CharacterType), c) + " : " + simulation.SmartCellCountWithCharacter(c) + "\r\n";
     }
 }
示例#7
0
        private void fillCellsProperties(RealtimeSimulation simulation)
        {
            if (simulation == null)
            {
                return;
            }
            lstCells.ItemsSource = null;
            var filteredCells = simulation.SortedCells(
                (Cell.FilterType)Enum.Parse(
                    typeof(Cell.FilterType),
                    comboFilter.SelectedItem.ToString(),
                    true),
                true).ToList();

            lstCells.ItemsSource = filteredCells;
            if (!filteredCells.Contains(simulation.Selected))
            {
                simulation.Selected = null;
            }
            else
            {
                lstCells.SelectedItem = simulation.Selected;
            }
        }
示例#8
0
        public virtual void MakeDecision(RealtimeSimulation simulation)
        {
            var others               = simulation.Cells.Where(x => x != this);
            var otherSmarts          = others.OfType <SmartCell>();
            var otherDummys          = others.Except(otherSmarts);
            var isAnyBiggerThanMe    = others.Count(x => x.Mass > Mass) > 0;
            var otherBiggest         = others.OrderByDescending(x => x.Mass).FirstOrDefault();
            var otherSmallest        = others.OrderBy(x => x.Mass).FirstOrDefault();;
            var otherSmalls          = others.Where(x => x.Mass <Mass && x.Mass> Mass / minMassDivisor).OrderByDescending(x => x.Mass);
            var biggestOfOtherSmalls = otherSmalls.FirstOrDefault();
            var otherBigs            = others.Where(x => x.Mass > Mass).OrderByDescending(x => x.Mass);
            var nearestBig           = others.Where(x => x.Mass > Mass).OrderBy(x => Center.Subtract(x.Center).Length).FirstOrDefault();
            var nearestSmall         = others.Where(x => x.Mass <Mass && x.Mass> Mass / minMassDivisor)
                                       .OrderBy(x => Center.Subtract(x.Center).Length)
                                       .ThenByDescending(x => x.Mass).FirstOrDefault();
            var slowestOfOtherSmalls = otherSmalls.OrderBy(x => x.Velocity.Length).FirstOrDefault();
            var fastestOfOtherSmalls = otherSmalls.OrderByDescending(x => x.Velocity.Length).FirstOrDefault();
            var biggestMomentum      = otherSmalls.OrderByDescending(x => x.Momentum.Length).FirstOrDefault();

            switch (Character)
            {
            case CharacterType.CatchSmall:
            {
                if (!isAnyBiggerThanMe)
                {
                    return;
                }
                if (biggestOfOtherSmalls == null)
                {
                    return;
                }
                var vector   = biggestOfOtherSmalls.Center - this.Center;
                var uVector1 = vector * -1;
                var uVector2 = biggestOfOtherSmalls.Velocity * -1;
                var uVector  = (uVector1 + uVector2).UnitVector;
                if (uVector.Length == 0)
                {
                    return;
                }
                var dropCell = new Cell()
                {
                    Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                };
                dropCell.GenerateRadiusFromMass();
                if (dropCell.Radius < 1 || Radius < 1)
                {
                    return;
                }
                caculateDropCellPosition(dropCell);
                DropObject(dropCell);
                simulation.AddCell(dropCell);
            }
            break;

            case CharacterType.EvadeBig:
            {
                if (!isAnyBiggerThanMe)
                {
                    return;
                }
                if (nearestBig == null)
                {
                    return;
                }
                var vector   = nearestBig.Position - this.Position;
                var uVector1 = vector * 1;
                var uVector2 = nearestBig.Velocity * 1;
                var uVector  = (uVector1 + uVector2).UnitVector;
                if (uVector.Length == 0)
                {
                    return;
                }
                var dropCell = new Cell()
                {
                    Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                };
                dropCell.GenerateRadiusFromMass();
                if (dropCell.Radius < 1 || Radius < 1)
                {
                    return;
                }
                caculateDropCellPosition(dropCell);
                DropObject(dropCell);
                simulation.AddCell(dropCell);
            }
            break;

            case CharacterType.CatchNearest:
            {
                if (nearestSmall == null)
                {
                    return;
                }
                if (!isAnyBiggerThanMe)
                {
                    return;
                }
                var vector   = nearestSmall.Position - this.Position;
                var uVector1 = vector * -1;
                var uVector2 = nearestSmall.Velocity * -1;
                var uVector  = (uVector1 + uVector2).UnitVector;
                if (uVector.Length == 0)
                {
                    return;
                }
                var dropCell = new Cell()
                {
                    Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                };
                dropCell.GenerateRadiusFromMass();
                if (dropCell.Radius < 1 || Radius < 1)
                {
                    return;
                }
                caculateDropCellPosition(dropCell);
                DropObject(dropCell);
                simulation.AddCell(dropCell);
            }
            break;

            case CharacterType.NoSpeed:
            {
                if (!isAnyBiggerThanMe)
                {
                    return;
                }
                if (Velocity.Length < minVelocityThresold)
                {
                    return;
                }
                var noSpeedMassDivider = 1 / massDivisor;
                var v1       = this.Velocity;
                var v2       = v1 * noSpeedMassDivider;
                var m2       = Mass / noSpeedMassDivider;
                var dropCell = new Cell()
                {
                    Mass = m2, Velocity = v2
                };
                dropCell.GenerateRadiusFromMass();
                if (dropCell.Radius < 1 || Radius < 1)
                {
                    return;
                }
                caculateDropCellPosition(dropCell);
                DropObject(dropCell);
                simulation.AddCell(dropCell);
            }
            break;

            case CharacterType.CatchSmallAndEvadeBig:
            {
                if (subCharacter == CharacterType.CatchSmall)
                {
                    subCharacter = CharacterType.EvadeBig;
                    if (!isAnyBiggerThanMe)
                    {
                        return;
                    }
                    if (biggestOfOtherSmalls == null)
                    {
                        return;
                    }
                    var vector   = biggestOfOtherSmalls.Center - this.Center;
                    var uVector1 = vector * -1;
                    var uVector2 = biggestOfOtherSmalls.Velocity * -1;
                    var uVector  = (uVector1 + uVector2).UnitVector;
                    if (uVector.Length == 0)
                    {
                        return;
                    }
                    var dropCell = new Cell()
                    {
                        Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                    };
                    dropCell.GenerateRadiusFromMass();
                    if (dropCell.Radius < 1 || Radius < 1)
                    {
                        return;
                    }
                    caculateDropCellPosition(dropCell);
                    DropObject(dropCell);
                    simulation.AddCell(dropCell);
                }
                else
                {
                    subCharacter = CharacterType.CatchSmall;
                    if (!isAnyBiggerThanMe)
                    {
                        return;
                    }
                    if (nearestBig == null)
                    {
                        return;
                    }
                    var vector   = nearestBig.Position - this.Position;
                    var uVector1 = vector * 1;
                    var uVector2 = nearestBig.Velocity * 1;
                    var uVector  = (uVector1 + uVector2).UnitVector;
                    if (uVector.Length == 0)
                    {
                        return;
                    }
                    var dropCell = new Cell()
                    {
                        Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                    };
                    dropCell.GenerateRadiusFromMass();
                    if (dropCell.Radius < 1 || Radius < 1)
                    {
                        return;
                    }
                    caculateDropCellPosition(dropCell);
                    DropObject(dropCell);
                    simulation.AddCell(dropCell);
                }
            }
            break;

            case CharacterType.CatchNearestAndEvadeBig:
            {
                if (subCharacter == CharacterType.CatchNearest)
                {
                    subCharacter = CharacterType.EvadeBig;
                    if (nearestSmall == null)
                    {
                        return;
                    }
                    if (!isAnyBiggerThanMe)
                    {
                        return;
                    }
                    var vector   = nearestSmall.Position - this.Position;
                    var uVector1 = vector * -1;
                    var uVector2 = nearestSmall.Velocity * -1;
                    var uVector  = (uVector1 + uVector2).UnitVector;
                    if (uVector.Length == 0)
                    {
                        return;
                    }
                    var dropCell = new Cell()
                    {
                        Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                    };
                    dropCell.GenerateRadiusFromMass();
                    if (dropCell.Radius < 1 || Radius < 1)
                    {
                        return;
                    }
                    caculateDropCellPosition(dropCell);
                    DropObject(dropCell);
                    simulation.AddCell(dropCell);
                }
                else
                {
                    subCharacter = CharacterType.CatchNearest;
                    if (!isAnyBiggerThanMe)
                    {
                        return;
                    }
                    if (nearestBig == null)
                    {
                        return;
                    }
                    var vector   = nearestBig.Position - this.Position;
                    var uVector1 = vector * 1;
                    var uVector2 = nearestBig.Velocity * 1;
                    var uVector  = (uVector1 + uVector2).UnitVector;
                    if (uVector.Length == 0)
                    {
                        return;
                    }
                    var dropCell = new Cell()
                    {
                        Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                    };
                    dropCell.GenerateRadiusFromMass();
                    if (dropCell.Radius < 1 || Radius < 1)
                    {
                        return;
                    }
                    caculateDropCellPosition(dropCell);
                    DropObject(dropCell);
                    simulation.AddCell(dropCell);
                }
            }
            break;

            case CharacterType.CatchSlow:
            {
                if (!isAnyBiggerThanMe)
                {
                    return;
                }
                if (slowestOfOtherSmalls == null)
                {
                    return;
                }
                var vector   = slowestOfOtherSmalls.Position - this.Position;
                var uVector1 = vector * -1;
                var uVector2 = slowestOfOtherSmalls.Velocity * -1;
                var uVector  = (uVector1 + uVector2).UnitVector;
                if (uVector.Length == 0)
                {
                    return;
                }
                var dropCell = new Cell()
                {
                    Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                };
                dropCell.GenerateRadiusFromMass();
                if (dropCell.Radius < 1 || Radius < 1)
                {
                    return;
                }
                caculateDropCellPosition(dropCell);
                DropObject(dropCell);
                simulation.AddCell(dropCell);
            }
            break;

            case CharacterType.CatchFast:
            {
                if (!isAnyBiggerThanMe)
                {
                    return;
                }
                if (fastestOfOtherSmalls == null)
                {
                    return;
                }
                var vector   = fastestOfOtherSmalls.Position - this.Position;
                var uVector1 = vector * -1;
                var uVector2 = fastestOfOtherSmalls.Velocity * -1;
                var uVector  = (uVector1 + uVector2).UnitVector;
                if (uVector.Length == 0)
                {
                    return;
                }
                var dropCell = new Cell()
                {
                    Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                };
                dropCell.GenerateRadiusFromMass();
                if (dropCell.Radius < 1 || Radius < 1)
                {
                    return;
                }
                caculateDropCellPosition(dropCell);
                DropObject(dropCell);
                simulation.AddCell(dropCell);
            }
            break;

            case CharacterType.FollowBig:
            {
                if (!isAnyBiggerThanMe)
                {
                    return;
                }
                if (otherBiggest != null)
                {
                    var vector   = otherBiggest.Position - this.Position;
                    var uVector  = vector.UnitVector * ((vector.Length > otherBiggest.Radius * 2) ? -1 : 1);
                    var dropCell = new Cell()
                    {
                        Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                    };
                    dropCell.GenerateRadiusFromMass();
                    if (dropCell.Radius < 1 || Radius < 1)
                    {
                        return;
                    }
                    caculateDropCellPosition(dropCell);
                    DropObject(dropCell);
                    simulation.AddCell(dropCell);
                }
            }
            break;

            case CharacterType.Matador:
            {
                if (!isAnyBiggerThanMe)
                {
                    return;
                }
                if (nearestBig != null)
                {
                    Point?ip1 = null;
                    if (nearestBig.Velocity.Length > 0)
                    {
                        ip1 = Geometry.IntersectionPoint(nearestBig.Center.Point, Diagonale, nearestBig.Velocity.X, nearestBig.Velocity.Y);
                        if (ip1 == null)
                        {
                            ip1 = Geometry.IntersectionPoint(nearestBig.Center.Point, InverseDiagonale, nearestBig.Velocity.X, nearestBig.Velocity.Y);
                        }
                    }

                    Point?ip2 = null;
                    if (Velocity.Length > 0)
                    {
                        ip2 = Geometry.IntersectionPoint(Center.Point, nearestBig.Diagonale, Velocity.X, Velocity.Y);
                        if (ip2 == null)
                        {
                            ip2 = Geometry.IntersectionPoint(Center.Point, nearestBig.InverseDiagonale, Velocity.X, Velocity.Y);
                        }
                    }

                    if (ip2 == null && ip1 == null)
                    {
                        return;
                    }

                    var uVector = new Vector2D(nearestBig.Velocity.Y, nearestBig.Velocity.X).UnitVector;
                    if (ip1 == null)
                    {
                        uVector = new Vector2D(Velocity.Y, Velocity.X).UnitVector;
                    }

                    var dropCell = new Cell()
                    {
                        Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                    };
                    dropCell.GenerateRadiusFromMass();
                    if (dropCell.Radius < 1 || Radius < 1)
                    {
                        return;
                    }
                    caculateDropCellPosition(dropCell);
                    DropObject(dropCell);
                    simulation.AddCell(dropCell);
                }
            }
            break;

            case CharacterType.CatchMomentum:
            {
                if (!isAnyBiggerThanMe)
                {
                    return;
                }
                if (biggestMomentum == null)
                {
                    return;
                }
                var vector   = biggestMomentum.Position - this.Position;
                var uVector1 = vector * -1;
                var uVector2 = biggestMomentum.Velocity * -1;
                var uVector  = (uVector1 + uVector2).UnitVector;
                if (uVector.Length == 0)
                {
                    return;
                }
                var dropCell = new Cell()
                {
                    Mass = this.Mass * massDivisor, Velocity = uVector * velocityMultiplier
                };
                dropCell.GenerateRadiusFromMass();
                if (dropCell.Radius < 1 || Radius < 1)
                {
                    return;
                }
                caculateDropCellPosition(dropCell);
                DropObject(dropCell);
                simulation.AddCell(dropCell);
            }
            break;

            default:
                break;
            }
        }
示例#9
0
 public Simulation(RealtimeSimulation simulation)
     : this()
 {
     _realtimeSimulation              = simulation;
     _realtimeSimulation.OnCollision += _realtimeSimulation_OnCollision;
 }
示例#10
0
 private void simulation_OnCollision(RealtimeSimulation sender, Cell cell1, Cell cell2)
 {
     propertiesChanged();
 }
示例#11
0
        private void generateDialog_Closed(object sender, EventArgs e)
        {
            var d = sender as GenerateSimulationDialog;

            if (d != null)
            {
                if (d.DialogResult.GetValueOrDefault())
                {
                    switch (d.RunIn)
                    {
                    case GenerateSimulationDialog.RunInEnum.Screen:
                        _simulation = RealtimeSimulation.GenerateSimulation(
                            d.StopWhenCompleted, d.TotalCycle,
                            drawingSurface.ActualWidth, drawingSurface.ActualHeight,
                            d.DummyCellCount, d.MaxVX, d.MaxVY, d.MaxRadius, d.MinRadius,
                            d.SmartCellCount, d.SMaxVX, d.SMaxVY, d.SMaxRadius, d.SMinRadius
                            );
                        _simulation.OnCollision   += _simulation_OnCollision;
                        _simulation.OnObjectAdded += _simulation_OnObjectAdded;
                        fillUniverseProperties(_simulation);
                        fillCellsProperties(_simulation);
                        break;

                    case GenerateSimulationDialog.RunInEnum.Memory:
                        var rSimulation = RealtimeSimulation.GenerateSimulation(
                            d.StopWhenCompleted, d.TotalCycle,
                            drawingSurface.ActualWidth, drawingSurface.ActualHeight,
                            d.DummyCellCount, d.MaxVX, d.MaxVY, d.MaxRadius, d.MinRadius,
                            d.SmartCellCount, d.SMaxVX, d.SMaxVY, d.SMaxRadius, d.SMinRadius
                            );
                        _memorySimulation              = new Simulation(rSimulation);
                        _memorySimulation.OnNextCycle += _memorySimulation_OnNextCycle;
                        _memorySimulation.OnCompleted += _memorySimulation_OnCompleted;
                        _memorySimulation.StartAsync();
                        enableButtons(false);
                        break;

                    case GenerateSimulationDialog.RunInEnum.MemoryBatch:
                        var simulationBatch = new SimulationBatch();
                        for (int i = 0; i < d.TotalBatch; i++)
                        {
                            var rSim = RealtimeSimulation.GenerateSimulation(
                                d.StopWhenCompleted, d.TotalCycle,
                                drawingSurface.ActualWidth, drawingSurface.ActualHeight,
                                d.DummyCellCount, d.MaxVX, d.MaxVY, d.MaxRadius, d.MinRadius,
                                d.SmartCellCount, d.SMaxVX, d.SMaxVY, d.SMaxRadius, d.SMinRadius
                                );
                            var sim = new Simulation(rSim);
                            simulationBatch.Add(sim);
                        }
                        var batchPlayer = new SimulationBatchPlayer(simulationBatch);
                        batchPlayer.Closed += batchPlayer_Closed;
                        batchPlayer.Show();
                        _simulationBatchPlayer = batchPlayer;
                        break;

                    case GenerateSimulationDialog.RunInEnum.Server:
                        break;

                    default:
                        break;
                    }
                }
            }
        }