public void GenerateSelection() { // Destroy the previous selection for (int i = transform.childCount - 1; i >= 0; i--) { Destroy(transform.GetChild(i).gameObject); } // Tracks what the game has given the player, so the game don't give duplicates var selectedTypes = new Type[3]; var selectedNames = new string[3]; // So the game doesn't keep retying to select a non-duplicate option forever var lagCounter = 0; const int lagCap = 5000; // TODO - Perhaps modify amount of choices for (var i = 0; i < 3; i++) { // If it's the first time opening the shop this level, the game should display a different selection if (shop.HasPlayerMadePurchase()) { // Add a new turret to the selection WeightedList <TurretBlueprint> turrets = _levelData.initialTurretSelection; TurretBlueprint selected = turrets.GetRandomItem(); // Gets a new turret if there is a duplicate (depending on settings) switch (_levelData.initialDuplicateCheck) { case DuplicateTypes.None: break; case DuplicateTypes.ByName: while (selectedNames.Contains(selected.displayName)) { selected = turrets.GetRandomItem(); lagCounter++; if (lagCounter > lagCap) { throw new OverflowException("Too many attempts to pick new turret"); } } break; case DuplicateTypes.ByType: while (selectedTypes.Contains(selected.GetType())) { selected = turrets.GetRandomItem(); lagCounter++; if (lagCounter > lagCap) { throw new OverflowException("Too many attempts to pick new turret"); } } break; default: throw new ArgumentOutOfRangeException(); } // Adds our selection to the UI GenerateTurretUI(selected); // Add the selection to our selected arrays to avoid duplicates selectedTypes[i] = selected.GetType(); selectedNames[i] = selected.displayName; continue; } // Select if the game should get an Module or a turret float choice = Random.Range(0f, _levelData.turretOptionWeight.Evaluate(GameStats.Rounds) + _levelData.moduleOptionWeight.Evaluate(GameStats.Rounds)); if (choice > _levelData.turretOptionWeight.Evaluate(GameStats.Rounds)) { // Grants an Module option WeightedList <Module> modules = _levelData.modules.ToWeightedList(GameStats.Rounds); Module selected = modules.GetRandomItem(); // Check the player actually has a turret of the modules type // But only if they have actually bought some turrets if (_turretTypes.Any()) { while (!(selected.GetValidTypes() == null || _turretTypes.Any(x => selected.GetValidTypes().Contains(x)))) { selected = modules.GetRandomItem(); lagCounter++; if (lagCounter > lagCap) { throw new OverflowException("Too many attempts to pick new Module"); } } } // Gets a new Module if the random has picked a duplicate (depending on settings) switch (_levelData.moduleDuplicateCheck) { case DuplicateTypes.None: break; case DuplicateTypes.ByName: while (selectedNames.Contains(selected.displayName)) { selected = modules.GetRandomItem(); lagCounter++; if (lagCounter > lagCap) { throw new OverflowException("Too many attempts to pick new Module"); } } break; case DuplicateTypes.ByType: while (selectedTypes.Contains(selected.GetType())) { selected = modules.GetRandomItem(); lagCounter++; if (lagCounter > lagCap) { throw new OverflowException("Too many attempts to pick new Module"); } } break; default: throw new ArgumentOutOfRangeException(); } // Adds the Module as an option to the player GenerateModuleUI(selected); // Add it to our "history" to avoid duplicates on our next selection selectedTypes[i] = selected.GetType(); selectedNames[i] = selected.displayName; } else { // Grants a turret option WeightedList <TurretBlueprint> turrets = _levelData.turrets.ToWeightedList(GameStats.Rounds); TurretBlueprint selected = turrets.GetRandomItem(); // Check the game didn't pick something it's already picked (depending on duplicate checking type) switch (_levelData.turretDuplicateCheck) { case DuplicateTypes.None: break; case DuplicateTypes.ByName: while (selectedNames.Contains(selected.displayName)) { selected = turrets.GetRandomItem(); lagCounter++; if (lagCounter > lagCap) { throw new OverflowException("Too many attempts to pick new turret"); } } break; case DuplicateTypes.ByType: while (selectedTypes.Contains(selected.GetType())) { selected = turrets.GetRandomItem(); lagCounter++; if (lagCounter > lagCap) { throw new OverflowException("Too many attempts to pick new turret"); } } break; default: throw new ArgumentOutOfRangeException(); } // Add the turret to the ui for the player to pick GenerateTurretUI(selected); // Add the turret to our history so the game don't pick it again selectedTypes[i] = selected.GetType(); selectedNames[i] = selected.displayName; } } }