private bool Import(TR2CombinedLevel level, EnemyTransportCollection enemies) { try { // The importer will handle any duplication between the entities to import and // remove so just pass the unfiltered lists to it. TRModelImporter importer = new TRModelImporter { ClearUnusedSprites = true, EntitiesToImport = enemies.EntitiesToImport, EntitiesToRemove = enemies.EntitiesToRemove, Level = level.Data, LevelName = level.Name, TextureRemapPath = @"Resources\Textures\Deduplication\" + level.JsonID + "-TextureRemap.json", TexturePositionMonitor = _outer.TextureMonitor.CreateMonitor(level.Name, enemies.EntitiesToImport) }; // Try to import the selected models into the level. importer.Import(); return(true); } catch (PackingException /* e*/) { //System.Diagnostics.Debug.WriteLine(level.Name + ": " + e.Message); // We need to reload the level to undo anything that may have changed. _outer.ReloadLevelData(level); // Tell the monitor to no longer track what we tried to import _outer.TextureMonitor.ClearMonitor(level.Name, enemies.EntitiesToImport); return(false); } }
// Executed in parallel, so just store the import result to process later synchronously. protected override void ProcessImpl() { foreach (TR2CombinedLevel level in _enemyMapping.Keys) { if (!level.IsAssault) { int count = _enemyMapping[level].Capacity; for (int i = 0; i < count; i++) { //if (i > 0) //{ // _outer.SetMessage(string.Format("Randomizing enemies [{0} - attempt {1} / {2}]", level.Name, i + 1, _outer.MaxPackingAttempts)); //} EnemyTransportCollection enemies = _enemyMapping[level][i]; if (Import(level, enemies)) { enemies.ImportResult = true; break; } } } if (!_outer.TriggerProgress()) { break; } } }
// This is triggered synchronously after the import work to ensure the RNG remains consistent internal void ApplyRandomization() { foreach (TR2CombinedLevel level in _enemyMapping.Keys) { if (!level.IsAssault) { EnemyTransportCollection importedCollection = null; foreach (EnemyTransportCollection enemies in _enemyMapping[level]) { if (enemies.ImportResult) { importedCollection = enemies; break; } } if (importedCollection == null) { // Cross-level was not possible with the enemy combinations. This could be due to either // a lack of space for texture packing, or the max ObjectTexture count (2048) was reached. _outer.TextureMonitor.RemoveMonitor(level.Name); // And just randomize normally // TODO: maybe trigger a warning to display at the end of randomizing to say that cross- // level was not possible? _outer.RandomizeEnemiesNatively(level); //System.Diagnostics.Debug.WriteLine(level.Name + ": Native enemies"); } else { // The import worked, so randomize the entities based on what we now have in place. //System.Diagnostics.Debug.WriteLine(level.Name + ": " + string.Join(", ", importedCollection.EntitiesToImport)); EnemyRandomizationCollection enemies = new EnemyRandomizationCollection { Available = importedCollection.EntitiesToImport, Droppable = TR2EntityUtilities.FilterDroppableEnemies(importedCollection.EntitiesToImport, !_outer.ProtectMonks), Water = TR2EntityUtilities.FilterWaterEnemies(importedCollection.EntitiesToImport) }; if (_outer.DocileBirdMonsters && importedCollection.BirdMonsterGuiser != TR2Entities.BirdMonster) { _outer.DisguiseEntity(level, importedCollection.BirdMonsterGuiser, TR2Entities.BirdMonster); enemies.BirdMonsterGuiser = importedCollection.BirdMonsterGuiser; } _outer.RandomizeEnemies(level, enemies); } _outer.SaveLevel(level); } if (!_outer.TriggerProgress()) { break; } } }