Files
linuxdistro-roguelite/Assets/Scripts/Run/RunRouteMap.cs
T

138 lines
7.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace KernelPanic.Run
{
public sealed class RouteNode
{
public int Wave { get; }
public int Lane { get; }
public int Id => Wave * 10 + Lane;
public WaveRoute Route { get; }
public RouteNode(int wave, int lane, WaveRoute route) { Wave = wave; Lane = lane; Route = route; }
}
[Serializable]
public sealed class RouteVisit
{
public int wave, nodeId;
public WaveRoute route;
public string outcome;
public bool cleared;
public RouteVisit Copy() => (RouteVisit)MemberwiseClone();
}
/// <summary>Stable topology and public event previews. Never consumes combat or shop randomness.</summary>
public static class RunRouteMap
{
private static uint Hash(int seed, int wave)
{
uint value = unchecked((uint)seed ^ (uint)wave * 0x9E3779B9u ^ 0xA37151E7u);
value ^= value >> 16; value = unchecked(value * 0x7FEB352Du);
value ^= value >> 15; return value;
}
public static IReadOnlyList<RouteNode> Row(int seed, int wave, int startWave = 2)
=> Row(seed, wave, startWave, 2);
public static IReadOnlyList<RouteNode> Row(int seed, int wave, int startWave, int revision)
{
if (wave < startWave) return Array.Empty<RouteNode>();
if (revision >= 2)
{
if (wave == RunJourney.BossWave)
return new[] { new RouteNode(wave, 1, WaveRoute.Encounter) };
if (wave == startWave)
return new[] { new RouteNode(wave, 0, WaveRoute.Repair), new RouteNode(wave, 1, WaveRoute.Challenge), new RouteNode(wave, 2, WaveRoute.Sacrifice) };
uint roll = Hash(seed, wave);
// Three lanes keep two alternatives reachable even at the outer edge.
// Occasional four-lane tiers widen the route, then merge back on even waves.
int width = wave % 2 == 1 && (roll & 1) != 0 ? 4 : 3;
// Every tier has recovery and a useful alternative; at most one optional elite.
var utility = new[] { WaveRoute.Forge, WaveRoute.Cache, WaveRoute.Recycle, WaveRoute.Sacrifice };
var routes = new List<WaveRoute> { WaveRoute.Repair, utility[(roll >> 4) % 4] };
bool elite = wave >= 7 || (wave >= 4 && (roll & 8) != 0);
if (width > 2) routes.Add(elite ? WaveRoute.Challenge : utility[((roll >> 4) + 1) % 4]);
if (width > 3) routes.Add(utility[((roll >> 4) + 2) % 4]);
// Keep recovery near the middle, instead of isolating it on an edge path.
(routes[0], routes[1]) = (routes[1], routes[0]);
return routes.Select((route, lane) => new RouteNode(wave, lane, route)).ToArray();
}
if (wave == startWave)
return new[] { new RouteNode(wave, 0, WaveRoute.Repair), new RouteNode(wave, 1, WaveRoute.Challenge), new RouteNode(wave, 2, WaveRoute.Sacrifice) };
if (wave == RunJourney.LegacyBossWave || (wave - startWave) % 3 == 2)
return new[] { new RouteNode(wave, 1, WaveRoute.Encounter) };
uint hash = Hash(seed, wave);
var pool = new[] { WaveRoute.Cache, WaveRoute.Forge, WaveRoute.Recycle, WaveRoute.Repair, WaveRoute.Challenge, WaveRoute.Sacrifice };
int count = (hash & 1) == 0 ? 2 : 3;
int offset = (int)((hash >> 4) % (uint)pool.Length);
return Enumerable.Range(0, count).Select(i => new RouteNode(wave, count == 2 ? i * 2 : i,
pool[(offset + i) % pool.Length])).ToArray();
}
public static bool Connected(int seed, RouteNode from, RouteNode to, int startWave = 2)
=> Connected(seed, from, to, startWave, 2);
public static bool Connected(int seed, RouteNode from, RouteNode to, int startWave, int revision)
{
if (from == null || to == null || to.Wave != from.Wave + 1) return false;
var before = Row(seed, from.Wave, startWave, revision).ToList();
var after = Row(seed, to.Wave, startWave, revision).ToList();
int a = before.FindIndex(n => n.Id == from.Id), b = after.FindIndex(n => n.Id == to.Id);
if (a < 0 || b < 0) return false;
if (before.Count == 1 || after.Count == 1) return true;
if (revision >= 2)
{
// Neighbouring lanes branch and merge, never jump across the whole map.
// Normalized positions keep every lane connected when tier widths change.
float x = (float)a / (before.Count - 1), y = (float)b / (after.Count - 1);
if (before.Count == 3 && after.Count == 4 && a == 1)
return b != ((Hash(seed, from.Wave) & 1) == 0 ? 3 : 0);
return Math.Abs(x - y) <= .51f;
}
uint hash = Hash(seed, from.Wave);
int target = (hash & 2) == 0 ? a % after.Count : after.Count - 1 - a % after.Count;
// A spanning set gives every node an entrance and exit; optional side paths add forks.
if (b == target) return true;
bool hasEntrance = Enumerable.Range(0, before.Count).Any(i =>
((hash & 2) == 0 ? i % after.Count : after.Count - 1 - i % after.Count) == b);
if (!hasEntrance && a == 0) return true;
return ((hash >> (a + 3)) & 1) != 0 && b == (target + 1) % after.Count;
}
public static IReadOnlyList<RouteNode> Available(int seed, int wave, RunJourney journey)
{
var row = Row(seed, wave, journey.mapStartWave, journey.mapRevision);
var previous = journey.visits?.LastOrDefault(v => v.wave == wave - 1);
if (previous == null) return row;
var from = Row(seed, wave - 1, journey.mapStartWave, journey.mapRevision).FirstOrDefault(n => n.Id == previous.nodeId);
return row.Where(to => Connected(seed, from, to, journey.mapStartWave, journey.mapRevision)).ToArray();
}
public static bool IsEvent(WaveRoute route) => route is WaveRoute.Forge or WaveRoute.Sacrifice or WaveRoute.Recycle;
public static string Title(WaveRoute route) => route switch
{
WaveRoute.Repair => "Repair stop", WaveRoute.Challenge => "Elite detour",
WaveRoute.Cache => "Lost cache", WaveRoute.Forge => "Patch workshop",
WaveRoute.Sacrifice => "Sacrifice altar", WaveRoute.Recycle => "Recycler",
_ => "Direct route"
};
public static string Symbol(WaveRoute route) => route switch
{
WaveRoute.Repair => "+", WaveRoute.Challenge => "!", WaveRoute.Cache => "$",
WaveRoute.Forge => "^", WaveRoute.Sacrifice => "X", WaveRoute.Recycle => "-", _ => ">"
};
public static string Detail(WaveRoute route) => route switch
{
WaveRoute.Repair => "Restore up to 6 Uptime on departure.",
WaveRoute.Challenge => $"Fight an extra Daemon. On clear: +{RunJourney.ChallengeBits} Bits and a draw rider on the first eligible card; +{RunJourney.ChallengeBits} more Bits if none is eligible.",
WaveRoute.Cache => "Recover 6 Bits on departure. No extra enemy.",
WaveRoute.Forge => "Choose a card to gain +2 effect magnitude for this run. You may pass.",
WaveRoute.Sacrifice => "Remove one card from this run's deck. Give a different card +4 effect magnitude for this run. You may pass.",
WaveRoute.Recycle => "Remove one card from this run's deck and recover 4 Bits. You may pass.",
_ => "A direct continuation to the next battle. No extra reward or enemy."
};
}
}