public AddBotArgs(Point3D position, Vector3D? angularVelocity, ItemAddedHandler itemAdded, BotDNA bot, int level, Point3D homingPoint, double homingRadius, WeaponDNA attachedWeapon = null, WeaponDNA[] inventoryWeapons = null)
     : base(position, angularVelocity, itemAdded)
 {
     this.Bot = bot;
     this.Level = level;
     this.HomingPoint = homingPoint;
     this.HomingRadius = homingRadius;
     this.AttachedWeapon = attachedWeapon;
     this.InventoryWeapons = inventoryWeapons;
 }
Beispiel #2
0
    public void Init()
    {
        // 0 Forward
        // 1 Left
        // 2 Right

        // Here its going to generate a no. from 0-3
        // This no. will make the Bot move accordingly
        bot_DNA  = new BotDNA(DNALength, 3);
        LifeSpan = 0;
        StartPos = transform.position;
        Alive    = true;
    }
Beispiel #3
0
        public (BotDNA bot, WeaponDNA weapon) GetBestBotDNA()
        {
            BotDNA winner = _winningBot;

            if (winner == null)
            {
                return(GetRandomDNA(_itemOptions, _shellColors, this.WeaponDNA));
            }
            else
            {
                return(winner, this.WeaponDNA);
            }
        }
Beispiel #4
0
 public void OffSpringGen(BotDNA Parent1, BotDNA Parent2)
 {
     for (int i = 0; i < DNALength; i++)
     {
         // OffSprings is made in such a way that, it has half of its genes from Parent1 and the other half from the Parent2
         if (i < (DNALength / 2.0f))
         {
             int c = Parent1.genes[i];
             genes[i] = c;
         }
         else
         {
             int c = Parent2.genes[i];
             genes[i] = c;
         }
     }
 }
