Skip to content

Commit

Permalink
Change BitArray in ConnectedOverlays to byte
Browse files Browse the repository at this point in the history
  • Loading branch information
ZivDero authored Jan 9, 2024
1 parent bb12f75 commit 0ef8d88
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/TSMapEditor/Models/ConnectedOverlayType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ConnectedOverlayFrame
{
public OverlayType OverlayType { get; set; }
public int FrameIndex { get; set; }
public BitArray ConnectsTo { get; set; }
public byte ConnectsTo { get; set; }
}

public class ConnectedOverlayType
Expand All @@ -32,7 +32,7 @@ public ConnectedOverlayType(IniSection iniSection, Rules rules)
if (connectionMaskString == null || connectionMaskString.Length != (int)Direction.Count || Regex.IsMatch(connectionMaskString, "[^01]"))
throw new INIConfigException ($"Connected overlay type {Name} has an invalid connection mask {connectionMaskString}!");

ConnectionMask = new BitArray(connectionMaskString.Select(c => c == '1').ToArray());
ConnectionMask = Convert.ToByte(connectionMaskString, 2);

for (int i = 0; i < FrameCount; i++)
{
Expand All @@ -48,7 +48,7 @@ public ConnectedOverlayType(IniSection iniSection, Rules rules)
if (connectsToString == null || connectsToString.Length != (int)Direction.Count || Regex.IsMatch(connectsToString, "[^01]"))
throw new INIConfigException ($"Connected overlay type {Name}, frame {i} has an invalid ConnectsTo mask {connectsToString}!");

BitArray connectsTo = new BitArray(connectsToString.Select(c => c == '1').ToArray());
byte connectsTo = Convert.ToByte(connectsToString, 2);

Frames.Add(new ConnectedOverlayFrame()
{
Expand All @@ -62,7 +62,7 @@ public ConnectedOverlayType(IniSection iniSection, Rules rules)
public string Name { get; protected set; }
public string UIName { get; protected set; }
public int FrameCount { get; protected set; }
public BitArray ConnectionMask { get; protected set; }
public byte ConnectionMask { get; protected set; }
public List<ConnectedOverlayFrame> Frames { get; protected set; } = new();
public List<ConnectedOverlayType> RelatedOverlays { get; protected set; } = new();
private readonly Random random = new ();
Expand All @@ -84,7 +84,7 @@ public void InitializeRelatedOverlays(IniSection iniSection, List<ConnectedOverl

public ConnectedOverlayFrame GetOverlayForCell(IMutationTarget mutationTarget, Point2D cellCoords)
{
BitArray connectionMask = new BitArray((int)Direction.Count);
byte connectionMask = 0;

for (int direction = 0; direction < (int)Direction.Count; direction++)
{
Expand All @@ -94,12 +94,11 @@ public ConnectedOverlayFrame GetOverlayForCell(IMutationTarget mutationTarget, P
continue;

if (ContainsOverlay(tile.Overlay))
connectionMask.Set(direction, true);
connectionMask |= (byte)(0b10000000 >> direction);
}

connectionMask.And(ConnectionMask);
var frames = Frames.FindAll(frame =>
((BitArray)frame.ConnectsTo.Clone()).Xor(connectionMask).OfType<bool>().All(e => !e));
connectionMask &= ConnectionMask;
var frames = Frames.FindAll(frame => frame.ConnectsTo == connectionMask);

if (frames.Count == 0)
return Frames[0];
Expand Down

0 comments on commit 0ef8d88

Please sign in to comment.