// Use this for initialization
    void Start()
    {
        m_rb      = gameObject.GetComponent <Rigidbody2D>();
        m_tm      = new TimeoutEventManager();
        m_actions = new PriorityList();

        m_renderer = gameObject.GetComponent <SpriteRenderer>();

        m_im = new IntervalEventManager();

        m_im.addListener(HFlineFollowCONFIG.CreatureDecisionLatency, () =>
        {
            m_actions.flush();
            //Flush actions, set neew behaviours
            set_behaviour_samples++;
            setBehaviours();
        });

        // m_im.addListener(2f, () =>
        // {
        //   // Debug.Log("Fit: " + fit_samples + ", Beh: " + set_behaviour_samples);
        //   fit_samples = 0;
        //   set_behaviour_samples = 0;
        // });

        m_im.addListener(1f, () => {
            m_health.add(-1f);
        });

        m_im.addListener(HFlineFollowCONFIG.CreatureFitnessLatency, fitnessUpdate);
    }
    void Start()
    {
        //Instantiate controller
        m_evolution = new DNABasedEvolutionManager <MindBodyDNDNA <ResourceFightDNCreature> >(
            new MindBodySpeciesDN <ResourceFightDNCreature>(0,
                                                            new TraitGenesSpecies(0, new HashSet <string> {
            "SPEED", "HEALTH", "DAMAGE", "ENERGY", "ATTACKSPEED"
        }, 1, new Range <float>(2f, 2f), 0, new Range <float>(0f, 0f)),
                                                            new DecisionNetSpecies <ResourceFightDNCreature>(0, ResourceFightDNCreature.getInputFactorys(), ResourceFightDNCreature.getOutputFactorys(), new Range <float>(0.8f, 1.2f))
                                                            ), 0.1f, 20, (float p_fitness) => { return(p_fitness * 0.95f); }, 1f
            );

        //Fill with 20 random
        for (int i = 0; i < 10; i++)
        {
            m_evolution.addRandom();
        }

        //Instantaite Interval and Listeners
        m_interval = new IntervalEventManager();

        for (int i = 0; i < 5; i++)
        {
            spawnResource();
        }

        for (int i = 0; i < 50; i++)
        {
            spawnCreature();
        }

        // spawnCreature();
        // spawnCreature();


        m_interval.addListener(5f, () => {
            for (int i = 0; i < 5; i++)
            {
                spawnCreature();
            }
        });

        m_interval.addListener(10f, () => {
            for (int i = 0; i < 1; i++)
            {
                spawnResource();
            }
        });

        m_interval.addListener(30f, () => {
            GameObject[] obs = ObjectLogger.getByType("CREATURE");

            foreach (GameObject ob in obs)
            {
                ob.GetComponent <ResourceFightDNCreature>().logFitness();
            }
        });
    }
示例#3
0
            public DNABasedEvolutionManager(ISpecies <ADNA <T> > p_species, float p_mutation_chance_percentage, int p_size, DFitnessMod p_dna_ageing, float p_interval)
            {
                m_gene_pool = new FitnessList <ADNA <T> >(p_size);
                m_mutation_chance_percentage = p_mutation_chance_percentage;
                m_species = p_species;

                m_interval = new IntervalEventManager();
                m_interval.addListener(p_interval, () => {
                    m_gene_pool.modifyFitness(p_dna_ageing);
                });
            }
    void Start()
    {
        Time.fixedDeltaTime = 0.04f;

        trial = DataCollector.Trials;

        setUpAI();

        setUpData();

        Task t1 = Task.Factory.StartNew(() => {
            m_evolution = createManager();

            for (int i = 0; i < m_evolution.Size; i++)
            {
                m_evolution.addRandom();
            }
        });

        m_interval = new IntervalEventManager();

        m_interval.addListener(0.2f, () =>
        {
            Task t2 = Task.Factory.StartNew(() => {
                m_goalLine.rotate(1f);
            });
        });


        t1.Wait();
        m_interval.addListener(lineFollowCONFIG.ControllerSpawnLatency, () =>
        {
            for (int i = 0; i < lineFollowCONFIG.ControllerCreatureSpawnAmount; i++)
            {
                spawn();
            }
        });
    }
    //Goal


    //----------------------------------------------------------
    //Unity Callbacks

    // Use this for initialization
    void Start()
    {
        m_rb              = gameObject.GetComponent <Rigidbody2D>();
        m_renderer        = gameObject.GetComponent <SpriteRenderer>();
        m_tm              = new TimeoutEventManager();
        m_im              = new IntervalEventManager();
        m_actions         = new PriorityList();
        m_cooldowns       = new CooldownLogger();
        m_sense_angle     = 30f;
        m_sense_proximity = 6f;
        m_brain_stop      = false;

        m_cooldowns.activate("SENSE", 0);

        m_im.addListener(0.1f, () => {
            m_decision_time = true;
        });
    }
    private void gather()
    {
        //Get list of gatherable resources
        GameObject[] obs = sense("RESOURCE");

        List <GameObject> resources     = new List <GameObject>();
        Vector3           this_position = gameObject.transform.position;

        foreach (GameObject o in obs)
        {
            if (Vector2Calc.proximity(this_position, o.transform.position) < 1f)
            {
                resources.Add(o);
            }
        }

        //If there are none, return
        if (resources.Count == 0)
        {
            return;
        }

        //Get the closest resource in gather arc
        float      closeness  = float.PositiveInfinity;
        GameObject to_harvest = null;

        foreach (GameObject o in resources)
        {
            float proximity = Vector2Calc.proximity(this_position, o.transform.position);
            if (!(proximity < closeness))
            {
                continue;
            }
            closeness  = proximity;
            to_harvest = o;
        }

        //Gather the resource
        m_brain_stop         = true;
        m_rb.velocity        = Vector3.zero;
        m_rb.angularVelocity = 0;
        m_actions.flush();

        m_cooldowns.activate("GATHER", 0.5f);

        GameObject line = LineCreator.createLine(this_position + (Vector3Calc.fromVec2(m_forward) * 0.1f), to_harvest.transform.position, Color.green, 0.05f, 0.5f);

        Resource          harvesting  = to_harvest.GetComponent <Resource>();
        DIntervalListener energy_suck = () =>
        {
            float harvest_power = m_energy.Max - m_energy.Value;
            if (harvest_power > 10f)
            {
                harvest_power = 10f;
            }
            if (to_harvest != null)
            {
                m_energy.add(harvesting.collect(harvest_power));
            }
        };

        m_im.addListener(0.1f, energy_suck);

        m_tm.addTimeout(0.5f, () =>
        {
            m_im.removeListener(0.1f, energy_suck);
            m_brain_stop = false;
        });
    }