Skip to content

Commit 1791f33

Browse files
committed
add support for behaviour tree db
1 parent a74cb17 commit 1791f33

2 files changed

Lines changed: 72 additions & 4 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using CathodeLib;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Runtime.InteropServices;
6+
using System.Linq;
7+
8+
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
9+
using UnityEngine;
10+
#else
11+
using System.Numerics;
12+
#endif
13+
14+
namespace CATHODE
15+
{
16+
/// <summary>
17+
/// DATA/ENV/x/WORLD/BEHAVIOR_TREE.DB
18+
/// </summary>
19+
public class BehaviorTreeDB : CathodeFile
20+
{
21+
public List<string> Entries = new List<string>();
22+
public static new Implementation Implementation = Implementation.LOAD | Implementation.SAVE | Implementation.CREATE;
23+
24+
public BehaviorTreeDB(string path) : base(path) { }
25+
public BehaviorTreeDB(MemoryStream stream, string path = "") : base(stream, path) { }
26+
public BehaviorTreeDB(byte[] data, string path = "") : base(data, path) { }
27+
28+
#region FILE_IO
29+
override protected bool LoadInternal(MemoryStream stream)
30+
{
31+
using (BinaryReader reader = new BinaryReader(stream))
32+
{
33+
int count = reader.ReadInt32();
34+
reader.BaseStream.Position += (count * 8) + 4;
35+
for (int i = 0; i < count; i++)
36+
{
37+
Entries.Add(Utilities.ReadString(reader));
38+
}
39+
}
40+
return true;
41+
}
42+
43+
override protected bool SaveInternal()
44+
{
45+
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(_filepath)))
46+
{
47+
writer.BaseStream.SetLength(0);
48+
writer.Write((Int64)Entries.Count);
49+
writer.Write(new byte[8 * Entries.Count]);
50+
List<int> offsets = new List<int>(Entries.Count);
51+
for (int i = 0; i < Entries.Count; i++)
52+
{
53+
offsets.Add((int)writer.BaseStream.Position);
54+
Utilities.WriteString(Entries[i], writer, true);
55+
}
56+
writer.BaseStream.Position = 8;
57+
for (int i = 0; i < Entries.Count; i++)
58+
{
59+
writer.Write((Int64)offsets[i]);
60+
}
61+
}
62+
return true;
63+
}
64+
#endregion
65+
}
66+
}

CathodeLib/Scripts/Level.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class Level
5858
public SoundEnvironmentData SoundEnvironmentData;
5959
public SoundEventData SoundEventData;
6060
public SoundLoadZones SoundLoadZones;
61+
public BehaviorTreeDB BehaviorTreeDB;
6162
public GalaxyItems GalaxyItems;
6263
public GalaxyDefinition GalaxyDefinition;
6364

@@ -95,7 +96,7 @@ public class State
9596
/// </summary>
9697
public Action OnSaveTick;
9798

98-
public const int NumberOfTicks = 31;
99+
public const int NumberOfTicks = 32;
99100

100101
/// <summary>
101102
/// A container for data related to a level in the game's "ENV" folder
@@ -158,7 +159,8 @@ public void Load()
158159
() => { SoundDialogueLookups = new SoundDialogueLookups(world + "SOUNDDIALOGUELOOKUPS.DAT"); OnLoadTick?.Invoke(); },
159160
() => { SoundEnvironmentData = new SoundEnvironmentData(world + "SOUNDENVIRONMENTDATA.DAT"); OnLoadTick?.Invoke(); },
160161
() => { SoundEventData = new SoundEventData(world + "SOUNDEVENTDATA.DAT"); OnLoadTick?.Invoke(); },
161-
() => { SoundLoadZones = new SoundLoadZones(world + "SOUNDLOADZONES.DAT"); OnLoadTick?.Invoke(); }
162+
() => { SoundLoadZones = new SoundLoadZones(world + "SOUNDLOADZONES.DAT"); OnLoadTick?.Invoke(); },
163+
() => { BehaviorTreeDB = new BehaviorTreeDB(world + "BEHAVIOR_TREE.DB"); OnLoadTick?.Invoke(); }
162164
);
163165

164166
Parallel.Invoke(
@@ -171,7 +173,6 @@ public void Load()
171173
//The following files are used by the game, but not handled yet:
172174
// - RENDERABLE/RADIOSITY_RUNTIME.BIN
173175
// - WORLD/RADIOSITY_COLLISION_MAPPING.BIN
174-
// - WORLD/BEHAVIOR_TREE.DB
175176
// - WORLD/COLLISION.HKX / HKX64
176177
// - WORLD/PHYSICS.HKX / HKX64
177178
// - WORLD/OCCLUDER_TRIANGLE_BVH.BIN
@@ -275,7 +276,8 @@ public void Save()
275276
() => { SoundDialogueLookups.Save(); OnSaveTick?.Invoke(); },
276277
() => { SoundEnvironmentData.Save(); OnSaveTick?.Invoke(); },
277278
() => { SoundEventData.Save(); OnSaveTick?.Invoke(); },
278-
() => { SoundLoadZones.Save(); OnSaveTick?.Invoke(); }
279+
() => { SoundLoadZones.Save(); OnSaveTick?.Invoke(); },
280+
() => { BehaviorTreeDB.Save(); OnSaveTick?.Invoke(); }
279281
);
280282

281283
Commands.Save(); OnSaveTick?.Invoke();

0 commit comments

Comments
 (0)