@@ -234,6 +234,12 @@ private void ParseFerryConnections()
234
234
235
235
TsFerryConnection conn = null ;
236
236
237
+ ulong startPortToken = 0 ;
238
+ ulong endPortToken = 0 ;
239
+ int price = 0 ;
240
+ int time = 0 ;
241
+ int distance = 0 ;
242
+
237
243
foreach ( var line in lines )
238
244
{
239
245
if ( line . Contains ( ":" ) )
@@ -255,22 +261,40 @@ private void ParseFerryConnections()
255
261
if ( line . Contains ( "ferry_connection" ) )
256
262
{
257
263
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 ) ;
263
274
}
275
+
276
+ if ( key . Contains ( "distance" ) ) {
277
+ distance = Int32 . Parse ( value ) ;
278
+ }
264
279
}
265
280
266
281
if ( ! line . Contains ( "}" ) || conn == null ) continue ; ;
267
282
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 ;
273
283
}
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 ) ;
274
298
}
275
299
}
276
300
@@ -414,10 +438,11 @@ private void LoadNavigation() {
414
438
{
415
439
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 ) ) ;
416
440
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;*/
421
446
roads . Add ( road ) ;
422
447
if ( GetNodeByUid ( road . StartNodeUid ) == precnode )
423
448
{
@@ -463,6 +488,7 @@ private void LoadNavigation() {
463
488
{
464
489
TsPrefabItem forwardPrefab = ( TsPrefabItem ) node . ForwardItem ;
465
490
TsPrefabItem backwardPrefab = ( TsPrefabItem ) node . BackwardItem ;
491
+ if ( forwardPrefab . Hidden || backwardPrefab . Hidden ) continue ;
466
492
if ( forwardPrefab . Navigation . ContainsKey ( backwardPrefab ) == false )
467
493
{
468
494
forwardPrefab . Navigation . Add ( backwardPrefab , new Tuple < float , List < TsItem > > ( 0 , null ) ) ;
@@ -498,9 +524,9 @@ private void LoadNavigation() {
498
524
var ports = new List < TsItem > ( ) ;
499
525
ports . Add ( FerryPortbyId [ connection . StartPortToken ] ) ;
500
526
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 ) ) ;
502
528
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 ) ) ;
504
530
}
505
531
}
506
532
}
@@ -542,7 +568,7 @@ private void CalculatePath(TsPrefabItem Start, TsPrefabItem End) {
542
568
543
569
if ( toWalk . Uid == End . Uid ) break ;
544
570
545
- var currentWeight = walkedNodes [ toWalk ] . Item1 ; //+ toWalk.Prefab.indicativeWeight / 25;
571
+ var currentWeight = walkedNodes [ toWalk ] . Item1 ;
546
572
547
573
foreach ( var jump in toWalk . Navigation )
548
574
{
@@ -560,7 +586,11 @@ private void CalculatePath(TsPrefabItem Start, TsPrefabItem End) {
560
586
}
561
587
var nextRoad = toWalk . Navigation [ newNode ] . Item2 ;
562
588
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
+ }
564
594
}
565
595
566
596
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) {
570
600
TsPrefabItem route = End ;
571
601
while ( route != null )
572
602
{
573
- var gotoNew = walkedNodes [ route ] . Item2 ;
603
+ TsPrefabItem gotoNew ;
604
+ if ( walkedNodes . ContainsKey ( route ) ) gotoNew = walkedNodes [ route ] . Item2 ;
605
+ else gotoNew = nodesToWalk [ route ] . Item2 ;
574
606
if ( gotoNew == null ) break ;
575
607
if ( gotoNew . Navigation . ContainsKey ( route ) && gotoNew . Navigation [ route ] . Item2 != null ) {
576
608
if ( gotoNew . Navigation [ route ] . Item2 . Count == 2 && gotoNew . Navigation [ route ] . Item2 [ 0 ] is TsFerryItem && gotoNew . Navigation [ route ] . Item2 [ 1 ] is TsFerryItem ) {
577
609
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 ] ;
579
611
if ( ! RouteFerryPorts . ContainsKey ( startPort ) ) RouteFerryPorts . Add ( startPort , endPort ) ;
580
612
} else {
581
613
for ( int i = gotoNew . Navigation [ route ] . Item2 . Count - 1 ; i >= 0 ; i -- )
@@ -637,7 +669,7 @@ private void SetForwardBackward() {
637
669
/// <summary>
638
670
/// Given two roads it search for a path that are inside prefabs between them using a DFS search
639
671
/// </summary>
640
- private bool SetInternalRoutePrefab ( TsRoadItem Start , TsRoadItem End ) {
672
+ private Tuple < bool , float > SetInternalRoutePrefab ( TsRoadItem Start , TsRoadItem End ) {
641
673
TsNode startNode = null ;
642
674
Dictionary < TsPrefabItem , bool > visited = new Dictionary < TsPrefabItem , bool > ( ) ;
643
675
Stack < List < Tuple < TsNode , TsPrefabItem > > > prefabsToCheck = new Stack < List < Tuple < TsNode , TsPrefabItem > > > ( ) ;
@@ -676,19 +708,23 @@ private bool SetInternalRoutePrefab(TsRoadItem Start,TsRoadItem End) {
676
708
prefabsToCheck . Push ( newPath ) ;
677
709
}
678
710
}
711
+ var returnValue = new Tuple < bool , float > ( false , 0 ) ;
679
712
foreach ( var path in possiblePaths )
680
713
{
681
714
bool success = true ;
715
+ float totalLength = 0.0f ;
682
716
for ( int i = 0 ; i < path . Count - 1 ; i ++ )
683
717
{
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 ) {
685
720
success = false ;
686
721
break ;
687
722
}
723
+ totalLength += tempData . Item2 ;
688
724
}
689
- if ( success && path . Count >= 1 ) return true ;
725
+ if ( success && path . Count >= 1 ) return new Tuple < bool , float > ( true , totalLength / Start . RoadLook . GetWidth ( ) ) ;
690
726
}
691
- return false ;
727
+ return returnValue ;
692
728
}
693
729
694
730
/// <summary>
@@ -789,19 +825,22 @@ public void AddFerryPortLocation(ulong ferryPortId, float x, float z)
789
825
}
790
826
}
791
827
792
- public bool AddPrefabPath ( TsPrefabItem prefab , TsNode startNode , TsNode endNode ) {
828
+ public Tuple < bool , float > AddPrefabPath ( TsPrefabItem prefab , TsNode startNode , TsNode endNode ) {
793
829
//Optional - some prefabs, like gas stastions will be completely selected instead of selecting a sigle road
794
830
//if (prefab.Prefab.PrefabNodes.Count <= 2) {
795
831
// RoutePrefabs.Add(prefab);
796
832
// return true;
797
833
//}
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 ) ;
799
836
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 ;
805
844
}
806
845
807
846
}
0 commit comments