private double timeCounter; // use this for fadetime of effects #endregion Fields #region Constructors public FoodEffect(Ant myAnt, Vector2 foodPosition) { this.ant = myAnt; this.foodPosition = foodPosition; }
// give birth to an ant public Ant(Ant dad, Ant mom, Random rng, Entity[,] grid) { bool foundSpawn = false; int xMin = Math.Min((int)dad.position.X, (int)mom.position.X); int xMax = Math.Max((int)dad.position.X, (int)mom.position.X); int yMin = Math.Min((int)dad.position.Y, (int)mom.position.Y); int yMax = Math.Max((int)dad.position.Y, (int)mom.position.Y); for (int i = (int)xMin - 1; i < grid.GetLength(0) && i <= xMax + 1 && i >= 0; i++) { for (int j = (int)yMin - 1; j < grid.GetLength(1) && j <= yMax + 1 && j >= 0; j++) { if (grid[i, j] == null) { position.X = i; position.Y = j; foundSpawn = true; break; } } if (foundSpawn) { break; } } // only add it to the grid if you found a proper spot if (foundSpawn) { attributes = new Dictionary <string, double>(); foreach (KeyValuePair <string, double> kvp in mom.attributes) { // chance of totally random mutation if (rng.NextDouble() <= EXTREME_MUTATION_CHANCE) { double range = Statistics.attributeRange[kvp.Key].Y - Statistics.attributeRange[kvp.Key].X; attributes[kvp.Key] = range * rng.NextDouble() + Statistics.attributeRange[kvp.Key].X; //old code //attributes[kvp.Key] = (dad.attributes[kvp.Key] + mom.attributes[kvp.Key]) * (rng.NextDouble() - RANGE_OFFSET) * (dad.attributes["mutationFactor"] + mom.attributes["mutationFactor"]) / 2.0; } else { attributes[kvp.Key] = (dad.attributes[kvp.Key] + mom.attributes[kvp.Key]) / 2.0 + (rng.NextDouble() - RANGE_OFFSET) * ((dad.attributes["mutationFactor"] + mom.attributes["mutationFactor"]) / 2.0) * (Statistics.attributeRange[kvp.Key].Y - Statistics.attributeRange[kvp.Key].X); if (attributes[kvp.Key] > Statistics.attributeRange[kvp.Key].Y) { attributes[kvp.Key] = Statistics.attributeRange[kvp.Key].Y; } else if (attributes[kvp.Key] < Statistics.attributeRange[kvp.Key].X) { attributes[kvp.Key] = Statistics.attributeRange[kvp.Key].X; } } Statistics.currentSum[kvp.Key] += attributes[kvp.Key]; } Statistics.birthEffects.Add(new BirthEffect(mom, dad, this)); if (Statistics.totalNumberOfAnts > Statistics.largestPopulation) { Statistics.largestPopulation = Statistics.totalNumberOfAnts; } Statistics.totalNumberOfAnts++; dad.children.Add(this); mom.children.Add(this); dad.numChildren++; mom.numChildren++; if (dad.numChildren > Statistics.largestNumberOfChildren) { Statistics.largestNumberOfChildren = dad.numChildren; } if (mom.numChildren > Statistics.largestNumberOfChildren) { Statistics.largestNumberOfChildren = mom.numChildren; } int generationMax = dad.generation; if (mom.generation > generationMax) { generationMax = mom.generation; } this.generation = generationMax + 1; if (this.generation > Statistics.longestGenerationLived) { Statistics.longestGenerationLived = this.generation; } grid[(int)position.X, (int)position.Y] = this; isObstacle = true; diffusionGrid = new double[grid.GetLength(0), grid.GetLength(1)]; lastSeen = new int[grid.GetLength(0), grid.GetLength(1)]; } }
public void Update(Entity[,] grid, GameTime gameTime, Random rng) { // update health every second if (gameTime.TotalGameTime.TotalSeconds - timeOfLastUpdate > 1) { timeOfLastUpdate = gameTime.TotalGameTime.TotalSeconds; health -= STANDING_COST; } int curTime = (int) gameTime.TotalGameTime.TotalSeconds; Vector2 newPosition = position; foreach (char c in Entity.Directions) { newPosition = directionTranslate(c); // give birth to ant sometimes if (newPosition.Y >= 0 && newPosition.Y < grid.GetLength(1) && newPosition.X >= 0 && newPosition.X < grid.GetLength(0) && grid[(int)newPosition.X, (int)newPosition.Y] is Ant) { Ant other = (Ant) grid[(int)newPosition.X, (int)newPosition.Y]; if (curTime - timeOfLastChild >= attributes["childbirthCooldown"] && curTime - other.timeOfLastChild >= other.attributes["childbirthCooldown"] && curTime - other.timeOfLastChild >= MINIMUM_CHILDBIRTH_TIME && curTime - timeOfLastChild >= MINIMUM_CHILDBIRTH_TIME) { if (rng.NextDouble() <= attributes["childbirthPercentage"] / 100) { Ant child = new Ant(this, other, rng, grid); child.sprite = other.sprite; timeOfLastChild = curTime; other.timeOfLastChild = curTime; // health of parents go into raising the child child.health = other.attributes["childbirthCost"] + attributes["childbirthCost"]; other.health -= 1.5*other.attributes["childbirthCost"]; health -= 1.5*attributes["childbirthCost"]; } } } } // punish ants for having high eyesight, strength, etc health -= attributes["eyesightRange"] * .0001; // death if (health <= 0 || moves >= attributes["oldestPossibleAge"]) { // kill this ant if it has low health Die(grid); } }
// give birth to an ant public Ant(Ant dad, Ant mom, Random rng, Entity[,] grid) { bool foundSpawn = false; int xMin = Math.Min((int)dad.position.X, (int)mom.position.X); int xMax = Math.Max((int)dad.position.X, (int)mom.position.X); int yMin = Math.Min((int)dad.position.Y, (int)mom.position.Y); int yMax = Math.Max((int)dad.position.Y, (int)mom.position.Y); for (int i = (int)xMin - 1; i < grid.GetLength(0) && i <= xMax + 1 && i >= 0; i++) { for (int j = (int)yMin - 1; j < grid.GetLength(1) && j <= yMax+1 && j >= 0; j++) { if (grid[i, j] == null) { position.X = i; position.Y = j; foundSpawn = true; break; } } if (foundSpawn) break; } // only add it to the grid if you found a proper spot if (foundSpawn) { attributes = new Dictionary<string, double>(); foreach (KeyValuePair<string, double> kvp in mom.attributes) { // chance of totally random mutation if (rng.NextDouble() <= EXTREME_MUTATION_CHANCE) { double range = Statistics.attributeRange[kvp.Key].Y - Statistics.attributeRange[kvp.Key].X; attributes[kvp.Key] = range * rng.NextDouble() + Statistics.attributeRange[kvp.Key].X; //old code //attributes[kvp.Key] = (dad.attributes[kvp.Key] + mom.attributes[kvp.Key]) * (rng.NextDouble() - RANGE_OFFSET) * (dad.attributes["mutationFactor"] + mom.attributes["mutationFactor"]) / 2.0; } else { attributes[kvp.Key] = (dad.attributes[kvp.Key] + mom.attributes[kvp.Key]) / 2.0 + (rng.NextDouble() - RANGE_OFFSET) * ((dad.attributes["mutationFactor"] + mom.attributes["mutationFactor"]) / 2.0) * (Statistics.attributeRange[kvp.Key].Y - Statistics.attributeRange[kvp.Key].X); if (attributes[kvp.Key] > Statistics.attributeRange[kvp.Key].Y) { attributes[kvp.Key] = Statistics.attributeRange[kvp.Key].Y; } else if (attributes[kvp.Key] < Statistics.attributeRange[kvp.Key].X) { attributes[kvp.Key] = Statistics.attributeRange[kvp.Key].X; } } Statistics.currentSum[kvp.Key] += attributes[kvp.Key]; } Statistics.birthEffects.Add(new BirthEffect(mom, dad, this)); if (Statistics.totalNumberOfAnts > Statistics.largestPopulation) { Statistics.largestPopulation = Statistics.totalNumberOfAnts; } Statistics.totalNumberOfAnts++; dad.children.Add(this); mom.children.Add(this); dad.numChildren++; mom.numChildren++; if (dad.numChildren > Statistics.largestNumberOfChildren) { Statistics.largestNumberOfChildren = dad.numChildren; } if (mom.numChildren > Statistics.largestNumberOfChildren) { Statistics.largestNumberOfChildren = mom.numChildren; } int generationMax = dad.generation; if (mom.generation > generationMax) generationMax = mom.generation; this.generation = generationMax + 1; if (this.generation > Statistics.longestGenerationLived) { Statistics.longestGenerationLived = this.generation; } grid[(int)position.X, (int)position.Y] = this; isObstacle = true; diffusionGrid = new double[grid.GetLength(0), grid.GetLength(1)]; lastSeen = new int[grid.GetLength(0), grid.GetLength(1)]; } }
public bool isParentOf(Ant a) { foreach (Ant ant in children) { if (a == ant) return true; } return false; }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); antSprite = Content.Load<Texture2D>("antRealistic"); antSpriteAlt = Content.Load<Texture2D>("antTransparent2"); foodSprite = Content.Load<Texture2D>("food"); backgroundSprite = Content.Load<Texture2D>("greenNoise"); cursorSprite = Content.Load<Texture2D>("cursorSheet2"); font = Content.Load<SpriteFont>("font"); statMenuFont = Content.Load<SpriteFont>("statMenuFont"); // initialize foreach (KeyValuePair<string, double> kvp in attributes) { Statistics.firstAverages[kvp.Key] = 0; Statistics.currentSum[kvp.Key] = 0; } for (int i = 0; i < STARTING_ANTS; i++) { Vector2 pos = new Vector2((int) rng.Next(GRID_WIDTH), (int) rng.Next(GRID_HEIGHT)); // half of the starting ants are mutants, half are not Ant ant = new Ant(attributes, rng, grid, i%2==0); grid[(int)pos.X, (int)pos.Y] = ant; foreach (KeyValuePair<string, double> kvp in attributes) { Statistics.firstAverages[kvp.Key] += ant.Attributes()[kvp.Key] / STARTING_ANTS; } grid[(int)pos.X, (int)pos.Y].SetPosition(pos); grid[(int)pos.X, (int)pos.Y].SetSprite(antSprite); } string trunc = ""; for (int i = 0; i < TRUNCATED_STRING_LIMIT; i++) { trunc += "a"; } AVERAGES_BAR_OFFSET_WIDTH = (int)statMenuFont.MeasureString(trunc).X; }
private double timeCounter = 0; // use this for fadetime of effects public BirthEffect(Ant myMom, Ant myDad, Ant myBaby) { mom = myMom; dad = myDad; baby = myBaby; }
private double timeCounter = 0; // use this for fadetime of effects #endregion Fields #region Constructors public BirthEffect(Ant myMom, Ant myDad, Ant myBaby) { mom = myMom; dad = myDad; baby = myBaby; }
private double timeCounter; // use this for fadetime of effects public FoodEffect(Ant myAnt, Vector2 foodPosition) { this.ant = myAnt; this.foodPosition = foodPosition; }