public bool UpdateSimulation() { if (Rifle == null || Rifle.MarkedForClose) { return(false); } int updateRate = (OwnerIsLocalPlayer ? LocalToolHandler.PAINT_UPDATE_TICKS : Constants.TICKS_PER_SECOND); if (Main.Tick % updateRate == 0) { MyFixedPoint?amount = Rifle?.Owner?.GetInventory()?.GetItemAmount(Main.Constants.PAINT_MAG_ID); Ammo = (amount.HasValue ? (int)amount.Value : 0); } if (MagazineSubpart != null) { bool VisibleMag = (Ammo > 0); if (MagazineSubpart.Render.Visible != VisibleMag) { MagazineSubpart.Render.Visible = VisibleMag; } } if (SprayCooldown > 0) { SprayCooldown--; } SoundEmitter.PlaySpray = (Spraying && SprayCooldown == 0 && !OwnerInfo.ColorPickMode); SoundEmitter.Update(Main.Settings.spraySoundVolume); return(true); }
private void ConsumeFuel(ref MyDefinitionId gasTypeId, double iceAmount) { if (((Sync.IsServer && (base.CubeGrid.GridSystems.ControlSystem != null)) && (iceAmount > 0.0)) && !MySession.Static.CreativeMode) { List <MyPhysicalInventoryItem> items = this.GetInventory(0).GetItems(); if ((items.Count > 0) && (iceAmount > 0.0)) { int num = 0; while (num < items.Count) { MatrixD?nullable; MyPhysicalInventoryItem item = items[num]; if (item.Content is MyObjectBuilder_GasContainerObject) { num++; continue; } if (iceAmount < ((float)item.Amount)) { MyFixedPoint point = MyFixedPoint.Max((MyFixedPoint)iceAmount, MyFixedPoint.SmallestPossibleValue); nullable = null; this.GetInventory(0).RemoveItems(item.ItemId, new MyFixedPoint?(point), true, false, nullable); return; } iceAmount -= (float)item.Amount; MyFixedPoint?amount = null; nullable = null; this.GetInventory(0).RemoveItems(item.ItemId, amount, true, false, nullable); } } } }
public MyEntity RemoveItems(uint itemId, MyFixedPoint?amount = null, bool sendEvent = true, bool spawn = false, MatrixD?spawnPos = null) { var item = GetItemByID(itemId); var am = amount.HasValue ? amount.Value : (item.HasValue? item.Value.Amount : 1); MyEntity spawned = null; if (Sync.IsServer) { if (item.HasValue && RemoveItemsInternal(itemId, am, sendEvent)) { if (spawn) { var owner = Owner as MyEntity; if (!spawnPos.HasValue) { spawnPos = MatrixD.CreateWorld(owner.PositionComp.GetPosition() + owner.PositionComp.WorldMatrix.Forward + owner.PositionComp.WorldMatrix.Up, owner.PositionComp.WorldMatrix.Forward, owner.PositionComp.WorldMatrix.Up); } spawned = item.Value.Spawn(am, spawnPos.Value, owner); } SyncObject.SendRemoveItemsAnnounce(this, am, itemId); } } else { SyncObject.SendRemoveItemsRequest(this, am, itemId, spawn); } return(spawned); }
public bool ContainItems(MyFixedPoint?amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None) { int?itemPos = FindFirstPositionOfType(contentId, flags); if (!itemPos.HasValue) { return(false); } int i = itemPos.Value; MyFixedPoint amountPresent = m_items[i].Amount; if (!amount.HasValue || amountPresent >= amount) { return(true); } for (i++; i < m_items.Count; ++i) { var content = m_items[i].Content; if (contentId == content.GetObjectId() && flags == content.Flags) { amountPresent += m_items[i].Amount; } } return(amountPresent >= amount); }
public bool CheckConveyorResources(MyFixedPoint?amount, MyDefinitionId contentId) { foreach (var inv in m_inventoryOwners) { if (inv != null && inv is IMyInventoryOwner) { var cargo = inv as IMyInventoryOwner; var flags = cargo.GetInventory(0).GetFlags(); var flag = MyInventoryFlags.CanSend | MyInventoryFlags.CanReceive; List <MyInventory> inventories = new List <MyInventory>(); for (int i = 0; i < cargo.InventoryCount; i++) { inventories.Add(cargo.GetInventory(i)); } foreach (var inventory in inventories) { if (inventory.ContainItems(amount, contentId) && ((flags == flag || flags == MyInventoryFlags.CanSend) || cargo == this)) { return(true); } } } } return(false); }
public bool CheckConveyorResources(MyFixedPoint?amount, MyDefinitionId contentId) { foreach (var inv in m_inventoryOwners) { if (inv != null) { var cargo = inv as MyEntity; if (cargo != null && cargo.HasInventory) { System.Diagnostics.Debug.Assert((cargo.GetInventory(0) as MyInventory) != null, "Null or other inventory type!"); var flags = (cargo.GetInventory(0) as MyInventory).GetFlags(); var flag = MyInventoryFlags.CanSend | MyInventoryFlags.CanReceive; List <MyInventory> inventories = new List <MyInventory>(); for (int i = 0; i < cargo.InventoryCount; i++) { System.Diagnostics.Debug.Assert((cargo.GetInventory(i) as MyInventory) != null, "Null or other inventory type!"); inventories.Add(cargo.GetInventory(i) as MyInventory); } foreach (var inventory in inventories) { if (inventory.ContainItems(amount, contentId) && ((flags == flag || flags == MyInventoryFlags.CanSend) || cargo == this)) { return(true); } } } } } return(false); }
public override void UpdateBeforeSimulation100() { base.UpdateBeforeSimulation100(); if (((Sync.IsServer && (this.DrainAll && (base.Enabled && base.IsFunctional))) && base.IsWorking) && !this.GetInventory(0).IsFull) { MyFixedPoint?maxAmount = null; MyGridConveyorSystem.PullAllRequest(this, this.GetInventory(0), base.OwnerId, this.m_inventoryConstraint, maxAmount, true); } }
protected override void Init(MyObjectBuilder_DefinitionBase builder) { base.Init(builder); MyObjectBuilder_RefineryDefinition definition = builder as MyObjectBuilder_RefineryDefinition; this.RefineSpeed = definition.RefineSpeed; this.MaterialEfficiency = definition.MaterialEfficiency; this.OreAmountPerPullRequest = definition.OreAmountPerPullRequest; }
public MyEntity RemoveItemsAt(int itemIndex, MyFixedPoint?amount = null, bool sendEvent = true, bool spawn = false, MatrixD?spawnPos = null) { if (itemIndex < 0 || itemIndex >= m_items.Count) { Debug.Assert(true, "Index out of range."); return(null); } return(RemoveItems(m_items[itemIndex].ItemId, amount, sendEvent, spawn, spawnPos)); }
public bool TransferItemTo(IMyInventory dstInventory, MyInventoryItem item, MyFixedPoint?amount = null) { var myItem = GetItemByID(item.ItemId); if (myItem == null) { return(false); } var existingAmount = GetItemAmount(item.Type); Remove(item.Type, amount ?? existingAmount); return(existingAmount > 0); }
private static void TransferOrRemove(MyInventory src, MyFixedPoint?amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyInventory dst = null, bool spawn = false) { //Debug.Assert(!amount.HasValue || amount.Value > 0, "Transfering 0 amount of item."); if (src.ContainItems(amount, contentId, flags)) { bool transferAll = !amount.HasValue; MyFixedPoint remainingAmount = transferAll ? 0 : amount.Value; int i = 0; while (i < src.m_items.Count) { if (!transferAll && remainingAmount == 0) { break; } MyInventoryItem item = src.m_items[i]; //TODO(AF) Remove oxygen specific code from inventory. //Will be fixed once MyInventory will support Entities. var oxygenBottle = item.Content as MyObjectBuilder_OxygenContainerObject; if (oxygenBottle != null && oxygenBottle.OxygenLevel == 1f) { i++; continue; } // End of oxygen specific code if (item.Content.GetObjectId() != contentId) { i++; continue; } if (transferAll || remainingAmount >= item.Amount) { remainingAmount -= item.Amount; Transfer(src, dst, item.ItemId, -1, spawn: spawn); } else { Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn); remainingAmount = 0; } } } }
public override void UpdateAfterSimulation100() { if (Sync.IsServer && base.IsWorking) { if ((this.m_useConveyorSystem != null) && (this.GetInventory(0).VolumeFillFactor < 0.6f)) { MyFixedPoint?maxAmount = null; MyGridConveyorSystem.PullAllRequest(this, this.GetInventory(0), base.OwnerId, this.HasIce() ? this.GetInventory(0).Constraint : this.m_oreConstraint, maxAmount, false); } if (this.AutoRefill && this.CanRefill()) { this.RefillBottles(); } } this.m_isProducing = true; base.NeedsUpdate |= MyEntityUpdateEnum.EACH_FRAME; }
public override void UpdateAfterSimulation() { base.UpdateAfterSimulation(); if (Sync.IsServer) { if ((this.FilledRatio > 0.0) && this.UseConveyorSystem) { MyInventory destinationInventory = this.GetInventory(0); if (destinationInventory.VolumeFillFactor < 0.6f) { MyFixedPoint?maxAmount = null; MyGridConveyorSystem.PullAllRequest(this, destinationInventory, base.OwnerId, destinationInventory.Constraint, maxAmount, true); } } this.ExecuteGasTransfer(); } }
public static void TransferItemInternal( this MyInventory src, MyInventory dst, uint srcItemId, bool spawn = false, int destItemIndex = -1, MyFixedPoint?amount = null) { lock (Args) { Args[0] = src; Args[1] = dst; Args[2] = srcItemId; Args[3] = spawn; Args[4] = destItemIndex; Args[5] = amount ?? src.GetItemByID(srcItemId)?.Amount ?? 0; Method.Invoke(null, Flags, null, Args, null); Array.Clear(Args, 0, Args.Length); } }
public static MyFixedPoint MoveItem(IMyCargoContainer from, IMyCargoContainer to, string type, MyFixedPoint?amount) { MyFixedPoint amountmoved = new MyFixedPoint(); MyInventoryItem?item; bool moved; for (int i = from.GetInventory(0).ItemCount; i >= 0; i--) { item = from.GetInventory(0).GetItemAt(i); if (item.HasValue && item.Value.Type.SubtypeId == type) { MyFixedPoint amounttomove = amount.HasValue ? MyFixedPoint.Min(amount.Value - amountmoved, item.Value.Amount) : item.Value.Amount; moved = to.GetInventory(0).TransferItemFrom(from.GetInventory(0), item.Value, amounttomove); if (!moved) { return(amountmoved); } amountmoved += amounttomove; if (amountmoved >= amount) { return(amountmoved); } } } return(amountmoved); }
public static void Transfer(MyInventory src, MyInventory dst, uint srcItemId, int dstIdx = -1, MyFixedPoint?amount = null, bool spawn = false) { var itemNullable = src.GetItemByID(srcItemId); if (!itemNullable.HasValue) { return; } var item = itemNullable.Value; if (dst != null && !dst.CheckConstraint(item.Content.GetObjectId())) { return; } var transferAmount = amount ?? item.Amount; SyncObject.TransferItems(src, transferAmount, srcItemId, dst, dstIdx, spawn); }
/// <summary> /// Attempts to move certain item type from one inventory to another. /// </summary> /// <param name="from">Source inventory.</param> /// <param name="to">Destination inventory.</param> /// <param name="type">Item type to move.</param> /// <param name="amount">How much to move. If not specified, attempts to move every item of set type.</param> /// <returns>The amount actually moved.</returns> public static MyFixedPoint MoveItem(IMyInventory from, IMyInventory to, MyItemType type, MyFixedPoint?amount = null) { MyFixedPoint amountmoved = new MyFixedPoint(); MyInventoryItem?item; bool moved; for (int i = from.ItemCount; i >= 0; i--) { item = from.GetItemAt(i); if (item.HasValue && item.Value.Type == type) { MyFixedPoint amounttomove = amount.HasValue ? MyFixedPoint.Min(amount.Value - amountmoved, item.Value.Amount) : item.Value.Amount; moved = to.TransferItemFrom(from, item.Value, amounttomove); if (!moved) { return(amountmoved); } amountmoved += amounttomove; if (amountmoved >= amount) { return(amountmoved); } } } return(amountmoved); }
private static void TransferOrRemove(MyInventory src, MyFixedPoint?amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyInventory dst = null, bool spawn = false) { //Debug.Assert(!amount.HasValue || amount.Value > 0, "Transfering 0 amount of item."); if (src.ContainItems(amount, contentId, flags)) { bool transferAll = !amount.HasValue; MyFixedPoint remainingAmount = transferAll ? 0 : amount.Value; //TODO(AF) Remove oxygen specific code from inventory. //Will be fixed once MyInventory will support Entities. // If the requested item is an oxygen container, do a preliminary loop to pull any non-full items first. if (contentId.TypeId == typeof(MyObjectBuilder_OxygenContainerObject)) { int k = 0; while (k < src.m_items.Count) { if (!transferAll && remainingAmount == 0) { break; } MyPhysicalInventoryItem item = src.m_items[k]; // Skip full oxygen bottles in this loop. They will not be skipped in the next one. var oxygenBottle = item.Content as MyObjectBuilder_OxygenContainerObject; if (oxygenBottle != null && oxygenBottle.OxygenLevel == 1f) { k++; continue; } if (item.Content.GetObjectId() != contentId) { k++; continue; } if (transferAll || remainingAmount >= item.Amount) { remainingAmount -= item.Amount; Transfer(src, dst, item.ItemId, -1, spawn: spawn); } else { Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn); remainingAmount = 0; } } } // End of oxygen specific code int i = 0; while (i < src.m_items.Count) { if (!transferAll && remainingAmount == 0) { break; } MyPhysicalInventoryItem item = src.m_items[i]; if (item.Content.GetObjectId() != contentId) { i++; continue; } if (transferAll || remainingAmount >= item.Amount) { remainingAmount -= item.Amount; Transfer(src, dst, item.ItemId, -1, spawn: spawn); } else { Transfer(src, dst, item.ItemId, -1, remainingAmount, spawn); remainingAmount = 0; } } } }
public void TransferItemFrom(MyInventory sourceInventory, int sourceItemIndex, int?targetItemIndex = null, bool?stackIfPossible = null, MyFixedPoint?amount = null) { Debug.Assert(sourceInventory != null); if (this == sourceInventory) { return; } if (sourceItemIndex < 0 || sourceItemIndex >= sourceInventory.m_items.Count) { return; } Transfer(sourceInventory, this, sourceInventory.GetItems()[sourceItemIndex].ItemId, targetItemIndex.HasValue ? targetItemIndex.Value : -1, amount); }
public bool TransferItemFrom(IMyInventory sourceInventory, MyInventoryItem item, MyFixedPoint?amount = null) => throw new NotImplementedException();
public static void Transfer(MyInventory src, MyInventory dst, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None, MyFixedPoint?amount = null, bool spawn = false) { TransferOrRemove(src, amount, contentId, flags, dst); }
public bool TransferItemTo(IMyInventory dstInventory, MyInventoryItem item, MyFixedPoint?amount = null) { throw new NotImplementedException(); }
public bool TransferItemFrom(IMyInventory sourceInventory, int sourceItemIndex, int?targetItemIndex = null, bool?stackIfPossible = null, MyFixedPoint?amount = null) { throw new NotImplementedException(); }
public bool TransferItemTo(IMyInventory dst, int sourceItemIndex, int?targetItemIndex = default(int?), bool?stackIfPossible = default(bool?), MyFixedPoint?amount = default(MyFixedPoint?)) { throw new NotImplementedException(); }
public static MyFixedPoint ItemPullRequest(IMyConveyorEndpointBlock start, MyInventory destinationInventory, long playerId, MyDefinitionId itemId, MyFixedPoint?amount = null, bool remove = false) { MyFixedPoint transferred = 0; using (var invertedConductivity = new MyConveyorLine.InvertedConductivity()) { /*if (amount.HasValue) * Debug.Assert(itemId.TypeId == typeof(MyObjectBuilder_Ore) || * itemId.TypeId == typeof(MyObjectBuilder_Ingot) || * MyFixedPoint.Floor(amount.Value) == amount.Value);*/ SetTraversalPlayerId(playerId); SetTraversalInventoryItemDefinitionId(itemId); PrepareTraversal(start.ConveyorEndpoint, null, IsAccessAllowedPredicate, NeedsLargeTube(itemId) ? IsConveyorLargePredicate : null); foreach (var conveyorEndpoint in MyGridConveyorSystem.Pathfinding) { MyCubeBlock owner = (conveyorEndpoint.CubeBlock != null && conveyorEndpoint.CubeBlock.HasInventory) ? conveyorEndpoint.CubeBlock : null; if (owner == null) { continue; } for (int i = 0; i < owner.InventoryCount; ++i) { var inventory = owner.GetInventory(i) as MyInventory; System.Diagnostics.Debug.Assert(inventory != null, "Null or other inventory type!"); if ((inventory.GetFlags() & MyInventoryFlags.CanSend) == 0) { continue; } if (inventory == destinationInventory) { continue; } var availableAmount = inventory.GetItemAmount(itemId); if (amount.HasValue) { availableAmount = amount.HasValue ? MyFixedPoint.Min(availableAmount, amount.Value) : availableAmount; if (availableAmount == 0) { continue; } if (remove) { transferred += inventory.RemoveItemsOfType(availableAmount, itemId); } else { transferred += MyInventory.Transfer(inventory, destinationInventory, itemId, MyItemFlags.None, availableAmount); } amount -= availableAmount; if (amount.Value == 0) { return(transferred); } } else { if (remove) { transferred += inventory.RemoveItemsOfType(availableAmount, itemId); } else { transferred += MyInventory.Transfer(inventory, destinationInventory, itemId, MyItemFlags.None, availableAmount); } } } } } return(transferred); }
public static bool ItemPushRequest(IMyConveyorEndpointBlock start, MyInventory srcInventory, long playerId, MyPhysicalInventoryItem toSend, MyFixedPoint?amount = null) { var itemBuilder = toSend.Content; if (amount.HasValue) { Debug.Assert(toSend.Content.TypeId == typeof(MyObjectBuilder_Ore) || toSend.Content.TypeId == typeof(MyObjectBuilder_Ingot) || MyFixedPoint.Floor(amount.Value) == amount.Value); } MyFixedPoint remainingAmount = toSend.Amount; if (amount.HasValue) { remainingAmount = amount.Value; } SetTraversalPlayerId(playerId); var toSendContentId = toSend.Content.GetId(); SetTraversalInventoryItemDefinitionId(toSendContentId); if (NeedsLargeTube(toSendContentId)) { PrepareTraversal(start.ConveyorEndpoint, null, IsAccessAllowedPredicate, IsConveyorLargePredicate); } else { PrepareTraversal(start.ConveyorEndpoint, null, IsAccessAllowedPredicate); } bool success = false; foreach (var conveyorEndpoint in MyGridConveyorSystem.Pathfinding) { MyCubeBlock owner = (conveyorEndpoint.CubeBlock != null && conveyorEndpoint.CubeBlock.HasInventory) ? conveyorEndpoint.CubeBlock : null; if (owner == null) { continue; } for (int i = 0; i < owner.InventoryCount; ++i) { var inventory = owner.GetInventory(i) as MyInventory; System.Diagnostics.Debug.Assert(inventory != null, "Null or other inventory type!"); if ((inventory.GetFlags() & MyInventoryFlags.CanReceive) == 0) { continue; } if (inventory == srcInventory) { continue; } var fittingAmount = inventory.ComputeAmountThatFits(toSendContentId); fittingAmount = MyFixedPoint.Min(fittingAmount, remainingAmount); if (!inventory.CheckConstraint(toSendContentId)) { continue; } if (fittingAmount == 0) { continue; } MyInventory.Transfer(srcInventory, inventory, toSend.ItemId, -1, fittingAmount); success = true; } } return(success); }
public bool ContainItems(MyFixedPoint? amount, MyDefinitionId contentId, MyItemFlags flags = MyItemFlags.None) { MyFixedPoint amountPresent = GetItemAmount(contentId, flags); return amountPresent >= amount; }
public static void ItemPullRequest(IMyConveyorEndpointBlock start, MyInventory destinationInventory, long playerId, MyDefinitionId itemId, MyFixedPoint?amount = null) { using (var invertedConductivity = new MyConveyorLine.InvertedConductivity()) { if (amount.HasValue) { Debug.Assert(itemId.TypeId == typeof(MyObjectBuilder_Ore) || itemId.TypeId == typeof(MyObjectBuilder_Ingot) || MyFixedPoint.Floor(amount.Value) == amount.Value); } SetTraversalPlayerId(playerId); SetTraversalInventoryItemDefinitionId(itemId); PrepareTraversal(start.ConveyorEndpoint, null, IsAccessAllowedPredicate, NeedsLargeTube(itemId) ? IsConveyorLargePredicate : null); foreach (var conveyorEndpoint in MyGridConveyorSystem.Pathfinding) { IMyInventoryOwner owner = conveyorEndpoint.CubeBlock as IMyInventoryOwner; if (owner == null) { continue; } for (int i = 0; i < owner.InventoryCount; ++i) { var inventory = owner.GetInventory(i); if ((inventory.GetFlags() & MyInventoryFlags.CanSend) == 0) { continue; } if (inventory == destinationInventory) { continue; } if (amount.HasValue) { var availableAmount = inventory.GetItemAmount(itemId); availableAmount = amount.HasValue ? MyFixedPoint.Min(availableAmount, amount.Value) : availableAmount; if (availableAmount == 0) { continue; } MyInventory.Transfer(inventory, destinationInventory, itemId, MyItemFlags.None, availableAmount); amount -= availableAmount; if (amount.Value == 0) { return; } } else { MyInventory.Transfer(inventory, destinationInventory, itemId, MyItemFlags.None); } } } } }