Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final class LTrackSearch
- {
- define public int Forward = Junction.DIRECTION_FORWARD;
- define public int Backward = Junction.DIRECTION_BACKWARD;
- define public int Left = Junction.DIRECTION_LEFT;
- define public int Right = Junction.DIRECTION_RIGHT;
- define public int Undefined = Junction.DIRECTION_NONE;
- // Checks if search has reached its iteration and distance limit or track has ended
- public bool EndOfTrack();
- // Returns errors and search status
- public bool HasErrors();
- public bool NoErrors();
- public LTrackSearchStatus Status();
- // Moves search to next object
- public bool MoveNext();
- // Returns object search is currently positioned at
- public object Current();
- // Returns current object facing relative to search direction
- public bool GetFacing();
- // Returns distance to current object from start position
- // Note distance is always increased, even if you have reversed search direction
- public float GetDistance();
- // Moves search to next object of specified class
- public bool MoveNextMapObject();
- public bool MoveNextTrackside();
- public bool MoveNextJunction();
- public MapObject CurrentMapObject();
- public Trackside CurrentTrackside();
- public Junction CurrentJunction();
- // Returns true if current object is Trackside or Junction
- public bool OnTrackside();
- public bool OnJunction();
- // If current object is Trackside or Junction:
- // Calculates which direction has search arrived for current object
- // so if you change direction like:
- // SetDirection(GetArrivedDirection())
- // you will go backwards.
- public int GetArrivedDirection();
- public bool SetDirection(int dir);
- // Stack methods allow you to save current search state
- // for traversing full route graph.
- // Returns true if stack is empty, i.e. PopState has been called
- // same number of times as PushState
- public bool IsEmpty();
- // Saves a copy of current search state.
- // Note that custom state is not saved.
- // Returns true if succeeded, false if depth limit has reached.
- public bool PushState();
- // Deletes current search state and restores previously saved.
- // Returns true if succeeded, false if no state left to restore.
- public bool PopState();
- // Saves a copy of current search state and sets custom state.
- // Equivalent of following code:
- // if (PushState()) {
- // SetState(customState);
- // return true;
- // }
- // return false;
- public bool PushState(int state);
- public bool PushState(object stateData);
- public bool PushState(int state, object stateData);
- // Sets custom state.
- // Useful to replace call stack in graph traverse algorithm to increase
- // maximum traverse depth.
- // Note that thread call stack is approx 100 calls deep.
- public void SetState(int state);
- public void SetState(object stateData);
- public void SetState(int state, object stateData);
- // Returns saved custom state
- public int State();
- public object StateData();
- // Sets distance offset for current search state
- public void SetBaseDistance(float dist);
- // Initialisation methods which allow chaining.
- // Usage:
- // LTrackSearch it = new LTrackSearch().DistanceLimit(4000.0f)
- // .SleepAfter(10000)
- // .DepthLimit(50)
- // .Begin(Owner, LTrackSearch.Forward));
- public LTrackSearch DistanceLimit(float distLimit);
- public LTrackSearch IterationLimit(int iterLimit);
- public LTrackSearch DepthLimit(int depLimit);
- public LTrackSearch SleepAfter(int iters);
- public LTrackSearch DetectCycles(bool enable);
- public LTrackSearch Begin(object origin, int dir);
- public LTrackSearch Begin(object origin, int dir, int initialState);
- /// USAGE
- define int StateScan = 0;
- define int StateJnLeft = 1;
- define int StateJnRight = 2;
- define int StateBack = 3;
- define int StateDone = 4;
- final RFHumpRoute Scan()
- {
- RFHumpRoute node;
- LTrackSearch it = new LTrackSearch().DistanceLimit(4000.0f)
- .SleepAfter(10000)
- .DepthLimit(50)
- .Begin(Owner, LTrackSearch.Forward, StateScan);
- while (true) {
- switch (it.State()) {
- case StateScan: {
- if (it.MoveNextTrackside()) {
- RFHumpMarker marker = cast <RFHumpMarker> it.CurrentTrackside();
- if (marker and marker.HumpTrack >= 0) {
- if (marker.HumpTrack > 0) {
- node = new RFHumpRoute();
- node.TrackNumber = marker.HumpTrack;
- node.Marker = marker;
- }
- it.SetState(StateBack);
- }
- else if (it.OnJunction()) {
- int dir = it.GetArrivedDirection();
- if (dir == LTrackSearch.Backward) {
- node = new RFHumpRoute();
- node.JnId = it.CurrentJunction().GetId();
- it.SetState(StateJnLeft, node);
- it.PushState(StateScan);
- it.SetDirection(LTrackSearch.Left);
- }
- }
- }
- else
- it.SetState(StateBack);
- break;
- }
- case StateJnLeft: {
- RFHumpRoute parent = cast <RFHumpRoute> it.StateData();
- parent.Left = node;
- it.SetState(StateJnRight);
- it.PushState(StateScan);
- it.SetDirection(LTrackSearch.Right);
- break;
- }
- case StateJnRight: {
- RFHumpRoute parent = cast <RFHumpRoute> it.StateData();
- parent.Right = node;
- node = parent;
- it.SetState(StateBack);
- break;
- }
- case StateBack: {
- if (!it.IsEmpty())
- it.PopState();
- break;
- }
- case StateDone:
- default: return node;
- }
- }
- return node;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement