@@ -53,143 +53,44 @@ public override bool Validate()
5353 return title == "FINAL FANTASY 2 " ;
5454 }
5555
56- public byte [ ] GetOverworldSubTiles ( ) => Get ( OverworldSubTileGraphicsOffset , 32 * MapSubTileCount ) ;
57- public byte [ ] GetOverworldTileFormations ( ) => Get ( OverworldTileFormationsOffset , 4 * MapTileCount ) ;
58- public byte [ ] GetOverworldSubTilePaletteOffsets ( ) => Get ( OverworldSubTilePaletteOffsetsOffset , MapSubTileCount ) ;
56+ public Tileset Tileset { get ; private set ; }
57+ public Map Map { get ; private set ; }
5958
60- public ushort [ ] GetOverworldPalette ( )
59+ public void LoadOverworldMap ( )
6160 {
62- var paletteBytes = Get ( OverworldPaletteOffset , 2 * 64 ) ;
63-
64- var palette = new ushort [ 64 ] ;
65- Buffer . BlockCopy ( paletteBytes , 0 , palette , 0 , 2 * 64 ) ;
66-
67- return palette ;
68- }
69-
70- public byte [ , ] GetOverworldMap ( )
71- {
72- var pointerBytes = Get ( OverworldRowPointersOffset , 2 * OverworldRowCount ) ;
73-
61+ var data = Get ( OverworldRowDataOffset , OverworldRowDataMaxLength ) ;
62+ var pointerBytes = Get ( OverworldRowPointersOffset , OverworldRowCount * 2 ) ;
7463 var pointers = new ushort [ OverworldRowCount ] ;
75- Buffer . BlockCopy ( pointerBytes , 0 , pointers , 0 , 2 * OverworldRowCount ) ;
64+ Buffer . BlockCopy ( pointerBytes , 0 , pointers , 0 , pointerBytes . Length ) ;
7665
77- var rows = new byte [ OverworldRowCount , OverworldRowLength ] ;
78- for ( int y = 0 ; y < OverworldRowCount ; y ++ )
79- {
80- var dataOffset = OverworldRowDataOffset + pointers [ y ] ;
81- var rowOffset = 0 ;
82- byte tile = Data [ dataOffset ] ;
83- while ( tile != 0xFF )
84- {
85- if ( tile == 0x00 )
86- {
87- rows [ y , rowOffset ++ ] = 0x00 ;
88- rows [ y , rowOffset ++ ] = 0x70 ;
89- rows [ y , rowOffset ++ ] = 0x71 ;
90- rows [ y , rowOffset ++ ] = 0x72 ;
91- }
92- else if ( tile == 0x10 )
93- {
94- rows [ y , rowOffset ++ ] = 0x10 ;
95- rows [ y , rowOffset ++ ] = 0x73 ;
96- rows [ y , rowOffset ++ ] = 0x74 ;
97- rows [ y , rowOffset ++ ] = 0x75 ;
98- }
99- else if ( tile == 0x20 )
100- {
101- rows [ y , rowOffset ++ ] = 0x20 ;
102- rows [ y , rowOffset ++ ] = 0x76 ;
103- rows [ y , rowOffset ++ ] = 0x77 ;
104- rows [ y , rowOffset ++ ] = 0x78 ;
105- }
106- else if ( tile == 0x30 )
107- {
108- rows [ y , rowOffset ++ ] = 0x30 ;
109- rows [ y , rowOffset ++ ] = 0x79 ;
110- rows [ y , rowOffset ++ ] = 0x7A ;
111- rows [ y , rowOffset ++ ] = 0x7B ;
112- }
113- else if ( tile >= 0x80 )
114- {
115- tile -= 0x80 ;
116- var count = Data [ ++ dataOffset ] + 1 ;
117- for ( int j = 0 ; j < count ; j ++ )
118- {
119- rows [ y , rowOffset ++ ] = tile ;
120- }
121- }
122- else
123- {
124- rows [ y , rowOffset ++ ] = tile ;
125- }
66+ Map = new Map ( MapType . Overworld , data , pointers ) ;
12667
127- dataOffset ++ ;
128- tile = Data [ dataOffset ] ;
129- }
130- }
68+ var subTiles = Get ( OverworldSubTileGraphicsOffset , 32 * MapSubTileCount ) ;
69+ var formations = Get ( OverworldTileFormationsOffset , 4 * MapTileCount ) ;
70+ var paletteBytes = Get ( OverworldPaletteOffset , 2 * 64 ) ;
71+ var palette = new ushort [ 64 ] ;
72+ Buffer . BlockCopy ( paletteBytes , 0 , palette , 0 , 2 * 64 ) ;
73+ var paletteOffsets = Get ( OverworldSubTilePaletteOffsetsOffset , MapSubTileCount ) ;
13174
132- return rows ;
75+ Tileset = new Tileset ( subTiles , formations , palette , paletteOffsets ) ;
13376 }
13477
135- public void SaveOverworldMap ( byte [ , ] map )
78+ public void SaveOverworldMap ( )
13679 {
137- var compressedBytes = new byte [ OverworldRowCount * OverworldRowLength ] ;
138- ushort dataOffset = 0 ;
139- var pointers = new ushort [ OverworldRowCount ] ;
140-
141- for ( int y = 0 ; y < OverworldRowCount ; y ++ )
142- {
143- pointers [ y ] = dataOffset ;
144- int x = 0 ;
145- while ( x < OverworldRowLength )
146- {
147- if ( map [ y , x ] == 0x00 || map [ y , x ] == 0x10 || map [ y , x ] == 0x20 || map [ y , x ] == 0x30 )
148- {
149- compressedBytes [ dataOffset ++ ] = map [ y , x ] ;
150- x += 4 ;
151- }
152- else if ( x == OverworldRowLength - 1 )
153- {
154- compressedBytes [ dataOffset ++ ] = map [ y , x ++ ] ;
155- }
156- else if ( map [ y , x + 1 ] == map [ y , x ] )
157- {
158- compressedBytes [ dataOffset ++ ] = ( byte ) ( map [ y , x ++ ] + 0x80 ) ;
159-
160- byte repeatCount = 0 ;
161- while ( x < OverworldRowLength && map [ y , x - 1 ] == map [ y , x ] )
162- {
163- x ++ ;
164- repeatCount ++ ;
165- }
166- compressedBytes [ dataOffset ++ ] = repeatCount ;
167- }
168- else
169- {
170- compressedBytes [ dataOffset ++ ] = map [ y , x ++ ] ;
171- }
172- }
173-
174- compressedBytes [ dataOffset ++ ] = 0xFF ;
175- }
176-
177- if ( dataOffset > OverworldRowDataMaxLength )
80+ var length = Map . Length ;
81+ if ( length > OverworldRowDataMaxLength )
17882 {
179- throw new IndexOutOfRangeException ( $ "Overworld map data is too big: { dataOffset } bytes used, { OverworldRowDataMaxLength } bytes allowed") ;
83+ throw new IndexOutOfRangeException ( $ "Overworld map data is too big: { length } bytes used, { OverworldRowDataMaxLength } bytes allowed") ;
18084 }
18185
182- var pointerBytes = new byte [ OverworldRowCount * 2 ] ;
86+ byte [ ] data ;
87+ ushort [ ] pointers ;
88+ byte [ ] pointerBytes = new byte [ OverworldRowCount * 2 ] ;
89+ Map . GetCompressedData ( out data , out pointers ) ;
18390 Buffer . BlockCopy ( pointers , 0 , pointerBytes , 0 , pointerBytes . Length ) ;
184- Put ( OverworldRowPointersOffset , pointerBytes ) ;
18591
186- var mapDataBytes = new byte [ OverworldRowDataMaxLength ] ;
187- Buffer . BlockCopy ( compressedBytes , 0 , mapDataBytes , 0 , dataOffset ) ;
188- while ( dataOffset < OverworldRowDataMaxLength )
189- {
190- mapDataBytes [ dataOffset ++ ] = 0xFF ;
191- }
192- Put ( OverworldRowDataOffset , mapDataBytes ) ;
92+ Put ( OverworldRowDataOffset , data ) ;
93+ Put ( OverworldRowPointersOffset , pointerBytes ) ;
19394 }
19495 }
19596}
0 commit comments