Exposes the TilingPath code used by the map generator as a map editor tool to help automate the process of tiling sequences of templates together. No additional template stitching definitions are added as part of this commit. As such, the tool currently only supports the same auto tiling as the map generator does. This includes: - RA and CnC support. - Beaches, land cliffs, and roads. Support for additional mods and templates can be added in follow-ups by adding the necessary MultiBrush definitions. Related changes included in this commit to support this work include: - Add support for sparse EditorBlits. - Add support for MultiBrush to EditorBlitSource conversion. - Add support for EditorBlitSource previewing. - Adjust the "Road" types used in segmented MultiBrush definitions.
498 lines
12 KiB
C#
498 lines
12 KiB
C#
#region Copyright & License Information
|
|
/*
|
|
* Copyright (c) The OpenRA Developers and Contributors
|
|
* This file is part of OpenRA, which is free software. It is made
|
|
* available to you under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version. For more
|
|
* information, see COPYING.
|
|
*/
|
|
#endregion
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Linq;
|
|
using OpenRA.Graphics;
|
|
using OpenRA.Mods.Common.EditorBrushes;
|
|
using OpenRA.Mods.Common.MapGenerator;
|
|
using OpenRA.Mods.Common.Terrain;
|
|
using OpenRA.Support;
|
|
using OpenRA.Traits;
|
|
|
|
namespace OpenRA.Mods.Common.Traits
|
|
{
|
|
[TraitLocation(SystemActors.EditorWorld)]
|
|
public sealed class TilingPathToolInfo : TraitInfo, IEditorToolInfo
|
|
{
|
|
[FluentReference]
|
|
const string Label = "label-tool-tiling-path";
|
|
|
|
[Desc("The widget tree to open when the tool is selected.")]
|
|
const string PanelWidget = "TILING_PATH_TOOL_PANEL";
|
|
|
|
[Desc("The preferred defaults for the start type.")]
|
|
public readonly string[] DefaultStart = [];
|
|
[Desc("The preferred defaults for the inner type.")]
|
|
public readonly string[] DefaultInner = [];
|
|
[Desc("The preferred defaults for the end type.")]
|
|
public readonly string[] DefaultEnd = [];
|
|
|
|
public override object Create(ActorInitializer init)
|
|
{
|
|
return new TilingPathTool(init.Self, this);
|
|
}
|
|
|
|
string IEditorToolInfo.Label => Label;
|
|
string IEditorToolInfo.PanelWidget => PanelWidget;
|
|
}
|
|
|
|
public sealed class TilingPathTool : IRenderAnnotations, INotifyActorDisposing, IWorldLoaded
|
|
{
|
|
/// <summary>
|
|
/// Holds the shape of a path being planned out in the map editor.
|
|
/// </summary>
|
|
public sealed class PathPlan
|
|
{
|
|
public readonly int Start;
|
|
public readonly int End;
|
|
public readonly bool Loop;
|
|
public readonly ImmutableArray<CPos> Rallies;
|
|
public int AutoStart
|
|
{
|
|
get
|
|
{
|
|
if (Start != Direction.None)
|
|
{
|
|
return Start;
|
|
}
|
|
else
|
|
{
|
|
if (Rallies.Length >= 2)
|
|
return Direction.FromCVecRounding(Rallies[1] - Rallies[0]);
|
|
else
|
|
return Direction.None;
|
|
}
|
|
}
|
|
}
|
|
|
|
public int AutoEnd
|
|
{
|
|
get
|
|
{
|
|
if (End != Direction.None)
|
|
{
|
|
return End;
|
|
}
|
|
else if (Loop)
|
|
{
|
|
return AutoStart;
|
|
}
|
|
else
|
|
{
|
|
if (Rallies.Length >= 2)
|
|
return Direction.FromCVecRounding(Rallies[^1] - Rallies[^2]);
|
|
else
|
|
return Direction.None;
|
|
}
|
|
}
|
|
}
|
|
|
|
public CPos FirstPoint => Rallies[0];
|
|
public CPos LastPoint => Loop ? Rallies[0] : Rallies[^1];
|
|
|
|
PathPlan(int start, int end, bool loop, ImmutableArray<CPos> rallies)
|
|
{
|
|
if (rallies == null || rallies.Length == 0)
|
|
throw new ArgumentException("rallies must have at least one point");
|
|
|
|
Start = start;
|
|
End = end;
|
|
Loop = loop && rallies.Length >= 3;
|
|
Rallies = rallies;
|
|
}
|
|
|
|
/// <summary>Start a new path with a given first point.</summary>
|
|
public PathPlan(CPos first)
|
|
{
|
|
Start = Direction.None;
|
|
End = Direction.None;
|
|
Loop = false;
|
|
Rallies = [first];
|
|
}
|
|
|
|
/// <summary>Return a copy, modifying the start direction.</summary>
|
|
public PathPlan WithStart(int start)
|
|
{
|
|
return new PathPlan(start, End, Loop, Rallies);
|
|
}
|
|
|
|
/// <summary>Return a copy, modifying the end direction.</summary>
|
|
public PathPlan WithEnd(int end)
|
|
{
|
|
return new PathPlan(Start, end, Loop, Rallies);
|
|
}
|
|
|
|
/// <summary>Return a copy, modifying whether the path is looped.</summary>
|
|
public PathPlan WithLoop(bool loop)
|
|
{
|
|
return new PathPlan(Start, End, loop, Rallies);
|
|
}
|
|
|
|
/// <summary>Return a copy, with a rally appended.</summary>
|
|
public PathPlan WithRallyAppended(CPos cpos)
|
|
{
|
|
return new PathPlan(Start, Direction.None, Loop, [.. Rallies, cpos]);
|
|
}
|
|
|
|
/// <summary>Return a copy, with the rally at index removed.</summary>
|
|
public PathPlan WithRallyRemoved(int index)
|
|
{
|
|
if (Rallies.Length == 1)
|
|
return null;
|
|
|
|
return new PathPlan(
|
|
index != 0 ? Start : Direction.None,
|
|
index != Rallies.Length - 1 ? End : Direction.None,
|
|
Loop,
|
|
[.. Rallies[..index], .. Rallies[(index + 1)..]]);
|
|
}
|
|
|
|
/// <summary>Return a copy, with the rally at index replace/moved.</summary>
|
|
public PathPlan WithRallyReplaced(int index, CPos cpos)
|
|
{
|
|
return new PathPlan(Start, End, Loop, [.. Rallies[..index], cpos, .. Rallies[(index + 1)..]]);
|
|
}
|
|
|
|
/// <summary>Return a copy, with a rally inserted before index.</summary>
|
|
public PathPlan WithRallyInserted(int index, CPos cpos)
|
|
{
|
|
return new PathPlan(Start, End, Loop, [.. Rallies[..index], cpos, .. Rallies[index..]]);
|
|
}
|
|
|
|
/// <summary>Return a copy, with everything translated by offset.</summary>
|
|
public PathPlan Moved(CVec offset)
|
|
{
|
|
var rallies = Rallies.Select(r => r + offset).ToImmutableArray();
|
|
return new PathPlan(Start, End, Loop, rallies);
|
|
}
|
|
|
|
/// <summary>Return a copy, with rallies reversed and directions swapped.</summary>
|
|
public PathPlan Reversed()
|
|
{
|
|
if (Loop)
|
|
{
|
|
var reversedStart = End;
|
|
var reversedEnd = Start;
|
|
if (Start != Direction.None && End == Direction.None)
|
|
{
|
|
reversedStart = AutoEnd;
|
|
reversedEnd = Direction.None;
|
|
}
|
|
|
|
return new PathPlan(
|
|
Direction.Reverse(reversedStart),
|
|
Direction.Reverse(reversedEnd),
|
|
Loop,
|
|
Rallies.Skip(1).Append(Rallies[0]).Reverse().ToImmutableArray());
|
|
}
|
|
else
|
|
{
|
|
return new PathPlan(
|
|
Direction.Reverse(End),
|
|
Direction.Reverse(Start),
|
|
Loop,
|
|
Rallies.Reverse().ToImmutableArray());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert the rally points into a sequence of unit-space CPos points, suitable for
|
|
/// processing with TilingPath.
|
|
/// </summary>
|
|
public CPos[] Points()
|
|
{
|
|
return PointsWithRallyIndex().Select(pair => pair.CPos).ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert the rally points into a sequence of unit-space CPos points and their
|
|
/// associated latest rally index. For loops, the last rally index is the number of the
|
|
/// rallies.
|
|
/// </summary>
|
|
public (CPos CPos, int RallyIndex)[] PointsWithRallyIndex()
|
|
{
|
|
if (Rallies.Length == 1)
|
|
return [(Rallies[0], 0)];
|
|
|
|
var points = new List<(CPos CPos, int RallyIndex)>();
|
|
var cpos = Rallies[0];
|
|
points.Add((cpos, 0));
|
|
var inertia = Direction.ToCVec(AutoStart);
|
|
if (inertia.X != 0 && inertia.Y != 0)
|
|
inertia = new CVec(inertia.X, 0);
|
|
|
|
void AddPointsUpTo(CPos target, int i)
|
|
{
|
|
if (cpos == target)
|
|
throw new InvalidOperationException("there are duplicate rally points");
|
|
|
|
var offset = target - cpos;
|
|
var xStep = Math.Sign(offset.X);
|
|
var yStep = Math.Sign(offset.Y);
|
|
|
|
var axisAligned = xStep == 0 || yStep == 0;
|
|
|
|
if (axisAligned)
|
|
{
|
|
while (cpos != target)
|
|
{
|
|
inertia = new CVec(xStep, yStep);
|
|
cpos += inertia;
|
|
points.Add((cpos, i));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var xUnderModulo = Math.Abs(offset.Y);
|
|
var yUnderModulo = Math.Abs(offset.X);
|
|
|
|
// Technically, these range from 0 inclusive to modulo inclusive!
|
|
var xModulo = xUnderModulo * 2;
|
|
var yModulo = yUnderModulo * 2;
|
|
|
|
if (xUnderModulo < yUnderModulo)
|
|
inertia = new CVec(xStep, 0);
|
|
else if (yUnderModulo > xUnderModulo)
|
|
inertia = new CVec(0, yStep);
|
|
else
|
|
inertia =
|
|
Direction.ToCVec(
|
|
Direction.FromCVecNonDiagonal(
|
|
inertia + new CVec(xStep * 2, yStep * 2)));
|
|
|
|
while (cpos != target)
|
|
{
|
|
if (xUnderModulo < yUnderModulo)
|
|
{
|
|
yUnderModulo -= xUnderModulo;
|
|
xUnderModulo = xModulo;
|
|
inertia = new CVec(xStep, 0);
|
|
}
|
|
else if (xUnderModulo > yUnderModulo)
|
|
{
|
|
xUnderModulo -= yUnderModulo;
|
|
yUnderModulo = yModulo;
|
|
inertia = new CVec(0, yStep);
|
|
}
|
|
else if (inertia.X != 0)
|
|
{
|
|
xUnderModulo = xModulo;
|
|
yUnderModulo = 0;
|
|
}
|
|
else
|
|
{
|
|
yUnderModulo = yModulo;
|
|
xUnderModulo = 0;
|
|
}
|
|
|
|
cpos += inertia;
|
|
points.Add((cpos, i));
|
|
}
|
|
}
|
|
}
|
|
|
|
for (var i = 1; i < Rallies.Length; i++)
|
|
AddPointsUpTo(Rallies[i], i);
|
|
|
|
if (Loop)
|
|
AddPointsUpTo(Rallies[0], Rallies.Length);
|
|
|
|
return points.ToArray();
|
|
}
|
|
}
|
|
|
|
/// <summary>Whether the TilingPathTool can be used.</summary>
|
|
public readonly bool Available;
|
|
|
|
public readonly World World;
|
|
public WorldRenderer WorldRenderer = null;
|
|
public readonly ImmutableArray<MultiBrush> SegmentedBrushes;
|
|
public readonly ImmutableArray<string> StartTypes;
|
|
public readonly ImmutableArray<string> InnerTypes;
|
|
public readonly ImmutableArray<string> EndTypes;
|
|
public PathPlan Plan { get; private set; } = null;
|
|
public string StartType { get; private set; } = null;
|
|
public string InnerType { get; private set; } = null;
|
|
public string EndType { get; private set; } = null;
|
|
public bool ClosedLoops { get; private set; } = true;
|
|
public int RandomSeed { get; private set; } = 0;
|
|
public int MaxDeviation { get; private set; } = 5;
|
|
public EditorBlitSource? EditorBlitSource { get; private set; } = null;
|
|
|
|
bool disposed;
|
|
|
|
public TilingPathTool(Actor self, TilingPathToolInfo info)
|
|
{
|
|
World = self.World;
|
|
|
|
var templatedTerrainInfo = World.Map.Rules.TerrainInfo as ITemplatedTerrainInfo;
|
|
SegmentedBrushes =
|
|
templatedTerrainInfo.MultiBrushCollections.Keys
|
|
.Order()
|
|
.SelectMany(name => MultiBrush.LoadCollection(World.Map, name))
|
|
.Where(multiBrush => multiBrush.Segment != null)
|
|
.ToImmutableArray();
|
|
|
|
Available = SegmentedBrushes.Length > 0;
|
|
|
|
if (!Available)
|
|
return;
|
|
|
|
StartTypes = SegmentedBrushes
|
|
.Where(b => b.Segment != null)
|
|
.Select(b => string.Join(".", b.Segment.Start.Split('.').SkipLast(1)))
|
|
.Distinct()
|
|
.Order()
|
|
.ToImmutableArray();
|
|
|
|
InnerTypes = SegmentedBrushes
|
|
.Where(b => b.Segment != null)
|
|
.Select(b => b.Segment.Inner.Split('.')[0])
|
|
.Distinct()
|
|
.Order()
|
|
.ToImmutableArray();
|
|
|
|
EndTypes = SegmentedBrushes
|
|
.Where(b => b.Segment != null)
|
|
.Select(b => string.Join(".", b.Segment.End.Split('.').SkipLast(1)))
|
|
.Distinct()
|
|
.Order()
|
|
.ToImmutableArray();
|
|
|
|
StartType = info.DefaultStart
|
|
.FirstOrDefault(StartTypes.Contains, StartTypes[0]);
|
|
InnerType = info.DefaultInner
|
|
.FirstOrDefault(InnerTypes.Contains, InnerTypes[0]);
|
|
EndType = info.DefaultEnd
|
|
.FirstOrDefault(EndTypes.Contains, EndTypes[0]);
|
|
}
|
|
|
|
public void WorldLoaded(World w, WorldRenderer wr)
|
|
{
|
|
WorldRenderer = wr;
|
|
}
|
|
|
|
void INotifyActorDisposing.Disposing(Actor self)
|
|
{
|
|
if (disposed)
|
|
return;
|
|
|
|
disposed = true;
|
|
}
|
|
|
|
IEnumerable<IRenderable> IRenderAnnotations.RenderAnnotations(Actor self, WorldRenderer wr)
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
bool IRenderAnnotations.SpatiallyPartitionable => false;
|
|
|
|
EditorBlitSource? TilePlan(PathPlan plan)
|
|
{
|
|
if (WorldRenderer == null)
|
|
return null;
|
|
|
|
if (plan == null || plan.Rallies.Length < 2)
|
|
return null;
|
|
|
|
var points = plan.Points();
|
|
if (points == null)
|
|
return null;
|
|
|
|
(string Start, string End)[] terminalTypes = [(StartType, EndType)];
|
|
if (ClosedLoops && plan.Loop)
|
|
{
|
|
terminalTypes = StartTypes.Concat(EndTypes)
|
|
.Distinct()
|
|
.Where(t => t.Split('.')[0] == InnerType)
|
|
.Select(t => (t, t))
|
|
.ToArray();
|
|
}
|
|
|
|
var map = World.Map;
|
|
var permittedTemplates =
|
|
TilingPath.PermittedSegments.FromTypes(
|
|
SegmentedBrushes,
|
|
terminalTypes.Select(t => t.Start),
|
|
[InnerType],
|
|
terminalTypes.Select(t => t.End));
|
|
|
|
foreach (var (startType, endType) in terminalTypes)
|
|
{
|
|
var tilingPath = new TilingPath(
|
|
map,
|
|
points,
|
|
MaxDeviation,
|
|
startType,
|
|
endType,
|
|
permittedTemplates);
|
|
tilingPath.Start.Direction = plan.AutoStart;
|
|
tilingPath.End.Direction = plan.AutoEnd;
|
|
var result = tilingPath.Tile(new MersenneTwister(RandomSeed));
|
|
if (result != null)
|
|
return result.ToEditorBlitSource(WorldRenderer);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
EditorBlitSource = TilePlan(Plan);
|
|
}
|
|
|
|
public void SetPlan(PathPlan value)
|
|
{
|
|
Plan = value;
|
|
Update();
|
|
}
|
|
|
|
public void SetStartType(string value)
|
|
{
|
|
StartType = value;
|
|
Update();
|
|
}
|
|
|
|
public void SetInnerType(string value)
|
|
{
|
|
InnerType = value;
|
|
Update();
|
|
}
|
|
|
|
public void SetEndType(string value)
|
|
{
|
|
EndType = value;
|
|
Update();
|
|
}
|
|
|
|
public void SetClosedLoops(bool value)
|
|
{
|
|
ClosedLoops = value;
|
|
Update();
|
|
}
|
|
|
|
public void SetRandomSeed(int value)
|
|
{
|
|
RandomSeed = value;
|
|
Update();
|
|
}
|
|
|
|
public void SetMaxDeviation(int value)
|
|
{
|
|
MaxDeviation = value;
|
|
Update();
|
|
}
|
|
}
|
|
}
|