Skip to content

Commit

Permalink
Orders tab with new Mine action (#1640)
Browse files Browse the repository at this point in the history
* cleanup and some localizations

* new OrderAction (Build, Deconstruct, Mine), more generic implementation

* removed unused code

* when in developer mode, completes job immediately
  • Loading branch information
dusho authored and bjubes committed Dec 12, 2016
1 parent 7a1fecb commit 88af2d0
Show file tree
Hide file tree
Showing 34 changed files with 1,113 additions and 440 deletions.
112 changes: 112 additions & 0 deletions Assets/Editor/UnitTests/Models/OrderActionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#region License
// ====================================================
// Project Porcupine Copyright(C) 2016 Team Porcupine
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using NUnit.Framework;
using ProjectPorcupine.OrderActions;

public class OrderActionTest
{
[Test]
public void TestOrderActionCreation()
{
string inputXML = @"<OrderAction type='Build' >
<Job time='1' />
<Inventory type='Steel Plate' amount='5' />
<Inventory type='Copper Plate' amount='2' />
</OrderAction>";

XmlReader reader = new XmlTextReader(new StringReader(inputXML));
reader.Read();

OrderAction component = ProjectPorcupine.OrderActions.OrderAction.Deserialize(reader);

Assert.NotNull(component);
Assert.AreEqual("Build", component.Type);
}

[Test]
public void TestBuildXmlSerialization()
{
Build buildOrder = new Build();

buildOrder.JobInfo = new OrderAction.JobInformation() { Time = 1 };
buildOrder.Inventory = new List<OrderAction.InventoryInfo>()
{
new OrderAction.InventoryInfo()
{
Type = "Steel Plate",
Amount = 5
},
new OrderAction.InventoryInfo()
{
Type = "Copper Plate",
Amount = 2
}
};

// serialize
StringWriter writer = new StringWriter();
XmlSerializer serializer = new XmlSerializer(typeof(OrderAction), new Type[] { typeof(Build) });

serializer.Serialize(writer, buildOrder);

StringReader sr = new StringReader(writer.ToString());

// if you want to dump file to disk for visual check, uncomment this
////File.WriteAllText("Build.xml", writer.ToString());

// deserialize
Build deserializedBuildOrder = (Build)serializer.Deserialize(sr);

Assert.NotNull(deserializedBuildOrder);
Assert.AreEqual("Steel Plate", deserializedBuildOrder.Inventory[0].Type);
}

[Test]
public void TestDeconstructXmlSerialization()
{
Deconstruct deconstructOrder = new Deconstruct();

deconstructOrder.JobInfo = new OrderAction.JobInformation() { Time = 1 };
deconstructOrder.Inventory = new List<OrderAction.InventoryInfo>()
{
new OrderAction.InventoryInfo()
{
Type = "Steel Plate",
Amount = 5
},
new OrderAction.InventoryInfo()
{
Type = "Copper Plate",
Amount = 2
}
};

// serialize
StringWriter writer = new StringWriter();
XmlSerializer serializer = new XmlSerializer(typeof(OrderAction), new Type[] { typeof(Deconstruct) });

serializer.Serialize(writer, deconstructOrder);

StringReader sr = new StringReader(writer.ToString());

// if you want to dump file to disk for visual check, uncomment this
////File.WriteAllText("Deconstruct.xml", writer.ToString());

// deserialize
Deconstruct deserializedDeconstructOrder = (Deconstruct)serializer.Deserialize(sr);

Assert.NotNull(deserializedDeconstructOrder);
Assert.AreEqual("Copper Plate", deserializedDeconstructOrder.Inventory[1].Type);
}
}
12 changes: 12 additions & 0 deletions Assets/Editor/UnitTests/Models/OrderActionTest.cs.meta

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

14 changes: 1 addition & 13 deletions Assets/Resources/UI/MenuLeft/ConstructionMenu.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ GameObject:
- 222: {fileID: 222000013910140856}
- 114: {fileID: 114000012491780442}
- 114: {fileID: 114000011946741832}
- 114: {fileID: 114000011449879948}
m_Layer: 5
m_Name: ConstructionMenu
m_TagString: Untagged
Expand Down Expand Up @@ -405,7 +404,7 @@ MonoBehaviour:
m_OnValueChanged:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 114000011449879948}
- m_Target: {fileID: 0}
m_MethodName: FilterTextChanged
m_Mode: 0
m_Arguments:
Expand Down Expand Up @@ -437,17 +436,6 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_ShowMaskGraphic: 0
--- !u!114 &114000011449879948
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1000012915241740}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 437c972c83d2a7941bf45b10959021ba, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &114000011803155106
MonoBehaviour:
m_ObjectHideFlags: 1
Expand Down
139 changes: 95 additions & 44 deletions Assets/Scripts/Controllers/InputOutput/BuildModeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#endregion
using System.Collections.Generic;
using System.Linq;
using ProjectPorcupine.OrderActions;
using ProjectPorcupine.Rooms;

