Пример #1
0
        private void BlockAdded(IMySlimBlock slimblock)
        {
            //Log.Trace(slimblock.ToString() + " added to InventoryManager for " + Grid.DisplayName, "blockAdded");

            if (slimblock == null)
            {
                Log.Error("Received null slimblock", "blockAdded");
                return;
            }

            MyEntity fatEntity = slimblock.FatBlock as MyEntity;

            if (fatEntity == null)
            {
                return;
            }

            MyInventoryBase inventory;

            if (!fatEntity.TryGetInventory(out inventory))
            {
                return;
            }

            Log.Trace("Adding inventory " + slimblock.ToString(), "blockAdded");
            InventoryTotals[inventory] = new ItemCountsAggregate();
            //InventoryAggregate.ChildList.AddComponent(inventory);
            OnContentsChanged(inventory);
            inventory.ContentsChanged += OnContentsChanged;
        }
Пример #2
0
 private void NotifyContentsChanged(ItemCountsAggregate change)
 {
     if (ContentsChange != null)
     {
         ContentsChange(change);
     }
 }
Пример #3
0
 public GridInventoriesManager(IMyCubeGrid grid, List<MyDefinitionId> watchedItems = null)
 {
     Grid = grid;
     Log = new Logger("SEGarden.World.Inventory.GridInventoriesManager", (() => Grid.EntityId.ToString()));
     Totals = new ItemCountsAggregate();
     InventoryTotals = new Dictionary<MyInventoryBase, ItemCountsAggregate>();
     //InventoryAggregate = new Sandbox.Game.Entities.Inventory.MyInventoryAggregate();
     WatchedItems = watchedItems;
 }
Пример #4
0
 public GridInventoriesManager(IMyCubeGrid grid, List <MyDefinitionId> watchedItems = null)
 {
     Grid            = grid;
     Log             = new Logger("SEGarden.World.Inventory.GridInventoriesManager", (() => Grid.EntityId.ToString()));
     Totals          = new ItemCountsAggregate();
     InventoryTotals = new Dictionary <MyInventoryBase, ItemCountsAggregate>();
     //InventoryAggregate = new Sandbox.Game.Entities.Inventory.MyInventoryAggregate();
     WatchedItems = watchedItems;
 }
Пример #5
0
        /*
         * public Dictionary<MyDefinitionId, MyFixedPoint> GetCounts() {
         *  VRage.Exceptions.ThrowIf<FieldAccessException>(this.Counts == null, "this.Counts");
         *
         *  return new Dictionary<MyDefinitionId, MyFixedPoint>(Counts);
         * }
         */

        public ItemCountsAggregate Copy()
        {
            //Log.Trace(String.Format("Copying {0}", this.ToString()), "Copy");
            ItemCountsAggregate result = new ItemCountsAggregate();

            foreach (var kvp in this.Counts)
            {
                result.Set(kvp.Key, kvp.Value);
            }

            //Log.Trace("Returning result", "Copy");
            return(result);
        }
Пример #6
0
        public static ItemCountsAggregate operator -(ItemCountsAggregate value1, ItemCountsAggregate value2)
        {
            VRage.Exceptions.ThrowIf <ArgumentNullException>(value1 == null, "value1");
            VRage.Exceptions.ThrowIf <ArgumentNullException>(value2 == null, "value2");

            ItemCountsAggregate result = value1.Copy();

            foreach (var kvp in value2.Counts)
            {
                result.Counts[kvp.Key] = result.Get(kvp.Key) - kvp.Value;
            }

            return(result);
        }
Пример #7
0
        public static ItemCountsAggregate operator *(ItemCountsAggregate value1, MyFixedPoint value2)
        {
            VRage.Exceptions.ThrowIf <ArgumentNullException>(value1 == null, "value1");

            //Log.Trace(String.Format("Calculating {0} * {1}", value1.ToString(), value2), "operator *");

            ItemCountsAggregate result = value1.Copy();

            foreach (var key in value1.Counts.Keys)
            {
                //Log.Trace(String.Format("Multiplying {0} * {1} for {3}", result.Counts[kvp.Key], value2, kvp.Key), "operator *");
                result.Counts[key] *= value2;
            }

            //Log.Trace("Returning result", "operator *");
            return(result);
        }
