Skip to content

Commit a58c67b

Browse files
committed
add support for GALAXY
1 parent 0bc8c6f commit a58c67b

3 files changed

Lines changed: 212 additions & 2 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
using static CATHODE.GalaxyItems;
8+
using CATHODE.ShaderTypes;
9+
10+
11+
12+
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
13+
using UnityEngine;
14+
#else
15+
using System.Numerics;
16+
#endif
17+
18+
namespace CATHODE
19+
{
20+
/// <summary>
21+
/// DATA/ENV/x/WORLD/GALAXY/GALAXY.DEFINITION_BIN
22+
/// </summary>
23+
public class GalaxyDefinition : CathodeFile
24+
{
25+
public string Name;
26+
public int StarCount;
27+
public List<StarTemplate> Entries = new List<StarTemplate>();
28+
29+
public static new Implementation Implementation = Implementation.LOAD | Implementation.SAVE | Implementation.CREATE;
30+
31+
public GalaxyDefinition(string path) : base(path) { }
32+
public GalaxyDefinition(MemoryStream stream, string path = "") : base(stream, path) { }
33+
public GalaxyDefinition(byte[] data, string path = "") : base(data, path) { }
34+
35+
#region FILE_IO
36+
override protected bool LoadInternal(MemoryStream stream)
37+
{
38+
using (BinaryReader reader = new BinaryReader(stream))
39+
{
40+
reader.BaseStream.Position += 4;
41+
byte[] stringBlock = reader.ReadBytes(128);
42+
Name = Utilities.ReadString(stringBlock);
43+
StarCount = reader.ReadInt32();
44+
int count = reader.ReadInt32();
45+
Entries = Utilities.ConsumeArray<StarTemplate>(reader, count).ToList();
46+
}
47+
return true;
48+
}
49+
50+
override protected bool SaveInternal()
51+
{
52+
if (StarCount > 16 * 1024)
53+
return false;
54+
55+
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(_filepath)))
56+
{
57+
writer.BaseStream.SetLength(0);
58+
writer.Write(5);
59+
writer.Write(new byte[128]);
60+
writer.BaseStream.Position -= 128;
61+
Utilities.WriteString(Name, writer, false);
62+
writer.BaseStream.Position += 128 - Name.Length;
63+
writer.Write(StarCount);
64+
writer.Write(Entries.Count);
65+
Utilities.Write<StarTemplate>(writer, Entries.ToArray());
66+
}
67+
return true;
68+
}
69+
#endregion
70+
71+
#region STRUCTURES
72+
[StructLayout(LayoutKind.Sequential, Pack = 1)]
73+
public class StarTemplate
74+
{
75+
public float Frequency; //Frequency of occurance relative to other galaxy items
76+
public float MinSize; //Angular size in radians
77+
public float MaxSize; //Angular size in radians
78+
public float MinIntensity; //0->1
79+
public float MaxIntensity; //0->1
80+
public Vector3 Colour; //R,G,B (0->1)
81+
}
82+
#endregion
83+
}
84+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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/GALAXY/GALAXY.ITEMS_BIN
18+
/// </summary>
19+
public class GalaxyItems : CathodeFile
20+
{
21+
public List<Star> Entries = new List<Star>();
22+
public static new Implementation Implementation = Implementation.LOAD | Implementation.SAVE | Implementation.CREATE;
23+
24+
public GalaxyItems(string path) : base(path) { }
25+
public GalaxyItems(MemoryStream stream, string path = "") : base(stream, path) { }
26+
public GalaxyItems(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+
reader.BaseStream.Position += 4;
34+
int count = reader.ReadInt32();
35+
Entries = Utilities.ConsumeArray<Star>(reader, count).ToList();
36+
}
37+
return true;
38+
}
39+
40+
override protected bool SaveInternal()
41+
{
42+
if (Entries.Count > 16 * 1024)
43+
return false;
44+
45+
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(_filepath)))
46+
{
47+
writer.BaseStream.SetLength(0);
48+
writer.Write(5);
49+
writer.Write(Entries.Count);
50+
Utilities.Write<Star>(writer, Entries.ToArray());
51+
}
52+
return true;
53+
}
54+
#endregion
55+
56+
#region HELPERS
57+
public bool Generate(GalaxyDefinition definition)
58+
{
59+
Entries.Clear();
60+
if (definition == null || definition.StarCount > 16 * 1024)
61+
return false;
62+
if (definition.StarCount == 0 || definition.Entries == null || definition.Entries.Count == 0)
63+
return true;
64+
65+
float totalFrequency = 0f;
66+
foreach (var t in definition.Entries)
67+
totalFrequency += Math.Max(0f, t.Frequency);
68+
if (totalFrequency <= 0f)
69+
totalFrequency = 1f;
70+
71+
var rng = new Random();
72+
for (int i = 0; i < definition.StarCount; i++)
73+
{
74+
float roll = (float)rng.NextDouble() * totalFrequency;
75+
GalaxyDefinition.StarTemplate template = definition.Entries[0];
76+
foreach (var t in definition.Entries)
77+
{
78+
float f = Math.Max(0f, t.Frequency);
79+
if (roll < f) { template = t; break; }
80+
roll -= f;
81+
}
82+
83+
float sizeRange = template.MaxSize - template.MinSize;
84+
float size = template.MinSize + (sizeRange <= 0f ? 0f : (float)rng.NextDouble() * sizeRange);
85+
float intensityRange = template.MaxIntensity - template.MinIntensity;
86+
float intensity = template.MinIntensity + (intensityRange <= 0f ? 0f : (float)rng.NextDouble() * intensityRange);
87+
88+
float px = (float)(rng.NextDouble() * 2.0 - 1.0);
89+
float py = (float)(rng.NextDouble() * 2.0 - 1.0);
90+
float pz = (float)(rng.NextDouble() * 2.0 - 1.0);
91+
92+
Entries.Add(new Star
93+
{
94+
Size = size,
95+
Intensity = intensity,
96+
Colour = template.Colour,
97+
Position = new Vector3(px, py, pz)
98+
});
99+
}
100+
return true;
101+
}
102+
#endregion
103+
104+
#region STRUCTURES
105+
[StructLayout(LayoutKind.Sequential, Pack = 1)]
106+
public class Star
107+
{
108+
public float Size; //0->1
109+
public float Intensity; //0->1
110+
public Vector3 Colour; //R,G,B (0->1)
111+
public Vector3 Position; //X,Y,Z (-1->1)
112+
}
113+
#endregion
114+
}
115+
}

