コード例 #1
0
        public override void Update()
        {
            mined = false;

            if (Main.GameUpdateCount % 30 == 0)
            {
                UpdateFrame();
                //Main.NewText("hasFrame = " + hasFrame);
            }

            if (on && Main.GameUpdateCount % 30 == 0)
            {
                Main.PlaySound(SoundID.Item22, Position.X * 16, Position.Y * 16);
            }

            if (on && hasFrame && pickaxe != null && pickaxe.pick > 0)
            {
                if (Main.GameUpdateCount % pickaxe.useTime == 0)
                {
                    //Main.PlaySound(SoundID.Dig, Position.X * 16, Position.Y * 16);

                    for (int y = -top; y <= bottom; y++)
                    {
                        for (int x = -left; x <= right; x++)
                        {
                            int tx = Position.X + x;
                            int ty = Position.Y + y;
                            if (Main.tile[tx, ty] != null && Main.tile[tx, ty].active() && Main.tileSolid[Main.tile[tx, ty].type] && filter.FilterAccepts(Main.tile[tx, ty]))
                            {
                                if (Util.CanPickTile(tx, ty, pickaxe.pick))
                                {
                                    List <Item> before = GetItemsInFrame();
                                    Util.PickTile(tx, ty, pickaxe.pick);
                                    List <Item> after = GetItemsInFrame();
                                    foreach (Item i in after)
                                    {
                                        if (!before.Contains(i))
                                        {
                                            i.position.X  = (Position.X + 1) * 16 - 8 + Main.rand.Next(16);
                                            i.position.Y  = (Position.Y + 1) * 16 - 8 + Main.rand.Next(16);
                                            i.velocity.X /= 4;
                                            i.velocity.Y /= 4;
                                            i.instanced   = true;
                                        }
                                    }
                                    mined = true;
                                    goto BrokeBlock;
                                }
                            }
                        }
                    }
                    BrokeBlock : { }
                }
            }

            if (changed)
            {
                NetMessage.SendData(MessageID.TileEntitySharing, -1, -1, null, ID, Position.X, Position.Y);
                changed = false;
            }
        }
コード例 #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;
            }
        }