forked from andrewcchen/DS3EnemyEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnemyDataManager.cs
362 lines (304 loc) · 11.8 KB
/
EnemyDataManager.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
using SoulsFormats;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DS3EnemyEditor
{
class EnemyDataManager
{
private DataGridView dataGridView;
private MSB3 msb3;
private List<MSB3.Part.Enemy> enemies;
public EnemyDataManager(DataGridView dataGridView)
{
this.dataGridView = dataGridView;
}
public void InitializeTable()
{
dataGridView.AllowUserToAddRows = false;
dataGridView.AllowUserToDeleteRows = false;
dataGridView.CellValidating += CellValidatingHandler;
dataGridView.CellValidated += CellValidatedHandler;
DataGridViewColumnCollection columns = dataGridView.Columns;
DataGridViewColumn column;
DataGridViewTextBoxCell template = new DataGridViewTextBoxCell();
column = new DataGridViewColumn(template);
column.Name = column.HeaderText = "Name";
columns.Add(column);
column = new DataGridViewColumn(template);
column.Name = column.HeaderText = "ModelName";
columns.Add(column);
column = new DataGridViewColumn(template);
column.Name = column.HeaderText = "ThinkParamID";
column.ToolTipText = "Controls enemy AI";
column.ValueType = typeof(int);
columns.Add(column);
column = new DataGridViewColumn(template);
column.Name = column.HeaderText = "NPCParamID";
column.ToolTipText = "Controls enemy stats";
column.ValueType = typeof(int);
columns.Add(column);
column = new DataGridViewColumn(template);
column.Name = column.HeaderText = "EventEntityID";
column.ToolTipText = "Used to identify the part in event scripts";
column.ValueType = typeof(int);
columns.Add(column);
column = new DataGridViewColumn(template);
column.Name = column.HeaderText = "TalkID";
column.ToolTipText = "Controls enemy speech";
column.ValueType = typeof(int);
columns.Add(column);
column = new DataGridViewColumn(template);
column.Name = column.HeaderText = "CharaInitID";
column.ToolTipText = "Controls enemy equipment";
column.ValueType = typeof(int);
columns.Add(column);
column = new DataGridViewColumn(template);
column.Name = column.HeaderText = "Position";
columns.Add(column);
column = new DataGridViewColumn(template);
column.Name = column.HeaderText = "Rotation";
columns.Add(column);
column = new DataGridViewColumn(template);
column.Name = column.HeaderText = "Description";
column.ReadOnly = true;
columns.Add(column);
dataGridView.AutoResizeColumns();
}
public void LoadMSB3(String filename)
{
msb3 = SoulsFile<MSB3>.Read(filename);
enemies = msb3.Parts.Enemies;
PopulateTable();
}
public void SaveMSB3(String filename)
{
CleanupEnemyModels();
msb3.Write(filename);
}
public void Reset()
{
dataGridView.Rows.Clear();
}
public void DeleteSelected()
{
if (dataGridView.SelectedRows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
int index = row.Index;
dataGridView.Rows.RemoveAt(index);
enemies.RemoveAt(index);
}
} else if (dataGridView.SelectedCells.Count == 1)
{
int index = dataGridView.SelectedCells[0].RowIndex;
dataGridView.Rows.RemoveAt(index);
enemies.RemoveAt(index);
} else
{
return;
}
UpdateRowHeaders();
}
public void DuplicateSelected()
{
DataGridViewRow row;
if (dataGridView.SelectedRows.Count == 1)
{
row = dataGridView.SelectedRows[0];
} else if (dataGridView.SelectedCells.Count == 1)
{
row = dataGridView.Rows[dataGridView.SelectedCells[0].RowIndex];
} else
{
return;
}
int index = row.Index;
enemies.Insert(index + 1, enemies[index]);
enemies[index + 1].Name += "_dup";
dataGridView.Rows.Insert(index + 1, (DataGridViewRow) row.Clone());
UpdateRow(index + 1);
UpdateRowHeaders();
}
public bool SearchString(string str)
{
int startRow = 0, startCol = 0;
if (dataGridView.SelectedRows.Count > 0)
{
startRow = dataGridView.SelectedRows[0].Index;
} else if (dataGridView.SelectedCells.Count > 0)
{
startRow = dataGridView.SelectedCells[0].RowIndex;
startCol = dataGridView.SelectedCells[0].ColumnIndex;
}
CompareInfo compare = CultureInfo.InvariantCulture.CompareInfo;
int r = startRow, c = startCol;
do
{
c += 1;
if (c >= dataGridView.Columns.Count)
{
c = 0;
r += 1;
if (r >= dataGridView.Rows.Count)
r = 0;
}
if (compare.IndexOf(dataGridView[c, r].Value.ToString(), str,
CompareOptions.IgnoreCase) >= 0)
{
dataGridView.CurrentCell = dataGridView[c, r];
return true;
}
} while (r != startRow || c != startCol);
return false;
}
private void PopulateTable()
{
dataGridView.CellValueChanged -= CellValueChangedHandler;
DataGridViewRowCollection rows = dataGridView.Rows;
rows.Clear();
for (int i = 0; i < enemies.Count; i++)
{
UpdateRow(rows.Add());
}
UpdateRowHeaders();
dataGridView.AutoResizeColumns();
dataGridView.CellValueChanged += CellValueChangedHandler;
}
private void UpdateRow(int index)
{
MSB3.Part.Enemy enemy = enemies[index];
DataGridViewRow row = dataGridView.Rows[index];
row.Cells["Name"].Value = enemy.Name;
row.Cells["ModelName"].Value = enemy.ModelName;
row.Cells["ThinkParamID"].Value = enemy.ThinkParamID;
row.Cells["NPCParamID"].Value = enemy.NPCParamID;
row.Cells["EventEntityID"].Value = enemy.EntityID;
row.Cells["TalkID"].Value = enemy.TalkID;
row.Cells["CharaInitID"].Value = enemy.CharaInitID;
row.Cells["Position"].Value = enemy.Position.ToString();
row.Cells["Rotation"].Value = enemy.Rotation.ToString();
row.Cells["Description"].Value = ChrIDs.f(enemy.ModelName);
}
private void AddEnemyModel(string modelName)
{
List<MSB3.Model.Enemy> models = msb3.Models.Enemies;
if (models.FindIndex(model => model.Name == modelName) == -1)
{
//models.Add(new MSB3.Model(modelName));
var f = new MSB3.Model.Enemy();
f.Name = modelName;
models.Add(f);
}
}
private void CleanupEnemyModels()
{
msb3.Models.Enemies.RemoveAll(model =>
msb3.Parts.Enemies.Count(p => p.ModelName == model.Name) == 0);
}
private void UpdateRowHeaders()
{
foreach (DataGridViewRow row in dataGridView.Rows)
{
row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
}
}
private void CellValidatingHandler(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewColumn column = dataGridView.Columns[e.ColumnIndex];
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
DataGridViewCell cell = row.Cells[e.ColumnIndex];
switch (column.Name)
{
case "ThinkParamID":
case "NPCParamID":
case "EventEntityID":
case "TalkID":
case "CharaInitID":
if (!int.TryParse(e.FormattedValue.ToString(), out _))
{
row.ErrorText = "Invalid integer";
e.Cancel = true;
}
break;
case "Position":
case "Rotation":
try
{
ParseVector3(e.FormattedValue.ToString());
} catch (Exception)
{
row.ErrorText = "Invalid vector3";
e.Cancel = true;
}
break;
}
}
private void CellValidatedHandler(object sender, DataGridViewCellEventArgs e)
{
dataGridView.Rows[e.RowIndex].ErrorText = null;
}
private void CellValueChangedHandler(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0) return;
DataGridViewColumn column = dataGridView.Columns[e.ColumnIndex];
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
DataGridViewCell cell = row.Cells[e.ColumnIndex];
MSB3.Part.Enemy enemy = enemies[e.RowIndex];
switch (column.Name)
{
case "Name":
enemy.Name = (string) cell.Value;
break;
case "ModelName":
enemy.ModelName = (string) cell.Value;
row.Cells["Description"].Value = ChrIDs.f(enemy.ModelName);
AddEnemyModel(enemy.ModelName);
break;
case "ThinkParamID":
enemy.ThinkParamID = (int) cell.Value;
break;
case "NPCParamID":
enemy.NPCParamID = (int) cell.Value;
break;
case "EventEntityID":
enemy.EntityID = (int) cell.Value;
break;
case "TalkID":
enemy.TalkID = (int) cell.Value;
break;
case "CharaInitID":
enemy.CharaInitID = (int)cell.Value;
break;
case "Position":
enemy.Position = ParseVector3((string) cell.Value);
break;
case "Rotation":
enemy.Rotation = ParseVector3((string)cell.Value);
break;
case "Description":
break;
default:
throw new Exception("CellValueChangedHandler missing code path");
}
}
private static Vector3 ParseVector3(string input)
{
char[] charsToTrim = { '<', '>', ' ', '\t' };
string[] vs = input.Split(',').Select(s => s.Trim(charsToTrim)).ToArray();
if (vs.Length == 3)
{
return new Vector3(float.Parse(vs[0]), float.Parse(vs[1]), float.Parse(vs[2]));
} else
{
throw new FormatException("Invalid format for Vector3");
}
}
}
}