Files
OpenRA/OpenRA.Mods.Common/Warheads/DestroyResourceWarhead.cs
let5sne.win10 9cf6ebb986
Some checks failed
Continuous Integration / Linux (.NET 8.0) (push) Has been cancelled
Continuous Integration / Windows (.NET 8.0) (push) Has been cancelled
Initial commit: OpenRA game engine
Fork from OpenRA/OpenRA with one-click launch script (start-ra.cmd)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 21:46:54 +08:00

67 lines
2.1 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.Collections.Frozen;
using System.Collections.Immutable;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
{
[Desc("Destroys resources in a circle.")]
public class DestroyResourceWarhead : Warhead
{
[Desc("Size of the area. The resources are removed within this area.", "Provide 2 values for a ring effect (outer/inner).")]
public readonly ImmutableArray<int> Size = [0, 0];
[Desc("Amount of resources to be removed. If zero, all resources within the area will be removed.")]
public readonly byte ResourceAmount = 0;
[Desc("Resource types to remove with this warhead.", "If empty, all resource types will be removed.")]
public readonly FrozenSet<string> ResourceTypes = FrozenSet<string>.Empty;
public override void DoImpact(in Target target, WarheadArgs args)
{
if (target.Type == TargetType.Invalid)
return;
var firedBy = args.SourceActor;
var pos = target.CenterPosition;
var world = firedBy.World;
var dat = world.Map.DistanceAboveTerrain(pos);
if (dat > AirThreshold)
return;
var targetTile = world.Map.CellContaining(pos);
var resourceLayer = world.WorldActor.Trait<IResourceLayer>();
var minRange = (Size.Length > 1 && Size[1] > 0) ? Size[1] : 0;
var allCells = world.Map.FindTilesInAnnulus(targetTile, minRange, Size[0]);
var removeAllTypes = ResourceTypes.Count == 0;
foreach (var cell in allCells)
{
var cellContents = resourceLayer.GetResource(cell);
if (removeAllTypes || ResourceTypes.Contains(cellContents.Type))
{
if (ResourceAmount <= 0)
resourceLayer.ClearResources(cell);
else
resourceLayer.RemoveResource(cellContents.Type, cell, ResourceAmount);
}
}
}
}
}