Beispiel #5
0
 public ArcBotNPC(BotDNA dna, int level, Point3D position, World world, Map map, KeepItems2D keepItems2D, MaterialIDs materialIDs, Viewport3D viewport, EditorOptions editorOptions, ItemOptionsArco itemOptions, IGravityField gravity, DragHitShape dragPlane, Point3D homingPoint, double homingRadius, bool runNeural, bool repairPartPositions)
     : base(dna, level, position, world, map, keepItems2D, materialIDs, viewport, editorOptions, itemOptions, gravity, dragPlane, homingPoint, homingRadius, runNeural, repairPartPositions)
 {
 }
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            const double LIFESPAN = 45;     // seconds
            const double MINSCORE = .5;
            const int LEVEL = 1;

            lock (_lock)
            {
                if (_isDisposed)
                {
                    return;
                }

                foreach (TrackedBot tracked in _trackedBots)
                {
                    switch (tracked.State)
                    {
                        case BotState.Adding:
                        case BotState.Removing:
                            break;

                        case BotState.None:
                            #region None

                            // Create a bot

                            BotDNA dna = null;
                            BotDNA winningBot = _winningBot;
                            if (winningBot == null)
                            {
                                dna = GetRandomDNA(_shellColors).Item1;
                            }
                            else
                            {
                                // Create a mutated copy of the winning design
                                dna = UtilityCore.Clone(winningBot);
                                dna.UniqueID = Guid.NewGuid();
                                dna.Generation++;

                                if (dna.Parts != null)
                                {
                                    MutateUtility.Mutate(dna.Parts, _mutateArgs);
                                }
                            }

                            tracked.State = BotState.Adding;

                            tracked.Lifespan = StaticRandom.NextPercent(LIFESPAN, .1);      // randomizing the lifespan a bit to stagger when bots get killed/created

                            Point3D position = Math3D.GetRandomVector_Circular(HOMINGRADIUS / 4d).ToPoint();
                            _dreamWorld.Add(new OfflineWorld.AddBotArgs(position, null, tracked.BotAdded, dna, LEVEL, new Point3D(0, 0, 0), HOMINGRADIUS, this.WeaponDNA));

                            #endregion
                            break;

                        case BotState.Added:
                            #region Added

                            // See if this should die
                            double age = (DateTime.UtcNow - tracked.Bot.CreationTime).TotalSeconds;
                            if (age > tracked.Lifespan)
                            {
                                FitnessTracker rules = tracked.Rules;
                                tracked.Rules = null;      // set it to null as soon as possible so that the rules tick doesn't do unnecessary work

                                double score = rules.Score / age;

                                if (score > MINSCORE && score > _winningScore)
                                {
                                    BotDNA winningDNA = tracked.Bot.DNAFinal;

                                    if (winningDNA != null)     // it should be non null long before this point, but just make sure
                                    {
                                        _winningScore = score;
                                        _winningBot = winningDNA;
                                    }
                                }

                                // Kill it
                                tracked.State = BotState.Removing;
                                _dreamWorld.Remove(new OfflineWorld.RemoveArgs(tracked.Bot.Token, tracked.BotRemoved));
                            }

                            #endregion
                            break;
                    }

                    _timer.Start();
                }
            }
        }
        public static Tuple<BotDNA, WeaponDNA> GetRandomDNA(BotShellColorsDNA shellColors = null, WeaponDNA weapon = null)
        {
            Random rand = StaticRandom.GetRandomForThread();

            BotDNA bot = new BotDNA()
            {
                UniqueID = Guid.NewGuid(),
                Lineage = Guid.NewGuid().ToString(),
                Generation = 0,
                DraggingMaxVelocity = rand.NextPercent(5, .25),
                DraggingMultiplier = rand.NextPercent(20, .25),
            };

            #region Parts

            List<ShipPartDNA> parts = new List<ShipPartDNA>();

            double partSize;

            // Homing
            partSize = rand.NextPercent(1, .5);
            parts.Add(new ShipPartDNA() { PartType = SensorHoming.PARTTYPE, Position = new Point3D(0, 0, 2.5), Orientation = Quaternion.Identity, Scale = new Vector3D(partSize, partSize, partSize) });

            // Vision
            //TODO: Support filtering by type
            partSize = rand.NextPercent(1, .5);
            parts.Add(new ShipPartDNA() { PartType = SensorVision.PARTTYPE, Position = new Point3D(0, 0, 1.5), Orientation = Quaternion.Identity, Scale = new Vector3D(partSize, partSize, partSize) });

            // Brains
            int numBrains = 1 + Convert.ToInt32(rand.NextPow(5, 1d) * 4);

            for (int cntr = 0; cntr < numBrains; cntr++)
            {
                partSize = rand.NextPercent(1, .5);

                Point3D position = new Point3D(0, 0, 0);
                if (numBrains > 1)
                {
                    position = Math3D.GetRandomVector_Circular(1).ToPoint();
                }

                parts.Add(new ShipPartDNA() { PartType = Brain.PARTTYPE, Position = position, Orientation = Quaternion.Identity, Scale = new Vector3D(partSize, partSize, partSize) });
            }

            // MotionController_Linear - always exactly one of these
            partSize = rand.NextPercent(1, .5);
            parts.Add(new ShipPartDNA() { PartType = MotionController_Linear.PARTTYPE, Position = new Point3D(0, 0, -1.5), Orientation = Quaternion.Identity, Scale = new Vector3D(partSize, partSize, partSize) });

            // Store it
            bot.Parts = parts.ToArray();

            #endregion

            if (shellColors == null)
            {
                bot.ShellColors = BotShellColorsDNA.GetRandomColors();
            }
            else
            {
                bot.ShellColors = shellColors;
            }

            #region Weapon

            WeaponDNA weaponActual = null;
            if (weapon == null)
            {
                if (rand.NextDouble() < .95d)
                {
                    WeaponHandleMaterial[] weaponMaterials = new WeaponHandleMaterial[] { WeaponHandleMaterial.Soft_Wood, WeaponHandleMaterial.Hard_Wood };

                    weaponActual = new WeaponDNA()
                    {
                        UniqueID = Guid.NewGuid(),
                        Handle = WeaponHandleDNA.GetRandomDNA(weaponMaterials[StaticRandom.Next(weaponMaterials.Length)])
                    };
                }
            }
            else
            {
                weaponActual = weapon;
            }

            #endregion

            return Tuple.Create(bot, weaponActual);
        }
Beispiel #8
0
 public ArcBotNPC(BotDNA dna, int level, Point3D position, World world, Map map, KeepItems2D keepItems2D, MaterialIDs materialIDs, Viewport3D viewport, EditorOptions editorOptions, ItemOptionsArco itemOptions, IGravityField gravity, DragHitShape dragPlane, Point3D homingPoint, double homingRadius, bool runNeural, bool repairPartPositions)
     : base(dna, level, position, world, map, keepItems2D, materialIDs, viewport, editorOptions, itemOptions, gravity, dragPlane, homingPoint, homingRadius, runNeural, repairPartPositions) { }
