Skip to content

Feature: Better Straight Wire Routing #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"visualstudiotoolsforunity.vstuc"
]
}
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Unity",
"type": "vstuc",
"request": "attach"
}
]
}
70 changes: 70 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"files.exclude": {
"**/.DS_Store": true,
"**/.git": true,
"**/.vs": true,
"**/.gitmodules": true,
"**/.vsconfig": true,
"**/*.booproj": true,
"**/*.pidb": true,
"**/*.suo": true,
"**/*.user": true,
"**/*.userprefs": true,
"**/*.unityproj": true,
"**/*.dll": true,
"**/*.exe": true,
"**/*.pdf": true,
"**/*.mid": true,
"**/*.midi": true,
"**/*.wav": true,
"**/*.gif": true,
"**/*.ico": true,
"**/*.jpg": true,
"**/*.jpeg": true,
"**/*.png": true,
"**/*.psd": true,
"**/*.tga": true,
"**/*.tif": true,
"**/*.tiff": true,
"**/*.3ds": true,
"**/*.3DS": true,
"**/*.fbx": true,
"**/*.FBX": true,
"**/*.lxo": true,
"**/*.LXO": true,
"**/*.ma": true,
"**/*.MA": true,
"**/*.obj": true,
"**/*.OBJ": true,
"**/*.asset": true,
"**/*.cubemap": true,
"**/*.flare": true,
"**/*.mat": true,
"**/*.meta": true,
"**/*.prefab": true,
"**/*.unity": true,
"build/": true,
"Build/": true,
"Library/": true,
"library/": true,
"obj/": true,
"Obj/": true,
"Logs/": true,
"logs/": true,
"ProjectSettings/": true,
"UserSettings/": true,
"temp/": true,
"Temp/": true
},
"files.associations": {
"*.asset": "yaml",
"*.meta": "yaml",
"*.prefab": "yaml",
"*.unity": "yaml",
},
"explorer.fileNesting.enabled": true,
"explorer.fileNesting.patterns": {
"*.sln": "*.csproj",
},
"dotnet.defaultSolution": "Digital-Logic-Sim-Tests.sln"
}
4 changes: 4 additions & 0 deletions Assets/Scripts/Description/Types/ProjectDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public struct ProjectDescription
public bool Prefs_SimPaused;
public int Prefs_SimTargetStepsPerSecond;
public int Prefs_SimStepsPerClockTick;
public int Prefs_WireRouting;
public int Prefs_WireStyle;



// List of all player-created chips (in order of creation -- oldest first)
public string[] AllCustomChipNames;
Expand Down
97 changes: 89 additions & 8 deletions Assets/Scripts/Game/Elements/WireInstance.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using DLS.Description;
using DLS.Graphics;
using UnityEngine;
Expand Down Expand Up @@ -63,7 +64,15 @@ public WireInstance(ConnectionInfo firstConnectionInfo, int spawnOrder)
bitCount = firstPin.bitCount;
originalWireConnectionPoint = firstConnectionInfo.connectionPoint;
WirePoints.Add(GetAttachmentPoint(firstConnectionInfo));
WirePoints.Add(WirePoints[0]); // end point to be controlled by mouse during placement mode

if (Project.ActiveProject.ShouldRouteWires)
{
WirePoints.Add(WirePoints[0]); // add second point to allow routing wires
WirePoints.Add(WirePoints[0]); // add third point to allow routing wires
//WirePoints.Add(WirePoints[0]); // add fourth point to allow routing wires
}

WirePoints.Add(WirePoints[^1]); // end point to be controlled by mouse during placement mode

BitWires = new BitWire[(int)bitCount];
this.spawnOrder = spawnOrder;
Expand Down Expand Up @@ -117,12 +126,21 @@ public void FinishPlacingWire(ConnectionInfo connection)
PinInstance endPin = connection.pin;
WirePoints[^1] = endPin.GetWorldPos();

if (Project.ActiveProject.ShouldRouteWires)
{
// If routing wires, we need to reroute the second last point to account for the end pin snapping to the grid or chip pin
Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), endPin.GetWorldPos(), Project.ActiveProject.WireRoutingMode, GetWireLength());
SetWirePoint(points[0], WirePoints.Count - 3);
SetWirePoint(points[1], WirePoints.Count - 2);

}

