public void ResetBrain()
 {
     brain        = null;
     neurons      = null;
     connections  = null;
     creature     = null;
     treeDataList = new List <TreeData>();
 }
Пример #2
0
    public void CreateCreature(Creature_V2 parent1, Creature_V2 parent2)
    {
        float energy      = 1f;
        float life        = 1f;
        float veloForward = 0f;
        float veloAngular = 0f;

        int[]      randomTile          = map_v2.RandomFloorTile();
        Vector3    bodyPosition        = parent1.position - (parent1.trans.up * 2f * parent1.GetRadius());
        Vector3    leftPos             = Vector3.zero;
        Vector3    rightPos            = Vector3.zero;
        GameObject creatureGameObject  = Instantiate(creaturePrefab, bodyPosition, creaturePrefab.transform.rotation) as GameObject;
        GameObject leftLineGameObject  = Instantiate(linePrefab) as GameObject;
        GameObject rightLineGameObject = Instantiate(linePrefab) as GameObject;

        leftLineGameObject.transform.parent  = creatureGameObject.transform;
        rightLineGameObject.transform.parent = creatureGameObject.transform;

        LineRenderer leftLine  = leftLineGameObject.GetComponent <LineRenderer>();
        LineRenderer rightLine = rightLineGameObject.GetComponent <LineRenderer>();

        leftLine.SetWidth(0.02f, 0.02f);
        rightLine.SetWidth(0.02f, 0.02f);

        LineRenderer[] lineSensor = new LineRenderer[4];
        for (int i = 0; i < lineSensor.Length; i++)
        {
            GameObject newLine = Instantiate(linePrefab) as GameObject;
            newLine.transform.parent = creatureGameObject.transform;
            lineSensor[i]            = newLine.GetComponent <LineRenderer>();
            lineSensor[i].SetWidth(0.02f, 0.02f);
        }

        GameObject spikeLineGameObject = Instantiate(linePrefab) as GameObject;

        spikeLineGameObject.transform.parent = creatureGameObject.transform;
        LineRenderer spikeLine = spikeLineGameObject.GetComponent <LineRenderer>();

        spikeLine.SetWidth(0.02f, 0.02f);

        Creature_V2 strongerParent = parent1.GetEnergy() > parent2.GetEnergy() ? parent1 : parent2;
        Creature_V2 weakerParent   = parent1.GetEnergy() > parent2.GetEnergy() ? parent2 : parent1;

        Brain_V2 brain = new Brain_V2(strongerParent.GetBrain(), totalCreaturesCount);

        brain.Mutate(strongerParent.GetBrain().GetWeights(), weakerParent.GetEnergy() / strongerParent.GetEnergy());
        creatureGameObject.transform.GetChild(1).GetComponent <TextMesh>().text = brain.GetName();

        string parentNames = strongerParent.GetName() + "@" + weakerParent.GetName();

        Creature_V2 creature = new Creature_V2(totalCreaturesCount, strongerParent.GetGeneration() + 1, creatureGameObject.transform, leftLine, rightLine, lineSensor, spikeLine, brain, new HSBColor(1f, 0f, 0f), bodyPosition, leftPos, rightPos, 0.5f, UnityEngine.Random.Range(0f, 360f), worldDeltaTime, creatureGameObject.transform.localScale.x / 2f, energy, energy, life, minLife, lifeDecrease, eatDamage, veloDamage, angDamage, fightDamage, veloForward, veloAngular, map_v2, this, parentNames);

        creatureList.Add(creature);
        totalCreaturesCount++;

        parent1.AddChildren(creature);
        parent2.AddChildren(creature);
    }
    public void SetBrain(Brain_V2 brain, List <TreeData> treeDataList, Creature_V2 creature)
    {
        this.brain        = brain;
        this.treeDataList = treeDataList;
        this.creature     = creature;

        neurons     = brain.GetNeurons();
        connections = brain.GetWeights();
    }
    public void Kill()
    {
        children.Clear();
        brain = null;
        map.RemoveCreatureFromTileList(tileDetail[0], tileDetail[1], this);
        map = null;

        world.RemoveCreature(this);
        GameObject.Destroy(trans.gameObject);
    }
