Skip to content

Instantly share code, notes, and snippets.

@NavidK0
Last active January 18, 2021 09:45
Show Gist options
  • Save NavidK0/a3c3fc69e45a5c0df29574ae4493055e to your computer and use it in GitHub Desktop.
Save NavidK0/a3c3fc69e45a5c0df29574ae4493055e to your computer and use it in GitHub Desktop.
ValidSeedFinder for Dungeon Architect (Unity)
using System.IO;
using DungeonArchitect;
using DungeonArchitect.Builders.Snap;
using Sirenix.OdinInspector;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
namespace NF.EditorRuntime
{
/// <summary>
/// A small script I wrote that will find and cache all the valid seeds it can find within an attempt range.
///
/// This allows you to cache of dungeon seeds without having to have the load of finding a valid
/// dungeon at runtime.
///
/// -- Navid
/// </summary>
public class FindValidSeeds : MonoBehaviour
{
public Dungeon Dungeon;
public int Preferred = 10000;
public int MaxTries = 15000;
[Button]
public void SearchAndSave()
{
var dungeonActiveModel = (SnapModel) Dungeon.ActiveModel;
var seedCount = 0;
var text = "";
// Try for x iterations
for (var i = 0; i < MaxTries && seedCount < Preferred; i++)
{
float progress = (float) (seedCount) / Preferred;
if (EditorUtility.DisplayCancelableProgressBar(
$"Find seeds: {Dungeon.name} ({i}/{MaxTries})",
$"Found: {seedCount} ({progress:0.00%})\tBad: {i - seedCount} ({(float) (i - seedCount) / i:0.00%})",
progress)
)
{
Debug.Log("Canceled by user!");
break;
}
Dungeon.Config.Seed = (uint) i;
Dungeon.Build();
if (dungeonActiveModel.modules.Length > 0)
{
text += Dungeon.Config.Seed + ",";
seedCount++;
}
}
EditorUtility.ClearProgressBar();
Dungeon.Config.Seed = 0;
text = text.Substring(0, text.Length - 1);
Debug.Log(seedCount + " valid seed(s) found out of " + MaxTries + " tested.");
string path = EditorUtility.SaveFilePanel("Save Cached Seeds", "Assets/Data", $"CachedSeeds_{Dungeon.name}",
"txt");
if (string.IsNullOrWhiteSpace(path))
{
Debug.LogError("Directory was not chosen!");
return;
}
File.WriteAllText(path, text);
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment