Пример #1
0
        // CH: TODO: This method actually doesn't have a bad internal structure, but it should be refactored BIG TIME (and put to MyHudBlockInfo)!
        private static void SetBlockComponentsInternal(MyHudBlockInfo hudInfo, MyCubeBlockDefinition blockDefinition, MySlimBlock block, MyInventoryBase availableInventory)
        {
            hudInfo.Components.Clear();

            if (block != null)
            {
                Debug.Assert(block.BlockDefinition == blockDefinition, "The definition given to SetBlockComponnentsInternal was not a definition of the block");
            }

            hudInfo.InitBlockInfo(blockDefinition);
            hudInfo.ShowAvailable = MyPerGameSettings.AlwaysShowAvailableBlocksOnHud;

            if (!MyFakes.ENABLE_SMALL_GRID_BLOCK_COMPONENT_INFO && blockDefinition.CubeSize == MyCubeSize.Small) return;

            if (block != null)
            {
                hudInfo.BlockIntegrity = block.Integrity / block.MaxIntegrity;
            }

            // CH: TODO: Multiblocks
            if (block != null && block.IsMultiBlockPart)
            {
                var multiBlockInfo = block.CubeGrid.GetMultiBlockInfo(block.MultiBlockId);
                Debug.Assert(multiBlockInfo != null);
                if (multiBlockInfo != null)
                {
                    // Load all block definition components
                    foreach (var blockDefId in multiBlockInfo.MultiBlockDefinition.BlockDefinitions) 
                    {
                        MyCubeBlockDefinition blockDef;
                        if (MyDefinitionManager.Static.TryGetCubeBlockDefinition(blockDefId.Id, out blockDef))
                        {
                            hudInfo.AddComponentsForBlock(blockDef);
                        }
                    }

                    // Merge components from all blocks
                    hudInfo.MergeSameComponents();

                    // Add mounted counts to components
                    foreach (var multiBlockPart in multiBlockInfo.Blocks)
                    {
                        for (int j = 0; j < multiBlockPart.BlockDefinition.Components.Length; ++j)
                        {
                            var comp = multiBlockPart.BlockDefinition.Components[j];
                            var groupInfo = multiBlockPart.ComponentStack.GetGroupInfo(j);

                            for (int i = 0; i < hudInfo.Components.Count; i++)
                            {
                                if (hudInfo.Components[i].DefinitionId == comp.Definition.Id)
                                {
                                    var c = hudInfo.Components[i];
                                    c.MountedCount += groupInfo.MountedCount;
                                    hudInfo.Components[i] = c;
                                    break;
                                }
                            }
                        }
                    }

                    // Inventory counts
                    for (int i = 0; i < hudInfo.Components.Count; i++)
                    {
                        if (availableInventory != null)
                        {
                            var c = hudInfo.Components[i];
                            c.AvailableAmount = (int)MyCubeBuilder.BuildComponent.GetItemAmountCombined(availableInventory, c.DefinitionId);
                            hudInfo.Components[i] = c;
                        }

                        // Get amount in stockpile
                        int amount = 0;
                        foreach (var multiBlockPart in multiBlockInfo.Blocks)
                        {
                            if (!multiBlockPart.StockpileEmpty)
                                amount += multiBlockPart.GetConstructionStockpileItemAmount(hudInfo.Components[i].DefinitionId);
                        }

                        if (amount > 0)
                        {
                            //RKTODO: ??? see below code for non multiblocks
                            /*amount =*/ SetHudInfoComponentAmount(hudInfo, amount, i);
                        }
                    }
                }
            }
            else if (block == null && blockDefinition.MultiBlock != null)
            {
                MyDefinitionId defId = new MyDefinitionId(typeof(MyObjectBuilder_MultiBlockDefinition), blockDefinition.MultiBlock);
                var mbDefinition = MyDefinitionManager.Static.TryGetMultiBlockDefinition(defId);
                if (mbDefinition != null)
                {
                    foreach (var blockDefId in mbDefinition.BlockDefinitions)
                    {
                        MyCubeBlockDefinition blockDef;
                        if (MyDefinitionManager.Static.TryGetCubeBlockDefinition(blockDefId.Id, out blockDef))
                        {
                            hudInfo.AddComponentsForBlock(blockDef);
                        }
                    }

                    // Merge components from all blocks
                    hudInfo.MergeSameComponents();

                    for (int i = 0; i < hudInfo.Components.Count; ++i)
                    {
                        var component = hudInfo.Components[i];
                        component.AvailableAmount = (int)MyCubeBuilder.BuildComponent.GetItemAmountCombined(availableInventory, component.DefinitionId);
                        hudInfo.Components[i] = component;
                    }
                }
            }
            else
            {
                for (int i = 0; i < blockDefinition.Components.Length; i++)
                {
                    MyComponentStack.GroupInfo groupInfo = new MyComponentStack.GroupInfo();
                    if (block != null)
                    {
                        groupInfo = block.ComponentStack.GetGroupInfo(i);
                    }
                    else
                    {
                        var component = blockDefinition.Components[i];
                        groupInfo.Component = component.Definition;
                        groupInfo.TotalCount = component.Count;
                        groupInfo.MountedCount = 0;
                        groupInfo.AvailableCount = 0;
                        groupInfo.Integrity = 0.0f;
                        groupInfo.MaxIntegrity = component.Count * component.Definition.MaxIntegrity;
                    }
                    AddBlockComponent(hudInfo, groupInfo, availableInventory);
                }

                if (block != null && !block.StockpileEmpty)
                {
                    // For each component
                    foreach (var comp in block.BlockDefinition.Components)
                    {
                        // Get amount in stockpile
                        int amount = block.GetConstructionStockpileItemAmount(comp.Definition.Id);

                        if (amount > 0)
                        {
                            for (int i = 0; i < hudInfo.Components.Count; i++)
                            {
                                if (block.ComponentStack.GetGroupInfo(i).Component == comp.Definition)
                                {
                                    if (block.ComponentStack.IsFullyDismounted)
                                    {
                                        return;
                                    }

                                    amount = SetHudInfoComponentAmount(hudInfo, amount, i);
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
		public static void SetBlockComponents(MyHudBlockInfo hudInfo, MySlimBlock block, MyInventoryBase availableInventory = null)
		{
			hudInfo.Components.Clear();
			for (int i = 0; i < block.ComponentStack.GroupCount; i++)
			{
				var groupInfo = block.ComponentStack.GetGroupInfo(i);
				var componentInfo = new MyHudBlockInfo.ComponentInfo();
                componentInfo.DefinitionId = groupInfo.Component.Id;
				componentInfo.ComponentName = groupInfo.Component.DisplayNameText;
				componentInfo.Icon = groupInfo.Component.Icon;
				componentInfo.TotalCount = groupInfo.TotalCount;
				componentInfo.MountedCount = groupInfo.MountedCount;
				if (availableInventory != null)
					componentInfo.AvailableAmount = (int)availableInventory.GetItemAmount(groupInfo.Component.Id);

				hudInfo.Components.Add(componentInfo);
			}

			if (!block.StockpileEmpty)
			{
				// For each component
				foreach (var comp in block.BlockDefinition.Components)
				{
					// Get amount in stockpile
					int amount = block.GetConstructionStockpileItemAmount(comp.Definition.Id);

					for (int i = 0; amount > 0 && i < hudInfo.Components.Count; i++)
					{
						if (block.ComponentStack.GetGroupInfo(i).Component == comp.Definition)
						{
							if (block.ComponentStack.IsFullyDismounted)
							{
								return;
							}
							// Distribute amount in stockpile from bottom to top
							var info = hudInfo.Components[i];
							int space = info.TotalCount - info.MountedCount;
							int movedItems = Math.Min(space, amount);
							info.StockpileCount = movedItems;
							amount -= movedItems;
							hudInfo.Components[i] = info;
						}
					}
				}
			}
		}