Пример #5
0
    private void SaveWorld()
    {
        string filename = "world_snapshot.lses";

        StreamWriter writer = new StreamWriter(filename);

        writer.Write(brainNetwork.Length + " ");
        for (int i = 0; i < brainNetwork.Length; i++)
        {
            writer.Write(brainNetwork[i] + " ");
        }

        writer.Write(creatureList.Count + " ");
        for (int i = 0; i < creatureList.Count; i++)
        {
            Creature_V2 creature = creatureList[i];
            Brain_V2    brain    = creature.GetBrain();
            //float[][] neurons = brain.GetNeurons();
            float[][][] connections = brain.GetWeights();

            writer.Write(creature.GetName() + " "
                         + creature.GetParentNames() + " "
                         + creature.GetEnergy() + " "
                         + creature.GetLife() + " "
                         + creature.position.x + " "
                         + creature.position.y + " "
                         + creature.rotation + " "
                         + creature.veloForward + " "
                         + creature.veloAngular + " ");

            for (int j = 0; j < connections.Length; j++)
            {
                for (int k = 0; k < connections[j].Length; k++)
                {
                    for (int l = 0; l < connections[j][k].Length; l++)
                    {
                        writer.Write(connections[j][k][l] + " ");
                    }
                }
            }
        }

        Tile_V2[,] tiles = map_v2.GetTilesArray();

        for (int j = 0; j < 100; j++)
        {
            for (int k = 0; k < 100; k++)
            {
                writer.Write(tiles[j, k].currentEnergy + " ");
            }
        }

        writer.Close();
    }
    public Creature_V2(int ID, int generation, Transform trans, LineRenderer leftLine, LineRenderer rightLine, LineRenderer[] sensorLine, LineRenderer spikeLine,
                       Brain_V2 brain, HSBColor bodyColor, Vector3 bodyPos, Vector3 leftPos, Vector3 rightPos, float sensorSize,
                       float angle, float worldDeltaTime, float initialRadius, float initialEnergy, float currentEnergy, float life, float minLife, float lifeDecerase, float eatDamage, float veloDamage, float angDamage, float fightDamage, float veloForward, float veloAngular,
                       TileMap_V2 map, WolrdManager_V2 world, String parentNames)
        : base(initialRadius, bodyPos, angle, veloForward, veloAngular, 1f, worldDeltaTime)
    {
        this.ID         = ID;
        this.generation = generation;
        this.trans      = trans;

        this.leftLine   = leftLine;
        this.rightLine  = rightLine;
        this.sensorLine = sensorLine;
        this.spikeLine  = spikeLine;

        this.brain          = brain;
        this.bodyColor      = bodyColor;
        this.leftPos        = leftPos;
        this.rightPos       = rightPos;
        this.sensorSize     = sensorSize;
        this.worldDeltaTime = worldDeltaTime;
        this.initialRadius  = initialRadius;
        this.map            = map;
        this.world          = world;
        this.parentNames    = parentNames;

        this.textMesh   = trans.GetChild(1).GetComponent <TextMesh>();
        this.fightTrans = trans.GetChild(3);


        for (int i = 0; i < sensorPos.Length; i++)
        {
            sensorPos[i] = Vector3.zero;
        }

        this.spikeLength = 0f;
        this.spikePos    = Vector3.zero;

        this.children = new List <Creature_V2>();
        //energyDensity = 1f/(Mathf.PI * initialRadius * initialRadius);

        this.bodyMaterial  = trans.GetComponent <Renderer>().material;
        this.mouthMaterial = trans.GetChild(0).GetComponent <Renderer>().material;

        this.energy = new Energy(initialEnergy, map, worldDeltaTime, minLife, lifeDecerase, eatDamage, veloDamage, angDamage, fightDamage);
        this.energy.SetEnergy(currentEnergy);
        this.energy.SetLife(life);

        this.bodyColor  = new HSBColor(UnityEngine.Random.Range(0f, 1f), 1f, 1f);
        this.mouthColor = new HSBColor(UnityEngine.Random.Range(0f, 1f), 1f, 1f);

        this.creatureName = brain.GetName();
    }
Пример #7
0
    public void CreateCreature()
    {
        float energy      = 1f;
        float life        = 1f;
        float veloForward = 0f;
        float veloAngular = 0f;

        int[]      randomTile          = map_v2.RandomFloorTile();
        Vector3    bodyPosition        = new Vector3(randomTile[0] + 0.5f, randomTile[1] + 0.5f, creaturePrefab.transform.position.z);
        Vector3    leftPos             = Vector3.zero;
        Vector3    rightPos            = Vector3.zero;
        GameObject creatureGameObject  = Instantiate(creaturePrefab, bodyPosition, creaturePrefab.transform.rotation) as GameObject;
        GameObject leftLineGameObject  = Instantiate(linePrefab) as GameObject;
        GameObject rightLineGameObject = Instantiate(linePrefab) as GameObject;

        leftLineGameObject.transform.parent  = creatureGameObject.transform;
        rightLineGameObject.transform.parent = creatureGameObject.transform;

        LineRenderer leftLine  = leftLineGameObject.GetComponent <LineRenderer>();
        LineRenderer rightLine = rightLineGameObject.GetComponent <LineRenderer>();

        leftLine.SetWidth(0.02f, 0.02f);
        rightLine.SetWidth(0.02f, 0.02f);

        GameObject spikeLineGameObject = Instantiate(linePrefab) as GameObject;

        spikeLineGameObject.transform.parent = creatureGameObject.transform;
        LineRenderer spikeLine = spikeLineGameObject.GetComponent <LineRenderer>();

        spikeLine.SetWidth(0.02f, 0.02f);

        LineRenderer[] lineSensor = new LineRenderer[4];
        for (int i = 0; i < lineSensor.Length; i++)
        {
            GameObject newLine = Instantiate(linePrefab) as GameObject;
            newLine.transform.parent = creatureGameObject.transform;
            lineSensor[i]            = newLine.GetComponent <LineRenderer>();
            lineSensor[i].SetWidth(0.02f, 0.02f);
        }

        Brain_V2 brain = new Brain_V2(brainNetwork, totalCreaturesCount, mutationNumber, mutationSign, mutationRandom, mutationIncrease, mutationDecrease, mutationWeakerParentFactor);

        creatureGameObject.transform.GetChild(1).GetComponent <TextMesh>().text = brain.GetName();

        Creature_V2 creature = new Creature_V2(totalCreaturesCount, 0, creatureGameObject.transform, leftLine, rightLine, lineSensor, spikeLine, brain, new HSBColor(1f, 0f, 0f), bodyPosition, leftPos, rightPos, 0.5f, UnityEngine.Random.Range(0f, 360f), worldDeltaTime, creatureGameObject.transform.localScale.x / 2f, energy, energy, life, minLife, lifeDecrease, eatDamage, veloDamage, angDamage, fightDamage, veloForward, veloAngular, map_v2, this, "WORLD");

        creatureList.Add(creature);
        totalCreaturesCount++;
    }