forked from az64/mm-rando
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemies.cs
More file actions
293 lines (278 loc) · 10.3 KB
/
Enemies.cs
File metadata and controls
293 lines (278 loc) · 10.3 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace MMRando
{
public partial class ROMFuncs
{
private class Enemy
{
public int Actor = new int();
public int Object = new int();
public int ObjectSize = new int();
public List<int> Variables = new List<int>();
public int Type = new int();
public int Stationary = new int();
public List<int> SceneExclude = new List<int>();
}
private class ValueSwap
{
public int OldV = new int();
public int NewV = new int();
}
static List<Enemy> Enemies;
private static void GetEnemyList()
{
Enemies = new List<Enemy>();
string[] lines = Properties.Resources.ENEMIES.Split(new[] {"\r\n", "\r", "\n"}, StringSplitOptions.None);
int i = 0;
while (i < lines.Length)
{
if (lines[i].StartsWith("-"))
{
i++;
continue;
};
Enemy e = new Enemy();
e.Actor = Convert.ToInt32(lines[i], 16);
e.Object = Convert.ToInt32(lines[i + 1], 16);
e.ObjectSize = GetObjSize(e.Object);
string[] varlist = lines[i + 2].Split(',');
for (int j = 0; j < varlist.Length; j++)
{
e.Variables.Add(Convert.ToInt32(varlist[j], 16));
};
e.Type = Convert.ToInt32(lines[i + 3], 16);
e.Stationary = Convert.ToInt32(lines[i + 4], 16);
if (lines[i + 5] != "")
{
string[] selist = lines[i + 5].Split(',');
for (int j = 0; j < selist.Length; j++)
{
e.SceneExclude.Add(Convert.ToInt32(selist[j], 16));
};
};
Enemies.Add(e);
i += 6;
};
}
private static List<int> GetSceneEnemyActors(Scene S)
{
List<int> ActorList = new List<int>();
for (int i = 0; i < S.Maps.Count; i++)
{
for (int j = 0; j < S.Maps[i].Actors.Count; j++)
{
int k = Enemies.FindIndex(u => u.Actor == S.Maps[i].Actors[j].n);
if (k != -1)
{
if (!Enemies[k].SceneExclude.Contains(S.Number))
{
ActorList.Add(Enemies[k].Actor);
};
};
};
};
return ActorList;
}
private static List<int> GetSceneEnemyObjects(Scene S)
{
List<int> ObjList = new List<int>();
for (int i = 0; i < S.Maps.Count; i++)
{
for (int j = 0; j < S.Maps[i].Objects.Count; j++)
{
int k = Enemies.FindIndex(u => u.Object == S.Maps[i].Objects[j]);
if (k != -1)
{
if (!ObjList.Contains(Enemies[k].Object))
{
if (!Enemies[k].SceneExclude.Contains(S.Number))
{
ObjList.Add(Enemies[k].Object);
};
};
};
};
};
return ObjList;
}
private static void SetSceneEnemyActors(Scene S, List<ValueSwap[]> A)
{
for (int i = 0; i < S.Maps.Count; i++)
{
for (int j = 0; j < S.Maps[i].Actors.Count; j++)
{
int k = A.FindIndex(u => u[0].OldV == S.Maps[i].Actors[j].n);
if (k != -1)
{
S.Maps[i].Actors[j].n = A[k][0].NewV;
S.Maps[i].Actors[j].v = A[k][1].NewV;
A.RemoveAt(k);
};
};
};
}
private static void SetSceneEnemyObjects(Scene S, List<ValueSwap> O)
{
for (int i = 0; i < S.Maps.Count; i++)
{
for (int j = 0; j < S.Maps[i].Objects.Count; j++)
{
int k = O.FindIndex(u => u.OldV == S.Maps[i].Objects[j]);
if (k != -1)
{
S.Maps[i].Objects[j] = O[k].NewV;
};
};
};
}
private static List<Enemy> GetMatchPool(List<Enemy> Actors, Random R)
{
List<Enemy> Pool = new List<Enemy>();
for (int i = 0; i < Actors.Count; i++)
{
Enemy E = Enemies.Find(u => u.Actor == Actors[i].Actor);
for (int j = 0; j < Enemies.Count; j++)
{
if ((Enemies[j].Type == E.Type) && (Enemies[j].Stationary == E.Stationary))
{
if (!Pool.Contains(Enemies[j]))
{
Pool.Add(Enemies[j]);
};
}
else if ((Enemies[j].Type == E.Type) && (R.Next(5) == 0))
{
if (!Pool.Contains(Enemies[j]))
{
Pool.Add(Enemies[j]);
};
};
};
};
return Pool;
}
private static void SwapSceneEnemies(Scene S, Random R)
{
List<int> Actors = GetSceneEnemyActors(S);
if (Actors.Count == 0)
{
return;
};
List<int> Objects = GetSceneEnemyObjects(S);
if (Objects.Count == 0)
{
return;
};
// if actor doesn't exist but object does, probably spawned by something else
List<int> ObjRemove = new List<int>();
foreach (int o in Objects)
{
List<Enemy> ObjectMatch = Enemies.FindAll(u => u.Object == o);
bool exists = false;
for (int i = 0; i < ObjectMatch.Count; i++)
{
exists |= Actors.Contains(ObjectMatch[i].Actor);
};
if (!exists)
{
ObjRemove.Add(o); ;
};
};
foreach (int o in ObjRemove)
{
Objects.Remove(o);
};
List<ValueSwap[]> ActorsUpdate = new List<ValueSwap[]>();
List<ValueSwap> ObjsUpdate;
List<List<Enemy>> Updates;
List<List<Enemy>> Matches;
while (true)
{
ObjsUpdate = new List<ValueSwap>();
Updates = new List<List<Enemy>>();
Matches = new List<List<Enemy>>();
int oldsize = 0;
int newsize = 0;
for (int i = 0; i < Objects.Count; i++)
{
Updates.Add(Enemies.FindAll(u => ((u.Object == Objects[i]) && (Actors.Contains(u.Actor)))));
Matches.Add(GetMatchPool(Updates[i], R));
int k = R.Next(Matches[i].Count);
int newobj = Matches[i][k].Object;
newsize += Matches[i][k].ObjectSize;
oldsize += Updates[i][0].ObjectSize;
ValueSwap NewObject = new ValueSwap();
NewObject.OldV = Objects[i];
NewObject.NewV = newobj;
ObjsUpdate.Add(NewObject);
};
if (newsize <= oldsize)
{
//this should take into account map/scene size and size of all loaded actors...
//not really accurate but *should* work for now to prevent crashing
break;
};
};
for (int i = 0; i < ObjsUpdate.Count; i++)
{
int j = 0;
while (j != Actors.Count)
{
Enemy Old = Updates[i].Find(u => u.Actor == Actors[j]);
if (Old != null)
{
List<Enemy> SubMatches = Matches[i].FindAll(u => u.Object == ObjsUpdate[i].NewV);
int l;
while (true)
{
l = R.Next(SubMatches.Count);
if ((Old.Type == SubMatches[l].Type) && (Old.Stationary == SubMatches[l].Stationary))
{
break;
}
else
{
if ((Old.Type == SubMatches[l].Type) && (R.Next(5) == 0))
{
break;
};
};
};
ValueSwap NewActor = new ValueSwap();
NewActor.OldV = Actors[j];
NewActor.NewV = SubMatches[l].Actor;
ValueSwap NewVar = new ValueSwap();
NewVar.NewV = SubMatches[l].Variables[R.Next(SubMatches[l].Variables.Count)];
ActorsUpdate.Add(new ValueSwap[] { NewActor, NewVar });
Actors.RemoveAt(j);
}
else
{
j++;
};
};
};
SetSceneEnemyActors(S, ActorsUpdate);
SetSceneEnemyObjects(S, ObjsUpdate);
UpdateScene(S);
}
public static void ShuffleEnemies(Random R)
{
int[] SceneSkip = new int[] { 0x08, 0x20, 0x24, 0x4F, 0x69 };
GetEnemyList();
ReadSceneTable();
GetMaps();
GetMapHeaders();
GetActors();
for (int i = 0; i < SceneList.Count; i++)
{
if (!SceneSkip.Contains(SceneList[i].Number))
{
SwapSceneEnemies(SceneList[i], R);
};
};
}
}
}