コード例 #1
0
ファイル: Main.cs プロジェクト: aedenthorn/mytimeatportia
                private static bool Prefix(ref AnimalFarmUnit __instance)
                {
                    if (!enabled)
                    {
                        return(false);
                    }
                    if (__instance.Animals.Count >= __instance.HouseData.AnimalMax)
                    {
                        Dbgl($"farm already full, cancelling");
                        return(false);
                    }

                    int tipsId;

                    Dictionary <int, List <AnimalinFarm> > aifs = new Dictionary <int, List <AnimalinFarm> >();

                    foreach (AnimalinFarm aif in __instance.Animals)
                    {
                        if (aif.Age >= aif.Data.StandardPoint)
                        {
                            if (aifs.ContainsKey(aif.Data.Id))
                            {
                                aifs[aif.Data.Id].Add(aif);
                            }
                            else
                            {
                                aifs.Add(aif.Data.Id, new List <AnimalinFarm>()
                                {
                                    aif
                                });
                            }
                        }
                    }

                    Dictionary <int, int> newAnimals = new Dictionary <int, int>();

                    foreach (KeyValuePair <int, List <AnimalinFarm> > kvp in aifs)
                    {
                        newAnimals.Add(kvp.Key, 0);
                    }

                    while (aifs.Keys.Count > 0)
                    {
                        if (__instance.Animals.Count >= __instance.HouseData.AnimalMax)
                        {
                            Dbgl($"farm filled up, breaking");
                            break;
                        }
                        int idx = aifs.Keys.ToArray()[UnityEngine.Random.Range(0, aifs.Count - 1)];
                        if (aifs[idx].Count < 2)
                        {
                            aifs.Remove(idx);
                        }
                        else
                        {
                            List <AnimalinFarm> aifl          = aifs[idx];
                            List <AnimalinFarm> matureAnimals = aifl.FindAll((AnimalinFarm a) => a.Age >= a.Data.StandardPoint);
                            if (matureAnimals.Count < 2)
                            {
                                aifs.Remove(idx);
                            }
                            else
                            {
                                int          a  = UnityEngine.Random.Range(0, matureAnimals.Count - 1);
                                AnimalinFarm aa = matureAnimals[a];
                                matureAnimals.RemoveAt(a);
                                int          b  = UnityEngine.Random.Range(0, matureAnimals.Count - 1);
                                AnimalinFarm ab = matureAnimals[a];
                                matureAnimals.RemoveAt(b);
                                float chancea = settings.BasePregnancyChance + (100 - settings.BasePregnancyChance) * ((aa.Age - aa.Data.StandardPoint) / aa.Data.StandardPoint);
                                float chanceb = settings.BasePregnancyChance + (100 - settings.BasePregnancyChance) * ((ab.Age - ab.Data.StandardPoint) / ab.Data.StandardPoint);
                                float chance  = (chancea + chanceb) / 2f;
                                Dbgl($"type: {idx} age: {aa.Age} {ab.Age} preg chance: {chance}");
                                if (UnityEngine.Random.Range(0, 100) <= chance)
                                {
                                    Dbgl($"birthing");
                                    __instance.AddAnimal(idx, out tipsId);
                                    typeof(AnimalinFarm).GetField("age", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(aa, aa.Data.StandardPoint);
                                    typeof(AnimalinFarm).GetField("age", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(ab, ab.Data.StandardPoint);
                                    newAnimals[idx]++;
                                }
                                aifs[idx].Remove(aa);
                                aifs[idx].Remove(ab);
                            }
                        }
                    }
                    foreach (KeyValuePair <int, int> kvp in newAnimals)
                    {
                        if (kvp.Value > 0)
                        {
                            string name = TextMgr.GetStr(AnimalData.GetDataByDataId(kvp.Key).NameId, -1);
                            Dbgl($"sending tips ui");
                            Singleton <TipsMgr> .Instance.SendSystemTip($"{(kvp.Value > 1 ? kvp.Value + " " + name + "s" : "A " + name)} gave birth!");
                        }
                    }
                    return(false);
                }