Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Community seeds #72

Merged
merged 7 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 42 additions & 28 deletions src/Randomizer.App/RomGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Randomizer.App.ViewModels;
using Randomizer.Shared;
using Randomizer.Shared.Models;
using Randomizer.SMZ3;
using Randomizer.SMZ3.FileData;
using Randomizer.SMZ3.Generation;
using Randomizer.SMZ3.Regions;
Expand Down Expand Up @@ -42,28 +43,36 @@ public RomGenerator(Smz3Randomizer randomizer,
/// <returns>True if the rom was generated successfully, false otherwise</returns>
public bool GenerateRom(RandomizerOptions options, out string path, out string error, out GeneratedRom rom)
{
var bytes = GenerateRomBytes(options, out var seed);

var folderPath = Path.Combine(options.RomOutputPath, $"{DateTimeOffset.Now:yyyyMMdd-HHmmss}_{seed.Seed}");
Directory.CreateDirectory(folderPath);

var romFileName = $"SMZ3_Cas_{DateTimeOffset.Now:yyyyMMdd-HHmmss}_{seed.Seed}.sfc";
var romPath = Path.Combine(folderPath, romFileName);
EnableMsu1Support(options, bytes, romPath, out var msuError);
Rom.UpdateChecksum(bytes);
File.WriteAllBytes(romPath, bytes);
try
{
var bytes = GenerateRomBytes(options, out var seed);
var folderPath = Path.Combine(options.RomOutputPath, $"{DateTimeOffset.Now:yyyyMMdd-HHmmss}_{seed.Seed}");
Directory.CreateDirectory(folderPath);

var spoilerLog = GetSpoilerLog(options, seed);
var spoilerPath = Path.ChangeExtension(romPath, ".txt");
File.WriteAllText(spoilerPath, spoilerLog);
var romFileName = $"SMZ3_Cas_{DateTimeOffset.Now:yyyyMMdd-HHmmss}_{seed.Seed}.sfc";
var romPath = Path.Combine(folderPath, romFileName);
EnableMsu1Support(options, bytes, romPath, out var msuError);
Rom.UpdateChecksum(bytes);
File.WriteAllBytes(romPath, bytes);

rom = SaveSeedToDatabase(options, seed, romPath, spoilerPath);
var spoilerLog = GetSpoilerLog(options, seed);
var spoilerPath = Path.ChangeExtension(romPath, ".txt");
File.WriteAllText(spoilerPath, spoilerLog);

error = msuError;
path = romPath;
rom = SaveSeedToDatabase(options, seed, romPath, spoilerPath);

return true;
error = msuError;
path = romPath;

return true;
}
catch (RandomizerGenerationException e)
{
path = null;
error = $"Error generating rom\n{e.Message}\nPlease try again. If it persists, try modifying your seed settings.";
rom = null;
return false;
}
}

/// <summary>
Expand Down Expand Up @@ -144,12 +153,7 @@ protected byte[] GenerateRomBytes(RandomizerOptions options, out SeedData seed)
/// <returns>The db entry for the generated rom</returns>
protected GeneratedRom SaveSeedToDatabase(RandomizerOptions options, SeedData seed, string romPath, string spoilerPath)
{
var jsonOptions = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};

var settings = JsonSerializer.Serialize(options.ToConfig(), jsonOptions);
var settings = Config.ToConfigString(options.ToConfig(), true);

var rom = new GeneratedRom()
{
Expand Down Expand Up @@ -186,11 +190,21 @@ private string GetSpoilerLog(RandomizerOptions options, SeedData seed)
log.AppendLine(Underline($"SMZ3 Cas’ spoiler log", '='));
log.AppendLine($"Generated on {DateTime.Now:F}");
log.AppendLine($"Seed: {options.SeedOptions.Seed} (actual: {seed.Seed})");
log.AppendLine($"Sword: {options.SeedOptions.SwordLocation}");
log.AppendLine($"Morph: {options.SeedOptions.MorphLocation}");
log.AppendLine($"Bombs: {options.SeedOptions.MorphBombsLocation}");
log.AppendLine($"Shaktool: {options.SeedOptions.ShaktoolItem}");
log.AppendLine($"Peg World: {options.SeedOptions.PegWorldItem}");
log.AppendLine($"Settings String: {Config.ToConfigString(seed.Playthrough.Config, true)}");
log.AppendLine($"Early Items: {string.Join(',', seed.Playthrough.Config.EarlyItems.Select(x => x.ToString()).ToArray())}");

var locationPrefs = new List<string>();
foreach (var (locationId, value) in seed.Playthrough.Config.LocationItems)
{
var itemPref = value < Enum.GetValues(typeof(ItemPool)).Length ? ((ItemPool)value).ToString() : ((ItemType)value).ToString();
locationPrefs.Add($"{seed.Worlds[0].World.Locations.First(x => x.Id == locationId).Name} - {itemPref}");
}
log.AppendLine($"Location Preferences: {string.Join(',', locationPrefs.ToArray())}");

var type = options.LogicConfig.GetType();
var logicOptions = string.Join(',', type.GetProperties().Select(x => $"{x.Name}: {x.GetValue(seed.Playthrough.Config.LogicConfig)}"));
log.AppendLine($"Logic Options: {logicOptions}");

log.AppendLine((options.SeedOptions.Keysanity ? "[Keysanity] " : "")
+ (options.SeedOptions.Race ? "[Race] " : ""));
if (File.Exists(options.PatchOptions.Msu1Path))
Expand Down
97 changes: 70 additions & 27 deletions src/Randomizer.App/ViewModels/RandomizerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -54,7 +55,7 @@ public RandomizerOptions(GeneralOptions generalOptions,
[JsonPropertyName("Logic")]
public LogicConfig LogicConfig { get; set; }

public bool ItemLocationsExpanded { get; set; } = false;
public bool EarlyItemsExpanded { get; set; } = false;

public bool CustomizationExpanded { get; set; } = false;
public bool LogicExpanded { get; set; } = false;
Expand All @@ -65,6 +66,7 @@ public RandomizerOptions(GeneralOptions generalOptions,

public double WindowHeight { get; set; } = 600d;


public string RomOutputPath
{
get => Directory.Exists(GeneralOptions.RomOutputPath)
Expand All @@ -84,34 +86,75 @@ public void Save(string path)
File.WriteAllText(path, json);
}

public Config ToConfig() => new()
public Config ToConfig()
{
GameMode = GameMode.Normal,
Z3Logic = Z3Logic.Normal,
SMLogic = SMLogic.Normal,
ItemLocations =
if (string.IsNullOrWhiteSpace(SeedOptions.ConfigString)) {
return new()
{
GameMode = GameMode.Normal,
Z3Logic = Z3Logic.Normal,
SMLogic = SMLogic.Normal,
ItemLocations =
{
[ItemType.ProgressiveSword] = SeedOptions.SwordLocation,
[ItemType.Morph] = SeedOptions.MorphLocation,
[ItemType.Bombs] = SeedOptions.MorphBombsLocation,
[ItemType.Boots] = SeedOptions.PegasusBootsLocation,
[ItemType.SpaceJump] = SeedOptions.SpaceJumpLocation,
},
ShaktoolItemPool = SeedOptions.ShaktoolItem,
PegWorldItemPool = SeedOptions.PegWorldItem,
KeyShuffle = SeedOptions.Keysanity ? KeyShuffle.Keysanity : KeyShuffle.None,
Race = SeedOptions.Race,
ExtendedMsuSupport = PatchOptions.CanEnableExtendedSoundtrack && PatchOptions.EnableExtendedSoundtrack,
ShuffleDungeonMusic = PatchOptions.ShuffleDungeonMusic,
HeartColor = PatchOptions.HeartColor,
LowHealthBeepSpeed = PatchOptions.LowHealthBeepSpeed,
DisableLowEnergyBeep = PatchOptions.DisableLowEnergyBeep,
CasualSMPatches = PatchOptions.CasualSuperMetroidPatches,
MenuSpeed = PatchOptions.MenuSpeed,
LinkName = PatchOptions.LinkSprite == Sprite.DefaultLink ? "Link" : PatchOptions.LinkSprite.Name,
SamusName = PatchOptions.SamusSprite == Sprite.DefaultSamus ? "Samus" : PatchOptions.SamusSprite.Name,
LocationItems = SeedOptions.LocationItems,
EarlyItems = SeedOptions.EarlyItems,
LogicConfig = LogicConfig.Clone()
};
}
else
{
[ItemType.ProgressiveSword] = SeedOptions.SwordLocation,
[ItemType.Morph] = SeedOptions.MorphLocation,
[ItemType.Bombs] = SeedOptions.MorphBombsLocation,
[ItemType.Boots] = SeedOptions.PegasusBootsLocation,
[ItemType.SpaceJump] = SeedOptions.SpaceJumpLocation,
},
ShaktoolItemPool = SeedOptions.ShaktoolItem,
PegWorldItemPool = SeedOptions.PegWorldItem,
KeyShuffle = SeedOptions.Keysanity ? KeyShuffle.Keysanity : KeyShuffle.None,
Race = SeedOptions.Race,
ExtendedMsuSupport = PatchOptions.CanEnableExtendedSoundtrack && PatchOptions.EnableExtendedSoundtrack,
ShuffleDungeonMusic = PatchOptions.ShuffleDungeonMusic,
HeartColor = PatchOptions.HeartColor,
LowHealthBeepSpeed = PatchOptions.LowHealthBeepSpeed,
DisableLowEnergyBeep = PatchOptions.DisableLowEnergyBeep,
CasualSMPatches = PatchOptions.CasualSuperMetroidPatches,
MenuSpeed = PatchOptions.MenuSpeed,
LinkName = PatchOptions.LinkSprite == Sprite.DefaultLink ? "Link" : PatchOptions.LinkSprite.Name,
SamusName = PatchOptions.SamusSprite == Sprite.DefaultSamus ? "Samus" : PatchOptions.SamusSprite.Name,
LogicConfig = LogicConfig.Clone()
};
var oldConfig = Config.FromConfigString(SeedOptions.ConfigString);
return new Config()
{
GameMode = GameMode.Normal,
Z3Logic = Z3Logic.Normal,
SMLogic = SMLogic.Normal,
ItemLocations =
{
[ItemType.ProgressiveSword] = SeedOptions.SwordLocation,
[ItemType.Morph] = SeedOptions.MorphLocation,
[ItemType.Bombs] = SeedOptions.MorphBombsLocation,
[ItemType.Boots] = SeedOptions.PegasusBootsLocation,
[ItemType.SpaceJump] = SeedOptions.SpaceJumpLocation,
},
ShaktoolItemPool = SeedOptions.ShaktoolItem,
PegWorldItemPool = SeedOptions.PegWorldItem,
KeyShuffle = SeedOptions.Keysanity ? KeyShuffle.Keysanity : KeyShuffle.None,
Race = SeedOptions.Race,
ExtendedMsuSupport = PatchOptions.CanEnableExtendedSoundtrack && PatchOptions.EnableExtendedSoundtrack,
ShuffleDungeonMusic = PatchOptions.ShuffleDungeonMusic,
HeartColor = PatchOptions.HeartColor,
LowHealthBeepSpeed = PatchOptions.LowHealthBeepSpeed,
DisableLowEnergyBeep = PatchOptions.DisableLowEnergyBeep,
CasualSMPatches = PatchOptions.CasualSuperMetroidPatches,
MenuSpeed = PatchOptions.MenuSpeed,
LinkName = PatchOptions.LinkSprite == Sprite.DefaultLink ? "Link" : PatchOptions.LinkSprite.Name,
SamusName = PatchOptions.SamusSprite == Sprite.DefaultSamus ? "Samus" : PatchOptions.SamusSprite.Name,
LocationItems = oldConfig.LocationItems,
EarlyItems = oldConfig.EarlyItems,
LogicConfig = oldConfig.LogicConfig
};
}
}

public RandomizerOptions Clone()
{
Expand Down
9 changes: 8 additions & 1 deletion src/Randomizer.App/ViewModels/SeedOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Windows;

using Randomizer.Shared;
using Randomizer.SMZ3;

namespace Randomizer.App.ViewModels
Expand All @@ -19,6 +19,9 @@ public class SeedOptions
[JsonIgnore]
public string Seed { get; set; }

[JsonIgnore]
public string ConfigString { get; set; }

public ItemPlacement SwordLocation { get; set; }

public ItemPlacement MorphLocation { get; set; }
Expand All @@ -36,5 +39,9 @@ public class SeedOptions
public bool Keysanity { get; set; }

public bool Race { get; set; }

public ISet<ItemType> EarlyItems { get; set; } = new HashSet<ItemType>();

public IDictionary<int, int> LocationItems { get; set; } = new Dictionary<int, int>();
}
}
Loading