public enum BuildMode
Expand All @@ -16,7 +17,8 @@ public enum BuildMode
ROOMBEHAVIOR,
FURNITURE,
UTILITY,
DECONSTRUCT
DECONSTRUCT,
MINE
}

public class BuildModeController
Expand Down Expand Up @@ -48,7 +50,7 @@ public void SetMouseController(MouseController currentMouseController)

public bool IsObjectDraggable()
{
if (buildMode == BuildMode.FLOOR || buildMode == BuildMode.DECONSTRUCT || buildMode == BuildMode.UTILITY)
if (buildMode == BuildMode.FLOOR || buildMode == BuildMode.DECONSTRUCT || buildMode == BuildMode.UTILITY || buildMode == BuildMode.MINE)
{
// floors are draggable
return true;
Expand Down Expand Up @@ -108,6 +110,12 @@ public void SetMode_Deconstruct()
mouseController.StartBuildMode();
}

public void SetMode_Mine()
{
buildMode = BuildMode.MINE;
mouseController.StartBuildMode();
}

public void DoBuild(Tile tile)
{
if (buildMode == BuildMode.ROOMBEHAVIOR)
Expand Down Expand Up @@ -143,13 +151,13 @@ public void DoBuild(Tile tile)
// Create a job for it to be build
Job job;

if (PrototypeManager.FurnitureConstructJob.Has(furnitureType))
Furniture toBuildProto = PrototypeManager.Furniture.Get(furnitureType);
OrderAction orderAction = toBuildProto.GetOrderAction<Build>();
if (orderAction != null)
{
// Make a clone of the job prototype
job = PrototypeManager.FurnitureConstructJob.Get(furnitureType).Clone();

// Assign the correct tile.
job.tile = tile;
job = orderAction.CreateJob(tile, furnitureType);
//// this is here so OrderAction can be used for utility as well as furniture
job.OnJobCompleted += (theJob) => World.Current.FurnitureManager.ConstructJobCompleted(theJob);
}
else
{
Expand All @@ -159,14 +167,14 @@ public void DoBuild(Tile tile)
job.Description = "job_build_" + furnitureType + "_desc";
}

Furniture furnituteToBuild = PrototypeManager.Furniture.Get(furnitureType).Clone();
furnituteToBuild.SetRotation(CurrentPreviewRotation);
job.buildablePrototype = furnituteToBuild;
Furniture furnitureToBuild = PrototypeManager.Furniture.Get(furnitureType).Clone();
furnitureToBuild.SetRotation(CurrentPreviewRotation);
job.buildablePrototype = furnitureToBuild;

// Add the job to the queue or build immediately if in Dev mode
if (CommandSettings.DeveloperModeToggle)
{
World.Current.FurnitureManager.PlaceFurniture(furnituteToBuild, job.tile);
{
World.Current.FurnitureManager.PlaceFurniture(furnitureToBuild, job.tile);
}
else
{
Expand Down Expand Up @@ -219,13 +227,13 @@ public void DoBuild(Tile tile)
// Create a job for it to be build
Job job;

if (PrototypeManager.UtilityConstructJob.Has(utilityType))
Utility toBuildProto = PrototypeManager.Utility.Get(utilityType);
OrderAction orderAction = toBuildProto.GetOrderAction<Build>();
if (orderAction != null)
{
// Make a clone of the job prototype
job = PrototypeManager.UtilityConstructJob.Get(utilityType).Clone();

// Assign the correct tile.
job.tile = tile;
job = orderAction.CreateJob(tile, utilityType);
//// this is here so OrderAction can be used for utility as well as furniture
job.OnJobCompleted += (theJob) => World.Current.UtilityManager.ConstructJobCompleted(theJob);
}
else
{
Expand Down Expand Up @@ -293,31 +301,9 @@ public void DoBuild(Tile tile)
if (tile.Furniture != null && (canDeconstructAll || tile.Furniture.HasTypeTag("Non-deconstructible") == false))
{
// check if this is a WALL neighbouring a pressured and pressureless environment, and if so, bail
if (tile.Furniture.HasTypeTag("Wall"))
{
Tile[] neighbors = tile.GetNeighbours(); // diagOkay??
int pressuredNeighbors = 0;
int vacuumNeighbors = 0;
foreach (Tile neighbor in neighbors)
{
if (neighbor != null && neighbor.Room != null)
{
if (neighbor.Room.IsOutsideRoom() || MathUtilities.IsZero(neighbor.Room.GetTotalGasPressure()))
{
vacuumNeighbors++;
}
else
{
pressuredNeighbors++;
}
}
}

if (vacuumNeighbors > 0 && pressuredNeighbors > 0)
{
UnityDebugger.Debugger.Log("BuildModeController", "Someone tried to deconstruct a wall between a pressurized room and vacuum!");
return;
}
if (IsTilePartOfPressuredRoom(tile))
{
return;
}

tile.Furniture.SetDeconstructJob();
Expand All @@ -331,6 +317,38 @@ public void DoBuild(Tile tile)
tile.Utilities.Last().Value.SetDeconstructJob();
}
}
else if (buildMode == BuildMode.MINE)
{
if (tile.Furniture != null)
{
Job existingMineJob;
bool hasMineJob = tile.Furniture.Jobs.HasJobWithPredicate(x => x.OrderName == typeof(Mine).Name, out existingMineJob);
if (!hasMineJob)
{
OrderAction mineAction = tile.Furniture.GetOrderAction<Mine>();
if (mineAction != null)
{
// check if this is a WALL neighbouring a pressured and pressureless environment, and if so, bail
if (IsTilePartOfPressuredRoom(tile))
{
return;
}

Job job = mineAction.CreateJob(tile, null);
if (CommandSettings.DeveloperModeToggle)
{
// complete job right away, needs buildable
job.buildable = tile.Furniture;
job.DoWork(0);
}
else
{
tile.Furniture.Jobs.Add(job);
}
}
}
}
}
else
{
UnityDebugger.Debugger.LogError("BuildModeController", "UNIMPLEMENTED BUILD MODE");
Expand Down Expand Up @@ -392,6 +410,39 @@ public bool DoesSameUtilityTypeAlreadyExist(string type, Tile tile)
return tile.Utilities.ContainsKey(proto.Name);
}

private bool IsTilePartOfPressuredRoom(Tile tile)
{
// check if this is a WALL neighbouring a pressured and pressureless environment, and if so, bail
if (tile.Furniture.HasTypeTag("Wall"))
{
Tile[] neighbors = tile.GetNeighbours(); // diagOkay??
int pressuredNeighbors = 0;
int vacuumNeighbors = 0;
foreach (Tile neighbor in neighbors)
{
if (neighbor != null && neighbor.Room != null)
{
if (neighbor.Room.IsOutsideRoom() || MathUtilities.IsZero(neighbor.Room.GetTotalGasPressure()))
{
vacuumNeighbors++;
}
else
{
pressuredNeighbors++;
}
}
}

if (vacuumNeighbors > 0 && pressuredNeighbors > 0)
{
UnityDebugger.Debugger.Log("BuildModeController", "Someone tried to deconstruct a wall between a pressurized room and vacuum!");
return true;
}
}

return false;
}

// Rotate the preview furniture to the left.
private void RotateFurnitireLeft()
{
Expand Down
Loading

0 comments on commit 88af2d0

Please sign in to comment.