Skip to content

Commit

Permalink
Update for CV
Browse files Browse the repository at this point in the history
  • Loading branch information
IntelOrca committed Mar 23, 2024
1 parent ead53e6 commit 5989a28
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 15 deletions.
41 changes: 41 additions & 0 deletions src/IntelOrca.Biohazard/Room/CvMotionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,47 @@ public CvMotionList WithNewBaseOffset(int baseOffset)
bw.Write(Data[(PageCount * 4)..]);
return new CvMotionList(baseOffset, ms.ToArray());
}

public Builder ToBuilder()
{
var result = new Builder();
result.BaseOffset = BaseOffset;
result.Pages.AddRange(Pages.ToArray());
return result;
}

public class Builder
{
public int BaseOffset { get; set; }
public List<CvMotionListPage> Pages { get; } = new List<CvMotionListPage>();

public CvMotionList ToCvMotionList()
{
var ms = new MemoryStream();
var bw = new BinaryWriter(ms);

var offset = BaseOffset + (Pages.Count * 4);
foreach (var page in Pages)
{
if (page.Data.Length == 0)
{
bw.Write(0);
}
else
{
bw.Write(offset);
offset += page.Data.Length;
}
}

foreach (var page in Pages)
{
bw.Write(page.Data);
}

return new CvMotionList(BaseOffset, ms.ToArray());
}
}
}

public readonly struct CvMotionListPage
Expand Down
1 change: 0 additions & 1 deletion src/IntelOrca.Biohazard/Script/ScdReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ private void ReadScriptCv(BinaryReader br, int length, BioScriptKind kind, IBioS
var functionOffset = functionOffsets[i];
var functionEnd = functionOffsets[i + 1];
var functionEndMin = functionOffset;
var ifStack = 0;
var isEnd = false;
br.BaseStream.Position = functionOffset;
while (br.BaseStream.Position < functionEnd)
Expand Down
53 changes: 39 additions & 14 deletions src/survey/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class Program
[DllImport("kernel32.dll")]
private extern static bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesRead);

private static string _jsonPath = @"M:\git\rer\IntelOrca.Biohazard.BioRand\data\re2\enemy.json";
private static string _jsonPath = @"M:\git\rer\IntelOrca.Biohazard.BioRand\data\recv\enemy.json";

private static bool _exit;
private static byte[] _buffer = new byte[64];
Expand Down Expand Up @@ -66,20 +66,25 @@ public static void Main(string[] args)
var p = pAll.FirstOrDefault(x => x.ProcessName.StartsWith("Bio"));
if (p != null)
{
Spy(p);
Spy(p, 0x0500, 500);
}
p = pAll.FirstOrDefault(x => x.ProcessName.StartsWith("bio2"));
if (p != null)
{
Spy(p);
Spy(p, 0x2300, 500);
}
p = pAll.FirstOrDefault(x => x.ProcessName.StartsWith("BIOHAZARD(R) 3"));
if (p != null)
{
Spy(p);
Spy(p, 0x2300, 500);
}
p = pAll.FirstOrDefault(x => x.ProcessName.StartsWith("pcsx2"));
if (p != null)
{
Spy(p, 0x0080, 5);
}

Console.WriteLine("Waiting for RE 1, RE 2 or RE 3 to start...");
Console.WriteLine("Waiting for RE 1, RE 2, RE 3 or RE CV to start...");
Thread.Sleep(4000);
}
}
Expand Down Expand Up @@ -114,7 +119,7 @@ private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs
_exit = true;
}

