Big project cleanup with overhaul of file responsibilities (KISS) and code (DRY, YAGNI)

This commit is contained in:
2026-05-14 11:17:02 +02:00
parent bd6cdeb97b
commit 300c8f5a42
54 changed files with 2030 additions and 1745 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ public class CraftNode : ProgramNode
if (amount <= 0)
{
lastExecutionMessage = "Amount has to be atleast 1";
lastExecutionMessage = "Amount has to be at least 1";
return NodeResult.FAILURE;
}
+113 -76
View File
@@ -4,90 +4,127 @@ using Godot;
public class ExploreNode : ProgramNode
{
public Vector3 startPosition;
public Vector3I targetPosition;
public List<Vector3> pathPoints;
public ExploreNode()
{
DisplayText = "Explore";
}
public override NodeResult Execute(Robot robot, double delta)
{
if (pathPoints == null)
{
int safetyCounter = 0;
int layerRange = Math.Max(GameData.lowestLayer, 1);
while (true)
{
targetPosition = new Vector3I(GameData.rand.Next(GameData.layerSize), GameData.rand.Next(layerRange), GameData.rand.Next(GameData.layerSize));
if (!GameData.map[targetPosition.Y].tiles[targetPosition.X, targetPosition.Z].wasVisited) break;
safetyCounter++;
if (safetyCounter > Math.Pow(GameData.layerSize, 2) * 2)
{
lastExecutionMessage = "No tiles left to explore";
return NodeResult.SUCCESS;
}
}
}
public Vector3 startPosition;
public Vector3I targetPosition;
public List<Vector3> pathPoints;
pathPoints ??= new List<Vector3>(Pathfinding.GetPath(Pathfinding.GetClosestStartPoint(robot.Position), targetPosition));
if (pathPoints.Count <= 0)
{
lastExecutionMessage = $"No path available {targetPosition}";
return NodeResult.FAILURE;
}
public ExploreNode()
{
DisplayText = "Explore";
}
startPosition = robot.Position;
Vector3 target = pathPoints[0] - startPosition;
float distance = target.Length();
public override NodeResult Execute(Robot robot, double delta)
{
if (pathPoints == null && !TrySelectTarget())
{
lastExecutionMessage = "No tiles left to explore";
return NodeResult.SUCCESS;
}
float movementSpeed = robot.GetMovementSpeed();
if (pathPoints == null)
{
pathPoints = new List<Vector3>(
Pathfinding.GetPath(Pathfinding.GetClosestStartPoint(robot.Position), targetPosition)
);
}
if (distance < 0.1f * Mathf.Sqrt(movementSpeed))
{
robot.Position = pathPoints[0];
Vector3I mapIndex = Pathfinding.GetClosestStartPoint(robot.Position);
Tile tile = GameData.map[mapIndex.Y].tiles[mapIndex.X, mapIndex.Z];
if (!tile.wasVisited)
{
tile.VisitTile();
}
if (pathPoints.Count <= 0)
{
lastExecutionMessage = $"No path available {targetPosition}";
return NodeResult.FAILURE;
}
pathPoints.Remove(pathPoints[0]);
if (pathPoints.Count <= 0)
{
lastExecutionMessage = "Current exploration finished";
pathPoints = null;
return NodeResult.RUNNING;
}
return MoveAlongPath(robot, delta);
}
lastExecutionMessage = "";
return NodeResult.RUNNING;
}
private bool TrySelectTarget()
{
int safetyCounter = 0;
int layerRange = Math.Max(GameData.lowestLayer, 1);
int maximumAttempts = (int)Math.Pow(GameData.layerSize, 2) * 2;
Vector3 direction = target / distance;
Vector3 lookDirection = new Vector3(direction.X, 0, direction.Z);
if (lookDirection.Length() > 0.1f)
{
robot.LookAt(robot.GlobalPosition + lookDirection, Vector3.Up);
}
robot.GlobalPosition += direction * (float)delta * movementSpeed;
while (safetyCounter <= maximumAttempts)
{
targetPosition = new Vector3I(
GameData.rand.Next(GameData.layerSize),
GameData.rand.Next(layerRange),
GameData.rand.Next(GameData.layerSize)
);
if (!GameData.map[targetPosition.Y].tiles[targetPosition.X, targetPosition.Z].wasVisited)
{
return true;
}
return NodeResult.RUNNING;
}
safetyCounter++;
}
public override ProgramNode Duplicate()
{
ExploreNode duplicate = new ExploreNode
{
targetPosition = targetPosition
};
return duplicate;
}
return false;
}
public override string Save()
{
return $"Name: {DisplayText}";
}
private NodeResult MoveAlongPath(Robot robot, double delta)
{
startPosition = robot.Position;
Vector3 target = pathPoints[0] - startPosition;
float distance = target.Length();
float movementSpeed = robot.GetMovementSpeed();
if (distance < 0.1f * Mathf.Sqrt(movementSpeed))
{
return FinishCurrentStep(robot);
}
Vector3 direction = target / distance;
RotateRobot(robot, direction);
robot.GlobalPosition += direction * (float)delta * movementSpeed;
return NodeResult.RUNNING;
}
private NodeResult FinishCurrentStep(Robot robot)
{
robot.Position = pathPoints[0];
VisitCurrentTile(robot);
pathPoints.RemoveAt(0);
if (pathPoints.Count > 0)
{
lastExecutionMessage = "";
return NodeResult.RUNNING;
}
lastExecutionMessage = "Current exploration finished";
pathPoints = null;
return NodeResult.RUNNING;
}
private void VisitCurrentTile(Robot robot)
{
Vector3I mapIndex = Pathfinding.GetClosestStartPoint(robot.Position);
Tile tile = GameData.map[mapIndex.Y].tiles[mapIndex.X, mapIndex.Z];
if (!tile.wasVisited)
{
tile.VisitTile();
}
}
private void RotateRobot(Robot robot, Vector3 direction)
{
Vector3 lookDirection = new Vector3(direction.X, 0, direction.Z);
if (lookDirection.Length() <= 0.1f) return;
robot.LookAt(robot.GlobalPosition + lookDirection, Vector3.Up);
}
public override ProgramNode Duplicate()
{
return new ExploreNode
{
targetPosition = targetPosition
};
}
public override string Save()
{
return $"Name: {DisplayText}";
}
}
+3 -17
View File
@@ -9,11 +9,12 @@ public class ForNode : ProgramNode
{
DisplayText = "For";
}
public override NodeResult Execute(Robot robot, double delta)
{
bool isConditionFulfilled = DetermineCondition();
amountExecuted++;
return isConditionFulfilled? NodeResult.SUCCESS : NodeResult.CONDITIONFALSE;
return isConditionFulfilled ? NodeResult.SUCCESS : NodeResult.CONDITIONFALSE;
}
private bool DetermineCondition()
@@ -41,21 +42,6 @@ public class ForNode : ProgramNode
Dictionary<StringName, ProgramNode> availableNodes
)
{
nextNode = null;
NegativeNode = null;
foreach (Godot.Collections.Dictionary connection in connections)
{
int port = (int)connection["from_port"];
ProgramNode connectedNode = GetConnectedNode(connection, availableNodes);
if (port == 0)
{
nextNode = connectedNode;
}
else
{
NegativeNode = connectedNode;
}
}
SetBranchNodes(connections, availableNodes);
}
}
+39 -42
View File
@@ -2,51 +2,48 @@ using Godot;
public class HarvestNode : ProgramNode
{
public HarvestNode()
{
DisplayText = "Harvest";
}
public override NodeResult Execute(Robot robot, double delta)
{
Vector3I mapIndex = Pathfinding.GetClosestStartPoint(robot.Position);
Tile tile = GameData.map[mapIndex.Y].tiles[mapIndex.X, mapIndex.Z];
public HarvestNode()
{
DisplayText = "Harvest";
}
if (!tile.containsResource)
{
lastExecutionMessage = "No resource on this tile";
return NodeResult.FAILURE;
}
public override NodeResult Execute(Robot robot, double delta)
{
Vector3I mapIndex = Pathfinding.GetClosestStartPoint(robot.Position);
Tile tile = GameData.map[mapIndex.Y].tiles[mapIndex.X, mapIndex.Z];
if (!tile.resource.CanExtract())
{
lastExecutionMessage = "Resource is depleted and not endless or you haven't unlocked it yet";
return NodeResult.SUCCESS;
}
if (!tile.containsResource || tile.resource == null)
{
lastExecutionMessage = "No resource on this tile";
return NodeResult.FAILURE;
}
if (tile.resource.Extract(delta))
{
SoundManager.PlayMining();
if (!GameData.inventory.AddItem(new Item {data = tile.resource.item}, 1))
{
lastExecutionMessage = "Not enough space";
return NodeResult.FAILURE;
}
else
{
return NodeResult.SUCCESS;
}
}
return NodeResult.RUNNING;
}
if (!tile.resource.CanExtract())
{
lastExecutionMessage = "Resource is depleted and not endless or you haven't unlocked it yet";
return NodeResult.SUCCESS;
}
public override ProgramNode Duplicate()
{
HarvestNode duplicate = new HarvestNode();
return duplicate;
}
if (!tile.resource.Extract(delta)) return NodeResult.RUNNING;
public override string Save()
{
return $"Name: {DisplayText}";
}
SoundManager.PlayMining();
if (!GameData.inventory.AddItem(new Item { data = tile.resource.item }, 1))
{
lastExecutionMessage = "Not enough space";
return NodeResult.FAILURE;
}
lastExecutionMessage = "";
return NodeResult.SUCCESS;
}
public override ProgramNode Duplicate()
{
return new HarvestNode();
}
public override string Save()
{
return $"Name: {DisplayText}";
}
}
+11 -31
View File
@@ -10,6 +10,7 @@ public class IfNode : ProgramNode
{
DisplayText = "If";
}
public override NodeResult Execute(Robot robot, double delta)
{
if (selectedItem == null)
@@ -19,27 +20,21 @@ public class IfNode : ProgramNode
}
bool isConditionFulfilled = DetermineCondition();
return isConditionFulfilled? NodeResult.SUCCESS : NodeResult.CONDITIONFALSE;
return isConditionFulfilled ? NodeResult.SUCCESS : NodeResult.CONDITIONFALSE;
}
private bool DetermineCondition()
{
int inventoryAmount = GameData.inventory.GetItemAmount(selectedItem.data.Id);
switch (comparator)
return comparator switch
{
case "is bigger than":
return inventoryAmount > amount;
case "is less than":
return inventoryAmount < amount;
case "is not":
return inventoryAmount != amount;
case "is less than or equal to":
return inventoryAmount <= amount;
case "is bigger than or equal to":
return inventoryAmount >= amount;
default:
return inventoryAmount == amount;
}
"is bigger than" => inventoryAmount > amount,
"is less than" => inventoryAmount < amount,
"is not" => inventoryAmount != amount,
"is less than or equal to" => inventoryAmount <= amount,
"is bigger than or equal to" => inventoryAmount >= amount,
_ => inventoryAmount == amount
};
}
public override ProgramNode Duplicate()
@@ -63,21 +58,6 @@ public class IfNode : ProgramNode
Dictionary<StringName, ProgramNode> availableNodes
)
{
nextNode = null;
NegativeNode = null;
foreach (Godot.Collections.Dictionary connection in connections)
{
int port = (int)connection["from_port"];
ProgramNode connectedNode = GetConnectedNode(connection, availableNodes);
if (port == 0)
{
nextNode = connectedNode;
}
else
{
NegativeNode = connectedNode;
}
}
SetBranchNodes(connections, availableNodes);
}
}
+85 -66
View File
@@ -3,79 +3,98 @@ using Godot;
public class MoveNode : ProgramNode
{
public Vector3 startPosition;
public Vector3I targetPosition;
public List<Vector3> pathPoints;
public MoveNode()
{
DisplayText = "Move";
}
public override NodeResult Execute(Robot robot, double delta)
{
Vector3I closestPosition = Pathfinding.GetClosestStartPoint(robot.Position);
pathPoints ??= new List<Vector3>(Pathfinding.GetPath(closestPosition, targetPosition));
public Vector3 startPosition;
public Vector3I targetPosition;
public List<Vector3> pathPoints;
if (pathPoints.Count <= 0)
{
if ((closestPosition - targetPosition).Length() == 0)
{
lastExecutionMessage = "";
return NodeResult.SUCCESS;
}
lastExecutionMessage = "No path available";
return NodeResult.FAILURE;
}
public MoveNode()
{
DisplayText = "Move";
}
startPosition = robot.Position;
Vector3 target = pathPoints[0] - startPosition;
float distance = target.Length();
float movementSpeed = robot.GetMovementSpeed();
public override NodeResult Execute(Robot robot, double delta)
{
Vector3I closestPosition = Pathfinding.GetClosestStartPoint(robot.Position);
if (pathPoints == null)
{
pathPoints = new List<Vector3>(Pathfinding.GetPath(closestPosition, targetPosition));
}
if (distance < 0.1f * Mathf.Sqrt(movementSpeed))
{
robot.Position = pathPoints[0];
Vector3I mapIndex = Pathfinding.GetClosestStartPoint(robot.Position);
Tile tile = GameData.map[mapIndex.Y].tiles[mapIndex.X, mapIndex.Z];
if (!tile.wasVisited)
{
tile.VisitTile();
}
if (pathPoints.Count <= 0)
{
if ((closestPosition - targetPosition).Length() == 0)
{
lastExecutionMessage = "";
return NodeResult.SUCCESS;
}
pathPoints.Remove(pathPoints[0]);
if (pathPoints.Count <= 0)
{
pathPoints = null;
lastExecutionMessage = "";
return NodeResult.SUCCESS;
}
lastExecutionMessage = "No path available";
return NodeResult.FAILURE;
}
lastExecutionMessage = "";
return NodeResult.RUNNING;
}
return MoveAlongPath(robot, delta);
}
Vector3 direction = target / distance;
Vector3 lookDirection = new Vector3(direction.X, 0, direction.Z);
if (lookDirection.Length() > 0.1f)
{
robot.LookAt(robot.GlobalPosition + lookDirection, Vector3.Up);
}
robot.GlobalPosition += direction * (float)delta * movementSpeed;
private NodeResult MoveAlongPath(Robot robot, double delta)
{
startPosition = robot.Position;
Vector3 target = pathPoints[0] - startPosition;
float distance = target.Length();
float movementSpeed = robot.GetMovementSpeed();
return NodeResult.RUNNING;
}
if (distance < 0.1f * Mathf.Sqrt(movementSpeed))
{
return FinishCurrentStep(robot);
}
public override ProgramNode Duplicate()
{
MoveNode duplicate = new MoveNode
{
targetPosition = targetPosition
};
return duplicate;
}
Vector3 direction = target / distance;
RotateRobot(robot, direction);
robot.GlobalPosition += direction * (float)delta * movementSpeed;
public override string Save()
{
return $"Name: {DisplayText}, Position: ({targetPosition.X}|{targetPosition.Y}|{targetPosition.Z})";
}
return NodeResult.RUNNING;
}
private NodeResult FinishCurrentStep(Robot robot)
{
robot.Position = pathPoints[0];
VisitCurrentTile(robot);
pathPoints.RemoveAt(0);
lastExecutionMessage = "";
if (pathPoints.Count > 0) return NodeResult.RUNNING;
pathPoints = null;
return NodeResult.SUCCESS;
}
private void VisitCurrentTile(Robot robot)
{
Vector3I mapIndex = Pathfinding.GetClosestStartPoint(robot.Position);
Tile tile = GameData.map[mapIndex.Y].tiles[mapIndex.X, mapIndex.Z];
if (!tile.wasVisited)
{
tile.VisitTile();
}
}
private void RotateRobot(Robot robot, Vector3 direction)
{
Vector3 lookDirection = new Vector3(direction.X, 0, direction.Z);
if (lookDirection.Length() <= 0.1f) return;
robot.LookAt(robot.GlobalPosition + lookDirection, Vector3.Up);
}
public override ProgramNode Duplicate()
{
return new MoveNode
{
targetPosition = targetPosition
};
}
public override string Save()
{
return $"Name: {DisplayText}, Position: ({targetPosition.X}|{targetPosition.Y}|{targetPosition.Z})";
}
}
+47 -25
View File
@@ -3,35 +3,57 @@ using System.Collections.Generic;
public abstract class ProgramNode
{
public ProgramNode nextNode;
public ProgramNode NegativeNode;
public string DisplayText;
public string lastExecutionMessage;
public ProgramNode nextNode;
public ProgramNode NegativeNode;
public string DisplayText;
public string lastExecutionMessage;
public abstract NodeResult Execute(Robot robot, double delta);
public abstract ProgramNode Duplicate();
public abstract string Save();
public abstract NodeResult Execute(Robot robot, double delta);
public abstract ProgramNode Duplicate();
public abstract string Save();
public virtual void SetNextNode(
List<Godot.Collections.Dictionary> connections,
Dictionary<StringName, ProgramNode> availableNodes
)
{
nextNode = null;
public virtual void SetNextNode(
List<Godot.Collections.Dictionary> connections,
Dictionary<StringName, ProgramNode> availableNodes
)
{
nextNode = null;
if (connections.Count <= 0) return;
if (connections.Count <= 0) return;
nextNode = GetConnectedNode(connections[0], availableNodes);
}
nextNode = GetConnectedNode(connections[0], availableNodes);
}
protected ProgramNode GetConnectedNode(
Godot.Collections.Dictionary connection,
Dictionary<StringName, ProgramNode> availableNodes
)
{
StringName nodeName = connection["to_node"].AsStringName();
if (!availableNodes.ContainsKey(nodeName)) return null;
protected void SetBranchNodes(
List<Godot.Collections.Dictionary> connections,
Dictionary<StringName, ProgramNode> availableNodes
)
{
nextNode = null;
NegativeNode = null;
return availableNodes[nodeName];
}
foreach (Godot.Collections.Dictionary connection in connections)
{
ProgramNode connectedNode = GetConnectedNode(connection, availableNodes);
if ((int)connection["from_port"] == 0)
{
nextNode = connectedNode;
}
else
{
NegativeNode = connectedNode;
}
}
}
protected ProgramNode GetConnectedNode(
Godot.Collections.Dictionary connection,
Dictionary<StringName, ProgramNode> availableNodes
)
{
StringName nodeName = connection["to_node"].AsStringName();
if (!availableNodes.ContainsKey(nodeName)) return null;
return availableNodes[nodeName];
}
}
+11 -32
View File
@@ -10,9 +10,9 @@ public class WhileNode : ProgramNode
{
DisplayText = "While";
}
public override NodeResult Execute(Robot robot, double delta)
{
if (selectedItem == null)
{
lastExecutionMessage = "No Item selected";
@@ -20,27 +20,21 @@ public class WhileNode : ProgramNode
}
bool isConditionFulfilled = DetermineCondition();
return isConditionFulfilled? NodeResult.SUCCESS : NodeResult.CONDITIONFALSE;
return isConditionFulfilled ? NodeResult.SUCCESS : NodeResult.CONDITIONFALSE;
}
private bool DetermineCondition()
{
int inventoryAmount = GameData.inventory.GetItemAmount(selectedItem.data.Id);
switch (comparator)
return comparator switch
{
case "is bigger than":
return inventoryAmount > amount;
case "is less than":
return inventoryAmount < amount;
case "is not":
return inventoryAmount != amount;
case "is less than or equal to":
return inventoryAmount <= amount;
case "is bigger than or equal to":
return inventoryAmount >= amount;
default:
return inventoryAmount == amount;
}
"is bigger than" => inventoryAmount > amount,
"is less than" => inventoryAmount < amount,
"is not" => inventoryAmount != amount,
"is less than or equal to" => inventoryAmount <= amount,
"is bigger than or equal to" => inventoryAmount >= amount,
_ => inventoryAmount == amount
};
}
public override ProgramNode Duplicate()
@@ -64,21 +58,6 @@ public class WhileNode : ProgramNode
Dictionary<StringName, ProgramNode> availableNodes
)
{
nextNode = null;
NegativeNode = null;
foreach (Godot.Collections.Dictionary connection in connections)
{
int port = (int)connection["from_port"];
ProgramNode connectedNode = GetConnectedNode(connection, availableNodes);
if (port == 0)
{
nextNode = connectedNode;
}
else
{
NegativeNode = connectedNode;
}
}
SetBranchNodes(connections, availableNodes);
}
}