-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapData.cs
321 lines (285 loc) · 7.74 KB
/
MapData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Reflection.Metadata;
using System.Text.Json;
using System.Security.Cryptography;
namespace FFRMapEditorMono
{
public enum OverworldTeleportIndex : byte
{
Cardia1 = 0, // CardiaNorth
Coneria = 1,
Pravoka = 2,
Elfland = 3,
Melmond = 4,
CrescentLake = 5,
Gaia = 6,
Onrac = 7,
Lefein = 8,
ConeriaCastle1 = 9,
ElflandCastle = 10,
NorthwestCastle = 11,
CastleOrdeals1 = 12,
TempleOfFiends1 = 13,
EarthCave1 = 14,
GurguVolcano1 = 15,
IceCave1 = 16,
Cardia2 = 17, // CardiaGrass
BahamutCave1 = 18,
Waterfall = 19,
DwarfCave = 20,
MatoyasCave = 21,
SardasCave = 22,
MarshCave1 = 23,
MirageTower1 = 24,
TitansTunnelEast = 25,
TitansTunnelWest = 26,
Cardia4 = 27, // CardiaMarsh
Cardia5 = 28, // CardiaSmall
Cardia6 = 29, // CardiaForest
DefaultLocation = 30, // DefaultLocation
Unused = 31,
None = 0xff
}
public enum ExitTeleportIndex : byte
{
ExitTitanE = 0,
ExitTitanW = 1,
ExitIceCave = 2,
ExitCastleOrdeals = 3,
ExitCastleConeria = 4,
ExitEarthCave = 5,
ExitGurguVolcano = 6,
ExitSeaShrine = 7,
ExitSkyPalace = 8,
ExitUnused1 = 9,
ExitUnused2 = 10,
ExitUnused3 = 11,
ExitUnused4 = 12,
ExitUnused5 = 13,
ExitUnused6 = 14,
ExitUnused7 = 15,
None = 255
}
public enum OwEncounterGroup
{
UpperOnracGroup = 0,
NorthernEdgeGroup = 2,
OrdealsGroup = 3,
LowerOnracGroup = 8,
CardiaGroup = 10,
MirageDesertGroup = 13,
LefeinGroup = 15,
OopsAllImpsGroup = 18,
TempleOfFiendsGroup = 27,
ElflandGroup = 29,
MelmondGroup = 33,
ConeriaGroup = 36,
PravokaGroup = 37,
IceCaveGroup = 39,
AspWolfGroup = 45,
CrescentLakeGroup = 54,
}
public struct SCCoords
{
public byte X { get; set; }
public byte Y { get; set; }
public SCCoords(int x, int y)
{
X = (byte)x;
Y = (byte)y;
}
}
public class ShipLocation
{
public ShipLocation() { }
public ShipLocation(byte x, byte y, byte teleporterIndex)
{
X = x;
Y = y;
TeleporterIndex = teleporterIndex;
}
public byte TeleporterIndex { get; set; }
public byte X { get; set; }
public byte Y { get; set; }
}
public enum TeleportType
{
Enter,
Exit,
Tele
}
public class TeleportFixup
{
public TeleportFixup() { }
public TeleportFixup(TeleportType tp, int idx, TeleData to)
{
this.Type = tp;
this.Index = idx;
this.To = to;
}
public TeleportType Type { get; set; }
public int? Index { get; set; }
public TeleData? From { get; set; }
public TeleData To { get; set; }
}
public enum MapId
{
Overworld = 0,
None = 255,
}
public struct TeleData
{
public TeleData(MapId m, byte x, byte y)
{
Map = m;
X = x;
Y = y;
}
public MapId Map { get; set; }
public byte X { get; set; }
public byte Y { get; set; }
public void FlipXcoordinate()
{
var x1 = X & 0x3F;
var x2 = X & 0xC0;
X = (byte)((64 - x1 - 1) | x2);
}
public void FlipYcoordinate()
{
var y1 = Y & 0x3F;
var y2 = Y & 0xC0;
Y = (byte)((64 - y1 - 1) | y2);
}
}
public class DomainFixup
{
public byte From { get; set; }
public byte To { get; set; }
}
public class OwMapExchangeData
{
public OwMapExchangeData(OwMapExchangeData copy)
{
this.StartingLocation = copy.StartingLocation;
this.AirShipLocation = copy.AirShipLocation;
this.BridgeLocation = copy.BridgeLocation;
this.CanalLocation = copy.CanalLocation;
this.ShipLocations = copy.ShipLocations;
this.TeleporterFixups = copy.TeleporterFixups;
this.DomainFixups = copy.DomainFixups;
this.DomainUpdates = copy.DomainUpdates;
this.OverworldCoordinates = copy.OverworldCoordinates;
this.DecompressedMapRows = copy.DecompressedMapRows;
this.FFRVersion = copy.FFRVersion;
this.Checksum = copy.Checksum;
this.Seed = copy.Seed;
this.HorizontalBridge = copy.HorizontalBridge;
}
public string FFRVersion { get; set; }
public string Checksum { get; set; }
public int Seed { get; set; }
public SCCoords? StartingLocation { get; set; }
public SCCoords? AirShipLocation { get; set; }
public SCCoords? BridgeLocation { get; set; }
public SCCoords? CanalLocation { get; set; }
public ShipLocation[] ShipLocations { get; set; }
public TeleportFixup[] TeleporterFixups { get; set; }
public DomainFixup[] DomainFixups { get; set; }
public DomainFixup[] DomainUpdates { get; set; }
public Dictionary<string, SCCoords> OverworldCoordinates { get; set; }
public List<string> DecompressedMapRows { get; set; }
public bool HorizontalBridge { get; set; }
public OwMapExchangeData()
{
Seed = 0xFFFFFF;
FFRVersion = "1.0";
Checksum = "";
StartingLocation = new SCCoords(0, 0);
AirShipLocation = new SCCoords(0, 0);
BridgeLocation = new SCCoords(0, 0);
CanalLocation = new SCCoords(0, 0);
ShipLocations = new ShipLocation[0];
TeleporterFixups = new TeleportFixup[0];
DomainFixups = new DomainFixup[0];
DomainUpdates = new DomainFixup[0];
OverworldCoordinates = new();
DecompressedMapRows = new();
HorizontalBridge = false;
}
public string ComputeChecksum()
{
var copy = new OwMapExchangeData(this);
copy.FFRVersion = "";
copy.Checksum = "";
copy.Seed = 0;
var content = JsonSerializer.Serialize<OwMapExchangeData>(copy);
//content = "\'" + content.Replace("\\u002B", "+") + "\'";
content = content.Replace("\\u002B", "+");
using (SHA256 hasher = SHA256.Create())
{
byte[] JsonBlob = Encoding.UTF8.GetBytes(content);
byte[] hash = hasher.ComputeHash(JsonBlob);
return Convert.ToHexString(hash).Substring(0, 32);
}
}
public byte[] DecodeMap()
{
var rows = new List<List<byte>>();
foreach (var c in DecompressedMapRows)
{
rows.Add(new List<byte>(Convert.FromBase64String(c)));
}
return rows.SelectMany(x => x).ToArray();
}
public void EncodeMapFromBytes(byte[] data)
{
List<string> rows = new();
var map = data;
for (int y = 0; y < 256; y++)
{
rows.Add(Convert.ToBase64String(map.Skip(256 * y).Take(256).ToArray()));
}
DecompressedMapRows = rows;
}
public void EncodeMap(Overworld overworld)
{
var map = overworld.GetOwBytes();
EncodeMapFromBytes(map);
OverworldCoordinates = new();
List<(OverworldTeleportIndex id, SCCoords coord)> coordList = new();
foreach (var entry in OwDataGroup.OwTeleportTiles)
{
var foundtiles = map.Select((x, i) => (i, x)).Where(b => b.x == entry.tile).ToList();
if (!foundtiles.Any())
{
continue;
}
var index = foundtiles.Last().i;
OverworldCoordinates.Add(Enum.GetName<OverworldTeleportIndex>(entry.id), new SCCoords(index % 256, index / 256));
coordList.Add((entry.id, new SCCoords(index % 256, index / 256)));
}
List<TeleportFixup> tempFixups = new();
foreach (var exit in OwDataGroup.ExitTeleportTiles.Select(x => x.id).ToList())
{
var coord = coordList.Find(x => x.id == OwDataGroup.ExitTeleportOwTeleporter[exit]).coord;
tempFixups.Add(new TeleportFixup(TeleportType.Exit, (int)exit, new TeleData(MapId.None, coord.X, coord.Y)));
}
TeleporterFixups = tempFixups.OrderBy(t => t.Index).ToArray();
List<byte> dockTiles = new() { 0x0F, 0x1F, 0x77, 0x78, 0x79, 0x7A };
List<byte> seaTiles = new() { 0x07, 0x16, 0x17, 0x18, 0x27 };
DomainUpdates = overworld.GetDomainsData().ToArray();
ShipLocations = overworld.GetShipData().ToArray();
StartingLocation = overworld.GetMapObjectPosition(MapObject.StartingPosition);
BridgeLocation = overworld.GetMapObjectPosition(MapObject.Bridge);
CanalLocation = overworld.GetMapObjectPosition(MapObject.Canal);
AirShipLocation = overworld.GetMapObjectPosition(MapObject.Airship);
Checksum = ComputeChecksum();
}
}
}