Skip to content

Commit 58fab34

Browse files
author
BuildTools
committed
Fixed Ferry Stuff + Added length path inside prefabs + Various Fixes
1 parent 5d2b92c commit 58fab34

File tree

3 files changed

+75
-40
lines changed

3 files changed

+75
-40
lines changed

TsMap/TsFerryConnection.cs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public class TsFerryConnection
1313
public PointF EndPortLocation { get; private set; }
1414
public List<PointF> connections = new List<PointF>();
1515

16+
public int Price;
17+
public int Time;
18+
public int Distance;
19+
1620
public void AddConnectionPosition(float x, float z)
1721
{
1822
connections.Add(new PointF(x / 256f, z / 256f));

TsMap/TsMapper.cs

+70-31
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ private void ParseFerryConnections()
234234

235235
TsFerryConnection conn = null;
236236

237+
ulong startPortToken = 0;
238+
ulong endPortToken = 0;
239+
int price = 0;
240+
int time = 0;
241+
int distance = 0;
242+
237243
foreach (var line in lines)
238244
{
239245
if (line.Contains(":"))
@@ -255,22 +261,40 @@ private void ParseFerryConnections()
255261
if (line.Contains("ferry_connection"))
256262
{
257263
var portIds = value.Split('.');
258-
conn = new TsFerryConnection
259-
{
260-
StartPortToken = ScsHash.StringToToken(portIds[1]),
261-
EndPortToken = ScsHash.StringToToken(portIds[2].TrimEnd('{').Trim())
262-
};
264+
startPortToken = ScsHash.StringToToken(portIds[1]);
265+
endPortToken = ScsHash.StringToToken(portIds[2].TrimEnd('{').Trim());
266+
}
267+
268+
if (key.Contains("price")) {
269+
price = Int32.Parse(value);
270+
}
271+
272+
if (key.Contains("time")) {
273+
time = Int32.Parse(value);
263274
}
275+
276+
if (key.Contains("distance")) {
277+
distance = Int32.Parse(value);
278+
}
264279
}
265280

266281
if (!line.Contains("}") || conn == null) continue;;
267282

268-
var existingItem = _ferryConnectionLookup.FirstOrDefault(item =>
269-
(item.StartPortToken == conn.StartPortToken && item.EndPortToken == conn.EndPortToken) ||
270-
(item.StartPortToken == conn.EndPortToken && item.EndPortToken == conn.StartPortToken)); // Check if connection already exists
271-
if (existingItem == null) _ferryConnectionLookup.Add(conn);
272-
conn = null;
273283
}
284+
285+
conn = new TsFerryConnection
286+
{
287+
StartPortToken = startPortToken,
288+
EndPortToken = endPortToken,
289+
Price = price,
290+
Time = time,
291+
Distance = distance
292+
};
293+
294+
var existingItem = _ferryConnectionLookup.FirstOrDefault(item =>
295+
(item.StartPortToken == conn.StartPortToken && item.EndPortToken == conn.EndPortToken) ||
296+
(item.StartPortToken == conn.EndPortToken && item.EndPortToken == conn.StartPortToken)); // Check if connection already exists
297+
if (existingItem == null) _ferryConnectionLookup.Add(conn);
274298
}
275299
}
276300

