Skip to content

Commit

Permalink
Unreachable Job Fix (#1580)
Browse files Browse the repository at this point in the history
* Jobs now keep track of what characters can't reach them.
Characters will correctly abandon jobs they can't reach.

* Deleted debugging lines.

* Resets the unreachable state for jobs when furniture is removed.

* Fixed the deletion of jobs when unreachable.

* Changed the Character.JobState to null instead of idle when unable to find a job. reduces time between job calculations for unreachable jobs.

* Commenting Corrections.

* Added handling for characters that can't reach inventory.

* Jobs now do not get picked up by characters if they are unable to reach any inventory that is required.

* Jobs now get re-added to the queue when new inventory is made.

* Refactored JobObjectType to Type to avoid redundancy.

* Changed Steel Wall's type to be more in line with the rest of the document.

* Unreachable inventory now gets properly handled.
  • Loading branch information
Geoffrotism authored and bjubes committed Nov 8, 2016
1 parent c4f9a68 commit 07b1e81
Show file tree
Hide file tree
Showing 24 changed files with 363 additions and 234 deletions.
Binary file modified Assets/Plugins/Net20/Newtonsoft.Json.dll.mdb
Binary file not shown.
5 changes: 5 additions & 0 deletions Assets/Plugins/Net20/Newtonsoft.Json.dll.mdb.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 9 additions & 12 deletions Assets/Plugins/x86_64/fmod.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Assets/Resources/Prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Assets/Scripts/Controllers/InputOutput/BuildModeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void DoBuild(Tile tile)
Debug.ULogErrorChannel("BuildModeController", "There is no furniture job prototype for '" + furnitureType + "'");
job = new Job(tile, furnitureType, World.Current.FurnitureManager.ConstructJobCompleted, 0.1f, null, Job.JobPriority.High);
job.adjacent = true;
job.JobDescription = "job_build_" + furnitureType + "_desc";
job.Description = "job_build_" + furnitureType + "_desc";
}

Furniture furnituteToBuild = PrototypeManager.Furniture.Get(furnitureType).Clone();
Expand Down Expand Up @@ -233,15 +233,15 @@ public void DoBuild(Tile tile)
{
Debug.ULogErrorChannel("BuildModeController", "There is no furniture job prototype for '" + utilityType + "'");
job = new Job(tile, utilityType, World.Current.UtilityManager.ConstructJobCompleted, 0.1f, null, Job.JobPriority.High);
job.JobDescription = "job_build_" + utilityType + "_desc";
job.Description = "job_build_" + utilityType + "_desc";
}

job.buildablePrototype = PrototypeManager.Utility.Get(utilityType);

