using System; using System.Collections.Generic; using Server; using Server.Items; namespace Server.Items { // The class is now static, meaning it exists globally and not as an Item public static class FarmController { // Vegetable Plot Boundaries private static Rectangle2D VeggieRect = new Rectangle2D(795, 771, 17, 15); // Reagent Plot Boundaries private static Rectangle2D ReagentRect = new Rectangle2D(820, 843, 23, 16); // Lists must be static now private static List m_VeggieTypes = new List { typeof(FarmableCabbage), typeof(FarmableCarrot), typeof(FarmableCotton), typeof(FarmableFlax), typeof(FarmableLettuce), typeof(FarmableOnion), typeof(FarmablePumpkin), typeof(FarmableTurnip), typeof(FarmableWheat) }; //private static List m_ReagentTypes = new List { typeof(FarmableBlackPearl), typeof(FarmableBloodmoss), typeof(FarmableGarlic), typeof(FarmableGinseng), typeof(FarmableMandrakeRoot), typeof(FarmableNightshade), typeof(FarmableSpidersSilk), typeof(FarmableSulfurousAsh) }; // The core looks for 'Initialize' on startup and runs it automatically public static void Initialize() { double mySpawnVal = Server.ValidSettings.FarmSpawnTimer(); TimeSpan spawnInterval = TimeSpan.FromMinutes(mySpawnVal); // Start the global background timer Timer.DelayCall(spawnInterval, spawnInterval, SpawnTick); } private static void SpawnTick() { // Spawn 2 vegetables and 2 reagents each tick if space allows SpawnInArea(VeggieRect, m_VeggieTypes, 2); //SpawnInArea(ReagentRect, m_ReagentTypes, 2); } private static void SpawnInArea(Rectangle2D rect, List types, int count) { // Safety net for empty lists if (types == null || types.Count == 0) return; for (int i = 0; i < count; i++) { int x = Utility.Random(rect.X, rect.Width); int y = Utility.Random(rect.Y, rect.Height); Point3D loc = new Point3D(x, y, 0); bool occupied = false; // Get the items in range IPooledEnumerable eable = Map.Felucca.GetItemsInRange(loc, 0); // If the loop runs even once, something is there foreach (Item item in eable) { occupied = true; break; } // Free the pool to prevent memory leaks eable.Free(); // If the spot is empty, spawn the crop if (!occupied) { Type type = types[Utility.Random(types.Count)]; Item crop = (Item)Activator.CreateInstance(type); crop.MoveToWorld(loc, Map.Felucca); } } } } }