Beispiel #9
0
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            const double LIFESPAN = 45;     // seconds
            const double MINSCORE = .5;
            const int    LEVEL    = 1;

            lock (_lock)
            {
                if (_isDisposed)
                {
                    return;
                }

                foreach (TrackedBot tracked in _trackedBots)
                {
                    switch (tracked.State)
                    {
                    case BotState.Adding:
                    case BotState.Removing:
                        break;

                    case BotState.None:
                        #region None

                        // Create a bot

                        BotDNA dna        = null;
                        BotDNA winningBot = _winningBot;
                        if (winningBot == null)
                        {
                            dna = GetRandomDNA(_itemOptions, _shellColors).Item1;
                        }
                        else
                        {
                            // Create a mutated copy of the winning design
                            dna          = UtilityCore.Clone(winningBot);
                            dna.UniqueID = Guid.NewGuid();
                            dna.Generation++;

                            if (dna.Parts != null)
                            {
                                MutateUtility.Mutate(dna.Parts, _mutateArgs);
                            }
                        }

                        tracked.State = BotState.Adding;

                        tracked.Lifespan = StaticRandom.NextPercent(LIFESPAN, .1);          // randomizing the lifespan a bit to stagger when bots get killed/created

                        Point3D position = Math3D.GetRandomVector_Circular(HOMINGRADIUS / 4d).ToPoint();
                        _dreamWorld.Add(new OfflineWorld.AddBotArgs(position, null, tracked.BotAdded, dna, LEVEL, new Point3D(0, 0, 0), HOMINGRADIUS, this.WeaponDNA));

                        #endregion
                        break;

                    case BotState.Added:
                        #region Added

                        // See if this should die
                        double age = (DateTime.UtcNow - tracked.Bot.CreationTime).TotalSeconds;
                        if (age > tracked.Lifespan)
                        {
                            FitnessTracker rules = tracked.Rules;
                            tracked.Rules = null;          // set it to null as soon as possible so that the rules tick doesn't do unnecessary work

                            double score = rules.Score / age;

                            if (score > MINSCORE && score > _winningScore)
                            {
                                BotDNA winningDNA = tracked.Bot.DNAFinal;

                                if (winningDNA != null)         // it should be non null long before this point, but just make sure
                                {
                                    _winningScore = score;
                                    _winningBot   = winningDNA;
                                }
                            }

                            // Kill it
                            tracked.State = BotState.Removing;
                            _dreamWorld.Remove(new OfflineWorld.RemoveArgs(tracked.Bot.Token, tracked.BotRemoved));
                        }

                        #endregion
                        break;
                    }

                    _timer.Start();
                }
            }
        }
Beispiel #10
0
        public static (BotDNA bot, WeaponDNA weapon) GetRandomDNA(ItemOptionsArco itemOptions, BotShellColorsDNA shellColors = null, WeaponDNA weapon = null)
        {
            Random rand = StaticRandom.GetRandomForThread();

            BotDNA bot = new BotDNA()
            {
                UniqueID            = Guid.NewGuid(),
                Lineage             = Guid.NewGuid().ToString(),
                Generation          = 0,
                DraggingMaxVelocity = rand.NextPercent(5, .25),
                DraggingMultiplier  = rand.NextPercent(20, .25),
            };

            #region Parts

            List <ShipPartDNA> parts = new List <ShipPartDNA>();

            double partSize;

            // Homing
            partSize = rand.NextPercent(1, .5);
            parts.Add(new ShipPartDNA()
            {
                PartType = SensorHoming.PARTTYPE, Position = new Point3D(0, 0, 2.5), Orientation = Quaternion.Identity, Scale = new Vector3D(partSize, partSize, partSize)
            });

            // Vision
            //TODO: Support filtering by type
            partSize = rand.NextPercent(1, .5);
            parts.Add(new SensorVisionDNA()
            {
                PartType = SensorVision.PARTTYPE, Position = new Point3D(0, 0, 1.5), Orientation = Quaternion.Identity, Scale = new Vector3D(partSize, partSize, partSize), SearchRadius = itemOptions.VisionSensor_SearchRadius
            });

            // Brains
            int numBrains = 1 + Convert.ToInt32(rand.NextPow(5, 1d) * 4);

            for (int cntr = 0; cntr < numBrains; cntr++)
            {
                partSize = rand.NextPercent(1, .5);

                Point3D position = new Point3D(0, 0, 0);
                if (numBrains > 1)
                {
                    position = Math3D.GetRandomVector_Circular(1).ToPoint();
                }

                parts.Add(new ShipPartDNA()
                {
                    PartType = Brain.PARTTYPE, Position = position, Orientation = Quaternion.Identity, Scale = new Vector3D(partSize, partSize, partSize)
                });
            }

            // MotionController - always exactly one of these
            partSize = rand.NextPercent(1, .5);
            parts.Add(new ShipPartDNA()
            {
                PartType = MotionController2.PARTTYPE, Position = new Point3D(0, 0, -1.5), Orientation = Quaternion.Identity, Scale = new Vector3D(partSize, partSize, partSize)
            });

            // Store it
            bot.Parts = parts.ToArray();

            #endregion

            if (shellColors == null)
            {
                bot.ShellColors = BotShellColorsDNA.GetRandomColors();
            }
            else
            {
                bot.ShellColors = shellColors;
            }

            #region Weapon

            WeaponDNA weaponActual = null;
            if (weapon == null)
            {
                if (rand.NextDouble() < .95d)
                {
                    WeaponHandleMaterial[] weaponMaterials = new WeaponHandleMaterial[] { WeaponHandleMaterial.Soft_Wood, WeaponHandleMaterial.Hard_Wood };

                    weaponActual = new WeaponDNA()
                    {
                        UniqueID = Guid.NewGuid(),
                        Handle   = WeaponHandleDNA.GetRandomDNA(weaponMaterials[StaticRandom.Next(weaponMaterials.Length)])
                    };
                }
            }
            else
            {
                weaponActual = weapon;
            }

            #endregion

            return(bot, weaponActual);
        }
