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+ }
0 commit comments