GameLevelState.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ShelfState
  5. {
  6. public readonly List<List<int>> Layers = new List<List<int>>();
  7. public int SealChains;
  8. public List<int> Front => Layers[0];
  9. public bool IsSealed => SealChains > 0;
  10. public int LayerCount => Layers.Count;
  11. }
  12. public class GameMoveOutcome
  13. {
  14. public bool Success;
  15. public string Message = "";
  16. public int ClearedSets;
  17. public int RevealedLayers;
  18. public int UnlockedShelves;
  19. public int RemovedFruit;
  20. }
  21. public class GameLevelState
  22. {
  23. public static readonly int[] FruitOrder = { 1, 2, 4, 6, 11, 5, 8, 10 };
  24. public LevelDefinition Definition { get; private set; }
  25. public List<ShelfState> Shelves { get; private set; }
  26. public int MovesLeft { get; private set; }
  27. public float TimeLeft { get; private set; }
  28. public int Score { get; private set; }
  29. public int Served { get; private set; }
  30. public GameMoveOutcome LastOutcome { get; private set; } = new GameMoveOutcome();
  31. public bool Timed => Definition != null && Definition.Timed;
  32. public bool Chained => Definition != null && Definition.Chained;
  33. public int TotalFruits => Definition != null ? Definition.TotalFruits : 0;
  34. public bool IsCleared => RemainingFruits <= 0;
  35. public bool IsOutOfMoves => MovesLeft <= 0 && !IsCleared;
  36. public bool IsOutOfTime => Timed && TimeLeft <= 0f && !IsCleared;
  37. public float TimeLimit => Definition != null ? Definition.TimeLimit : 0f;
  38. public int RemainingFruits
  39. {
  40. get
  41. {
  42. int total = 0;
  43. for (int i = 0; i < Shelves.Count; i++)
  44. {
  45. for (int layer = 0; layer < Shelves[i].Layers.Count; layer++)
  46. total += Shelves[i].Layers[layer].Count;
  47. }
  48. return total;
  49. }
  50. }
  51. public int SealedShelves
  52. {
  53. get
  54. {
  55. int total = 0;
  56. for (int i = 0; i < Shelves.Count; i++)
  57. {
  58. if (Shelves[i].IsSealed)
  59. total++;
  60. }
  61. return total;
  62. }
  63. }
  64. public int TotalChains
  65. {
  66. get
  67. {
  68. int total = 0;
  69. for (int i = 0; i < Shelves.Count; i++)
  70. total += Mathf.Max(0, Shelves[i].SealChains);
  71. return total;
  72. }
  73. }
  74. public static GameLevelState Create(LevelDefinition definition, int seed = 0)
  75. {
  76. var state = new GameLevelState();
  77. state.Definition = definition;
  78. var random = seed == 0 ? new System.Random(Guid.NewGuid().GetHashCode()) : new System.Random(seed);
  79. state.Build(random);
  80. return state;
  81. }
  82. void Build(System.Random random)
  83. {
  84. int count = Definition.totalShelves;
  85. Shelves = new List<ShelfState>(count);
  86. for (int i = 0; i < count; i++)
  87. Shelves.Add(new ShelfState());
  88. int[] extraLayers = new int[count];
  89. var shelfOrder = new List<int>();
  90. for (int i = 0; i < count; i++)
  91. shelfOrder.Add(i);
  92. Shuffle(shelfOrder, random);
  93. int cursor = 0;
  94. for (int i = 0; i < Definition.tripleShelves && cursor < count; i++, cursor++)
  95. extraLayers[shelfOrder[cursor]] = 2;
  96. for (int i = 0; i < Definition.doubleShelves && cursor < count; i++, cursor++)
  97. extraLayers[shelfOrder[cursor]] = 1;
  98. int[] frontCounts = new int[count];
  99. for (int i = 0; i < count; i++)
  100. frontCounts[i] = 3;
  101. var emptyOrder = new List<int>();
  102. for (int i = 0; i < count; i++)
  103. emptyOrder.Add(i);
  104. Shuffle(emptyOrder, random);
  105. if (Definition.emptySlots <= 3)
  106. {
  107. frontCounts[emptyOrder[0]] = 2;
  108. frontCounts[emptyOrder[1]] = 1;
  109. }
  110. else
  111. {
  112. frontCounts[emptyOrder[0]] = 2;
  113. frontCounts[emptyOrder[1]] = 2;
  114. frontCounts[emptyOrder[2]] = 1;
  115. frontCounts[emptyOrder[3]] = 1;
  116. }
  117. for (int i = 0; i < count; i++)
  118. {
  119. var shelf = Shelves[i];
  120. shelf.Layers.Add(new List<int>(frontCounts[i]));
  121. for (int layer = 0; layer < extraLayers[i]; layer++)
  122. shelf.Layers.Add(new List<int>(3));
  123. }
  124. var frontSlots = new List<SlotRef>();
  125. var allSlots = new List<SlotRef>();
  126. for (int i = 0; i < count; i++)
  127. {
  128. var shelf = Shelves[i];
  129. for (int layer = 0; layer < shelf.Layers.Count; layer++)
  130. {
  131. int slots = layer == 0 ? frontCounts[i] : 3;
  132. for (int slot = 0; slot < slots; slot++)
  133. {
  134. var reference = new SlotRef(i, layer, slot);
  135. allSlots.Add(reference);
  136. if (layer == 0)
  137. frontSlots.Add(reference);
  138. }
  139. }
  140. }
  141. var pool = new List<int>(Definition.TotalFruits);
  142. int[] playable = new int[Definition.fruitTypes];
  143. for (int i = 0; i < playable.Length; i++)
  144. playable[i] = FruitOrder[i];
  145. for (int set = 0; set < Definition.sets; set++)
  146. {
  147. int fruit = set < playable.Length
  148. ? playable[set]
  149. : playable[random.Next(playable.Length)];
  150. pool.Add(fruit);
  151. pool.Add(fruit);
  152. pool.Add(fruit);
  153. }
  154. Shuffle(pool, random);
  155. if (pool.Count != allSlots.Count)
  156. {
  157. Debug.LogWarning("[GameLevel] Fruit pool and slot count differ: " + pool.Count + " vs " + allSlots.Count);
  158. }
  159. int limit = Mathf.Min(pool.Count, allSlots.Count);
  160. for (int i = 0; i < limit; i++)
  161. {
  162. var reference = allSlots[i];
  163. var targetLayer = Shelves[reference.Shelf].Layers[reference.Layer];
  164. int candidate = pool[i];
  165. if (targetLayer.Count >= 2 &&
  166. targetLayer[targetLayer.Count - 1] == candidate &&
  167. targetLayer[targetLayer.Count - 2] == candidate)
  168. {
  169. for (int j = i + 1; j < limit; j++)
  170. {
  171. if (pool[j] == candidate)
  172. continue;
  173. int swap = pool[j];
  174. pool[j] = candidate;
  175. pool[i] = swap;
  176. candidate = swap;
  177. break;
  178. }
  179. }
  180. targetLayer.Add(candidate);
  181. }
  182. FixInitialMatches(random);
  183. AssignSeals(random, frontCounts);
  184. MovesLeft = Definition.moves;
  185. TimeLeft = Definition.TimeLimit;
  186. Score = 0;
  187. Served = 0;
  188. LastOutcome = new GameMoveOutcome();
  189. }
  190. void AssignSeals(System.Random random, int[] frontCounts)
  191. {
  192. if (!Definition.Chained || Definition.seals <= 0)
  193. return;
  194. var candidates = new List<int>();
  195. for (int i = 0; i < Shelves.Count; i++)
  196. {
  197. if (frontCounts[i] == 3 && Shelves[i].Front.Count == 3)
  198. candidates.Add(i);
  199. }
  200. Shuffle(candidates, random);
  201. int seals = Mathf.Min(Definition.seals, candidates.Count);
  202. for (int i = 0; i < seals; i++)
  203. Shelves[candidates[i]].SealChains = Definition.sealChains;
  204. }
  205. void FixInitialMatches(System.Random random)
  206. {
  207. for (int pass = 0; pass < 240; pass++)
  208. {
  209. bool changed = false;
  210. for (int shelfIndex = 0; shelfIndex < Shelves.Count; shelfIndex++)
  211. {
  212. var shelf = Shelves[shelfIndex];
  213. for (int layer = 0; layer < shelf.Layers.Count; layer++)
  214. {
  215. var fruits = shelf.Layers[layer];
  216. if (fruits.Count != 3 || fruits[0] != fruits[1] || fruits[1] != fruits[2])
  217. continue;
  218. if (TryBreakTriple(shelfIndex, layer, random))
  219. changed = true;
  220. }
  221. }
  222. if (!changed)
  223. break;
  224. }
  225. }
  226. bool TryBreakTriple(int shelfIndex, int layer, System.Random random)
  227. {
  228. var source = Shelves[shelfIndex].Layers[layer];
  229. int fruit = source[0];
  230. for (int pass = 0; pass < 2; pass++)
  231. {
  232. for (int targetShelf = 0; targetShelf < Shelves.Count; targetShelf++)
  233. {
  234. var shelf = Shelves[targetShelf];
  235. for (int targetLayer = 0; targetLayer < shelf.Layers.Count; targetLayer++)
  236. {
  237. if (targetShelf == shelfIndex && targetLayer == layer)
  238. continue;
  239. var donor = shelf.Layers[targetLayer];
  240. for (int slot = 0; slot < donor.Count; slot++)
  241. {
  242. int swap = donor[slot];
  243. if (swap == fruit)
  244. continue;
  245. if (pass == 0)
  246. {
  247. int same = 0;
  248. for (int i = 0; i < donor.Count; i++)
  249. {
  250. if (i != slot && donor[i] == fruit)
  251. same++;
  252. }
  253. if (same >= 2)
  254. continue;
  255. }
  256. donor[slot] = fruit;
  257. source[random.Next(3)] = swap;
  258. return true;
  259. }
  260. }
  261. }
  262. }
  263. return false;
  264. }
  265. public GameMoveOutcome TryMove(int fromShelf, int fromSlot, int toShelf, int toSlot = -1)
  266. {
  267. var outcome = new GameMoveOutcome();
  268. if (fromShelf < 0 || fromShelf >= Shelves.Count || toShelf < 0 || toShelf >= Shelves.Count)
  269. return outcome;
  270. if (fromShelf == toShelf)
  271. return outcome;
  272. var source = Shelves[fromShelf];
  273. var target = Shelves[toShelf];
  274. if (source.IsSealed || target.IsSealed)
  275. {
  276. outcome.Message = "game.banner.shelf_chained";
  277. return outcome;
  278. }
  279. if (fromSlot < 0 || fromSlot >= source.Front.Count)
  280. return outcome;
  281. if (target.Front.Count >= 3 && toSlot >= target.Front.Count)
  282. toSlot = 0;
  283. int fruit = source.Front[fromSlot];
  284. if (toSlot >= 0 && toSlot < target.Front.Count)
  285. {
  286. int swap = target.Front[toSlot];
  287. target.Front[toSlot] = fruit;
  288. source.Front[fromSlot] = swap;
  289. }
  290. else if (target.Front.Count < 3)
  291. {
  292. source.Front.RemoveAt(fromSlot);
  293. target.Front.Add(fruit);
  294. }
  295. else
  296. {
  297. int targetIndex = toSlot < 0 ? 0 : toSlot;
  298. int swap = target.Front[targetIndex];
  299. target.Front[targetIndex] = fruit;
  300. source.Front[fromSlot] = swap;
  301. }
  302. MovesLeft = Mathf.Max(0, MovesLeft - 1);
  303. Score += 2;
  304. outcome.Success = true;
  305. Resolve(outcome);
  306. LastOutcome = outcome;
  307. return outcome;
  308. }
  309. public GameMoveOutcome UseHammer(int shelfIndex)
  310. {
  311. var outcome = new GameMoveOutcome();
  312. if (shelfIndex < 0 || shelfIndex >= Shelves.Count)
  313. return outcome;
  314. var shelf = Shelves[shelfIndex];
  315. if (shelf.IsSealed)
  316. {
  317. outcome.Message = "game.banner.shelf_chained";
  318. return outcome;
  319. }
  320. if (shelf.Front.Count <= 0)
  321. {
  322. outcome.Message = "game.banner.shelf_empty";
  323. return outcome;
  324. }
  325. outcome.RemovedFruit = shelf.Front[0];
  326. shelf.Front.RemoveAt(0);
  327. Score += 5;
  328. outcome.Success = true;
  329. Resolve(outcome);
  330. LastOutcome = outcome;
  331. return outcome;
  332. }
  333. public GameMoveOutcome ShuffleVisible(System.Random random)
  334. {
  335. var outcome = new GameMoveOutcome();
  336. var visible = new List<int>();
  337. for (int i = 0; i < Shelves.Count; i++)
  338. {
  339. if (Shelves[i].IsSealed)
  340. continue;
  341. visible.AddRange(Shelves[i].Front);
  342. }
  343. Shuffle(visible, random);
  344. int cursor = 0;
  345. for (int i = 0; i < Shelves.Count; i++)
  346. {
  347. if (Shelves[i].IsSealed)
  348. continue;
  349. var front = Shelves[i].Front;
  350. int count = front.Count;
  351. front.Clear();
  352. for (int slot = 0; slot < count; slot++)
  353. front.Add(visible[cursor++]);
  354. }
  355. FixVisibleMatches(random);
  356. outcome.Success = true;
  357. Resolve(outcome);
  358. LastOutcome = outcome;
  359. return outcome;
  360. }
  361. void FixVisibleMatches(System.Random random)
  362. {
  363. for (int pass = 0; pass < 60; pass++)
  364. {
  365. bool changed = false;
  366. for (int i = 0; i < Shelves.Count; i++)
  367. {
  368. if (Shelves[i].IsSealed)
  369. continue;
  370. var front = Shelves[i].Front;
  371. if (front.Count != 3 || front[0] != front[1] || front[1] != front[2])
  372. continue;
  373. for (int j = 0; j < Shelves.Count && !changed; j++)
  374. {
  375. if (i == j || Shelves[j].IsSealed)
  376. continue;
  377. var other = Shelves[j].Front;
  378. for (int slot = 0; slot < other.Count; slot++)
  379. {
  380. if (other[slot] == front[0])
  381. continue;
  382. int swap = other[slot];
  383. other[slot] = front[0];
  384. front[random.Next(3)] = swap;
  385. changed = true;
  386. break;
  387. }
  388. }
  389. if (changed)
  390. break;
  391. }
  392. if (!changed)
  393. break;
  394. }
  395. }
  396. public bool UseMasterKey(out string message)
  397. {
  398. for (int i = 0; i < Shelves.Count; i++)
  399. {
  400. if (!Shelves[i].IsSealed)
  401. continue;
  402. Shelves[i].SealChains = 0;
  403. var outcome = new GameMoveOutcome();
  404. Resolve(outcome);
  405. LastOutcome = outcome;
  406. message = "game.banner.chain_broken";
  407. return true;
  408. }
  409. for (int i = 0; i < Shelves.Count; i++)
  410. {
  411. if (Shelves[i].Front.Count <= 0)
  412. continue;
  413. Shelves[i].Front.Clear();
  414. var outcome = new GameMoveOutcome();
  415. Resolve(outcome);
  416. LastOutcome = outcome;
  417. message = "game.banner.shelf_cleared";
  418. return true;
  419. }
  420. message = "game.banner.nothing_to_unlock";
  421. return false;
  422. }
  423. public void AddMoves(int amount)
  424. {
  425. MovesLeft = Mathf.Max(0, MovesLeft + amount);
  426. }
  427. public bool Tick(float deltaTime)
  428. {
  429. if (!Timed || IsCleared)
  430. return false;
  431. TimeLeft = Mathf.Max(0f, TimeLeft - Mathf.Max(0f, deltaTime));
  432. return TimeLeft <= 0f;
  433. }
  434. public Dictionary<int, int> CountVisibleTypes(bool allLayers = true)
  435. {
  436. var counts = new Dictionary<int, int>();
  437. for (int i = 0; i < Shelves.Count; i++)
  438. {
  439. var shelf = Shelves[i];
  440. int layerLimit = allLayers ? shelf.Layers.Count : 1;
  441. for (int layer = 0; layer < layerLimit; layer++)
  442. {
  443. var fruits = shelf.Layers[layer];
  444. for (int slot = 0; slot < fruits.Count; slot++)
  445. {
  446. int fruit = fruits[slot];
  447. if (!counts.ContainsKey(fruit))
  448. counts[fruit] = 0;
  449. counts[fruit]++;
  450. }
  451. }
  452. }
  453. return counts;
  454. }
  455. void Resolve(GameMoveOutcome outcome)
  456. {
  457. int guard = 0;
  458. bool changed = true;
  459. while (changed && guard++ < 40)
  460. {
  461. changed = false;
  462. for (int i = 0; i < Shelves.Count; i++)
  463. {
  464. var shelf = Shelves[i];
  465. var front = shelf.Front;
  466. if (front.Count == 3 && front[0] == front[1] && front[1] == front[2])
  467. {
  468. front.Clear();
  469. Served++;
  470. Score += 100;
  471. outcome.ClearedSets++;
  472. if (Timed)
  473. TimeLeft = Mathf.Min(TimeLimit, TimeLeft + 2f);
  474. if (Chained)
  475. {
  476. for (int s = 0; s < Shelves.Count; s++)
  477. {
  478. if (Shelves[s].SealChains <= 0)
  479. continue;
  480. Shelves[s].SealChains--;
  481. if (Shelves[s].SealChains == 0)
  482. outcome.UnlockedShelves++;
  483. }
  484. }
  485. changed = true;
  486. }
  487. }
  488. for (int i = 0; i < Shelves.Count; i++)
  489. {
  490. var shelf = Shelves[i];
  491. if (shelf.Front.Count == 0 && shelf.Layers.Count > 1)
  492. {
  493. shelf.Layers.RemoveAt(0);
  494. outcome.RevealedLayers++;
  495. changed = true;
  496. }
  497. }
  498. }
  499. }
  500. static void Shuffle<T>(IList<T> list, System.Random random)
  501. {
  502. for (int i = list.Count - 1; i > 0; i--)
  503. {
  504. int j = random.Next(i + 1);
  505. T temp = list[i];
  506. list[i] = list[j];
  507. list[j] = temp;
  508. }
  509. }
  510. struct SlotRef
  511. {
  512. public readonly int Shelf;
  513. public readonly int Layer;
  514. public readonly int Slot;
  515. public SlotRef(int shelf, int layer, int slot)
  516. {
  517. Shelf = shelf;
  518. Layer = layer;
  519. Slot = slot;
  520. }
  521. }
  522. }