public MyFixedPoint GetItemAmountCombined(MyInventoryBase inventory, MyDefinitionId contentId)
        {
            int amount = 0;
            var group  = MyDefinitionManager.Static.GetGroupForComponent(contentId, out amount);

            if (group == null)
            {
                MyComponentSubstitutionDefinition substitutions;
                if (MyDefinitionManager.Static.TryGetComponentSubstitutionDefinition(contentId, out substitutions))
                {
                    foreach (var providingComponent in substitutions.ProvidingComponents)
                    {
                        amount += (int)inventory.GetItemAmount(providingComponent.Key) / providingComponent.Value;
                    }
                }

                return(amount + inventory.GetItemAmount(contentId));
            }
            else
            {
                Clear();
                inventory.CountItems(m_componentCounts);
                AddItem(group.Id, amount, int.MaxValue);
                Solve(m_componentCounts);
                return(GetSolvedItemCount());
            }
        }
Exemplo n.º 2
0
        public MyFixedPoint GetItemAmountCombined(MyInventoryBase inventory, MyDefinitionId contentId)
        {
            int amount = 0;
            var group = MyDefinitionManager.Static.GetGroupForComponent(contentId, out amount);
            if (group == null)
            {
                MyComponentSubstitutionDefinition substitutions;
                if (MyDefinitionManager.Static.TryGetComponentSubstitutionDefinition(contentId, out substitutions))
                {                    
                    foreach (var providingComponent in substitutions.ProvidingComponents)
                    {
                        amount += (int)inventory.GetItemAmount(providingComponent.Key) / providingComponent.Value;
                    }
                    return amount;
                }

                return inventory.GetItemAmount(contentId);
            }
            else
            {
                Clear();
                inventory.CountItems(m_componentCounts);
                AddItem(group.Id, amount, int.MaxValue);
                Solve(m_componentCounts);
                return GetSolvedItemCount();
            }
        }
Exemplo n.º 3
0
 public MyFixedPoint GetItemAmountCombined(MyInventoryBase inventory, MyDefinitionId contentId)
 {
     int amount = 0;
     var group = MyDefinitionManager.Static.GetGroupForComponent(contentId, out amount);
     if (group == null)
     {
         return inventory.GetItemAmount(contentId);
     }
     else
     {
         Clear();
         inventory.CountItems(m_componentCounts);
         AddItem(group.Id, amount, int.MaxValue);
         Solve(m_componentCounts);
         return GetSolvedItemCount();
     }
 }
Exemplo n.º 4
0
        public MyFixedPoint GetItemAmountCombined(MyInventoryBase inventory, MyDefinitionId contentId)
        {
            int amount = 0;
            var group  = MyDefinitionManager.Static.GetGroupForComponent(contentId, out amount);

            if (group == null)
            {
                return(inventory.GetItemAmount(contentId));
            }
            else
            {
                Clear();
                inventory.CountItems(m_componentCounts);
                AddItem(group.Id, amount, int.MaxValue);
                Solve(m_componentCounts);
                return(GetSolvedItemCount());
            }
        }
Exemplo n.º 5
0
        public MyFixedPoint GetItemAmountCombined(MyInventoryBase inventory, MyDefinitionId contentId)
        {
            if (inventory == null)
            {
                return(0);
            }
            int amount = 0;
            MyComponentGroupDefinition groupForComponent = MyDefinitionManager.Static.GetGroupForComponent(contentId, out amount);

            if (groupForComponent == null)
            {
                return(amount + inventory.GetItemAmount(contentId, MyItemFlags.None, true));
            }
            this.Clear();
            inventory.CountItems(m_componentCounts);
            this.AddItem(groupForComponent.Id, amount, 0x7fffffff);
            this.Solve(m_componentCounts);
            return(this.GetSolvedItemCount());
        }
Exemplo n.º 6
0
 public void RemoveItemsCombined(MyInventoryBase inventory, DictionaryReader <MyDefinitionId, int> toRemove)
 {
     this.Clear();
     foreach (KeyValuePair <MyDefinitionId, int> pair in toRemove)
     {
         int amount = 0;
         MyComponentGroupDefinition groupForComponent = MyDefinitionManager.Static.GetGroupForComponent(pair.Key, out amount);
         if (groupForComponent != null)
         {
             this.AddItem(groupForComponent.Id, amount, pair.Value);
             continue;
         }
         if ((MySessionComponentEquivalency.Static != null) && MySessionComponentEquivalency.Static.HasEquivalents(pair.Key))
         {
             HashSet <MyDefinitionId> equivalents = MySessionComponentEquivalency.Static.GetEquivalents(pair.Key);
             if (equivalents == null)
             {
                 continue;
             }
             int num2 = pair.Value;
             foreach (MyDefinitionId id in equivalents)
             {
                 if (num2 <= 0)
                 {
                     break;
                 }
                 num2 -= (int)inventory.RemoveItemsOfType(num2, id, MyItemFlags.None, false);
             }
             continue;
         }
         inventory.RemoveItemsOfType(pair.Value, pair.Key, MyItemFlags.None, false);
     }
     inventory.CountItems(m_componentCounts);
     this.Solve(m_componentCounts);
     inventory.ApplyChanges(this.m_solution);
 }
        public bool CanCombineItems(MyInventoryBase inventory, DictionaryReader <MyDefinitionId, int> items)
        {
            bool result = true;

            Clear();
            inventory.CountItems(m_componentCounts);

            foreach (var item in items)
            {
                int itemValue    = 0;
                int neededAmount = item.Value;

                MyComponentGroupDefinition group = null;

                group = MyDefinitionManager.Static.GetGroupForComponent(item.Key, out itemValue);
                if (group == null)
                {
                    MyFixedPoint itemAmount;
                    // Checking if this component is not provided by the group
                    MyComponentSubstitutionDefinition substitutions;
                    if (MyDefinitionManager.Static.TryGetComponentSubstitutionDefinition(item.Key, out substitutions))
                    {
                        int providedAmount;
                        if (!substitutions.IsProvidedByComponents(m_componentCounts, out providedAmount))
                        {
                            result = false;
                            break;
                        }
                        else if (providedAmount < neededAmount)
                        {
                            result = false;
                            break;
                        }
                    }
                    else if (!m_componentCounts.TryGetValue(item.Key, out itemAmount))
                    {
                        result = false;
                        break;
                    }
                    else if (itemAmount < neededAmount)
                    {
                        result = false;
                        break;
                    }
                }
                else
                {
                    AddItem(group.Id, itemValue, neededAmount);
                }
            }

            if (result)
            {
                result &= Solve(m_componentCounts);
            }

            if (MyDebugDrawSettings.ENABLE_DEBUG_DRAW)
            {
                if (result == false)
                {
                    MyRenderProxy.DebugDrawText2D(new Vector2(0.0f, 0.0f), "Can not build", Color.Red, 1.0f);
                }
                else
                {
                    List <MyComponentChange> solution = null;
                    GetSolution(out solution);

                    float yCoord = 0.0f;
                    foreach (var change in solution)
                    {
                        string text = "";
                        if (change.IsAddition())
                        {
                            text += "+ " + change.Amount.ToString() + "x" + change.ToAdd.ToString();
                            MyRenderProxy.DebugDrawText2D(new Vector2(0.0f, yCoord), text, Color.Green, 1.0f);
                            yCoord += 20.0f;
                        }
                        else if (change.IsRemoval())
                        {
                            text += "- " + change.Amount.ToString() + "x" + change.ToRemove.ToString();
                            MyRenderProxy.DebugDrawText2D(new Vector2(0.0f, yCoord), text, Color.Red, 1.0f);
                            yCoord += 20.0f;
                        }
                        else
                        {
                            text += "- " + change.Amount.ToString() + "x" + change.ToRemove.ToString();
                            MyRenderProxy.DebugDrawText2D(new Vector2(0.0f, yCoord), text, Color.Orange, 1.0f);
                            yCoord += 20.0f;

                            text  = "";
                            text += "+ " + change.Amount.ToString() + "x" + change.ToAdd.ToString();
                            MyRenderProxy.DebugDrawText2D(new Vector2(0.0f, yCoord), text, Color.Orange, 1.0f);
                            yCoord += 20.0f;
                        }
                    }
                }
            }

            return(result);
        }
        public void RemoveItemsCombined(MyInventoryBase inventory, DictionaryReader <MyDefinitionId, int> toRemove)
        {
            Clear();
            foreach (var material in toRemove) // rename material to component
            {
                int groupAmount = 0;
                MyComponentGroupDefinition group = MyDefinitionManager.Static.GetGroupForComponent(material.Key, out groupAmount);

                // The component does not belong to any component group => we are looking exactly for the given component
                if (group == null)
                {
                    MyComponentSubstitutionDefinition substitutionDefinition = null;
                    if (MyDefinitionManager.Static.TryGetComponentSubstitutionDefinition(material.Key, out substitutionDefinition))
                    {
                        int amountToRemove = material.Value;
                        foreach (var entry in substitutionDefinition.ProvidingComponents)
                        {
                            if (amountToRemove > 0)
                            {
                                var removed = inventory.RemoveItemsOfType(amountToRemove * entry.Value, entry.Key);
                                amountToRemove -= (int)removed;
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (amountToRemove > 0)
                        {
                            var removed = inventory.RemoveItemsOfType(amountToRemove, material.Key);
                            amountToRemove -= (int)removed;
                        }
                    }
                    else
                    {
                        inventory.RemoveItemsOfType(material.Value, material.Key);
                        continue;
                    }
                }
                else
                {
                    AddItem(group.Id, groupAmount, material.Value);
                }
            }

            inventory.CountItems(m_componentCounts);
            bool success = Solve(m_componentCounts);

            Debug.Assert(success, "Could not combine required items!");

            inventory.ApplyChanges(m_solution);

            /*CheckUpdate();
             *
             * m_remainder.Clear();
             * foreach (var material in toRemove)
             * {
             *  m_remainder.Add(material.Key, material.Value);
             * }
             *
             * bool success = true;
             *
             * m_cuttingSolver.Clear();
             * foreach (var material in m_remainder)
             * {
             *  int groupAmount = 0;
             *  MyComponentGroupDefinition group = MyDefinitionManager.Static.GetGroupForComponent(material.Key, out groupAmount);
             *
             *  // The component does not belong to any component group => we are looking exactly for the given component
             *  if (group == null)
             *  {
             *      success &= RemoveItemsOfTypeInternal(material.Key, material.Value);
             *      Debug.Assert(success, "Could not find the required component although we were permitted to build!");
             *      continue;
             *  }
             *  else
             *  {
             *      m_cuttingSolver.AddItem(group.Id, groupAmount, material.Value);
             *  }
             *
             *  m_componentCounts.Clear();
             *  CollectItems(m_componentCounts);
             *  success &= m_cuttingSolver.Solve(m_componentCounts);
             *
             *  List<MyComponentCombiner.ComponentChange> changes = null;
             *  m_cuttingSolver.GetSolution(out changes);
             *  foreach (var change in changes)
             *  {
             *      if (change.IsRemoval())
             *      {
             *          success &= RemoveItemsOfTypeInternal(change.ToRemove, change.Amount);
             *          Debug.Assert(success, "Could not remove compnents, although the solver told us it should be possible!");
             *      }
             *      else if (change.IsChange())
             *      {
             *          ComponentInfo cInfo = null;
             *          m_componentInfos.TryGetValue(change.ToRemove, out cInfo);
             *          Debug.Assert(cInfo != null, "Could not find a component in MyAreaInventory!");
             *
             *          if (cInfo == null) continue;
             *
             *          for (int i = 0; i < change.Amount; ++i)
             *          {
             *              int dummy = 0;
             *              long entityId = cInfo.RemoveComponent(1, out dummy);
             *              if (entityId == 0) break;
             *
             *              var grid = TryGetComponent(entityId);
             *              if (grid == null)
             *              {
             *                  break;
             *              }
             *
             *              SpawnRemainingData spawnData = new SpawnRemainingData();
             *              PrepareSpawnRemainingMaterial(grid, ref spawnData);
             *
             *              grid.Physics.Enabled = false;
             *              grid.SyncObject.SendCloseRequest();
             *
             *              spawnData.DefId = change.ToAdd;
             *              SpawnRemainingMaterial(ref spawnData);
             *          }
             *      }
             *  }
             * }
             *
             * return success;*/
        }
Exemplo n.º 9
0
        public bool CanCombineItems(MyInventoryBase inventory, DictionaryReader<MyDefinitionId, int> items)
        {
            bool result = true;

            Clear();
            inventory.CountItems(m_componentCounts);

            foreach (var item in items)
            {
                int itemValue = 0;
                int neededAmount = item.Value;

                MyComponentGroupDefinition group = null;
                
                group = MyDefinitionManager.Static.GetGroupForComponent(item.Key, out itemValue);
                if (group == null)
                {
                    MyFixedPoint itemAmount;
                    // Checking if this component is not provided by the group
                    MyComponentSubstitutionDefinition substitutions;
                    if (MyDefinitionManager.Static.TryGetComponentSubstitutionDefinition(item.Key, out substitutions))
                    {
                        int providedAmount;
                        if (!substitutions.IsProvidedByComponents(m_componentCounts, out providedAmount))
                        {
                            result = false;
                            break;
                        }
                        else if (providedAmount < neededAmount)
                        {
                            result = false;
                            break;
                        }
                    }
                    else if (!m_componentCounts.TryGetValue(item.Key, out itemAmount))
                    {
                        result = false;
                        break;
                    }
                    else if (itemAmount < neededAmount)
                    {
                        result = false;
                        break;
                    }                    
                }
                else
                {
                    AddItem(group.Id, itemValue, neededAmount);
                }
            }

            if (result)
            {
                result &= Solve(m_componentCounts);
            }

            if (MyDebugDrawSettings.ENABLE_DEBUG_DRAW)
            {
                if (result == false)
                    MyRenderProxy.DebugDrawText2D(new Vector2(0.0f, 0.0f), "Can not build", Color.Red, 1.0f);
                else
                {
                    List<MyComponentChange> solution = null;
                    GetSolution(out solution);

                    float yCoord = 0.0f;
                    foreach (var change in solution)
                    {
                        string text = "";
                        if (change.IsAddition())
                        {
                            text += "+ " + change.Amount.ToString() + "x" + change.ToAdd.ToString();
                            MyRenderProxy.DebugDrawText2D(new Vector2(0.0f, yCoord), text, Color.Green, 1.0f);
                            yCoord += 20.0f;
                        }
                        else if (change.IsRemoval())
                        {
                            text += "- " + change.Amount.ToString() + "x" + change.ToRemove.ToString();
                            MyRenderProxy.DebugDrawText2D(new Vector2(0.0f, yCoord), text, Color.Red, 1.0f);
                            yCoord += 20.0f;
                        }
                        else
                        {
                            text += "- " + change.Amount.ToString() + "x" + change.ToRemove.ToString();
                            MyRenderProxy.DebugDrawText2D(new Vector2(0.0f, yCoord), text, Color.Orange, 1.0f);
                            yCoord += 20.0f;

                            text = "";
                            text += "+ " + change.Amount.ToString() + "x" + change.ToAdd.ToString();
                            MyRenderProxy.DebugDrawText2D(new Vector2(0.0f, yCoord), text, Color.Orange, 1.0f);
                            yCoord += 20.0f;
                        }
                    }
                }
            }

            return result;
        }
Exemplo n.º 10
0
        public void RemoveItemsCombined(MyInventoryBase inventory, DictionaryReader<MyDefinitionId, int> toRemove)
        {
            Clear();
            foreach (var material in toRemove) // rename material to component
            {
                int groupAmount = 0;
                MyComponentGroupDefinition group = MyDefinitionManager.Static.GetGroupForComponent(material.Key, out groupAmount);

                // The component does not belong to any component group => we are looking exactly for the given component
                if (group == null)
                {
                    MyComponentSubstitutionDefinition substitutionDefinition = null;
                    if (MyDefinitionManager.Static.TryGetComponentSubstitutionDefinition(material.Key, out substitutionDefinition))
                    {
                        int amountToRemove = material.Value;
                        foreach (var entry in substitutionDefinition.ProvidingComponents)
                        {
                            if (amountToRemove > 0)
                            {
                                var removed = inventory.RemoveItemsOfType(amountToRemove * entry.Value, entry.Key);
                                amountToRemove -= (int)removed;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        inventory.RemoveItemsOfType(material.Value, material.Key);
                        continue;
                    }
                }
                else
                {
                    AddItem(group.Id, groupAmount, material.Value);
                }
            }

            inventory.CountItems(m_componentCounts);
            bool success = Solve(m_componentCounts);
            Debug.Assert(success, "Could not combine required items!");

            inventory.ApplyChanges(m_solution);

            /*CheckUpdate();

            m_remainder.Clear();
            foreach (var material in toRemove)
            {
                m_remainder.Add(material.Key, material.Value);
            }

            bool success = true;

            m_cuttingSolver.Clear();
            foreach (var material in m_remainder)
            {
                int groupAmount = 0;
                MyComponentGroupDefinition group = MyDefinitionManager.Static.GetGroupForComponent(material.Key, out groupAmount);

                // The component does not belong to any component group => we are looking exactly for the given component
                if (group == null)
                {
                    success &= RemoveItemsOfTypeInternal(material.Key, material.Value);
                    Debug.Assert(success, "Could not find the required component although we were permitted to build!");
                    continue;
                }
                else
                {
                    m_cuttingSolver.AddItem(group.Id, groupAmount, material.Value);
                }

                m_componentCounts.Clear();
                CollectItems(m_componentCounts);
                success &= m_cuttingSolver.Solve(m_componentCounts);

                List<MyComponentCombiner.ComponentChange> changes = null;
                m_cuttingSolver.GetSolution(out changes);
                foreach (var change in changes)
                {
                    if (change.IsRemoval())
                    {
                        success &= RemoveItemsOfTypeInternal(change.ToRemove, change.Amount);
                        Debug.Assert(success, "Could not remove compnents, although the solver told us it should be possible!");
                    }
                    else if (change.IsChange())
                    {
                        ComponentInfo cInfo = null;
                        m_componentInfos.TryGetValue(change.ToRemove, out cInfo);
                        Debug.Assert(cInfo != null, "Could not find a component in MyAreaInventory!");

                        if (cInfo == null) continue;

                        for (int i = 0; i < change.Amount; ++i)
                        {
                            int dummy = 0;
                            long entityId = cInfo.RemoveComponent(1, out dummy);
                            if (entityId == 0) break;

                            var grid = TryGetComponent(entityId);
                            if (grid == null)
                            {
                                break;
                            }

                            SpawnRemainingData spawnData = new SpawnRemainingData();
                            PrepareSpawnRemainingMaterial(grid, ref spawnData);

                            grid.Physics.Enabled = false;
                            grid.SyncObject.SendCloseRequest();

                            spawnData.DefId = change.ToAdd;
                            SpawnRemainingMaterial(ref spawnData);
                        }
                    }
                }
            }

            return success;*/
        }
Exemplo n.º 11
0
        public bool CanCombineItems(MyInventoryBase inventory, DictionaryReader <MyDefinitionId, int> items)
        {
            bool flag = true;

            this.Clear();
            inventory.CountItems(m_componentCounts);
            foreach (KeyValuePair <MyDefinitionId, int> pair in items)
            {
                int amount = 0;
                int num2   = pair.Value;
                MyComponentGroupDefinition groupForComponent = null;
                groupForComponent = MyDefinitionManager.Static.GetGroupForComponent(pair.Key, out amount);
                if (groupForComponent != null)
                {
                    this.AddItem(groupForComponent.Id, amount, num2);
                    continue;
                }
                if ((MySessionComponentEquivalency.Static != null) && MySessionComponentEquivalency.Static.HasEquivalents(pair.Key))
                {
                    if (MySessionComponentEquivalency.Static.IsProvided(m_componentCounts, pair.Key, pair.Value))
                    {
                        continue;
                    }
                    flag = false;
                }
                else
                {
                    MyFixedPoint point;
                    if (!m_componentCounts.TryGetValue(pair.Key, out point))
                    {
                        flag = false;
                    }
                    else
                    {
                        if (point >= num2)
                        {
                            continue;
                        }
                        flag = false;
                    }
                }
                break;
            }
            if (flag)
            {
                flag &= this.Solve(m_componentCounts);
            }
            if (MyDebugDrawSettings.ENABLE_DEBUG_DRAW)
            {
                if (!flag)
                {
                    MyRenderProxy.DebugDrawText2D(new Vector2(0f, 0f), "Can not build", Color.Red, 1f, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, false);
                }
                else
                {
                    List <MyComponentChange> changes = null;
                    this.GetSolution(out changes);
                    float y = 0f;
                    foreach (MyComponentChange change in changes)
                    {
                        string text = "";
                        if (change.IsAddition())
                        {
                            string[] textArray1 = new string[] { text, "+ ", change.Amount.ToString(), "x", change.ToAdd.ToString() };
                            text = string.Concat(textArray1);
                            MyRenderProxy.DebugDrawText2D(new Vector2(0f, y), text, Color.Green, 1f, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, false);
                            y += 20f;
                            continue;
                        }
                        if (change.IsRemoval())
                        {
                            string[] textArray2 = new string[] { text, "- ", change.Amount.ToString(), "x", change.ToRemove.ToString() };
                            text = string.Concat(textArray2);
                            MyRenderProxy.DebugDrawText2D(new Vector2(0f, y), text, Color.Red, 1f, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, false);
                            y += 20f;
                            continue;
                        }
                        string[] textArray3 = new string[] { text, "- ", change.Amount.ToString(), "x", change.ToRemove.ToString() };
                        text = string.Concat(textArray3);
                        MyRenderProxy.DebugDrawText2D(new Vector2(0f, y), text, Color.Orange, 1f, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, false);
                        y   += 20f;
                        text = "";
                        string[] textArray4 = new string[] { text, "+ ", change.Amount.ToString(), "x", change.ToAdd.ToString() };
                        text = string.Concat(textArray4);
                        MyRenderProxy.DebugDrawText2D(new Vector2(0f, y), text, Color.Orange, 1f, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP, false);
                        y += 20f;
                    }
                }
            }
            return(flag);
        }