Add AppearsOnMapPreview

Allows preplaced actors to be rendered on map preview when saving the
map.

Also removes requirements for ResourceLayer in OpenRA.Game and moves it
to OpenRA.Mods.Common.
This commit is contained in:
Mustafa Alperen Seki
2018-10-25 21:15:56 +03:00
committed by reaperrr
parent ea4f24d0b7
commit 9fef2c01ec
8 changed files with 109 additions and 15 deletions

View File

@@ -646,8 +646,24 @@ namespace OpenRA
public byte[] SavePreview() public byte[] SavePreview()
{ {
var tileset = Rules.TileSet; var tileset = Rules.TileSet;
var resources = Rules.Actors["world"].TraitInfos<ResourceTypeInfo>() var actorTypes = Rules.Actors.Values.Where(a => a.HasTraitInfo<IMapPreviewSignatureInfo>());
.ToDictionary(r => r.ResourceType, r => r.TerrainType); var actors = ActorDefinitions.Where(a => actorTypes.Where(ai => ai.Name == a.Value.Value).Any());
var positions = new List<Pair<MPos, Color>>();
foreach (var actor in actors)
{
var s = new ActorReference(actor.Value.Value, actor.Value.ToDictionary());
var ai = Rules.Actors[actor.Value.Value];
var impsis = ai.TraitInfos<IMapPreviewSignatureInfo>();
foreach (var impsi in impsis)
impsi.PopulateMapPreviewSignatureCells(this, ai, s, positions);
}
// ResourceLayer is on world actor, which isn't caught above, so an extra check for it.
var worldActorInfo = Rules.Actors["world"];
var worldimpsis = worldActorInfo.TraitInfos<IMapPreviewSignatureInfo>();
foreach (var worldimpsi in worldimpsis)
worldimpsi.PopulateMapPreviewSignatureCells(this, worldActorInfo, null, positions);
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
{ {
@@ -672,15 +688,10 @@ namespace OpenRA
for (var x = 0; x < width; x++) for (var x = 0; x < width; x++)
{ {
var uv = new MPos(x + Bounds.Left, y + Bounds.Top); var uv = new MPos(x + Bounds.Left, y + Bounds.Top);
var resourceType = Resources[uv].Type; var actorsThere = positions.Where(ap => ap.First == uv);
if (resourceType != 0) if (actorsThere.Any())
{ {
// Cell contains resources leftColor = rightColor = actorsThere.First().Second;
string res;
if (!resources.TryGetValue(resourceType, out res))
continue;
leftColor = rightColor = tileset[tileset.GetTerrainIndex(res)].Color;
} }
else else
{ {

View File

@@ -181,7 +181,6 @@
<Compile Include="Traits\Target.cs" /> <Compile Include="Traits\Target.cs" />
<Compile Include="Traits\TraitsInterfaces.cs" /> <Compile Include="Traits\TraitsInterfaces.cs" />
<Compile Include="Traits\World\Faction.cs" /> <Compile Include="Traits\World\Faction.cs" />
<Compile Include="Traits\World\ResourceType.cs" />
<Compile Include="Traits\World\ScreenShaker.cs" /> <Compile Include="Traits\World\ScreenShaker.cs" />
<Compile Include="Traits\World\DebugVisualizations.cs" /> <Compile Include="Traits\World\DebugVisualizations.cs" />
<Compile Include="World.cs" /> <Compile Include="World.cs" />

View File

@@ -287,6 +287,11 @@ namespace OpenRA.Traits
public interface ISelectionDecorations { void DrawRollover(Actor self, WorldRenderer worldRenderer); } public interface ISelectionDecorations { void DrawRollover(Actor self, WorldRenderer worldRenderer); }
public interface IMapPreviewSignatureInfo : ITraitInfoInterface
{
void PopulateMapPreviewSignatureCells(Map map, ActorInfo ai, ActorReference s, List<Pair<MPos, Color>> destinationBuffer);
}
public interface IOccupySpaceInfo : ITraitInfoInterface public interface IOccupySpaceInfo : ITraitInfoInterface
{ {
IReadOnlyDictionary<CPos, SubCell> OccupiedCells(ActorInfo info, CPos location, SubCell subCell = SubCell.Any); IReadOnlyDictionary<CPos, SubCell> OccupiedCells(ActorInfo info, CPos location, SubCell subCell = SubCell.Any);

View File

@@ -11,7 +11,7 @@
using System.Linq; using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Traits; using OpenRA.Mods.Common.Traits;
namespace OpenRA.Mods.Common.Widgets namespace OpenRA.Mods.Common.Widgets
{ {

View File

@@ -280,6 +280,7 @@
<Compile Include="Traits\Air\FlyAwayOnIdle.cs" /> <Compile Include="Traits\Air\FlyAwayOnIdle.cs" />
<Compile Include="Traits\Air\FallsToEarth.cs" /> <Compile Include="Traits\Air\FallsToEarth.cs" />
<Compile Include="Traits\Air\ReturnOnIdle.cs" /> <Compile Include="Traits\Air\ReturnOnIdle.cs" />
<Compile Include="Traits\AppearsOnMapPreview.cs" />
<Compile Include="Traits\Armament.cs" /> <Compile Include="Traits\Armament.cs" />
<Compile Include="Traits\Armor.cs" /> <Compile Include="Traits\Armor.cs" />
<Compile Include="Traits\AttackMove.cs" /> <Compile Include="Traits\AttackMove.cs" />
@@ -565,6 +566,7 @@
<Compile Include="Traits\World\Locomotor.cs" /> <Compile Include="Traits\World\Locomotor.cs" />
<Compile Include="Traits\World\JumpjetLocomotor.cs" /> <Compile Include="Traits\World\JumpjetLocomotor.cs" />
<Compile Include="Traits\World\PaletteFromEmbeddedSpritePalette.cs" /> <Compile Include="Traits\World\PaletteFromEmbeddedSpritePalette.cs" />
<Compile Include="Traits\World\ResourceType.cs" />
<Compile Include="Traits\World\SubterraneanLocomotor.cs" /> <Compile Include="Traits\World\SubterraneanLocomotor.cs" />
<Compile Include="Traits\World\LoadWidgetAtGameStart.cs" /> <Compile Include="Traits\World\LoadWidgetAtGameStart.cs" />
<Compile Include="Traits\World\MPStartLocations.cs" /> <Compile Include="Traits\World\MPStartLocations.cs" />

View File

@@ -0,0 +1,59 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* 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.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Render this actor when creating the minimap while saving the map.")]
public class AppearsOnMapPreviewInfo : TraitInfo<AppearsOnMapPreview>, IMapPreviewSignatureInfo, Requires<IOccupySpaceInfo>
{
[Desc("Use this color to render the actor, instead of owner player color.")]
public readonly Color Color = new Color();
[Desc("Use this terrain color to render the actor, instead of owner player color.",
"Overrides `Color` if both set.")]
public readonly string Terrain = null;
void IMapPreviewSignatureInfo.PopulateMapPreviewSignatureCells(Map map, ActorInfo ai, ActorReference s, List<Pair<MPos, Color>> destinationBuffer)
{
var tileSet = map.Rules.TileSet;
Color color;
if (!string.IsNullOrEmpty(Terrain))
{
color = tileSet[tileSet.GetTerrainIndex(Terrain)].Color;
}
else if (Color != new Color())
{
color = Color;
}
else
{
var owner = map.PlayerDefinitions.Where(p => s.InitDict.Get<OwnerInit>().PlayerName == p.Value.Nodes.First(k => k.Key == "Name").Value.Value).First();
var colorValue = owner.Value.Nodes.Where(n => n.Key == "Color");
var ownerColor = colorValue.Any() ? colorValue.First().Value.Value : "FFFFFF";
Color.TryParse(ownerColor, out color);
}
var ios = ai.TraitInfo<IOccupySpaceInfo>();
var cells = ios.OccupiedCells(ai, s.InitDict.Get<LocationInit>().Value(null));
foreach (var cell in cells)
destinationBuffer.Add(new Pair<MPos, Color>(cell.Key.ToMPos(map), color));
}
}
public class AppearsOnMapPreview { }
}

View File

@@ -11,10 +11,12 @@
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Traits namespace OpenRA.Mods.Common.Traits
{ {
public class ResourceTypeInfo : ITraitInfo public class ResourceTypeInfo : ITraitInfo, IMapPreviewSignatureInfo
{ {
[Desc("Sequence image that holds the different variants.")] [Desc("Sequence image that holds the different variants.")]
public readonly string Image = "resources"; public readonly string Image = "resources";
@@ -64,6 +66,22 @@ namespace OpenRA.Traits
[Desc("Harvester content pip color.")] [Desc("Harvester content pip color.")]
public PipType PipColor = PipType.Yellow; public PipType PipColor = PipType.Yellow;
void IMapPreviewSignatureInfo.PopulateMapPreviewSignatureCells(Map map, ActorInfo ai, ActorReference s, List<Pair<MPos, Color>> destinationBuffer)
{
var tileSet = map.Rules.TileSet;
var color = tileSet[tileSet.GetTerrainIndex(TerrainType)].Color;
for (var i = 0; i < map.MapSize.X; i++)
{
for (var j = 0; j < map.MapSize.Y; j++)
{
var cell = new MPos(i, j);
if (map.Resources[cell].Type == ResourceType)
destinationBuffer.Add(new Pair<MPos, Color>(cell, color));
}
}
}
public object Create(ActorInitializer init) { return new ResourceType(this, init.World); } public object Create(ActorInitializer init) { return new ResourceType(this, init.World); }
} }

View File

@@ -11,7 +11,7 @@
using System.Linq; using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic namespace OpenRA.Mods.Common.Widgets.Logic