public MapCore(MapCore other) { heightLimit = other.heightLimit; realNodes = new MapNode[other.realNodes.GetLength(0), other.realNodes.GetLength(1)]; foreach (var position in Enumerables.InRange2(other.Size)) { this[position] = other[position]; } }
// Generation. public override UnitSystemCore Generate(int seed, CancellationToken?cancellationToken = null) { var unitSystem = new UnitSystemCore(); int added = 0; cancellationToken?.ThrowIfCancellationRequested(); // Initialize random. var random = new System.Random(seed); // Count free nodes. int freeNodes = 0; foreach (var position in Enumerables.InRange2(Map.Size)) { if (Map[position].Type == MapNode.NodeType.Common) { ++freeNodes; } } if (freeNodes <= UnitCount) { foreach (var position in Enumerables.InRange2(Map.Size)) { if (Map[position].Type == MapNode.NodeType.Common) { unitSystem.AddUnit(BuildUnit(random, position, Map, ref added), position); } cancellationToken?.ThrowIfCancellationRequested(); } return(unitSystem); } cancellationToken?.ThrowIfCancellationRequested(); // Place units at free nodes. int toAdd = UnitCount; while (added < UnitCount) { Vector2Int position; do { position = new Vector2Int(random.Next(Map.Size.x), random.Next(Map.Size.y)); cancellationToken?.ThrowIfCancellationRequested(); } while (Map[position].Type != MapNode.NodeType.Common || unitSystem[position] != null); unitSystem.AddUnit(BuildUnit(random, position, Map, ref added), position); cancellationToken?.ThrowIfCancellationRequested(); } return(unitSystem); }
public override MapCore Generate(int seed, CancellationToken?cancellationToken = null) { cancellationToken?.ThrowIfCancellationRequested(); var map = new MapCore(Size, HeightLimit); foreach (var position in Enumerables.InRange2(Size)) { float noiseValue = noise.cellular2x2(0.05f * new float2(position.x, position.y)).x; float normalizedHeight = (noiseValue - 0.4f) / (1 - 0.4f); if (normalizedHeight >= 0) { float height = 2 * ShiftedErf(normalizedHeight); var node = new MapNode(MapNode.NodeType.Common, height); map[position] = node; } cancellationToken?.ThrowIfCancellationRequested(); } return(map); }
// Building methods. private async Task ReadNodes(MapPresentation map, CancellationToken cancellation) { if (map is null) { throw new ArgumentNullException(nameof(map)); } cancellation.ThrowIfCancellationRequested(); // Place nodes. await Task.Run(() => { nodes = new LayoutNode[map.Size.x, map.Size.y]; foreach (var position in Enumerables.InRange2(NodeCount)) { if (map[position].Type != MapNode.NodeType.Common) { continue; } nodes[position.x, position.y] = new LayoutNode(position); } cancellation.ThrowIfCancellationRequested(); }); }
public LayoutCore(LayoutCore other) { // Copy regions. regions = new List <LayoutRegion>(); var oldToNew = new Dictionary <LayoutRegion, LayoutRegion>(); foreach (var oldRegion in other.regions) { var newRegion = new LayoutRegion(oldRegion.Center); regions.Add(newRegion); oldToNew[oldRegion] = newRegion; } foreach (var oldRegion in regions) { var newRegion = oldToNew[oldRegion]; foreach (var pair in oldRegion.Adjacency) { var oldAdjacent = pair.Key; var newAdjacent = oldToNew[oldAdjacent]; newRegion.AddAdjacent(newAdjacent); } } // Copy nodes. nodes = new LayoutNode[other.NodeCount.x, other.NodeCount.y]; foreach (var position in Enumerables.InRange2(NodeCount)) { var oldNode = other.nodes[position.x, position.y]; if (oldNode is null) { continue; } nodes[position.x, position.y] = new LayoutNode(position); } foreach (var position in Enumerables.InRange2(NodeCount)) { var oldNode = other.nodes[position.x, position.y]; if (oldNode is null) { continue; } var newNode = nodes[position.x, position.y]; if (!(oldNode.Region is null)) { newNode.Region = oldToNew[oldNode.Region]; newNode.Region.AddNode(newNode); } foreach (var pair in oldNode.Reachable) { var oldReachable = pair.Key; var newReachable = nodes[oldReachable.Position.x, oldReachable.Position.y]; if (oldNode.Visible.ContainsKey(oldReachable)) { newNode.AddVisible(newReachable); } else { var distance = pair.Value; newNode.AddReachable(newReachable, distance); } } } }