public override MyObjectBuilder_CubeBlock GetObjectBuilderCubeBlock(bool copy = false) { var ob = (MyObjectBuilder_ProductionBlock)base.GetObjectBuilderCubeBlock(copy); ob.InputInventory = InputInventory.GetObjectBuilder(); ob.OutputInventory = OutputInventory.GetObjectBuilder(); ob.UseConveyorSystem = m_useConveyorSystem; ob.NextItemId = m_nextItemId; if (m_queue.Count > 0) { ob.Queue = new MyObjectBuilder_ProductionBlock.QueueItem[m_queue.Count]; for (int i = 0; i < m_queue.Count; ++i) { ob.Queue[i].Id = m_queue[i].Blueprint.Id; ob.Queue[i].Amount = m_queue[i].Amount; ob.Queue[i].ItemId = m_queue[i].ItemId; } } else { ob.Queue = null; } return(ob); }
public override void Init(MyObjectBuilder_CubeBlock objectBuilder, MyCubeGrid cubeGrid) { base.Init(objectBuilder, cubeGrid); MyDebug.AssertDebug(BlockDefinition is MyRefineryDefinition); m_refineryDef = BlockDefinition as MyRefineryDefinition; InputInventory.Constraint = m_refineryDef.InputInventoryConstraint; bool removed = InputInventory.FilterItemsUsingConstraint(); Debug.Assert(!removed, "Inventory filter removed items which were present in the object builder."); InputInventory.ContentsChanged += inventory_OnContentsChanged; OutputInventory.Constraint = m_refineryDef.OutputInventoryConstraint; removed = OutputInventory.FilterItemsUsingConstraint(); Debug.Assert(!removed, "Inventory filter removed items which were present in the object builder."); OutputInventory.ContentsChanged += inventory_OnContentsChanged; m_queueNeedsRebuild = true; UpgradeValues.Add("Productivity", 0f); UpgradeValues.Add("Effectiveness", 1f); UpgradeValues.Add("PowerEfficiency", 1f); ResourceSink.RequiredInputChanged += PowerReceiver_RequiredInputChanged; OnUpgradeValuesChanged += UpdateDetailedInfo; UpdateDetailedInfo(); }
private void ChangeRequirementsToResults(MyBlueprintDefinitionBase queueItem, MyFixedPoint blueprintAmount) { Debug.Assert(Sync.IsServer); if (!MySession.Static.CreativeMode) { blueprintAmount = MyFixedPoint.Min(OutputInventory.ComputeAmountThatFits(queueItem), blueprintAmount); } if (blueprintAmount == 0) { return; } foreach (var prerequisite in queueItem.Prerequisites) { var obPrerequisite = (MyObjectBuilder_PhysicalObject)Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.CreateNewObject(prerequisite.Id); var prerequisiteAmount = blueprintAmount * prerequisite.Amount; InputInventory.RemoveItemsOfType(prerequisiteAmount, obPrerequisite); } foreach (var result in queueItem.Results) { var resultId = result.Id; var obResult = (MyObjectBuilder_PhysicalObject)Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.CreateNewObject(resultId); var resultAmount = blueprintAmount * result.Amount * m_refineryDef.MaterialEfficiency * UpgradeValues["Effectiveness"]; OutputInventory.AddItems(resultAmount, obResult); } RemoveFirstQueueItemAnnounce(blueprintAmount); }
private void ChangeRequirementsToResults(MyBlueprintDefinitionBase queueItem, MyFixedPoint blueprintAmount) { Debug.Assert(Sync.IsServer); Debug.Assert(m_refineryDef != null, "m_refineryDef shouldn't be null!!!"); if (Sync.IsServer == false) { return; } if (MySession.Static == null || queueItem == null || queueItem.Prerequisites == null || OutputInventory == null || InputInventory == null || queueItem.Results == null) { return; } if (!MySession.Static.CreativeMode) { blueprintAmount = MyFixedPoint.Min(OutputInventory.ComputeAmountThatFits(queueItem), blueprintAmount); } if (blueprintAmount == 0) { return; } foreach (var prerequisite in queueItem.Prerequisites) { var obPrerequisite = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(prerequisite.Id); if (obPrerequisite == null) { Debug.Fail("obPrerequisite shouldn't be null!!!"); continue; } var prerequisiteAmount = blueprintAmount * prerequisite.Amount; InputInventory.RemoveItemsOfType(prerequisiteAmount, obPrerequisite); } foreach (var result in queueItem.Results) { var resultId = result.Id; var obResult = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(resultId); if (obResult == null) { Debug.Fail("obResult shouldn't be null!!!"); continue; } var conversionRatio = result.Amount * m_refineryDef.MaterialEfficiency * UpgradeValues["Effectiveness"]; if (conversionRatio > (MyFixedPoint)1.0f) { conversionRatio = (MyFixedPoint)1.0f; } var resultAmount = blueprintAmount * conversionRatio; OutputInventory.AddItems(resultAmount, obResult); } RemoveFirstQueueItemAnnounce(blueprintAmount); }
public void pickUpJobCall(Item itemToBeDelivered, IInventory minerInventory) { int amount = itemToBeDelivered.getAmount(); if (amount > minerInventory.getLeftOverInventorySpace()) { amount = minerInventory.getLeftOverInventorySpace(); } OutputInventory.putItem(itemToBeDelivered, minerInventory, amount); }
/// <param name="unloading">Specify true if the plugin is unloading.</param> public void Destroy(bool destroyOutputContainer, bool unloading = false) { resetDespawnTimer.DestroyToPool(); foreach (var player in NearbyPlayers) { OnPlayerLeave(player); } if (!unloading) { // Drop queue items if (CraftingTasks.Count > 0) { var container = new ItemContainer(); container.ServerInitialize(null, 36); foreach (var task in CraftingTasks) { foreach (var ingredient in task.Blueprint.ingredients) { var item = ItemManager.CreateByItemID(ingredient.itemid, (int)ingredient.amount * task.Amount); if (!item.MoveToContainer(container)) { item.Drop(Position + Recycler.transform.up * 1.25f, Recycler.GetDropVelocity(), Recycler.ServerRotation); } } } var droppedContainer = container.Drop(Constants.ItemDropPrefab, Position + Recycler.transform.up * 1.25f, Recycler.ServerRotation); droppedContainer.playerName = Lang.Translate(null, "queue-items"); } } Recycler.Kill(); CodeLock?.Kill(); if (!outputContainer.IsDestroyed) { // Remove rock from output container that keeps it from despawning when emptied outputInventory.GetSlot(outputInventory.capacity - 1).Remove(); // Force kill output bag if there's nothing in it. if (!destroyOutputContainer && OutputInventory.AnyItems()) { // Enable physics on output container outputContainer.GetComponent <Rigidbody>().isKinematic = false; } else { outputContainer.Kill(); } } }
protected void InitializeInventoryCounts(bool inputInventory = true) { m_tmpInventoryCounts.Clear(); foreach (var item in inputInventory ? InputInventory.GetItems() : OutputInventory.GetItems()) { MyFixedPoint count = 0; MyDefinitionId itemId = new MyDefinitionId(item.Content.TypeId, item.Content.SubtypeId); m_tmpInventoryCounts.TryGetValue(itemId, out count); m_tmpInventoryCounts[itemId] = count + item.Amount; } }
private void SmeltItem() { if (checkInputInventory()) { foreach (var inputItem in getActualInputItems()) { InputInventory.RemoveItem(inputItem); } foreach (var outputItem in getOutputItems()) { OutputInventory.AddItem(Item.CreateItem(outputItem, outputItem.getAmount())); } } }
public override void UpdateBeforeSimulation100() { base.UpdateBeforeSimulation100(); if (Sync.IsServer && IsWorking && m_useConveyorSystem) { if (InputInventory.VolumeFillFactor < 0.6f) { MyGridConveyorSystem.PullAllRequest(this, InputInventory, OwnerId, InputInventory.Constraint); } if (OutputInventory.VolumeFillFactor > 0.75f) { Debug.Assert(OutputInventory.GetItems().Count > 0); MyGridConveyorSystem.PushAnyRequest(this, OutputInventory, OwnerId); } } }
private void FinishAssembling(MyBlueprintDefinitionBase blueprint) { var amountMult = (MyFixedPoint)(1.0f / MySession.Static.AssemblerEfficiencyMultiplier); for (int i = 0; i < blueprint.Prerequisites.Length; ++i) { var item = blueprint.Prerequisites[i]; InputInventory.RemoveItemsOfType(item.Amount * amountMult, item.Id); } foreach (var res in blueprint.Results) { MyObjectBuilder_PhysicalObject resOb = (MyObjectBuilder_PhysicalObject)Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.CreateNewObject(res.Id.TypeId, res.Id.SubtypeName); OutputInventory.AddItems(res.Amount, resOb); } }
public override void Init(MyObjectBuilder_CubeBlock objectBuilder, MyCubeGrid cubeGrid) { // Need to be initialized before base.Init because when loading world with producting refinery // it will be missing when recompute power and cause disappearing of refinery. UpgradeValues.Add("Productivity", 0f); UpgradeValues.Add("Effectiveness", 1f); UpgradeValues.Add("PowerEfficiency", 1f); base.Init(objectBuilder, cubeGrid); MyDebug.AssertDebug(BlockDefinition is MyRefineryDefinition); m_refineryDef = BlockDefinition as MyRefineryDefinition; if (InventoryAggregate.InventoryCount > 2) { Debug.Fail("Inventory aggregate has to many inventories, probably wrong save. If you continue the unused inventories will be removed. Save the world to correct it. Please report this is if problem prevail."); FixInputOutputInventories(m_refineryDef.InputInventoryConstraint, m_refineryDef.OutputInventoryConstraint); } InputInventory.Constraint = m_refineryDef.InputInventoryConstraint; bool removed = InputInventory.FilterItemsUsingConstraint(); Debug.Assert(!removed, "Inventory filter removed items which were present in the object builder."); OutputInventory.Constraint = m_refineryDef.OutputInventoryConstraint; removed = OutputInventory.FilterItemsUsingConstraint(); Debug.Assert(!removed, "Inventory filter removed items which were present in the object builder."); m_queueNeedsRebuild = true; m_baseIdleSound = BlockDefinition.PrimarySound; m_processSound = BlockDefinition.ActionSound; ResourceSink.RequiredInputChanged += PowerReceiver_RequiredInputChanged; OnUpgradeValuesChanged += UpdateDetailedInfo; UpdateDetailedInfo(); NeedsUpdate |= VRage.ModAPI.MyEntityUpdateEnum.EACH_100TH_FRAME; }
private void FinishDisassembling(MyBlueprintDefinitionBase blueprint) { if (RepeatEnabled && Sync.IsServer) { OutputInventory.ContentsChanged -= OutputInventory_ContentsChanged; } foreach (var res in blueprint.Results) { OutputInventory.RemoveItemsOfType(res.Amount, res.Id); } if (RepeatEnabled && Sync.IsServer) { OutputInventory.ContentsChanged += OutputInventory_ContentsChanged; } var amountMult = (MyFixedPoint)(1.0f / MySession.Static.AssemblerEfficiencyMultiplier); for (int i = 0; i < blueprint.Prerequisites.Length; ++i) { var item = blueprint.Prerequisites[i]; var itemOb = (MyObjectBuilder_PhysicalObject)Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.CreateNewObject(item.Id.TypeId, item.Id.SubtypeName); InputInventory.AddItems(item.Amount * amountMult, itemOb); } }
public void takeItem(Item item, IInventory minerInventory, int?amount = null) { OutputInventory.TakeItem(item, minerInventory, amount); }
public override void UpdateBeforeSimulation100() { base.UpdateBeforeSimulation100(); if (m_inventoryOwnersDirty) { GetCoveyorInventoryOwners(); } if (Sync.IsServer && IsWorking && m_useConveyorSystem) { if (DisassembleEnabled) // Dissasembling { if (OutputInventory.VolumeFillFactor < 0.99f) { //MyGridConveyorSystem.PullAllRequest(this, OutputInventory, OwnerId, OutputInventory.Constraint); var item = TryGetFirstQueueItem(); if (item != null) { if (!OutputInventory.ContainItems(null, item.Value.Blueprint.Results[0].Id)) { MyGridConveyorSystem.ItemPullRequest(this, OutputInventory, OwnerId, item.Value.Blueprint.Results[0].Id, item.Value.Amount); } } } if (InputInventory.VolumeFillFactor > 0.75f) { Debug.Assert(InputInventory.GetItems().Count > 0); MyGridConveyorSystem.PushAnyRequest(this, InputInventory, OwnerId); } } else // Assembling { if (IsSlave && m_queue.Count < 1 && MyFakes.ENABLE_ASSEMBLER_COOPERATION && !RepeatEnabled) { GetItemFromOtherAssemblers(); } if (InputInventory.VolumeFillFactor < 0.99f) { var next = false; int i = 0; var time = 0f; do { var item = TryGetQueueItem(i); if (item.HasValue) { var factor = MySession.Static.AssemblerSpeedMultiplier / MySession.Static.AssemblerEfficiencyMultiplier; var itemAmount = 1; var remainingTime = TIME_IN_ADVANCE - time; if (item.Value.Blueprint.BaseProductionTimeInSeconds < remainingTime) { itemAmount = Math.Min((int)item.Value.Amount, Convert.ToInt32(Math.Floor(remainingTime / (item.Value.Blueprint.BaseProductionTimeInSeconds / factor)))); time += itemAmount * item.Value.Blueprint.BaseProductionTimeInSeconds / MySession.Static.AssemblerSpeedMultiplier; if (time < TIME_IN_ADVANCE) { next = true; } } foreach (var component in item.Value.Blueprint.Prerequisites) { var availableAmount = InputInventory.GetItemAmount(component.Id); if (i > 0) { availableAmount = 0; } var neededAmount = component.Amount * itemAmount - availableAmount; if (neededAmount <= 0) { continue; } MyGridConveyorSystem.ItemPullRequest(this, InputInventory, OwnerId, component.Id, neededAmount); } } if (i > 0) { next = false; } i++; } while (next); } if (OutputInventory.VolumeFillFactor > 0.75f) { Debug.Assert(OutputInventory.GetItems().Count > 0); MyGridConveyorSystem.PushAnyRequest(this, OutputInventory, OwnerId); } } } }
private void DisassembleAllInOutput() { ClearQueue(sendEvent: false); var items = OutputInventory.GetItems(); var toAdd = new List <Tuple <MyBlueprintDefinitionBase, MyFixedPoint> >(); bool add = true; foreach (var item in items) { var blueprint = MyDefinitionManager.Static.TryGetBlueprintDefinitionByResultId(item.Content.GetId()); if (blueprint != null) { var tuple = Tuple.Create(blueprint, item.Amount); toAdd.Add(tuple); } else { add = false; toAdd.Clear(); break; } } if (add) { foreach (var bp in toAdd) { InsertQueueItemRequest(-1, bp.Item1, bp.Item2); } return; } InitializeInventoryCounts(inputInventory: false); MyFixedPoint disassembleAmount, remainingAmount; for (int i = 0; i < m_assemblerDef.BlueprintClasses.Count; ++i) { foreach (var blueprint in m_assemblerDef.BlueprintClasses[i]) { disassembleAmount = MyFixedPoint.MaxValue; foreach (var result in blueprint.Results) { remainingAmount = 0; m_tmpInventoryCounts.TryGetValue(result.Id, out remainingAmount); if (remainingAmount == 0) { disassembleAmount = 0; break; } disassembleAmount = MyFixedPoint.Min((MyFixedPoint)((double)remainingAmount / (double)result.Amount), disassembleAmount); } if (blueprint.Atomic) { disassembleAmount = MyFixedPoint.Floor(disassembleAmount); } if (disassembleAmount > 0) { InsertQueueItemRequest(-1, blueprint, disassembleAmount); foreach (var result in blueprint.Results) { m_tmpInventoryCounts.TryGetValue(result.Id, out remainingAmount); remainingAmount -= result.Amount * disassembleAmount; Debug.Assert(remainingAmount >= 0); if (remainingAmount == 0) { m_tmpInventoryCounts.Remove(result.Id); } else { m_tmpInventoryCounts[result.Id] = remainingAmount; } } } } } m_tmpInventoryCounts.Clear(); }
private void UpdateInventoryFlags() { OutputInventory.SetFlags(DisassembleEnabled ? MyInventoryFlags.CanReceive : MyInventoryFlags.CanSend); InputInventory.SetFlags(DisassembleEnabled ? MyInventoryFlags.CanSend : MyInventoryFlags.CanReceive); }
public override void Init(MyObjectBuilder_CubeBlock objectBuilder, MyCubeGrid cubeGrid) { SyncFlag = true; base.Init(objectBuilder, cubeGrid); m_inputInventory = new MyInventory( ProductionBlockDefinition.InventoryMaxVolume, ProductionBlockDefinition.InventorySize, MyInventoryFlags.CanReceive, this); m_outputInventory = new MyInventory( ProductionBlockDefinition.InventoryMaxVolume, ProductionBlockDefinition.InventorySize, MyInventoryFlags.CanSend, this); var ob = (MyObjectBuilder_ProductionBlock)objectBuilder; if (ob.InputInventory != null) { InputInventory.Init(ob.InputInventory); } if (ob.OutputInventory != null) { OutputInventory.Init(ob.OutputInventory); } m_nextItemId = ob.NextItemId; bool nextIdWasZero = m_nextItemId == 0; base.IsWorkingChanged += CubeBlock_IsWorkingChanged; PowerReceiver = new MyPowerReceiver( MyConsumerGroupEnum.Factory, false, ProductionBlockDefinition.OperationalPowerConsumption, ComputeRequiredPower); PowerReceiver.IsPoweredChanged += Receiver_IsPoweredChanged; PowerReceiver.Update(); AddDebugRenderComponent(new Components.MyDebugRenderComponentDrawPowerReciever(PowerReceiver, 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); MySandboxGame.Log.WriteLine(string.Format("Could not add item into production block's queue: Blueprint {0} was not found.", item.Id)); } } UpdatePower(); } m_useConveyorSystem = ob.UseConveyorSystem; m_lastUpdateTime = MySandboxGame.TotalGamePlayTimeInMilliseconds; }
public override void UpdateBeforeSimulation100() { base.UpdateBeforeSimulation100(); if (m_inventoryOwnersDirty) { GetCoveyorInventoryOwners(); } if (Sync.IsServer && IsWorking && m_useConveyorSystem) { if (DisassembleEnabled) // Dissasembling { if (OutputInventory.VolumeFillFactor < 0.99f) { //MyGridConveyorSystem.PullAllRequest(this, OutputInventory, OwnerId, OutputInventory.Constraint); var item = TryGetFirstQueueItem(); if (item != null) { if (!OutputInventory.ContainItems(null, item.Value.Blueprint.Results[0].Id)) { MyGridConveyorSystem.ItemPullRequest(this, OutputInventory, OwnerId, item.Value.Blueprint.Results[0].Id, item.Value.Amount); } } } if (InputInventory.VolumeFillFactor > 0.75f) { Debug.Assert(InputInventory.GetItems().Count > 0); MyGridConveyorSystem.PushAnyRequest(this, InputInventory, OwnerId); } } else // Assembling { //if (IsSlave && m_queue.Count < 1 && MyFakes.ENABLE_ASSEMBLER_COOPERATION && !RepeatEnabled) //{ // GetItemFromOtherAssemblers(TIME_IN_ADVANCE); //} if (InputInventory.VolumeFillFactor < 0.99f) { m_requiredComponents.Clear(); var next = false; int i = 0; var time = 0f; do { var item = TryGetQueueItem(i); var remainingTime = TIME_IN_ADVANCE - time; if (item.HasValue) { var productivity = (((MyAssemblerDefinition)BlockDefinition).AssemblySpeed + UpgradeValues["Productivity"]); var factor = MySession.Static.AssemblerSpeedMultiplier * productivity; var itemAmount = 1; if (item.Value.Blueprint.BaseProductionTimeInSeconds / factor < remainingTime) { itemAmount = Math.Min((int)item.Value.Amount, Convert.ToInt32(Math.Ceiling(remainingTime / (item.Value.Blueprint.BaseProductionTimeInSeconds / factor)))); } time += itemAmount * item.Value.Blueprint.BaseProductionTimeInSeconds / factor; if (time < TIME_IN_ADVANCE) { next = true; } var amountMult = (MyFixedPoint)(1.0f / MySession.Static.AssemblerEfficiencyMultiplier); foreach (var component in item.Value.Blueprint.Prerequisites) { var requiredAmount = component.Amount * itemAmount * amountMult; bool found = false; for (int j = 0; j < m_requiredComponents.Count; j++) { if (m_requiredComponents[j].Id == component.Id) { m_requiredComponents[j] = new MyBlueprintDefinitionBase.Item { Amount = m_requiredComponents[j].Amount + requiredAmount, Id = component.Id }; found = true; break; } } if (!found) { m_requiredComponents.Add(new MyBlueprintDefinitionBase.Item { Amount = requiredAmount, Id = component.Id }); } } } i++; if (i >= m_queue.Count) { next = false; } } while (next); foreach (var component in m_requiredComponents) { var availableAmount = InputInventory.GetItemAmount(component.Id); var neededAmount = component.Amount - availableAmount; if (neededAmount <= 0) { continue; } MyGridConveyorSystem.ItemPullRequest(this, InputInventory, OwnerId, component.Id, neededAmount); } if (IsSlave && !RepeatEnabled) { var remainingTime = TIME_IN_ADVANCE - time; if (remainingTime > 0) { GetItemFromOtherAssemblers(remainingTime); } } } if (OutputInventory.VolumeFillFactor > 0.75f) { Debug.Assert(OutputInventory.GetItems().Count > 0); MyGridConveyorSystem.PushAnyRequest(this, OutputInventory, OwnerId); } } } }