Example #1
0
    void most_similar_creature()
    {
        targetCrt = null;                               // reference to the script of the closest creature
        GameObject target     = null;
        GameObject c          = null;                   // current collider being looked at
        float      similarity = Mathf.Infinity;
        float      curr_similarity;

        cs = Physics.OverlapSphere(_t.position, (float)los);


        if (cs.Length == 0)
        {
            target = null;
            return;
        }

        foreach (Collider col in cs)
        {
            c = (GameObject)col.transform.gameObject;
            if (c && c.gameObject.name == "root" && c != crt.root.gameObject)
            {
                other_crt       = c.transform.parent.GetComponent <Creature>();
                curr_similarity = GeneticsUtils.similar_colour(crt.chromosome, other_crt.chromosome);

                if (curr_similarity < similarity)
                {
                    target     = c.transform.parent.gameObject;
                    similarity = curr_similarity;
                }

                Vector3 diff = c.transform.position - _t.position;
                if (diff.magnitude < (float)crt_mate_range)
                {
                    other_crt = c.transform.parent.GetComponent <Creature>();
                    Genitalia other_genital = other_crt.genital.GetComponent <Genitalia>();
                    if (crt.state == Creature.State.persuing_mate || other_crt.state == Creature.State.persuing_mate)
                    {
                        co.observe(crt.genital.gameObject, other_genital.gameObject);
                        other_crt.state = Creature.State.mating;
                        crt.state       = Creature.State.mating;
                    }
                    similarity = curr_similarity;
                }
            }

            distance_to_goal = 0F;
            goal             = null;
            if (target)
            {
                targetCrt        = target.GetComponent <Creature>();
                goal             = targetCrt.root;
                distance_to_goal = distanceToGoal();
            }
        }
    }
    public void observe(GameObject a, GameObject b)
    {
        collision_events.Add(new CollEvent(a, b));
        CollEvent dup = findMatch(a, b);

        // If a duplicate event has been found spawn a child
        if (null != dup)
        {
            collision_events.Clear();
            Vector3 pos = (a.transform.position - b.transform.position) * 0.5F + b.transform.position;

            // Get references to the scripts of each creature
            Creature a_script = a.transform.parent.parent.GetComponent <Creature>();
            Creature b_script = b.transform.parent.parent.GetComponent <Creature>();

            double a_energy = a_script.getEnergy();
            double b_energy = b_script.getEnergy();

            Chromosome newChromosome;
            newChromosome = GeneticsUtils.crossover(a_script.chromosome, b_script.chromosome, crossover_rate);
            newChromosome = GeneticsUtils.mutate(newChromosome, mutation_rate, mutation_factor);

            spw.spawn(pos, Vector3.zero,
                      a_energy * energy_scale + b_energy * energy_scale,
                      newChromosome
                      );
            a_script.subtractEnergy(a_energy * energy_scale);
            b_script.subtractEnergy(b_energy * energy_scale);

            a_script.offspring++;
            b_script.offspring++;
        }
        else
        {
            collision_events.Add(new CollEvent(b, a));
        }
    }