Board.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace TowerDevil
  5. {
  6. public sealed class BoardFrame
  7. {
  8. public readonly Piece[,] Cells;
  9. public readonly List<Vector2Int> Flash;
  10. public readonly Dictionary<Vector2Int, Vector2Int> MergeTargets;
  11. public readonly string Cue;
  12. public BoardFrame(Piece[,] cells, string cue, List<Vector2Int> flash = null,
  13. Dictionary<Vector2Int, Vector2Int> targets = null)
  14. {
  15. Cells = (Piece[,])cells.Clone();
  16. Cue = cue;
  17. Flash = flash;
  18. MergeTargets = targets;
  19. }
  20. }
  21. public sealed partial class Run
  22. {
  23. public const int Size = 6;
  24. public readonly Piece[,] Board = new Piece[Size, Size];
  25. public readonly Profile Profile;
  26. readonly System.Random random;
  27. public Phase Phase { get; private set; }
  28. public int Day, Moves, Plates, Score, Kills, Forged, Bosses, Unbanked, Earned, EarnedShards;
  29. public bool UsedCoinRevive, UsedAdRevive, UsedTowerAd, UsedRepairAd;
  30. int bankedScore, bankedKills, bankedForged, lastPaidDay, shardHalves, coinTenths;
  31. bool deathBonusPaid;
  32. public int MaxPlates => 5 + (Profile.gateLevel - 1) * 2;
  33. public int Available => Profile.coins + Unbanked;
  34. public bool CanArrange => Phase == Phase.Plan || Phase == Phase.Rest;
  35. public event Action<BoardFrame> BoardChanged;
  36. public Run(Profile profile, int seed)
  37. {
  38. Profile = profile;
  39. random = new System.Random(seed);
  40. Plates = MaxPlates;
  41. for (int r = 0; r < Size; r++)
  42. for (int c = 0; c < Size; c++)
  43. {
  44. Piece p;
  45. do
  46. {
  47. p = Material();
  48. } while ((c >= 2 && Same(Board[r, c - 1], p) && Same(Board[r, c - 2], p)) ||
  49. (r >= 2 && Same(Board[r - 1, c], p) && Same(Board[r - 2, c], p)));
  50. Board[r, c] = p;
  51. }
  52. for (int i = 0; i < Profile.workshop[1]; i++)
  53. PlaceTower(2, 5);
  54. StartNight(1, false);
  55. }
  56. #if UNITY_EDITOR
  57. public void SetReviewBoard()
  58. {
  59. int[,] layout = { { 0, 1, 2, 6, 3, 1 }, { 4, 2, 1, 0, 2, 4 }, { 3, 0, 4, 3, 7, 0 },
  60. { 1, 5, 2, 4, 1, 3 }, { 4, 3, 9, 0, 2, 1 }, { 0, 2, 1, 8, 3, 4 } };
  61. StartNight(4, false);
  62. Plates = 4;
  63. Moves = 5;
  64. for (int r = 0; r < Size; r++)
  65. for (int c = 0; c < Size; c++)
  66. Board[r, c] = new Piece((Kind)layout[r, c]);
  67. }
  68. #endif
  69. Piece Material()
  70. {
  71. int n = random.Next(95);
  72. return new Piece(n < 24 ? Kind.Stone
  73. : n < 48 ? Kind.Timber
  74. : n < 67 ? Kind.Crystal
  75. : n < 86 ? Kind.Ice
  76. : Kind.Gold);
  77. }
  78. static bool Same(Piece a, Piece b) => a != null && b != null && a.Matchable && a.Kind == b.Kind
  79. && a.Tier == b.Tier;
  80. public static bool Inside(int r, int c) => r >= 0 && r < Size && c >= 0 && c < Size;
  81. void Frame(string cue,
  82. List<Vector2Int> cells = null) => BoardChanged?.Invoke(new BoardFrame(Board, cue, cells));
  83. public bool Swap(int row, int col, int toRow, int toCol)
  84. {
  85. if (!CanArrange || Moves == 0 || !Inside(row, col) || !Inside(toRow, toCol) ||
  86. Math.Abs(row - toRow) + Math.Abs(col - toCol) != 1 || Board[row, col] == null ||
  87. Board[toRow, toCol] == null)
  88. return false;
  89. (Board[row, col], Board[toRow, toCol]) = (Board[toRow, toCol], Board[row, col]);
  90. if (Phase != Phase.Rest)
  91. Moves--;
  92. Score += 2;
  93. Frame("swap");
  94. Resolve(new Vector2Int(toCol, toRow));
  95. return true;
  96. }
  97. List<List<Vector2Int>> Lines()
  98. {
  99. var lines = new List<List<Vector2Int>>();
  100. for (int axis = 0; axis < 2; axis++)
  101. for (int i = 0; i < Size; i++)
  102. {
  103. for (int start = 0; start < Size;)
  104. {
  105. int end = start + 1;
  106. Piece first = axis == 0 ? Board[i, start] : Board[start, i];
  107. while (end < Size && Same(first, axis == 0 ? Board[i, end] : Board[end, i]))
  108. end++;
  109. if (end - start >= 3)
  110. {
  111. var cells = new List<Vector2Int>();
  112. for (int j = start; j < end; j++)
  113. cells.Add(axis == 0 ? new Vector2Int(j, i) : new Vector2Int(i, j));
  114. lines.Add(cells);
  115. }
  116. start = end;
  117. }
  118. }
  119. return lines;
  120. }
  121. void Resolve(Vector2Int? origin)
  122. {
  123. for (int pass = 0; pass < 16; pass++)
  124. {
  125. var lines = Lines();
  126. if (lines.Count == 0)
  127. break;
  128. var clear = new HashSet<Vector2Int>();
  129. var products = new Dictionary<Vector2Int, Piece>();
  130. var targets = BoardChanged != null ? new Dictionary<Vector2Int, Vector2Int>() : null;
  131. foreach (var line in lines)
  132. {
  133. var target = origin ?? line[line.Count / 2];
  134. Vector2Int anchor = line[0];
  135. int best = int.MaxValue;
  136. foreach (var cell in line)
  137. {
  138. clear.Add(cell);
  139. int dist = Math.Abs(cell.x - target.x) + Math.Abs(cell.y - target.y);
  140. if (!products.ContainsKey(cell) && dist < best)
  141. {
  142. best = dist;
  143. anchor = cell;
  144. }
  145. }
  146. Piece source = Board[anchor.y, anchor.x], product;
  147. if (source.Kind == Kind.Gold)
  148. product = new Piece(Kind.Chest);
  149. else if (source.Kind == Kind.Chest || source.Tower)
  150. product = new Piece(source.Kind, source.Tier + 1);
  151. else
  152. product = new Piece(Kind.Cannon + (int)source.Kind);
  153. products[anchor] = product;
  154. if (targets != null)
  155. foreach (var cell in line)
  156. if (cell == anchor || !targets.ContainsKey(cell))
  157. targets[cell] = anchor;
  158. int bonus = line.Count - 3;
  159. if (Phase != Phase.Rest)
  160. Moves += bonus;
  161. Score += 40 + (product.Tower ? 30 : 60 + 25 * bonus);
  162. if (product.Tower)
  163. Forged++;
  164. }
  165. BoardChanged?.Invoke(new BoardFrame(Board, "merge", new List<Vector2Int>(clear), targets));
  166. foreach (var cell in clear)
  167. Board[cell.y, cell.x] = null;
  168. foreach (var p in products)
  169. Board[p.Key.y, p.Key.x] = p.Value;
  170. Frame("forge");
  171. Gravity(Phase != Phase.Rest);
  172. Frame("fall");
  173. origin = null;
  174. }
  175. }
  176. void Gravity(bool refill)
  177. {
  178. for (int c = 0; c < Size; c++)
  179. {
  180. int write = Size - 1;
  181. for (int r = Size - 1; r >= 0; r--)
  182. if (Board[r, c] != null)
  183. Board[write--, c] = Board[r, c];
  184. while (write >= 0)
  185. Board[write--, c] = refill ? Material() : null;
  186. }
  187. }
  188. public bool OpenChest(int r, int c)
  189. {
  190. if (!CanArrange || !Inside(r, c) || Board[r, c]?.Kind != Kind.Chest)
  191. return false;
  192. if (Phase != Phase.Rest)
  193. Moves += Rules.ChestMoves[Board[r, c].Tier - 1];
  194. Board[r, c] = null;
  195. Score += 25;
  196. Gravity(Phase != Phase.Rest);
  197. Frame("chest");
  198. Resolve(null);
  199. return true;
  200. }
  201. public bool Demolish(int r, int c)
  202. {
  203. if (!CanArrange || !Inside(r, c) || Board[r, c]?.Tower != true)
  204. return false;
  205. Board[r, c] = null;
  206. Credit(250);
  207. Gravity(Phase != Phase.Rest);
  208. Frame("fall");
  209. return true;
  210. }
  211. bool PlaceTower(int firstRow, int endRow)
  212. {
  213. var slots = new List<Vector2Int>();
  214. for (int r = firstRow; r < endRow; r++)
  215. for (int c = 0; c < Size; c++)
  216. if (Board[r, c] != null && !Board[r, c].Tower && Board[r, c].Kind != Kind.Chest)
  217. slots.Add(new Vector2Int(c, r));
  218. if (slots.Count == 0)
  219. return false;
  220. var s = slots[random.Next(slots.Count)];
  221. Board[s.y, s.x] = new Piece(Kind.Cannon + random.Next(4));
  222. return true;
  223. }
  224. public bool AdTower()
  225. {
  226. if (!CanArrange || UsedTowerAd || !PlaceTower(1, Size))
  227. return false;
  228. UsedTowerAd = true;
  229. Frame("forge");
  230. return true;
  231. }
  232. public bool BuyChest(int tier)
  233. {
  234. if (!CanArrange || tier < 1 || tier > 4 || !Spend(Rules.ChestPrice[tier - 1], Rules.ChestNames[tier - 1]))
  235. return false;
  236. Profile.stock[tier - 1]++;
  237. DeliverStock();
  238. Profile.Save();
  239. Frame("chest");
  240. return true;
  241. }
  242. void DeliverStock()
  243. {
  244. var slots = new List<int>();
  245. for (int c = 0; c < Size; c++)
  246. if (Board[0, c] != null && !Board[0, c].Tower && Board[0, c].Kind != Kind.Chest)
  247. slots.Add(c);
  248. for (int tier = 1; tier <= 4; tier++)
  249. while (Profile.stock[tier - 1] > 0 && slots.Count > 0)
  250. {
  251. int i = random.Next(slots.Count);
  252. Board[0, slots[i]] = new Piece(Kind.Chest, tier);
  253. slots.RemoveAt(i);
  254. Profile.stock[tier - 1]--;
  255. }
  256. }
  257. bool Spend(int price, string reason)
  258. {
  259. if (Available < price)
  260. return false;
  261. int local = Math.Min(Unbanked, price);
  262. Unbanked -= local;
  263. Profile.coins -= price - local;
  264. Profile.Record(-price, reason);
  265. return true;
  266. }
  267. void Credit(int tenths)
  268. {
  269. coinTenths += tenths;
  270. int whole = coinTenths / 10;
  271. Unbanked += whole;
  272. Earned += whole;
  273. coinTenths %= 10;
  274. }
  275. public bool Repair(bool ad)
  276. {
  277. if (Phase != Phase.Rest || Plates >= MaxPlates || (ad ? UsedRepairAd : !Spend(200, "Mend seal")))
  278. return false;
  279. if (ad)
  280. UsedRepairAd = true;
  281. Plates = Math.Min(MaxPlates, Plates + 2);
  282. Profile.Save();
  283. return true;
  284. }
  285. public bool UpgradeGate()
  286. {
  287. if (Phase != Phase.Rest || Profile.gateLevel >= 8 ||
  288. !Spend(500 + 250 * (Profile.gateLevel - 1), "Seal upgrade"))
  289. return false;
  290. Profile.gateLevel++;
  291. Plates += 2;
  292. Profile.Save();
  293. return true;
  294. }
  295. void Bank()
  296. {
  297. Profile.bestNight = Math.Max(Profile.bestNight, Day);
  298. Profile.coins += Unbanked;
  299. if (Unbanked > 0)
  300. Profile.Record(Unbanked, "Night " + Day + " earnings");
  301. Unbanked = 0;
  302. Profile.score += Score - bankedScore;
  303. bankedScore = Score;
  304. Profile.kills += Kills - bankedKills;
  305. bankedKills = Kills;
  306. Profile.forged += Forged - bankedForged;
  307. bankedForged = Forged;
  308. Profile.Save();
  309. }
  310. public void Quit()
  311. {
  312. if (Phase == Phase.Closed)
  313. return;
  314. Phase = Phase.Closed;
  315. Bank();
  316. }
  317. void Defeat()
  318. {
  319. Phase = Phase.Over;
  320. if (!deathBonusPaid)
  321. {
  322. Profile.shards += 2;
  323. EarnedShards += 2;
  324. deathBonusPaid = true;
  325. }
  326. Bank();
  327. }
  328. public bool Revive(bool ad)
  329. {
  330. if (Phase != Phase.Over || (ad ? UsedAdRevive : UsedCoinRevive || !Spend(300, "Revive")))
  331. return false;
  332. if (ad)
  333. UsedAdRevive = true;
  334. else
  335. UsedCoinRevive = true;
  336. Plates = MaxPlates;
  337. StartNight(Day, false);
  338. return true;
  339. }
  340. }
  341. }