Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BEFORE
- final class RFHumpRouteScanner
- {
- public RFHumpController Owner;
- public float MaxSearchDistance = 5000.0f;
- public bool Overflow = false;
- public int BadJunctionId = -1;
- public RFHumpRoute Scan();
- // ****************************************************
- //
- // I M P L E M E N T A T I O N
- //
- // ****************************************************
- int ops = 0;
- int hops = 0;
- float distance = 0.0f;
- // final void L(string l) { Interface.Log(l); }
- // final string LN(MapObject o) { return "'" + o.GetLocalisedName() + "' (" + o.GetId()+")"; }
- // final string CN(MapObject o) { return o.GetGSClassName() + ", asset: "+o.GetAsset().GetKUID().GetLogString(); }
- final void IncOps() { if (++ops >= 10000) { ops = 0; Owner.Sleep(0.01f); } }
- final bool IncHops() { Overflow = Overflow or ++hops > 50; return !Overflow; }
- final void DecHops() { --hops; }
- final int GetJunctionDirectionToObject(Junction jnc, Trackside origin)
- {
- int[] directions = new int[4];
- int dirCount = 0;
- directions[dirCount++] = Junction.DIRECTION_BACKWARD;
- directions[dirCount++] = Junction.DIRECTION_LEFT;
- directions[dirCount++] = Junction.DIRECTION_RIGHT;
- directions[dirCount++] = Junction.DIRECTION_FORWARD;
- int originDirection = Junction.DIRECTION_NONE;
- Trackside[] firstInDirection = new Trackside[dirCount];
- bool ambiguity = false;
- float searchRestartDistance = 5000.0f;
- int i;
- for(i = 0; i < dirCount; i++) {
- float totalDistance = 0.0f;
- GSTrackSearch gs = jnc.BeginTrackSearch(directions[i]);
- if (!gs) continue;
- while (gs.SearchNextObject()) {
- Trackside found = cast <Trackside> gs.GetObject();
- if (!found) continue;
- int j;
- for (j = 0; j < dirCount; ++j)
- ambiguity = ambiguity or firstInDirection[j] == found;
- if (!firstInDirection[i])
- firstInDirection[i] = found;
- if(found == origin) {
- ambiguity = ambiguity or originDirection != Junction.DIRECTION_NONE;
- originDirection = directions[i];
- break;
- }
- float distance = totalDistance + gs.GetDistance();
- if (distance > MaxSearchDistance or found.isclass(Junction) or ambiguity)
- break;
- if(distance >= searchRestartDistance) {
- totalDistance = distance;
- gs = found.BeginTrackSearch(gs.GetFacingRelativeToSearchDirection());
- }
- IncOps();
- }
- }
- if (ambiguity)
- return Junction.DIRECTION_NONE;
- return originDirection;
- }
- RFHumpRoute Scan(GSTrackSearch it, Trackside origin)
- {
- if (!IncHops())
- return null;
- RFHumpRoute ret = null;
- Trackside prev = origin, found;
- while (it.SearchNextObject()) {
- if (!(found = cast <Trackside> it.GetObject()))
- continue;
- RFHumpMarker marker;
- Junction junc;
- if ((marker = cast <RFHumpMarker> found) and marker.HumpTrack >= 0) {
- if (marker.HumpTrack > 0) {
- ret = new RFHumpRoute();
- ret.TrackNumber = marker.HumpTrack;
- ret.MarkerId = marker.GetId();
- }
- break;
- }
- else if (junc = cast <Junction> found) {
- int dir = GetJunctionDirectionToObject(junc, prev);
- if (dir == Junction.DIRECTION_BACKWARD) {
- ret = new RFHumpRoute();
- ret.JnId = junc.GetId();
- ret.Left = Scan(junc.BeginTrackSearch(Junction.DIRECTION_LEFT), junc);
- ret.Right = Scan(junc.BeginTrackSearch(Junction.DIRECTION_RIGHT), junc);
- }
- else if (dir == Junction.DIRECTION_LEFT) {
- ret = new RFHumpRoute();
- ret.JnId = junc.GetId();
- ret.Left = Scan(junc.BeginTrackSearch(Junction.DIRECTION_BACKWARD), junc);
- }
- else if (dir == Junction.DIRECTION_RIGHT) {
- ret = new RFHumpRoute();
- ret.JnId = junc.GetId();
- ret.Right = Scan(junc.BeginTrackSearch(Junction.DIRECTION_BACKWARD), junc);
- }
- else {
- BadJunctionId = junc.GetId();
- }
- break;
- }
- if (it.GetDistance() > MaxSearchDistance)
- break;
- prev = found;
- IncOps();
- }
- DecHops();
- return ret;
- }
- public RFHumpRoute Scan()
- {
- Overflow = false;
- BadJunctionId = -1;
- return Scan(Owner.BeginTrackSearch(true), Owner);
- }
- };
- AFTER
- final class RFHumpJuncDirSearch isclass RFHumpTrackSearchObserver
- {
- public Junction Jn;
- public int Dir;
- public Trackside Origin;
- public Trackside[] FirstObjects;
- public Trackside First = null;
- public bool OriginFound = false;
- public bool FirstConflict = false;
- public GSTrackSearch Begin() { return Jn.BeginTrackSearch(Dir); }
- public bool Next(RFHumpTrackSearchCursor cursor)
- {
- Trackside obj = cast <Trackside> cursor.GetObject();
- if (obj) {
- if (!First)
- First = obj;
- OriginFound = obj == Origin;
- int i, count = FirstObjects.size();
- for (i = 0; i < count; ++i)
- if (FirstObjects[i] == obj)
- FirstConflict = true;
- }
- return !OriginFound and !FirstConflict;
- }
- };
- public int GetJunctionDirectionToObject(Junction jnc, Trackside origin)
- {
- int[] directions = new int[4];
- int dirCount = 0;
- directions[dirCount++] = Junction.DIRECTION_BACKWARD;
- directions[dirCount++] = Junction.DIRECTION_LEFT;
- directions[dirCount++] = Junction.DIRECTION_RIGHT;
- directions[dirCount++] = Junction.DIRECTION_FORWARD;
- int originDirection = Junction.DIRECTION_NONE;
- Trackside[] firstInDirection = new Trackside[dirCount];
- bool ambiguity = false;
- int i;
- for(i = 0; i < dirCount; i++) {
- RFHumpJuncDirSearch s = new RFHumpJuncDirSearch();
- s.Jn = jnc;
- s.Dir = directions[i];
- s.Origin = origin;
- s.FirstObjects = firstInDirection;
- RFHumpTrackSearch.Begin(s, 0, 0, 0);
- if (s.FirstConflict or s.OriginFound and originDirection != Junction.DIRECTION_NONE)
- return Junction.DIRECTION_NONE;
- if(s.OriginFound)
- originDirection = directions[i];
- firstInDirection[i] = s.First;
- }
- return originDirection;
- }
- final class RFHumpRoutesSearch isclass RFHumpTrackSearchObserver
- {
- public GSTrackSearch Search;
- public int Dir;
- public Trackside Origin;
- public int Hops = 0;
- public int MaxHops = 20;
- public RFHumpRoute Root = null;
- public bool Overflow = false;
- public int BadJunctionId = 0;
- public Trackside Prev;
- public bool IsOk() { return !Overflow and !BadJunctionId; }
- public RFHumpRoute Scan(GSTrackSearch it, Trackside origin)
- {
- if (!IsOk())
- return null;
- RFHumpRoutesSearch s = new RFHumpRoutesSearch();
- s.Search = it;
- s.Origin = origin;
- s.Hops = Hops + 1;
- s.MaxHops = MaxHops;
- RFHumpTrackSearch.Begin(s, 1000000, 0, 0);
- if (s.Overflow) Overflow = s.Overflow;
- if (s.BadJunctionId) BadJunctionId = s.BadJunctionId;
- return s.Root;
- }
- public GSTrackSearch Begin()
- {
- Prev = Origin;
- if (Hops < MaxHops)
- return Search;
- Overflow = true;
- return null;
- }
- public bool Next(RFHumpTrackSearchCursor cursor)
- {
- Trackside current = cast <Trackside> cursor.GetObject();
- if (current) {
- RFHumpMarker marker = cast <RFHumpMarker> current;
- Junction junc = cast <Junction> current;
- if (marker and marker.HumpTrack >= 0) {
- if (marker.HumpTrack > 0) {
- Root = new RFHumpRoute();
- Root.TrackNumber = marker.HumpTrack;
- Root.MarkerId = marker.GetId();
- }
- return false;
- }
- else if (junc) {
- int dir = RFHumpUtils.GetJunctionDirectionToObject(junc, Prev);
- if (dir == Junction.DIRECTION_BACKWARD) {
- Root = new RFHumpRoute();
- Root.JnId = junc.GetId();
- Root.Left = Scan(junc.BeginTrackSearch(Junction.DIRECTION_LEFT), junc);
- Root.Right = Scan(junc.BeginTrackSearch(Junction.DIRECTION_RIGHT), junc);
- }
- else if (dir == Junction.DIRECTION_LEFT) {
- Root = new RFHumpRoute();
- Root.JnId = junc.GetId();
- Root.Left = Scan(junc.BeginTrackSearch(Junction.DIRECTION_BACKWARD), junc);
- }
- else if (dir == Junction.DIRECTION_RIGHT) {
- Root = new RFHumpRoute();
- Root.JnId = junc.GetId();
- Root.Right = Scan(junc.BeginTrackSearch(Junction.DIRECTION_BACKWARD), junc);
- }
- else
- BadJunctionId = junc.GetId();
- return false;
- }
- Prev = current;
- }
- return IsOk();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement