forked from liamt19/Lizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBulletDataFormat.cs
66 lines (55 loc) · 1.88 KB
/
BulletDataFormat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Runtime.InteropServices;
namespace Lizard.Logic.Datagen
{
[StructLayout(LayoutKind.Explicit)]
public unsafe struct BulletDataFormat : TOutputFormat
{
// STM here is used to fix the game result, which is dependent on the STM:
// If black is to move, the result is flipped around WhiteWin <-> BlackWin.
// WE don't know the result when creating the entries, and STM isn't stored within them anywhere,
// So manually place the STM in the last byte of padding of the entries.
[FieldOffset( 0)] BulletFormatEntry BFE;
[FieldOffset(29)] Move BestMove;
[FieldOffset(31)] byte STM;
public int Score
{
get => BFE.score;
set => BFE.score = (short)value;
}
public GameResult Result
{
get => (GameResult)BFE.result;
set => BFE.result = (byte)value;
}
public void SetSTM(int stm) { STM = (byte)stm; }
public void SetResult(GameResult gr)
{
if (STM == Black)
{
gr = (GameResult)(2 - gr);
}
Result = gr;
}
public string GetWritableTextData()
{
return "";
}
public byte[] GetWritableData()
{
int len = Marshal.SizeOf<BulletFormatEntry>();
IntPtr ptr = Marshal.AllocHGlobal(len);
byte[] myBuffer = new byte[len];
Marshal.StructureToPtr(BFE, ptr, false);
Marshal.Copy(ptr, myBuffer, 0, len);
Marshal.FreeHGlobal(ptr);
return myBuffer;
}
public void Fill(Position pos, Move bestMove, int score)
{
BFE = BulletFormatEntry.FromBitboard(ref pos.bb, pos.ToMove, (short)score, GameResult.Draw);
STM = (byte)pos.ToMove;
BestMove = bestMove;
}
}
}