Advertisement
agmike

Final LTrackSearch & Usage

Aug 28th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.77 KB | None | 0 0
  1. final class LTrackSearch
  2. {
  3.     define public int Forward   = Junction.DIRECTION_FORWARD;
  4.     define public int Backward  = Junction.DIRECTION_BACKWARD;
  5.     define public int Left      = Junction.DIRECTION_LEFT;
  6.     define public int Right     = Junction.DIRECTION_RIGHT;
  7.     define public int Undefined = Junction.DIRECTION_NONE;
  8.    
  9.     // Checks if search has reached its iteration and distance limit or track has ended
  10.     public bool EndOfTrack();
  11.    
  12.     // Returns errors and search status
  13.     public bool HasErrors();
  14.     public bool NoErrors();
  15.     public LTrackSearchStatus Status();
  16.    
  17.     // Moves search to next object
  18.     public bool MoveNext();
  19.    
  20.     // Returns object search is currently positioned at
  21.     public object Current();
  22.     // Returns current object facing relative to search direction
  23.     public bool GetFacing();
  24.     // Returns distance to current object from start position
  25.     // Note distance is always increased, even if you have reversed search direction
  26.     public float GetDistance();
  27.    
  28.     // Moves search to next object of specified class
  29.     public bool MoveNextMapObject();
  30.     public bool MoveNextTrackside();
  31.     public bool MoveNextJunction();
  32.     public MapObject CurrentMapObject();
  33.     public Trackside CurrentTrackside();
  34.     public Junction CurrentJunction();
  35.    
  36.     // Returns true if current object is Trackside or Junction
  37.     public bool OnTrackside();
  38.     public bool OnJunction();
  39.    
  40.    
  41.     // If current object is Trackside or Junction:
  42.     // Calculates which direction has search arrived for current object
  43.     // so if you change direction like:
  44.     // SetDirection(GetArrivedDirection())
  45.     // you will go backwards.
  46.     public int GetArrivedDirection();
  47.     public bool SetDirection(int dir);
  48.    
  49.     // Stack methods allow you to save current search state
  50.     // for traversing full route graph.
  51.    
  52.     // Returns true if stack is empty, i.e. PopState has been called
  53.     // same number of times as PushState
  54.     public bool IsEmpty();
  55.    
  56.     // Saves a copy of current search state.
  57.     // Note that custom state is not saved.
  58.     // Returns true if succeeded, false if depth limit has reached.
  59.     public bool PushState();
  60.    
  61.     // Deletes current search state and restores previously saved.
  62.     // Returns true if succeeded, false if no state left to restore.
  63.     public bool PopState();
  64.    
  65.     // Saves a copy of current search state and sets custom state.
  66.     // Equivalent of following code:
  67.     // if (PushState()) {
  68.     //     SetState(customState);
  69.     //     return true;
  70.     // }
  71.     // return false;
  72.     public bool PushState(int state);
  73.     public bool PushState(object stateData);
  74.     public bool PushState(int state, object stateData);
  75.    
  76.     // Sets custom state.
  77.     // Useful to replace call stack in graph traverse algorithm to increase
  78.     // maximum traverse depth.
  79.     // Note that thread call stack is approx 100 calls deep.
  80.     public void SetState(int state);
  81.     public void SetState(object stateData);
  82.     public void SetState(int state, object stateData);
  83.    
  84.     // Returns saved custom state
  85.     public int State();
  86.     public object StateData();
  87.    
  88.     // Sets distance offset for current search state
  89.     public void SetBaseDistance(float dist);
  90.    
  91.     // Initialisation methods which allow chaining.
  92.     // Usage:
  93.     // LTrackSearch it = new LTrackSearch().DistanceLimit(4000.0f)
  94.     //                                     .SleepAfter(10000)
  95.     //                                     .DepthLimit(50)
  96.     //                                     .Begin(Owner, LTrackSearch.Forward));
  97.     public LTrackSearch DistanceLimit(float distLimit);
  98.     public LTrackSearch IterationLimit(int iterLimit);
  99.     public LTrackSearch DepthLimit(int depLimit);
  100.     public LTrackSearch SleepAfter(int iters);
  101.     public LTrackSearch DetectCycles(bool enable);
  102.     public LTrackSearch Begin(object origin, int dir);
  103.     public LTrackSearch Begin(object origin, int dir, int initialState);
  104.    
  105.     /// USAGE
  106.     define int StateScan = 0;
  107.     define int StateJnLeft = 1;
  108.     define int StateJnRight = 2;
  109.     define int StateBack = 3;
  110.     define int StateDone = 4;
  111.    
  112.     final RFHumpRoute Scan()
  113.     {
  114.         RFHumpRoute node;
  115.         LTrackSearch it = new LTrackSearch().DistanceLimit(4000.0f)
  116.                                             .SleepAfter(10000)
  117.                                             .DepthLimit(50)
  118.                                             .Begin(Owner, LTrackSearch.Forward, StateScan);
  119.         while (true) {
  120.             switch (it.State()) {
  121.                 case StateScan: {
  122.                     if (it.MoveNextTrackside()) {
  123.                         RFHumpMarker marker = cast <RFHumpMarker> it.CurrentTrackside();
  124.                         if (marker and marker.HumpTrack >= 0) {
  125.                             if (marker.HumpTrack > 0) {
  126.                                 node = new RFHumpRoute();
  127.                                 node.TrackNumber = marker.HumpTrack;
  128.                                 node.Marker = marker;
  129.                             }
  130.                             it.SetState(StateBack);
  131.                         }
  132.                         else if (it.OnJunction()) {
  133.                             int dir = it.GetArrivedDirection();
  134.                             if  (dir == LTrackSearch.Backward) {
  135.                                 node = new RFHumpRoute();
  136.                                 node.JnId = it.CurrentJunction().GetId();
  137.                                 it.SetState(StateJnLeft, node);
  138.                                 it.PushState(StateScan);
  139.                                 it.SetDirection(LTrackSearch.Left);
  140.                             }
  141.                         }
  142.                     }
  143.                     else
  144.                         it.SetState(StateBack);
  145.                     break;
  146.                 }
  147.                 case StateJnLeft: {
  148.                     RFHumpRoute parent = cast <RFHumpRoute> it.StateData();
  149.                     parent.Left = node;
  150.                     it.SetState(StateJnRight);
  151.                     it.PushState(StateScan);
  152.                     it.SetDirection(LTrackSearch.Right);
  153.                     break;
  154.                 }
  155.                 case StateJnRight: {
  156.                     RFHumpRoute parent = cast <RFHumpRoute> it.StateData();
  157.                     parent.Right = node;
  158.                     node = parent;
  159.                     it.SetState(StateBack);
  160.                     break;
  161.                 }
  162.                 case StateBack: {
  163.                     if (!it.IsEmpty())
  164.                         it.PopState();
  165.                     break;
  166.                 }
  167.                 case StateDone:
  168.                 default: return node;
  169.             }
  170.         }
  171.         return node;
  172.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement