Skip to content

Commit 207a1ea

Browse files
committed
save underworld/moon data
1 parent 5626b24 commit 207a1ea

3 files changed

Lines changed: 77 additions & 33 deletions

File tree

FF4/FF4Rom.cs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,53 @@ public Tileset LoadWorldMapTileset(MapType mapType)
141141
public void SaveWorldMap(Map map)
142142
{
143143
var length = map.CompressedSize;
144-
if (length > OverworldRowDataMaxLength)
144+
int maxLength =
145+
map.MapType == MapType.Overworld ? OverworldRowDataMaxLength :
146+
map.MapType == MapType.Underworld ? UnderworldRowDataMaxLength :
147+
map.MapType == MapType.Moon ? MoonRowDataMaxLength : 0;
148+
if (length > maxLength)
145149
{
146-
throw new IndexOutOfRangeException($"Overworld map data is too big: {length} bytes used, {OverworldRowDataMaxLength} bytes allowed");
150+
throw new IndexOutOfRangeException($"World map data is too big: {length} bytes used, {maxLength} bytes allowed");
147151
}
148152

149-
byte[] pointerBytes = new byte[OverworldRowCount * 2];
153+
byte[] pointerBytes = new byte[2*map.Height];
150154
map.GetCompressedData(out byte[] data, out ushort[] pointers);
151155
Buffer.BlockCopy(pointers, 0, pointerBytes, 0, pointerBytes.Length);
152156

153-
Put(OverworldRowDataOffset, data);
154-
Put(OverworldRowPointersOffset, pointerBytes);
157+
if (map.MapType == MapType.Overworld)
158+
{
159+
Put(OverworldRowDataOffset, data);
160+
Put(OverworldRowPointersOffset, pointerBytes);
161+
}
162+
else if (map.MapType == MapType.Underworld)
163+
{
164+
Put(UnderworldRowDataOffset, data);
165+
Put(UnderworldRowPointersOffset, pointerBytes);
166+
}
167+
else if (map.MapType == MapType.Moon)
168+
{
169+
Put(MoonRowDataOffset, data);
170+
Put(MoonRowPointersOffset, pointerBytes);
171+
}
155172
}
156173

157-
public void SaveWorldMapTileset(Tileset tileset)
174+
public void SaveWorldMapTileset(MapType mapType, Tileset tileset)
158175
{
159176
byte[] propertyBytes = new byte[MapTileCount*2];
160177
Buffer.BlockCopy(tileset.TileProperties, 0, propertyBytes, 0, propertyBytes.Length);
161178

162-
Put(OverworldTilePropertiesOffset, propertyBytes);
179+
if (mapType == MapType.Overworld)
180+
{
181+
Put(OverworldTilePropertiesOffset, propertyBytes);
182+
}
183+
else if (mapType == MapType.Underworld)
184+
{
185+
Put(UnderworldTilePropertiesOffset, propertyBytes);
186+
}
187+
else if (mapType == MapType.Moon)
188+
{
189+
Put(MoonTilePropertiesOffset, propertyBytes);
190+
}
163191
}
164192
}
165193
}

FF4/Map.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,27 +112,36 @@ public Map(MapType mapType, byte[] data, ushort[] pointers)
112112

113113
public void GetCompressedData(out byte[] data, out ushort[] pointers)
114114
{
115-
// Just for now until we cover all execution paths.
116-
data = null;
117-
pointers = null;
118-
119115
if (MapType == MapType.Overworld)
120116
{
121117
data = new byte[FF4Rom.OverworldRowDataMaxLength];
122-
pointers = new ushort[Height];
123-
int dataOffset = 0;
124-
for (int y = 0; y < Height; y++)
125-
{
126-
Buffer.BlockCopy(_compressedRows[y], 0, data, dataOffset, _compressedRowLengths[y]);
127-
pointers[y] = (ushort)dataOffset;
118+
}
119+
else if (MapType == MapType.Underworld)
120+
{
121+
data = new byte[FF4Rom.UnderworldRowDataMaxLength];
122+
}
123+
else if (MapType == MapType.Moon)
124+
{
125+
data = new byte[FF4Rom.MoonRowDataMaxLength];
126+
}
127+
else
128+
{
129+
throw new Exception("Invalid world map type");
130+
}
128131

129-
dataOffset += _compressedRowLengths[y];
130-
}
132+
pointers = new ushort[Height];
133+
int dataOffset = 0;
134+
for (int y = 0; y < Height; y++)
135+
{
136+
Buffer.BlockCopy(_compressedRows[y], 0, data, dataOffset, _compressedRowLengths[y]);
137+
pointers[y] = (ushort)dataOffset;
131138

132-
while (dataOffset < data.Length)
133-
{
134-
data[dataOffset++] = 0xFF;
135-
}
139+
dataOffset += _compressedRowLengths[y];
140+
}
141+
142+
while (dataOffset < data.Length)
143+
{
144+
data[dataOffset++] = 0xFF;
136145
}
137146
}
138147

FF4MapEdit/MainWindow.xaml.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,24 @@ private void SaveButton_Click(object sender, RoutedEventArgs e)
119119
{
120120
if (_map != null)
121121
{
122-
try
123-
{
124-
_rom.SaveWorldMap(_map);
125-
_rom.SaveWorldMapTileset(_tileset);
126-
}
127-
catch (IndexOutOfRangeException ex) when (ex.Message.StartsWith("Overworld map data is too big"))
128-
{
129-
MessageBox.Show(ex.Message, "Error while saving");
130-
}
131-
122+
SaveWorldMap();
132123
_rom.Save(_filename);
133124
}
134125
}
135126

127+
private void SaveWorldMap()
128+
{
129+
try
130+
{
131+
_rom.SaveWorldMap(_map);
132+
_rom.SaveWorldMapTileset(_map.MapType, _tileset);
133+
}
134+
catch (IndexOutOfRangeException ex) when (ex.Message.StartsWith("World map data is too big"))
135+
{
136+
MessageBox.Show(ex.Message, "Error while saving");
137+
}
138+
}
139+
136140
private void Tileset_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
137141
{
138142
GetClickedTile(sender, e, out int x, out int y);
@@ -153,7 +157,10 @@ private void MapComboBox_SelectionChanged(object sender, SelectionChangedEventAr
153157
{
154158
var mapType = (MapType)Enum.Parse(typeof(MapType), ((ComboBoxItem)MapComboBox.SelectedItem).Content.ToString());
155159
if (mapType != _map.MapType)
160+
{
161+
SaveWorldMap();
156162
LoadWorldMap(mapType);
163+
}
157164
}
158165

159166
private void HighlightSelectedTile(int x, int y)

0 commit comments

Comments
 (0)