Skip to content

Commit

Permalink
Fix #15: Some Lock IDs were duplicated
Browse files Browse the repository at this point in the history
Set the lock ID for all doors to prevent different doors from sharing the same lock ID.
  • Loading branch information
IntelOrca committed Nov 20, 2022
1 parent ec72ad3 commit 34afad9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 59 deletions.
33 changes: 18 additions & 15 deletions IntelOrca.Biohazard/DoorRandomiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class DoorRandomiser
private PlayEdge? _keyRichEdge;
private int _keyRichEdgeScore;
private bool _boxRoomReached;
private byte _lockId = 145;
private byte _lockId = 128;
private List<PlayNode> _nodesLeft = new List<PlayNode>();

private static readonly ConnectConstraint[] g_strictConstraints = new ConnectConstraint[]
Expand Down Expand Up @@ -458,18 +458,18 @@ private void AddToKeyItemRequiredCount(IEnumerable<ushort> keys)
}
}

private void ConnectDoor(PlayEdge aEdge, PlayEdge bEdge, bool oneWay = false)
private void ConnectDoor(PlayEdge aEdge, PlayEdge bEdge, bool isLocked = false)
{
var a = aEdge.Parent;
var b = bEdge.Parent;
aEdge.Node = b;
bEdge.Node = a;

if (aEdge.Requires.Length == 0 && aEdge.Lock == LockKind.Unblock)
if (aEdge.Requires.Length != 0 || aEdge.Lock == LockKind.Unblock)
{
// If the door is temporarily blocked, lock from the other side
// If the door is locked or temporarily blocked, lock from the other side
// This is a safety measure for loopbacks
oneWay = true;
isLocked = true;
}

if (a.Category != DoorRandoCategory.Static)
Expand All @@ -480,16 +480,20 @@ private void ConnectDoor(PlayEdge aEdge, PlayEdge bEdge, bool oneWay = false)
aRdt.RemoveDoorUnlock(aDoorId);
if (aEdge == bEdge)
{
aRdt.AddDoorLock(aDoorId, 255);
aRdt.SetDoorLock(aDoorId, 255);
}
else if (aEdge.Lock == LockKind.Side)
{
aEdge.Lock = LockKind.None;
aRdt.RemoveDoorLock(aDoorId);
}
if (oneWay)
if (isLocked)
{
aRdt.AddDoorUnlock(aDoorId, _lockId);
aEdge.LockId = _lockId;
if (aEdge.Requires.Length == 0)
aRdt.SetDoorUnlock(aDoorId, _lockId);
else
aRdt.EnsureDoorLockId(aDoorId, _lockId);
}
}

Expand All @@ -500,27 +504,26 @@ private void ConnectDoor(PlayEdge aEdge, PlayEdge bEdge, bool oneWay = false)
if (aEdge.Entrance == null)
{
bRdt.SetDoorTarget(bDoorId, b.RdtId, bEdge.Entrance!.Value, bEdge.OriginalTargetRdt);
bRdt.AddDoorLock(bDoorId, 255);
bRdt.SetDoorLock(bDoorId, 255);
}
else
{
bRdt.SetDoorTarget(bDoorId, a.RdtId, aEdge.Entrance.Value, bEdge.OriginalTargetRdt);
bRdt.RemoveDoorUnlock(bDoorId);
if (oneWay)
if (isLocked)
{
bEdge.Lock = LockKind.Side;
bRdt.RemoveDoorLock(bDoorId);
bRdt.AddDoorLock(bDoorId, _lockId);
bEdge.LockId = _lockId;
bRdt.SetDoorLock(bDoorId, _lockId);
}
else if (bEdge.Lock == LockKind.Side && b.Category != DoorRandoCategory.Static)
else if (b.Category != DoorRandoCategory.Static)
{
bEdge.Lock = LockKind.None;
bRdt.RemoveDoorLock(bDoorId);
}
}
}

if (oneWay)
if (isLocked)
{
_lockId++;
}
Expand Down
2 changes: 1 addition & 1 deletion IntelOrca.Biohazard/GameDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static Map GenerateMap(IEnumerable<Rdt> rooms)
mapRoomDoors.Add(new MapRoomDoor()
{
Target = new RdtId(door.NextStage, door.NextRoom).ToString(),
Requires = door.KeyType == 0 ? new ushort[0] : new ushort[] { door.KeyType }
Requires = door.LockType == 0 ? new ushort[0] : new ushort[] { door.LockType }
});
}
mapRooms.Add(room.RdtId.ToString(), new MapRoom()
Expand Down
12 changes: 6 additions & 6 deletions IntelOrca.Biohazard/Opcodes/DoorAotSeOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ internal class DoorAotSeOpcode : OpcodeBase, IDoorAotSetOpcode
public byte Texture { get; set; }
public byte Animation { get; set; }
public byte Sound { get; set; }
public byte KeyId { get; set; }
public byte KeyType { get; set; }
public byte LockId { get; set; }
public byte LockType { get; set; }
public byte Free { get; set; }

public RdtId Target
Expand Down Expand Up @@ -70,8 +70,8 @@ public static DoorAotSeOpcode Read(BinaryReader br, int offset)
Texture = br.ReadByte(),
Animation = br.ReadByte(),
Sound = br.ReadByte(),
KeyId = br.ReadByte(),
KeyType = br.ReadByte(),
LockId = br.ReadByte(),
LockType = br.ReadByte(),
Free = br.ReadByte()
};
}
Expand Down Expand Up @@ -99,8 +99,8 @@ public override void Write(BinaryWriter bw)
bw.Write(Texture);
bw.Write(Animation);
bw.Write(Sound);
bw.Write(KeyId);
bw.Write(KeyType);
bw.Write(LockId);
bw.Write(LockType);
bw.Write(Free);
}
}
Expand Down
12 changes: 6 additions & 6 deletions IntelOrca.Biohazard/Opcodes/DoorAotSet4pOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ internal class DoorAotSet4pOpcode : OpcodeBase, IDoorAotSetOpcode
public byte Texture { get; set; }
public byte Animation { get; set; }
public byte Sound { get; set; }
public byte KeyId { get; set; }
public byte KeyType { get; set; }
public byte LockId { get; set; }
public byte LockType { get; set; }
public byte Free { get; set; }