CathodeLib/Scripts/Level.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public class Level
5858
public SoundEnvironmentData SoundEnvironmentData;
5959
public SoundEventData SoundEventData;
6060
public SoundLoadZones SoundLoadZones;
61+
public GalaxyItems GalaxyItems;
62+
public GalaxyDefinition GalaxyDefinition;
6163

6264
public class State
6365
{
@@ -93,7 +95,7 @@ public class State
9395
/// </summary>
9496
public Action OnSaveTick;
9597

96-
public const int NumberOfTicks = 30;
98+
public const int NumberOfTicks = 32;
9799

98100
/// <summary>
99101
/// A container for data related to a level in the game's "ENV" folder
@@ -159,10 +161,14 @@ public void Load()
159161
() => { SoundLoadZones = new SoundLoadZones(world + "SOUNDLOADZONES.DAT"); OnLoadTick?.Invoke(); }
160162
);
161163

164+
Parallel.Invoke(
165+
() => { GalaxyItems = new GalaxyItems(renderable + "GALAXY/GALAXY.ITEMS_BIN"); OnLoadTick?.Invoke(); },
166+
() => { GalaxyDefinition = new GalaxyDefinition(renderable + "GALAXY/GALAXY.DEFINITION_BIN"); OnLoadTick?.Invoke(); }
167+
);
168+
162169
Commands = new Commands(world + "COMMANDS" + (File.Exists(world + "COMMANDS.PAK") ? ".PAK" : ".BIN"), EnvironmentAnimations, CollisionMaps, RenderableElements); OnLoadTick?.Invoke();
163170

164171
//RENDERABLE/DAMAGE/*
165-
//RENDERABLE/GALAXY/*
166172
//RENDERABLE/RADIOSITY_RUNTIME.BIN
167173
//WORLD/BEHAVIOR_TREE.DB
168174
//WORLD/COLLISION.HKX
@@ -278,6 +284,11 @@ public void Save()
278284

279285
Commands.Save(); OnSaveTick?.Invoke();
280286

287+
Parallel.Invoke(
288+
() => { GalaxyItems.Save(); OnSaveTick?.Invoke(); },
289+
() => { GalaxyDefinition.Save(); OnSaveTick?.Invoke(); }
290+
);
291+
281292
//TODO: save states
282293
OnSaveTick?.Invoke();
283294

0 commit comments

Comments
 (0)