コード例 #1
0
ファイル: BotTree.cs プロジェクト: MitchKeenan0/TreeGame
    void Start()
    {
        limbList = new List <TreeLimb>();
        leafList = new List <Leaf>();
        soil     = FindObjectOfType <Soil>();
        energy   = GetComponent <Energy>();

        // create trunk
        GameObject stem  = Instantiate(limbPrefab, transform);
        TreeLimb   tLimb = stem.GetComponent <TreeLimb>();

        tLimb.SetLimbLine(false, transform.position, transform.up * 0.1f);
        tLimb.SetGrowTargetLength(0.3f);
        tLimb.SetGrowTargetWidth(0.02f);
        tLimb.SetColliderEnabled(true);
        tLimb.AttachToRb(soil.GetComponent <Rigidbody2D>());
        limbList.Add(tLimb);

        PlantPhysics pp = stem.GetComponent <PlantPhysics>();

        pp.SetComfortableAngle(3f);

        PlantPhysics pPhysics = tLimb.GetComponent <PlantPhysics>();

        pPhysics.SetNaturalDirection(Vector3.up);

        tLimb.ImbueLeaf();
        tLimb.bLifeCritical = true;

        sproutCoroutine = SproutInterval();
        StartCoroutine(sproutCoroutine);
    }
コード例 #2
0
        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            pop          = InitPopulation();
            score        = new PlantScore();
            this.genetic = new BasicEA(pop, score);

            //this.genetic.Speciation = new ArraySpeciation<DoubleArrayGenome>();

            genetic.AddOperation(0.9, new Splice(PlantUniverse.GenomeSize / 3));
            genetic.AddOperation(0.1, new MutatePerturb(0.1));

            // Display

            this.universe = new PlantUniverse();
            this.universe.Reset();


            DoubleArrayGenome bestGenome = (DoubleArrayGenome)genetic.BestGenome;
            PlantPhysics      physics    = new PlantPhysics();
            PlantGrowth       growth     = new PlantGrowth();

            for (int i = 0; i < 100; i++)
            {
                physics.RunPhysics(universe);
                growth.RunGrowth(universe, bestGenome.Data);
            }

            this.display          = new DisplayPlant(CanvasOutput);
            this.display.Universe = this.universe;
            Thread t = new Thread(DoWork);

            t.Start();
        }
コード例 #3
0
        private void DoWork()
        {
            int generation = 0;

            UpdateStatus("Starting up...");

            while (!_done)
            {
                generation++;
                this.genetic.Iteration();

                this.universe.Reset();

                DoubleArrayGenome bestGenome = (DoubleArrayGenome)this.genetic.BestGenome;
                PlantGrowth       growth     = new PlantGrowth();
                PlantPhysics      physics    = new PlantPhysics();

                for (int i = 0; i < PlantUniverse.EvaluationCycles; i++)
                {
                    physics.RunPhysics(universe);
                    growth.RunGrowth(universe, bestGenome.Data);
                }

                double bestScore = this.genetic.BestGenome.Score;
                UpdateStatus("Generation: " + generation + ", Best Score: " + bestScore);



                //System.out.println(Arrays.toString(bestGenome.getLongTermMemory()));
            }
        }
コード例 #4
0
        /// <inheritdoc />
        public double CalculateScore(IMLMethod algo)
        {
            var genome   = (DoubleArrayGenome)algo;
            var universe = new PlantUniverse();

            universe.Reset();
            var physics = new PlantPhysics();
            var growth  = new PlantGrowth();

            // Run the generations.
            for (int i = 0; i < PlantUniverse.EvaluationCycles; i++)
            {
                physics.RunPhysics(universe);
                growth.RunGrowth(universe, genome.Data);
            }

            // Count the amount of green.
            int    count = 0;
            double sum   = 0;

            for (int row = 0; row < PlantUniverse.UniverseHeight; row++)
            {
                for (int col = 0; col < PlantUniverse.UniverseWidth; col++)
                {
                    PlantUniverseCell cell = universe.GetCell(row, col);
                    if (cell.IsAlive)
                    {
                        if (row >= PlantUniverse.GroundLine)
                        {
                            sum += 0.5;
                        }
                        else
                        {
                            sum += cell.Leafyness;
                        }
                    }
                    count++;
                }
            }
            return(sum / count);
        }
