private void OnChildAggregateCountChanged(MyInventoryAggregate obj, int change) { InventoryCount += change; }
/// <summary> /// Naive looking for inventories with some items.. /// </summary> static public MyInventoryAggregate FixInputOutputInventories(MyInventoryAggregate inventoryAggregate, MyInventoryConstraint inputInventoryConstraint, MyInventoryConstraint outputInventoryConstraint) { MyInventory inputInventory = null; MyInventory outputInventory = null; foreach (var inventory in inventoryAggregate.ChildList.Reader) { var myInventory = inventory as MyInventory; if (myInventory == null) { continue; } if (myInventory.GetItemsCount() > 0) { if (inputInventory == null) { bool check = true; if (inputInventoryConstraint != null) { foreach (var item in myInventory.GetItems()) { check &= inputInventoryConstraint.Check(item.GetDefinitionId()); } } if (check) { inputInventory = myInventory; } } if (outputInventory == null && inputInventory != myInventory) { bool check = true; if (outputInventoryConstraint != null) { foreach (var item in myInventory.GetItems()) { check &= outputInventoryConstraint.Check(item.GetDefinitionId()); } } if (check) { outputInventory = myInventory; } } } } if (inputInventory == null || outputInventory == null) { foreach (var inventory in inventoryAggregate.ChildList.Reader) { var myInventory = inventory as MyInventory; if (myInventory == null) { continue; } if (inputInventory == null) { inputInventory = myInventory; } else if (outputInventory == null) { outputInventory = myInventory; } else { break; } } } inventoryAggregate.RemoveComponent(inputInventory); inventoryAggregate.RemoveComponent(outputInventory); var fixedAggregate = new MyInventoryAggregate(); fixedAggregate.AddComponent(inputInventory); fixedAggregate.AddComponent(outputInventory); return(fixedAggregate); }
private void OnInventoryAggregateRemoved(MyEntityComponentBase component) { m_inputInventory = null; m_outputInventory = null; m_inventoryAggregate.BeforeRemovedFromContainer -= OnInventoryAggregateRemoved; m_inventoryAggregate.OnAfterComponentAdd -= OnInventoryAddedToAggregate; m_inventoryAggregate.OnBeforeComponentRemove -= OnBeforeInventoryRemovedFromAggregate; m_inventoryAggregate = null; }
protected virtual void OnBeforeInventoryRemovedFromAggregate(MyInventoryAggregate aggregate, MyInventoryBase inventory) { if (inventory == m_inputInventory) { m_inputInventory = null; } else if (inventory == m_outputInventory) { m_outputInventory = null; } else { Debug.Fail("Removing inventory from aggregate, but isn't neither input nor output! This shouldn't happend."); } }
private void OnComponentAdded(Type type, VRage.Game.Components.MyEntityComponentBase component) { var aggregate = component as MyInventoryAggregate; if (aggregate != null) { m_inventoryAggregate = aggregate; m_inventoryAggregate.BeforeRemovedFromContainer += OnInventoryAggregateRemoved; m_inventoryAggregate.OnAfterComponentAdd += OnInventoryAddedToAggregate; m_inventoryAggregate.OnBeforeComponentRemove += OnBeforeInventoryRemovedFromAggregate; foreach (var inventory in m_inventoryAggregate.ChildList.Reader) { MyInventory inv = inventory as MyInventory; OnInventoryAddedToAggregate(aggregate, inv); } } }
protected virtual void OnInventoryAddedToAggregate(MyInventoryAggregate aggregate, MyInventoryBase inventory) { if (m_inputInventory == null) { m_inputInventory = inventory as MyInventory; } else if (m_outputInventory == null) { m_outputInventory = inventory as MyInventory; } else { Debug.Fail("Adding inventory to aggregate, but input and output inventory is already set!"); } }
public override void Init(MyObjectBuilder_CubeBlock objectBuilder, MyCubeGrid cubeGrid) { SyncFlag = true; var sinkComp = new MyResourceSinkComponent(); sinkComp.Init(ProductionBlockDefinition.ResourceSinkGroup, ProductionBlockDefinition.OperationalPowerConsumption, ComputeRequiredPower); sinkComp.IsPoweredChanged += Receiver_IsPoweredChanged; ResourceSink = sinkComp; base.Init(objectBuilder, cubeGrid); var ob = (MyObjectBuilder_ProductionBlock)objectBuilder; if (InventoryAggregate == null) { InventoryAggregate = new MyInventoryAggregate(); } if (InputInventory == null) { InputInventory = new MyInventory( ProductionBlockDefinition.InventoryMaxVolume, ProductionBlockDefinition.InventorySize, MyInventoryFlags.CanReceive); if (ob.InputInventory != null) InputInventory.Init(ob.InputInventory); } Debug.Assert(InputInventory.Owner == this, "Ownership was not set!"); if (OutputInventory == null) { OutputInventory = new MyInventory( ProductionBlockDefinition.InventoryMaxVolume, ProductionBlockDefinition.InventorySize, MyInventoryFlags.CanSend); if (ob.OutputInventory != null) OutputInventory.Init(ob.OutputInventory); } Debug.Assert(OutputInventory.Owner == this, "Ownership was not set!"); m_nextItemId = ob.NextItemId; bool nextIdWasZero = m_nextItemId == 0; base.IsWorkingChanged += CubeBlock_IsWorkingChanged; ResourceSink.Update(); AddDebugRenderComponent(new Components.MyDebugRenderComponentDrawPowerReciever(ResourceSink, this)); SlimBlock.ComponentStack.IsFunctionalChanged += ComponentStack_IsFunctionalChanged; if (ob.Queue != null) { m_queue.Clear(); if (m_queue.Capacity < ob.Queue.Length) m_queue.Capacity = ob.Queue.Length; for (int i = 0; i < ob.Queue.Length; ++i) { var item = ob.Queue[i]; Debug.Assert(item.ItemId != null || nextIdWasZero, "Item index was null while next id for production block was given non-zero. This is inconsistency."); var deserializedItem = DeserializeQueueItem(item); Debug.Assert(deserializedItem.Blueprint != null, "Could not add item into production block's queue: Blueprint was not found."); if (deserializedItem.Blueprint != null) { m_queue.Add(deserializedItem); } else { MySandboxGame.Log.WriteLine(string.Format("Could not add item into production block's queue: Blueprint {0} was not found.", item.Id)); } } UpdatePower(); } m_useConveyorSystem.Value = ob.UseConveyorSystem; m_lastUpdateTime = MySandboxGame.TotalGamePlayTimeInMilliseconds; }
public static MyInventoryAggregate FixInputOutputInventories(MyInventoryAggregate inventoryAggregate, MyInventoryConstraint inputInventoryConstraint, MyInventoryConstraint outputInventoryConstraint) { MyInventory objA = null; MyInventory component = null; foreach (MyInventory inventory3 in inventoryAggregate.ChildList.Reader) { if (inventory3 == null) { continue; } if (inventory3.GetItemsCount() > 0) { if (objA == null) { bool flag = true; if (inputInventoryConstraint != null) { foreach (MyPhysicalInventoryItem item in inventory3.GetItems()) { flag &= inputInventoryConstraint.Check(item.GetDefinitionId()); } } if (flag) { objA = inventory3; } } if ((component == null) && !ReferenceEquals(objA, inventory3)) { bool flag2 = true; if (outputInventoryConstraint != null) { foreach (MyPhysicalInventoryItem item2 in inventory3.GetItems()) { flag2 &= outputInventoryConstraint.Check(item2.GetDefinitionId()); } } if (flag2) { component = inventory3; } } } } if ((objA == null) || (component == null)) { foreach (MyInventory inventory4 in inventoryAggregate.ChildList.Reader) { if (inventory4 != null) { if (objA == null) { objA = inventory4; continue; } if (component != null) { break; } component = inventory4; } } } inventoryAggregate.RemoveComponent(objA); inventoryAggregate.RemoveComponent(component); MyInventoryAggregate aggregate = new MyInventoryAggregate(); aggregate.AddComponent(objA); aggregate.AddComponent(component); return(aggregate); }
/// <summary> /// Naive looking for inventories with some items.. /// </summary> static public MyInventoryAggregate FixInputOutputInventories(MyInventoryAggregate inventoryAggregate, MyInventoryConstraint inputInventoryConstraint, MyInventoryConstraint outputInventoryConstraint) { MyInventory inputInventory = null; MyInventory outputInventory = null; foreach (var inventory in inventoryAggregate.ChildList.Reader) { var myInventory = inventory as MyInventory; if (myInventory == null) continue; if (myInventory.GetItemsCount() > 0) { if (inputInventory == null) { bool check = true; if (inputInventoryConstraint != null) { foreach (var item in myInventory.GetItems()) { check &= inputInventoryConstraint.Check(item.GetDefinitionId()); } } if (check) { inputInventory = myInventory; } } if (outputInventory == null && inputInventory != myInventory) { bool check = true; if (outputInventoryConstraint != null) { foreach (var item in myInventory.GetItems()) { check &= outputInventoryConstraint.Check(item.GetDefinitionId()); } } if (check) { outputInventory = myInventory; } } } } if (inputInventory == null || outputInventory == null) { foreach (var inventory in inventoryAggregate.ChildList.Reader) { var myInventory = inventory as MyInventory; if (myInventory == null) continue; if (inputInventory == null) { inputInventory = myInventory; } else if (outputInventory == null) { outputInventory = myInventory; } else { break; } } } inventoryAggregate.RemoveComponent(inputInventory); inventoryAggregate.RemoveComponent(outputInventory); var fixedAggregate = new MyInventoryAggregate(); fixedAggregate.AddComponent(inputInventory); fixedAggregate.AddComponent(outputInventory); return fixedAggregate; }
public MyInventory(MyFixedPoint maxVolume, MyFixedPoint maxMass, Vector3 size, MyInventoryFlags flags, MyEntity owner) : base("Inventory") { m_maxVolume = maxVolume; m_maxMass = maxMass; m_flags = flags; SyncType = SyncHelpers.Compose(this); m_currentVolume.ValueChanged += (x) => PropertiesChanged(); m_currentVolume.ValidateNever(); m_currentMass.ValueChanged += (x) => PropertiesChanged(); m_currentMass.ValidateNever(); Clear(); //Debug.Assert(owner != null, "Inventory must have always owner!"); - nope, this can be deserialized and therefore owner can be set to null.. if (owner != null) { MyInventoryBase inventory; if ((owner as MyEntity).Components.TryGet<MyInventoryBase>(out inventory) && inventory is IMyComponentAggregate) { IMyComponentAggregate aggregate = inventory as IMyComponentAggregate; if (!aggregate.ChildList.Contains(this)) { aggregate.AddComponent(this); } } else if (inventory != null) { MyInventoryAggregate aggregate = new MyInventoryAggregate(); (owner as MyEntity).Components.Add<MyInventoryBase>(aggregate); aggregate.AddComponent(inventory); aggregate.AddComponent(this); } else { (owner as MyEntity).Components.Add<MyInventoryBase>(this); } } }