Sanitize resource warheads
- Fix potential crash due to invalid target (no CenterPosition) - Fix potential crash on multiple ResourceLayers
This commit is contained in:
@@ -17,35 +17,43 @@ using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Warheads
|
||||
{
|
||||
public class CreateResourceWarhead : Warhead
|
||||
public class CreateResourceWarhead : Warhead, IRulesetLoaded<WeaponInfo>
|
||||
{
|
||||
[Desc("Size of the area. The resources are seeded within this area.", "Provide 2 values for a ring effect (outer/inner).")]
|
||||
public readonly int[] Size = { 0, 0 };
|
||||
|
||||
[Desc("Will this splatter resources and which?")]
|
||||
[FieldLoader.Require]
|
||||
public readonly string AddsResourceType = null;
|
||||
|
||||
void IRulesetLoaded<WeaponInfo>.RulesetLoaded(Ruleset rules, WeaponInfo info)
|
||||
{
|
||||
var world = rules.Actors["world"];
|
||||
var resourceType = world.TraitInfos<ResourceTypeInfo>()
|
||||
.FirstOrDefault(t => t.Type == AddsResourceType);
|
||||
|
||||
if (resourceType == null)
|
||||
throw new YamlException("CreateResourceWarhead defines an invalid resource type '{0}'".F(AddsResourceType));
|
||||
}
|
||||
|
||||
// TODO: Allow maximum resource splatter to be defined. (Per tile, and in total).
|
||||
public override void DoImpact(Target target, WarheadArgs args)
|
||||
{
|
||||
if (string.IsNullOrEmpty(AddsResourceType))
|
||||
if (target.Type == TargetType.Invalid)
|
||||
return;
|
||||
|
||||
var firedBy = args.SourceActor;
|
||||
var pos = target.CenterPosition;
|
||||
var world = firedBy.World;
|
||||
var targetTile = world.Map.CellContaining(target.CenterPosition);
|
||||
var resLayer = world.WorldActor.Trait<ResourceLayer>();
|
||||
var targetTile = world.Map.CellContaining(pos);
|
||||
|
||||
var minRange = (Size.Length > 1 && Size[1] > 0) ? Size[1] : 0;
|
||||
var allCells = world.Map.FindTilesInAnnulus(targetTile, minRange, Size[0]);
|
||||
|
||||
var resourceType = world.WorldActor.TraitsImplementing<ResourceType>()
|
||||
.FirstOrDefault(t => t.Info.Type == AddsResourceType);
|
||||
.First(t => t.Info.Type == AddsResourceType);
|
||||
|
||||
if (resourceType == null)
|
||||
Log.Write("debug", "Warhead defines an invalid resource type '{0}'".F(AddsResourceType));
|
||||
else
|
||||
{
|
||||
var resLayer = world.WorldActor.Trait<ResourceLayer>();
|
||||
foreach (var cell in allCells)
|
||||
{
|
||||
if (!resLayer.CanSpawnResourceAt(resourceType, cell))
|
||||
@@ -56,5 +64,4 @@ namespace OpenRA.Mods.Common.Warheads
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,9 +24,13 @@ namespace OpenRA.Mods.Common.Warheads
|
||||
// TODO: Allow maximum resource removal to be defined. (Per tile, and in total).
|
||||
public override void DoImpact(Target target, WarheadArgs args)
|
||||
{
|
||||
if (target.Type == TargetType.Invalid)
|
||||
return;
|
||||
|
||||
var firedBy = args.SourceActor;
|
||||
var pos = target.CenterPosition;
|
||||
var world = firedBy.World;
|
||||
var targetTile = world.Map.CellContaining(target.CenterPosition);
|
||||
var targetTile = world.Map.CellContaining(pos);
|
||||
var resLayer = world.WorldActor.Trait<ResourceLayer>();
|
||||
|
||||
var minRange = (Size.Length > 1 && Size[1] > 0) ? Size[1] : 0;
|
||||
|
||||
Reference in New Issue
Block a user