Exemplo n.º 1
0
        public bool SpecialConnects(Tile t, int x, int y)
        {
            if (!t.active())
            {
                return(false);
            }

            if (t.frameX % 36 != 0)
            {
                x--;
            }
            if (t.frameY != 0)
            {
                y--;
            }

            if (this.ductType == DuctType.None)
            {
                return(false);
            }
            else
            {
                IConnectable conn = Connectable.Find(x, y);
                if (conn != null)
                {
                    return(conn.GetItems(this.ductType == DuctType.In ? ConnectableType.Input : ConnectableType.Output) != null);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        public override void Update()
        {
            if (Main.GameUpdateCount % TICK_DELAY != 0)
            {
                return;
            }

            switch (this.ductType)
            {
            case TEItemDuct.DuctType.None:
                break;

            case TEItemDuct.DuctType.In:
                foreach (Direction d in Direction.DIRECTIONS)
                {
                    int left = Position.X + d.dx;
                    int top  = Position.Y + d.dy;

                    Tile tile = Framing.GetTileSafely(left, top);

                    if (SpecialConnects(tile, left, top))
                    {
                        if (tile.frameX % 36 != 0)
                        {
                            left--;
                        }
                        if (tile.frameY != 0)
                        {
                            top--;
                        }

                        IConnectable conn = Connectable.Find(left, top);
                        if (conn != null)
                        {
                            Item[]            items         = conn.GetItems(ConnectableType.Input);
                            Func <Item, bool> validItemFunc = (Item it) => {
                                return(conn.Accepts(it, ConnectableType.Input));
                            };

                            for (int ind = 0; ind < items.Length; ind++)
                            {
                                Item i = items[ind];
                                if (i.active && i.type != ItemID.None && filter.FilterAccepts(i))
                                {
                                    if ((flowItems.Count + addItems.Count) < 4)
                                    {
                                        Item it = i.Clone();
                                        it.stack = 1;
                                        addItems.Add(Tuple.Create(it, Direction.NONE));

                                        if (i.stack > 1)
                                        {
                                            i.stack--;
                                        }
                                        else
                                        {
                                            i.TurnToAir();
                                        }

                                        conn.TransferredItem(i, ind, ConnectableType.Input);

                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
                break;

            case TEItemDuct.DuctType.Out:
                if (flowItems.Count == 0)
                {
                    break;
                }
                foreach (Direction d in Direction.DIRECTIONS)
                {
                    int left = Position.X + d.dx;
                    int top  = Position.Y + d.dy;

                    Tile tile = Framing.GetTileSafely(left, top);

                    if (SpecialConnects(tile, left, top))
                    {
                        if (tile.frameX % 36 != 0)
                        {
                            left--;
                        }
                        if (tile.frameY != 0)
                        {
                            top--;
                        }

                        IConnectable conn = Connectable.Find(left, top);
                        if (conn != null)
                        {
                            Item[]            items         = conn.GetItems(ConnectableType.Output);
                            Func <Item, bool> validItemFunc = (Item it) => {
                                return(conn.Accepts(it, ConnectableType.Output));
                            };

                            //Main.NewText("out searching chest");
                            foreach (Tuple <Item, Direction> it in flowItems)
                            {
                                if (!validItemFunc(it.Item1))
                                {
                                    continue;
                                }
                                int  putItem  = -1;
                                Item newStack = null;
                                for (int ind = 0; ind < items.Length; ind++)
                                {
                                    Item i = items[ind];
                                    if (i.active && i.type != 0)
                                    {
                                        if (i.stack < i.maxStack)
                                        {
                                            if (it.Item1.type == i.type)
                                            {
                                                //Main.NewText("out found stack");
                                                i.stack++;
                                                removeItems.Add(it);
                                                putItem  = ind;
                                                newStack = i;
                                                break;
                                            }
                                        }
                                    }
                                }
                                //Main.NewText("out no stack");
                                if (putItem == -1)
                                {
                                    for (int ind = 0; ind < items.Length; ind++)
                                    {
                                        Item i = items[ind];
                                        if (i.IsAir)
                                        {
                                            //Main.NewText("out put");
                                            items[ind] = it.Item1;
                                            removeItems.Add(it);
                                            putItem  = ind;
                                            newStack = it.Item1;
                                            break;
                                        }
                                    }
                                }

                                if (putItem != -1 && newStack != null)
                                {
                                    conn.TransferredItem(newStack, putItem, ConnectableType.Output);
                                }
                            }
                        }
                        break;
                    }
                }
                break;
            }

            //if(flowItems.Count > 0) Main.NewText("#" + flowItems.Count);
            if (ductType == DuctType.None || ductType == DuctType.In)
            {
                removeItems.AddRange(flowItems.FindAll(((Tuple <Item, Direction> ti) => {
                    Item i = ti.Item1;
                    //Main.NewText(i.HoverName);

                    List <Tuple <TEItemDuct, Direction> > possible = new List <Tuple <TEItemDuct, Direction> >();
                    Tuple <TEItemDuct, Direction> reverse = null;
                    foreach (Direction dir in Direction.DIRECTIONS)
                    {
                        Tile tile = Framing.GetTileSafely(Position.X + dir.dx, Position.Y + dir.dy);

                        int left = Position.X + dir.dx;
                        int top = Position.Y + dir.dy;

                        if (tile.active() && tile.type == MoreMechanisms.instance.TileType("ItemDuctTile"))
                        {
                            int index = GetInstance <TEItemDuct>().Find(left, top);
                            if (index != -1)
                            {
                                TEItemDuct ent = (TEItemDuct)TileEntity.ByID[index];

                                //Main.NewText("adjacent duct " + ((Direction)d) + " " + ent.flowItems.Count + " " + ent.addItems.Count);
                                if ((ent.flowItems.Count + ent.addItems.Count) < 4 && ent.filter.FilterAccepts(i))
                                {
                                    //Main.NewText("add item");
                                    if (ti.Item2 == dir)
                                    {
                                        reverse = Tuple.Create(ent, dir.Opposite);
                                    }
                                    else
                                    {
                                        possible.Add(Tuple.Create(ent, dir.Opposite));
                                    }
                                }
                            }
                        }
                    }

                    if (possible.Count > 0)
                    {
                        Tuple <TEItemDuct, Direction> sel = possible[Main.rand.Next(possible.Count)];
                        sel.Item1.addItems.Add(Tuple.Create(i, sel.Item2));
                        return(true);
                    }
                    else if (reverse != null)
                    {
                        reverse.Item1.addItems.Add(Tuple.Create(i, reverse.Item2));
                        return(true);
                    }

                    return(false);
                })));
            }

            needUpdate.Add(this);

            if (changed)
            {
                NetMessage.SendData(MessageID.TileEntitySharing, -1, -1, null, ID, Position.X, Position.Y);
                changed = false;
            }
        }