// Add the job to the queue or build immediately if in dev mode
if (Settings.GetSetting("DialogBoxSettings_developerModeToggle", false))
{
World.Current.UtilityManager.PlaceUtility(job.JobObjectType, job.tile);
World.Current.UtilityManager.PlaceUtility(job.Type, job.tile);
}
else
{
Expand Down
14 changes: 7 additions & 7 deletions Assets/Scripts/Controllers/Sprites/JobSpriteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override void RemoveAll()

protected override void OnCreated(Job job)
{
if (job.JobTileType == null && job.JobObjectType == null)
if (job.JobTileType == null && job.Type == null)
{
// This job doesn't really have an associated sprite with it, so no need to render.
return;
Expand All @@ -81,7 +81,7 @@ protected override void OnCreated(Job job)
// Add our tile/GO pair to the dictionary.
objectGameObjectMap.Add(job, job_go);

job_go.name = "JOB_" + job.JobObjectType + "_" + job.tile.X + "_" + job.tile.Y + "_" + job.tile.Z;
job_go.name = "JOB_" + job.Type + "_" + job.tile.X + "_" + job.tile.Y + "_" + job.tile.Z;
job_go.transform.SetParent(objectParent.transform, true);

SpriteRenderer sr = job_go.AddComponent<SpriteRenderer>();
Expand All @@ -94,13 +94,13 @@ protected override void OnCreated(Job job)
sr.sprite = SpriteManager.GetSprite("Tile", "Solid");
sr.color = new Color32(128, 255, 128, 192);
}
else if (job.JobDescription.Contains("deconstruct"))
else if (job.Description.Contains("deconstruct"))
{
sr.sprite = SpriteManager.GetSprite("UI", "CursorCircle");
sr.color = Color.red;
job_go.transform.position = job.tile.Vector3;
}
else if (job.JobDescription.Contains("mine"))
else if (job.Description.Contains("mine"))
{
sr.sprite = SpriteManager.GetSprite("UI", "MiningIcon");
sr.color = new Color(1, 1, 1, 0.25f);
Expand All @@ -118,13 +118,13 @@ protected override void OnCreated(Job job)
if (job.buildablePrototype.GetType().ToString() == "Furniture")
{
Furniture furnitureToBuild = (Furniture)job.buildablePrototype;
sr.sprite = fsc.GetSpriteForFurniture(job.JobObjectType);
sr.sprite = fsc.GetSpriteForFurniture(job.Type);
job_go.transform.position = job.tile.Vector3 + ImageUtils.SpritePivotOffset(sr.sprite, furnitureToBuild.Rotation);
job_go.transform.Rotate(0, 0, furnitureToBuild.Rotation);
}
else if (job.buildablePrototype.GetType().ToString() == "Utility")
{
sr.sprite = usc.GetSpriteForUtility(job.JobObjectType);
sr.sprite = usc.GetSpriteForUtility(job.Type);
job_go.transform.position = job.tile.Vector3 + ImageUtils.SpritePivotOffset(sr.sprite);
}

Expand All @@ -134,7 +134,7 @@ protected override void OnCreated(Job job)
sr.sortingLayerName = "Jobs";

// FIXME: This hardcoding is not ideal! <== Understatement
if (job.JobObjectType == "Door")
if (job.Type == "Door")
{
// By default, the door graphic is meant for walls to the east & west
// Check to see if we actually have a wall north/south, and if so
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Models/Area/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ private void TickFixedFrequency(float deltaTime)
}

/// <summary>
/// Called when a furniture is created so that we can regenerate the til graph.
/// Called when a furniture is created so that we can regenerate the tile graph.
/// </summary>
/// <param name="furniture">Furniture.</param>
private void OnFurnitureCreated(Furniture furniture)
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Models/Buildable/Components/Workshop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private void ConsumeInventories(List<KeyValuePair<Tile, int>> flaggedForTaking)
private void PlaceInventoryToWorkshopInput(Job job)
{
job.CancelJob();
foreach (Inventory heldInventory in job.HeldInventory.Values)
foreach (Inventory heldInventory in job.DeliveredItems.Values)
{
if (heldInventory.StackSize > 0)
{
Expand Down Expand Up @@ -354,7 +354,7 @@ private void HaulingJobForInputs(ProductionChain prodChain)
false,
false);

job.JobDescription = string.Format("Hauling '{0}' to '{1}'", desiredInv, ParentFurniture.Name);
job.Description = string.Format("Hauling '{0}' to '{1}'", desiredInv, ParentFurniture.Name);
job.OnJobWorked += PlaceInventoryToWorkshopInput;
ParentFurniture.Jobs.Add(job);
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Models/Buildable/Furniture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ public void ReadXmlBuildingJob(XmlReader reader)
jobTime,
items.ToArray(),
Job.JobPriority.High);
job.JobDescription = "job_build_" + Type + "_desc";
job.Description = "job_build_" + Type + "_desc";

PrototypeManager.FurnitureConstructJob.Set(job);
}
Expand Down Expand Up @@ -838,7 +838,7 @@ public void ReadXmlDeconstructJob(XmlReader reader)
jobTime,
null,
Job.JobPriority.High);
job.JobDescription = "job_deconstruct_" + Type + "_desc";
job.Description = "job_deconstruct_" + Type + "_desc";
job.adjacent = true;

PrototypeManager.FurnitureDeconstructJob.Set(job);
Expand Down
8 changes: 6 additions & 2 deletions Assets/Scripts/Models/Buildable/FurnitureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ public Furniture PlaceFurniture(Furniture prototype, Tile tile, bool doRoomFlood
furnitures.Add(furniture);
furnituresVisible.Add(furniture);

// Do we need to recalculate our rooms?
// Do we need to recalculate our rooms/reachability for other jobs?
if (doRoomFloodFill && furniture.RoomEnclosure)
{
World.Current.RoomManager.DoRoomFloodFill(furniture.Tile, true);
World.Current.jobQueue.ReevaluateReachability();
}

if (Created != null)
Expand Down Expand Up @@ -160,7 +161,7 @@ public int CountWithType(string type)
}

/// <summary>
/// Reuturns a list of furniture using the given filter function.
/// Returns a list of furniture using the given filter function.
/// </summary>
/// <returns>A list of furnitures.</returns>
/// <param name="filterFunc">The filter function.</param>
Expand Down Expand Up @@ -308,5 +309,8 @@ private void OnRemoved(Furniture furniture)
{
furnituresVisible.Remove(furniture);
}

// Movement to jobs might have been opened, let's move jobs back into the queue to be re-evaluated.
World.Current.jobQueue.ReevaluateReachability();
}
}
2 changes: 1 addition & 1 deletion Assets/Scripts/Models/Buildable/TileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private void ReadBuildingJob(XmlReader parentReader)
false,
true)
{
JobDescription = "job_build_floor_" + this
Description = "job_build_floor_" + this
};
}
}
4 changes: 2 additions & 2 deletions Assets/Scripts/Models/Buildable/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public void ReadXmlBuildingJob(XmlReader reader)
jobTime,
items.ToArray(),
Job.JobPriority.High);
job.JobDescription = "job_build_" + Type + "_desc";
job.Description = "job_build_" + Type + "_desc";
PrototypeManager.UtilityConstructJob.Set(job);
}

