private static void AddNewSettlers(double addCount, int numbSkilled, ColonyState state) { var reason = string.Format(SettlerReasoning.GetSettleReason(), addCount); if (numbSkilled > 0) { if (numbSkilled == 1) { reason += string.Format(" {0} of them is skilled!", numbSkilled); } else { reason += string.Format(" {0} of them are skilled!", numbSkilled); } } PandaChat.Send(state.ColonyRef, reason, ChatColor.magenta); for (var i = 0; i < addCount; i++) { var newGuy = new NPCBase(state.ColonyRef, state.ColonyRef.GetRandomBanner().Position); NPCTracker.Add(newGuy); state.ColonyRef.RegisterNPC(newGuy); SettlerInventory.GetSettlerInventory(newGuy); newGuy.CustomData.SetAs(ISSETTLER, true); if (i <= numbSkilled) { newGuy.CustomData.SetAs(GameLoader.ALL_SKILLS, Random.Next(1, 10) * 0.002f); } ModLoader.Callbacks.OnNPCRecruited.Invoke(newGuy); } }
public static void OnPlayerConnectedEarly(Players.Player p) { if (p.IsConnected && !Configuration.OfflineColonies) { foreach (Colony c in p.Colonies) { if (c.Owners.Count(own => own.IsConnected) == 1) { var file = $"{GameLoader.GAMEDATA_FOLDER}/savegames/{ServerManager.WorldName}/NPCArchive/{c.ColonyID}.json"; if (File.Exists(file) && JSON.Deserialize(file, out var followersNode, false)) { File.Delete(file); PandaLogger.Log(ChatColor.cyan, $"Player {p.ID.steamID} is reconnected. Restoring Colony."); foreach (var node in followersNode.LoopArray()) { try { node.SetAs("id", GetAIID()); var npc = new NPCBase(c, node); c.RegisterNPC(npc); NPCTracker.Add(npc); ModLoader.TriggerCallbacks(ModLoader.EModCallbackType.OnNPCLoaded, npc, node); foreach (var job in new List <IJob>(c.JobFinder.JobsData.OpenJobs)) { if (node.TryGetAs("JobPoS", out JSONNode pos) && job.GetJobLocation() == (Vector3Int)pos) { if (job.IsValid && job.NeedsNPC) { npc.TakeJob(job); c.JobFinder.Remove(job); } break; } } } catch (Exception ex) { PandaLogger.LogError(ex); } } JSON.Serialize(file, new JSONNode(NodeType.Array)); c.JobFinder.Update(); c.SendCommonData(); } } } } }
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)); } } } }
public bool TryDoCommand(Players.Player causedBy, string chattext, List <string> splits) { if (!splits[0].Equals("/spawnnpc")) { return(false); } if (!PermissionsManager.CheckAndWarnPermission(causedBy, AntiGrief.MOD_PREFIX + "npcandbeds")) { return(true); } Match m = Regex.Match(chattext, @"/spawnnpc (?<amount>\d+) ?(?<player>['].+[']|[^ ]+)?"); if (!m.Success) { Chat.Send(causedBy, "Syntax: /spawnnpc {number} [targetplayer]"); return(true); } int amount = 0; if (!int.TryParse(m.Groups["amount"].Value, out amount) || amount <= 0) { Chat.Send(causedBy, "Number should be > 0"); return(true); } Players.Player target = causedBy; string error; if (!m.Groups["player"].Value.Equals("")) { if (!PlayerHelper.TryGetPlayer(m.Groups["player"].Value, out target, out error)) { Chat.Send(causedBy, "Could not find target: {error}"); } } Colony colony = target.ActiveColony; if (colony == null) { Chat.Send(target, "You need to be at an active colony to spawn beds"); if (causedBy != target) { Chat.Send(causedBy, " {target} has no active colony"); } return(true); } BannerTracker.Banner banner = colony.GetClosestBanner(causedBy.VoxelPosition); if (banner == null) { Chat.Send(causedBy, "No banners found for the active colony"); return(true); } for (int i = 0; i < amount; i++) { NPCBase npc = new NPCBase(colony, banner.Position); NPCTracker.Add(npc); colony.RegisterNPC(npc); ModLoader.Callbacks.OnNPCRecruited.Invoke(npc); } Chat.Send(causedBy, $"Spawned {amount} colonists"); return(true); }
public static void PressButton(ButtonPressCallbackData data) { if ((!data.ButtonIdentifier.Contains(".RecruitButton") && !data.ButtonIdentifier.Contains(".FireButton") && !data.ButtonIdentifier.Contains(".SwapJob") && !data.ButtonIdentifier.Contains(".ColonyToolMainMenu") && !data.ButtonIdentifier.Contains(".KillFired") && !data.ButtonIdentifier.Contains(".AutoRecruitment")) || data.Player.ActiveColony == null) { return; } Dictionary <string, JobCounts> jobCounts = GetJobCounts(data.Player.ActiveColony); Dictionary <Players.Player, string> AutoRecruit_Opt = new Dictionary <Players.Player, string>(); 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(Nach0Config.NAMESPACE + ".FiredJobName", job.Key); menu.LocalStorage.SetAs(Nach0Config.NAMESPACE + ".FiredJobCount", count); NetworkMenuManager.SendServerPopup(data.Player, menu); break; } } } else if (data.ButtonIdentifier.Contains(".KillFired")) { var firedJob = data.Storage.GetAs <string>(Nach0Config.NAMESPACE + ".FiredJobName"); var count = data.Storage.GetAs <int>(Nach0Config.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(".SwapJob")) { var firedJob = data.Storage.GetAs <string>(Nach0Config.NAMESPACE + ".FiredJobName"); var count = data.Storage.GetAs <int>(Nach0Config.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.LocalizeOrDefault("Notenoughfood", data.Player), ChatColor.red); Chat.Send(data.Player, "<color=red>Notenoughfood</color>"); break; } else { var newGuy = new NPCBase(data.Player.ActiveColony, data.Player.ActiveColony.GetClosestBanner(new Vector3Int(data.Player.Position)).Position); data.Player.ActiveColony.RegisterNPC(newGuy); //SettlerInventory.GetSettlerInventory(newGuy); NPCTracker.Add(newGuy); //ModLoader.TriggerCallbacks(ModLoader.EModCallbackType.OnNPCRecruited, 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)); } } } else if (data.ButtonIdentifier.Contains(".AutoRecruitment")) { if (AutoRecruit_Opt[data.Player].Equals("on")) { AutoRecruit_Opt[data.Player] = "off"; Chat.Send(data.Player, "<color=blue>Auto Recruitment is now " + AutoRecruit_Opt[data.Player] + "</color>"); } else { AutoRecruit_Opt[data.Player] = "on"; Chat.Send(data.Player, "<color=blue>Auto Recruitment is now " + AutoRecruit_Opt[data.Player] + "</color>"); } } }
public static bool EvaluateSettlers(ColonyState state) { var update = false; if (state.ColonyRef.OwnerIsOnline()) { if (state.NextGenTime == 0) { state.NextGenTime = Time.SecondsSinceStartDouble + Random.Next(8, 16) * IN_GAME_HOUR_IN_SECONDS; } if (Time.SecondsSinceStartDouble > state.NextGenTime && state.ColonyRef.FollowerCount >= MAX_BUYABLE) { var chance = state.ColonyRef.TemporaryData.GetAsOrDefault(PandaResearch.GetResearchKey(PandaResearch.SettlerChance), 0f) + state.Difficulty.AdditionalChance; chance += SettlerEvaluation.SpawnChance(state); var rand = Random.NextFloat(); if (chance > rand) { var addCount = Math.Floor(state.MaxPerSpawn * chance); // if we lost alot of colonists add extra to help build back up. if (state.ColonyRef.FollowerCount < state.HighestColonistCount) { var diff = state.HighestColonistCount - state.ColonyRef.FollowerCount; addCount += Math.Floor(diff * .25); } try { var skillChance = state.ColonyRef.TemporaryData.GetAsOrDefault(PandaResearch.GetResearchKey(PandaResearch.SkilledLaborer), 0f); var numbSkilled = 0; rand = Random.NextFloat(); try { if (skillChance > rand) { numbSkilled = Pipliz.Random.Next(1, 2 + Pipliz.Math.RoundToInt(state.ColonyRef.TemporaryData.GetAsOrDefault(PandaResearch.GetResearchKey(PandaResearch.NumberSkilledLaborer), 0f))); } } catch (Exception ex) { PandaLogger.Log("NumberSkilledLaborer"); PandaLogger.LogError(ex); } if (addCount > 0) { if (addCount > 30) { addCount = 30; } var reason = string.Format(SettlerReasoning.GetSettleReason(), addCount); if (numbSkilled > 0) { if (numbSkilled == 1) { reason += string.Format(" {0} of them is skilled!", numbSkilled); } else { reason += string.Format(" {0} of them are skilled!", numbSkilled); } } PandaChat.Send(state.ColonyRef, reason, ChatColor.magenta); for (var i = 0; i < addCount; i++) { var newGuy = new NPCBase(state.ColonyRef, state.ColonyRef.GetRandomBanner().Position); NPCTracker.Add(newGuy); state.ColonyRef.RegisterNPC(newGuy); SettlerInventory.GetSettlerInventory(newGuy); newGuy.CustomData.SetAs(ISSETTLER, true); if (i <= numbSkilled) { newGuy.CustomData.SetAs(GameLoader.ALL_SKILLS, Random.Next(1, 10) * 0.002f); } update = true; ModLoader.TriggerCallbacks(ModLoader.EModCallbackType.OnNPCRecruited, newGuy); } } } catch (Exception ex) { PandaLogger.Log("SkilledLaborer"); PandaLogger.LogError(ex); } if (state.ColonyRef.FollowerCount > state.HighestColonistCount) { state.HighestColonistCount = state.ColonyRef.FollowerCount; } } state.NextGenTime = Time.SecondsSinceStartDouble + Random.Next(8, 16) * IN_GAME_HOUR_IN_SECONDS; state.ColonyRef.SendCommonData(); } } return(update); }