internal ResearchTrigger(Ob_Trigger trigger) { FlattenResearch(trigger, _branches, new ResearchBranch()); var removed = true; var toRemove = new HashSet <ResearchBranch>(); while (removed) { foreach (var a in _branches) { if (!toRemove.Contains(a)) { foreach (var b in _branches) { if (a != b && (a.ResearchStates.Equals(b.ResearchStates) && a.Triggers.Keys.Equals(b.Triggers.Keys) && a.Unlocked.Equals(b.Unlocked))) { toRemove.Add(b); } } } } removed = toRemove.Count > 0; foreach (var k in toRemove) { _branches.Remove(k); } } }
public Ob_ResearchDefinition GetObjectBuilder() { Ob_Trigger trigger = Trigger.GetObjectBuilder(); if (!(trigger is Ob_Trigger_All)) { trigger = new Ob_Trigger_All() { Elements = new List <Ob_Trigger> { trigger } } } ; return(new Ob_ResearchDefinition() { Id = Id, Unlocks = UnlocksOriginal.Select(x => new NullableDefinitionId() { Id = x }).ToList(), AutoStart = AutoStart, Description = Description, DisplayName = DisplayName, CompletionMessage = CompletionMessage, UpdatesAsNotifications = UpdatesAsNotifications, ShowCompletionWindow = ShowCompletionWindow, Trigger = (Ob_Trigger_All)trigger }); } }
private void FlattenResearch(Ob_Trigger trigger, ISet <ResearchBranch> branches, ResearchBranch currentBranch) { if (trigger is Ob_Trigger_Composite) { trigger = ((Ob_Trigger_Composite)trigger).Simplify(); } if (trigger == null) { return; } trigger.UpdateKey(); var all = trigger as Ob_Trigger_All; var any = trigger as Ob_Trigger_Any; var research = trigger as Ob_Trigger_ResearchState; var unlock = trigger as Ob_Trigger_Unlocked; if (all != null) { foreach (var e in all.Elements.Distinct()) { FlattenResearch(e, branches, currentBranch); } return; } if (any != null) { foreach (var e in any.Elements.Distinct()) { FlattenResearch(e, branches, currentBranch.Fork()); } return; } branches.Add(currentBranch); if (research != null) { ResearchState currentVal = currentBranch.ResearchStates.GetValueOrDefault(research.Id, research.RequiredState); Utilities.Assert(currentVal == research.RequiredState, $"Research state mismatch: {currentVal} and {research.RequiredState}"); currentBranch.ResearchStates[research.Id] = research.RequiredState; return; } if (unlock != null) { currentBranch.Unlocked.Add(unlock.DefinitionId); return; } var hasItem = trigger as Ob_Trigger_HasItem; if (hasItem != null && !Utilities.ValidateDefinition <MyPhysicalItemDefinition>(hasItem.DefinitionId)) { return; } var interact = trigger as Ob_Trigger_Interact; if (interact != null) { var valid = true; foreach (var k in interact.HandItem) { valid &= Utilities.ValidateDefinition <MyPhysicalItemDefinition>(k); } foreach (var k in interact.BlockInteractTarget) { valid &= Utilities.ValidateDefinition <MyCubeBlockDefinition>(k); } if (!valid) { return; } } var key = trigger.StateStorageKey; Utilities.Assert(key != null, $"Trigger type {trigger.GetType().FullName} must have a state storage key"); Ob_Trigger existingTrigger; if (!_stateStorageProviders.TryGetValue(key, out existingTrigger)) { _stateStorageProviders[key] = existingTrigger = trigger; } currentBranch.Triggers[key] = existingTrigger; }