Esempio n. 1
0
        public override void Update()
        {
            for (int k = 0; k < RemoteAccesses.Count; k++)
            {
                if (!ByPosition.ContainsKey(RemoteAccesses[k]) || !(ByPosition[RemoteAccesses[k]] is TERemoteAccess))
                {
                    RemoteAccesses.RemoveAt(k);
                    k--;
                }
            }
            if (Main.netMode == 1)
            {
                return;
            }

            updateTimer++;
            if (updateTimer >= 60)
            {
                updateTimer = 0;
                if (Main.netMode != 2 || itemsLock.TryEnterWriteLock(2))
                {
                    try
                    {
                        CompactOne();
                    }
                    finally
                    {
                        if (Main.netMode == 2)
                        {
                            itemsLock.ExitWriteLock();
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private void Disengage(Point16 pos, int size)
        {
            if (!ByPosition.ContainsKey(pos))
            {
                return;
            }

            var entity = ByPosition[pos] as GearTileEntity;

            if (entity != null)
            {
                if (entity.size == size && entity.engaged)
                {
                    entity.rotationVelocity = 0;
                    entity.rotationOffset   = 0;

                    engaged = false;

                    Tile tile = Main.tile[Position.X, Position.Y];
                    (ModContent.GetModTile(tile.type) as GearTile)?.OnDisengage(this);

                    entity.RecurseOverGears(entity.Disengage);
                }
            }

            engaged = false;
        }
Esempio n. 3
0
 public IEnumerable <TEAbstractStorageUnit> GetStorageUnits()
 {
     return(storageUnits.Concat(RemoteAccesses.Where(remoteAccess => ByPosition.ContainsKey(remoteAccess) && ByPosition[remoteAccess] is TERemoteAccess)
                                .SelectMany(remoteAccess => ((TERemoteAccess)ByPosition[remoteAccess]).storageUnits))
            .Where(storageUnit => ByPosition.ContainsKey(storageUnit) && ByPosition[storageUnit] is TEAbstractStorageUnit)
            .Select(storageUnit => (TEAbstractStorageUnit)ByPosition[storageUnit]));
 }
Esempio n. 4
0
 public TEStorageHeart GetHeart()
 {
     if (center != new Point16(-1, -1) && ByPosition.ContainsKey(center) && ByPosition[center] is TEStorageCenter)
     {
         return(((TEStorageCenter)ByPosition[center]).GetHeart());
     }
     return(null);
 }
Esempio n. 5
0
 public override TEStorageHeart GetHeart()
 {
     if (locator.X < 0 || locator.Y < 0 || !ByPosition.ContainsKey(locator))
     {
         return(null);
     }
     return(ByPosition[locator] as TEStorageHeart);
 }
Esempio n. 6
0
            public static void Save(IValueSink sink, ByPosition value)
            {
                sink.EnterSequence();
                Value <uint> .Save(sink, value.ReferenceIndex);

                Value <int> .Save(sink, value.Count);

                sink.LeaveSequence();
            }
Esempio n. 7
0
        public static int Find(int x, int y)
        {
            TileEntity tileEntity;

            if (ByPosition.TryGetValue(new Point16(x, y), out tileEntity) && tileEntity.type == 1)
            {
                return(tileEntity.ID);
            }

            return(-1);
        }
Esempio n. 8
0
        public static void Kill(int x, int y)
        {
            TileEntity tileEntity;

            if (!ByPosition.TryGetValue(new Point16(x, y), out tileEntity) || tileEntity.type != 1)
            {
                return;
            }

            ByID.Remove(tileEntity.ID);
            ByPosition.Remove(new Point16(x, y));
        }
Esempio n. 9
0
        /// <summary>
        /// Returns the entity ID of this kind of tile entity at the given coordinates for you.
        /// </summary>
        public int Find(int i, int j)
        {
            Point16 pos = new Point16(i, j);

            if (ByPosition.ContainsKey(pos))
            {
                TileEntity tileEntity = ByPosition[pos];
                if (tileEntity.type == Type)
                {
                    return(tileEntity.ID);
                }
            }
            return(-1);
        }
Esempio n. 10
0
        /// <summary>
        /// A helper method that removes this kind of tile entity from the given coordinates for you.
        /// </summary>
        public void Kill(int i, int j)
        {
            Point16 pos = new Point16(i, j);

            if (ByPosition.ContainsKey(pos))
            {
                TileEntity tileEntity = ByPosition[pos];
                if (tileEntity.type == Type)
                {
                    ((ModTileEntity)tileEntity).OnKill();
                    ByID.Remove(tileEntity.ID);
                    ByPosition.Remove(pos);
                }
            }
        }
Esempio n. 11
0
        public override void NetReceive(BinaryReader trueReader, bool lightReceive)
        {
            /* Reads the buffer off the network */
            MemoryStream buffer       = new MemoryStream(65536);
            BinaryWriter bufferWriter = new BinaryWriter(buffer);

            bufferWriter.Write(trueReader.ReadBytes(trueReader.ReadUInt16()));
            buffer.Position = 0;

            /* Recreate the BinaryReader reader */
            DeflateStream decompressor = new DeflateStream(buffer, CompressionMode.Decompress, true);
            BinaryReader  reader       = new BinaryReader(decompressor);

            /* Original code */
            base.NetReceive(reader, lightReceive);
            if (ByPosition.ContainsKey(Position) && ByPosition[Position] is TEStorageUnit)
            {
                TEStorageUnit other = (TEStorageUnit)ByPosition[Position];
                items           = other.items;
                hasSpaceInStack = other.hasSpaceInStack;
                hasItem         = other.hasItem;
                hasItemNoPrefix = other.hasItemNoPrefix;
            }
            receiving = true;
            int  count = reader.ReadUInt16();
            bool flag  = false;

            for (int k = 0; k < count; k++)
            {
                if (UnitOperation.Receive(reader, this))
                {
                    flag = true;
                }
            }
            if (flag)
            {
                RepairMetadata();
            }
            receiving = false;

            /* Dispose all objects */
            reader.Dispose(); decompressor.Dispose(); bufferWriter.Dispose(); buffer.Dispose();
        }
Esempio n. 12
0
        private void Engage(Point16 pos, int size)
        {
            if (!ByPosition.ContainsKey(pos))
            {
                return;
            }

            var entity = ByPosition[pos] as GearTileEntity;

            if (entity != null)
            {
                if (entity.size == size && !entity.engaged)
                {
                    int   thisSize = Teeth;
                    int   nextSize = entity.Teeth;
                    float ratio    = (thisSize / (float)nextSize);

                    entity.rotationVelocity = rotationVelocity * -1 * ratio;

                    if (entity == this)                     //This is here to prevent the first gear which engages from reversing itself
                    {
                        entity.rotationVelocity *= -1;
                    }

                    float trueAngle = ((Position.ToVector2() * 16 + Vector2.One * 8) - (entity.Position.ToVector2() * 16 + Vector2.One * 8)).ToRotation();

                    entity.rotationOffset = -(ratio * rotationOffset) + ((1 + ratio) * trueAngle) + (float)Math.PI / entity.Teeth;

                    engaged = true;

                    Tile tile = Main.tile[Position.X, Position.Y];
                    (ModContent.GetModTile(tile.type) as GearTile)?.OnEngage(this);

                    entity.RecurseOverGears(entity.Engage);
                }
            }

            engaged = true;
        }
Esempio n. 13
0
 public static void Save(IValueSink sink, ByPosition value)
 {
     sink.EnterSequence();
     Value<uint>.Save(sink, value.ReferenceIndex);
     Value<int>.Save(sink, value.Count);
     sink.LeaveSequence();
 }
 public static bool IsStoragePoint(Point16 point)
 {
     return(ByPosition.ContainsKey(point) && ByPosition[point] is TEStoragePoint);
 }
        public void ResetAndSearch()
        {
            List <Point16> oldStorageUnits = new List <Point16>(storageUnits);

            storageUnits.Clear();
            HashSet <Point16> hashStorageUnits = new HashSet <Point16>();
            HashSet <Point16> explored         = new HashSet <Point16>();

            explored.Add(Position);
            Queue <Point16> toExplore = new Queue <Point16>();

            foreach (Point16 point in AdjacentComponents())
            {
                toExplore.Enqueue(point);
            }

            bool changed = false;

            while (toExplore.Count > 0)
            {
                Point16 explore = toExplore.Dequeue();
                if (!explored.Contains(explore) && explore != StorageComponent.killTile)
                {
                    explored.Add(explore);
                    if (ByPosition.ContainsKey(explore) && ByPosition[explore] is TEAbstractStorageUnit)
                    {
                        var storageUnit = (TEAbstractStorageUnit)ByPosition[explore];
                        if (storageUnit.Link(Position))
                        {
                            NetHelper.SendTEUpdate(storageUnit.ID, storageUnit.Position);
                            changed = true;
                        }
                        storageUnits.Add(explore);
                        hashStorageUnits.Add(explore);
                    }
                    foreach (Point16 point in AdjacentComponents(explore))
                    {
                        toExplore.Enqueue(point);
                    }
                }
            }

            foreach (Point16 oldStorageUnit in oldStorageUnits)
            {
                if (!hashStorageUnits.Contains(oldStorageUnit))
                {
                    if (ByPosition.ContainsKey(oldStorageUnit) && ByPosition[oldStorageUnit] is TEAbstractStorageUnit)
                    {
                        TileEntity storageUnit = ByPosition[oldStorageUnit];
                        ((TEAbstractStorageUnit)storageUnit).Unlink();
                        NetHelper.SendTEUpdate(storageUnit.ID, storageUnit.Position);
                    }
                    changed = true;
                }
            }

            if (changed)
            {
                TEStorageHeart heart = GetHeart();
                if (heart != null)
                {
                    heart.ResetCompactStage();
                }
                NetHelper.SendTEUpdate(ID, Position);
            }
        }