-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRailways.lua
More file actions
201 lines (157 loc) · 5.71 KB
/
Railways.lua
File metadata and controls
201 lines (157 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
--!strict
-- Railways
-- Yuuwa0519
-- 2022-05-01
-- Services
local CollectionService = game:GetService("CollectionService")
-- Var
local railways: { [string]: { [string]: TrackNode } } = {}
local DEBUG = false
-- Funcs
local function collectNodes(railwayFolder: Folder)
local railwayName: string = railwayFolder.Name
local trackNodes: { [string]: TrackNode } = {}
-- Register Nodes
trackNodes["DEFAULT"] = {
Id = "DEFAULT",
TrackType = "Track" :: TrackType,
Railway = railwayName,
Position = Vector3.new(0, 1000000, 0),
RootNodeId = nil,
BranchNodeId = nil,
NeighbourIds = {},
Reference = nil,
}
for _, node: Folder in ipairs(railwayFolder:GetChildren()) do
trackNodes[node:GetAttribute("NodeId")] = {
Id = node:GetAttribute("NodeId") :: string,
TrackType = "Track" :: TrackType,
Railway = railwayName,
Position = node:GetAttribute("NodePosition") :: Vector3,
RootNodeId = nil,
BranchNodeId = nil,
NeighbourIds = {},
Reference = node,
}
if DEBUG then
local debugPart = Instance.new("Part")
debugPart.Position = trackNodes[node:GetAttribute("NodeId")].Position
debugPart.Size = Vector3.new(1, 1, 1)
debugPart.Anchored = true
debugPart.CanCollide = false
debugPart.CanTouch = false
debugPart.CanQuery = false
debugPart.Color = Color3.new(0, 0.466666, 1)
debugPart.Material = Enum.Material.Neon
debugPart.Name = "RailDebugPart"
debugPart.Parent = workspace
end
end
-- Connect Nodes
for thisNodeId, trackNode: TrackNode in pairs(trackNodes) do
if trackNode.Reference then
-- Also work on determining rail type
local linksRaw: string = trackNode.Reference:GetAttribute("NodeLinks")
local links: { string } = string.split(linksRaw, ",")
for _, neighbourId: string in ipairs(links) do
local neighbourNode: TrackNode = trackNodes[neighbourId]
table.insert(trackNode.NeighbourIds, neighbourNode.Id)
-- table.insert(neighbourNode.NeighbourIds, trackNode.Id)
end
else
print("Lost Reference")
end
end
for _, trackNode in pairs(trackNodes) do
if #trackNode.NeighbourIds > 2 then
--[[
If the train comes from definite node, that means it can branch out into other nodes
If the trian comes from node other than definite node, it will always branch out into the definite node
]]
local closestDist: number, closestNode: TrackNode? = math.huge, nil
for _, neighbourNodeId: string in ipairs(trackNode.NeighbourIds) do
local otherTrackNode = trackNodes[neighbourNodeId]
local dist: number = (trackNode.Position - otherTrackNode.Position).Magnitude
if dist < closestDist then
closestDist = dist
closestNode = otherTrackNode
end
end
trackNode.TrackType = "SwitchTrack" :: TrackType
trackNode.RootNodeId = closestNode.Id
-- Also reorder the connecting node, from left to right
local lookVector: Vector3 = (trackNode.Position - closestNode.Position).Unit
local rightVector: Vector3 = lookVector:Cross(-Vector3.new(0, -1, 0))
local upVector: Vector3 = lookVector:Cross(rightVector)
local baseLookCFrame: CFrame = CFrame.fromMatrix(trackNode.Position, rightVector, upVector, lookVector)
table.sort(trackNode.NeighbourIds, function(otherNodeAId: string, otherNodeBId: string)
local otherNodeA: TrackNode = trackNodes[otherNodeAId]
local otherNodeB: TrackNode = trackNodes[otherNodeBId]
local leftnessOfNodeAFromBase: Vector3 = baseLookCFrame:PointToObjectSpace(otherNodeA.Position)
local leftnessOfNodeBFromBase: Vector3 = baseLookCFrame:PointToObjectSpace(otherNodeB.Position)
return leftnessOfNodeAFromBase.X < leftnessOfNodeBFromBase.X
end)
local rootNodeIndex: number? = table.find(trackNode.NeighbourIds, closestNode.Id)
if rootNodeIndex then
table.remove(trackNode.NeighbourIds, rootNodeIndex)
table.insert(trackNode.NeighbourIds, 1, closestNode.Id)
else
warn("Root Node is Lost!")
end
for _, neighbourId: string in ipairs(trackNode.NeighbourIds) do
local sameCount: number = 0
for _, otherNeighbourId: string in ipairs(trackNode.NeighbourIds) do
if neighbourId == otherNeighbourId then
sameCount += 1
end
end
if sameCount > 1 then
print(trackNode.NeighbourIds)
error("Duplicated Neighbour Id")
end
end
trackNode.BranchNodeId = trackNode.NeighbourIds[2]
print("Branch", trackNode.Id, trackNode.BranchNodeId)
else
-- Just assign random defininte node, its not important for non switchtrack tracks.
trackNode.RootNodeId = trackNode.NeighbourIds[1]
end
if DEBUG then
for _, neighbourNodeId: string in ipairs(trackNode.NeighbourIds) do
local neighborNode: TrackNode = trackNodes[neighbourNodeId]
local direction: Vector3 = (neighborNode.Position - trackNode.Position).Unit
local debugPart = Instance.new("Part")
debugPart.Position = trackNode.Position + direction * 0.7
debugPart.Size = Vector3.new(0.1, 0.1, 0.1)
debugPart.Anchored = true
debugPart.CanCollide = false
debugPart.CanTouch = false
debugPart.CanQuery = false
debugPart.Color = if trackNode.TrackType == "SwitchTrack"
and trackNode.RootNodeId == neighbourNodeId
then Color3.new(0.764705, 0, 1)
else Color3.new(0.168627, 1, 0)
debugPart.Material = Enum.Material.Neon
debugPart.Name = "RailDebugPart"
debugPart.Parent = workspace
end
end
end
railways[railwayFolder.Name] = trackNodes
end
-- Main
for _, trackFolder in ipairs(CollectionService:GetTagged("TrackComponent")) do
collectNodes(trackFolder :: Folder)
end
export type TrackType = "Track" | "SwitchTrack"
export type TrackNode = {
Id: string,
TrackType: TrackType,
Railway: string,
Position: Vector3,
RootNodeId: string,
BranchNodeId: string?,
NeighbourIds: { string },
Reference: Folder,
}
return railways