public RdtId Target
Expand Down Expand Up @@ -77,8 +77,8 @@ public static DoorAotSet4pOpcode Read(BinaryReader br, int offset)
Texture = br.ReadByte(),
Animation = br.ReadByte(),
Sound = br.ReadByte(),
KeyId = br.ReadByte(),
KeyType = br.ReadByte(),
LockId = br.ReadByte(),
LockType = br.ReadByte(),
Free = br.ReadByte()
};
}
Expand Down Expand Up @@ -110,8 +110,8 @@ public override void Write(BinaryWriter bw)
bw.Write(Texture);
bw.Write(Animation);
bw.Write(Sound);
bw.Write(KeyId);
bw.Write(KeyType);
bw.Write(LockId);
bw.Write(LockType);
bw.Write(Free);
}
}
Expand Down
4 changes: 2 additions & 2 deletions IntelOrca.Biohazard/Opcodes/IDoorAotSetOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ internal interface IDoorAotSetOpcode
byte Texture { get; set; }
byte Animation { get; set; }
byte Sound { get; set; }
byte KeyId { get; set; }
byte KeyType { get; set; }
byte LockId { get; set; }
byte LockType { get; set; }
byte Free { get; set; }
}
}
7 changes: 6 additions & 1 deletion IntelOrca.Biohazard/PlayGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ internal class PlayEdge
public RdtId OriginalTargetRdt { get; set; }
public PlayNode? Node { get; set; }
public LockKind Lock { get; set; }
public byte LockId { get; set; }
public bool NoReturn { get; set; }
public ushort[] Requires { get; }
public PlayNode[] RequiresRoom { get; set; } = Array.Empty<PlayNode>();
Expand All @@ -208,7 +209,11 @@ public string RequiresString
get
{
var s = "";
if (Lock != LockKind.None)
if (Lock == LockKind.Side)
{
s += $"(locked #{LockId}) ";
}
else if (Lock != LockKind.None)
{
s += $"({Lock.ToString().ToLowerInvariant()}) ";
}
Expand Down
39 changes: 15 additions & 24 deletions IntelOrca.Biohazard/Rdt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,53 +69,44 @@ public void SetDoorTarget(int id, RdtId target, DoorEntrance destination, RdtId
}
}

public void AddDoorLock(int id, byte keyId)
public void EnsureDoorLockId(int id, byte lockId)
{
foreach (var door in Doors)
{
if (door.Id == id)
if (door.Id == id && door.LockType != 0)
{
door.KeyId = keyId;
door.KeyType = 255;
door.LockId = lockId;
}
}
}

public void AddDoorUnlock(int id, byte keyId)
public void SetDoorLock(int id, byte lockId, byte lockType = 255)
{
foreach (var door in Doors)
{
if (door.Id == id)
{
door.KeyId = keyId;
door.KeyType = 254;
door.LockId = lockId;
door.LockType = lockType;
}
}
}

public void SetDoorUnlock(int id, byte lockId) => SetDoorLock(id, lockId, 254);

public void RemoveDoorUnlock(int id)
{
foreach (var door in Doors)
{
if (door.Id == id && door.KeyType == 254)
if (door.Id == id && door.LockType == 254)
{
door.KeyId = 0;
door.KeyType = 0;
door.LockId = 0;
door.LockType = 0;
}
}
}

public void RemoveDoorLock(int id)
{
foreach (var door in Doors)
{
if (door.Id == id)
{
door.KeyId = 0;
door.KeyType = 0;
}
}
}
public void RemoveDoorLock(int id) => SetDoorLock(id, 0, 0);

public void SetItem(byte id, ushort type, ushort amount)
{
Expand Down Expand Up @@ -241,9 +232,9 @@ public void Print()
door.NextStage + 1,
door.NextRoom,
door.Animation,
door.KeyId,
door.KeyType,
door.KeyType == 0xFF ? "side" : IntelOrca.Biohazard.Items.GetItemName(door.KeyType));
door.LockId,
door.LockType,
door.LockType == 0xFF ? "side" : IntelOrca.Biohazard.Items.GetItemName(door.LockType));
}
foreach (var item in Items)
{
Expand Down
8 changes: 4 additions & 4 deletions IntelOrca.Biohazard/ScriptDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ protected override void VisitDoorAotSe(DoorAotSeOpcode door)
door.Texture,
door.Animation,
door.Sound,
door.KeyId,
door.KeyType,
door.LockId,
door.LockType,
door.Free);
}

Expand Down Expand Up @@ -261,8 +261,8 @@ protected override void VisitDoorAotSet4p(DoorAotSet4pOpcode door)
door.Texture,
door.Animation,
door.Sound,
door.KeyId,
door.KeyType,
door.LockId,
door.LockType,
door.Free);
}

Expand Down

0 comments on commit 34afad9

Please sign in to comment.