This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL2Monster.cs
401 lines (314 loc) · 11.5 KB
/
L2Monster.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace LEdit
{
/*
* Lufia 2 - Monster Class
*/
public class L2Monster : Content
{
#region STATIC
public const int InitialCount = 0xE0;
public const long TableAddress = 0x1A05C0;
public const long TableAddress_LPatch = 0x408000;
public static Collection<L2Monster> Array = new Collection<L2Monster>();
public static void ExtractAll(ProgressForm progress)
{
if (progress != null)
{
progress.SetText("Extrahiere Monster...");
progress.SetMaxProgress(InitialCount);
progress.Reset();
}
BinaryReader rom = new BinaryReader(
new FileStream(Project.ROMFile.FullName, FileMode.Open, FileAccess.Read));
Project.GetOriginalFolder(ContentType.Monster).Create();
for (int i = 0; i < InitialCount; i++)
{
FileInfo file = Project.RequestOriginalSource(ContentType.Monster, i);
if (file.Exists)
file.Delete();
rom.BaseStream.Seek(TableAddress + (i << 1), SeekOrigin.Begin);
rom.BaseStream.Seek(TableAddress + (long)rom.ReadUInt16(), SeekOrigin.Begin);
new L2Monster(rom).Save(file);
if(progress != null)
progress.Progress(1);
}
rom.Close();
}
public static void StaticInit(ProgressForm progress)
{
if (progress != null)
{
progress.SetText("Lade Monster...");
progress.SetMaxProgress(InitialCount);
progress.Reset();
}
for (int i = 0; i < InitialCount; i++)
{
FileInfo file = Project.RequestSource(ContentType.Monster, i);
if (!file.Exists)
{
//TODO: Error!
}
L2Monster monster = new L2Monster();
monster.ID = i;
monster.Load(file);
Array.Add(monster);
if (progress != null)
progress.Progress(1);
}
PrepareContentSelectionDialog();
}
public static void StaticUnload()
{
}
public static void PrepareContentSelectionDialog()
{
ContentSelectionDialog dlg;
try
{
dlg = ContentSelectionDialog.Map[ContentType.Monster];
dlg.Dispose();
}
catch (Exception)
{
}
dlg = new ContentSelectionDialog("Monster auswählen");
foreach (L2Monster monster in Array)
dlg.AddItem(monster);
ContentSelectionDialog.Map.Remove(ContentType.Monster);
ContentSelectionDialog.Map.Add(ContentType.Monster, dlg);
}
public static void Compile(FileStream rom, ProgressForm progress)
{
if (progress != null)
{
progress.SetText("Füge Monster ein...");
progress.SetMaxProgress(Array.Count);
progress.Reset();
}
BinaryWriter romWriter = new BinaryWriter(rom);
long offset = Array.Count << 1;
for (int i = 0; i < Array.Count; i++)
{
rom.Seek(TableAddress_LPatch + (i << 1), SeekOrigin.Begin);
romWriter.Write((ushort)offset);
rom.Seek(TableAddress_LPatch + offset, SeekOrigin.Begin);
BinaryReader src = new BinaryReader(new FileStream(
Project.RequestSource(ContentType.Monster, i).FullName,
FileMode.Open, FileAccess.Read));
romWriter.Write(src.ReadBytes((int)src.BaseStream.Length));
src.Close();
offset = rom.Position - TableAddress_LPatch;
if (progress != null)
progress.Progress(1);
}
}
/*
public static void SaveAllToROM()
{
if (File.Exists(FileName))
{
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
int sz = (int)(fs.Length - 1);
if (sz <= 0x8000)
{
BinaryReader br = new BinaryReader(fs);
br.BaseStream.Seek(0x01, SeekOrigin.Begin);
byte[] data = br.ReadBytes(sz);
br.Close();
fs.Close();
fs = new FileStream(L2ROM.FileName, FileMode.Open, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Seek((int)TableOffset_LPatch, SeekOrigin.Begin);
bw.Write(data, 0, sz);
bw.Close();
fs.Close();
}
else
MessageBox.Show("Die Monsterdaten übersteigen die maximale Größe von 32KB!",
"Fehler",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
*/
#endregion
public L2String Name;
public byte Level;
public byte Unknown1;
public int MonsterSpriteID;
public byte Palette;
public int HP;
public int MP;
public int ANG;
public int ABW;
public byte BWG;
public byte INT;
public byte MUT;
public byte MGA;
public int Experience;
public int Gold;
public int GiftItem = -1;
public int GiftProbability;
private ushort offset_ex7, offset_ex8;
public L2BattleScript AttackScript = new L2BattleScript();
public L2BattleScript DefendScript = new L2BattleScript();
public L2Monster()
{
}
public L2Monster(BinaryReader br)
{
FromStream(br);
}
public override string GetName()
{
return Name.Value.TrimEnd();
}
public override Bitmap GetIcon()
{
return L2MonsterSprite.Array[MonsterSpriteID - 1].GetBitmap(Palette);
}
public override void FromStream(BinaryReader br)
{
if (br != null)
{
long address = br.BaseStream.Position;
Name = new L2String(br, 0xD);
Level = br.ReadByte();
Unknown1 = br.ReadByte();
MonsterSpriteID = (int)br.ReadByte();
//TODO: Sprite = L2MonsterSprite.Get(SpriteFile);
Palette = br.ReadByte();
HP = br.ReadUInt16();
MP = br.ReadUInt16();
ANG = br.ReadUInt16();
ABW = br.ReadUInt16();
BWG = br.ReadByte();
INT = br.ReadByte();
MUT = br.ReadByte();
MGA = br.ReadByte();
Experience = br.ReadUInt16();
Gold = br.ReadUInt16();
//Extra Data - CODE REPLICATION
offset_ex7 = 0;
offset_ex8 = 0;
for (byte b = br.ReadByte(); b != 0; b = br.ReadByte())
{
if (b == 0x03)
{
GiftItem = (byte)br.ReadByte();
GiftProbability = (int)br.ReadByte();
if ((GiftProbability & 0x01) != 0)
{
GiftItem |= 0x100;
GiftProbability &= 0xFE;
}
}
else if (b == 0x07)
offset_ex7 = br.ReadUInt16();
else if (b == 0x08)
offset_ex8 = br.ReadUInt16();
else
{
Console.WriteLine("??? - 0x" + b.ToString("X2"));
br.ReadUInt16(); //skip, unknown
}
}
//ex7 - ATTACK SCRIPT
if (offset_ex7 != 0)
{
br.BaseStream.Seek(address + (long)offset_ex7, SeekOrigin.Begin);
AttackScript = new L2BattleScript(br, offset_ex7);
}
//ex8 - DEFEND SCRIPT
if (offset_ex8 != 0)
{
br.BaseStream.Seek(address + (long)offset_ex8, SeekOrigin.Begin);
DefendScript = new L2BattleScript(br, offset_ex8);
}
}
}
public override void Load(FileInfo File)
{
BinaryReader br = new BinaryReader(File.OpenRead());
FromStream(br);
br.Close();
}
public override void ToStream(BinaryWriter bw)
{
ushort offset = 0;
ushort next_free_offset = 0;
Name.ToStream(bw, 0x0D, false);
bw.Write(Level);
bw.Write(Unknown1);
bw.Write((byte)MonsterSpriteID);
bw.Write(Palette);
bw.Write((ushort)HP);
bw.Write((ushort)MP);
bw.Write((ushort)ANG);
bw.Write((ushort)ABW);
bw.Write((byte)BWG);
bw.Write((byte)INT);
bw.Write((byte)MUT);
bw.Write((byte)MGA);
bw.Write((ushort)Experience);
bw.Write((ushort)Gold);
next_free_offset += 0x21;
if (GiftItem >= 0)
{
bw.Write((byte)0x03);
int item = GiftItem;
int probability = GiftProbability;
if ((item & 0x100) != 0)
{
item &= 0xFF;
probability |= 0x01;
}
bw.Write((byte)item);
bw.Write((byte)probability);
next_free_offset += 0x03;
}
ushort as_size = (ushort)AttackScript.Bytes.Count;
ushort ds_size = (ushort)DefendScript.Bytes.Count;
ushort as_offset = 0;
if (as_size > 0)
{
as_offset = (ushort)(next_free_offset + 0x04);
if (ds_size > 0)
as_offset += 0x03;
AttackScript.Offset = as_offset - offset;
bw.Write((byte)0x07);
bw.Write((ushort)(as_offset - offset));
next_free_offset += 0x03;
}
ushort ds_offset = 0;
if (ds_size > 0)
{
ds_offset = (ushort)(next_free_offset + 0x04 + as_size);
DefendScript.Offset = ds_offset - offset;
bw.Write((byte)0x08);
bw.Write((ushort)(ds_offset - offset));
next_free_offset += 0x03;
}
bw.Write((byte)0x00);
next_free_offset += 0x01;
for (int k = 0; k < as_size; k++)
bw.Write(AttackScript.Bytes[k]);
for (int k = 0; k < ds_size; k++)
bw.Write(DefendScript.Bytes[k]);
next_free_offset += (ushort)(as_size + ds_size);
}
public override void Save(FileInfo File)
{
BinaryWriter bw = new BinaryWriter(File.OpenWrite());
ToStream(bw);
bw.Close();
}
}
}