Skip to content

Commit

Permalink
Fixed knights throwing null ref exception when job is cleared.
Browse files Browse the repository at this point in the history
  • Loading branch information
JBurlison committed Jul 7, 2019
1 parent 5daf7f0 commit f02c3fd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
9 changes: 9 additions & 0 deletions Pandaros.Settlers/Pandaros.Settlers/AI/SettlerEvaluation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public static float SpawnChance(ColonyState state)
else
chance -= 0.2f;

if (state.ColonyRef.HappinessData.CachedHappiness > 100)
chance += 0.4f;
else if (state.ColonyRef.HappinessData.CachedHappiness > 50)
chance += 0.2f;
else if (state.ColonyRef.HappinessData.CachedHappiness < -50)
chance -= 0.4f;
else if (state.ColonyRef.HappinessData.CachedHappiness < 0)
chance -= 0.2f;

if (state.Difficulty != GameDifficulty.Easy && state.Difficulty != GameDifficulty.Normal)
if (state.ColonyRef.InSiegeMode ||
state.ColonyRef.LastSiegeModeSpawn != 0 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ public static class SettlerManager

public const int _NUMBEROFCRAFTSPERPERCENT = 200;
public const int _UPDATE_TIME = 10;
public static double IN_GAME_HOUR_IN_SECONDS = 3600 / TimeCycle.Settings.GameTimeScale;
public static double BED_LEAVE_HOURS = IN_GAME_HOUR_IN_SECONDS * 5;
public static double COLD_LEAVE_HOURS = IN_GAME_HOUR_IN_SECONDS * 5;
public static double HOT_LEAVE_HOURS = IN_GAME_HOUR_IN_SECONDS * 6;
public static double BED_LEAVE_HOURS = 5;
public static double COLD_LEAVE_HOURS = 5;
public static double HOT_LEAVE_HOURS = 6;
public static float _baseFoodPerHour;
public static double _updateTime;
public static double _magicUpdateTime = Time.SecondsSinceStartDouble + Random.Next(2, 5);
public static double _nextLaborerTime = Time.SecondsSinceStartDouble + Random.Next(2, 6);
public static double _nextbedTime = Time.SecondsSinceStartDouble + Random.Next(1, 2);
public static double _magicUpdateTime = TimeCycle.TotalHours + Random.Next(2, 5);
public static double _nextLaborerTime = TimeCycle.TotalHours + Random.Next(2, 6);
public static double _nextbedTime = TimeCycle.TotalHours + Random.Next(1, 2);

public static List<HealingOverTimeNPC> HealingSpells { get; } = new List<HealingOverTimeNPC>();
private static localization.LocalizationHelper _localizationHelper = new localization.LocalizationHelper("SettlerManager");
Expand Down Expand Up @@ -101,13 +100,13 @@ public static void OnUpdate()
if (ServerManager.ColonyTracker != null)
foreach (var colony in ServerManager.ColonyTracker.ColoniesByID.Values)
{
if (_magicUpdateTime < Time.SecondsSinceStartDouble)
if (_magicUpdateTime < TimeCycle.TotalHours)
{
foreach (var follower in colony.Followers)
{
var inv = SettlerInventory.GetSettlerInventory(follower);

if (inv.MagicItemUpdateTime < Time.SecondsSinceStartDouble)
if (inv.MagicItemUpdateTime < TimeCycle.TotalHours)
{
foreach (var item in inv.Armor)
if (item.Value.Id != 0 && ArmorFactory.ArmorLookup.TryGetValue(item.Value.Id, out var armor))
Expand Down Expand Up @@ -164,7 +163,7 @@ public static void OnUpdate()
}


if (_updateTime < Time.SecondsSinceStartDouble && colony.OwnerIsOnline())
if (_updateTime < TimeCycle.TotalHours && colony.OwnerIsOnline())
{
NPCBase lastNPC = null;

Expand Down Expand Up @@ -192,21 +191,17 @@ public static void OnUpdate()
UpdateFoodUse(cs);
}

if (_magicUpdateTime < Time.SecondsSinceStartDouble)
_magicUpdateTime = Time.SecondsSinceStartDouble + 1;
if (_magicUpdateTime < TimeCycle.TotalHours)
_magicUpdateTime = TimeCycle.TotalHours + 1;

if (_updateTime < Time.SecondsSinceStartDouble && TimeCycle.IsDay)
_updateTime = Time.SecondsSinceStartDouble + _UPDATE_TIME;
if (_updateTime < TimeCycle.TotalHours && TimeCycle.IsDay)
_updateTime = TimeCycle.TotalHours + _UPDATE_TIME;
}

[ModLoader.ModCallback(ModLoader.EModCallbackType.AfterWorldLoad, GameLoader.NAMESPACE + ".SettlerManager.AfterWorldLoad")]
public static void AfterWorldLoad()
{
_baseFoodPerHour = 1;
IN_GAME_HOUR_IN_SECONDS = 3600 / TimeCycle.Settings.GameTimeScale;
BED_LEAVE_HOURS = IN_GAME_HOUR_IN_SECONDS * 5;
COLD_LEAVE_HOURS = IN_GAME_HOUR_IN_SECONDS * 5;
HOT_LEAVE_HOURS = IN_GAME_HOUR_IN_SECONDS * 6;

foreach (var p in ServerManager.ColonyTracker.ColoniesByID.Values)
UpdateFoodUse(ColonyState.GetColonyState(p));
Expand Down Expand Up @@ -453,9 +448,9 @@ public static bool EvaluateSettlers(ColonyState state)
if (state.ColonyRef.OwnerIsOnline())
{
if (state.NextGenTime == 0)
state.NextGenTime = Time.SecondsSinceStartDouble + Random.Next(8, 16) * IN_GAME_HOUR_IN_SECONDS;
state.NextGenTime = TimeCycle.TotalHours + Random.Next(8, 16);

if (Time.SecondsSinceStartDouble > state.NextGenTime && state.ColonyRef.FollowerCount >= MAX_BUYABLE)
if (TimeCycle.TotalHours > state.NextGenTime && state.ColonyRef.FollowerCount >= MAX_BUYABLE)
{
var chance =
state.ColonyRef.TemporaryData.GetAsOrDefault(GameLoader.NAMESPACE + ".SettlerChance", 0f) +
Expand Down Expand Up @@ -538,7 +533,7 @@ public static bool EvaluateSettlers(ColonyState state)
}


state.NextGenTime = Time.SecondsSinceStartDouble + Random.Next(8, 16) * IN_GAME_HOUR_IN_SECONDS;
state.NextGenTime = TimeCycle.TotalHours + Random.Next(8, 16);

state.ColonyRef.SendCommonData();
}
Expand Down Expand Up @@ -664,7 +659,7 @@ private static bool EvaluateLaborers(ColonyState state)
{
var update = false;

if (TimeCycle.IsDay && Time.SecondsSinceStartDouble > _nextLaborerTime)
if (TimeCycle.IsDay && TimeCycle.TotalHours > _nextLaborerTime)
{
var unTrack = new List<NPCBase>();
var left = 0;
Expand Down Expand Up @@ -694,7 +689,7 @@ private static bool EvaluateLaborers(ColonyState state)
state.ColonyRef.SendCommonData();


_nextLaborerTime = Time.SecondsSinceStartDouble + Random.Next(4, 6) * IN_GAME_HOUR_IN_SECONDS * 24;
_nextLaborerTime = TimeCycle.TotalHours + Random.Next(4, 6);
}

return update;
Expand Down Expand Up @@ -750,11 +745,10 @@ private static bool EvaluateBeds(ColonyState state)

try
{
if (!TimeCycle.IsDay && Time.SecondsSinceStartDouble > _nextbedTime)
if (!TimeCycle.IsDay && TimeCycle.TotalHours > _nextbedTime)
{
// TODO Fix bed count
var remainingBeds = ServerManager.BlockEntityTracker.BedTracker.CalculateBedCount(state.ColonyRef) - state.ColonyRef.FollowerCount;
var left = 0;
var remainingBeds = ServerManager.BlockEntityTracker.BedTracker.CalculateBedCount(state.ColonyRef) - state.ColonyRef.FollowerCount;
var left = 0;

if (remainingBeds >= 0)
{
Expand Down Expand Up @@ -790,7 +784,7 @@ private static bool EvaluateBeds(ColonyState state)
state.ColonyRef.SendCommonData();
}

_nextbedTime = Time.SecondsSinceStartDouble + Random.Next(5, 8) * IN_GAME_HOUR_IN_SECONDS * 24;
_nextbedTime = TimeCycle.TotalHours + Random.Next(5, 8);
}
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace Pandaros.Settlers.Entities
public class PlayerState
{
private static readonly Dictionary<Players.Player, PlayerState> _playerStates = new Dictionary<Players.Player, PlayerState>();
private static string _Enviorment = GameLoader.NAMESPACE + ".Enviorment";
private static double MagicItemUpdateTime = Time.SecondsSinceStartDouble;

public PlayerState(Players.Player p)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public static SettlerInventory GetSettlerInventory(NPCBase npc)
{
SettlerInventory inv = null;

if (npc == null)
return inv;

if (npc.CustomData == null)
npc.CustomData = new JSONNode();

Expand Down
2 changes: 1 addition & 1 deletion Pandaros.Settlers/Pandaros.Settlers/Jobs/Knight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public void SetNPC(NPCBase npc)
{
UsedNPC = npc;
_inv = SettlerInventory.GetSettlerInventory(npc);
_stock = npc.Colony.Stockpile;
_stock = npc?.Colony?.Stockpile;
}

public void OnNPCCouldNotPathToGoal()
Expand Down

0 comments on commit f02c3fd

Please sign in to comment.