Skip to content

Commit

Permalink
Add support for reading a json of single-spawner data (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: Lusamine <[email protected]>
  • Loading branch information
kwsch and Lusamine authored Mar 28, 2022
1 parent e408e42 commit 1a25c31
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 1 deletion.
File renamed without changes.
17 changes: 17 additions & 0 deletions PermuteMMO/Program.cs → PermuteMMO.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
// Change the criteria for emitting matches here.
PermuteMeta.SatisfyCriteria = (result, advances) => result.IsShiny;

// If a spawner json exists, spawn from that instead
const string json = "spawner.json";
if (File.Exists(json))
{
var info = JsonDecoder.Deserialize<UserEnteredSpawnInfo>(File.ReadAllText(json));
var spawner = info.GetSpawn();
SpawnGenerator.MaxShinyRolls = spawner.Type is SpawnType.MMO ? 17 : 32;
ConsolePermuter.PermuteSingle(spawner, info.GetSeed(), info.Species);

Console.WriteLine("Press [ENTER] to exit.");
Console.ReadLine();
return;
}

const string file = "combo.bin";
Span<byte> data_mo, data_mmo;
if (File.Exists(file))
Expand Down Expand Up @@ -31,3 +45,6 @@
Console.WriteLine();
Console.WriteLine("==========");
ConsolePermuter.PermuteBlockMassOutbreak(data_mo);

Console.WriteLine("Press [ENTER] to exit.");
Console.ReadLine();
3 changes: 3 additions & 0 deletions PermuteMMO.Lib/Generation/SpawnGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class SpawnGenerator
public static PokedexSave8a Pokedex => SaveFile.PokedexSave;
public static byte[] BackingArray => SaveFile.Blocks.GetBlock(0x02168706).Data;
public static bool HasCharm { get; set; } = true;
public static int MaxShinyRolls { get; set; }
#endregion

private static SAV8LA GetFake()
Expand Down Expand Up @@ -98,6 +99,8 @@ private static SlotDetail[] GetFakeOutbreak(ushort species)

private static int GetRerollCount(in int species, SpawnType type)
{
if (MaxShinyRolls is not 0)
return MaxShinyRolls;
bool perfect = Pokedex.IsPerfect(species);
bool complete = Pokedex.IsComplete(species);
return 1 + (complete ? 1 : 0) + (perfect ? 2 : 0) + (HasCharm ? 3 : 0) + (int)type;
Expand Down
5 changes: 5 additions & 0 deletions PermuteMMO.Lib/Util/JsonDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace PermuteMMO.Lib;
/// </summary>
public static class JsonDecoder
{
/// <summary>
/// Wrapper to deserialize the json using whatever package this project is currently using.
/// </summary>
public static T Deserialize<T>(string json) where T : class => JsonConvert.DeserializeObject<T>(json);

/// <summary>
/// Converts the json string back to a usable dictionary.
/// </summary>
Expand Down
41 changes: 41 additions & 0 deletions PermuteMMO.Lib/Util/UserEnteredSpawnInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Globalization;

namespace PermuteMMO.Lib;

public sealed record UserEnteredSpawnInfo
{
public ushort Species { get; set; }
public string Seed { get; set; } = string.Empty;

public ulong GetSeed() => ulong.Parse(Seed);

public int BaseCount { get; set; }
public string BaseTable { get; set; } = string.Empty;

public int BonusCount { get; set; }
public string BonusTable { get; set; } = string.Empty;

public SpawnInfo GetSpawn()
{
var table = Parse(BaseTable);
var bonus = Parse(BonusTable);
if (table < 1000)
table = Species;

return new SpawnInfo
{
BaseCount = BaseCount,
BonusCount = BonusCount,
BaseTable = table,
BonusTable = bonus,
Type = bonus is not 0 ? SpawnType.MMO : SpawnType.Outbreak,
};
}

private static ulong Parse(string hex)
{
if (hex.StartsWith("0x"))
hex = hex[2..];
return ulong.Parse(hex, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
}
34 changes: 34 additions & 0 deletions PermuteMMO.Tests/UtilTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Diagnostics;
using System.IO;
using Newtonsoft.Json;
using PermuteMMO.Lib;
using PKHeX.Core;
using Xunit;

namespace PermuteMMO.Tests;

public static class UtilTests
{
[Fact]
public static void CreateJson()
{
var obj = new UserEnteredSpawnInfo
{
Species = (int)Species.Diglett,
Seed = 0xDEADBABE_BEEFCAFE.ToString(),
BaseCount = 10,
BaseTable = $"0x{0x1122_10F4_7DE9_8115:X16}",
BonusCount = 0,
BonusTable = $"0x{0:X16}",
};

var fileName = Path.Combine(Environment.CurrentDirectory, "spawner.json");
var settings = new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Populate };
var result = JsonConvert.SerializeObject(obj, Formatting.Indented, settings);
File.WriteAllText(fileName, result);

string argument = "/select, \"" + fileName + "\"";
Process.Start("explorer.exe", argument);
}
}
2 changes: 1 addition & 1 deletion PermuteMMO.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32210.238
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PermuteMMO.ConsoleApp", "PermuteMMO\PermuteMMO.ConsoleApp.csproj", "{D35AAC6B-C2BA-4573-8C2F-F3F135E0902F}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PermuteMMO.ConsoleApp", "PermuteMMO.ConsoleApp\PermuteMMO.ConsoleApp.csproj", "{D35AAC6B-C2BA-4573-8C2F-F3F135E0902F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PermuteMMO.Lib", "PermuteMMO.Lib\PermuteMMO.Lib.csproj", "{91D75103-E981-4FCC-9D3F-F5086641B057}"
EndProject
Expand Down

0 comments on commit 1a25c31

Please sign in to comment.