Example #1
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();
    }
    private void DrawBrain()
    {
        if (brain != null)
        {
            float xOff       = (int)(screenWidth / (neurons.Length - 1));
            float yOff       = (int)(screenHeight * 0.025f);
            float rectWidth  = (int)(screenWidth * 0.075f);
            float rectHeight = (int)(rectWidth / 1.5f);

            if (drawNetState == true)
            {
                for (int layerIndex = 0; layerIndex < connections.Length; layerIndex++)
                {
                    float currentLayerYRatio = (screenHeight / 2f) / (float)connections[layerIndex].Length;
                    float currentXPos        = (int)((layerIndex + 1) * xOff);
                    float previousXPos       = (int)((layerIndex) * xOff);

                    for (int neuronOfLayerIndex = 0; neuronOfLayerIndex < connections[layerIndex].Length; neuronOfLayerIndex++)
                    {
                        float previousLayerYRatio = (screenHeight / 2f) / (float)connections[layerIndex][neuronOfLayerIndex].Length;


                        for (int previousLayerNeuronIndex = 0; previousLayerNeuronIndex < connections[layerIndex][neuronOfLayerIndex].Length; previousLayerNeuronIndex++)
                        {
                            Vector2 pointA = new Vector2(currentXPos + (int)(rectWidth / 2f), currentLayerYRatio * neuronOfLayerIndex + yOff + (int)(rectHeight / 2f));
                            Vector2 pointB = new Vector2(previousXPos + (int)(rectWidth / 2f), previousLayerYRatio * previousLayerNeuronIndex + yOff + (int)(rectHeight / 2f));

                            Color lineColor = positiveLineColor;

                            if (connections[layerIndex][neuronOfLayerIndex][previousLayerNeuronIndex] <= 0f)
                            {
                                lineColor = negativeLineColor;
                            }

                            Drawing.DrawLine(pointA, pointB, lineColor, 2f, texture);
                        }
                    }
                }
            }
            else
            {
                Vector2 mousePosition = Event.current.mousePosition;

                for (int x = 0; x < neurons.Length; x++)
                {
                    float numberOfNeuronsInLayer = neurons[x].Length;
                    float yRatio = (screenHeight / 2f) / numberOfNeuronsInLayer;

                    float xpos = x * xOff;
                    float ypos = 0f;
                    for (int y = 0; y < numberOfNeuronsInLayer; y++)
                    {
                        ypos = y * yRatio + yOff;
                        Rect rec = new Rect(xpos, ypos, rectWidth, rectHeight);
                        if (rec.Contains(mousePosition) == true)
                        {
                            if (x == 0)
                            {
                                float nextLayerYRatio = (screenHeight / 2f) / (float)neurons[x + 1].Length;
                                float nextXPos        = (int)((x + 1) * xOff);
                                float currentXPos     = (int)((x) * xOff);

                                for (int z = 0; z < neurons[x + 1].Length; z++)
                                {
                                    Vector2 pointA = new Vector2(rec.x + (int)(rectWidth / 2f), rec.y + (int)(rectHeight / 2f));
                                    Vector2 pointB = new Vector2(nextXPos + (int)(rectWidth / 2f), nextLayerYRatio * z + yOff + (int)(rectHeight / 2f));

                                    Color lineColor = positiveLineColor;

                                    if (connections[x][z][y] <= 0f)
                                    {
                                        lineColor = negativeLineColor;
                                    }

                                    Drawing.DrawLine(pointA, pointB, lineColor, 1f, texture);
                                }
                                break;
                            }
                            else if (x == (neurons.Length - 1))
                            {
                                float nextLayerYRatio = (screenHeight / 2f) / (float)neurons[x - 1].Length;
                                float nextXPos        = (int)((x - 1) * xOff);

                                for (int z = 0; z < neurons[x - 1].Length; z++)
                                {
                                    Vector2 pointA = new Vector2(rec.x + (int)(rectWidth / 2f), rec.y + (int)(rectHeight / 2f));
                                    Vector2 pointB = new Vector2(nextXPos + (int)(rectWidth / 2f), nextLayerYRatio * z + yOff + (int)(rectHeight / 2f));

                                    Color lineColor = positiveLineColor;

                                    if (connections[x - 1][y][z] <= 0f)
                                    {
                                        lineColor = negativeLineColor;
                                    }

                                    Drawing.DrawLine(pointA, pointB, lineColor, 1f, texture);
                                }

                                break;
                            }
                            else
                            {
                                float nextLayerYRatio = (screenHeight / 2f) / (float)neurons[x - 1].Length;
                                float nextXPos        = (int)((x - 1) * xOff);

                                for (int z = 0; z < neurons[x - 1].Length; z++)
                                {
                                    Vector2 pointA = new Vector2(rec.x + (int)(rectWidth / 2f), rec.y + (int)(rectHeight / 2f));
                                    Vector2 pointB = new Vector2(nextXPos + (int)(rectWidth / 2f), nextLayerYRatio * z + yOff + (int)(rectHeight / 2f));

                                    Color lineColor = positiveLineColor;

                                    if (connections[x - 1][y][z] <= 0f)
                                    {
                                        lineColor = negativeLineColor;
                                    }

                                    Drawing.DrawLine(pointA, pointB, lineColor, 1f, texture);
                                }

                                float nextLayerYRatio2 = (screenHeight / 2f) / (float)neurons[x + 1].Length;
                                float nextXPos2        = (int)((x + 1) * xOff);

                                for (int z = 0; z < neurons[x + 1].Length; z++)
                                {
                                    Vector2 pointA = new Vector2(rec.x + (int)(rectWidth / 2f), rec.y + (int)(rectHeight / 2f));
                                    Vector2 pointB = new Vector2(nextXPos2 + (int)(rectWidth / 2f), nextLayerYRatio2 * z + yOff + (int)(rectHeight / 2f));

                                    Color lineColor = positiveLineColor;

                                    if (connections[x][z][y] <= 0f)
                                    {
                                        lineColor = negativeLineColor;
                                    }

                                    Drawing.DrawLine(pointA, pointB, lineColor, 1f, texture);
                                }
                                break;
                            }
                        }
                    }
                }
            }

            GUIStyle myStyle = new GUIStyle();
            myStyle.fontStyle = FontStyle.Bold;
            myStyle.fontSize  = (int)(screenWidth * 0.025f);

            for (int x = 0; x < neurons.Length; x++)
            {
                float numberOfNeuronsInLayer = neurons[x].Length;
                float yRatio = (screenHeight / 2f) / numberOfNeuronsInLayer;

                float xpos = x * xOff;
                float ypos = 0f;
                for (int y = 0; y < numberOfNeuronsInLayer; y++)
                {
                    ypos      = y * yRatio + yOff;
                    GUI.color = Color.white;
                    GUI.DrawTexture(new Rect(xpos, ypos, rectWidth, rectHeight), texture);
                    myStyle.normal.textColor = Color.black;
                    GUI.Label(new Rect((int)(xpos), (int)(ypos + (rectHeight / 4f)), rectWidth, rectHeight), neurons[x][y].ToString("0.000") + "", myStyle);

                    /*if (x == 0)
                     * {
                     *  myStyle.normal.textColor = Color.green;
                     *  GUI.Label(new Rect((int)(xpos + (rectWidth / 0.85f)), (int)(ypos + (rectHeight / 4f)), rectWidth, rectHeight), inputNames[y], myStyle);
                     * }
                     * else if (x == neurons.Length - 1)
                     * {
                     *  myStyle.normal.textColor = Color.green;
                     *  GUI.Label(new Rect((int)(xpos - (rectWidth / 0.475f)), (int)(ypos + (rectHeight / 4f)), rectWidth, rectHeight), outputNames[y], myStyle);
                     * }*/
                }
            }

            for (int i = 0; i < treeDataList.Count; i++)
            {
                myStyle.normal.textColor = treeDataList[i].color;

                if (i == 0)
                {
                    string state = creature.IsAlive() == true ? "[ALIVE]" : "[DEAD]";

                    string cretureInformation = treeDataList[i].name + " " + state +
                                                "\n                                                         Parents: [" + creature.GetParentNames() + "]" +
                                                "\n                                                         Child Count: " + creature.GetChildCount() +
                                                "\n                                                         Generation: " + creature.GetGeneration() +
                                                "\n                                                         Time Lived: " + creature.GetTimeLived().ToString("0.000") +
                                                "\n                                                         Life: " + creature.GetLife().ToString("0.000") +
                                                "\n                                                         Energy: " + creature.GetEnergy().ToString("0.000") +
                                                "\n                                                         Delta: " + creature.GetDeltaEnergy();

                    GUI.Label(new Rect(1f, screenHeight / 1.9f + rectHeight + i * (yOff / 1.5f), rectWidth, rectHeight), cretureInformation, myStyle);
                }
                else
                {
                    GUI.Label(new Rect(1f, screenHeight / 1.9f + rectHeight + i * (yOff / 1.5f), rectWidth, rectHeight), treeDataList[i].name, myStyle);
                }
            }
        }
    }