EndlessLevels.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace DiamondArrow
  5. {
  6. internal static class EndlessLevels
  7. {
  8. static readonly Vector2Int[] Directions = {
  9. Vector2Int.right, Vector2Int.up, Vector2Int.left, Vector2Int.down
  10. };
  11. public static LevelData Generate(int number, LevelData lastConfigured)
  12. {
  13. var random = new System.Random(unchecked(number * 16777619));
  14. int size = lastConfigured.size;
  15. var arrows = BuildBoard(size, random);
  16. // A second layout helps keep sparse boards near the density of the campaign.
  17. if (arrows.Length < size * size / 4)
  18. {
  19. var alternate = BuildBoard(size, random);
  20. if (alternate.Length > arrows.Length) arrows = alternate;
  21. }
  22. int seconds = Mathf.RoundToInt((float)lastConfigured.seconds * arrows.Length / lastConfigured.arrows.Length);
  23. return new LevelData { number = number, size = size, seconds = Math.Max(30, seconds), arrows = arrows };
  24. }
  25. static ArrowData[] BuildBoard(int size, System.Random random)
  26. {
  27. var occupied = new bool[size, size];
  28. var arrows = new List<ArrowData>();
  29. // Each candidate encodes one head cell and facing. Newly placed arrows can
  30. // only block earlier ones, so reverse placement order always solves the board.
  31. var candidates = new int[size * size * Directions.Length];
  32. for (int i = 0; i < candidates.Length; i++) candidates[i] = i;
  33. for (int count = candidates.Length; count > 0; count--)
  34. {
  35. int pick = random.Next(count);
  36. int candidate = candidates[pick];
  37. candidates[pick] = candidates[count - 1];
  38. int cell = candidate / 4;
  39. var head = new Vector2Int(cell % size, cell / size);
  40. int facing = candidate % 4;
  41. var direction = Directions[facing];
  42. var tail = head - direction;
  43. if (occupied[head.x, head.y] || !Inside(tail, size) || occupied[tail.x, tail.y]) continue;
  44. if (!ClearExit(head, direction, size, occupied)) continue;
  45. var path = new List<Cell> { new Cell(head.x, head.y), new Cell(tail.x, tail.y) };
  46. occupied[head.x, head.y] = occupied[tail.x, tail.y] = true;
  47. int length = random.Next(2, 6);
  48. while (path.Count < length)
  49. {
  50. int start = random.Next(4);
  51. bool extended = false;
  52. for (int d = 0; d < 4; d++)
  53. {
  54. var next = tail + Directions[(start + d) % 4];
  55. if (!Inside(next, size) || occupied[next.x, next.y]) continue;
  56. if ((next.x - head.x) * direction.x + (next.y - head.y) * direction.y > 0) continue;
  57. occupied[next.x, next.y] = true;
  58. path.Add(new Cell(next.x, next.y));
  59. tail = next;
  60. extended = true;
  61. break;
  62. }
  63. if (!extended) break;
  64. }
  65. path.Reverse();
  66. arrows.Add(new ArrowData { cells = path.ToArray() });
  67. }
  68. return arrows.ToArray();
  69. }
  70. static bool Inside(Vector2Int p, int size) => p.x >= 0 && p.x < size && p.y >= 0 && p.y < size;
  71. static bool ClearExit(Vector2Int head, Vector2Int direction, int size, bool[,] occupied)
  72. {
  73. for (var p = head + direction; Inside(p, size); p += direction)
  74. if (occupied[p.x, p.y]) return false;
  75. return true;
  76. }
  77. }
  78. }