private static void Spy(Process p)
private static void Spy(Process p, int expectedKey, int distance)
{
var lastGameState = new GameState();
var gameState = new GameState();
Expand All @@ -125,18 +130,17 @@ private static void Spy(Process p)
{
Console.CursorTop = 0;
Console.WriteLine($"Key: {gameState.Key,6:X4}");
Console.WriteLine($"Room: {gameState.Stage + 1:X}{gameState.Room:X2}");
Console.WriteLine($"Room: {gameState.Stage + 1:X}{gameState.Room:X2}{gameState.Variant}");
Console.WriteLine($"Cut: {gameState.Cut,6}");
Console.WriteLine($"X: {gameState.X,8}");
Console.WriteLine($"Y: {gameState.Y,8}");
Console.WriteLine($"Z: {gameState.Z,8}");
Console.WriteLine($"D: {gameState.D,8}");
Console.WriteLine($"F: {gameState.Floor,8}");

var expectedKey = p.ProcessName.StartsWith("Bio") ? 0x0500 : 0x2300;
if (gameState.Key == expectedKey)
{
AddEnemyPosition(gameState);
AddEnemyPosition(gameState, distance);
// RemoveEnemyPositions(gameState, 2000);
}

Expand All @@ -156,7 +160,7 @@ private static void Spy(Process p)
}
}

private static void AddEnemyPosition(GameState state)
private static void AddEnemyPosition(GameState state, int distance)
{
var pos = new EnemyPosition()
{
Expand All @@ -169,7 +173,7 @@ private static void AddEnemyPosition(GameState state)
};

var closeBy = _enemyPositions
.Where(x => x.IsVeryClose(pos))
.Where(x => x.IsVeryClose(pos, distance))
.ToArray();
if (closeBy.Length != 0)
return;
Expand Down Expand Up @@ -245,7 +249,7 @@ private static void GetGameState(Process p, GameState gameState)
gameState.Cut = buffer[4];
gameState.LastCut = buffer[6];
}
else
else if (p.ProcessName.StartsWith("BIOHAZARD(R) 3"))
{
ReadMemory(p, 0x00A61C84, buffer, 0, 2);
gameState.Key = BitConverter.ToUInt16(buffer, 0);
Expand All @@ -265,6 +269,26 @@ private static void GetGameState(Process p, GameState gameState)
gameState.Cut = buffer[4];
gameState.LastCut = buffer[6];
}
else
{
ReadMemory(p, 0x2044E1B0, buffer, 0, 4);
gameState.Key = (ushort)BitConverter.ToUInt32(buffer, 0);

ReadMemory(p, 0x204F6EE8, buffer, 0, 12);
gameState.X = (short)BitConverter.ToSingle(buffer, 0);
gameState.Y = (short)BitConverter.ToSingle(buffer, 4);
gameState.Z = (short)BitConverter.ToSingle(buffer, 8);

ReadMemory(p, 0x204322FC, buffer, 0, 12);
gameState.D = (short)(BitConverter.ToInt32(buffer, 4) & 0xFFFF);

gameState.Floor = 0;

ReadMemory(p, 0x204339B4, buffer, 0, 3);
gameState.Stage = buffer[0];
gameState.Room = buffer[1];
gameState.Variant = buffer[3];
}
}

private unsafe static bool ReadMemory(Process process, int address, byte[] buffer, int offset, int length)
Expand All @@ -282,6 +306,7 @@ public class GameState : IEquatable<GameState>
public ushort Key { get; set; }
public byte Stage { get; set; }
public byte Room { get; set; }
public byte? Variant { get; set; }
public byte Cut { get; set; }
public byte LastCut { get; set; }
public short X { get; set; }
Expand All @@ -290,7 +315,7 @@ public class GameState : IEquatable<GameState>
public short D { get; set; }
public byte Floor { get; set; }

public string RtdId => $"{Stage + 1:X}{Room:X2}";
public string RtdId => $"{Stage + 1:X}{Room:X2}{Variant}";

public override bool Equals(object obj)
{
Expand Down Expand Up @@ -394,7 +419,7 @@ public int DistanceTo(string room, int x, int y, int z)

public int DistanceTo(GameState other) => DistanceTo(other.RtdId, other.X, other.Y, other.Z);
public int DistanceTo(EnemyPosition other) => DistanceTo(other.Room, other.X, other.Y, other.Z);
public bool IsVeryClose(EnemyPosition other) => DistanceTo(other) <= 500;
public bool IsVeryClose(EnemyPosition other, int distance) => DistanceTo(other) <= distance;

public override string ToString()
{
Expand Down

0 comments on commit 5989a28

Please sign in to comment.