Skip to content

Commit

Permalink
Add options to choose what to randomize
Browse files Browse the repository at this point in the history
  • Loading branch information
clementgallet committed Jan 31, 2021
1 parent 47640a2 commit c075d05
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 38 deletions.
71 changes: 66 additions & 5 deletions SWD2Randomizer/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 52 additions & 24 deletions SWD2Randomizer/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,34 @@ private void randomizeBtn_Click(object sender, EventArgs e)
string zipFile = Path.Combine(Directory.GetCurrentDirectory(), "data01.impak.zip");
string extractedDir = Path.Combine(Directory.GetCurrentDirectory(), "data01");

string enfile = Path.Combine(baseDir, "Language", "en.csv.z");
using (FileStream originalFileStream = File.OpenRead(enfile))
{
originalFileStream.Seek(6, SeekOrigin.Begin);
string newFileName = enfile.Remove(enfile.Length - 2);

using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (DeflateStream decompressionStream = new DeflateStream(originalFileStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
}
}
}

StringBuilder logsb = new StringBuilder();

/* Generate a new seed if blank */
if (string.IsNullOrWhiteSpace(textSeed.Text))
{
SetSeedBasedOnDifficulty();
SetSeedBasedOnSettings();
}
else
{
GetSettingsFromSeed();
}

string difficulty = GetRandomizerDifficulty();
string difficulty = listDifficulty.SelectedItem.ToString();

logsb.Append("Selecting difficulty is ").AppendLine(difficulty);
logText.Text = logsb.ToString();
Expand Down Expand Up @@ -206,7 +225,7 @@ private void randomizeBtn_Click(object sender, EventArgs e)
locations = new LocationsCasual().Locations;
}

Randomizer randomizer = new Randomizer(extractedDir, parsedSeed, locations);
Randomizer randomizer = new Randomizer(extractedDir, parsedSeed, locations, checkItems.Checked, checkAreas.Checked, checkResources.Checked, checkUpgrades.Checked);

logsb.Append("Randomize");
logText.Text = logsb.ToString();
Expand Down Expand Up @@ -286,49 +305,58 @@ private void randomizeBtn_Click(object sender, EventArgs e)

}

private void SetSeedBasedOnDifficulty()
private void SetSeedBasedOnSettings()
{
textSeed.Text = "";
switch (listDifficulty.SelectedItem.ToString())
{
case "Casual":
textSeed.Text = string.Format("C{0:0000000}", (new SeedRandom()).Next(10000000));
textSeed.Text += "C";
break;
default:
textSeed.Text = string.Format("S{0:0000000}", (new SeedRandom()).Next(10000000));
textSeed.Text += "S";
break;
}
if (checkItems.Checked) { textSeed.Text += "I"; }
if (checkAreas.Checked) { textSeed.Text += "A"; }
if (checkResources.Checked) { textSeed.Text += "R"; }
if (checkUpgrades.Checked) { textSeed.Text += "U"; }

textSeed.Text += string.Format("{0:0000000}", (new SeedRandom()).Next(10000000));
}

private string GetRandomizerDifficulty()
private void GetSettingsFromSeed()
{
string difficulty = "Speedrunner";

if (textSeed.Text.ToUpper().Contains("C"))
{
difficulty = "Casual";
listDifficulty.SelectedItem = "Casual";
}
else if (textSeed.Text.ToUpper().Contains("S"))
{
difficulty = "Speedrunner";
listDifficulty.SelectedItem = "Speedrunner";
}

listDifficulty.SelectedItem = difficulty;

return difficulty;
checkItems.Checked = textSeed.Text.ToUpper().Contains("I");
checkAreas.Checked = textSeed.Text.ToUpper().Contains("A");
checkResources.Checked = textSeed.Text.ToUpper().Contains("R");
checkUpgrades.Checked = textSeed.Text.ToUpper().Contains("U");
}

private string GetSeed()
{
if (textSeed.Text.ToUpper().Contains("C"))
{
return textSeed.Text.ToUpper().Replace("C", "");
}
else if (textSeed.Text.ToUpper().Contains("S"))
{
return textSeed.Text.ToUpper().Replace("S", "");
}
MessageBox.Show("The seed string is unrecognized.", "Seed Difficulty", MessageBoxButtons.OK, MessageBoxIcon.Error);
return "";
string seed = textSeed.Text.ToUpper();
seed = seed.Replace("C", "");
seed = seed.Replace("S", "");
seed = seed.Replace("I", "");
seed = seed.Replace("A", "");
seed = seed.Replace("R", "");
seed = seed.Replace("U", "");
return seed;
}

private void groupBox2_Enter(object sender, EventArgs e)
{

}
}
}
4 changes: 2 additions & 2 deletions SWD2Randomizer/Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void PatchOasis()
}

XmlNode hubIntro = doc.SelectSingleNode("//Level[@Name='the_hub']/LayerFilters/Filter[@Layer='filter_intro']");
hubIntro.ParentNode.RemoveChild(hubIntro);
hubIntro.InnerXml = @"<Disable />";

XmlNode hubPostIntro = doc.SelectSingleNode("//Level[@Name='the_hub']/LayerFilters/Filter[@Layer='filter_post_intro']");
hubPostIntro.InnerXml = @"
Expand Down Expand Up @@ -288,7 +288,7 @@ public void PatchIntro()

checkQuestProp.InnerXml = @"
<Id>32566045</Id>
<Name>ProgressQuest5</Name>
<Name>ProgressQuest6</Name>
<Position>2389, 2036</Position>
<Definition>ProgressQuest</Definition>
<Area>2280, 2004, 216, 63</Area>
Expand Down
35 changes: 28 additions & 7 deletions SWD2Randomizer/Randomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,39 @@ class Randomizer
private readonly int seed;
private List<string> haveFlags;
private List<Location> locations;
private bool items;
private bool areas;
private bool resources;
private bool upgrades;

public Randomizer(string baseDir, int seed, List<Location> locations)
public Randomizer(string baseDir, int seed, List<Location> locations, bool items, bool areas, bool resources, bool upgrades)
{
random = new SeedRandom(seed);
this.locations = locations;
this.seed = seed;
this.baseDir = baseDir;
this.items = items;
this.areas = areas;
this.resources = resources;
this.upgrades = upgrades;
haveFlags = new List<string>();
}

public int Randomize()
{
bool ret;

ret = PermuteFlags(Location.RandomizeType.Upgrade);
if (!ret) return -1;
if (items)
{
ret = PermuteFlags(Location.RandomizeType.Upgrade);
if (!ret) return -1;
}

ret = PermuteFlags(Location.RandomizeType.Area);
if (!ret) return -1;
if (areas)
{
ret = PermuteFlags(Location.RandomizeType.Area);
if (!ret) return -1;
}

ret = CheckValid();
if (!ret) return 0;
Expand Down Expand Up @@ -168,8 +182,15 @@ public int Randomize()
doc.Save(patchFile);
}

RandomizeResources();
RandomizeCogUpgrades();
if (resources)
{
RandomizeResources();
}

if (upgrades)
{
RandomizeCogUpgrades();
}

return 1;
}
Expand Down

0 comments on commit c075d05

Please sign in to comment.