-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRandomizerV11.cs
291 lines (228 loc) · 9.92 KB
/
RandomizerV11.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
using System.Collections.Generic;
using System.IO;
using SuperMetroidRandomizer.IO;
using SuperMetroidRandomizer.Net;
using SuperMetroidRandomizer.Properties;
using SuperMetroidRandomizer.Rom;
namespace SuperMetroidRandomizer.Random
{
public enum RandomizerDifficulty
{
None,
Casual,
Speedrunner,
Masochist,
FunTime,
Insane,
}
public class RandomizerV11
{
private static SeedRandom random;
private List<ItemType> haveItems;
private List<ItemType> itemPool;
private readonly int seed;
private readonly IRomLocations romLocations;
private RandomizerLog log;
public RandomizerV11(int seed, IRomLocations romLocations, RandomizerLog log)
{
random = new SeedRandom(seed);
this.romLocations = romLocations;
this.seed = seed;
this.log = log;
}
public string CreateRom(string filename, bool spoilerOnly = false)
{
if (filename.Contains("\\") && !Directory.Exists(filename.Substring(0, filename.LastIndexOf('\\'))))
{
Directory.CreateDirectory(filename.Substring(0, filename.LastIndexOf('\\')));
}
GenerateItemList();
GenerateItemPositions();
WriteRom(filename);
if (spoilerOnly)
{
return log.GetLogOutput();
}
WriteRom(filename);
return "";
}
private void WriteRom(string filename)
{
string usedFilename = FileName.Fix(filename, string.Format(romLocations.SeedFileString, seed));
var hideLocations = !(romLocations is RomLocationsCasual);
using (var rom = new FileStream(usedFilename, FileMode.OpenOrCreate))
{
//rom.Write(Resources.RomImageSMPB072VP, 0, 3211264);
//For the vanilla palettes version
rom.Write(Resources.RomImage073, 0, 3211264);
foreach (var location in romLocations.Locations)
{
rom.Seek(location.Address, SeekOrigin.Begin);
var newItem = new byte[2];
if (!location.NoHidden && location.Item.Type != ItemType.Nothing && location.Item.Type != ItemType.ChargeBeam && location.ItemStorageType == ItemStorageType.Normal)
{
// hide the item half of the time (to be a jerk)
if (hideLocations && random.Next(2) == 0)
{
location.ItemStorageType = ItemStorageType.Hidden;
}
}
switch (location.ItemStorageType)
{
case ItemStorageType.Normal:
newItem = StringToByteArray(location.Item.Normal);
break;
case ItemStorageType.Hidden:
newItem = StringToByteArray(location.Item.Hidden);
break;
case ItemStorageType.Chozo:
newItem = StringToByteArray(location.Item.Chozo);
break;
}
rom.Write(newItem, 0, 2);
if (location.Item.Type == ItemType.Nothing)
{
// give same index as morph ball
rom.Seek(location.Address + 4, SeekOrigin.Begin);
rom.Write(StringToByteArray("\x1a"), 0, 1);
}
if (location.Item.Type == ItemType.ChargeBeam)
{
// we have 4 copies of charge to reduce tedium, give them all the same index
//I chose to remove this for now to make it a bit more obvious that there aren't mistakes in the randomizing process
rom.Seek(location.Address + 4, SeekOrigin.Begin);
rom.Write(StringToByteArray("\xfa"), 0, 1);
}
}
WriteSeedInRom(rom);
WriteControls(rom);
rom.Close();
}
if (log != null)
{
log.WriteLog(usedFilename);
}
}
private void WriteControls(FileStream rom)
{
foreach (var address in Controller.ShotAddresses)
{
rom.Seek(address, SeekOrigin.Begin);
rom.Write(StringToByteArray(Controller.Buttons[Settings.Default.ControlsShot]), 0, 2);
}
foreach (var address in Controller.JumpAddresses)
{
rom.Seek(address, SeekOrigin.Begin);
rom.Write(StringToByteArray(Controller.Buttons[Settings.Default.ControlsJump]), 0, 2);
}
foreach (var address in Controller.DashAddresses)
{
rom.Seek(address, SeekOrigin.Begin);
rom.Write(StringToByteArray(Controller.Buttons[Settings.Default.ControlsDash]), 0, 2);
}
foreach (var address in Controller.ItemSelectAddresses)
{
rom.Seek(address, SeekOrigin.Begin);
rom.Write(StringToByteArray(Controller.Buttons[Settings.Default.ControlsItemSelect]), 0, 2);
}
foreach (var address in Controller.ItemCancelAddresses)
{
rom.Seek(address, SeekOrigin.Begin);
rom.Write(StringToByteArray(Controller.Buttons[Settings.Default.ControlsItemCancel]), 0, 2);
}
foreach (var address in Controller.AngleUpAddresses)
{
rom.Seek(address, SeekOrigin.Begin);
rom.Write(StringToByteArray(Controller.Buttons[Settings.Default.ControlsAngleUp]), 0, 2);
}
foreach (var address in Controller.AngleDownAddresses)
{
rom.Seek(address, SeekOrigin.Begin);
rom.Write(StringToByteArray(Controller.Buttons[Settings.Default.ControlsAngleDown]), 0, 2);
}
// foreach (var address in Controller.MoonwalkAddress)
// {
// rom.Seek(address, SeekOrigin.Begin);
//
// rom.Write(StringToByteArray(Controller.Buttons[Settings.Default.ControlsMoonwalk]), 0, 2);
// }
}
private void WriteSeedInRom(FileStream rom)
{
string seedStr = string.Format(romLocations.SeedRomString, RandomizerVersion.Current, seed.ToString().PadLeft(7, '0')).PadRight(21).Substring(0, 21);
rom.Seek(0x7fc0, SeekOrigin.Begin);
rom.Write(StringToByteArray(seedStr), 0, 21);
}
private static byte[] StringToByteArray(string input)
{
var retVal = new byte[input.Length];
var i = 0;
foreach (var ch in input)
{
retVal[i] = (byte)ch;
i++;
}
return retVal;
}
private void GenerateItemPositions()
{
do
{
var currentLocations = romLocations.GetAvailableLocations(haveItems);
var candidateItemList = new List<ItemType>();
// Generate candidate item list
foreach (var candidateItem in itemPool)
{
haveItems.Add(candidateItem);
var newLocations = romLocations.GetAvailableLocations(haveItems);
if (newLocations.Count > currentLocations.Count)
{
romLocations.TryInsertCandidateItem(currentLocations, candidateItemList, candidateItem);
}
haveItems.Remove(candidateItem);
}
// Grab an item from the candidate list if there are any, otherwise, grab a random item
if (candidateItemList.Count > 0)
{
var insertedItem = candidateItemList[random.Next(candidateItemList.Count)];
itemPool.Remove(insertedItem);
haveItems.Add(insertedItem);
int insertedLocation = romLocations.GetInsertedLocation(currentLocations, insertedItem, random);
currentLocations[insertedLocation].Item = new Item(insertedItem);
if (log != null)
{
log.AddOrderedItem(currentLocations[insertedLocation]);
}
}
else
{
ItemType insertedItem = romLocations.GetInsertedItem(currentLocations, itemPool, random);
itemPool.Remove(insertedItem);
haveItems.Add(insertedItem);
int insertedLocation = romLocations.GetInsertedLocation(currentLocations, insertedItem, random);
currentLocations[insertedLocation].Item = new Item(insertedItem);
}
} while (itemPool.Count > 0);
var unavailableLocations = romLocations.GetUnavailableLocations(haveItems);
foreach (var unavailableLocation in unavailableLocations)
{
unavailableLocation.Item = new Item(ItemType.Nothing);
}
if (log != null)
{
log.AddGeneratedItems(romLocations.Locations);
}
}
private void GenerateItemList()
{
romLocations.ResetLocations();
haveItems = new List<ItemType>();
itemPool = romLocations.GetItemPool(random);
var unavailableLocations = romLocations.GetUnavailableLocations(itemPool);
for (int i = itemPool.Count; i < 104 - unavailableLocations.Count; i++)
{
itemPool.Add(ItemType.Nothing);
}
}
}
}