public static IntVec3 BestCarnivalSetupPosition(IntVec3 initPos, Map map) { IntVec3 averageColPos; if (!(averageColPos = CellsUtil.ApproxClosestColonistBuilding(map, initPos, ThingDefOf.Door)).IsValid && !(averageColPos = CellsUtil.ApproxClosestColonistBuilding(map, initPos, ThingDefOf.Wall)).IsValid) { averageColPos = AverageColPos; } else { averageColPos = averageColPos.AverageWith(AverageColPos); } var distSqrdToColony = initPos.DistanceToSquared(averageColPos); if (Prefs.DevMode) { Log.Message("[Carnivale] setupSpot: initPos=" + initPos + ", averageColPos=" + averageColPos + ", distSqrdToColony=" + distSqrdToColony); } var result = initPos; if (!TryCarnivalSetupPosition_Triangular(initPos, averageColPos, distSqrdToColony, map, out result)) { if (Prefs.DevMode) { Log.Warning("\t[Carnivale] setupSpot: triangular algorithm failed. Using old random iteration algorithm."); } TryCarnivalSetupPosition_Random(initPos, averageColPos, 7, map, out result); } if (Prefs.DevMode) { if (result == initPos) { Log.Error(string.Concat(new object[] { "[Carnivale] Could not find carnival setup spot from ", initPos, ", expect more errors. Using ", initPos })); } Log.Message("[Carnivale] setupSpot: final pass: "******". distToColony=" + result.DistanceTo(averageColPos)); map.debugDrawer.FlashCell(result, 0.5f, "Setup Spot"); } return(result); }
public static IntVec3 PreCalculateBannerCell() { if (!Info.Active) { Log.Error("[Carnivale] Tried to perform a cell calculation while current carnival is inactive."); return(IntVec3.Invalid); } var map = Info.map; var setupCentre = Info.setupCentre; var baseRadius = Info.baseRadius; var minDistToCentre = baseRadius / 2; var maxDistToCentre = baseRadius + 3f; var minDistSqrdToCentre = (int)(minDistToCentre * minDistToCentre * 2); var maxDistSqrdToCentre = (int)(maxDistToCentre * maxDistToCentre * 2); IntVec3 colonistPos; if (!(colonistPos = CellsUtil.ApproxClosestColonistBuilding(map, setupCentre, ThingDefOf.Door)).IsValid) { colonistPos = CellsUtil.AverageColonistPosition(map); } else { colonistPos = colonistPos.AverageWith(CellsUtil.AverageColonistPosition(map)); } // Initial pass var closestCell = CellRect.CenteredOn(setupCentre, (int)maxDistToCentre).EdgeCells.ClosestCellTo(colonistPos, map); if (Prefs.DevMode) { Log.Message("[Carnivale] bannerCell initial pass: "******"Road")) { adjustedCell += IntVec3.East * 2; } else if ((adjustedCell + IntVec3.West * 2).GetTerrain(map).HasTag("Road")) { adjustedCell += IntVec3.West * 2; } else { adjustedCell += IntVec3.North; } closestCell = adjustedCell; if (Prefs.DevMode) { Log.Message("\t[Carnivale] bannerCell road pass: "******". distSqrdToCentre=" + dist + ", minDistSqrdToCentre=" + minDistSqrdToCentre + ", maxDistSqrdToCentre=" + maxDistSqrdToCentre); } break; } else { if (Prefs.DevMode) { Log.Warning("\t[Carnivale] bannerCell road pass failure: " + rcell + ". distSqrdToCentre=" + dist + ", minDistSqrdToCentre=" + minDistSqrdToCentre + ", maxDistSqrdToCentre=" + maxDistSqrdToCentre); } } } // line of sight pass if (!GenSight.LineOfSight(setupCentre, closestCell, map)) { Func <IntVec3, float> weightLoSSetupCentre = c => 2f / (setupCentre.CountObstructingCellsTo(c, map) + 1f); Func <IntVec3, float> weightLoSColony = c => 1f / (c.CountObstructingCellsTo(colonistPos, map) + 1f); Func <IntVec3, float> weightBest = c => (weightLoSSetupCentre(c) == 2f ? 2f : 0f) + (weightLoSColony(c) == 1f ? 1f : 0f); var candidateCells = CellTriangle .FromTarget(setupCentre, closestCell, 45f, maxDistToCentre) .Where(c => c.InBounds(map) && c.DistanceToSquared(setupCentre) >= minDistSqrdToCentre); try { closestCell = candidateCells.MaxBy(weightBest); if (Prefs.DevMode) { Log.Message("\t[Carnivale] bannerCell optimal LoS pass: "******"\t[Carnivale] bannerCell sub-optimal LoS pass: "******"\t[Carnivale] bannerCell failed LoS passes. Is candidateCells empty? Leaving it at: " + closestCell); } } } // Mountain proximity pass var attempts = 0; IntVec3 nearestMineable; while (attempts < 10 && closestCell.DistanceSquaredToNearestMineable(map, 12, out nearestMineable) <= 36) { closestCell = CellRect.CenteredOn(closestCell, 5).FurthestCellFrom(nearestMineable); attempts++; if (Prefs.DevMode) { Log.Message("\t[Carnivale] bannerCell mountain proximity pass #" + attempts + ": " + closestCell); } } if (attempts == 10 && Prefs.DevMode) { Log.Warning("\t[Carnivale] bannerCell mountain proximity passes took too many tries. Leaving it at: " + closestCell); } // End passes if (Prefs.DevMode) { Log.Message("[Carnivale] bannerCell pre-buildability pass: " + closestCell); } return(closestCell); }