Esempio n. 1
0
        public FluidNetwork() : base()
        {
            pipesConnectedToMachines = new List <Point16>();
            pumpTimers = new Dictionary <Point16, Timer>();

            OnEntryPlace += pos => {
                Tile tile = Framing.GetTileSafely(pos);

                if (ModContent.GetModTile(tile.type) is FluidPumpTile)
                {
                    Capacity += ModContent.GetInstance <FluidTransportTile>().Capacity;

                    if (!pumpTimers.ContainsKey(pos))
                    {
                        pumpTimers.Add(pos, new Timer()
                        {
                            value = 34
                        });
                    }
                }
                else if (ModContent.GetModTile(tile.type) is FluidTransportTile transport)
                {
                    Capacity += transport.Capacity;
                }

                if (TileEntityUtils.TryFindMachineEntity(pos + new Point16(0, -1), out MachineEntity entity) && (entity is ILiquidMachine || entity is IGasMachine))
                {
                    pipesConnectedToMachines.Add(pos);
                }

                if (TileEntityUtils.TryFindMachineEntity(pos + new Point16(-1, 0), out entity) && (entity is ILiquidMachine || entity is IGasMachine))
                {
                    if (!pipesConnectedToMachines.Contains(pos))
                    {
                        pipesConnectedToMachines.Add(pos);
                    }
                }

                if (TileEntityUtils.TryFindMachineEntity(pos + new Point16(1, 0), out entity) && (entity is ILiquidMachine || entity is IGasMachine))
                {
                    if (!pipesConnectedToMachines.Contains(pos))
                    {
                        pipesConnectedToMachines.Add(pos);
                    }
                }

                if (TileEntityUtils.TryFindMachineEntity(pos + new Point16(0, 1), out entity) && (entity is ILiquidMachine || entity is IGasMachine))
                {
                    if (!pipesConnectedToMachines.Contains(pos))
                    {
                        pipesConnectedToMachines.Add(pos);
                    }
                }
            };

            OnEntryKill += pos => {
                Tile  tile = Framing.GetTileSafely(pos);
                float cap  = 0;
                if (ModContent.GetModTile(tile.type) is FluidPumpTile)
                {
                    cap = ModContent.GetInstance <FluidTransportTile>().Capacity;

                    pumpTimers.Remove(pos);
                }
                else if (ModContent.GetModTile(tile.type) is FluidTransportTile transport)
                {
                    cap += transport.Capacity;
                }

                Capacity    -= cap;
                StoredFluid -= cap;

                pipesConnectedToMachines.Remove(pos);
            };

            PostRefreshConnections += () => {
                if (StoredFluid > Capacity)
                {
                    StoredFluid = Capacity;
                }
            };
        }
Esempio n. 2
0
        /// <summary>
        /// Attempts to move the item in this object along the <seealso cref="itemNetwork"/>
        /// </summary>
        public void Update()
        {
            oldCenter = worldCenter;

            var newTilePos = worldCenter.ToTileCoordinates16();

            if (delayPathCalc && itemNetwork.HasEntryAt(newTilePos))
            {
                delayPathCalc = false;
            }

            CalculatePath();

            MoveAlongNetwork();

            newTilePos = worldCenter.ToTileCoordinates16();
            Tile sourceTile = Framing.GetTileSafely(newTilePos);

            if (ModContent.GetModTile(sourceTile.type) is ItemPumpTile pump)
            {
                //Force the move direction to be away from the pump
                moveDir = -(pump.GetBackwardsOffset(newTilePos) - newTilePos).ToVector2();
            }

            if (!delayPathCalc && (!sourceTile.active() || !itemNetwork.HasEntryAt(newTilePos)))
            {
                Item data = ItemIO.Load(itemData);

                //If the tile is a machine or chest, try to put this item in there
                Chest         chest    = null;
                MachineEntity entity   = null;
                bool          sendBack = true;

                if ((entity = FindConnectedMachine(itemNetwork, newTilePos, data)) != null || (chest = FindConnectedChest(itemNetwork, newTilePos, data, out _)) != null)
                {
                    if (chest is Chest)
                    {
                        sendBack = !chest.TryInsertItems(data);

                        if (sendBack)
                        {
                            itemData = ItemIO.Save(data);
                        }
                    }
                    else
                    {
                        entity.InputItemFromNetwork(this, out sendBack);
                    }

                    if (!sendBack)
                    {
                        itemNetwork.RemovePath(this.id);
                    }
                    else
                    {
                        //Send the item back into the network and force it to create a new path
                        needsPathRefresh = true;
                        moveDir         *= -1;
                        delayPathCalc    = true;
                    }
                }
                else
                {
                    if (FindConnectedChest(itemNetwork, newTilePos, data, out _) is null && !(TileEntityUtils.TryFindMachineEntity(newTilePos + new Point16(0, -1), out _) || TileEntityUtils.TryFindMachineEntity(newTilePos + new Point16(-1, 0), out _) || TileEntityUtils.TryFindMachineEntity(newTilePos + new Point16(0, 1), out _) || TileEntityUtils.TryFindMachineEntity(newTilePos + new Point16(1, 0), out _)))
                    {
                        SpawnInWorld();
                    }
                    else
                    {
                        wander           = true;
                        moveDir         *= -1;
                        delayPathCalc    = true;
                        needsPathRefresh = true;

                        worldCenter = itemNetwork.GetEntries().Select(pipe => pipe.Position).OrderBy(p => Vector2.DistanceSquared(p.ToWorldCoordinates(), worldCenter)).First().ToWorldCoordinates();
                    }
                }