private static void UpdateMagicItemms(ColonyState state)
        {
            try
            {
                if (state.MagicUpdateTime < Time.SecondsSinceStartDouble)
                {
                    var colony = state.ColonyRef;

                    foreach (var follower in colony.Followers)
                    {
                        var inv = ColonistInventory.Get(follower);

                        if (inv.MagicItemUpdateTime < Time.SecondsSinceStartDouble)
                        {
                            foreach (var item in inv.Armor)
                            {
                                if (item.Value.Id != 0 && ArmorFactory.ArmorLookup.TryGetValue(item.Value.Id, out var armor))
                                {
                                    armor.Update();

                                    if (armor.HPTickRegen != 0)
                                    {
                                        follower.Heal(armor.HPTickRegen);
                                    }
                                }
                            }

                            if (WeaponFactory.WeaponLookup.TryGetValue(inv.Weapon.Id, out var wep))
                            {
                                wep.Update();

                                if (wep.HPTickRegen != 0)
                                {
                                    follower.Heal(wep.HPTickRegen);
                                }
                            }

                            inv.MagicItemUpdateTime = Time.SecondsSinceStartDouble + Random.Next(3, 5);
                        }
                    }

                    state.MagicUpdateTime = Time.SecondsSinceStartDouble + 5;
                }
            }
            catch (Exception ex)
            {
                APILogger.LogError(ex);
            }
        }
 public static NPCModel MapNPC(NPC.NPCBase follower)
 {
     return(new NPCModel()
     {
         FoodHoursCarried = follower.FoodHoursCarried,
         GoalPosition = new SerializableVector3(follower.UsedPath.path?.Goal.Vector),
         Health = follower.health,
         Id = follower.ID,
         NpcTypeJobId = follower.Job.NPCType.Type,
         NPCInventory = follower.Inventory,
         Position = new SerializableVector3(follower.Position),
         ColonistInventory = ColonistInventory.Get(follower),
         BedPosition = new SerializableVector3(follower.UsedBedLocation)
     });
 }
Example #3
0
        public static void OnNPCGathered(IJob job, Vector3Int location, List <ItemTypeDrops> results)
        {
            if (job != null && job.NPC != null && results != null && results.Count > 0)
            {
                IncrimentSkill(job.NPC);
                var inv = ColonistInventory.Get(job.NPC);

                foreach (var item in results)
                {
                    if (ItemTypes.TryGetType(item.Type, out var itemType))
                    {
                        inv.IncrimentStat(itemType.Name, item.Amount);
                    }
                }
            }
        }
        public static IPandaDamage GetWeapon(ModLoader.OnHitData box)
        {
            var weap = box.HitSourceObject as IPandaDamage;

            if (weap == null && box.HitSourceType == ModLoader.OnHitData.EHitSourceType.NPC)
            {
                var npc = box.HitSourceObject as NPCBase;

                if (npc != null)
                {
                    var inv = ColonistInventory.Get(npc);

                    if (!inv.Weapon.IsEmpty() && WeaponLookup.ContainsKey(inv.Weapon.Id))
                    {
                        weap = WeaponLookup[inv.Weapon.Id];
                    }
                }
            }

            if (weap == null &&
                (box.HitSourceType == ModLoader.OnHitData.EHitSourceType.PlayerProjectile ||
                 box.HitSourceType == ModLoader.OnHitData.EHitSourceType.PlayerClick))
            {
                var p = box.HitSourceObject as Players.Player;

                if (p != null)
                {
                    var ps = PlayerState.GetPlayerState(p);

                    if (!ps.Weapon.IsEmpty() && WeaponLookup.ContainsKey(ps.Weapon.Id))
                    {
                        weap = WeaponLookup[ps.Weapon.Id];
                    }
                }
            }

            return(weap);
        }
