Board.cs 14 KB

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