Exemple #1
0
        internal void Update()
        {
            HandlerCache.Clear();

            for (int i = 0; i < Tiles.Count; i++)
            {
                Duct        duct    = Tiles[i];
                ItemHandler handler = duct.Module?.GetHandler();
                if (handler != null)
                {
                    if (HandlerCache.ContainsKey(handler))
                    {
                        HandlerCache[handler].Add(duct.Module);
                    }
                    else
                    {
                        HandlerCache.Add(handler, new List <BaseModule> {
                            duct.Module
                        });
                    }
                }
            }

            //Main.NewText("Tracking " + HandlerCache.Count + " handlers and " + HandlerCache.Sum(pair => pair.Value.Count) + " modules");

            for (int i = 0; i < NetworkItems.Count; i++)
            {
                NetworkItem item = NetworkItems[i];
                item.Update();
                if (item.item == null || item.item.IsAir)
                {
                    NetworkItems.Remove(item);
                }
            }
        }
Exemple #2
0
        internal void Load(TagCompound tag)
        {
            RoutedLayer layer = Routed.RoutedLayer;

            Tiles        = new List <Duct>();
            NetworkItems = new List <NetworkItem>();

            foreach (TagCompound compound in tag.GetList <TagCompound>("Tiles"))
            {
                Duct element = new Duct
                {
                    Position = compound.Get <Point16>("Position"),
                    Layer    = layer,
                    Network  = this
                };
                element.Load(compound.GetCompound("Data"));

                layer[element.Position] = element;
                Tiles.Add(element);
            }

            foreach (TagCompound compound in tag.GetList <TagCompound>("NetworkItems"))
            {
                NetworkItems.Add(new NetworkItem(compound));
            }

            RegenerateCache();
        }
Exemple #3
0
        public NetworkItem(TagCompound tag)
        {
            item        = tag.Get <Item>("Item");
            origin      = Routed.RoutedLayer[tag.Get <Point16>("Position")];
            destination = Routed.RoutedLayer[tag.Get <Point16>("Destination")];

            path            = Pathfinding.FindPath(origin, destination);
            CurrentPosition = PreviousPosition = path.Pop();
        }
Exemple #4
0
        public NetworkItem(Item item, Duct origin, Duct destination)
        {
            this.item        = item;
            this.origin      = origin;
            this.destination = destination;

            path            = Pathfinding.FindPath(origin, destination);
            CurrentPosition = PreviousPosition = path.Pop();
            timer           = speed = Routed.RoutedLayer[CurrentPosition].Speed;
        }
Exemple #5
0
        public bool PullItem(int type, int count, Duct destination)
        {
            foreach (ProviderModule module in ProviderModules)
            {
                ItemHandler handler = module.GetHandler();
                if (handler == null)
                {
                    continue;
                }

                int available = module.GetAvailableItems(type);
                if (available <= 0)
                {
                    continue;
                }

                for (int i = 0; i < handler.Slots; i++)
                {
                    Item item = handler.Items[i];
                    if (item.IsAir || item.type != type)
                    {
                        continue;
                    }

                    int extractedAmount = Utility.Min(count, item.stack, available);
                    if (extractedAmount <= 0)
                    {
                        continue;
                    }

                    NetworkItem networkItem = new NetworkItem(handler.ExtractItem(i, extractedAmount), module.Parent, destination);
                    NetworkItems.Add(networkItem);

                    ItemCache[item.netID] -= extractedAmount;
                    if (ItemCache[item.netID] <= 0)
                    {
                        ItemCache.Remove(item.type);
                    }

                    UpdateUIs();

                    count     -= extractedAmount;
                    available -= extractedAmount;
                    if (count <= 0)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemple #6
0
        public bool PushItem(Item item, Duct origin)
        {
            foreach (MarkerModule module in MarkerModules.Where(markerModule =>
            {
                var other = markerModule.GetHandler();
                if (other == null)
                {
                    return(false);
                }
                return(other.HasSpace(item) && markerModule.IsItemValid(item));
            }).OrderByDescending(markerModule => markerModule.priority))
            {
                ItemHandler handler = module.GetHandler();

                int sum = 0;
                for (int i = 0; i < handler.Slots; i++)
                {
                    Item slot      = handler.Items[i];
                    int  slotLimit = handler.GetItemLimit(i) ?? item.maxStack;

                    if (slot.IsAir)
                    {
                        sum += slotLimit;
                    }
                    else if (slot.type == item.type)
                    {
                        sum += slotLimit - slot.stack;
                    }
                }

                Item sent = new Item();
                sent.SetDefaults(item.type);
                int extracted = Math.Min(sum, item.stack);
                sent.stack  = extracted;
                item.stack -= extracted;

                NetworkItems.Add(new NetworkItem(sent, origin, module.Parent));

                if (item.stack <= 0)
                {
                    return(true);
                }
            }

            return(false);
        }