Add RequiresSpecificOwners trait

To enforce specific owners via Lint rules,
and possibly other means later.
This is for cases where accidentally setting an
unfitting owner via editor could cause issues.

Example: AI might try to attack Creeps-owned trees
and get stuck.
This commit is contained in:
reaperrr
2018-08-04 01:20:21 +02:00
committed by Paul Chote
parent 310b63150f
commit fcb09d069b
3 changed files with 39 additions and 0 deletions

View File

@@ -13,6 +13,8 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Scripting;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Lint
@@ -73,6 +75,11 @@ namespace OpenRA.Mods.Common.Lint
emitError("Duplicate spawn point locations detected.");
}
// Check for actors that require specific owners
var actorsWithRequiredOwner = map.Rules.Actors
.Where(a => a.Value.HasTraitInfo<RequiresSpecificOwnersInfo>())
.ToDictionary(a => a.Key, a => a.Value.TraitInfo<RequiresSpecificOwnersInfo>());
foreach (var kv in map.ActorDefinitions)
{
var actorReference = new ActorReference(kv.Value.Value, kv.Value.ToDictionary());
@@ -89,6 +96,11 @@ namespace OpenRA.Mods.Common.Lint
emitError("Actor {0} needs to be owned by the player that owns the world. ".F(kv.Key) +
"Use the `Spawn` and `LockSpawn` player properties to force players onto a particular spawn instead.");
}
RequiresSpecificOwnersInfo info;
if (actorsWithRequiredOwner.TryGetValue(kv.Value.Value, out info))
if (!info.ValidOwnerNames.Contains(ownerName))
emitError("Actor {0} owner {1} is not one of ValidOwnerNames: {2}".F(kv.Key, ownerName, info.ValidOwnerNames.JoinWith(", ")));
}
}
}

View File

@@ -501,6 +501,7 @@
<Compile Include="Traits\RepairsBridges.cs" />
<Compile Include="Traits\Replaceable.cs" />
<Compile Include="Traits\Replacement.cs" />
<Compile Include="Traits\RequiresSpecificOwners.cs" />
<Compile Include="Traits\SeedsResource.cs" />
<Compile Include="Traits\StoresResources.cs" />
<Compile Include="Traits\Render\SelectionDecorations.cs" />

View File

@@ -0,0 +1,26 @@
#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 OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Can be used to enforce specific owners (like 'Neutral' or 'Creeps') for this actor.")]
public class RequiresSpecificOwnersInfo : TraitInfo<RequiresSpecificOwners>
{
[Desc("Only allow players listed here as owners.")]
[FieldLoader.Require]
public readonly HashSet<string> ValidOwnerNames = new HashSet<string>();
}
public class RequiresSpecificOwners { }
}