Beispiel #11
0
        public NPCNest(NPCNestDNA dna, double radius, World world, Map map, KeepItems2D keepItems2D, MaterialIDs materialIDs, Viewport3D viewport, EditorOptions editorOptions, ItemOptionsArco itemOptions, IGravityField gravity, DragHitShape dragPlane)
        {
            // Store stuff
            _world = world;
            _map = map;
            _keepItems2D = keepItems2D;
            _materialIDs = materialIDs;
            _viewport = viewport;
            _editorOptions = editorOptions;
            _itemOptions = itemOptions;
            _gravity = gravity;
            _dragPlane = dragPlane;

            // DNA
            NPCNestDNA fixedDNA = GetFinalDNA(dna);
            _shellColors = fixedDNA.ShellColors;
            _botDNA = fixedDNA.BotDNA;
            _weaponDNA = fixedDNA.WeaponDNA;

            //TODO: Hand this a winner list, and filter criteria
            _dreamer = new EvolutionDreamer(_itemOptions, _shellColors, 4);     //TODO: Num bots should come from item options
            _dreamer.WeaponDNA = EvolutionDreamer.GetRandomDNA().Item2;

            #region WPF Model

            var models = GetModel(_shellColors, radius);
            this.Model = models.Item1;
            _eggModels = models.Item2;

            _rotateTransform = new QuaternionRotation3D();
            _translateTransform = new TranslateTransform3D();

            Transform3DGroup transform = new Transform3DGroup();
            transform.Children.Add(new RotateTransform3D(_rotateTransform));
            transform.Children.Add(_translateTransform);

            ModelVisual3D visual = new ModelVisual3D();
            visual.Transform = transform;
            visual.Content = this.Model;

            this.Visuals3D = new Visual3D[] { visual };

            #endregion

            // Energy tank
            _energy = new Container();
            _energy.QuantityMax = _itemOptions.Nest_Energy_Max * radius;
            _energy.QuantityCurrent = _energy.QuantityMax * .5d;

            // Finish
            this.Token = TokenGenerator.NextToken();
            this.Radius = radius;
            this.CreationTime = DateTime.UtcNow;
        }
Beispiel #12
0
 public AddBotArgs(Point3D position, Vector3D?angularVelocity, ItemAddedHandler itemAdded, BotDNA bot, int level, Point3D homingPoint, double homingRadius, WeaponDNA attachedWeapon = null, WeaponDNA[] inventoryWeapons = null)
     : base(position, angularVelocity, itemAdded)
 {
     this.Bot              = bot;
     this.Level            = level;
     this.HomingPoint      = homingPoint;
     this.HomingRadius     = homingRadius;
     this.AttachedWeapon   = attachedWeapon;
     this.InventoryWeapons = inventoryWeapons;
 }