Пример #8
0
        /// <summary>
        /// Consumes a given selection of items from managed inventories
        /// Adjusts the desired removal amount by how much we actually removed
        /// and returns it.
        /// </summary>
        public void Consume(ref ItemCountsAggregate toRemove, long consumerId = 0)
        {
            VRage.Exceptions.ThrowIf <ArgumentNullException>(toRemove == null, "toRemove");

            Log.Trace("toRemove: " + toRemove.ToString(), "Consume");

            var items = new List <MyDefinitionId>(toRemove.Counts.Keys);

            var inventories = new List <MyInventoryBase>(InventoryTotals.Keys);

            foreach (var item in items)
            {
                //remainingToRemove -= InventoryAggregate.RemoveItemsOfType(remainingToRemove, item);

                MyFixedPoint remainingToRemove = toRemove.Get(item);
                Log.Trace(String.Format("Looking to remove: {0} of {1}", remainingToRemove, item), "Consume");

                foreach (var inventory in inventories)
                {
                    if (remainingToRemove <= 0)
                    {
                        break;
                    }

                    MyFixedPoint amountAvailable = inventory.GetItemAmount(item);
                    if (amountAvailable <= 0)
                    {
                        continue;
                    }

                    MyFixedPoint amountRemoved = amountAvailable < remainingToRemove ?
                                                 amountAvailable : remainingToRemove;

                    Log.Trace(String.Format("Removing: {0} from {1}", amountRemoved, inventory.Entity.EntityId), "Consume");
                    SkipNextNotify = true;
                    inventory.RemoveItemsOfType(amountRemoved, item);
                    remainingToRemove -= amountRemoved;
                }

                toRemove.Set(item, remainingToRemove);
            }

            Log.Trace(String.Format("Remaining after attempted removals: {0}", toRemove.ToString()), "Consume");
        }
Пример #9
0
        /*
         * private void BeforeContentsChanged(MyInventoryBase inventory) {
         *  Log.Trace("BeforeContentsChanged called on inventory " + inventory.Entity.ToString(), "BeforeContentsChanged");
         * }
         */

        private void OnContentsChanged(MyInventoryBase inventory)
        {
            Log.Trace("Updating inventory cache with inventory " + inventory.Entity.ToString(), "OnContentsChanged");

            ItemCountsAggregate cachedCount;

            if (!InventoryTotals.TryGetValue(inventory, out cachedCount))
            {
                Log.Error("Received an update for inventory we're not tracking.", "UpdateInventory");
                return;
            }

            ItemCountsAggregate originalCounts = cachedCount.Copy();

            Totals -= originalCounts;

            if (WatchedItems != null)
            {
                foreach (var id in WatchedItems)
                {
                    cachedCount.Set(id, inventory.GetItemAmount(id));
                }
            }
            else
            {
                foreach (var item in inventory.GetItems())
                {
                    cachedCount.Set(item.Content.GetObjectId(), item.Amount);
                }
            }

            Totals += cachedCount;

            if (SkipNextNotify)
            {
                SkipNextNotify = false;
            }
            else
            {
                NotifyContentsChanged(cachedCount - originalCounts);
            }

            //DebugPrint();
        }
Пример #10
0
        /// <summary>
        /// Consumes a given selection of items from managed inventories
        /// Adjusts the desired removal amount by how much we actually removed
        /// and returns it.
        /// </summary>
        public void Consume(ref ItemCountsAggregate toRemove, long consumerId = 0)
        {
            VRage.Exceptions.ThrowIf<ArgumentNullException>(toRemove == null, "toRemove");

            Log.Trace("toRemove: " + toRemove.ToString(), "Consume");

            var items = new List<MyDefinitionId>(toRemove.Counts.Keys);

            var inventories = new List<MyInventoryBase>(InventoryTotals.Keys);

            foreach (var item in items) {

                //remainingToRemove -= InventoryAggregate.RemoveItemsOfType(remainingToRemove, item);

                MyFixedPoint remainingToRemove = toRemove.Get(item);
                Log.Trace(String.Format("Looking to remove: {0} of {1}", remainingToRemove, item), "Consume");

                foreach (var inventory in inventories) {
                    if (remainingToRemove <= 0) break;

                    MyFixedPoint amountAvailable = inventory.GetItemAmount(item);
                    if (amountAvailable <= 0) continue;

                    MyFixedPoint amountRemoved = amountAvailable < remainingToRemove ?
                        amountAvailable : remainingToRemove;

                    Log.Trace(String.Format("Removing: {0} from {1}", amountRemoved, inventory.Entity.EntityId), "Consume");
                    SkipNextNotify = true;
                    inventory.RemoveItemsOfType(amountRemoved, item);
                    remainingToRemove -= amountRemoved;
                }

                toRemove.Set(item, remainingToRemove);
            }

            Log.Trace(String.Format("Remaining after attempted removals: {0}", toRemove.ToString()), "Consume");
        }