@@ -414,10 +438,11 @@ private void LoadNavigation() {
414438
{
415439
var length = (float)Math.Sqrt(Math.Pow(GetNodeByUid(road.StartNodeUid).X - GetNodeByUid(road.EndNodeUid).X, 2) + Math.Pow(GetNodeByUid(road.StartNodeUid).Z - GetNodeByUid(road.EndNodeUid).Z, 2));
416440
TsRoadItem roadObj = (TsRoadItem) road;
417-
if (roadObj.RoadLook.IsHighway) totalLength += (length / 2) / roadObj.RoadLook.GetWidth();
418-
else if (roadObj.RoadLook.IsLocal) totalLength += (length / 1.5f) / roadObj.RoadLook.GetWidth();
419-
else if (roadObj.RoadLook.IsExpress) totalLength += length / roadObj.RoadLook.GetWidth();
420-
else length += length * 2;
441+
totalLength += length / roadObj.RoadLook.GetWidth();
442+
/*if (roadObj.RoadLook.IsHighway) totalLength += (length / 2) / roadObj.RoadLook.GetWidth();
443+
else if (roadObj.RoadLook.IsLocal) totalLength += (length / 1.75f) / roadObj.RoadLook.GetWidth();
444+
else if (roadObj.RoadLook.IsExpress) totalLength += (length / 1.25f) / roadObj.RoadLook.GetWidth();
445+
else length += length * 2;*/
421446
roads.Add(road);
422447
if (GetNodeByUid(road.StartNodeUid) == precnode)
423448
{
@@ -463,6 +488,7 @@ private void LoadNavigation() {
463488
{
464489
TsPrefabItem forwardPrefab = (TsPrefabItem)node.ForwardItem;
465490
TsPrefabItem backwardPrefab = (TsPrefabItem)node.BackwardItem;
491+
if (forwardPrefab.Hidden || backwardPrefab.Hidden) continue;
466492
if (forwardPrefab.Navigation.ContainsKey(backwardPrefab) == false)
467493
{
468494
forwardPrefab.Navigation.Add(backwardPrefab,new Tuple<float, List<TsItem>>(0, null));
@@ -498,9 +524,9 @@ private void LoadNavigation() {
498524
var ports = new List<TsItem>();
499525
ports.Add(FerryPortbyId[connection.StartPortToken]);
500526
ports.Add(FerryPortbyId[connection.EndPortToken]);
501-
ferryToPrefab[connection.StartPortToken].Navigation.Add(ferryToPrefab[connection.EndPortToken],new Tuple<float,List<TsItem>>(0,ports));
527+
ferryToPrefab[connection.StartPortToken].Navigation.Add(ferryToPrefab[connection.EndPortToken],new Tuple<float,List<TsItem>>(connection.Distance,ports));
502528
ports.Reverse();
503-
ferryToPrefab[connection.EndPortToken].Navigation.Add(ferryToPrefab[connection.StartPortToken],new Tuple<float,List<TsItem>>(0,ports));
529+
ferryToPrefab[connection.EndPortToken].Navigation.Add(ferryToPrefab[connection.StartPortToken],new Tuple<float,List<TsItem>>(connection.Distance,ports));
504530
}
505531
}
506532
}
@@ -542,7 +568,7 @@ private void CalculatePath(TsPrefabItem Start, TsPrefabItem End) {
542568

543569
if (toWalk.Uid == End.Uid) break;
544570

545-
var currentWeight = walkedNodes[toWalk].Item1;//+ toWalk.Prefab.indicativeWeight / 25;
571+
var currentWeight = walkedNodes[toWalk].Item1;
546572

547573
foreach (var jump in toWalk.Navigation)
548574
{
@@ -560,7 +586,11 @@ private void CalculatePath(TsPrefabItem Start, TsPrefabItem End) {
560586
}
561587
var nextRoad = toWalk.Navigation[newNode].Item2;
562588
if (precRoad != null && nextRoad != null && (precRoad.Count != 0 && nextRoad.Count != 0)
563-
&& precRoad.LastOrDefault() is TsRoadItem && nextRoad[0] is TsRoadItem && !SetInternalRoutePrefab((TsRoadItem)precRoad.LastOrDefault(),(TsRoadItem)nextRoad[0])) continue;
589+
&& precRoad.LastOrDefault() is TsRoadItem && nextRoad[0] is TsRoadItem) {
590+
var result = SetInternalRoutePrefab((TsRoadItem)precRoad.LastOrDefault(),(TsRoadItem)nextRoad[0]);
591+
if (!result.Item1) continue;
592+
else newWeight += result.Item2;
593+
}
564594
}
565595

566596
if (!walkedNodes.ContainsKey(newNode) && nodesToWalk[newNode].Item1 > newWeight) nodesToWalk[newNode] = new Tuple<float, TsPrefabItem>(newWeight, toWalk);
@@ -570,12 +600,14 @@ private void CalculatePath(TsPrefabItem Start, TsPrefabItem End) {
570600
TsPrefabItem route = End;
571601
while (route != null)
572602
{
573-
var gotoNew = walkedNodes[route].Item2;
603+
TsPrefabItem gotoNew;
604+
if (walkedNodes.ContainsKey(route)) gotoNew = walkedNodes[route].Item2;
605+
else gotoNew = nodesToWalk[route].Item2;
574606
if (gotoNew == null) break;
575607
if (gotoNew.Navigation.ContainsKey(route) && gotoNew.Navigation[route].Item2 != null) {
576608
if (gotoNew.Navigation[route].Item2.Count == 2 && gotoNew.Navigation[route].Item2[0] is TsFerryItem && gotoNew.Navigation[route].Item2[1] is TsFerryItem) {
577609
var startPort = (TsFerryItem)gotoNew.Navigation[route].Item2[0];
578-
var endPort = (TsFerryItem)gotoNew.Navigation[route].Item2[0];
610+
var endPort = (TsFerryItem)gotoNew.Navigation[route].Item2[1];
579611
if (!RouteFerryPorts.ContainsKey(startPort)) RouteFerryPorts.Add(startPort,endPort);
580612
} else {
581613
for (int i=gotoNew.Navigation[route].Item2.Count-1;i>=0;i--)
@@ -637,7 +669,7 @@ private void SetForwardBackward() {
637669
/// <summary>
638670
/// Given two roads it search for a path that are inside prefabs between them using a DFS search
639671
/// </summary>
640-
private bool SetInternalRoutePrefab(TsRoadItem Start,TsRoadItem End) {
672+
private Tuple<bool,float> SetInternalRoutePrefab(TsRoadItem Start,TsRoadItem End) {
641673
TsNode startNode = null;
642674
Dictionary<TsPrefabItem,bool> visited = new Dictionary<TsPrefabItem,bool>();
643675
Stack<List<Tuple<TsNode,TsPrefabItem>>> prefabsToCheck = new Stack<List<Tuple<TsNode,TsPrefabItem>>>();
@@ -676,19 +708,23 @@ private bool SetInternalRoutePrefab(TsRoadItem Start,TsRoadItem End) {
676708
prefabsToCheck.Push(newPath);
677709
}
678710
}
711+
var returnValue = new Tuple<bool,float>(false,0);
679712
foreach (var path in possiblePaths)
680713
{
681714
bool success = true;
715+
float totalLength = 0.0f;
682716
for (int i = 0; i < path.Count - 1; i++)
683717
{
684-
if (!AddPrefabPath(path[i].Item2,path[i].Item1,path[i+1].Item1)) {
718+
var tempData = AddPrefabPath(path[i].Item2,path[i].Item1,path[i+1].Item1);
719+
if (!tempData.Item1) {
685720
success = false;
686721
break;
687722
}
723+
totalLength += tempData.Item2;
688724
}
689-
if (success && path.Count >= 1) return true;
725+
if (success && path.Count >= 1) return new Tuple<bool,float>(true,totalLength / Start.RoadLook.GetWidth());
690726
}
691-
return false;
727+
return returnValue;
692728
}
693729

694730
/// <summary>
@@ -789,19 +825,22 @@ public void AddFerryPortLocation(ulong ferryPortId, float x, float z)
789825
}
790826
}
791827

792-
public bool AddPrefabPath(TsPrefabItem prefab,TsNode startNode,TsNode endNode) {
828+
public Tuple<bool,float> AddPrefabPath(TsPrefabItem prefab,TsNode startNode,TsNode endNode) {
793829
//Optional - some prefabs, like gas stastions will be completely selected instead of selecting a sigle road
794830
//if (prefab.Prefab.PrefabNodes.Count <= 2) {
795831
// RoutePrefabs.Add(prefab);
796832
// return true;
797833
//}
798-
var f = prefab.GetNearestNode(this,startNode,0);
834+
var returnValue = new Tuple<bool,float>(false,0);
835+
var s = prefab.GetNearestNode(this,startNode,0);
799836
var e = prefab.GetNearestNode(this,endNode,1);
800-
if (f.id == -1 || e.id == -1) return false;
801-
if (prefab.Prefab.NavigationRoutes.ContainsKey(new Tuple<TsPrefabNode,TsPrefabNode>(f,e))) PrefabNav[prefab] = prefab.Prefab.NavigationRoutes[new Tuple<TsPrefabNode,TsPrefabNode>(f,e)].Item1;
802-
else return false;
803-
return true;
804-
// TODO: Add the possibility to return also the weight of the path to be used in Dijkstra's Algorithm
837+
if (s.id == -1 || e.id == -1) return returnValue;
838+
var key = new Tuple<TsPrefabNode,TsPrefabNode>(s,e);
839+
if (prefab.Prefab.NavigationRoutes.ContainsKey(key)) {
840+
PrefabNav[prefab] = prefab.Prefab.NavigationRoutes[key].Item1;
841+
returnValue = new Tuple<bool,float>(true,prefab.Prefab.NavigationRoutes[key].Item2);
842+
}
843+
return returnValue;
805844
}
806845

807846
}

TsMap/TsPrefab.cs

+1-9
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ public class TsPrefab
7777
public List<TsPrefabCurve> PrefabCurves {get; private set; }
7878
public Dictionary<Tuple<TsPrefabNode,TsPrefabNode>, Tuple<List<TsPrefabCurve>,float>> NavigationRoutes {get; private set; }
7979

80-
public float indicativeWeight {get; private set;} // this is used in Dijkstra's Algorithm but it's not accurate and NEEDS IMPORVEMENT
81-
8280
public TsPrefab(TsMapper mapper, string filePath, ulong token, string category)
8381
{
8482
_filePath = filePath;
@@ -272,8 +270,6 @@ private void Parse()
272270
if (pointInVicinity == null) TriggerPoints.Add(triggerPoint);
273271
}
274272

275-
int nDistances = 0;
276-
float totDist = 0.0f;
277273
foreach (var inputNode in PrefabNodes)
278274
{
279275
foreach (var outputNode in PrefabNodes)
@@ -326,14 +322,10 @@ private void Parse()
326322
path.Add(actualCurve);
327323
actualCurve = distances[actualCurve].Item2;
328324
}
329-
NavigationRoutes.Add(new Tuple<TsPrefabNode,TsPrefabNode>(inputNode,outputNode), new Tuple<List<TsPrefabCurve>,float>(path,length));
330-
totDist += distanceLength;
331-
nDistances++;
325+
NavigationRoutes.Add(new Tuple<TsPrefabNode,TsPrefabNode>(inputNode,outputNode), new Tuple<List<TsPrefabCurve>,float>(path,distanceLength));
332326
}
333327
}
334328
}
335-
336-
indicativeWeight = totDist / nDistances;
337329

338330
_stream = null;
339331

0 commit comments

Comments
 (0)