private static PlacedFoodData GetClosestFood(NPC npc, GameLocation location)
        {
            List <PlacedFoodData> foodList = new List <PlacedFoodData>();

            foreach (var f in location.furniture)
            {
                if (f.heldObject.Value != null && f.heldObject.Value.Edibility > 0)
                {
                    for (int x = f.boundingBox.X / 64; x < (f.boundingBox.X + f.boundingBox.Width) / 64; x++)
                    {
                        for (int y = f.boundingBox.Y / 64; y < (f.boundingBox.Y + f.boundingBox.Height) / 64; y++)
                        {
                            foodList.Add(new PlacedFoodData(f, new Vector2(x, y), f.heldObject.Value, -1));
                        }
                    }
                }
                if (fdfAPI != null)
                {
                    int slots = fdfAPI.GetTotalSlots(f);
                    for (int i = 0; i < slots; i++)
                    {
                        if (f.modData.ContainsKey("aedenthorn.FurnitureDisplayFramework/" + i) && f.modData["aedenthorn.FurnitureDisplayFramework/" + i].Length > 0)
                        {
                            string[] parts = f.modData["aedenthorn.FurnitureDisplayFramework/" + i].Split(',');
                            //SMonitor.Log($"Got food {parts[0]} at slot {i} of {f.Name}");
                            var slotObj = new Object(int.Parse(parts[0]), int.Parse(parts[1]));
                            if (slotObj.Edibility > 0)
                            {
                                var slotRect = fdfAPI.GetSlotRect(f, i);
                                if (slotRect != null)
                                {
                                    foodList.Add(new PlacedFoodData(f, new Vector2((f.boundingBox.X + slotRect.Value.X) / 64, (f.boundingBox.Y + slotRect.Value.Y) / 64), slotObj, i));
                                }
                            }
                        }
                    }
                }
            }
            if (foodList.Count == 0)
            {
                //SMonitor.Log("Got no food");
                return(null);
            }
            List <string> favList  = new List <string>(Game1.NPCGiftTastes["Universal_Love"].Split(' '));
            List <string> likeList = new List <string>(Game1.NPCGiftTastes["Universal_Like"].Split(' '));
            List <string> okayList = new List <string>(Game1.NPCGiftTastes["Universal_Neutral"].Split(' '));

            if (Game1.NPCGiftTastes.TryGetValue(npc.Name, out string NPCLikes) && NPCLikes != null)
            {
                favList.AddRange(NPCLikes.Split('/')[1].Split(' '));
                likeList.AddRange(NPCLikes.Split('/')[3].Split(' '));
                okayList.AddRange(NPCLikes.Split('/')[5].Split(' '));
            }
            for (int i = foodList.Count - 1; i >= 0; i--)
            {
                if (favList.Contains(foodList[i].foodObject.ParentSheetIndex + ""))
                {
                    foodList[i].value = 3;
                }
                else
                {
                    if (likeList.Contains(foodList[i].foodObject.ParentSheetIndex + ""))
                    {
                        foodList[i].value = 2;
                    }
                    else
                    {
                        if (okayList.Contains(foodList[i].foodObject.ParentSheetIndex + ""))
                        {
                            foodList[i].value = 2;
                        }
                        else
                        {
                            foodList.RemoveAt(i);
                        }
                    }
                }
            }
            if (foodList.Count == 0)
            {
                //SMonitor.Log("Got no food");
                return(null);
            }

            foodList.Sort(delegate(PlacedFoodData a, PlacedFoodData b)
            {
                var compare = b.value.CompareTo(a.value);
                if (compare != 0)
                {
                    return(compare);
                }
                return(Vector2.Distance(a.foodTile, npc.getTileLocation()).CompareTo(Vector2.Distance(b.foodTile, npc.getTileLocation())));
            });

            SMonitor.Log($"Got {foodList.Count} possible food for {npc.Name}; best: {foodList[0].foodObject.Name} at {foodList[0].foodTile}, value {foodList[0].value}");
            return(foodList[0]);
        }
Exemple #2
0
        public static void GetRandomPost(string friend)
        {
            IDictionary <string, string> data;

            try
            {
                data = Helper.Content.Load <Dictionary <string, string> >($"Characters\\Dialogue\\{friend}", ContentSource.GameContent);
            }
            catch
            {
                return;
            }
            if (data == null)
            {
                return;
            }

            string NPCLikes;

            Game1.NPCGiftTastes.TryGetValue(friend, out NPCLikes);
            if (NPCLikes == null)
            {
                return;
            }
            string[] likes = NPCLikes.Split('/')[1].Split(' ');
            if (likes.Length == 0)
            {
                return;
            }
            List <Object> foods = new List <Object>();
            List <Object> favs  = new List <Object>();

            foreach (string str in likes)
            {
                if (!int.TryParse(str, out int idx))
                {
                    continue;
                }
                var obj = new Object(idx, 1, false, -1, 0);
                if (obj.Type == "Cooking")
                {
                    foods.Add(obj);
                }
                else
                {
                    favs.Add(obj);
                }
            }

            if (foods.Count > 0)
            {
                string itemName = foods[Game1.random.Next(foods.Count)].DisplayName;
                IEnumerable <KeyValuePair <string, string> > strings = data.Where(s => s.Key.StartsWith("SocialNetwork_Food_"));

                if (strings.Count() > 0)
                {
                    string str = strings.ElementAt(Game1.random.Next(strings.Count())).Value;
                    str = str.Replace("{0}", itemName);
                    NPC       npc        = Game1.getCharacterFromName(friend);
                    Texture2D portrait   = npc.Sprite.Texture;
                    Rectangle sourceRect = npc.getMugShotSourceRect();
                    ModEntry.postList.Add(new SocialPost(npc, portrait, sourceRect, str));
                    Monitor.Log($"added food post from {npc.displayName}");
                    return;
                }
            }
            if (favs.Count > 0)
            {
                string itemName = favs[Game1.random.Next(favs.Count)].DisplayName;
                IEnumerable <KeyValuePair <string, string> > strings = data.Where(s => s.Key.StartsWith("SocialNetwork_Favorite_"));
                if (strings.Count() > 0)
                {
                    string str = strings.ElementAt(Game1.random.Next(strings.Count())).Value;
                    str = str.Replace("{0}", itemName);
                    NPC       npc        = Game1.getCharacterFromName(friend);
                    Texture2D portrait   = npc.Sprite.Texture;
                    Rectangle sourceRect = npc.getMugShotSourceRect();
                    ModEntry.postList.Add(new SocialPost(npc, portrait, sourceRect, str));
                    Monitor.Log($"added favorite post from {npc.displayName}");
                    return;
                }
            }
        }