forked from ZoeyZolotova/mm-rando
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnemies.cs
286 lines (274 loc) · 10.4 KB
/
Enemies.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
using MMRando.Models.Rom;
using MMRando.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MMRando
{
public class Enemies
{
public class ValueSwap
{
public int OldV;
public int NewV;
}
private static List<Enemy> EnemyList { get; set; }
public static void ReadEnemyList()
{
EnemyList = 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 = ObjUtils.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));
}
}
EnemyList.Add(e);
i += 6;
}
}
public static List<int> GetSceneEnemyActors(Scene scene)
{
List<int> ActorList = new List<int>();
for (int i = 0; i < scene.Maps.Count; i++)
{
for (int j = 0; j < scene.Maps[i].Actors.Count; j++)
{
int k = EnemyList.FindIndex(u => u.Actor == scene.Maps[i].Actors[j].n);
if (k != -1)
{
if (!EnemyList[k].SceneExclude.Contains(scene.Number))
{
ActorList.Add(EnemyList[k].Actor);
}
}
}
}
return ActorList;
}
public static List<int> GetSceneEnemyObjects(Scene scene)
{
List<int> ObjList = new List<int>();
for (int i = 0; i < scene.Maps.Count; i++)
{
for (int j = 0; j < scene.Maps[i].Objects.Count; j++)
{
int k = EnemyList.FindIndex(u => u.Object == scene.Maps[i].Objects[j]);
if (k != -1)
{
if (!ObjList.Contains(EnemyList[k].Object))
{
if (!EnemyList[k].SceneExclude.Contains(scene.Number))
{
ObjList.Add(EnemyList[k].Object);
}
}
}
}
}
return ObjList;
}
public static void SetSceneEnemyActors(Scene scene, List<ValueSwap[]> A)
{
for (int i = 0; i < scene.Maps.Count; i++)
{
for (int j = 0; j < scene.Maps[i].Actors.Count; j++)
{
int k = A.FindIndex(u => u[0].OldV == scene.Maps[i].Actors[j].n);
if (k != -1)
{
scene.Maps[i].Actors[j].n = A[k][0].NewV;
scene.Maps[i].Actors[j].v = A[k][1].NewV;
A.RemoveAt(k);
}
}
}
}
public static void SetSceneEnemyObjects(Scene scene, List<ValueSwap> O)
{
for (int i = 0; i < scene.Maps.Count; i++)
{
for (int j = 0; j < scene.Maps[i].Objects.Count; j++)
{
int k = O.FindIndex(u => u.OldV == scene.Maps[i].Objects[j]);
if (k != -1)
{
scene.Maps[i].Objects[j] = O[k].NewV;
}
}
}
}
public 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 = EnemyList.Find(u => u.Actor == Actors[i].Actor);
for (int j = 0; j < EnemyList.Count; j++)
{
if ((EnemyList[j].Type == E.Type) && (EnemyList[j].Stationary == E.Stationary))
{
if (!Pool.Contains(EnemyList[j]))
{
Pool.Add(EnemyList[j]);
}
}
else if ((EnemyList[j].Type == E.Type) && (R.Next(5) == 0))
{
if (!Pool.Contains(EnemyList[j]))
{
Pool.Add(EnemyList[j]);
}
}
}
}
return Pool;
}
public static void SwapSceneEnemies(Scene scene, Random rng)
{
List<int> Actors = GetSceneEnemyActors(scene);
if (Actors.Count == 0)
{
return;
}
List<int> Objects = GetSceneEnemyObjects(scene);
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 = EnemyList.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(EnemyList.FindAll(u => ((u.Object == Objects[i]) && (Actors.Contains(u.Actor)))));
Matches.Add(GetMatchPool(Updates[i], rng));
int k = rng.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 = rng.Next(SubMatches.Count);
if ((Old.Type == SubMatches[l].Type) && (Old.Stationary == SubMatches[l].Stationary))
{
break;
}
else
{
if ((Old.Type == SubMatches[l].Type) && (rng.Next(5) == 0))
{
break;
}
}
if (SubMatches.FindIndex(u => u.Type == Old.Type) == -1)
{
break;
}
}
ValueSwap NewActor = new ValueSwap();
NewActor.OldV = Actors[j];
NewActor.NewV = SubMatches[l].Actor;
ValueSwap NewVar = new ValueSwap();
NewVar.NewV = SubMatches[l].Variables[rng.Next(SubMatches[l].Variables.Count)];
ActorsUpdate.Add(new ValueSwap[] { NewActor, NewVar });
Actors.RemoveAt(j);
}
else
{
j++;
}
}
}
SetSceneEnemyActors(scene, ActorsUpdate);
SetSceneEnemyObjects(scene, ObjsUpdate);
SceneUtils.UpdateScene(scene);
}
public static void ShuffleEnemies(Random random)
{
int[] SceneSkip = new int[] { 0x08, 0x20, 0x24, 0x4F, 0x69 };
ReadEnemyList();
SceneUtils.ReadSceneTable();
SceneUtils.GetMaps();
SceneUtils.GetMapHeaders();
SceneUtils.GetActors();
for (int i = 0; i < RomData.SceneList.Count; i++)
{
if (!SceneSkip.Contains(RomData.SceneList[i].Number))
{
SwapSceneEnemies(RomData.SceneList[i], random);
}
}
}
}
}