Skip to content

Commit

Permalink
Convert crushclasses to bitset
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisforbes authored and pchote committed Jul 28, 2018
1 parent 82f6c2b commit 51e0005
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 28 deletions.
11 changes: 5 additions & 6 deletions OpenRA.Mods.Cnc/Traits/Mine.cs
Expand Up @@ -9,7 +9,6 @@
*/
#endregion

using System.Collections.Generic;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
Expand All @@ -18,10 +17,10 @@ namespace OpenRA.Mods.Cnc.Traits
{
class MineInfo : ITraitInfo
{
public readonly HashSet<string> CrushClasses = new HashSet<string>();
public readonly BitSet<CrushClass> CrushClasses = default(BitSet<CrushClass>);
public readonly bool AvoidFriendly = true;
public readonly bool BlockFriendly = true;
public readonly HashSet<string> DetonateClasses = new HashSet<string>();
public readonly BitSet<CrushClass> DetonateClasses = default(BitSet<CrushClass>);

public object Create(ActorInitializer init) { return new Mine(this); }
}
Expand All @@ -35,9 +34,9 @@ public Mine(MineInfo info)
this.info = info;
}

void INotifyCrushed.WarnCrush(Actor self, Actor crusher, HashSet<string> crushClasses) { }
void INotifyCrushed.WarnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses) { }

void INotifyCrushed.OnCrush(Actor self, Actor crusher, HashSet<string> crushClasses)
void INotifyCrushed.OnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
if (!info.CrushClasses.Overlaps(crushClasses))
return;
Expand All @@ -52,7 +51,7 @@ void INotifyCrushed.OnCrush(Actor self, Actor crusher, HashSet<string> crushClas
self.Kill(crusher, mobile != null ? mobile.Info.LocomotorInfo.CrushDamageTypes : default(BitSet<DamageType>));
}

bool ICrushable.CrushableBy(Actor self, Actor crusher, HashSet<string> crushClasses)
bool ICrushable.CrushableBy(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
if (info.BlockFriendly && !crusher.Info.HasTraitInfo<MineImmuneInfo>() && self.Owner.Stances[crusher.Owner] == Stance.Ally)
return false;
Expand Down
Expand Up @@ -9,8 +9,8 @@
*/
#endregion

using System.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits
Expand Down Expand Up @@ -42,7 +42,7 @@ public GrantExternalConditionToCrusher(Actor self, GrantExternalConditionToCrush
this.Info = info;
}

void INotifyCrushed.OnCrush(Actor self, Actor crusher, HashSet<string> crushClasses)
void INotifyCrushed.OnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
var external = crusher.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == Info.OnCrushCondition && t.CanGrantCondition(crusher, self));
Expand All @@ -51,7 +51,7 @@ void INotifyCrushed.OnCrush(Actor self, Actor crusher, HashSet<string> crushClas
external.GrantCondition(crusher, self, Info.OnCrushDuration);
}

void INotifyCrushed.WarnCrush(Actor self, Actor crusher, HashSet<string> crushClasses)
void INotifyCrushed.WarnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
var external = crusher.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == Info.WarnCrushCondition && t.CanGrantCondition(crusher, self));
Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Mods.Common/Traits/Crates/Crate.cs
Expand Up @@ -91,9 +91,9 @@ public Crate(ActorInitializer init, CrateInfo info)
SetPosition(self, init.Get<LocationInit, CPos>());
}

void INotifyCrushed.WarnCrush(Actor self, Actor crusher, HashSet<string> crushClasses) { }
void INotifyCrushed.WarnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses) { }

void INotifyCrushed.OnCrush(Actor self, Actor crusher, HashSet<string> crushClasses)
void INotifyCrushed.OnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
// Crate can only be crushed if it is not in the air.
if (!self.IsAtGroundLevel() || !crushClasses.Contains(info.CrushClass))
Expand Down Expand Up @@ -216,7 +216,7 @@ public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientAc
return GetAvailableSubCell(a, SubCell.Any, ignoreActor, checkTransientActors) != SubCell.Invalid;
}

bool ICrushable.CrushableBy(Actor self, Actor crusher, HashSet<string> crushClasses)
bool ICrushable.CrushableBy(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
// Crate can only be crushed if it is not in the air.
return self.IsAtGroundLevel() && crushClasses.Contains(info.CrushClass);
Expand Down
10 changes: 5 additions & 5 deletions OpenRA.Mods.Common/Traits/Crushable.cs
Expand Up @@ -21,7 +21,7 @@ class CrushableInfo : ConditionalTraitInfo
[Desc("Sound to play when being crushed.")]
public readonly string CrushSound = null;
[Desc("Which crush classes does this actor belong to.")]
public readonly HashSet<string> CrushClasses = new HashSet<string> { "infantry" };
public readonly BitSet<CrushClass> CrushClasses = new BitSet<CrushClass>("infantry");
[Desc("Probability of mobile actors noticing and evading a crush attempt.")]
public readonly int WarnProbability = 75;
[Desc("Will friendly units just crush me instead of pathing around.")]
Expand All @@ -40,7 +40,7 @@ public Crushable(Actor self, CrushableInfo info)
this.self = self;
}

void INotifyCrushed.WarnCrush(Actor self, Actor crusher, HashSet<string> crushClasses)
void INotifyCrushed.WarnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
if (!CrushableInner(crushClasses, crusher.Owner))
return;
Expand All @@ -50,7 +50,7 @@ void INotifyCrushed.WarnCrush(Actor self, Actor crusher, HashSet<string> crushCl
mobile.Nudge(self, crusher, true);
}

