chore: initial commit
This commit is contained in:
@@ -0,0 +1,286 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
public class MMGridGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Prepares the grid array for use in the generate methods
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] PrepareGrid(ref int width, ref int height)
|
||||
{
|
||||
int[,] grid = new int[width, height];
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Carves or adds to the grid
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static bool SetGridCoordinate(int[,] grid, int x, int y, int value)
|
||||
{
|
||||
if (
|
||||
(x >= 0)
|
||||
&& (x <= grid.GetUpperBound(0))
|
||||
&& (y >= 0)
|
||||
&& (y <= grid.GetUpperBound(1))
|
||||
)
|
||||
{
|
||||
grid[x, y] = value;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a tilemap's contents into a grid
|
||||
/// </summary>
|
||||
/// <param name="tilemap"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] TilemapToGrid(Tilemap tilemap, int width, int height)
|
||||
{
|
||||
if(tilemap == null)
|
||||
{
|
||||
Debug.LogError("[MMGridGenerator] You're trying to convert a tilemap into a grid but didn't specify what tilemap to convert.");
|
||||
return null;
|
||||
}
|
||||
|
||||
int[,] grid = new int[width, height];
|
||||
Vector3Int currentPosition = Vector3Int.zero;
|
||||
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
currentPosition.x = i;
|
||||
currentPosition.y = j;
|
||||
currentPosition += MMTilemapGridRenderer.ComputeOffset(width-1, height-1);
|
||||
|
||||
grid[i, j] = (tilemap.GetTile(currentPosition) == null) ? 0 : 1;
|
||||
}
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Outputs the contents of a grid
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
public static void DebugGrid(int[,] grid, int width, int height)
|
||||
{
|
||||
string output = "";
|
||||
for (int j = height-1; j >= 0; j--)
|
||||
{
|
||||
output += "line "+j+" [";
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
output += grid[i, j];
|
||||
if (i < width - 1)
|
||||
{
|
||||
output += ", ";
|
||||
}
|
||||
}
|
||||
output += "]\n";
|
||||
}
|
||||
MMDebug.DebugLogInfo(output);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the int value at the specified coordinate on a grid
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <param name="errorValue"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetValueAtGridCoordinate(int[,] grid, int x, int y, int errorValue)
|
||||
{
|
||||
if (
|
||||
(x >= 0)
|
||||
&& (x <= grid.GetUpperBound(0))
|
||||
&& (y >= 0)
|
||||
&& (y <= grid.GetUpperBound(1))
|
||||
)
|
||||
{
|
||||
return grid[x, y];
|
||||
}
|
||||
else
|
||||
{
|
||||
return errorValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inverts the contents of a grid (1 becomes 0, 0 becomes 1)
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] InvertGrid(int[,] grid)
|
||||
{
|
||||
for (int i = 0; i <= grid.GetUpperBound(0); i++)
|
||||
{
|
||||
for (int j = 0; j <= grid.GetUpperBound(1); j++)
|
||||
{
|
||||
grid[i, j] = grid[i, j] == 0 ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Smoothens a grid to get rid of spikes / isolated points
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] SmoothenGrid(int[,] grid)
|
||||
{
|
||||
int width = grid.GetUpperBound(0);
|
||||
int height = grid.GetUpperBound(1);
|
||||
|
||||
for (int i = 0; i <= width; i ++)
|
||||
{
|
||||
for (int j = 0; j <= height; j ++)
|
||||
{
|
||||
int adjacentWallsCount = GetAdjacentWallsCount(grid, i , j);
|
||||
|
||||
if (adjacentWallsCount > 4)
|
||||
{
|
||||
grid[i,j] = 1;
|
||||
}
|
||||
else if (adjacentWallsCount < 4)
|
||||
{
|
||||
grid[i,j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Carves "safe spots" with 0s into the specfied grid
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="layer"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] ApplySafeSpots(int[,] grid, List<MMTilemapGeneratorLayer.MMTilemapGeneratorLayerSafeSpot> safeSpots)
|
||||
{
|
||||
foreach (MMTilemapGeneratorLayer.MMTilemapGeneratorLayerSafeSpot safeSpot in safeSpots)
|
||||
{
|
||||
int minX = Mathf.Min(safeSpot.Start.x, safeSpot.End.x);
|
||||
int maxX = Mathf.Max(safeSpot.Start.x, safeSpot.End.x);
|
||||
int minY = Mathf.Min(safeSpot.Start.y, safeSpot.End.y);
|
||||
int maxY = Mathf.Max(safeSpot.Start.y, safeSpot.End.y);
|
||||
|
||||
for (int i = minX; i < maxX; i++)
|
||||
{
|
||||
for (int j = minY; j < maxY; j++)
|
||||
{
|
||||
SetGridCoordinate(grid, i, j, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds bounds (walls made of 1) to a grid, on the selected sides
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="top"></param>
|
||||
/// <param name="bottom"></param>
|
||||
/// <param name="left"></param>
|
||||
/// <param name="right"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] BindGrid(int[,] grid, bool top, bool bottom, bool left, bool right)
|
||||
{
|
||||
int width = grid.GetUpperBound(0);
|
||||
int height = grid.GetUpperBound(1);
|
||||
|
||||
if (top)
|
||||
{
|
||||
for (int i = 0; i <= width; i++)
|
||||
{
|
||||
|
||||
grid[i, height] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (bottom)
|
||||
{
|
||||
for (int i = 0; i <= width; i++)
|
||||
{
|
||||
grid[i, 0] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (left)
|
||||
{
|
||||
for (int j = 0; j <= height; j++)
|
||||
{
|
||||
grid[0, j] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (right)
|
||||
{
|
||||
for (int j = 0; j <= height; j++)
|
||||
{
|
||||
grid[width, j] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the amount of adjacent walls for a specific coordinate
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetAdjacentWallsCount(int[,] grid, int x, int y)
|
||||
{
|
||||
int width = grid.GetUpperBound(0);
|
||||
int height = grid.GetUpperBound(1);
|
||||
int wallCount = 0;
|
||||
for (int i = x - 1; i <= x + 1; i ++)
|
||||
{
|
||||
for (int j = y - 1; j <= y + 1; j ++)
|
||||
{
|
||||
if ((i >= 0) && (i <= width) && (j >= 0) && (j <= height))
|
||||
{
|
||||
if ((i != x) || (j != y))
|
||||
{
|
||||
wallCount += grid[i,j];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
wallCount ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return wallCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a5db094ce3821d44a8a41a4cb7d45cc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a grid of the specified size, either entirely full or empty
|
||||
/// </summary>
|
||||
public class MMGridGeneratorFull : MMGridGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a grid of the specified size, either entirely full or empty
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="full"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] Generate(int width, int height, bool full)
|
||||
{
|
||||
int[,] grid = PrepareGrid(ref width, ref height);
|
||||
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
SetGridCoordinate(grid, i, j, full ? 1 : 0);
|
||||
}
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fa40f7f3424f6fb41b91e1888ad1d960
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,170 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a grid with a path in the specified direction
|
||||
/// </summary>
|
||||
public class MMGridGeneratorPath : MMGridGenerator
|
||||
{
|
||||
public enum Directions { TopToBottom, BottomToTop, LeftToRight, RightToLeft }
|
||||
|
||||
/// <summary>
|
||||
/// Generates a grid with a path in the specified direction
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="seed"></param>
|
||||
/// <param name="direction"></param>
|
||||
/// <param name="startPosition"></param>
|
||||
/// <param name="pathMinWidth"></param>
|
||||
/// <param name="pathMaxWidth"></param>
|
||||
/// <param name="directionChangeDistance"></param>
|
||||
/// <param name="widthChangePercentage"></param>
|
||||
/// <param name="directionChangePercentage"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] Generate(int width, int height, int seed, Directions direction, Vector2Int startPosition, int pathMinWidth, int pathMaxWidth, int directionChangeDistance, int widthChangePercentage, int directionChangePercentage)
|
||||
{
|
||||
int[,] grid = PrepareGrid(ref width, ref height);
|
||||
grid = MMGridGeneratorFull.Generate(width, height, true);
|
||||
System.Random random = new System.Random(seed);
|
||||
Random.InitState(seed);
|
||||
|
||||
int pathWidth = 1;
|
||||
int initialX = startPosition.x;
|
||||
int initialY = startPosition.y;
|
||||
|
||||
SetGridCoordinate(grid, initialX, initialY, 0);
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
case Directions.TopToBottom:
|
||||
int x1 = initialX;
|
||||
for (int i = -pathWidth; i <= pathWidth; i++)
|
||||
{
|
||||
SetGridCoordinate(grid, x1 + i, initialY, 0);
|
||||
}
|
||||
for (int y = initialY; y > 0; y--)
|
||||
{
|
||||
pathWidth = ComputeWidth(random, widthChangePercentage, pathMinWidth, pathMaxWidth, pathWidth);
|
||||
x1 = DetermineNextStep(random, x1, directionChangeDistance, directionChangePercentage, pathMaxWidth, width);
|
||||
for (int i = -pathWidth; i <= pathWidth; i++)
|
||||
{
|
||||
SetGridCoordinate(grid, x1 + i, y, 0);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Directions.BottomToTop:
|
||||
int x2 = initialX;
|
||||
for (int i = -pathWidth; i <= pathWidth; i++)
|
||||
{
|
||||
SetGridCoordinate(grid, x2 + i, initialY, 0);
|
||||
}
|
||||
for (int y = initialY; y < height; y++)
|
||||
{
|
||||
pathWidth = ComputeWidth(random, widthChangePercentage, pathMinWidth, pathMaxWidth, pathWidth);
|
||||
x2 = DetermineNextStep(random, x2, directionChangeDistance, directionChangePercentage, pathMaxWidth, width);
|
||||
for (int i = -pathWidth; i <= pathWidth; i++)
|
||||
{
|
||||
SetGridCoordinate(grid, x2 + i, y, 0);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Directions.LeftToRight:
|
||||
int y1 = initialY;
|
||||
for (int i = -pathWidth; i <= pathWidth; i++)
|
||||
{
|
||||
SetGridCoordinate(grid, initialX, y1 + i, 0);
|
||||
}
|
||||
for (int x = initialX; x < width; x++)
|
||||
{
|
||||
pathWidth = ComputeWidth(random, widthChangePercentage, pathMinWidth, pathMaxWidth, pathWidth);
|
||||
y1 = DetermineNextStep(random, y1, directionChangeDistance, directionChangePercentage, pathMaxWidth, width);
|
||||
for (int i = -pathWidth; i <= pathWidth; i++)
|
||||
{
|
||||
SetGridCoordinate(grid, x, y1 + i, 0);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Directions.RightToLeft:
|
||||
int y2 = initialY;
|
||||
for (int i = -pathWidth; i <= pathWidth; i++)
|
||||
{
|
||||
SetGridCoordinate(grid, initialX, y2 + i, 0);
|
||||
}
|
||||
for (int x = initialX; x > 0; x--)
|
||||
{
|
||||
pathWidth = ComputeWidth(random, widthChangePercentage, pathMinWidth, pathMaxWidth, pathWidth);
|
||||
y2 = DetermineNextStep(random, y2, directionChangeDistance, directionChangePercentage, pathMaxWidth, width);
|
||||
for (int i = -pathWidth; i <= pathWidth; i++)
|
||||
{
|
||||
SetGridCoordinate(grid, x, y2 + i, 0);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines the new width of the path
|
||||
/// </summary>
|
||||
/// <param name="random"></param>
|
||||
/// <param name="widthChangePercentage"></param>
|
||||
/// <param name="pathMinWidth"></param>
|
||||
/// <param name="pathMaxWidth"></param>
|
||||
/// <param name="pathWidth"></param>
|
||||
/// <returns></returns>
|
||||
private static int ComputeWidth(System.Random random, int widthChangePercentage, int pathMinWidth, int pathMaxWidth, int pathWidth)
|
||||
{
|
||||
if (random.Next(0, 100) > widthChangePercentage)
|
||||
{
|
||||
int widthChange = Random.Range(-pathMaxWidth, pathMaxWidth);
|
||||
pathWidth += widthChange;
|
||||
if (pathWidth < pathMinWidth)
|
||||
{
|
||||
pathWidth = pathMinWidth;
|
||||
}
|
||||
if (pathWidth > pathMaxWidth)
|
||||
{
|
||||
pathWidth = pathMaxWidth;
|
||||
}
|
||||
}
|
||||
|
||||
return pathWidth;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines in what direction to move the path
|
||||
/// </summary>
|
||||
/// <param name="random"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="directionChangeDistance"></param>
|
||||
/// <param name="directionChangePercentage"></param>
|
||||
/// <param name="pathMaxWidth"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <returns></returns>
|
||||
private static int DetermineNextStep(System.Random random, int x, int directionChangeDistance, int directionChangePercentage, int pathMaxWidth, int width)
|
||||
{
|
||||
if (random.Next(0, 100) > directionChangePercentage)
|
||||
{
|
||||
int xChange = Random.Range(-directionChangeDistance, directionChangeDistance);
|
||||
x += xChange;
|
||||
if (x < pathMaxWidth)
|
||||
{
|
||||
x = pathMaxWidth;
|
||||
}
|
||||
if (x > (width - pathMaxWidth))
|
||||
{
|
||||
x = width - pathMaxWidth;
|
||||
}
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6040f34cccf59644a9602da5bc052d06
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Random = System.Random;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a grid of the specified size based on a seeded perlin noise, the smaller the seed, the blockier the grid
|
||||
/// </summary>
|
||||
public class MMGridGeneratorPerlinNoise : MMGridGenerator
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Generates a grid of the specified size based on a seeded perlin noise, the smaller the seed, the blockier the grid
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="seed"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] Generate(int width, int height, float seed)
|
||||
{
|
||||
int[,] grid = PrepareGrid(ref width, ref height);
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
int value = Mathf.RoundToInt(Mathf.PerlinNoise(i * seed, j * seed));
|
||||
SetGridCoordinate(grid, i, j, value);
|
||||
}
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a6c4a44291630848b8d616911bd8c47
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Random = System.Random;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a grid with a ground floor
|
||||
/// </summary>
|
||||
public class MMGridGeneratorPerlinNoiseGround : MMGridGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a grid with a ground floor
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="seed"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] Generate(int width, int height, float seed)
|
||||
{
|
||||
int[,] grid = PrepareGrid(ref width, ref height);
|
||||
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
int groundHeight = Mathf.FloorToInt((Mathf.PerlinNoise(i, seed) - 0.5f) * height) + (height/2);
|
||||
for (int j = groundHeight; j >= 0; j--)
|
||||
{
|
||||
SetGridCoordinate(grid, i, j, 1);
|
||||
}
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4829c639bd6729945b0664d15e2f740e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MoreMountains.Tools;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a simple grid filled with random points
|
||||
/// </summary>
|
||||
public class MMGridGeneratorRandom : MMGridGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a simple grid filled with random points
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="seed"></param>
|
||||
/// <param name="fillPercentage"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] Generate(int width, int height, int seed, int fillPercentage)
|
||||
{
|
||||
int[,] grid = PrepareGrid(ref width, ref height);
|
||||
|
||||
grid = MMGridGeneratorFull.Generate(width, height, true);
|
||||
System.Random random = new System.Random(seed);
|
||||
|
||||
for (int i = 0; i <= width; i ++)
|
||||
{
|
||||
for (int j = 0; j <= height; j ++)
|
||||
{
|
||||
int value = (random.Next(0,100) < fillPercentage)? 1: 0;
|
||||
SetGridCoordinate(grid, i, j, value);
|
||||
}
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c05de6f8b07240b4e87c6219630c251d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,95 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a grid with a path carved by a drunkard walk algorithm
|
||||
/// See http://pcg.wikidot.com/pcg-algorithm:drunkard-walk
|
||||
/// </summary>
|
||||
public class MMGridGeneratorRandomWalk : MMGridGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a grid with a path carved by a drunkard walk algorithm
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="seed"></param>
|
||||
/// <param name="fillPercentage"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] Generate(int width, int height, int seed, int fillPercentage, Vector2Int startingPoint, int maxIterations)
|
||||
{
|
||||
int[,] grid = PrepareGrid(ref width, ref height);
|
||||
grid = MMGridGeneratorFull.Generate(width, height, true);
|
||||
System.Random random = new System.Random(seed);
|
||||
|
||||
int requiredFillQuantity = ((width * height) * fillPercentage) / 100;
|
||||
int fillCounter = 0;
|
||||
|
||||
int currentX = startingPoint.x;
|
||||
int currentY = startingPoint.y;
|
||||
grid[currentX, currentY] = 0;
|
||||
fillCounter++;
|
||||
int iterationsCounter = 0;
|
||||
|
||||
while ((fillCounter < requiredFillQuantity) && (iterationsCounter < maxIterations))
|
||||
{
|
||||
int direction = random.Next(4);
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
case 0:
|
||||
if ((currentY + 1) < height)
|
||||
{
|
||||
currentY++;
|
||||
grid = Carve(grid, currentX, currentY, ref fillCounter);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if ((currentY - 1) > 1)
|
||||
{
|
||||
currentY--;
|
||||
grid = Carve(grid, currentX, currentY, ref fillCounter);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if ((currentX - 1) > 1)
|
||||
{
|
||||
currentX--;
|
||||
grid = Carve(grid, currentX, currentY, ref fillCounter);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if ((currentX + 1) < width)
|
||||
{
|
||||
currentX++;
|
||||
grid = Carve(grid, currentX, currentY, ref fillCounter);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
iterationsCounter++;
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <param name="fillCounter"></param>
|
||||
/// <returns></returns>
|
||||
private static int[,] Carve(int[,] grid, int x, int y, ref int fillCounter)
|
||||
{
|
||||
if (grid[x, y] == 1)
|
||||
{
|
||||
grid[x, y] = 0;
|
||||
fillCounter++;
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bcddb04b7bb50904182da35f2b38777c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,106 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a grid with a path carved by a drunkard walk algorithm that will avoid another grid's walls
|
||||
/// </summary>
|
||||
public class MMGridGeneratorRandomWalkAvoider : MMGridGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a grid with a path carved by a drunkard walk algorithm that will avoid another grid's walls
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="seed"></param>
|
||||
/// <param name="fillPercentage"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] Generate(int width, int height, int seed, int fillPercentage, Vector2Int startingPoint, int[,] obstacles, int obstacleDistance, int maxIterations)
|
||||
{
|
||||
int[,] grid = PrepareGrid(ref width, ref height);
|
||||
grid = MMGridGeneratorFull.Generate(width, height, true);
|
||||
System.Random random = new System.Random(seed);
|
||||
|
||||
int requiredFillQuantity = ((width * height) * fillPercentage) / 100;
|
||||
int fillCounter = 0;
|
||||
|
||||
int currentX = startingPoint.x;
|
||||
int currentY = startingPoint.y;
|
||||
grid[currentX, currentY] = 0;
|
||||
fillCounter++;
|
||||
|
||||
int iterationsCount = 0;
|
||||
|
||||
while ((fillCounter < requiredFillQuantity) && (iterationsCount < maxIterations))
|
||||
{
|
||||
int direction = random.Next(4);
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
case 0: // up
|
||||
if ( ((currentY + 1) <= height) && !ObstacleAt(obstacles, currentX, currentY + obstacleDistance) )
|
||||
{
|
||||
currentY++;
|
||||
grid = Carve(grid, currentX, currentY, ref fillCounter);
|
||||
}
|
||||
break;
|
||||
case 1: // down
|
||||
if ( ((currentY - 1) > 1) && !ObstacleAt(obstacles, currentX, currentY - obstacleDistance) )
|
||||
{
|
||||
currentY--;
|
||||
grid = Carve(grid, currentX, currentY, ref fillCounter);
|
||||
}
|
||||
break;
|
||||
case 2: // left
|
||||
if (((currentX - 1) > 1) && !ObstacleAt(obstacles, currentX - obstacleDistance, currentY ) )
|
||||
{
|
||||
currentX--;
|
||||
grid = Carve(grid, currentX, currentY, ref fillCounter);
|
||||
}
|
||||
break;
|
||||
case 3: // right
|
||||
if (((currentX + 1) <= width) && !ObstacleAt(obstacles, currentX + obstacleDistance, currentY) )
|
||||
{
|
||||
currentX++;
|
||||
grid = Carve(grid, currentX, currentY, ref fillCounter);
|
||||
}
|
||||
break;
|
||||
}
|
||||
iterationsCount++;
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if an obstacle is found at the specified coordinates, false otherwise
|
||||
/// </summary>
|
||||
/// <param name="obstacles"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <returns></returns>
|
||||
private static bool ObstacleAt(int[,] obstacles, int x, int y)
|
||||
{
|
||||
return (MMGridGenerator.GetValueAtGridCoordinate(obstacles, x, y, 1) == 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <param name="fillCounter"></param>
|
||||
/// <returns></returns>
|
||||
private static int[,] Carve(int[,] grid, int x, int y, ref int fillCounter)
|
||||
{
|
||||
if (GetValueAtGridCoordinate(grid, x, y, 0) == 1)
|
||||
{
|
||||
SetGridCoordinate(grid, x, y, 0);
|
||||
fillCounter++;
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3c349afb15421f141ae3ba659175c815
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,70 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Uses random walk to generate a ground with controlled elevation
|
||||
/// </summary>
|
||||
public class MMGridGeneratorRandomWalkGround : MMGridGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Uses random walk to generate a ground with controlled elevation
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="seed"></param>
|
||||
/// <param name="minHeightDifference"></param>
|
||||
/// <param name="maxHeightDifference"></param>
|
||||
/// <param name="minFlatDistance"></param>
|
||||
/// <param name="maxFlatDistance"></param>
|
||||
/// <returns></returns>
|
||||
public static int[,] Generate(int width, int height, int seed, int minHeightDifference, int maxHeightDifference, int minFlatDistance, int maxFlatDistance, int maxHeight)
|
||||
{
|
||||
System.Random random = new System.Random(seed.GetHashCode());
|
||||
Random.InitState(seed);
|
||||
|
||||
int[,] grid = PrepareGrid(ref width, ref height);
|
||||
|
||||
int groundHeight = Random.Range(0, maxHeight);
|
||||
int previousGroundHeight = groundHeight;
|
||||
int currentFlatDistance = -1;
|
||||
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
groundHeight = previousGroundHeight;
|
||||
int newElevation = Random.Range(minHeightDifference, maxHeightDifference);
|
||||
int flatDistance = Random.Range(minFlatDistance, maxFlatDistance);
|
||||
|
||||
if (currentFlatDistance >= flatDistance - 1)
|
||||
{
|
||||
if (random.Next(2) > 0)
|
||||
{
|
||||
groundHeight -= newElevation;
|
||||
}
|
||||
else if (previousGroundHeight + newElevation < height)
|
||||
{
|
||||
groundHeight += newElevation;
|
||||
}
|
||||
|
||||
groundHeight = Mathf.Clamp(groundHeight, 1, maxHeight);
|
||||
currentFlatDistance = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentFlatDistance++;
|
||||
}
|
||||
|
||||
for (int j = groundHeight; j >= 0; j--)
|
||||
{
|
||||
grid[i, j] = 1;
|
||||
}
|
||||
|
||||
previousGroundHeight = groundHeight;
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 21e69a8c38f55e84ea754b184697f036
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user