コード例 #5
0
ファイル: BotTree.cs プロジェクト: MitchKeenan0/TreeGame
    void SpawnNewLimb()
    {
        bool bLimb = false;
        int  tries = 0;

        while (!bLimb && (tries < (limbList.Count * limbList.Count)))
        {
            tries++;
            int parentLimbIndex = Random.Range(0, limbList.Count);
            if ((limbList[parentLimbIndex] != null))
            {
                TreeLimb   parentLimb       = limbList[parentLimbIndex];
                float      parentLength     = parentLimb.GetLimbLength();
                float      minimumLength    = Mathf.Clamp(Mathf.Sqrt(parentLength), 0f, parentLength * 0.9f);
                float      sproutLimbHeight = Random.Range(minimumLength, parentLength);
                Vector3    sproutPosition   = parentLimb.transform.position + (parentLimb.transform.up * sproutLimbHeight);
                Quaternion spawnRotation    = Quaternion.Euler(new Vector3(0f, 0f, Random.Range(-50f, 50f)));

                GameObject stem = Instantiate(limbPrefab, Vector3.zero, spawnRotation);
                stem.transform.SetParent(parentLimb.transform, false);
                stem.transform.position = sproutPosition;

                TreeLimb stemLimb = stem.GetComponent <TreeLimb>();
                stemLimb.SetLimbLine(true, sproutPosition, stemLimb.transform.up * 0.1f);
                stemLimb.AttachToRb(parentLimb.GetComponent <Rigidbody2D>());
                stemLimb.SetColliderEnabled(true);
                stemLimb.SetGrowTargetLength(parentLimb.GetLimbLength() * 0.36f);
                stemLimb.SetGrowTargetWidth(0.02f);
                stemLimb.ParentalGrowth(1.1f, 1.02f);

                PlantPhysics pPhysics = stemLimb.GetComponent <PlantPhysics>();
                pPhysics.SetNaturalDirection(stemLimb.transform.up);
                stemLimb.ImbueLeaf();

                limbList.Add(stemLimb);

                bLimb = true;
            }
        }
    }
コード例 #6
0
    public void Spend()
    {
        if (targetLimb != null)
        {
            limbPrototype.transform.SetParent(targetLimb.transform);
            limbPrototype.AttachToRb(targetLimb.GetComponent <Rigidbody2D>());
            limbPrototype.SetColliderEnabled(true);

            limbPrototype.SetGrowTargetLength(targetLimb.GetLimbLength() * 0.36f);
            limbPrototype.SetGrowTargetWidth(0.02f);
            limbPrototype.ParentalGrowth(1.1f, 1.05f);

            PlantPhysics pPhysics = limbPrototype.GetComponent <PlantPhysics>();
            pPhysics.SetNaturalDirection(limbPrototype.transform.up);
            limbPrototype.ImbueLeaf();

            closeupUI.SetActive(false);

            bTargeting = false;
            Destroy(gameObject);
        }
    }
コード例 #7
0
    void Update()
    {
        if (bAlive)
        {
            if (growWidth != growTargetWidth)
            {
                growWidth = Mathf.Lerp(growWidth, growTargetWidth, Time.deltaTime);
                SetLimbWidth(growWidth);
                PlantPhysics physics = GetComponent <PlantPhysics>();
                physics.SetForceScale(growWidth);
            }

            if (growDistance != growTargetLength)
            {
                growDistance = Mathf.Lerp(growDistance, growTargetLength, Time.deltaTime);
                growDistance = Mathf.Clamp(growDistance, 0f, growTargetLength);
                SetLimbLine(false, transform.position, (transform.up * growDistance));
            }

            SetVisible(true);
        }
    }