Advertisement
agmike

LTrackSearch usage

Sep 17th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. final class AGMHumpRoute
  2. {
  3.     // Either TrackNumber
  4.     public int TrackNumber = -1;
  5.     public int MarkerId;
  6.    
  7.     // Or Junction LEFT RIGHT
  8.     public int JnId = -1;
  9.     public AGMHumpRoute LEFT;
  10.     public AGMHumpRoute RIGHT;
  11. };
  12.  
  13.     define int StateScan = 0;
  14.     define int StateJnLeft = 1;
  15.     define int StateJnRight = 2;
  16.     define int StateBack = 3;
  17.     define int StateDone = 4;
  18.    
  19.     final AGMHumpRouteScanResult Scan()
  20.     {
  21.         AGMHumpRoute node;
  22.         LTrackSearch it = new LTrackSearch().DistanceLimit(400000.0f)
  23.                                             .SleepAfter(1000)
  24.                                             .DepthLimit(20)
  25.                                             .DetectCycles(true, true)
  26.                                             .FromTrackside(Owner, LTrackSearch.FORWARD, StateScan);
  27.         while (true) {
  28.             Log("S: "+it.State());
  29.             switch (it.State()) {
  30.                 case StateScan: {
  31.                     if (it.MoveNextTrackside()) {
  32.                         Log("obj: "+it.CurrentTrackside().GetLocalisedName());
  33.                         AGMHumpTrackMarker marker = cast <AGMHumpTrackMarker> it.CurrentTrackside();
  34.                         if (marker and marker.HumpTrack >= 0) {
  35.                             Log("mrk: "+marker.GetLocalisedName());
  36.                             if (marker.HumpTrack > 0) {
  37.                                 node = new AGMHumpRoute();
  38.                                 node.TrackNumber = marker.HumpTrack;
  39.                                 node.MarkerId = marker.GetId();
  40.                             }
  41.                             it.SetState(StateBack);
  42.                         }
  43.                         else if (it.CurrentJunction()) {
  44.                             int dir = it.GetArrivedDirection();
  45.                             Log("jn: '"+it.CurrentJunction().GetLocalisedName()+"', dir " + dir);
  46.                             if  (dir == LTrackSearch.BACKWARD) {
  47.                                 node = new AGMHumpRoute();
  48.                                 node.JnId = it.CurrentJunction().GetId();
  49.                                 it.SetState(StateJnLeft, (object)node);
  50.                                 node = null;
  51.                                 it.PushState(StateScan);
  52.                                 it.SetDirection(LTrackSearch.LEFT);
  53.                             }
  54.                         }
  55.                     }
  56.                     else
  57.                         it.SetState(StateBack);
  58.                     continue;
  59.                 }
  60.                 case StateJnLeft: {
  61.                     AGMHumpRoute parent = cast <AGMHumpRoute> it.StateData();
  62.                     parent.LEFT = node;
  63.                     node = null;
  64.                     it.SetState(StateJnRight);
  65.                     it.PushState(StateScan);
  66.                     it.SetDirection(LTrackSearch.RIGHT);
  67.                     continue;
  68.                 }
  69.                 case StateJnRight: {
  70.                     AGMHumpRoute parent = cast <AGMHumpRoute> it.StateData();
  71.                     parent.RIGHT = node;
  72.                     node = parent;
  73.                     it.SetState(StateBack);
  74.                     continue;
  75.                 }
  76.                 case StateBack: {
  77.                     if (it.IsEmpty())
  78.                         break;
  79.                     it.PopState();
  80.                     continue;
  81.                 }
  82.                 default: break;
  83.             }
  84.             break;
  85.         }
  86.        
  87.         AGMHumpRouteScanResult ret = new AGMHumpRouteScanResult();
  88.         LTrackSearchStatus status = it.Status();
  89.         ret.Root = node;
  90.         ret.Overflow = status.Overflow;
  91.         ret.BadJunctionIds = status.BadJunctionIds;
  92.         ret.CyclicJunctionIds = status.CyclicJunctionIds;
  93.         return ret;
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement