| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace TowerDevil
- {
- public sealed class BoardFrame
- {
- public readonly Piece[,] Cells;
- public readonly List<Vector2Int> Flash;
- public readonly Dictionary<Vector2Int, Vector2Int> MergeTargets;
- public readonly string Cue;
- public BoardFrame(Piece[,] cells, string cue, List<Vector2Int> flash = null,
- Dictionary<Vector2Int, Vector2Int> targets = null)
- {
- Cells = (Piece[,])cells.Clone();
- Cue = cue;
- Flash = flash;
- MergeTargets = targets;
- }
- }
- public sealed partial class Run
- {
- public const int Size = 6;
- public readonly Piece[,] Board = new Piece[Size, Size];
- public readonly Profile Profile;
- readonly System.Random random;
- public Phase Phase { get; private set; }
- public int Day, Moves, Plates, Score, Kills, Forged, Bosses, Unbanked, Earned, EarnedShards;
- public bool UsedCoinRevive, UsedAdRevive, UsedTowerAd, UsedRepairAd;
- int bankedScore, bankedKills, bankedForged, lastPaidDay, shardHalves, coinTenths;
- bool deathBonusPaid;
- public int MaxPlates => 5 + (Profile.gateLevel - 1) * 2;
- public int Available => Profile.coins + Unbanked;
- public bool CanArrange => Phase == Phase.Plan || Phase == Phase.Rest;
- public event Action<BoardFrame> BoardChanged;
- public Run(Profile profile, int seed)
- {
- Profile = profile;
- random = new System.Random(seed);
- Plates = MaxPlates;
- for (int r = 0; r < Size; r++)
- for (int c = 0; c < Size; c++)
- {
- Piece p;
- do
- {
- p = Material();
- } while ((c >= 2 && Same(Board[r, c - 1], p) && Same(Board[r, c - 2], p)) ||
- (r >= 2 && Same(Board[r - 1, c], p) && Same(Board[r - 2, c], p)));
- Board[r, c] = p;
- }
- for (int i = 0; i < Profile.workshop[1]; i++)
- PlaceTower(2, 5);
- StartNight(1, false);
- }
- #if UNITY_EDITOR
- public void SetReviewBoard()
- {
- int[,] layout = { { 0, 1, 2, 6, 3, 1 }, { 4, 2, 1, 0, 2, 4 }, { 3, 0, 4, 3, 7, 0 },
- { 1, 5, 2, 4, 1, 3 }, { 4, 3, 9, 0, 2, 1 }, { 0, 2, 1, 8, 3, 4 } };
- StartNight(4, false);
- Plates = 4;
- Moves = 5;
- for (int r = 0; r < Size; r++)
- for (int c = 0; c < Size; c++)
- Board[r, c] = new Piece((Kind)layout[r, c]);
- }
- #endif
- Piece Material()
- {
- int n = random.Next(95);
- return new Piece(n < 24 ? Kind.Stone
- : n < 48 ? Kind.Timber
- : n < 67 ? Kind.Crystal
- : n < 86 ? Kind.Ice
- : Kind.Gold);
- }
- static bool Same(Piece a, Piece b) => a != null && b != null && a.Matchable && a.Kind == b.Kind
- && a.Tier == b.Tier;
- public static bool Inside(int r, int c) => r >= 0 && r < Size && c >= 0 && c < Size;
- void Frame(string cue,
- List<Vector2Int> cells = null) => BoardChanged?.Invoke(new BoardFrame(Board, cue, cells));
- public bool Swap(int row, int col, int toRow, int toCol)
- {
- if (!CanArrange || Moves == 0 || !Inside(row, col) || !Inside(toRow, toCol) ||
- Math.Abs(row - toRow) + Math.Abs(col - toCol) != 1 || Board[row, col] == null ||
- Board[toRow, toCol] == null)
- return false;
- (Board[row, col], Board[toRow, toCol]) = (Board[toRow, toCol], Board[row, col]);
- if (Phase != Phase.Rest)
- Moves--;
- Score += 2;
- Frame("swap");
- Resolve(new Vector2Int(toCol, toRow));
- return true;
- }
- List<List<Vector2Int>> Lines()
- {
- var lines = new List<List<Vector2Int>>();
- for (int axis = 0; axis < 2; axis++)
- for (int i = 0; i < Size; i++)
- {
- for (int start = 0; start < Size;)
- {
- int end = start + 1;
- Piece first = axis == 0 ? Board[i, start] : Board[start, i];
- while (end < Size && Same(first, axis == 0 ? Board[i, end] : Board[end, i]))
- end++;
- if (end - start >= 3)
- {
- var cells = new List<Vector2Int>();
- for (int j = start; j < end; j++)
- cells.Add(axis == 0 ? new Vector2Int(j, i) : new Vector2Int(i, j));
- lines.Add(cells);
- }
- start = end;
- }
- }
- return lines;
- }
- void Resolve(Vector2Int? origin)
- {
- for (int pass = 0; pass < 16; pass++)
- {
- var lines = Lines();
- if (lines.Count == 0)
- break;
- var clear = new HashSet<Vector2Int>();
- var products = new Dictionary<Vector2Int, Piece>();
- var targets = BoardChanged != null ? new Dictionary<Vector2Int, Vector2Int>() : null;
- foreach (var line in lines)
- {
- var target = origin ?? line[line.Count / 2];
- Vector2Int anchor = line[0];
- int best = int.MaxValue;
- foreach (var cell in line)
- {
- clear.Add(cell);
- int dist = Math.Abs(cell.x - target.x) + Math.Abs(cell.y - target.y);
- if (!products.ContainsKey(cell) && dist < best)
- {
- best = dist;
- anchor = cell;
- }
- }
- Piece source = Board[anchor.y, anchor.x], product;
- if (source.Kind == Kind.Gold)
- product = new Piece(Kind.Chest);
- else if (source.Kind == Kind.Chest || source.Tower)
- product = new Piece(source.Kind, source.Tier + 1);
- else
- product = new Piece(Kind.Cannon + (int)source.Kind);
- products[anchor] = product;
- if (targets != null)
- foreach (var cell in line)
- if (cell == anchor || !targets.ContainsKey(cell))
- targets[cell] = anchor;
- int bonus = line.Count - 3;
- if (Phase != Phase.Rest)
- Moves += bonus;
- Score += 40 + (product.Tower ? 30 : 60 + 25 * bonus);
- if (product.Tower)
- Forged++;
- }
- BoardChanged?.Invoke(new BoardFrame(Board, "merge", new List<Vector2Int>(clear), targets));
- foreach (var cell in clear)
- Board[cell.y, cell.x] = null;
- foreach (var p in products)
- Board[p.Key.y, p.Key.x] = p.Value;
- Frame("forge");
- Gravity(Phase != Phase.Rest);
- Frame("fall");
- origin = null;
- }
- }
- void Gravity(bool refill)
- {
- for (int c = 0; c < Size; c++)
- {
- int write = Size - 1;
- for (int r = Size - 1; r >= 0; r--)
- if (Board[r, c] != null)
- Board[write--, c] = Board[r, c];
- while (write >= 0)
- Board[write--, c] = refill ? Material() : null;
- }
- }
- public bool OpenChest(int r, int c)
- {
- if (!CanArrange || !Inside(r, c) || Board[r, c]?.Kind != Kind.Chest)
- return false;
- if (Phase != Phase.Rest)
- Moves += Rules.ChestMoves[Board[r, c].Tier - 1];
- Board[r, c] = null;
- Score += 25;
- Gravity(Phase != Phase.Rest);
- Frame("chest");
- Resolve(null);
- return true;
- }
- public bool Demolish(int r, int c)
- {
- if (!CanArrange || !Inside(r, c) || Board[r, c]?.Tower != true)
- return false;
- Board[r, c] = null;
- Credit(250);
- Gravity(Phase != Phase.Rest);
- Frame("fall");
- return true;
- }
- bool PlaceTower(int firstRow, int endRow)
- {
- var slots = new List<Vector2Int>();
- for (int r = firstRow; r < endRow; r++)
- for (int c = 0; c < Size; c++)
- if (Board[r, c] != null && !Board[r, c].Tower && Board[r, c].Kind != Kind.Chest)
- slots.Add(new Vector2Int(c, r));
- if (slots.Count == 0)
- return false;
- var s = slots[random.Next(slots.Count)];
- Board[s.y, s.x] = new Piece(Kind.Cannon + random.Next(4));
- return true;
- }
- public bool AdTower()
- {
- if (!CanArrange || UsedTowerAd || !PlaceTower(1, Size))
- return false;
- UsedTowerAd = true;
- Frame("forge");
- return true;
- }
- public bool BuyChest(int tier)
- {
- if (!CanArrange || tier < 1 || tier > 4 || !Spend(Rules.ChestPrice[tier - 1], Rules.ChestNames[tier - 1]))
- return false;
- Profile.stock[tier - 1]++;
- DeliverStock();
- Profile.Save();
- Frame("chest");
- return true;
- }
- void DeliverStock()
- {
- var slots = new List<int>();
- for (int c = 0; c < Size; c++)
- if (Board[0, c] != null && !Board[0, c].Tower && Board[0, c].Kind != Kind.Chest)
- slots.Add(c);
- for (int tier = 1; tier <= 4; tier++)
- while (Profile.stock[tier - 1] > 0 && slots.Count > 0)
- {
- int i = random.Next(slots.Count);
- Board[0, slots[i]] = new Piece(Kind.Chest, tier);
- slots.RemoveAt(i);
- Profile.stock[tier - 1]--;
- }
- }
- bool Spend(int price, string reason)
- {
- if (Available < price)
- return false;
- int local = Math.Min(Unbanked, price);
- Unbanked -= local;
- Profile.coins -= price - local;
- Profile.Record(-price, reason);
- return true;
- }
- void Credit(int tenths)
- {
- coinTenths += tenths;
- int whole = coinTenths / 10;
- Unbanked += whole;
- Earned += whole;
- coinTenths %= 10;
- }
- public bool Repair(bool ad)
- {
- if (Phase != Phase.Rest || Plates >= MaxPlates || (ad ? UsedRepairAd : !Spend(200, "Mend seal")))
- return false;
- if (ad)
- UsedRepairAd = true;
- Plates = Math.Min(MaxPlates, Plates + 2);
- Profile.Save();
- return true;
- }
- public bool UpgradeGate()
- {
- if (Phase != Phase.Rest || Profile.gateLevel >= 8 ||
- !Spend(500 + 250 * (Profile.gateLevel - 1), "Seal upgrade"))
- return false;
- Profile.gateLevel++;
- Plates += 2;
- Profile.Save();
- return true;
- }
- void Bank()
- {
- Profile.bestNight = Math.Max(Profile.bestNight, Day);
- Profile.coins += Unbanked;
- if (Unbanked > 0)
- Profile.Record(Unbanked, "Night " + Day + " earnings");
- Unbanked = 0;
- Profile.score += Score - bankedScore;
- bankedScore = Score;
- Profile.kills += Kills - bankedKills;
- bankedKills = Kills;
- Profile.forged += Forged - bankedForged;
- bankedForged = Forged;
- Profile.Save();
- }
- public void Quit()
- {
- if (Phase == Phase.Closed)
- return;
- Phase = Phase.Closed;
- Bank();
- }
- void Defeat()
- {
- Phase = Phase.Over;
- if (!deathBonusPaid)
- {
- Profile.shards += 2;
- EarnedShards += 2;
- deathBonusPaid = true;
- }
- Bank();
- }
- public bool Revive(bool ad)
- {
- if (Phase != Phase.Over || (ad ? UsedAdRevive : UsedCoinRevive || !Spend(300, "Revive")))
- return false;
- if (ad)
- UsedAdRevive = true;
- else
- UsedCoinRevive = true;
- Plates = MaxPlates;
- StartNight(Day, false);
- return true;
- }
- }
- }
|