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); }
private static bool Event_answerDialogue_Prefix(string questionKey, int answerChoice) { if (!Config.EnableMod || questionKey != "cave") { return(true); } caveChoice = SHelper.Content.Load <Dictionary <string, CaveChoice> >(frameworkPath, ContentSource.GameContent)[answerIds[answerChoice]]; SMonitor.Log($"Chose {caveChoice.choice}, objects {caveChoice.objects.Count}"); SHelper.Data.WriteSaveData("farm-cave-framework-choice", caveChoice.id); if (caveChoice.objects.Count > 0) { FarmCave cave = Game1.getLocationFromName("FarmCave") as FarmCave; foreach (var o in caveChoice.objects) { Object obj = GetObjectFromID(o.id, new Vector2(o.X, o.Y)); if (obj != null) { cave.setObject(new Vector2(o.X, o.Y), obj); } } } return(false); }