public static void RemoveMushroomBoxesFromCave(FarmCave farmCave) { foreach (Vector2 key in farmCave.Objects.Keys.Where(key => farmCave.Objects[key].ParentSheetIndex == Utils.ObjectIdMushroomBox).ToList()) { farmCave.Objects.Remove(key); } farmCave.UpdateReadyFlag(); }
private static bool FarmCave_DayUpdate_Prefix(FarmCave __instance, int dayOfMonth) { if (!Config.EnableMod) { return(true); } LoadCaveChoice(); if (caveChoice == null) { return(true); } var ptr = typeof(GameLocation).GetMethod("DayUpdate", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).MethodHandle.GetFunctionPointer(); var baseMethod = (Func <GameLocation>)Activator.CreateInstance(typeof(Func <GameLocation>), __instance, ptr); baseMethod(); if (caveChoice.resources.Count > 0) { SMonitor.Log($"Spawning resources"); float totalWeight = 0; foreach (var r in caveChoice.resources) { totalWeight += r.weight; } int spawned = 0; while (Game1.random.NextDouble() < Math.Min(0.99f, caveChoice.resourceChance / 100f)) { float currentWeight = 0; double chance = Game1.random.NextDouble(); foreach (var r in caveChoice.resources) { currentWeight += r.weight; if (chance < currentWeight / totalWeight) { Vector2 v = new Vector2(Game1.random.Next(1, __instance.map.Layers[0].LayerWidth - 1), Game1.random.Next(1, __instance.map.Layers[0].LayerHeight - 4)); if (__instance.isTileLocationTotallyClearAndPlaceable(v)) { spawned++; int amount = Game1.random.Next(r.min, r.max + 1); Object obj = GetObjectFromID(r.id, Vector2.Zero, amount, true); __instance.setObject(v, obj); } break; } } } SMonitor.Log($"Spawned {spawned} resources, total weight {totalWeight}"); } __instance.UpdateReadyFlag(); return(false); }