Add experimental RA procedural map generator

Add an experimental procedural map generator for the Red Alert mod,
along with supporting code that may assist in the development of map
generators for other mods.

Map generation may be accessed as a tool in the Map Editor. This change
does not presently introduce direct lobby options for generated maps.

Features:

- Terrain with land, water, beaches, cliffs, roads, debris, and trees.
- Placement of mpspawns, neutral buildings, and resources.
- Rotational and mirror symmetry options.
- Various configurable parameters with presets.
- Deterministic with configurable seed.
- Performant.
This commit is contained in:
Ashley Newson
2025-01-07 22:48:08 +00:00
committed by Gustas
parent 0536c58b78
commit 417f787294
41 changed files with 11795 additions and 7 deletions

View File

@@ -10,6 +10,8 @@
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA.Support
{
@@ -32,7 +34,10 @@ namespace OpenRA.Support
mt[i] = 1812433253u * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i;
}
public int Next()
/// <summary>
/// Produces an unsigned integer between -0x80000000 and 0x7fffffff inclusive.
/// </summary>
public uint NextUint()
{
if (index == 0) Generate();
@@ -45,6 +50,24 @@ namespace OpenRA.Support
index = (index + 1) % 624;
TotalCount++;
Last = (int)(y % int.MaxValue);
return y;
}
/// <summary>
/// Produces an unsigned integer between -0x80000000 and 0x7fffffff inclusive.
/// </summary>
public ulong NextUlong()
{
return (ulong)NextUint() << 32 | NextUint();
}
/// <summary>
/// Produces signed integers between -0x7fffffff and 0x7fffffff inclusive.
/// 0 is twice as likely as any other number.
/// </summary>
public int Next()
{
NextUint();
return Last;
}
@@ -65,11 +88,73 @@ namespace OpenRA.Support
return Next(0, high);
}
/// <summary>
/// Produces random 32-bit floats between 0 inclusive and 1 inclusive.
/// Note that whilst floats are 32-bit (23-bit mantissa), the entropy is not. Lower numbers preserve more entropy.
/// </summary>
public float NextFloat()
{
return Math.Abs(Next() / (float)0x7fffffff);
}
/// <summary>
/// Produces uniformally distributed random floats between 0 inclusive and 1 exclusive.
/// Note that whilst floats are 32-bit (23-bit mantissa), each output contains exactly 23 bits of entropy.
/// </summary>
public float NextFloatExclusive()
{
return (NextUint() & 0x7fffff) / (float)0x800000;
}
/// <summary>
/// Produces uniformally distributed random doubles between 0 inclusive and 1 exclusive.
/// Note that whilst doubles are 64-bit (52-bit mantissa), each output contains exactly 52 bits of entropy.
/// </summary>
public double NextDoubleExclusive()
{
return (NextUlong() & 0xfffffffffffffL) / (double)0x10000000000000L;
}
/// <summary>
/// Pick a random an index from a list of weights.
/// </summary>
public int PickWeighted(IReadOnlyList<float> weights)
{
var total = weights.Sum();
var spin = NextFloatExclusive() * total;
int i;
float acc = 0;
for (i = 0; i < weights.Count; i++)
{
acc += weights[i];
if (spin < acc)
return i;
}
// This might be possible due to floating point precision loss
// (in rare cases). Or we might have been given rubbish
// weights. Return anything > 0.
for (i = 0; i < weights.Count; i++)
if (weights[i] > 0)
return i;
// All <= 0!
return Next(0, weights.Count);
}
/// <summary>
/// Shuffle a portion of a list list in place. Has minor biases.
/// </summary>
public void ShuffleInPlace<T>(IList<T> list, int start, int len)
{
for (var i = len; i > 1; i--)
{
var swap = Next(i);
(list[start + i - 1], list[start + swap]) =
(list[start + swap], list[start + i - 1]);
}
}
void Generate()
{
unchecked