Example #5
0
        public static void PressButton(ButtonPressCallbackData data)
        {
            if ((!data.ButtonIdentifier.Contains(".RecruitButton") &&
                 !data.ButtonIdentifier.Contains(".FireButton") &&
                 !data.ButtonIdentifier.Contains(".MoveFired") &&
                 !data.ButtonIdentifier.Contains(".ColonyToolMainMenu") &&
                 !data.ButtonIdentifier.Contains(".KillFired") &&
                 !data.ButtonIdentifier.Contains(".CallToArms")) || data.Player.ActiveColony == null)
            {
                return;
            }

            Dictionary <string, JobCounts> jobCounts = GetJobCounts(data.Player.ActiveColony);

            if (data.ButtonIdentifier.Contains(".ColonyToolMainMenu"))
            {
                NetworkMenuManager.SendServerPopup(data.Player, BuildMenu(data.Player, jobCounts, false, string.Empty, 0));
            }
            else if (data.ButtonIdentifier.Contains(".FireButton"))
            {
                foreach (var job in jobCounts)
                {
                    if (data.ButtonIdentifier.Contains(job.Key))
                    {
                        var recruit = data.Storage.GetAs <int>(job.Key + ".Recruit");
                        var count   = GetCountValue(recruit);
                        var menu    = BuildMenu(data.Player, jobCounts, true, job.Key, count);

                        menu.LocalStorage.SetAs(GameInitializer.NAMESPACE + ".FiredJobName", job.Key);
                        menu.LocalStorage.SetAs(GameInitializer.NAMESPACE + ".FiredJobCount", count);

                        NetworkMenuManager.SendServerPopup(data.Player, menu);
                        break;
                    }
                }
            }
            else if (data.ButtonIdentifier.Contains(".KillFired"))
            {
                var firedJob = data.Storage.GetAs <string>(GameInitializer.NAMESPACE + ".FiredJobName");
                var count    = data.Storage.GetAs <int>(GameInitializer.NAMESPACE + ".FiredJobCount");

                foreach (var job in jobCounts)
                {
                    if (job.Key == firedJob)
                    {
                        if (count > job.Value.TakenCount)
                        {
                            count = job.Value.TakenCount;
                        }

                        for (int i = 0; i < count; i++)
                        {
                            var npc = job.Value.TakenJobs[i].NPC;
                            npc.ClearJob();
                            npc.OnDeath();
                        }

                        break;
                    }
                }

                data.Player.ActiveColony.SendCommonData();
                jobCounts = GetJobCounts(data.Player.ActiveColony);
                NetworkMenuManager.SendServerPopup(data.Player, BuildMenu(data.Player, jobCounts, false, string.Empty, 0));
            }
            else if (data.ButtonIdentifier.Contains(".MoveFired"))
            {
                var firedJob = data.Storage.GetAs <string>(GameInitializer.NAMESPACE + ".FiredJobName");
                var count    = data.Storage.GetAs <int>(GameInitializer.NAMESPACE + ".FiredJobCount");

                foreach (var job in jobCounts)
                {
                    if (data.ButtonIdentifier.Contains(job.Key))
                    {
                        if (count > job.Value.AvailableCount)
                        {
                            count = job.Value.AvailableCount;
                        }

                        if (jobCounts.TryGetValue(firedJob, out var firedJobCounts))
                        {
                            for (int i = 0; i < count; i++)
                            {
                                if (firedJobCounts.TakenCount > i)
                                {
                                    var npc = firedJobCounts.TakenJobs[i].NPC;
                                    npc.ClearJob();
                                    npc.TakeJob(job.Value.AvailableJobs[i]);
                                    data.Player.ActiveColony.JobFinder.Remove(job.Value.AvailableJobs[i]);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }

                        data.Player.ActiveColony.SendCommonData();
                        break;
                    }
                }

                jobCounts = GetJobCounts(data.Player.ActiveColony);
                NetworkMenuManager.SendServerPopup(data.Player, BuildMenu(data.Player, jobCounts, false, string.Empty, 0));
            }
            else if (data.ButtonIdentifier.Contains(".RecruitButton"))
            {
                foreach (var job in jobCounts)
                {
                    if (data.ButtonIdentifier.Contains(job.Key))
                    {
                        var recruit = data.Storage.GetAs <int>(job.Key + ".Recruit");
                        var count   = GetCountValue(recruit);

                        if (count > job.Value.AvailableCount)
                        {
                            count = job.Value.AvailableCount;
                        }

                        for (int i = 0; i < count; i++)
                        {
                            var num = 0f;
                            data.Player.ActiveColony.HappinessData.RecruitmentCostCalculator.GetCost(data.Player.ActiveColony.HappinessData.CachedHappiness, data.Player.ActiveColony, out float foodCost);
                            if (data.Player.ActiveColony.Stockpile.TotalFood < foodCost ||
                                !data.Player.ActiveColony.Stockpile.TryRemoveFood(ref num, foodCost))
                            {
                                PandaChat.Send(data.Player, _localizationHelper, "Notenoughfood", ChatColor.red);
                                break;
                            }
                            else
                            {
                                var newGuy = new NPCBase(data.Player.ActiveColony, data.Player.ActiveColony.GetClosestBanner(new Vector3Int(data.Player.Position)).Position);
                                newGuy.FoodHoursCarried = ServerManager.ServerSettings.NPCs.InitialFoodCarriedHours;
                                data.Player.ActiveColony.RegisterNPC(newGuy);
                                ColonistInventory.Get(newGuy);
                                NPCTracker.Add(newGuy);
                                ModLoader.Callbacks.OnNPCRecruited.Invoke(newGuy);

                                if (newGuy.IsValid)
                                {
                                    newGuy.TakeJob(job.Value.AvailableJobs[i]);
                                    data.Player.ActiveColony.JobFinder.Remove(job.Value.AvailableJobs[i]);
                                }
                            }
                        }


                        data.Player.ActiveColony.SendCommonData();

                        jobCounts = GetJobCounts(data.Player.ActiveColony);
                        NetworkMenuManager.SendServerPopup(data.Player, BuildMenu(data.Player, jobCounts, false, string.Empty, 0));
                    }
                }
            }
        }
Example #6
0
 public static void OnNPCDied(NPCBase npc)
 {
     ColonistInventory.Get(npc);
     UpdateFoodUse(ColonyState.GetColonyState(npc.Colony));
 }
        public static void OnMonsterHit(IMonster monster, ModLoader.OnHitData d)
        {
            var cs          = ColonyState.GetColonyState(monster.OriginalGoal);
            var pandaArmor  = monster as IPandaArmor;
            var pamdaDamage = d.HitSourceObject as IPandaDamage;
            var skilled     = 0f;

            if (pandaArmor != null && Random.NextFloat() <= pandaArmor.MissChance)
            {
                d.ResultDamage = 0;
                return;
            }

            if (pamdaDamage == null && d.HitSourceType == ModLoader.OnHitData.EHitSourceType.NPC)
            {
                var npc = d.HitSourceObject as NPCBase;
                var inv = ColonistInventory.Get(npc);
                ColonistManager.IncrimentSkill(npc);
                skilled = inv.GetSkillModifier();

                if (inv.Weapon != null && Items.Weapons.WeaponFactory.WeaponLookup.TryGetValue(inv.Weapon.Id, out var wep))
                {
                    pamdaDamage = wep;
                }
            }

            if (pandaArmor != null && pamdaDamage != null)
            {
                d.ResultDamage = Items.Weapons.WeaponFactory.CalcDamage(pandaArmor, pamdaDamage);
            }
            else if (pandaArmor != null)
            {
                d.ResultDamage = DamageType.Physical.CalcDamage(pandaArmor.ElementalArmor, d.ResultDamage);

                if (pandaArmor.AdditionalResistance.TryGetValue(DamageType.Physical, out var flatResist))
                {
                    d.ResultDamage = d.ResultDamage - d.ResultDamage * flatResist;
                }
            }

            double skillRoll = Pipliz.Random.Next() + skilled;

            if (skillRoll < skilled)
            {
                d.ResultDamage += d.ResultDamage;
            }

            d.ResultDamage = d.ResultDamage - d.ResultDamage * cs.Difficulty.MonsterDamageReduction;

            if (d.ResultDamage < 0)
            {
                d.ResultDamage = 0;
            }

            if (d.HitSourceType == ModLoader.OnHitData.EHitSourceType.NPC)
            {
                var npc = d.HitSourceObject as NPCBase;
                var inv = ColonistInventory.Get(npc);
                inv.IncrimentStat("Damage Done", d.ResultDamage);

                if (skillRoll < skilled)
                {
                    inv.IncrimentStat("Double Damage Hits");
                }
            }

            if (d.ResultDamage >= monster.CurrentHealth)
            {
                monster.OnRagdoll();
                var    rewardMonster = monster as IPandaZombie;
                string monsterType   = "zombie";

                if (rewardMonster != null)
                {
                    monsterType = rewardMonster.MosterType;
                }

                if (monster.OriginalGoal.OwnerIsOnline())
                {
                    if (!string.IsNullOrEmpty(monsterType) &&
                        LootTables.Lookup.TryGetValue(monsterType, out var lootTable))
                    {
                        float luck = 0;

                        if (d.HitSourceObject is ILucky luckSrc)
                        {
                            luck = luckSrc.Luck;
                        }
                        else if ((d.HitSourceType == ModLoader.OnHitData.EHitSourceType.PlayerClick ||
                                  d.HitSourceType == ModLoader.OnHitData.EHitSourceType.PlayerProjectile) &&
                                 d.HitSourceObject is Players.Player player)
                        {
                            var ps = PlayerState.GetPlayerState(player);

                            foreach (var armor in ps.Armor)
                            {
                                if (Items.Armor.ArmorFactory.ArmorLookup.TryGetValue(armor.Value.Id, out var a))
                                {
                                    luck += a.Luck;
                                }
                            }

                            if (Items.Weapons.WeaponFactory.WeaponLookup.TryGetValue(ps.Weapon.Id, out var w))
                            {
                                luck += w.Luck;
                            }
                        }
                        else if (d.HitSourceType == ModLoader.OnHitData.EHitSourceType.NPC &&
                                 d.HitSourceObject is NPCBase nPC)
                        {
                            var inv = ColonistInventory.Get(nPC);

                            foreach (var armor in inv.Armor)
                            {
                                if (Items.Armor.ArmorFactory.ArmorLookup.TryGetValue(armor.Value.Id, out var a))
                                {
                                    luck += a.Luck;
                                }
                            }

                            if (Items.Weapons.WeaponFactory.WeaponLookup.TryGetValue(inv.Weapon.Id, out var w))
                            {
                                luck += w.Luck;
                            }
                        }

                        var roll = lootTable.GetDrops(luck);

                        foreach (var item in roll)
                        {
                            monster.OriginalGoal.Stockpile.Add(item.Key, item.Value);
                        }
                    }
                }
            }
        }
Example #8
0
        public static void OnNPCJobChanged(ValueTuple <NPCBase, IJob, IJob> data)
        {
            try
            {
                if (data.Item1 != null)
                {
                    if (data.Item1.CustomData == null)
                    {
                        data.Item1.CustomData = new JSONNode();
                    }

                    var inv = ColonistInventory.Get(data.Item1);

                    if (data.Item1.NPCType.IsLaborer)
                    {
                        inv.UnemployedLeaveTime = TimeCycle.TotalHours + 48;
                    }
                    else
                    {
                        inv.UnemployedLeaveTime = 0;
                    }
                }

                if (data.Item3 is GuardJobInstance guardJob && data.Item3.TryGetNPCGuardDefaultSettings(out var settings))
                {
                    if (settings != null)
                    {
                        guardJob.Settings = new GuardJobSettings()
                        {
                            BlockTypes              = settings.BlockTypes,
                            CooldownMissingItem     = settings.CooldownMissingItem,
                            CooldownSearchingTarget = settings.CooldownSearchingTarget,
                            CooldownShot            = settings.CooldownShot,
                            Damage            = settings.Damage,
                            NPCType           = settings.NPCType,
                            NPCTypeKey        = settings.NPCTypeKey,
                            OnHitAudio        = settings.OnHitAudio,
                            OnShootAudio      = settings.OnShootAudio,
                            OnShootResultItem = settings.OnShootResultItem,
                            Range             = settings.Range,
                            RecruitmentItem   = settings.RecruitmentItem,
                            ShootItem         = settings.ShootItem,
                            SleepType         = settings.SleepType
                        }
                    }
                    ;
                }
                else if (data.Item3 is CraftingJobInstance craftingJob)
                {
                    if (craftingJob.Settings.GetType() == typeof(CraftingJobRotatedLitSettings) && data.Item3.TryGetNPCCraftDefaultSettings(out CraftingJobRotatedLitSettings craftSettingslit))
                    {
                        craftingJob.Settings = new CraftingJobRotatedLitSettings(craftSettingslit.BlockTypes[0].Name, craftSettingslit.NPCTypeKey, craftSettingslit.CraftingCooldown, craftSettingslit.MaxCraftsPerHaul, craftSettingslit.OnCraftedAudio)
                        {
                            BlockTypes       = craftSettingslit.BlockTypes,
                            CraftingCooldown = craftSettingslit.CraftingCooldown,
                            MaxCraftsPerHaul = craftSettingslit.MaxCraftsPerHaul,
                            NPCType          = craftSettingslit.NPCType,
                            NPCTypeKey       = craftSettingslit.NPCTypeKey,
                            OnCraftedAudio   = craftSettingslit.OnCraftedAudio,
                            RecruitmentItem  = craftSettingslit.RecruitmentItem
                        }
                    }
                    ;
                    else if (craftingJob.Settings.GetType() == typeof(CraftingJobRotatedSettings) && data.Item3.TryGetNPCCraftDefaultSettings(out CraftingJobRotatedSettings craftSettingsRot))
                    {
                        craftingJob.Settings = new CraftingJobRotatedSettings(craftSettingsRot.BlockTypes[0].Name, craftSettingsRot.NPCTypeKey, craftSettingsRot.CraftingCooldown, craftSettingsRot.MaxCraftsPerHaul, craftSettingsRot.OnCraftedAudio)
                        {
                            BlockTypes       = craftSettingsRot.BlockTypes,
                            CraftingCooldown = craftSettingsRot.CraftingCooldown,
                            MaxCraftsPerHaul = craftSettingsRot.MaxCraftsPerHaul,
                            NPCType          = craftSettingsRot.NPCType,
                            NPCTypeKey       = craftSettingsRot.NPCTypeKey,
                            OnCraftedAudio   = craftSettingsRot.OnCraftedAudio,
                            RecruitmentItem  = craftSettingsRot.RecruitmentItem
                        }
                    }
                    ;
                    else if (craftingJob.Settings.GetType() == typeof(CraftingJobSettings) && data.Item3.TryGetNPCCraftDefaultSettings(out CraftingJobSettings craftSettings))
                    {
                        craftingJob.Settings = new CraftingJobSettings()
                        {
                            BlockTypes       = craftSettings.BlockTypes,
                            CraftingCooldown = craftSettings.CraftingCooldown,
                            MaxCraftsPerHaul = craftSettings.MaxCraftsPerHaul,
                            NPCType          = craftSettings.NPCType,
                            NPCTypeKey       = craftSettings.NPCTypeKey,
                            OnCraftedAudio   = craftSettings.OnCraftedAudio,
                            RecruitmentItem  = craftSettings.RecruitmentItem
                        }
                    }
                    ;
                }

                data.Item1?.ApplyJobResearch();
            }
            catch (Exception ex)
            {
                APILogger.LogError(ex);
            }
        }
        //[ModLoader.ModCallback(ModLoader.EModCallbackType.OnNPCJobChanged, GameInitializer.NAMESPACE + ".SettlerManager.OnNPCJobChanged")]
        public static void OnNPCJobChanged(ValueTuple <NPCBase, IJob, IJob> data)
        {
            try
            {
                if (data.Item1 != null)
                {
                    if (data.Item1.CustomData == null)
                    {
                        data.Item1.CustomData = new JSONNode();
                    }

                    var inv = ColonistInventory.Get(data.Item1);

                    if (data.Item1.NPCType.IsLaborer)
                    {
                        inv.UnemployedLeaveTime = TimeCycle.TotalHours + 48;
                    }
                    else
                    {
                        inv.UnemployedLeaveTime = 0;
                    }
                }

                if (data.Item3 is GuardJobInstance guardJob && data.Item3.TryGetNPCGuardDefaultSettings(out var settings))
                {
                    if (settings != null)
                    {
                        var setting = Activator.CreateInstance(settings.GetType()) as GuardJobSettings;
                        setting.BlockTypes              = settings.BlockTypes;
                        setting.CooldownMissingItem     = settings.CooldownMissingItem;
                        setting.CooldownSearchingTarget = settings.CooldownSearchingTarget;
                        setting.CooldownShot            = settings.CooldownShot;
                        setting.Damage            = settings.Damage;
                        setting.NPCType           = settings.NPCType;
                        setting.NPCTypeKey        = settings.NPCTypeKey;
                        setting.OnHitAudio        = settings.OnHitAudio;
                        setting.OnShootAudio      = settings.OnShootAudio;
                        setting.OnShootResultItem = settings.OnShootResultItem;
                        setting.Range             = settings.Range;
                        setting.RecruitmentItem   = settings.RecruitmentItem;
                        setting.ShootItem         = settings.ShootItem;
                        setting.SleepType         = settings.SleepType;

                        guardJob.Settings = setting;
                    }
                }
                else if (data.Item3 is CraftingJobInstance craftingJob)
                {
                    if (craftingJob.Settings.GetType() == typeof(CraftingJobRotatedLitSettings) && data.Item3.TryGetNPCCraftDefaultSettings(out CraftingJobRotatedLitSettings craftSettingslit))
                    {
                        var litSettings = Activator.CreateInstance(craftSettingslit.GetType(), craftSettingslit.BlockTypes[0].Name, craftSettingslit.NPCTypeKey, craftSettingslit.CraftingCooldown, craftSettingslit.MaxCraftsPerHaul, craftSettingslit.OnCraftedAudio) as CraftingJobRotatedLitSettings;

                        litSettings.BlockTypes       = craftSettingslit.BlockTypes;
                        litSettings.CraftingCooldown = craftSettingslit.CraftingCooldown;
                        litSettings.MaxCraftsPerHaul = craftSettingslit.MaxCraftsPerHaul;
                        litSettings.NPCType          = craftSettingslit.NPCType;
                        litSettings.NPCTypeKey       = craftSettingslit.NPCTypeKey;
                        litSettings.OnCraftedAudio   = craftSettingslit.OnCraftedAudio;
                        litSettings.RecruitmentItem  = craftSettingslit.RecruitmentItem;

                        craftingJob.Settings = litSettings;
                    }
                    else if (craftingJob.Settings.GetType() == typeof(CraftingJobRotatedSettings) && data.Item3.TryGetNPCCraftDefaultSettings(out CraftingJobRotatedSettings craftSettingsRot))
                    {
                        var litSettings = Activator.CreateInstance(craftSettingsRot.GetType(), craftSettingsRot.BlockTypes[0].Name, craftSettingsRot.NPCTypeKey, craftSettingsRot.CraftingCooldown, craftSettingsRot.MaxCraftsPerHaul, craftSettingsRot.OnCraftedAudio) as CraftingJobRotatedSettings;

                        litSettings.BlockTypes       = craftSettingsRot.BlockTypes;
                        litSettings.CraftingCooldown = craftSettingsRot.CraftingCooldown;
                        litSettings.MaxCraftsPerHaul = craftSettingsRot.MaxCraftsPerHaul;
                        litSettings.NPCType          = craftSettingsRot.NPCType;
                        litSettings.NPCTypeKey       = craftSettingsRot.NPCTypeKey;
                        litSettings.OnCraftedAudio   = craftSettingsRot.OnCraftedAudio;
                        litSettings.RecruitmentItem  = craftSettingsRot.RecruitmentItem;

                        craftingJob.Settings = litSettings;
                    }
                    else if (craftingJob.Settings.GetType() == typeof(CraftingJobSettings) && data.Item3.TryGetNPCCraftDefaultSettings(out CraftingJobSettings craftSettings))
                    {
                        var cSettings = Activator.CreateInstance(craftSettings.GetType()) as CraftingJobSettings;


                        cSettings.BlockTypes       = craftSettings.BlockTypes;
                        cSettings.CraftingCooldown = craftSettings.CraftingCooldown;
                        cSettings.MaxCraftsPerHaul = craftSettings.MaxCraftsPerHaul;
                        cSettings.NPCType          = craftSettings.NPCType;
                        cSettings.NPCTypeKey       = craftSettings.NPCTypeKey;
                        cSettings.OnCraftedAudio   = craftSettings.OnCraftedAudio;
                        cSettings.RecruitmentItem  = craftSettings.RecruitmentItem;


                        craftingJob.Settings = cSettings;
                    }
                }

                data.Item1?.ApplyJobResearch();
            }