public void CreatureTraverseParentTree(Creature_V2 parent, int colorIndex, string indent) { List <Creature_V2> children = parent.GetChildren(); if (children.Count > 0) { for (int i = 0; i < children.Count; i++) { TreeData treeData = new TreeData(); if (children[i].IsAlive()) { treeData.name = indent + children[i].GetName(); } else { treeData.name = indent + children[i].GetName() + " [DEAD]"; } if (colorIndex == colors.Length) { colorIndex = 0; } treeData.color = colors[colorIndex]; treeDataList.Add(treeData); CreatureTraverseParentTree(children[i], colorIndex + 1, indent + " "); } } }
public void AddCreatureToTileList(int x, int y, Creature_V2 creature) { if (IsValidLocation(x, y) == true) { tiles[y, x].creatureListOnTile.Add(creature); } }
public void ResetBrain() { brain = null; neurons = null; connections = null; creature = null; treeDataList = new List <TreeData>(); }
public void RemoveCreature(Creature_V2 creature) { creatureList.Remove(creature); if (creatureList.Count < minCreatureCount) { CreateCreature(); } }
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 ResetAllNodes() { if (this.creature != null) { this.creature.SetIsNode(false); } this.creature = null; this.treeDataList = new List <TreeData>(); }
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 float GetTileEnergy(int x, int y) * { * if (IsValidLocation(x, y) == true) * { * return tiles[y, x].currentEnergy; * } * return 0f; * }*/ public void RemoveCreatureFromTileList(int x, int y, Creature_V2 creature) { if (IsValidLocation(x, y) == true) { int index = tiles[y, x].creatureListOnTile.IndexOf(creature); if (index != -1) { tiles[y, x].creatureListOnTile.RemoveAt(index); } } }
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++; }
public void MakeTree(List <Creature_V2> creatures) { this.creature = creatures.OrderBy(o => o.GetID()).ToList()[0]; //order the list in acending order and get the creature at index 0 //Debug.LogError(CreatureTraverseRecursive(this.creature)); TreeData treeData = new TreeData(); treeData.color = colors[0]; treeData.name = this.creature.GetName(); treeDataList.Add(treeData); CreatureTraverseParentTree(this.creature, 1, " "); this.creature.SetIsNode(true); }
//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); }
void Start() { creature = null; }
public void UpdateCreatureEnergy(int x, int y, float[] output, float groundHue, float mouthHue, Creature_V2 spikeCreature) { float accelForward = output[0]; float accelAngular = output[1]; float eatFood = output[4]; float birth = output[5]; float fight = output[6]; deltaEnergy = 0f; deltaEnergy -= (worldDeltaTime / 5f) * (Mathf.Sqrt(Mathf.Max(currentEnergy / initialEnergy, 1f))); //natural energy loss (creature will die in 5 years) deltaEnergy -= (Mathf.Abs(accelForward) * worldDeltaTime) / veloDamage /*5f3f*/; //if creature keep moving at max speed it will die in 5 years deltaEnergy -= (Mathf.Abs(accelAngular) * worldDeltaTime) / angDamage /*1f0.5f*/; //if creature keeps turing at max acceleration it will die in 2 years // At worst if the creatures keep turning and moving at max rate it will die in 1.1 year int tileType = map.GetTileType(x, y); if (tileType == Tile_V2.TILE_WATER) { deltaEnergy -= worldDeltaTime * 20f; } else if (eatFood > 0 && tileType != Tile_V2.TILE_INFERT) { deltaEnergy += map.Eat(x, y) / 1.1f; // 1 (cirmum) = 2*pi*r // 1/(pi*2) = r = 0.15915494309189533576888376337251 // turn r = 1 will make c = 2*pi // lenght of two chords combined = 2*sin(theta/2) float mouthX = Mathf.Cos(mouthHue * Mathf.PI * 2); float mouthY = Mathf.Sin(mouthHue * Mathf.PI * 2); float groundX = Mathf.Cos(groundHue * Mathf.PI * 2); float groundY = Mathf.Sin(groundHue * Mathf.PI * 2); float dist = Mathf.Pow(Mathf.Pow(mouthX - groundX, 2) + Mathf.Pow(mouthY - groundY, 2), 0.5f); deltaEnergy -= (dist * worldDeltaTime * eatDamage); //2 is good } if (fight > 0 && /*maturity > MIN_BIRTH_MATURITY &&*/ spikeCreature != null && maturity > MIN_FIGHT_MATURITY) { float enemyEnergy = spikeCreature.GetEnergy(); if (enemyEnergy > 0f) { float energySuck = fightDamage * (Mathf.Max(currentEnergy, 0f)); enemyEnergy -= energySuck; if (enemyEnergy < 0f) { energySuck = (energySuck + enemyEnergy) + 0.005f; } deltaEnergy += (energySuck / 1.25f); spikeCreature.RemoveEnergy(energySuck); } /*float enemyLife = spikeCreature.GetLife(); * if (enemyLife > 0f) * { * float lifeSuck = fightDamage * (Mathf.Max(currentEnergy, 0f)) * 0.1f; * * enemyLife -= lifeSuck; * * life += (lifeSuck / 1.25f); * spikeCreature.RemoveLife(lifeSuck); * }*/ } if (currentEnergy > MIN_BRITH_ENERGY && birth > 0f && birthFrameCounter == 0) { birthFrameCounter++; } else if (birthFrameCounter < 6 && birthFrameCounter > 0) { birthFrameCounter++; } else if (birthFrameCounter == 6) { deltaEnergy -= 1.5f; giveBirth = true; birthFrameCounter = 0; } currentEnergy += deltaEnergy; if (currentEnergy <= 0f || life <= 0) { isAlive = false; } maturity += worldDeltaTime; //life -= ((worldDeltaTime /3) / Mathf.Pow((Mathf.Max(currentEnergy, 1f) / initialEnergy), 0.25f)); life -= ((worldDeltaTime / minLife) / Mathf.Pow(Mathf.Max(currentEnergy, 1f), lifeDecerase)); //life -= (worldDeltaTime / (5 + (Mathf.Max(currentEnergy, 0f))*2f)); }