void INotifyCrushed.OnCrush(Actor self, Actor crusher, HashSet<string> crushClasses)
void INotifyCrushed.OnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
if (!CrushableInner(crushClasses, crusher.Owner))
return;
Expand All @@ -61,12 +61,12 @@ void INotifyCrushed.OnCrush(Actor self, Actor crusher, HashSet<string> crushClas
self.Kill(crusher, crusherMobile != null ? crusherMobile.Info.LocomotorInfo.CrushDamageTypes : default(BitSet<DamageType>));
}

bool ICrushable.CrushableBy(Actor self, Actor crusher, HashSet<string> crushClasses)
bool ICrushable.CrushableBy(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
return CrushableInner(crushClasses, crusher.Owner);
}

bool CrushableInner(HashSet<string> crushClasses, Player crushOwner)
bool CrushableInner(BitSet<CrushClass> crushClasses, Player crushOwner)
{
if (IsTraitDisabled)
return false;
Expand Down
5 changes: 3 additions & 2 deletions OpenRA.Mods.Common/Traits/Render/WithDeathAnimation.cs
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Effects;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits.Render
Expand Down Expand Up @@ -98,7 +99,7 @@ public void SpawnDeathAnimation(Actor self, WPos pos, string image, string seque
self.World.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, w, image, sequence, palette)));
}

void INotifyCrushed.OnCrush(Actor self, Actor crusher, HashSet<string> crushClasses)
void INotifyCrushed.OnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
crushed = true;

Expand All @@ -112,6 +113,6 @@ void INotifyCrushed.OnCrush(Actor self, Actor crusher, HashSet<string> crushClas
SpawnDeathAnimation(self, self.CenterPosition, rs.GetImage(self), Info.CrushedSequence, crushPalette);
}

void INotifyCrushed.WarnCrush(Actor self, Actor crusher, HashSet<string> crushClasses) { }
void INotifyCrushed.WarnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses) { }
}
}
8 changes: 4 additions & 4 deletions OpenRA.Mods.Common/Traits/TransformCrusherOnCrush.cs
Expand Up @@ -9,8 +9,8 @@
*/
#endregion

using System.Collections.Generic;
using OpenRA.Mods.Common.Activities;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits
Expand All @@ -22,7 +22,7 @@ public class TransformCrusherOnCrushInfo : ITraitInfo

public readonly bool SkipMakeAnims = true;

public readonly HashSet<string> CrushClasses = new HashSet<string>();
public readonly BitSet<CrushClass> CrushClasses = default(BitSet<CrushClass>);

public virtual object Create(ActorInitializer init) { return new TransformCrusherOnCrush(init, this); }
}
Expand All @@ -38,9 +38,9 @@ public TransformCrusherOnCrush(ActorInitializer init, TransformCrusherOnCrushInf
faction = init.Contains<FactionInit>() ? init.Get<FactionInit, string>() : init.Self.Owner.Faction.InternalName;
}

void INotifyCrushed.WarnCrush(Actor self, Actor crusher, HashSet<string> crushClasses) { }
void INotifyCrushed.WarnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses) { }

void INotifyCrushed.OnCrush(Actor self, Actor crusher, HashSet<string> crushClasses)
void INotifyCrushed.OnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
{
if (!info.CrushClasses.Overlaps(crushClasses))
return;
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Traits/World/Locomotor.cs
Expand Up @@ -60,7 +60,7 @@ public class LocomotorInfo : ITraitInfo
public readonly bool MoveIntoShroud = true;

[Desc("e.g. crate, wall, infantry")]
public readonly HashSet<string> Crushes = new HashSet<string>();
public readonly BitSet<CrushClass> Crushes = default(BitSet<CrushClass>);

[Desc("Types of damage that are caused while crushing. Leave empty for no damage types.")]
public readonly BitSet<DamageType> CrushDamageTypes = default(BitSet<DamageType>);
Expand Down Expand Up @@ -277,7 +277,7 @@ bool IsBlockedBy(Actor self, Actor otherActor, Actor ignoreActor, CellConditions
}

// If we cannot crush the other actor in our way, we are blocked.
if (Crushes == null || Crushes.Count == 0)
if (Crushes.IsEmpty)
return true;

// If the other actor in our way cannot be crushed, we are blocked.
Expand Down
9 changes: 6 additions & 3 deletions OpenRA.Mods.Common/TraitsInterfaces.cs
Expand Up @@ -75,17 +75,20 @@ public interface IDemolishable
bool IsValidTarget(Actor self, Actor saboteur);
}

// Type tag for crush class bits
public class CrushClass { }

[RequireExplicitImplementation]
public interface ICrushable
{
bool CrushableBy(Actor self, Actor crusher, HashSet<string> crushClasses);
bool CrushableBy(Actor self, Actor crusher, BitSet<CrushClass> crushClasses);
}

[RequireExplicitImplementation]
public interface INotifyCrushed
{
void OnCrush(Actor self, Actor crusher, HashSet<string> crushClasses);
void WarnCrush(Actor self, Actor crusher, HashSet<string> crushClasses);
void OnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses);
void WarnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses);
}

[RequireExplicitImplementation]
Expand Down

0 comments on commit 51e0005

Please sign in to comment.