/* OnSaving * When the game saves overnight, I add the child back to the FarmHouse.characters list * so that if the mod is uninstalled, the child is returned properly. * Additionally, I remove the child copy NPC for the same reason. * If the mod is uninstalled, the new NPC shouldn't be in the save data. * * I save the Friendship data for the generated NPC here. * Otherwise, exiting the game would reset gift data. */ private void OnSaving(object sender, SavingEventArgs e) { foreach (NPC childCopy in copies.Values) { //Remove childcopy from save file first foreach (GameLocation location in Game1.locations) { if (location.characters.Contains(childCopy)) { location.getCharacters().Remove(childCopy); } } //Check indoor locations for a child NPC foreach (BuildableGameLocation location in Game1.locations.OfType <BuildableGameLocation>()) { foreach (Building building in location.buildings) { if (building.indoors.Value != null && building.indoors.Value.characters.Contains(childCopy)) { building.indoors.Value.getCharacters().Remove(childCopy); } } } //Save NPC Gift data Game1.player.friendshipData.TryGetValue(childCopy.Name, out Friendship friendship); if (friendship != null) { if (friendship.LastGiftDate != null)//null when loading from Child for the first time { string lastGiftDate = friendship.LastGiftDate.DayOfMonth + " " + friendship.LastGiftDate.Season + " " + friendship.LastGiftDate.Year; NPCFriendshipData childCopyData = new NPCFriendshipData(friendship.Points, friendship.GiftsThisWeek, lastGiftDate); helper.Data.WriteJsonFile("assets/data_" + childCopy.Name + ".json", childCopyData); } } } FarmHouse farmHouse = Utility.getHomeOfFarmer(Game1.player); //Add children foreach (Child child in children) { if (!farmHouse.getCharacters().Contains(child)) { farmHouse.addCharacter(child); } } }
/* OnDayStarted * Every time the game is saved, the children are re-added to the FarmHouse * So every morning, I check if there are children in the FarmHouse and remove them, * and I add their dopplegangers to the FarmHouse. */ private void OnDayStarted(object sender, DayStartedEventArgs e) { FarmHouse farmHouse = Utility.getHomeOfFarmer(Game1.player); int index = 1; foreach (Child child in farmHouse.getChildren()) { //If the child just aged up/first time loading save if (child.daysOld >= ageForCP && children != null && !children.Contains(child)) { //Add child to list & remove from farmHouse children.Add(child); farmHouse.getCharacters().Remove(child); //Set the parent for the child, from config or from default foreach (string name in Config.ChildParentPairs.Keys) { if (child.Name.Equals(name)) { Config.ChildParentPairs.TryGetValue(child.Name, out string parentName); children_parents.Add(child.Name, parentName); } } if (!children_parents.ContainsKey(child.Name)) { children_parents.Add(child.Name, Game1.player.spouse); } //Create childCopy, add childCopy to list, add to farmHouse at random spot Point openPoint = farmHouse.getRandomOpenPointInHouse(Game1.random, 0, 30); Point defaultBedPoint = farmHouse.getChildBed(child.Gender); defaultBedPoint = new Point(defaultBedPoint.X, defaultBedPoint.Y); Vector2 location = openPoint == null ? new Vector2(openPoint.X * 64f, openPoint.Y * 64f) : new Vector2(defaultBedPoint.X * 64f, defaultBedPoint.Y * 64f); Dictionary <string, string> dispositions = Game1.content.Load <Dictionary <string, string> >("Data\\NPCDispositions"); if (dispositions.ContainsKey(child.Name)) { string[] defaultPosition = dispositions[child.Name].Split('/')[10].Split(' '); location = new Vector2(int.Parse(defaultPosition[1]) * 64f, int.Parse(defaultPosition[2]) * 64f); } //new NPC(new AnimatedSprite("Characters\\George", 0, 16, 32), new Vector2(1024f, 1408f), "JoshHouse", 0, "George", false, (Dictionary<int, int[]>) null, Game1.content.Load<Texture2D>("Portraits\\George")); NPC childCopy = new NPC(child.Sprite, location, "FarmHouse", 2, child.Name, false, null, null) //schedule null, portrait null { DefaultMap = Game1.player.homeLocation.Value, DefaultPosition = location, Breather = false, HideShadow = false, Position = location, displayName = child.Name }; copies.Add(child.Name, childCopy); farmHouse.addCharacter(childCopy); //Check if I've made this NPC before & set gift info try { NPCFriendshipData childCopyFriendship = helper.Data.ReadJsonFile <NPCFriendshipData>(helper.Content.GetActualAssetKey("assets/data_" + childCopy.Name + ".json", ContentSource.ModFolder)); if (childCopyFriendship != null) { Game1.player.friendshipData.TryGetValue(child.Name, out Friendship childFriendship); childFriendship.GiftsThisWeek = childCopyFriendship.GiftsThisWeek; childFriendship.LastGiftDate = new WorldDate(childCopyFriendship.GetYear(), childCopyFriendship.GetSeason(), childCopyFriendship.GetDay()); } } catch (Exception) { } } //If NPC was already generated previously else if (copies.ContainsKey(child.Name)) { //Remove child farmHouse.getCharacters().Remove(child); //Add copy at random location in the house copies.TryGetValue(child.Name, out NPC childCopy); childCopy.Position = childCopy.DefaultPosition; farmHouse.addCharacter(childCopy); } index++; } }