Skip to content

Commit

Permalink
Remove more LINQ
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed Jan 13, 2024
1 parent e8a610f commit 1fb8aad
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions src/FactorioTools/OilField/Steps/AddElectricPoles.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using static Knapcode.FactorioTools.OilField.Helpers;

namespace Knapcode.FactorioTools.OilField;
Expand Down Expand Up @@ -627,24 +626,39 @@ private static void ConnectElectricPoles(Context context, ILocationDictionary<El

while (groups.Count > 1)
{
var closest = PointsToLines(electricPoles.Keys)
.Select(e => new
Endpoints? closest = null;
int closestDistanceSquared = default;
var lines = PointsToLines(electricPoles.Keys);
for (var i = 0; i < lines.Count; i++)
{
var endpoint = lines[i];
var groupA = groups.Single(g => g.Contains(endpoint.A));
var groupB = groups.Single(g => g.Contains(endpoint.B));
if (groupA == groupB)
{
Endpoints = e,
GroupA = groups.Single(g => g.Contains(e.A)),
GroupB = groups.Single(g => g.Contains(e.B)),
DistanceSquared = GetElectricPoleDistanceSquared(e.A, e.B, context.Options),
})
.Where(c => c.GroupA != c.GroupB)
.Where(c => c.DistanceSquared > context.Options.ElectricPoleWireReachSquared)
.MinBy(c => c.DistanceSquared);
continue;
}

var distanceSquared = GetElectricPoleDistanceSquared(endpoint.A, endpoint.B, context.Options);
if (distanceSquared <= context.Options.ElectricPoleWireReachSquared)
{
continue;
}

if (closest is null
|| distanceSquared < closestDistanceSquared)
{
closest = endpoint;
closestDistanceSquared = distanceSquared;
}
}

if (closest is null)
{
throw new FactorioToolsException("No closest electric pole could be found.");
}

AddSinglePoleForConnection(context, electricPoles, groups, Math.Sqrt(closest.DistanceSquared), closest.Endpoints);
AddSinglePoleForConnection(context, electricPoles, groups, Math.Sqrt(closestDistanceSquared), closest);
}
}

Expand Down Expand Up @@ -718,7 +732,26 @@ private static void AddSinglePoleForConnection(Context context, ILocationDiction
}

var center = AddElectricPole(context, electricPoles, selectedPoint);
var connectedGroups = groups.Where(g => center.Neighbors.Select(n => context.Grid.EntityToLocation[n]).Any(l => g.Contains(l))).ToList();
var connectedGroups = new List<ILocationSet>(groups.Count);
for (var i = 0; i < groups.Count; i++)
{
var group = groups[i];
var match = false;
foreach (var neighbor in center.Neighbors)
{
var location = context.Grid.EntityToLocation[neighbor];
if (group.Contains(location))
{
match = true;
break;
}
}

if (match)
{
connectedGroups.Add(group);
}
}

if (connectedGroups.Count == 0)
{
Expand Down Expand Up @@ -760,7 +793,13 @@ private static List<ILocationSet> GetElectricPoleGroups(Context context, ILocati
}
}

var group = entities.Select(e => context.Grid.EntityToLocation[e]).ToSet(context, allowEnumerate: true);
var group = context.GetLocationSet(allowEnumerate: true);
foreach (var entity in entities)
{
var location = context.Grid.EntityToLocation[entity];
group.Add(location);
}

remaining.ExceptWith(group);
groups.Add(group);
}
Expand Down

0 comments on commit 1fb8aad

Please sign in to comment.