Пример #1
0
        public SerializableAugmentedTile(AugmentedTile CopyFrom)
        {
            InitializeDefaults();

            if (CopyFrom != null)
            {
                this.TileXPosition = CopyFrom.Position.X;
                this.TileYPosition = CopyFrom.Position.Y;

                List <AugmentorType> TempTypes      = new List <AugmentorType>();
                List <int>           TempQuantities = new List <int>();
                foreach (KeyValuePair <AugmentorType, int> KVP in CopyFrom.Quantities)
                {
                    AugmentorType Type     = KVP.Key;
                    int           Quantity = KVP.Value;
                    if (Quantity > 0)
                    {
                        TempTypes.Add(Type);
                        TempQuantities.Add(Quantity);
                    }
                }

                this.AugmentorTypes      = TempTypes.ToArray();
                this.AugmentorQuantities = TempQuantities.ToArray();
            }
        }
        internal void OnMachineRemoved(AugmentedTile Tile)
        {
            if (Tile.Location != this)
            {
                throw new InvalidOperationException("Object instance mismatch in AugmentedLocation.OnMachineRemoved.");
            }

            Tiles.Remove(EncodeTileToString(Tile.Position));

            if (!Context.IsMultiplayer || Context.IsMainPlayer)
            {
                //  Now that the managed machine has been removed, refund the player with the augmentors that were placed there
                //  by spawning them on the ground
                foreach (KeyValuePair <AugmentorType, int> KVP in Tile.Quantities)
                {
                    int Quantity = KVP.Value;
                    if (Quantity > 0)
                    {
                        Augmentor Refund         = Augmentor.CreateInstance(KVP.Key, Quantity);
                        int       SpawnDirection = Augmentor.Randomizer.Next(4);
                        Game1.createItemDebris(Refund, new Vector2(Tile.Position.X * Game1.tileSize, Tile.Position.Y * Game1.tileSize), SpawnDirection, Location, -1);
                    }
                }
            }
        }
        internal void OnMachineRemoved(AugmentedTile Tile)
        {
            if (Tile.Location != this)
            {
                throw new InvalidOperationException("Object instance mismatch in AugmentedLocation.OnMachineRemoved.");
            }

            Tiles.Remove(EncodeTileToString(Tile.Position));

            if (!Context.IsMultiplayer || Context.IsMainPlayer)
            {
                //  Now that the managed machine has been removed, refund the player with the augmentors that were placed there
                //  by spawning them on the ground
                foreach (KeyValuePair <AugmentorType, int> KVP in Tile.Quantities)
                {
                    int Quantity = KVP.Value;
                    if (Quantity > 0)
                    {
                        Augmentor Refund = Augmentor.CreateInstance(KVP.Key, Quantity);
                        int       SpawnDirection;
                        //  When spawning items at the edge of the map, sometimes it seems to move them off the map. Mostly only happens when removing augmentors from incubators in coops,
                        //  so as a temporary workaround, spawn the items in the direction of the player when handling indestructible machines like incubators.
                        if (!MachineInfo.IsDestructible(Tile.Machine))
                        {
                            SpawnDirection = Game1.MasterPlayer.getGeneralDirectionTowards(Tile.VectorPosition, 0, true);
                        }
                        else
                        {
                            SpawnDirection = Augmentor.Randomizer.Next(4);
                        }
                        Game1.createItemDebris(Refund, new Vector2(Tile.Position.X * Game1.tileSize, Tile.Position.Y * Game1.tileSize), SpawnDirection, Location, -1);
                    }
                }
            }
        }
        public PlacedAugmentorsManager()
        {
            this.Locations = new Dictionary <string, AugmentedLocation>();

            try
            {
                if (!Context.IsMultiplayer || Context.IsMainPlayer)
                {
                    //  Load data from save file
                    SerializablePlacedAugmentors SavedData = MachineAugmentorsMod.ModInstance.Helper.Data.ReadSaveData <SerializablePlacedAugmentors>(SavedDataKey);
                    if (SavedData != null)
                    {
                        foreach (SerializableAugmentedLocation SavedLocation in SavedData.Locations)
                        {
                            string       LocationName = SavedLocation.UniqueLocationName;
                            GameLocation GL           = Game1.getLocationFromName(LocationName);
                            if (GL == null)
                            {
                                MachineAugmentorsMod.ModInstance.Monitor.Log(string.Format("Warning - Could not find a GameLocation named '{0}'. Some of your augmentors may not have been loaded from your save file!", LocationName), LogLevel.Warn);
                            }
                            else
                            {
                                AugmentedLocation AL = new AugmentedLocation(this, LocationName);
                                Locations.Add(LocationName, AL);

                                foreach (SerializableAugmentedTile SavedTile in SavedLocation.Tiles)
                                {
                                    if (!GL.Objects.TryGetValue(new Vector2(SavedTile.TileXPosition, SavedTile.TileYPosition), out Object Item))
                                    {
                                        string Warning = string.Format("Warning - GameLocation '{0}' does not have a machine at ({1},{2}). Some of your augmentors may not have been loaded from your save file!",
                                                                       LocationName, SavedTile.TileXPosition, SavedTile.TileYPosition);
                                        MachineAugmentorsMod.ModInstance.Monitor.Log(Warning, LogLevel.Warn);
                                    }
                                    else
                                    {
                                        AugmentedTile AT = new AugmentedTile(AL, SavedTile.TileXPosition, SavedTile.TileYPosition);
                                        AL.Tiles.Add(AugmentedLocation.EncodeTileToString(SavedTile.TileXPosition, SavedTile.TileYPosition), AT);

                                        for (int i = 0; i < SavedTile.AugmentorTypes.Length; i++)
                                        {
                                            AugmentorType Type     = SavedTile.AugmentorTypes[i];
                                            int           Quantity = SavedTile.AugmentorQuantities[i];
                                            AT.Quantities.Add(Type, Quantity);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MachineAugmentorsMod.ModInstance.Monitor.Log("Error while loading augmentor data from ReadSaveData: " + ex, LogLevel.Error);
            }
        }
        internal void OnAugmentorPlaced(AugmentorType Type, int Qty, bool RemoveFromInventory, int TileX, int TileY, Augmentor Instance = null)
        {
            string Key = EncodeTileToString(TileX, TileY);

            if (!Tiles.TryGetValue(Key, out AugmentedTile Tile))
            {
                Tile = new AugmentedTile(this, TileX, TileY);
                Tiles.Add(Key, Tile);
            }

            Tile.OnAugmentorPlaced(Type, Qty, RemoveFromInventory, Instance);
        }
        internal void LoadSettings(SerializablePlacedAugmentors Data)
        {
            if (Data != null)
            {
                foreach (SerializableAugmentedLocation SavedLocation in Data.Locations)
                {
                    string       LocationName = SavedLocation.UniqueLocationName;
                    GameLocation GL           = Game1.getLocationFromName(LocationName);
                    if (GL == null)
                    {
                        MachineAugmentorsMod.ModInstance.Monitor.Log(string.Format("Warning - Could not find a GameLocation named '{0}'. Some of your augmentors may not have been loaded from your save file!", LocationName), LogLevel.Warn);
                    }
                    else
                    {
                        AugmentedLocation AL = new AugmentedLocation(this, LocationName);
                        Locations.Add(LocationName, AL);

                        foreach (SerializableAugmentedTile SavedTile in SavedLocation.Tiles)
                        {
                            if (!GL.Objects.TryGetValue(new Vector2(SavedTile.TileXPosition, SavedTile.TileYPosition), out Object Item))
                            {
                                string Warning = string.Format("Warning - GameLocation '{0}' does not have a machine at ({1},{2}). Some of your augmentors may not have been loaded from your save file!",
                                                               LocationName, SavedTile.TileXPosition, SavedTile.TileYPosition);
                                MachineAugmentorsMod.ModInstance.Monitor.Log(Warning, LogLevel.Warn);
                            }
                            else
                            {
                                AugmentedTile AT = new AugmentedTile(AL, SavedTile.TileXPosition, SavedTile.TileYPosition);
                                AL.Tiles.Add(AugmentedLocation.EncodeTileToString(SavedTile.TileXPosition, SavedTile.TileYPosition), AT);

                                for (int i = 0; i < SavedTile.AugmentorTypes.Length; i++)
                                {
                                    AugmentorType Type     = SavedTile.AugmentorTypes[i];
                                    int           Quantity = SavedTile.AugmentorQuantities[i];
                                    AT.Quantities.Add(Type, Quantity);
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>Checks the given tile position of each augmented location, to find a <see cref="AugmentedTile"/> with the matching Machine.</summary>
        public bool TryFindAugmentedTile(Object Machine, int TileX, int TileY, out AugmentedTile Result)
        {
            Result = null;
            if (Machine == null || !Machine.bigCraftable || !Machine.isPlaceable())
            {
                return(false);
            }

            string Key = AugmentedLocation.EncodeTileToString(TileX, TileY);

            foreach (AugmentedLocation AL in this.Locations.Values)
            {
                AugmentedTile Tile;
                if (AL.Tiles.TryGetValue(Key, out Tile) && Tile.Machine == Machine)
                {
                    Result = Tile;
                    return(true);
                }
            }

            return(false);
        }
 /// <summary>Checks the given tile position of each augmented location, to find a <see cref="AugmentedTile"/> with the matching Machine.</summary>
 public bool TryFindAugmentedTile(Object Machine, Vector2 TileLocation, out AugmentedTile Result)
 {
     return(TryFindAugmentedTile(Machine, (int)TileLocation.X, (int)TileLocation.Y, out Result));
 }