public MyBlockGroup AddUpdateGroup(MyBlockGroup group) { //Can happen on split //Debug.Assert(group.Blocks.Count > 0, "Empty group should not be added to system."); if (group.Blocks.Count == 0) { return(null); } bool modified = false; for (int index = 0; index < BlockGroups.Count; index++) { var g = BlockGroups[index]; if (g.Name.CompareTo(group.Name) == 0) { if (group.CubeGrid != null) //change came from grid i.e. destroyed block that was in group { for (int i = 0; i < g.Blocks.Count; i++) { if (g.Blocks[i].CubeGrid == group.CubeGrid) { g.Blocks.RemoveAt(i); i--; } } } else //change came from gui, we clear group and add all blocks that came in { g.Blocks.Clear(); } foreach (var b in group.Blocks) { if (g.Blocks.Contains(b)) { continue; } g.Blocks.Add(b); } group = g; modified = true; break; } } if (!modified) //new group { var g = new MyBlockGroup(null); g.Name.Clear().AppendStringBuilder(group.Name); g.Blocks.AddList(group.Blocks); BlockGroups.Add(g); group = g; } if (GroupAdded != null) { GroupAdded(group); } return(group); }
internal bool UpdateState(Menu oldMenu, Item item, Update update, bool reset = true) { if (reset) { oldMenu.CleanUp(); GroupNames.Clear(); foreach (var group in BlockGroups) { group.Clear(); MembersPool.Return(group); } BlockGroups.Clear(); foreach (var group in Ai.BlockGroups) { var groupName = group.Key; GroupNames.Add(groupName); var membersList = MembersPool.Get(); foreach (var comp in group.Value.Comps) { var groupMember = new GroupMember { Comp = comp, Name = groupName }; membersList.Add(groupMember); } BlockGroups.Add(membersList); } } var groupReady = BlockGroups.Count > 0; if (groupReady) { switch (update) { case Update.Parent: _currentMenu = item.ParentName; break; case Update.Sub: _currentMenu = item.SubName; break; default: break; } var menu = Menus[_currentMenu]; if (menu.ItemCount <= 1) { menu.LoadInfo(reset); } } return(groupReady); }
internal void ReScanBlockGroups() { if (TerminalSystem == null) { TerminalSystem = MyAPIGateway.TerminalActionsHelper.GetTerminalSystemForGrid(MyGrid); } if (TerminalSystem != null) { TerminalSystem.GetBlockGroups(null, group => { GroupInfo groupInfo = null; if (BlockGroups.TryGetValue(group.Name, out groupInfo)) { groupInfo.ChangeState = GroupInfo.ChangeStates.None; groupInfo.Name = group.Name; } group.GetBlocks(null, block => { var cube = (MyCubeBlock)block; WeaponComponent comp; if (cube.Components.TryGet(out comp) && SubGrids.Contains(cube.CubeGrid)) { if (groupInfo == null) { groupInfo = Session.GroupInfoPool.Get(); groupInfo.Name = group.Name; groupInfo.ChangeState = GroupInfo.ChangeStates.Add; BlockGroups.Add(group.Name, groupInfo); } groupInfo.Comps.Add(comp); if (groupInfo.ChangeState == GroupInfo.ChangeStates.None) { groupInfo.ChangeState = GroupInfo.ChangeStates.Modify; } } return(false); }); return(false); }); BlockGroups.ApplyAdditionsAndModifications(); foreach (var group in BlockGroups) { if (group.Value.ChangeState == GroupInfo.ChangeStates.None) { group.Value.Comps.Clear(); Session.GroupInfoPool.Return(group.Value); BlockGroups.Remove(group.Key); } else { group.Value.ChangeState = GroupInfo.ChangeStates.None; } if (Session.MpActive && group.Value.Comps != null) { foreach (var comp in group.Value.Comps) { if (comp.Set?.Value?.Overrides != null) { SyncGridOverrides(this, group.Key, comp.Set.Value.Overrides); } break; } } } BlockGroups.ApplyRemovals(); ScanBlockGroups = false; } }