Пример #11
0
        /*
        private void BeforeContentsChanged(MyInventoryBase inventory) {
            Log.Trace("BeforeContentsChanged called on inventory " + inventory.Entity.ToString(), "BeforeContentsChanged");
        }
        */
        private void OnContentsChanged(MyInventoryBase inventory)
        {
            Log.Trace("Updating inventory cache with inventory " + inventory.Entity.ToString(), "OnContentsChanged");

            ItemCountsAggregate cachedCount;
            if (!InventoryTotals.TryGetValue(inventory, out cachedCount)) {
                Log.Error("Received an update for inventory we're not tracking.", "UpdateInventory");
                return;
            }

            ItemCountsAggregate originalCounts = cachedCount.Copy();
            Totals -= originalCounts;

            if (WatchedItems != null) {
                foreach (var id in WatchedItems) {
                    cachedCount.Set(id, inventory.GetItemAmount(id));
                }
            }
            else {
                foreach (var item in inventory.GetItems()) {
                    cachedCount.Set(item.Content.GetObjectId(), item.Amount);
                }
            }

            Totals += cachedCount;

            if (SkipNextNotify) {
                SkipNextNotify = false;
            }
            else {
                NotifyContentsChanged(cachedCount - originalCounts);
            }

            //DebugPrint();
        }
Пример #12
0
 private void NotifyContentsChanged(ItemCountsAggregate change)
 {
     if (ContentsChange != null) ContentsChange(change);
 }
Пример #13
0
        private void BlockAdded(IMySlimBlock slimblock)
        {
            //Log.Trace(slimblock.ToString() + " added to InventoryManager for " + Grid.DisplayName, "blockAdded");

            if (slimblock == null) {
                Log.Error("Received null slimblock", "blockAdded");
                return;
            }

            MyEntity fatEntity = slimblock.FatBlock as MyEntity;
            if (fatEntity == null) return;

            MyInventoryBase inventory;
            if (!fatEntity.TryGetInventory(out inventory)) return;

            Log.Trace("Adding inventory " + slimblock.ToString(), "blockAdded");
            InventoryTotals[inventory] = new ItemCountsAggregate();
            //InventoryAggregate.ChildList.AddComponent(inventory);
            OnContentsChanged(inventory);
            inventory.ContentsChanged += OnContentsChanged;
        }
Пример #14
0
 public bool Contains(ItemCountsAggregate other)
 {
     return(other.Counts.All((kvp) => (kvp.Value <= Get(kvp.Key))));
 }
Пример #15
0
        /*
        public Dictionary<MyDefinitionId, MyFixedPoint> GetCounts() {
            VRage.Exceptions.ThrowIf<FieldAccessException>(this.Counts == null, "this.Counts");

            return new Dictionary<MyDefinitionId, MyFixedPoint>(Counts);
        }
        */
        public ItemCountsAggregate Copy()
        {
            //Log.Trace(String.Format("Copying {0}", this.ToString()), "Copy");
            ItemCountsAggregate result = new ItemCountsAggregate();

            foreach (var kvp in this.Counts)
                result.Set(kvp.Key, kvp.Value);

            //Log.Trace("Returning result", "Copy");
            return result;
        }
Пример #16
0
 public bool Contains(ItemCountsAggregate other)
 {
     return other.Counts.All((kvp) => (kvp.Value <= Get(kvp.Key)));
 }