// If wire connection started out at another wire, it is not known (until now when we have the end pin) whether that
// initial connection should be to the source or target pin of that wire (so correct if needed)
ConnectionInfo correctedFirstConnection = FirstConnectionInfo;
// initial connection should be to the source or target pin of that wire (so correct if needed)
ConnectionInfo correctedFirstConnection = FirstConnectionInfo;
if (FirstConnectionInfo.pin.IsSourcePin == endPin.IsSourcePin) // same type, must fix
{
Debug.Assert(FirstConnectionInfo.IsConnectedAtWire, "Connection is source->source or target->target, but connection didn't start from wire?!");
System.Diagnostics.Debug.Assert(FirstConnectionInfo.IsConnectedAtWire, "Connection is source->source or target->target, but connection didn't start from wire?!");
if (endPin.IsSourcePin) correctedFirstConnection.pin = GetBusCorrectedTargetPin(FirstConnectionInfo.connectedWire);
else correctedFirstConnection.pin = FirstConnectionInfo.connectedWire.SourcePin;
}
Expand Down Expand Up @@ -207,17 +225,69 @@ public void SetWirePoint(Vector2 p, int i)
public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPoint)
{
if (Project.ActiveProject.ShouldSnapToGrid) p = GridHelper.SnapToGrid(p, true, true);
if (Project.ActiveProject.ForceStraightWires) p = GridHelper.ForceStraightLine(straightLineRefPoint, p);

if (Project.ActiveProject.ForceStraightWires && (!Project.ActiveProject.ShouldRouteWires || IsFullyConnected)) p = GridHelper.ForceStraightLine(straightLineRefPoint, p);
if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires /*&& WirePoints.Count > 3*/ && !IsFullyConnected)
{
// If routing wires, we need to route the wire to the new point
int refPointIndex = 0;
if (WirePoints.Count > 4)
{
refPointIndex = WirePoints.Count - 4; // use the point before the last two points
}


Vector2[] points = GridHelper.RouteWire(GetWirePoint(refPointIndex), p, Project.ActiveProject.WireRoutingMode, GetWireLength());
p = points[2];
SetWirePoint(points[0], WirePoints.Count - 3);
SetWirePoint(points[1], WirePoints.Count - 2);
//SetWirePoint(points[2], WirePoints.Count - 2);
}
else if (Project.ActiveProject.ShouldRouteWires)
{
// If button to route wires is not pressed (or released), set the last two points to be the same (overlapping points can be removed later)
if (WirePoints.Count > 2) WirePoints[^2] = p;
if (WirePoints.Count > 3) WirePoints[^3] = p;
if (WirePoints.Count > 4) WirePoints[^4] = p;

}

SetWirePoint(p, i);
}
/*
public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPoint, ChipInteractionController.RotationTarget r)
{
if (Project.ActiveProject.ShouldSnapToGrid) p = GridHelper.SnapToGrid(p, true, true);
if (Project.ActiveProject.ForceStraightWires && (!Project.ActiveProject.ShouldRouteWires || IsFullyConnected)) p = GridHelper.ForceStraightLine(straightLineRefPoint, p);
if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 3 && !IsFullyConnected)
{
// If routing wires, we need to route the wire to the new point
Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 4), p, Project.ActiveProject.WireRoutingMode, GetWireLength());
p = points[2];
SetWirePoint(points[0], WirePoints.Count - 3);
SetWirePoint(points[1], WirePoints.Count - 2);
}
else
{
// If button to route wires is not pressed (or released), set the last two points to be the same (overlapping points can be removed later)
if (WirePoints.Count > 2) WirePoints[^2] = p;
}

SetWirePoint(p, i);
}*/

public void SetLastWirePoint(Vector2 p)
{
SetWirePointWithSnapping(p, WirePoints.Count - 1, GetWirePoint(WirePoints.Count - 2));
}

public void AddWirePoint(Vector2 p) => WirePoints.Add(p);
public void AddWirePoint(Vector2 p)
{
WirePoints.Add(p);
if (!Project.ActiveProject.ShouldRouteWires) return;
WirePoints.Add(p);
WirePoints.Add(p);
//WirePoints.Add(p); // add 3 extra points to allow routing wires
}

public void DeleteWirePoint(int i)
{
Expand Down Expand Up @@ -245,7 +315,7 @@ public bool RemoveLastPoint()
// Connection info for the end of this wire that connects to another wire
public ref ConnectionInfo GetWireConnectionInfo()
{
Debug.Assert(ConnectedWire != null, "No connected wire?");
System.Diagnostics.Debug.Assert(ConnectedWire != null, "No connected wire?");
return ref SourceConnectionInfo.IsConnectedAtWire ? ref SourceConnectionInfo : ref TargetConnectionInfo;
}

Expand Down Expand Up @@ -323,6 +393,17 @@ public Color GetColour(int bitIndex)

return col;
}

public float GetWireLength()
{
float length = 0f;
for (int i = 0; i < WirePoints.Count - 1; i++)
{
length += Vector2.Distance(WirePoints[i], WirePoints[i + 1]);
}

return length;
}

public static Vector2 ClosestPointOnLineSegment(Vector2 p, Vector2 a1, Vector2 a2)
{
Expand Down
Loading