Implement ground-level bridge destruction and repair.
This commit is contained in:
223
OpenRA.Mods.Common/Traits/Buildings/BridgeHut.cs
Normal file
223
OpenRA.Mods.Common/Traits/Buildings/BridgeHut.cs
Normal file
@@ -0,0 +1,223 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 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.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Allows bridges to be targeted for demolition and repair.")]
|
||||
class BridgeHutInfo : IDemolishableInfo, ITraitInfo
|
||||
{
|
||||
[Desc("Bridge types to act on")]
|
||||
public readonly string[] Types = { "GroundLevelBridge" };
|
||||
|
||||
[Desc("Offsets to look for adjacent bridges to act on")]
|
||||
public readonly CVec[] NeighbourOffsets = { };
|
||||
|
||||
[Desc("Delay between each segment repair step")]
|
||||
public readonly int RepairPropagationDelay = 20;
|
||||
|
||||
[Desc("Delay between each segment demolish step")]
|
||||
public readonly int DemolishPropagationDelay = 5;
|
||||
|
||||
[Desc("Hide the repair cursor if the bridge is only damaged (not destroyed)")]
|
||||
public readonly bool RequireForceAttackForHeal = false;
|
||||
|
||||
public bool IsValidTarget(ActorInfo actorInfo, Actor saboteur) { return false; } // TODO: bridges don't support frozen under fog
|
||||
|
||||
public object Create(ActorInitializer init) { return new BridgeHut(init.World, this); }
|
||||
}
|
||||
|
||||
class BridgeHut : INotifyCreated, IDemolishable, ITick
|
||||
{
|
||||
public readonly BridgeHutInfo Info;
|
||||
readonly BridgeLayer bridgeLayer;
|
||||
|
||||
// Fixed at map load
|
||||
readonly List<CPos[]> segmentLocations = new List<CPos[]>();
|
||||
|
||||
// Changes as segments are killed and repaired
|
||||
readonly Dictionary<CPos, IBridgeSegment> segments = new Dictionary<CPos, IBridgeSegment>();
|
||||
readonly HashSet<CPos> dirtyLocations = new HashSet<CPos>();
|
||||
|
||||
// Enabled during a repair action
|
||||
int repairStep;
|
||||
int repairDelay;
|
||||
Actor repairRepairer;
|
||||
|
||||
// Enabled during a demolish action
|
||||
int demolishStep;
|
||||
int demolishDelay;
|
||||
Actor demolishSaboteur;
|
||||
|
||||
public BridgeHut(World world, BridgeHutInfo info)
|
||||
{
|
||||
Info = info;
|
||||
bridgeLayer = world.WorldActor.Trait<BridgeLayer>();
|
||||
}
|
||||
|
||||
void INotifyCreated.Created(Actor self)
|
||||
{
|
||||
self.World.AddFrameEndTask(w =>
|
||||
{
|
||||
// Bridge segments and huts are expected to be placed in the map
|
||||
// editor or spawned during the normal actor loading
|
||||
//
|
||||
// The number and location of bridge segments are calculated here,
|
||||
// and assumed to not change for the remaining lifetime of the world
|
||||
//
|
||||
// Bridge segment footprints and neighbour offsets are assumed to remain
|
||||
// the same when a segment is destroyed or repaired.
|
||||
var seed = Info.NeighbourOffsets.Select(v => self.Location + v);
|
||||
var processed = new HashSet<CPos>();
|
||||
while (true)
|
||||
{
|
||||
var step = NextNeighbourStep(seed, processed).ToList();
|
||||
if (!step.Any())
|
||||
break;
|
||||
|
||||
foreach (var s in step)
|
||||
segments[s.Location] = s;
|
||||
|
||||
segmentLocations.Add(step.Select(s => s.Location).ToArray());
|
||||
seed = step.SelectMany(s => s.NeighbourOffsets.Select(n => s.Location + n)).ToList();
|
||||
}
|
||||
|
||||
repairStep = demolishStep = segmentLocations.Count;
|
||||
});
|
||||
}
|
||||
|
||||
void ITick.Tick(Actor self)
|
||||
{
|
||||
// Update any dead segments
|
||||
dirtyLocations.Clear();
|
||||
foreach (var kv in segments)
|
||||
if (!kv.Value.Valid)
|
||||
dirtyLocations.Add(kv.Key);
|
||||
|
||||
foreach (var c in dirtyLocations)
|
||||
segments[c] = bridgeLayer[c].TraitOrDefault<IBridgeSegment>();
|
||||
|
||||
if (repairStep < segmentLocations.Count && --repairDelay <= 0)
|
||||
RepairStep();
|
||||
|
||||
if (demolishStep < segmentLocations.Count && --demolishDelay <= 0)
|
||||
DemolishStep();
|
||||
}
|
||||
|
||||
IEnumerable<IBridgeSegment> NextNeighbourStep(IEnumerable<CPos> seed, HashSet<CPos> processed)
|
||||
{
|
||||
foreach (var c in seed)
|
||||
{
|
||||
var bridge = bridgeLayer[c];
|
||||
if (bridge == null)
|
||||
continue;
|
||||
|
||||
var segment = bridge.TraitOrDefault<IBridgeSegment>();
|
||||
if (segment != null && Info.Types.Contains(segment.Type) && processed.Add(segment.Location))
|
||||
yield return segment;
|
||||
}
|
||||
}
|
||||
|
||||
public void Repair(Actor self, Actor repairer)
|
||||
{
|
||||
if (Info.RepairPropagationDelay > 0)
|
||||
{
|
||||
repairStep = 0;
|
||||
repairRepairer = repairer;
|
||||
RepairStep();
|
||||
}
|
||||
else
|
||||
foreach (var s in segments.Values)
|
||||
s.Repair(repairer);
|
||||
}
|
||||
|
||||
public void RepairStep()
|
||||
{
|
||||
// Find the next segment that needs to be repaired
|
||||
while (repairStep < segmentLocations.Count)
|
||||
{
|
||||
var stepDamage = segmentLocations[repairStep]
|
||||
.Select(c => segments[c])
|
||||
.Max(s => s.DamageState);
|
||||
|
||||
if (stepDamage > DamageState.Undamaged)
|
||||
break;
|
||||
|
||||
repairStep++;
|
||||
}
|
||||
|
||||
if (repairStep < segmentLocations.Count)
|
||||
foreach (var c in segmentLocations[repairStep])
|
||||
segments[c].Repair(repairRepairer);
|
||||
|
||||
repairDelay = Info.RepairPropagationDelay;
|
||||
}
|
||||
|
||||
public void Demolish(Actor self, Actor saboteur)
|
||||
{
|
||||
if (Info.DemolishPropagationDelay > 0)
|
||||
{
|
||||
demolishStep = 0;
|
||||
demolishSaboteur = saboteur;
|
||||
DemolishStep();
|
||||
}
|
||||
else
|
||||
foreach (var s in segments.Values)
|
||||
s.Demolish(saboteur);
|
||||
}
|
||||
|
||||
public void DemolishStep()
|
||||
{
|
||||
// Find the next segment to demolish
|
||||
while (demolishStep < segmentLocations.Count)
|
||||
{
|
||||
var stepDamage = segmentLocations[demolishStep]
|
||||
.Select(c => segments[c])
|
||||
.Max(s => s.DamageState);
|
||||
|
||||
if (stepDamage < DamageState.Dead)
|
||||
break;
|
||||
|
||||
demolishStep++;
|
||||
}
|
||||
|
||||
if (demolishStep < segmentLocations.Count)
|
||||
foreach (var c in segmentLocations[demolishStep])
|
||||
segments[c].Demolish(demolishSaboteur);
|
||||
|
||||
demolishDelay = Info.DemolishPropagationDelay;
|
||||
|
||||
// Always advance at least one step (prevents sticking on placeholders)
|
||||
demolishStep++;
|
||||
}
|
||||
|
||||
public bool IsValidTarget(Actor self, Actor saboteur)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public DamageState BridgeDamageState
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!segments.Any())
|
||||
return DamageState.Undamaged;
|
||||
|
||||
return segments.Values.Max(s => s.DamageState);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Repairing { get { return repairStep < segmentLocations.Count; } }
|
||||
}
|
||||
}
|
||||
80
OpenRA.Mods.Common/Traits/Buildings/BridgePlaceholder.cs
Normal file
80
OpenRA.Mods.Common/Traits/Buildings/BridgePlaceholder.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 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.Linq;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Placeholder actor that transforms into another actor type when repaired.")]
|
||||
class BridgePlaceholderInfo : ITraitInfo
|
||||
{
|
||||
public readonly string Type = "GroundLevelBridge";
|
||||
|
||||
[FieldLoader.Require]
|
||||
[Desc("Actor type to replace with on repair.")]
|
||||
[ActorReference] public readonly string ReplaceWithActor = null;
|
||||
|
||||
public readonly CVec[] NeighbourOffsets = { };
|
||||
|
||||
public object Create(ActorInitializer init) { return new BridgePlaceholder(init.Self, this); }
|
||||
}
|
||||
|
||||
class BridgePlaceholder : IBridgeSegment, INotifyAddedToWorld, INotifyRemovedFromWorld
|
||||
{
|
||||
public readonly BridgePlaceholderInfo Info;
|
||||
readonly Actor self;
|
||||
readonly BridgeLayer bridgeLayer;
|
||||
|
||||
public BridgePlaceholder(Actor self, BridgePlaceholderInfo info)
|
||||
{
|
||||
Info = info;
|
||||
this.self = self;
|
||||
bridgeLayer = self.World.WorldActor.Trait<BridgeLayer>();
|
||||
}
|
||||
|
||||
void INotifyAddedToWorld.AddedToWorld(Actor self)
|
||||
{
|
||||
bridgeLayer.Add(self);
|
||||
}
|
||||
|
||||
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
|
||||
{
|
||||
bridgeLayer.Remove(self);
|
||||
}
|
||||
|
||||
void IBridgeSegment.Repair(Actor repairer)
|
||||
{
|
||||
self.World.AddFrameEndTask(w =>
|
||||
{
|
||||
self.Dispose();
|
||||
|
||||
w.CreateActor(Info.ReplaceWithActor, new TypeDictionary
|
||||
{
|
||||
new LocationInit(self.Location),
|
||||
new OwnerInit(self.Owner),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void IBridgeSegment.Demolish(Actor saboteur)
|
||||
{
|
||||
// Do nothing, we're already dead
|
||||
}
|
||||
|
||||
string IBridgeSegment.Type { get { return Info.Type; } }
|
||||
DamageState IBridgeSegment.DamageState { get { return DamageState.Dead; } }
|
||||
bool IBridgeSegment.Valid { get { return self.IsInWorld; } }
|
||||
CVec[] IBridgeSegment.NeighbourOffsets { get { return Info.NeighbourOffsets; } }
|
||||
CPos IBridgeSegment.Location { get { return self.Location; } }
|
||||
}
|
||||
}
|
||||
119
OpenRA.Mods.Common/Traits/Buildings/GroundLevelBridge.cs
Normal file
119
OpenRA.Mods.Common/Traits/Buildings/GroundLevelBridge.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 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.GameRules;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Bridge actor that can't be passed underneath.")]
|
||||
class GroundLevelBridgeInfo : ITraitInfo, IRulesetLoaded, Requires<BuildingInfo>, Requires<HealthInfo>
|
||||
{
|
||||
public readonly string TerrainType = "Bridge";
|
||||
|
||||
public readonly string Type = "GroundLevelBridge";
|
||||
|
||||
public readonly CVec[] NeighbourOffsets = { };
|
||||
|
||||
[Desc("The name of the weapon to use when demolishing the bridge")]
|
||||
[WeaponReference] public readonly string DemolishWeapon = "Demolish";
|
||||
|
||||
public WeaponInfo DemolishWeaponInfo { get; private set; }
|
||||
|
||||
public void RulesetLoaded(Ruleset rules, ActorInfo ai) { DemolishWeaponInfo = rules.Weapons[DemolishWeapon.ToLowerInvariant()]; }
|
||||
|
||||
public object Create(ActorInitializer init) { return new GroundLevelBridge(init.Self, this); }
|
||||
}
|
||||
|
||||
class GroundLevelBridge : IBridgeSegment, INotifyAddedToWorld, INotifyRemovedFromWorld
|
||||
{
|
||||
public readonly GroundLevelBridgeInfo Info;
|
||||
readonly Actor self;
|
||||
readonly BridgeLayer bridgeLayer;
|
||||
readonly IEnumerable<CPos> cells;
|
||||
readonly Health health;
|
||||
|
||||
public GroundLevelBridge(Actor self, GroundLevelBridgeInfo info)
|
||||
{
|
||||
Info = info;
|
||||
this.self = self;
|
||||
health = self.Trait<Health>();
|
||||
|
||||
bridgeLayer = self.World.WorldActor.Trait<BridgeLayer>();
|
||||
var buildingInfo = self.Info.TraitInfo<BuildingInfo>();
|
||||
cells = FootprintUtils.PathableTiles(self.Info.Name, buildingInfo, self.Location);
|
||||
}
|
||||
|
||||
void UpdateTerrain(Actor self, byte terrainIndex)
|
||||
{
|
||||
foreach (var cell in cells)
|
||||
self.World.Map.CustomTerrain[cell] = terrainIndex;
|
||||
|
||||
var domainIndex = self.World.WorldActor.TraitOrDefault<DomainIndex>();
|
||||
if (domainIndex != null)
|
||||
domainIndex.UpdateCells(self.World, cells);
|
||||
}
|
||||
|
||||
void INotifyAddedToWorld.AddedToWorld(Actor self)
|
||||
{
|
||||
bridgeLayer.Add(self);
|
||||
|
||||
var tileSet = self.World.Map.Rules.TileSet;
|
||||
var terrainIndex = tileSet.GetTerrainIndex(Info.TerrainType);
|
||||
UpdateTerrain(self, terrainIndex);
|
||||
KillInvalidActorsInFootprint(self);
|
||||
}
|
||||
|
||||
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
|
||||
{
|
||||
bridgeLayer.Remove(self);
|
||||
|
||||
UpdateTerrain(self, byte.MaxValue);
|
||||
KillInvalidActorsInFootprint(self);
|
||||
}
|
||||
|
||||
void KillInvalidActorsInFootprint(Actor self)
|
||||
{
|
||||
foreach (var c in cells)
|
||||
foreach (var a in self.World.ActorMap.GetActorsAt(c))
|
||||
if (a.Info.HasTraitInfo<IPositionableInfo>() && !a.Trait<IPositionable>().CanEnterCell(c))
|
||||
a.Kill(self);
|
||||
}
|
||||
|
||||
void IBridgeSegment.Repair(Actor repairer)
|
||||
{
|
||||
health.InflictDamage(self, repairer, new Damage(-health.MaxHP), true);
|
||||
}
|
||||
|
||||
void IBridgeSegment.Demolish(Actor saboteur)
|
||||
{
|
||||
self.World.AddFrameEndTask(w =>
|
||||
{
|
||||
if (self.IsDead)
|
||||
return;
|
||||
|
||||
// Use .FromPos since this actor is dead. Cannot use Target.FromActor
|
||||
Info.DemolishWeaponInfo.Impact(Target.FromPos(self.CenterPosition), saboteur, Enumerable.Empty<int>());
|
||||
|
||||
self.World.WorldActor.Trait<ScreenShaker>().AddEffect(15, self.CenterPosition, 6);
|
||||
self.Kill(saboteur);
|
||||
});
|
||||
}
|
||||
|
||||
string IBridgeSegment.Type { get { return Info.Type; } }
|
||||
DamageState IBridgeSegment.DamageState { get { return self.GetDamageState(); } }
|
||||
bool IBridgeSegment.Valid { get { return self.IsInWorld; } }
|
||||
CVec[] IBridgeSegment.NeighbourOffsets { get { return Info.NeighbourOffsets; } }
|
||||
CPos IBridgeSegment.Location { get { return self.Location; } }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user