示例#1
0
 public SerializableAugmentedLocation(AugmentedLocation CopyFrom)
 {
     InitializeDefaults();
     if (CopyFrom != null)
     {
         this.UniqueLocationName = CopyFrom.UniqueLocationName;
         this.Tiles = CopyFrom.Tiles.Select(x => new SerializableAugmentedTile(x.Value)).ToArray();
     }
 }
        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);
            }
        }
        public void OnAugmentorPlaced(string UniqueLocationName, AugmentorType Type, int Qty, bool RemoveFromInventory, int TileX, int TileY, Augmentor Instance = null)
        {
            if (!Locations.TryGetValue(UniqueLocationName, out AugmentedLocation Location))
            {
                Location = new AugmentedLocation(this, UniqueLocationName);
                Locations.Add(UniqueLocationName, Location);
            }

            Location.OnAugmentorPlaced(Type, Qty, RemoveFromInventory, TileX, TileY, Instance);
        }
        public AugmentedTile(AugmentedLocation Location, int X, int Y)
        {
            if (!Location.Location.Objects.TryGetValue(new Vector2(X, Y), out Object Item))
            {
                throw new InvalidOperationException(string.Format("The tile at {0},{1} of {2} does not have a placed machine.", X, Y, Location.UniqueLocationName));
            }

            this.Location = Location;
            this.Machine  = Item;
            //MachineInfo.TryGetMachineInfo(Machine, out MachineInfo MI);
            //this.MachineInfo = MI;
            this.Position   = new Point(X, Y);
            this.Quantities = new Dictionary <AugmentorType, int>();
        }
        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);
        }