private bool moveItem(IMyInventory src, IMyInventory tgt, MyInventoryItem item, MyFixedPoint amt) { bool ret = false; IMyRefinery prod1 = src as IMyRefinery; IMyRefinery prod2 = tgt as IMyRefinery; if (prod1 != null) { prod1.UseConveyorSystem = true; } if (prod2 != null) { prod2.UseConveyorSystem = true; } if (src.TransferItemTo(tgt, item, amt)) { //show("Moved "+amt+" of "+item.Type.SubtypeId+" from "+src.Owner.Name+" to "+tgt.Owner.Name); ret = true; } else { //show("Could not move "+item.Type.SubtypeId+" from "+src.Owner.Name+" to "+tgt.Owner.Name); ret = false; } if (prod1 != null) { prod1.UseConveyorSystem = false; } if (prod2 != null) { prod2.UseConveyorSystem = false; } return(ret); }
/* * private void tallyItems() { * maxCapacity = 0; * usedVolume = 0; * foreach (IMyCargoContainer box in cargo) { * IMyInventory inv = box.GetInventory(0); * maxCapacity += inv.MaxVolume.ToIntSafe(); * usedVolume += inv.CurrentVolume.ToIntSafe(); * List<MyInventoryItem> li = new List<MyInventoryItem>(); * inv.GetItems(li); * foreach (MyInventoryItem ii in li) { * int amt = 0; * string type = ii.Type.ToString(); * allItems.TryGetValue(type, out amt); * int has = ii.Amount.ToIntSafe(); * amt += has; * allItems[type] = amt; * totalItems += has; * } * } * } * * private void balanceReactors() { * int each = 0; * allItems.TryGetValue("uranium", out each); * each /= reactors.Count; * List<IMyReactor> overfilled = new List<IMyReactor>(); * } * * private string getIngot(string ore) { * * }*/ private bool empty(IMyInventory src, bool allowSmall = true) { List <MyInventoryItem> li = new List <MyInventoryItem>(); src.GetItems(li); foreach (MyInventoryItem item in li) { foreach (IMyCargoContainer box in cargo) { if (!allowSmall && box.BlockDefinition.SubtypeName.ToLowerInvariant().Contains("small")) { continue; } if (!isItemValidForContainer(item.Type.TypeId, item.Type.SubtypeId, box.CustomName)) { continue; } IMyInventory tgt = box.GetInventory(); if (moveItem(src, tgt, item)) { break; } } } return(src.ItemCount == 0); }
private FoundItem findItem(ItemProfile item) { List <IMyCargoContainer> cache = null; sourceCache.TryGetValue(item, out cache); if (cache == null || cache.Count == 0) { return(null); } foreach (IMyCargoContainer box in cache) { IMyInventory inv = box.GetInventory(); List <MyInventoryItem> li = new List <MyInventoryItem>(); //show("Found "+li.ToString()+" in "+box.CustomName+" for "+item.ToString()); inv.GetItems(li, item.match); if (li.Count > 0) { return(new FoundItem(li[0], inv)); } } return(null); }
internal FoundItem(MyInventoryItem inv, IMyInventory src) { item = inv; source = src; }
internal bool hasWork() { IMyInventory inv = refinery.InputInventory; return(inv.ItemCount > 0); }
private bool moveItem(IMyInventory src, IMyInventory tgt, MyInventoryItem item) { return(moveItem(src, tgt, item, item.Amount)); }
public void Main() //called each cycle //tick++; //if (tick%4 != 0) // return; { tick++; bool cancel = false; foreach (IMyShipConnector conn in connectors) { if (conn.Enabled && conn.Status == MyShipConnectorStatus.Connected) { if (shutdownWhenDockedTo(conn.DisplayName, conn.OtherConnector.DisplayName, conn.OtherConnector.CubeGrid.DisplayName)) { cancel = true; break; } } } if (cancel) { Echo("Docked to dominant grid; not managing inventories."); return; } Echo("Managing " + refineryCount + " refineries, " + assemblers.Count + " assemblers, " + oxyGenerators.Count + " O2 gens, and " + cargo.Count + " cargo containers."); if (tick % 50 == 0) { cacheSources(); } if (ENABLE_ORE_SORTING) { foreach (var entry in refineries) { Refinery r = getRandom <Refinery>(entry.Value); if (r != null && (r.refinery.Enabled || !SKIP_OFFLINE_REFINERIES)) { ICollection <string> li = r.validOres; if (ORE_PRIORITY.Length > 0) { li = applyPriorityRules(li); } foreach (string ore in li) { if (isOreValid(ore, r)) { tryMoveOre(ore, r); } } empty(r.refinery.OutputInventory); if (!SKIP_OFFLINE_REFINERIES) { r.refinery.Enabled = r.hasWork(); } } } IMyAssembler ass = getRandom <IMyAssembler>(assemblers); if (ass != null) { empty(ass.Mode == MyAssemblerMode.Disassembly ? ass.InputInventory : ass.OutputInventory); List <MyProductionItem> li = new List <MyProductionItem>(); ass.GetQueue(li); ass.Enabled = li.Count > 0 || ass.CooperativeMode || ass.BlockDefinition.SubtypeName.ToLowerInvariant().Contains("survivalkit"); } IMyGasGenerator gas = getRandom <IMyGasGenerator>(oxyGenerators); if (gas != null && (gas.Enabled || ENABLE_O2_GENS)) { tryMoveIce(gas); if (ENABLE_O2_GENS) { gas.Enabled = true; // || gas.GetInventory().ItemCount > 0; } } } if (MOVE_FROM_SMALL_TO_LARGE) { IMyCargoContainer box = getRandom <IMyCargoContainer>(cargo); if (box != null) { if (box.GetInventory().ItemCount > 0 && box.BlockDefinition.SubtypeName.ToLowerInvariant().Contains("small")) { IMyInventory inv = box.GetInventory(); empty(inv, false); //break; } } } if (tick % 5 == 0) { bool flag = false; if (ejectionWatchers.Count > 0 && usedVolume / (float)maxCapacity >= EJECTION_THRESHOLD) { foreach (ItemProfile p in ejectionWatchers) { float f = getItemFraction(p); Echo("Item type " + p.ToString() + " represents " + f * 100 + "% of items."); if (f >= EJECTION_THRESHOLD) { Echo("Ejecting excess."); tryPrepareEjection(p); flag = true; } } } if (!flag) { Echo("No excess to eject."); foreach (IMyShipConnector conn in ejectors) { conn.ThrowOut = false; } } /* * foreach (IMyShipConnector con in connectors) { * if (con.Status == MyShipConnectorStatus.Connected) { * FlowDirection flow = getActiveFlowDirection(con.CustomName, con.OtherConnector.CustomName, con.OtherConnector.CubeGrid.CustomName); * if (flow != FlowDirection.INERT) { * * } * } * }*/ } }