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); }
//DEBUG ONLY public string CreatureTraverseRecursive(Creature_V2 parent) { string add = ""; List <Creature_V2> children = parent.GetChildren(); if (children.Count == 0) { add = parent.GetID() + "::" + parent.GetName() + "__"; return(add); } for (int i = 0; i < children.Count; i++) { add += CreatureTraverseRecursive(children[i]); } return(parent.GetID() + "::" + parent.GetName() + "==>" + add); }
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(); }