/// <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); }
/// <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 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); }
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); } } }