-
Notifications
You must be signed in to change notification settings - Fork 25
/
RoadMaker.cs
230 lines (204 loc) · 9.02 KB
/
RoadMaker.cs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using ColossalFramework;
using ColossalFramework.Math;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using UnityEngine;
namespace Mapper
{
public class RoadMaker2
{
public OSM.OSMInterface osm;
private Randomizer rand;
private Dictionary<RoadTypes, NetInfo> netInfos = new Dictionary<RoadTypes, NetInfo>();
private Dictionary<uint, ushort> nodeMap = new Dictionary<uint, ushort>();
public RoadMaker2(OSM.OSMInterface osm)
{
this.osm = osm;
this.rand = new Randomizer(0u);
var roadTypes = Enum.GetNames(typeof(RoadTypes));
for (var i = 0; i < PrefabCollection<NetInfo>.PrefabCount(); i += 1)
{
var pp = PrefabCollection<NetInfo>.GetPrefab((uint)i);
if (pp != null)
{
if (roadTypes.Contains(pp.name.Replace(" ", "")))
{
netInfos.Add((RoadTypes)Enum.Parse(typeof(RoadTypes), pp.name.Replace(" ", "")), pp);
}
}
}
}
public IEnumerator MakeRoad(int p,bool pedestrians, bool roads, bool highways)
{
var nm = Singleton<NetManager>.instance;
if (! nm.CheckLimits())
{
yield return null;
}
var way = osm.ways.ElementAt(p);
NetInfo ni = null;
if (way.roadTypes == RoadTypes.None)
{
yield break;
}
if (!pedestrians && ((int)way.roadTypes <= (int)RoadTypes.PedestrianElevated))
{
yield break;
}
if (!roads && (int)way.roadTypes > (int)RoadTypes.PedestrianElevated && (int)way.roadTypes < (int)RoadTypes.TrainTrack)
{
yield break;
}
if (!highways && (int)way.roadTypes >= (int)RoadTypes.TrainTrack)
{
yield break;
}
if (netInfos.ContainsKey(way.roadTypes))
{
ni = netInfos[way.roadTypes];
}
else
{
Debug.Log("Failed to find net info: " + way.roadTypes.ToString());
yield return null;
}
float elevation = way.layer;
if (elevation < 0)
{
yield return null;
}
else if (elevation > 0)
{
elevation *= 11f;
var errors = default(ToolBase.ToolErrors);
ni = ni.m_netAI.GetInfo(elevation, elevation, 5f, false, false, false, false, ref errors);
}
if (!osm.nodes.ContainsKey(way.startNode) || !osm.nodes.ContainsKey(way.endNode))
{
yield return null;
}
ushort startNode;
if (nodeMap.ContainsKey(way.startNode))
{
startNode = nodeMap[way.startNode];
AdjustElevation(startNode, elevation);
}
else
{
CreateNode(out startNode, ref rand, ni, osm.nodes[way.startNode],elevation);
AdjustElevation(startNode, elevation);
nodeMap.Add(way.startNode, startNode);
}
ushort endNode;
if (nodeMap.ContainsKey(way.endNode))
{
endNode = nodeMap[way.endNode];
AdjustElevation(endNode, elevation);
}
else
{
CreateNode(out endNode, ref rand, ni, osm.nodes[way.endNode],elevation);
AdjustElevation(endNode, elevation);
nodeMap.Add(way.endNode, endNode);
}
var currentStartNode = startNode;
for (var i = 0; i < way.segments.Count(); i += 1)
{
var segment = way.segments[i];
ushort currentEndNode;
if (i == way.segments.Count() - 1)
{
currentEndNode = endNode;
}
else
{
CreateNode(out currentEndNode, ref rand, ni, segment.endPoint, elevation);
AdjustElevation(currentEndNode, elevation);
}
ushort segmentId;
Vector3 position = nm.m_nodes.m_buffer[(int)currentStartNode].m_position;
Vector3 position2 = nm.m_nodes.m_buffer[(int)currentEndNode].m_position;
if (segment.controlA.x == 0f && segment.controlB.x == 0f)
{
// Vector3 vector = VectorUtils.NormalizeXZ(segment.endPoint - segment.startPoint);
Vector3 vector = position2 - position;
vector = VectorUtils.NormalizeXZ(vector);
if (nm.CreateSegment(out segmentId, ref rand, ni, currentStartNode, currentEndNode, vector, -vector, Singleton<SimulationManager>.instance.m_currentBuildIndex, Singleton<SimulationManager>.instance.m_currentBuildIndex, false))
{
Singleton<SimulationManager>.instance.m_currentBuildIndex += 2u;
}
}
else
{
var control = new Vector3(segment.controlA.x,0,segment.controlA.y);
//control.y = Singleton<TerrainManager>.instance.SampleRawHeightSmooth(control);
var control2 = new Vector3(segment.controlB.x,0, segment.controlB.y);
//control2.y = Singleton<TerrainManager>.instance.SampleRawHeightSmooth(control2);
//Vector3 entry = VectorUtils.NormalizeXZ(Bezier3.Tangent(position, segment.controlA, segment.controlB, position2, 0f));
//Vector3 exit = VectorUtils.NormalizeXZ(Bezier3.Tangent(position, segment.controlA, segment.controlB, position2, 1f));
Vector3 entry = VectorUtils.NormalizeXZ(control - position);
Vector3 exit = VectorUtils.NormalizeXZ(position2 - control2);
if (nm.CreateSegment(out segmentId, ref rand, ni, currentStartNode, currentEndNode, entry, -exit, Singleton<SimulationManager>.instance.m_currentBuildIndex, Singleton<SimulationManager>.instance.m_currentBuildIndex, false))
{
Singleton<SimulationManager>.instance.m_currentBuildIndex += 2u;
}
}
currentStartNode = currentEndNode;
}
yield return null;
}
private void AdjustElevation(ushort startNode, float elevation)
{
var nm = Singleton<NetManager>.instance;
var node = nm.m_nodes.m_buffer[startNode];
var ele = (byte)Mathf.Clamp(Mathf.RoundToInt(Math.Max(node.m_elevation, elevation)), 0, 255);
var terrain = Singleton<TerrainManager>.instance.SampleRawHeightSmoothWithWater(node.m_position, false, 0f);
node.m_elevation = ele;
node.m_position = new Vector3(node.m_position.x, ele + terrain, node.m_position.z);
if (elevation < 11f)
{
node.m_flags |= NetNode.Flags.OnGround;
}
else
{
node.m_flags &= ~NetNode.Flags.OnGround;
UpdateSegment(node.m_segment0,elevation);
UpdateSegment(node.m_segment1, elevation);
UpdateSegment(node.m_segment2, elevation);
UpdateSegment(node.m_segment3, elevation);
UpdateSegment(node.m_segment4, elevation);
UpdateSegment(node.m_segment5, elevation);
UpdateSegment(node.m_segment6, elevation);
UpdateSegment(node.m_segment7, elevation);
}
nm.m_nodes.m_buffer[startNode] = node;
//Singleton<NetManager>.instance.UpdateNode(startNode);
}
private void UpdateSegment(ushort segmentId, float elevation)
{
if (segmentId == 0)
{
return;
}
var nm = Singleton<NetManager>.instance;
if (elevation > 4)
{
var errors = default(ToolBase.ToolErrors);
nm.m_segments.m_buffer[segmentId].Info = nm.m_segments.m_buffer[segmentId].Info.m_netAI.GetInfo(elevation,elevation,5,false,false,false,false,ref errors);
}
}
private void CreateNode(out ushort startNode, ref Randomizer rand, NetInfo netInfo, Vector2 oldPos, float elevation)
{
var pos = new Vector3(oldPos.x, 0, oldPos.y);
pos.y = Singleton<TerrainManager>.instance.SampleRawHeightSmoothWithWater(pos,false,0f);
var nm = Singleton<NetManager>.instance;
nm.CreateNode(out startNode, ref rand, netInfo, pos, Singleton<SimulationManager>.instance.m_currentBuildIndex);
Singleton<SimulationManager>.instance.m_currentBuildIndex += 1u;
}
}
}