/// <summary> /// Figures out how many steps of the path between the start bases the two Xel'Naga towers cover. /// If the towers cover more than one base, their worth is halved. If they cover a start base, their worth is cut in four (these two stack). /// </summary> /// <returns> A value between 0.0 and 1.0 multiplied by significance based on how many steps that are covered. </returns> private double XelNagaPlacement() { // If no Xel'Naga tower are found, return 0. if (this.xelNagaPosition2 == null) { return(0d); } double stepsCovered2 = this.pathBetweenStartBases.Count( step => MapHelper.CloseTo(step, this.xelNagaPosition2, this.mfo.DistanceToXelNaga)); double max2 = this.mfo.StepsInXelNagaRangeMax; double min2 = this.mfo.StepsInXelNagaRangeMin; var actual2 = (stepsCovered2 > max2) ? max2 - (stepsCovered2 - max2) : stepsCovered2; if (actual2 < min2) { actual2 = min2; } var normalized2 = (actual2 - min2) / (max2 - min2); var basesInVision2 = this.bases.Count(@base => MapHelper.CloseTo(this.xelNagaPosition2, @base)); var startBaseInVision2 = MapHelper.CloseTo(this.xelNagaPosition2, this.startBasePosition1) || MapHelper.CloseTo(this.xelNagaPosition2, this.startBasePosition2); if (basesInVision2 >= 2) { normalized2 /= 2; } if (startBaseInVision2) { normalized2 /= 4; } // If only one Xel'Naga tower is found, return the significance for just that one, but halved. if (this.xelNagaPosition1 == null) { return((normalized2 * this.mfo.XelNagaPlacementSignificance) / 2d); } double stepsCovered1 = this.pathBetweenStartBases.Count( step => MapHelper.CloseTo(step, this.xelNagaPosition1, this.mfo.DistanceToXelNaga)); double max1 = this.mfo.StepsInXelNagaRangeMax; double min1 = this.mfo.StepsInXelNagaRangeMin; var actual1 = (stepsCovered1 > max1) ? max1 - (stepsCovered1 - max1) : stepsCovered1; if (actual1 < min1) { actual1 = min1; } var normalized1 = (actual1 - min1) / (max1 - min1); var basesInVision1 = this.bases.Count(@base => MapHelper.CloseTo(this.xelNagaPosition1, @base)); var startBaseInVision1 = MapHelper.CloseTo(this.xelNagaPosition1, this.startBasePosition1) || MapHelper.CloseTo(this.xelNagaPosition1, this.startBasePosition2); if (basesInVision1 >= 2) { normalized1 /= 2; } if (startBaseInVision1) { normalized1 /= 4; } return(((normalized1 + normalized2) / 2d) * this.mfo.XelNagaPlacementSignificance); }
/// <summary> /// Calculates the fitness for the map. /// </summary> /// <returns> A double representing the fitness of the map. The higher, the better. </returns> public double CalculateFitness() { this.bases = new List <Position>(); // Find a startbase. No need to find all start bases, as the other base should be a complete mirror of this base. for (var tempY = this.ySize - 1; tempY >= 0; tempY--) { for (var tempX = 0; tempX < this.xSize; tempX++) { // Find start base 1 if (this.map.MapItems[tempX, tempY] == Enums.Item.StartBase && this.startBasePosition2 == null) { this.startBasePosition2 = new Tuple <int, int>(tempX + 2, tempY - 2); } // Find start base 2 if (this.map.MapItems[tempX, tempY] == Enums.Item.StartBase && this.startBasePosition1 == null && this.startBasePosition2 != null) { if (Math.Abs(tempX - this.startBasePosition2.Item1) > 3 || Math.Abs(tempY - this.startBasePosition2.Item2) > 3) { this.startBasePosition1 = new Position(tempX + 2, tempY - 2); } } // Check for highest level if ((this.map.HeightLevels[tempX, tempY] == Enums.HeightLevel.Height1 || this.map.HeightLevels[tempX, tempY] == Enums.HeightLevel.Height2) && this.map.HeightLevels[tempX, tempY] > this.heighestLevel) { this.heighestLevel = this.map.HeightLevels[tempX, tempY]; } var tempPos = new Position(tempX, tempY); // Check if the area is a base if (this.map.MapItems[tempX, tempY] == Enums.Item.Base && !MapHelper.CloseToAny(tempPos, this.bases, 5)) { this.bases.Add(new Position(tempX + 2, tempY - 2)); } // Save the first XelNaga tower found if (this.map.MapItems[tempX, tempY] == Enums.Item.XelNagaTower && this.xelNagaPosition2 == null) { this.xelNagaPosition2 = tempPos; } // Save the second XelNaga tower found if (this.map.MapItems[tempX, tempY] == Enums.Item.XelNagaTower && this.xelNagaPosition2 != null && this.xelNagaPosition1 == null && !MapHelper.CloseTo(tempPos, this.xelNagaPosition2, 4)) { this.xelNagaPosition1 = tempPos; } } } // If no start bases are found, the map is not feasible and running fitness calculations is not worth it. if (this.startBasePosition1 == null || this.startBasePosition2 == null) { this.FitnessValues = new MapFitnessValues( -this.mfo.MaxTotalFitness, -this.mfo.MaxTotalFitness, -this.mfo.MaxTotalFitness, -this.mfo.MaxTotalFitness, -this.mfo.MaxTotalFitness, -this.mfo.MaxTotalFitness, -this.mfo.MaxTotalFitness, -this.mfo.MaxTotalFitness, -this.mfo.MaxTotalFitness, -this.mfo.MaxTotalFitness, -this.mfo.MaxTotalFitness); return(this.FitnessValues.TotalFitness); } this.pathBetweenStartBases = this.mapPathfinding.FindPathFromTo( this.startBasePosition1, this.startBasePosition2, this.mfo.PathfindingIgnoreDestructibleRocks); var baseSpace = this.BaseSpace(); var baseHeight = this.BaseHeightLevel(); var pathBetweenStartBasesFitness = this.PathBetweenStartBases(); var newHeightReached = this.NewHeightReached(); var distanceToNaturalExpansion = this.DistanceToNaturalExpansion(); var distanceToNonNaturalExpansions = this.DistanceToNonNaturalExpansions(); var expansionsAvailable = this.ExpansionsAvailable(); var chokePoints = this.ChokePoints(); var xelNagaPlacement = this.XelNagaPlacement(); var startBaseOpeness = this.StartBaseOpeness(); var baseOpeness = this.BaseOpeness(); this.FitnessValues = new MapFitnessValues( baseSpace, baseHeight, pathBetweenStartBasesFitness, newHeightReached, distanceToNaturalExpansion, distanceToNonNaturalExpansions, expansionsAvailable, chokePoints, xelNagaPlacement, startBaseOpeness, baseOpeness); return(this.FitnessValues.TotalFitness); }