Expand Down Expand Up @@ -691,7 +691,7 @@ private void ReadXmlDeconstructJob(XmlReader reader)
jobTime,
null,
Job.JobPriority.High);
job.JobDescription = "job_deconstruct_" + Type + "_desc";
job.Description = "job_deconstruct_" + Type + "_desc";
PrototypeManager.UtilityDeconstructJob.Set(job);
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Models/Buildable/UtilityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Utility PlaceUtility(Utility prototype, Tile tile, bool doRoomFloodFill =
/// <param name="job">The completed job.</param>
public void ConstructJobCompleted(Job job)
{
PlaceUtility(job.JobObjectType, job.tile);
PlaceUtility(job.Type, job.tile);

// FIXME: I don't like having to manually and explicitly set
// flags that preven conflicts. It's too easy to forget to set/clear them!
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Models/Character/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public void Update(float deltaTime)
else
{
// TODO: Lack of job states should be more interesting. Maybe go to the pub and have a pint?
SetState(new IdleState(this));
SetState(new IdleState(this));
}
}
}
Expand Down Expand Up @@ -442,7 +442,7 @@ public string GetJobDescription()
return "job_no_job_desc";
}

return MyJob.JobDescription;
return MyJob.Description;
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Models/Character/CharacterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
[MoonSharpUserData]
public class CharacterManager : IEnumerable<Character>
{
private List<Character> characters;
public List<Character> characters;

/// <summary>
/// Initializes a new instance of the <see cref="CharacterManager"/> class.
Expand Down
13 changes: 9 additions & 4 deletions Assets/Scripts/Models/Inventory/InventoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ public bool PlaceInventory(Tile tile, Inventory inventory)
{
// The tile did not accept the inventory for whatever reason, therefore stop.
return false;

// TODO: Geoffrotism. Is this where we would hook in to handle inventory not being able to be placed in a tile.
}

CleanupInventory(inventory);

// We may also created a new stack on the tile, if the startTile was previously empty.
// We may also have to create a new stack on the tile, if the startTile was previously empty.
if (tileWasEmpty)
{
if (Inventories.ContainsKey(tile.Inventory.Type) == false)
Expand Down Expand Up @@ -152,12 +154,12 @@ public bool PlaceInventory(Job job, Character character)
}

// Check that there is a target to transfer to
if (job.HeldInventory.ContainsKey(sourceInventory.Type) == false)
if (job.DeliveredItems.ContainsKey(sourceInventory.Type) == false)
{
job.HeldInventory[sourceInventory.Type] = new Inventory(sourceInventory.Type, 0, sourceInventory.MaxStackSize);
job.DeliveredItems[sourceInventory.Type] = new Inventory(sourceInventory.Type, 0, sourceInventory.MaxStackSize);
}

Inventory targetInventory = job.HeldInventory[sourceInventory.Type];
Inventory targetInventory = job.DeliveredItems[sourceInventory.Type];
int transferAmount = Mathf.Min(targetInventory.MaxStackSize - targetInventory.StackSize, sourceInventory.StackSize);

sourceInventory.StackSize -= transferAmount;
Expand Down Expand Up @@ -367,6 +369,9 @@ private void InvokeInventoryCreated(Inventory inventory)
if (handler != null)
{
handler(inventory);

// Let the JobQueue know there is new inventory available.
World.Current.jobQueue.ReevaluateReachability();
}
}

Expand Down
Loading

0 comments on commit 07b1e81

Please sign in to comment.