Advertisement
klassekatze

FuelBoss

Jul 25th, 2023 (edited)
1,801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.31 KB | None | 0 0
  1. /*
  2.  * R e a d m e
  3.  * -----------
  4.  *
  5.  * Like ammo puller, but fuel tank puller, but no wasting, k
  6.  */
  7.  
  8. float fuel_target = 0.25f;//percentage of total overall hydro tank capacity that should be filled to
  9.  
  10. public Program()
  11. {
  12.     Runtime.UpdateFrequency = UpdateFrequency.Update100;
  13. }
  14.  
  15. public void Save()
  16. {
  17. }
  18.  
  19. public bool isThis(IMyTerminalBlock b)
  20. {
  21.     return b.OwnerId == Me.OwnerId && b.CubeGrid == Me.CubeGrid;
  22. }
  23.  
  24. List<IMyGasGenerator> extractors = new List<IMyGasGenerator>();
  25. List<IMyCargoContainer> cargos = new List<IMyCargoContainer>();
  26. List<IMyShipConnector> connectors = new List<IMyShipConnector>();
  27. List<IMyGasTank> tanks_hydrogen = new List<IMyGasTank>();
  28.  
  29. //600000L per fuel tank
  30. //3675L per jerry can//3725
  31.  
  32. Dictionary<string, float> tankCap = new Dictionary<string, float> { { "MyObjectBuilder_Component/Fuel_Tank", 600000 }, { "MyObjectBuilder_Component/SG_Fuel_Tank", 4000 } };
  33.  
  34. AggregateInventoryInterface invInterface = new AggregateInventoryInterface();
  35. bool ld = false;
  36. public void loadBlocks()
  37. {
  38.     ld = true;
  39.     extractors.Clear();
  40.     GridTerminalSystem.GetBlocksOfType(extractors, b => b.OwnerId == Me.OwnerId && b.CubeGrid == Me.CubeGrid && (b.DefinitionDisplayNameText == "Extractor" || b.DefinitionDisplayNameText == "Small Fuel Extractor"));
  41.     cargos.Clear();
  42.     GridTerminalSystem.GetBlocksOfType(cargos, isThis);
  43.     connectors.Clear();
  44.     GridTerminalSystem.GetBlocksOfType(connectors, isThis);
  45.     tanks_hydrogen.Clear();
  46.     GridTerminalSystem.GetBlocksOfType(tanks_hydrogen, b => b.OwnerId == Me.OwnerId && b.CubeGrid == Me.CubeGrid && b.DefinitionDisplayNameText.Contains("Hydrogen"));
  47.     List<IMyTerminalBlock> tmp = new List<IMyTerminalBlock>();
  48.     tmp.AddArray(cargos.ToArray());
  49.     tmp.AddArray(connectors.ToArray());
  50.     invInterface.setContainers(tmp);
  51.     invInterface.updateInterval = 300;
  52. }
  53. void upd()
  54. {
  55.     string txt = ""+ld+"\n";
  56.     double total_cap = 0;
  57.     double current_avail = 0;
  58.     foreach (var t in tanks_hydrogen)
  59.     {
  60.         total_cap += t.Capacity;
  61.         current_avail += t.Capacity * t.FilledRatio;
  62.     }
  63.     if (total_cap < MathHelper.EPSILON) return;
  64.     float cur_fil_ratio = (float)(current_avail / total_cap);
  65.     txt += current_avail + "/" + total_cap + "\n";
  66.     txt += cur_fil_ratio + "%\n";
  67.     if (cur_fil_ratio < fuel_target || current_avail < MathHelper.EPSILON)
  68.     {
  69.         txt += "seek fuel\n";
  70.         while (extractors.Count > 0 && (!extractors[0].IsFunctional || extractors[0].Closed)) extractors.RemoveAt(0);
  71.         if (extractors.Count > 0)
  72.         {
  73.             invInterface.update(false, 100);
  74.             var extractor = extractors[0];
  75.             txt += "found extractor\n";
  76.             var einv = extractor.GetInventory(0);
  77.             List<MyItemType> ai = new List<MyItemType>();
  78.             einv.GetAcceptedItems(ai);
  79.             txt += "accepts:"+ai.Count+"\n";
  80.             for (var i = 0; i < ai.Count; i++)
  81.             {
  82.                 var tt = ai[i];
  83.                 var ttc = tankCap.ContainsKey(tt.TypeId) ? tankCap[tt.TypeId] : 600000;
  84.                 if (total_cap - current_avail > ttc || current_avail < MathHelper.EPSILON)
  85.                 {
  86.                     txt += "a\n";
  87.                     if (!einv.ContainItems(1, tt))
  88.                     {
  89.                         txt += "b\n";
  90.                         int mvd = invInterface.TransferItemTo(tt, 1, einv);
  91.                         if (mvd == 0)
  92.                         {
  93.                             txt += "c\n";
  94.                             break;
  95.                         }
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.         //are we LG or SG
  101.         //MyObjectBuilder_Component/Fuel_Tank
  102.         //MyObjectBuilder_Component/SG_Fuel_Tank
  103.         //MyObjectBuilder_Ingot/FusionFuel
  104.     }
  105.     //Me.GetSurface(0).WriteText(txt);
  106. }
  107.  
  108.  
  109. int refreshBlocks = int.MinValue;
  110. int tick = 0;
  111. public void Main(string argument, UpdateType updateSource)
  112. {
  113.     tick += 100;
  114.     if(refreshBlocks+15000 < tick)
  115.     {
  116.         refreshBlocks = tick;
  117.         loadBlocks();
  118.     }
  119.     upd();
  120. }
  121.  
  122.  
  123. //can generate an inventory from a bunch of inventories, and can simplify programmatically sending stuff between such groupings.
  124. //the most immediate use here is automatically unstuffing grinders, stuffing torpedo launchers and other weapons, and sending excess garbage to connectors for ejection from the ship.
  125. //this interface doesn't recognize partial items, only whole units / integers.
  126. class AggregateInventoryInterface
  127. {
  128.     int tickOffset = 0;
  129.     static int offseti = 0;
  130.     public AggregateInventoryInterface()
  131.     {
  132.         tickOffset = offseti;
  133.         offseti += 10;
  134.     }
  135.     /*public void setContainers(List<IMyTerminalBlock> c)
  136.             {
  137.                 containers = c;
  138.             }*/
  139.     public void setContainers<T>(List<T> c) where T : IMyTerminalBlock
  140.     {
  141.         containers.Clear();
  142.         foreach (T i in c)
  143.         {
  144.             containers.Add(i);
  145.         }
  146.     }
  147.     List<IMyTerminalBlock> containers = new List<IMyTerminalBlock>();
  148.     public Dictionary<MyItemType, int> items = new Dictionary<MyItemType, int>();
  149.  
  150.     public int updateInterval = 60 * 3;
  151.     int lastUpdateTick = 0;
  152.     int tick = 0;
  153.     //if called every tick will update the inventory manifest every 3 seconds.
  154.     //may not be necessary - a force update is best before doing anything important.
  155.     public void update(bool force = false, int ticks = 1)
  156.     {
  157.         tick += ticks;
  158.         if (tick + tickOffset - lastUpdateTick > updateInterval || force)
  159.         {
  160.             lastUpdateTick = tick;
  161.             items.Clear();
  162.             foreach (IMyTerminalBlock t in containers)
  163.             {
  164.                 for (int i = 0; i < t.InventoryCount; i++)
  165.                 {
  166.                     var inv = t.GetInventory(i);
  167.                     List<MyInventoryItem> t_items = new List<MyInventoryItem>();
  168.                     inv.GetItems(t_items);
  169.                     foreach (MyInventoryItem item in t_items)
  170.                     {
  171.                         if (!items.ContainsKey(item.Type)) items[item.Type] = (int)Math.Floor((double)item.Amount);
  172.                         else items[item.Type] += (int)Math.Floor((double)item.Amount);
  173.                     }
  174.                 }
  175.             }
  176.         }
  177.     }
  178.     //these return the amount of items that could not be sent (unavailable, no room, whatever). Ergo, 0 means all were transferred.
  179.     public int TransferItemTo(MyItemType type, int amount_to_transfer, IMyInventory destination)
  180.     {
  181.         float unit_volume = type.GetItemInfo().Volume;
  182.         foreach (IMyTerminalBlock t in containers)
  183.         {
  184.             for (int i = 0; i < t.InventoryCount; i++)
  185.             {
  186.                 var inv = t.GetInventory(i);
  187.                 List<MyInventoryItem> t_items = new List<MyInventoryItem>();
  188.                 inv.GetItems(t_items);
  189.                 foreach (MyInventoryItem item in t_items)
  190.                 {
  191.                     if (item.Type == type)
  192.                     {
  193.                         int transfer_amt = (int)Math.Floor((double)item.Amount);
  194.  
  195.                         int max_room_for = (int)Math.Floor((double)(destination.MaxVolume - destination.CurrentVolume + (MyFixedPoint)0.001) / unit_volume);
  196.  
  197.                         if (max_room_for < transfer_amt) transfer_amt = max_room_for;
  198.  
  199.                         if (transfer_amt > amount_to_transfer) transfer_amt = amount_to_transfer;
  200.                         //log(">sending " + transfer_amt + " of " + item.Type.SubtypeId);
  201.  
  202.  
  203.                         if (inv.TransferItemTo(destination, item, transfer_amt)) amount_to_transfer -= transfer_amt;
  204.                         if (amount_to_transfer <= 0) return 0;
  205.                     }
  206.                 }
  207.             }
  208.         }
  209.         return amount_to_transfer;
  210.     }
  211.     //these return the amount of items that could not be sent (unavailable, no room, whatever). Ergo, 0 means all were transferred.
  212.     public int TransferItemTo(MyItemType type, int amount, AggregateInventoryInterface destination)
  213.     {
  214.         foreach (IMyTerminalBlock t in destination.containers)
  215.         {
  216.             for (int i = 0; i < t.InventoryCount; i++)
  217.             {
  218.  
  219.                 var inv = t.GetInventory(i);
  220.                 //log("sending " + amount + " of " + type.SubtypeId + " to " + t.DefinitionDisplayNameText);
  221.                 amount = TransferItemTo(type, amount, inv);
  222.                 if (amount <= 0) return amount;
  223.             }
  224.         }
  225.         return amount;
  226.     }
  227.     static string[] common_ammo_identifiers = new string[]
  228.             {
  229.                 "missile",
  230.                 "ammo",
  231.                 "magazine",
  232.                 "torpedo",
  233.                 "slug",
  234.                 "box"
  235.             };
  236.     static Dictionary<string, string> bulkreplace = new Dictionary<string, string>();
  237.     static void initbulk()
  238.     {
  239.         var r = bulkreplace;
  240.         if (r.Count != 0) return;
  241.         r["AngleGrinder"] = "Grinder";
  242.         r["CrateTomato"] = "Crate of Tomatoes";
  243.         r["HeavyArms"] = "Heavy Armaments";
  244.         r["GravityGenerator"] = "Gravity Comp.";
  245.         r["RadioCommunication"] = "Radio-comm Comp.";
  246.         r["Detector"] = "Detector Comp.";
  247.         r["LargeTube"] = "Large Steel Tube";
  248.         r["Construction"] = "Construction Comp.";
  249.     }
  250.     static public string prettyItemName(MyItemType item)
  251.     {
  252.         initbulk();
  253.         string name = item.SubtypeId.Replace("MyObjectBuilder_", "").Replace("_", " ");
  254.         var nfo = item.GetItemInfo();
  255.         foreach (KeyValuePair<string, string> kvp in bulkreplace)
  256.         {
  257.             if (name == kvp.Key)
  258.             {
  259.                 name = kvp.Value;
  260.                 break;
  261.             }
  262.             else if (name.StartsWith(kvp.Key))
  263.             {
  264.                 name = name.Replace(kvp.Key, kvp.Value);
  265.                 break;
  266.             }
  267.         }
  268.         if (nfo.IsAmmo)
  269.         {
  270.             var l = name.ToLower();
  271.             if (name.Length > 20) name = name.Replace("Magazine", "");
  272.             else
  273.             {
  274.  
  275.                 if (name.StartsWith("Missile"))
  276.                 {
  277.                     name = name.Substring("Missile".Length) + " Missile";
  278.                 }
  279.  
  280.                 if (l.IndexOf("magazine") == -1)// && l.IndexOf(' ') == -1)
  281.                 {
  282.  
  283.                 }
  284.             }
  285.             l = name.ToLower();
  286.             bool id = false;
  287.             foreach (var i in common_ammo_identifiers)
  288.             {
  289.                 if (l.IndexOf(i) != -1)
  290.                 {
  291.                     id = true;
  292.                     break;
  293.                 }
  294.             }
  295.             if (!id)
  296.             {
  297.                 name += "Ammo";
  298.             }
  299.         }
  300.         if (nfo.IsTool && name.Length > 5)
  301.         {
  302.  
  303.             string nsub = name.Substring(0, name.Length - 5);
  304.             if (name.EndsWith("1Item")) name = nsub;
  305.             else if (name.EndsWith("2Item")) name = nsub + " (Enhanced)";
  306.             else if (name.EndsWith("3Item")) name = nsub + " (Proficient)";
  307.             else if (name.EndsWith("4Item")) name = nsub + " (Elite)";
  308.         }
  309.         int capcount = 0;
  310.         foreach (char c in name) if (char.IsUpper(c)) capcount += 1;
  311.         if (capcount <= 1)
  312.         {
  313.             if (nfo.IsOre && name != "Stone") return name + " Ore";
  314.             if (nfo.IsIngot)
  315.             {
  316.                 if (name == "Stone") return "Gravel";
  317.                 return name + " Ingot";
  318.             }
  319.             //if (name == "Stone Ingot") name = "Gravel";
  320.         }
  321.         else
  322.         {
  323.             string rename = "";
  324.             // bool didLast = false;
  325.             for (int i = 0; i < name.Length; i++)
  326.             {
  327.                 if (i > 0 && i < name.Length - 1)
  328.                 {
  329.                     bool notlast = true;
  330.                     if (rename.Length > 0) notlast = rename[rename.Length - 1] != ' ';
  331.                     if (name[i - 1] != ' ' && name[i] != ' ' && name[i + 1] != ' ' && notlast)
  332.                     {
  333.                         bool prev = char.IsUpper(name[i - 1]);
  334.                         bool cur = char.IsUpper(name[i]);
  335.                         bool next = char.IsUpper(name[i + 1]);
  336.                         bool nextLetter = char.IsLetter(name[i + 1]);
  337.                         if ((cur && !next && nextLetter && name[i - 1] != '(' && name[i] != ' ') || (!prev && name[i - 1] != '(' && cur && name[i] != ' '))// && !prev)
  338.                         {
  339.                             rename += " ";
  340.                         }
  341.                         if (prev && !char.IsLetter(name[i]) && name[i] != ' ') rename += " ";
  342.                     }
  343.                 }
  344.                 rename += name[i]; ;
  345.             }
  346.             name = rename;
  347.         }
  348.         name = name.Replace(" Adv ", " Advanced ");
  349.         if (nfo.IsAmmo && name.Length > 4)
  350.         {
  351.             if (name.EndsWith("MCRN")) name = name.Substring(0, name.Length - 4) + "(MCRN)";
  352.             if (name.EndsWith("UNN")) name = name.Substring(0, name.Length - 3) + "(UNN)";
  353.         }
  354.  
  355.         return name;
  356.     }
  357.  
  358.     public string listInv(Func<MyItemType,bool> filter = null)
  359.     {
  360.         string r = "";
  361.  
  362.         int nlen = 0;
  363.         Dictionary<string, int> entries = new Dictionary<string, int>();
  364.         foreach (KeyValuePair<MyItemType, int> kvp in items)
  365.         {
  366.             if (filter != null) if (!filter(kvp.Key)) continue;
  367.             entries[prettyItemName(kvp.Key)] = kvp.Value;
  368.         }
  369.         foreach (KeyValuePair<string, int> kvp in entries)if (kvp.Key.Length > nlen) nlen = kvp.Key.Length;
  370.         foreach (KeyValuePair<string, int> kvp in entries)
  371.         {
  372.             string d = kvp.Key;
  373.             if (d.Length < nlen) d += new string('_', nlen - d.Length);
  374.             r += d + ": " + kvp.Value + "\n";
  375.         }
  376.         return r;
  377.     }
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement