Esempio n. 1
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 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;*/
        }
Esempio n. 3
0
        public void MoveItemsToConstructionStockpile(MyInventoryBase fromInventory)
        {
            if (MySession.Static.CreativeMode || MySession.Static.SimpleSurvival)
                return;

            m_tmpComponents.Clear();
            GetMissingComponents(m_tmpComponents);

            if (m_tmpComponents.Count() != 0)
            {
                EnsureConstructionStockpileExists();

                m_stockpile.ClearSyncList();
                foreach (var kv in m_tmpComponents)
                {
                    var id = new MyDefinitionId(typeof(MyObjectBuilder_Component), kv.Key);
                    int amountAvailable = (int)fromInventory.GetItemAmount(id);
                    int moveAmount = Math.Min(kv.Value, amountAvailable);
                    if (moveAmount > 0)
                    {
                        fromInventory.RemoveItemsOfType(moveAmount, id);
                        m_stockpile.AddItems(moveAmount, new MyDefinitionId(typeof(MyObjectBuilder_Component), kv.Key));
                    }
                }
                CubeGrid.SyncObject.SendStockpileChanged(this, m_stockpile.GetSyncList());
                m_stockpile.ClearSyncList();
            }
        }
Esempio n. 4
0
        private bool PasteInternal(MyInventoryBase buildInventory, bool missingDefinitions, bool deactivate, List<MyObjectBuilder_CubeGrid> pastedBuilders = null, List<MyCubeGrid> touchingGrids = null,
            UpdateAfterPasteCallback updateAfterPasteCallback = null)
        {
            MyGuiAudio.PlaySound(MyGuiSounds.HudPlaceBlock);

            MyEntities.RemapObjectBuilderCollection(m_copiedGrids);

            m_tmpPastedBuilders.Clear();
            m_tmpPastedBuilders.Capacity = m_copiedGrids.Count;
            MyCubeGrid firstPastedGrid = null;

            bool forceDynamicGrid = IsForcedDynamic() && !m_gridChangeToDynamicDisabled;

            int i = 0;
            bool retVal = false;
            List<MyCubeGrid> pastedGrids = new List<MyCubeGrid>();

            foreach (var gridBuilder in m_copiedGrids)
               {
                gridBuilder.CreatePhysics = true;
                gridBuilder.EnableSmallToLargeConnections = true;
                bool savedStaticFlag = gridBuilder.IsStatic;

                if (forceDynamicGrid)
                {
                    gridBuilder.IsStatic = false;
                }

                var previousPos = gridBuilder.PositionAndOrientation;
                gridBuilder.PositionAndOrientation = new MyPositionAndOrientation(m_previewGrids[i].WorldMatrix);

                var pastedGrid = MyEntities.CreateFromObjectBuilder(gridBuilder) as MyCubeGrid;

                if (pastedGrid == null)
                {
                    retVal = true;
                    continue;
                }

                if (MySession.Static.EnableStationVoxelSupport && pastedGrid.IsStatic)
                {
                    pastedGrid.TestDynamic = true;
                }

                //pastedGrid.PositionComp.SetPosition(MySector.MainCamera.Position);
                MyEntities.Add(pastedGrid);
                if (i == 0) firstPastedGrid = pastedGrid;

               
                if (missingDefinitions)
                    pastedGrid.DetectDisconnectsAfterFrame();

                //pastedGrid.PositionComp.SetWorldMatrix(m_previewGrids[i].WorldMatrix);
                i++;

                if (!pastedGrid.IsStatic && (!MyFakes.ENABLE_BATTLE_SYSTEM || !MySession.Static.Battle))
                    pastedGrid.Physics.LinearVelocity = m_objectVelocity;

                if (!pastedGrid.IsStatic && MySession.ControlledEntity != null && MySession.ControlledEntity.Entity.Physics != null && m_calculateVelocity
                    && (!MyFakes.ENABLE_BATTLE_SYSTEM || !MySession.Static.Battle))
                {
                    pastedGrid.Physics.AngularVelocity = MySession.ControlledEntity.Entity.Physics.AngularVelocity;
                }

                pastedGrids.Add(pastedGrid);

                gridBuilder.IsStatic = savedStaticFlag;

                retVal = true;
            }

            //Because blocks fills SubBlocks in this method..
            //TODO: Create LoadPhase2
            MyEntities.UpdateOnceBeforeFrame();

            foreach (var pastedGrid in pastedGrids)
            {
                var builder = pastedGrid.GetObjectBuilder();
                m_tmpPastedBuilders.Add(builder);

                if (pastedBuilders != null)
                    pastedBuilders.Add((MyObjectBuilder_CubeGrid)builder);
            }

            if (IsSnapped && SnapMode == MyGridPlacementSettings.SnapMode.Base6Directions && m_hitEntity is MyCubeGrid && firstPastedGrid != null && ((MyCubeGrid)m_hitEntity).GridSizeEnum == firstPastedGrid.GridSizeEnum)
            {
                var hitGrid = m_hitEntity as MyCubeGrid;

                MatrixI mergingTransform = hitGrid.CalculateMergeTransform(firstPastedGrid, hitGrid.WorldToGridInteger(firstPastedGrid.PositionComp.GetPosition()));
                MySyncCreate.RequestMergingCopyPaste(m_tmpPastedBuilders, m_hitEntity.EntityId, mergingTransform);
            }
            else if (touchingGrids != null && touchingGrids.Count > 0)
            {
                // Currently only first grid is supported for merging.
                MyCubeGrid touchingGrid = touchingGrids[0];

                if (touchingGrid != null)
                {
                    MatrixI mergingTransform = touchingGrid.CalculateMergeTransform(firstPastedGrid, touchingGrid.WorldToGridInteger(firstPastedGrid.PositionComp.GetPosition()));
                    MySyncCreate.RequestMergingCopyPaste(m_tmpPastedBuilders, touchingGrid.EntityId, mergingTransform);
                }
                else
                {
                    //MySyncCreate.RequestEntitiesCreate(m_tmpPastedBuilders);
                    MySyncCreate.SendEntitiesCreated(m_tmpPastedBuilders);
                }
            }
            else
            {
                // CH:TODO: This would probably be safer if it was requested from the server as well
                MySyncCreate.SendEntitiesCreated(m_tmpPastedBuilders);
            }

            // CH:TODO: Use only items for grids that were really added to not screw with players
            if (buildInventory != null)
            {
                foreach (var item in m_buildComponents.TotalMaterials)
                {
                    buildInventory.RemoveItemsOfType(item.Value, item.Key);
                }
            }

            if (deactivate)
                Deactivate();

            if (retVal && updateAfterPasteCallback != null)
            {
                updateAfterPasteCallback(pastedBuilders);
            }

            return retVal;
        }
        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;*/
        }
Esempio n. 6
0
        public void MoveFirstItemToConstructionStockpile(MyInventoryBase fromInventory)
        {
            if (MySession.Static.CreativeMode)
            {
                return;
            }

            EnsureConstructionStockpileExists();

            MyComponentStack.GroupInfo info = ComponentStack.GetGroupInfo(0);
            m_stockpile.ClearSyncList();
            if ((int)fromInventory.GetItemAmount(info.Component.Id) >= 1)
            {
                //Other player cant move your inventory and you also when trying to cosntruct so its safe already after check above ^^
                fromInventory.RemoveItemsOfType(1, info.Component.Id, MyItemFlags.None);
                //Debug.Assert(removed, "Item not found, but reported available few lines above");
                m_stockpile.AddItems(1, info.Component.Id);
            }
            CubeGrid.SyncObject.SendStockpileChanged(this, m_stockpile.GetSyncList());
            m_stockpile.ClearSyncList();
        }