Пример #1
0
 public static void SetStationName(TourCartStation station, string stationname)
 {
     if (string.IsNullOrEmpty(stationname))
     {
         Debug.LogWarning("TourStationWindow trying to set name for null or empty string!");
     }
     if (station.TrackNetwork != null && station.TrackNetwork.TourCartStations.ContainsKey(stationname))
     {
         Debug.LogWarning("User attempted to duplicate Tour Cart Station names");
         return;
     }
     if (!string.IsNullOrEmpty(station.StationName) && station.TrackNetwork != null && station.TrackNetwork.TourCartStations.ContainsKey(station.StationName))
     {
         station.TrackNetwork.TourCartStations.Remove(station.StationName);
     }
     station.StationName = stationname;
     if (station.TrackNetwork != null)
     {
         station.TrackNetwork.TourCartStations.Add(station.StationName, station);
     }
     if (!WorldScript.mbIsServer)
     {
         NetworkManager.instance.SendInterfaceCommand(InterfaceName, InterfaceStationName, stationname, null, station, 0f);
     }
     TourStationWindow.networkredraw = true;
     station.RequestImmediateNetworkUpdate();
     station.MarkDirtyDelayed();
 }
    /// <summary>
    ///     Reassigns tour carts to new track network based on connected junctions
    /// </summary>
    /// <param name="original">Track network to transfer the stations from</param>
    public void ReassignTourCartStations(FreightTrackNetwork original)
    {
        if (original == null || original.TourCartStations.Count == 0)
        {
            return;
        }
        List <string> keys  = original.TourCartStations.Keys.ToList();
        int           count = keys.Count;

        for (int n = 0; n < count; n++)
        {
            if (string.IsNullOrEmpty(keys[n]) || !original.TourCartStations.ContainsKey(keys[n]))
            {
                continue;
            }
            TourCartStation station = original.TourCartStations[keys[n]];
            if (station.ClosestJunction == null || station.TrackNetwork == null)
            {
                continue;
            }
            if (station.ClosestJunction.TrackNetwork == this && station.TrackNetwork.TourCartStations.ContainsKey(keys[n]) && !this.TourCartStations.ContainsKey(keys[n]))
            {
                station.TrackNetwork.TourCartStations.Remove(keys[n]);
                this.TourCartStations.Add(keys[n], station);
            }
        }
    }
Пример #3
0
    public override void SpawnWindow(SegmentEntity targetEntity)
    {
        TourCartStation station = targetEntity as TourCartStation;

        //Catch for when the window is called on an inappropriate machine
        if (station == null)
        {
            GenericMachinePanelScript.instance.Hide();
            UIManager.RemoveUIRules("Machine");
            return;
        }
        UIUtil.UIdelay = 0;
        UIUtil.UILock  = true;

        if (this.SetName || string.IsNullOrEmpty(station.StationName))
        {
            this.manager.SetTitle("Tour Cart Station - Set Name");
            UIManager.mbEditingTextField = true;
            UIManager.AddUIRules("TextEntry", UIRules.RestrictMovement | UIRules.RestrictLooking | UIRules.RestrictBuilding | UIRules.RestrictInteracting | UIRules.SetUIUpdateRate);
            GenericMachinePanelScript.instance.Scroll_Bar.GetComponent <UIScrollBar>().scrollValue = 0.0f;

            this.manager.AddButton("namecancel", "Cancel", 100, 0);
            this.manager.AddBigLabel("nametitle", "Enter Network ID", Color.white, 50, 40);
            this.manager.AddBigLabel("nameentry", "_", Color.cyan, 50, 65);
        }
        else
        {
            // Select destination
            this.manager.SetTitle("Select Tour Cart Destination");
            this.manager.AddButton("namestation", "Change Name", 15, 0);
            this.manager.AddBigLabel("stationname", station.StationName, Color.white, 165, 0);

            int spacing = 50; //Spacing between each registry line
            int yoffset = 65; //Offset below button row
            if (station.TrackNetwork != null && station.TrackNetwork.TourCartStations.Count != 0)
            {
                List <string> keys = station.TrackNetwork.TourCartStations.Keys.ToList();
                keys.Remove(station.StationName);
                for (int n = 0; n < keys.Count; n++)
                {
                    this.manager.AddButton("tourstation" + n, keys[n], 100, yoffset + (n * spacing));
                }
                if (keys.Count == 0)
                {
                    this.manager.AddBigLabel("nostations", "Add Additional Tour Cart Stations", Color.red, 0, yoffset);
                    this.manager.AddBigLabel("nostations1", "to Track Network...", Color.red, 0, yoffset + 22);
                }
            }
            else
            {
                this.manager.AddBigLabel("nostations", "Connect Tour Cart Station to", Color.red, 0, yoffset);
                this.manager.AddBigLabel("nostations1", "a Freight Track Network...", Color.red, 0, yoffset + 22);
            }
        }
        //dirty = true;
        networkredraw = false;
    }
Пример #4
0
    public override void UpdateMachine(SegmentEntity targetEntity)
    {
        TourCartStation station = targetEntity as TourCartStation;

        //Catch for when the window is called on an inappropriate machine
        if (station == null)
        {
            GenericMachinePanelScript.instance.Hide();
            UIManager.RemoveUIRules("Machine");
            return;
        }
        UIUtil.UIdelay = 0;

        if (networkredraw)
        {
            this.manager.RedrawWindow();
        }

        if (this.SetName || string.IsNullOrEmpty(station.StationName))
        {
            this.Counter++;
            foreach (char c in Input.inputString)
            {
                if (c == "\b"[0])  //Backspace
                {
                    if (this.EntryString.Length != 0)
                    {
                        this.EntryString = this.EntryString.Substring(0, this.EntryString.Length - 1);
                    }
                }
                else if (c == "\n"[0] || c == "\r"[0]) //Enter or Return
                {
                    TourStationWindow.SetStationName(station, this.EntryString);
                    this.SetName                 = false;
                    this.EntryString             = "";
                    UIManager.mbEditingTextField = false;
                    UIManager.RemoveUIRules("TextEntry");
                    return;
                }
                else
                {
                    this.EntryString += c;
                }
            }
            this.manager.UpdateLabel("nameentry", this.EntryString + (this.Counter % 20 > 10 ? "_" : ""), Color.cyan);
            //dirty = true;
            return;
        }
    }
Пример #5
0
 public void TravelTo(TourCartStation station, FreightTrackJunction start)
 {
     if (station != null && station.JunctionDirection != -1 && station.ClosestJunction != null)
     {
         string mobtype = this.GetSpawnedCartType();
         if (string.IsNullOrEmpty(mobtype))
         {
             return;
         }
         FreightCartMob tourcart = MobManager.instance.SpawnMob(MobType.Mod, mobtype, this.mSegment, this.mnX, this.mnY + (long)this.mForwards.y + 1L, this.mnZ, new Vector3(0.0f, 0.0f, 0.0f), this.mForwards) as FreightCartMob;
         ManagerSync.TourCart          = tourcart;
         tourcart.DestinationJunction  = station.ClosestJunction;
         tourcart.DestinationDirection = station.JunctionDirection;
         tourcart.JunctionRoute        = station.TrackNetwork.RouteFind(start, station.ClosestJunction);
         tourcart.NextJunction         = start;
     }
 }
Пример #6
0
    public static NetworkInterfaceResponse HandleNetworkCommand(Player player, NetworkInterfaceCommand nic)
    {
        TourCartStation station = nic.target as TourCartStation;

        string command = nic.command;

        if (command != null)
        {
            if (command == InterfaceStationName)
            {
                TourStationWindow.SetStationName(station, nic.payload);
            }
        }

        return(new NetworkInterfaceResponse
        {
            entity = station,
            inventory = player.mInventory
        });
    }
Пример #7
0
    public override bool ButtonClicked(string name, SegmentEntity targetEntity)
    {
        TourCartStation station = targetEntity as TourCartStation;

        if (name == "namestation")
        {
            this.SetName = true;
            this.manager.RedrawWindow();
            return(true);
        }
        else if (name == "namecancel")
        {
            this.SetName = false;
            this.manager.RedrawWindow();
        }
        else if (name.Contains("tourstation")) // drag drop to a slot
        {
            if (station.TrackNetwork == null || station.ClosestJunction == null)
            {
                Debug.LogWarning("TourStationWindow attemption to travel on null network or from null junction");
                return(false);
            }
            int slotNum = -1;
            int.TryParse(name.Replace("tourstation", ""), out slotNum); //Get slot name as number
            List <string> keys = station.TrackNetwork.TourCartStations.Keys.ToList();
            keys.Remove(station.StationName);

            if (slotNum > -1) // valid slot
            {
                station.TravelTo(station.TrackNetwork.TourCartStations[keys[slotNum]], station.ClosestJunction);
            }
            GenericMachinePanelScript.instance.Hide();
            UIManager.RemoveUIRules("Machine");
            return(true);
        }

        return(false);
    }
Пример #8
0
    /// <summary>
    ///     Follows a track segment to find all containing stations until it reaches another junction or determines track is invalid
    /// </summary>
    /// <param name="direction">0 - 3 representing the four directions out of the junction</param>
    /// <returns>True if it found complete segment</returns>
    public bool TrackFollow(int direction)
    {
        //Initialize the check from the junction
        long    nextX  = this.mnX;
        long    nextY  = this.mnY;
        long    nextZ  = this.mnZ;
        Vector3 dirvec = new Vector3();

        //Store the initial junction direction for later recording which direction the connected junction is associated with
        int initialdirection = direction;

        //List of freight cart stations found on this segment -> to be written to the final constructed FreightTrackSegment
        List <FreightCartStation> SegmentStations = new List <FreightCartStation>();

        //Store visited track pieces for catching when the segment enters a closed loop
        List <TrackPiece> VisitedTracks = new List <TrackPiece>();

        //Add a penalty for pathfinding in certain directions to avoid stations and other undesirable routes
        int PathfindPenalty = 0;

        //Begin loop here.  Direction can be set and used to check the next location each time through the loop
        //Allow segments only up to 2048 long due to cost of loop checking - may revise after testing
        for (int n = 0; n < 2048; n++)
        {
            switch (direction)
            {
            case 0:
                nextX++;
                dirvec = Vector3.right;
                break;

            case 1:
                nextZ++;
                dirvec = Vector3.forward;
                break;

            case 2:
                nextX--;
                dirvec = Vector3.left;
                break;

            case 3:
                nextZ--;
                dirvec = Vector3.back;
                break;

            default:
                nextX++;
                break;
            }

            ushort lValue1 = 0;
            byte   lFlags1 = 0;
            ushort type    = this.GetCube(nextX, nextY, nextZ, out lValue1, out lFlags1);
            this.mUnderSegment = this.mPrevGetSeg;
            //Debug.LogWarning("GetCube type: " + type.ToString() + " value: " + lValue1);
            bool foundslope = false;

            //Found air and need to check for a downward slope under it
            if (type == 1)
            {
                ushort  lValue2 = 0;
                byte    lFlags2 = 0;
                ushort  cube    = this.GetCube(nextX, nextY - 1L, nextZ, out lValue2, out lFlags2);
                Segment segment = this.mPrevGetSeg;
                type    = cube;
                lFlags1 = lFlags2;
                lValue1 = lValue2;
                if ((type == 538 && lValue1 == 2) || (type == ScrapTrackType && lValue1 == ScrapSlopeVal))
                {
                    foundslope = true;
                    nextY--; //decrement Y level for next loop through!
                }
                else
                {
                    if (type == 0)
                    {
                        Debug.LogError("Error, track follower has null under segment!");
                    }
                    if (this.mPrevGetSeg == null)
                    {
                        Debug.LogError("Error, prevseg was null!");
                    }
                    if (segment == null)
                    {
                        Debug.LogError("Error, old was null!");
                    }
                    if (this.mPrevGetSeg != segment)
                    {
                        Debug.LogWarning(("Track follower is looking for a slope, and has had to check across segment boundaries for this![Old/New" + segment.GetName() + " -> " + this.mPrevGetSeg.GetName()));
                    }
                    return(false);
                }
            }

            Vector3 trackvec = SegmentCustomRenderer.GetRotationQuaternion(lFlags1) * Vector3.forward;
            bool    oneway   = false;
            trackvec.Normalize();
            trackvec.x = trackvec.x >= -0.5 ? (trackvec.x <= 0.5 ? 0.0f : 1f) : -1f;
            trackvec.y = trackvec.y >= -0.5 ? (trackvec.y <= 0.5 ? 0.0f : 1f) : -1f;
            trackvec.z = trackvec.z >= -0.5 ? (trackvec.z <= 0.5 ? 0.0f : 1f) : -1f;
            //Begin checking track type
            if (type == TRACKTYPE || type == ScrapTrackType)
            {
                if ((type == TRACKTYPE && (lValue1 == TRACKSTRAIGHT || lValue1 == TRACKEMPTY || lValue1 == TRACKFULL)) || (type == ScrapTrackType && lValue1 == ScrapStraightVal))
                {
                    if (trackvec.y > 0.5 || trackvec.y < -0.5)
                    {
                        return(false);
                    }
                    else if (!(trackvec == dirvec) && !(trackvec == -dirvec))
                    {
                        dirvec = new Vector3(trackvec.x, 0f, trackvec.z);
                        oneway = true; // Can't set return path as the same -> they're different!
                    }
                }
                if ((type == TRACKTYPE && lValue1 == TRACKCORNER) || (type == ScrapTrackType && lValue1 == ScrapCornerVal))
                {
                    if (dirvec == new Vector3(-trackvec.z, 0.0f, trackvec.x))
                    {
                        dirvec = new Vector3(dirvec.z, 0.0f, -dirvec.x);
                    }
                    else if (trackvec == -dirvec)
                    {
                        dirvec = new Vector3(-dirvec.z, 0.0f, dirvec.x);
                    }
                    else
                    {
                        return(false);
                    }
                }
                if ((type == TRACKTYPE && lValue1 == TRACKSLOPE) || (type == ScrapTrackType && lValue1 == ScrapSlopeVal))
                {
                    Vector3 vector3_2 = trackvec;
                    dirvec.y = 0.0f;
                    dirvec.Normalize();
                    if (dirvec == trackvec)
                    {
                        if (foundslope)
                        {
                            return(false);
                        }
                        else
                        {
                            nextY++;
                        }
                    }
                    else if (dirvec == -trackvec)
                    {
                        ;
                    }
                }
                if (type == TRACKTYPE && lValue1 == TRACKBUFFER)
                {
                    dirvec = new Vector3(-dirvec.x, 0f, -dirvec.z);
                }
            }
            //Begin checking special types
            else if (type == CONTROLTYPE)
            {
                if (lValue1 == CONTROLLOAD || lValue1 == CONTROLUNLOAD || lValue1 == CONTROLTURBO)
                {
                    if ((trackvec == dirvec) || (trackvec == -dirvec))
                    {
                        //Do nothing... direction doesn't change
                        //Except turbo... reduce the penalty for this path!
                        if (lValue1 == CONTROLTURBO)
                        {
                            PathfindPenalty--;
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            //Check for freight stations
            else if (type == FREIGHTSTATIONTYPE)
            {
                if ((trackvec == dirvec) || (trackvec == -dirvec))
                {
                    Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
                    if (segment == null)
                    {
                        segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
                        if (segment == null)
                        {
                            Debug.Log((object)"Track junction track follower did not find segment");
                            return(false);
                        }
                    }
                    FreightCartStation fcs = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as FreightCartStation;
                    if (fcs == null)
                    {
                        Debug.LogWarning("Track Junction Track Follower tried to get a freight cart station but got other mod machine instead?");
                        return(false);
                    }
                    if (!SegmentStations.Contains(fcs))
                    {
                        SegmentStations.Add(fcs);
                    }
                    fcs.ClosestJunction   = this;
                    fcs.JunctionDirection = initialdirection;
                    // Penalize this route for multidirection pathfinding due to the station
                    PathfindPenalty += 5;
                }
                else
                {
                    return(false);
                }
            }
            //Is it a junction?
            else if (type == JUNCTIONTYPE)
            {
                //Debug.LogWarning("Track follower success!  Found another junction!");
                Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
                if (segment == null)
                {
                    segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
                    if (segment == null)
                    {
                        Debug.Log((object)"Track junction track follower did not find segment");
                        return(false);
                    }
                }
                FreightTrackJunction junction = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as FreightTrackJunction;
                if (junction == null)
                {
                    Debug.LogWarning("Track Junction Track Follower tried to get a track junction but got other mod machine instead?");
                    return(false);
                }
                this.ConnectedJunctions[initialdirection] = junction;
                //Don't let segment distance be negative just to be safe!  This should rarely happen...
                if (PathfindPenalty < 0 && Math.Abs(PathfindPenalty) > n)
                {
                    PathfindPenalty = -n;
                }
                FreightTrackSegment tracksegment = new FreightTrackSegment(this, junction, n + 1 + PathfindPenalty);
                tracksegment.Stations = SegmentStations;
                //Debug.LogWarning("trackseg station count: " + tracksegment.Stations.Count);
                this.ConnectedSegments[initialdirection] = tracksegment;
                this.SegmentDistances[initialdirection]  = n + 1;
                this.LinkStatusDirty = true;

                //handle the connection for the other junction so we don't need to double the work - only if return path is valid!
                //Mirror the direction to reflect the correct side of the connecting junction
                if (!oneway)
                {
                    int mirroreddir = direction += 2;
                    if (mirroreddir > 3)
                    {
                        mirroreddir -= 4;
                    }
                    junction.ConnectedJunctions[mirroreddir] = this;
                    junction.ConnectedSegments[mirroreddir]  = tracksegment;
                    junction.SegmentDistances[mirroreddir]   = n + 1;
                    junction.LinkStatusDirty = true;
                }
                return(true);
            }
            else if (type == TOURSTATIONTYPE)
            {
                if (trackvec != -dirvec)
                {
                    return(false);
                }
                Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
                if (segment == null)
                {
                    segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
                    if (segment == null)
                    {
                        Debug.Log((object)"Track junction track follower did not find segment");
                        return(false);
                    }
                }
                TourCartStation station = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as TourCartStation;
                station.TrackNetwork      = this.TrackNetwork;
                station.ClosestJunction   = this;
                station.JunctionDirection = initialdirection;
                this.ConnectedJunctions[initialdirection] = this;
                FreightTrackSegment tracksegment = new FreightTrackSegment(this, this, 2 * n + 1);
                this.SegmentDistances[initialdirection]  = 2 * n + 1;
                this.ConnectedSegments[initialdirection] = tracksegment;
                this.LinkStatusDirty = true;
                if (!string.IsNullOrEmpty(station.StationName) && !this.TrackNetwork.TourCartStations.ContainsKey(station.StationName))
                {
                    this.TrackNetwork.TourCartStations.Add(station.StationName, station);
                }
                return(true);
            }
            else
            {
                return(false);   //Not a track type
            }
            //Update the direction int based on the changed direction vector
            if (dirvec == Vector3.right)
            {
                direction = 0;
            }
            else if (dirvec == Vector3.forward)
            {
                direction = 1;
            }
            else if (dirvec == Vector3.left)
            {
                direction = 2;
            }
            else if (dirvec == Vector3.back)
            {
                direction = 3;
            }

            TrackPiece visitedpiece = new TrackPiece(new Vector3(nextX - this.mnX, nextY - this.mnY, nextZ - this.mnZ), direction);
            //Debug.LogWarning("Visited track piece: " + new Vector4(nextX - this.mnX, nextY - mnY, nextZ - mnZ, direction).ToString());
            //Store every track piece and check every 10th for monitoring for closed, endless loops of track
            if (n % 10 == 0)
            {
                int count = VisitedTracks.Count;
                for (int m = 0; m < count; m++)
                {
                    TrackPiece piece = VisitedTracks[m];
                    if (piece.Position == visitedpiece.Position && piece.Direction == visitedpiece.Direction)
                    {
                        //Debug.LogWarning("piece position: " + piece.Position.ToString() + " visited: " + visitedpiece.Position.ToString());
                        Debug.LogWarning("TrackJunction followed track route and found a closed loop.  Ending search.");
                        return(false);
                    }
                }
            }
            VisitedTracks.Add(visitedpiece);
            if (n == 2047)
            {
                Debug.LogWarning("Track Junction Found track length > 2048m -> ending search.");
            }
        }
        return(false);
    }
Пример #9
0
    /// <summary>
    ///     Follows a track segment to find all containing stations until it reaches another junction or determines track is invalid
    /// </summary>
    /// <param name="direction">0 - 3 representing the four directions out of the junction</param>
    /// <returns>True if it found complete segment</returns>
    public bool TrackFollow(int direction)
    {
        //Initialize the check from the junction
        long    nextX    = this.mnX;
        long    nextY    = this.mnY;
        long    nextZ    = this.mnZ;
        Vector3 dirvec   = new Vector3();
        bool    mirrorOk = true;

        //Store the initial junction direction for later recording which direction the connected junction is associated with
        int initialdirection = direction;

        //There are many ways to derail, so set that result now, and assume it later. Saves much repeat of these lines.
        this.DirectionResults [initialdirection]   = FreightTrackDirectionResults.Bad;
        this.ConnectedJunctions [initialdirection] = null;
        this.ConnectedSegments [initialdirection]  = null;

        //List of freight cart stations found on this segment -> to be written to the final constructed FreightTrackSegment
        List <FreightCartStation> SegmentStations = new List <FreightCartStation>();

        //Store visited track pieces for catching when the segment enters a closed loop
        //We're only testing for a unique key here, so no Tvalue will be used.
        StringDictionary VisitedTracks = new StringDictionary();

        //Begin loop here.  Direction can be set and used to check the next location each time through the loop
        //Allow segments only up to 512m long due to cost of loop checking - may revise after testing
        for (this.SegmentDistances[initialdirection] = 0; this.SegmentDistances[initialdirection] < 2048; this.SegmentDistances[initialdirection]++)
        {
            switch (direction)
            {
            case 0:
                nextX++;
                dirvec = Vector3.right;
                break;

            case 1:
                nextZ++;
                dirvec = Vector3.forward;
                break;

            case 2:
                nextX--;
                dirvec = Vector3.left;
                break;

            case 3:
                nextZ--;
                dirvec = Vector3.back;
                break;

            default:
                nextX++;
                break;
            }

            ushort lValue1 = 0;
            byte   lFlags1 = 0;
            ushort type    = this.GetCube(nextX, nextY, nextZ, out lValue1, out lFlags1);
            this.mUnderSegment = this.mPrevGetSeg;
            //Debug.LogWarning("GetCube type: " + type.ToString() + " value: " + lValue1);
            bool foundslope = false;

            //Found air and need to check for a downward slope under it
            if (type == 1)
            {
                ushort  lValue2 = 0;
                byte    lFlags2 = 0;
                ushort  cube    = this.GetCube(nextX, nextY - 1L, nextZ, out lValue2, out lFlags2);
                Segment segment = this.mPrevGetSeg;
                type    = cube;
                lFlags1 = lFlags2;
                lValue1 = lValue2;
                if ((type == 538 && lValue1 == 2) || (type == ScrapTrackType && lValue1 == ScrapSlopeVal))
                {
                    foundslope = true;
                    nextY--; //decrement Y level for next loop through!
                }
                else
                {
                    if (type == 0)
                    {
                        Debug.LogError("Error, track follower has null under segment!");
                    }
                    if (this.mPrevGetSeg == null)
                    {
                        Debug.LogError("Error, prevseg was null!");
                    }
                    if (segment == null)
                    {
                        Debug.LogError("Error, old was null!");
                    }
                    if (this.mPrevGetSeg != segment)
                    {
                        Debug.LogWarning(("Track follower is looking for a slope, and has had to check across segment boundaries for this![Old/New" + segment.GetName() + " -> " + this.mPrevGetSeg.GetName()));
                    }
                    return(false);
                }
            }

            Vector3 trackvec = SegmentCustomRenderer.GetRotationQuaternion(lFlags1) * Vector3.forward;
            trackvec.Normalize();
            trackvec.x = trackvec.x >= -0.5 ? (trackvec.x <= 0.5 ? 0.0f : 1f) : -1f;
            trackvec.y = trackvec.y >= -0.5 ? (trackvec.y <= 0.5 ? 0.0f : 1f) : -1f;
            trackvec.z = trackvec.z >= -0.5 ? (trackvec.z <= 0.5 ? 0.0f : 1f) : -1f;
            //Begin checking track type
            if (type == TRACKTYPE || type == ScrapTrackType)
            {
                if ((type == TRACKTYPE && (lValue1 == TRACKSTRAIGHT || lValue1 == TRACKEMPTY || lValue1 == TRACKFULL)) || (type == ScrapTrackType && lValue1 == ScrapStraightVal))
                {
                    if (trackvec.y > 0.5 || trackvec.y < -0.5)
                    {
                        return(false);
                    }
                    else if (!(trackvec == dirvec) && !(trackvec == -dirvec))
                    {
                        // Came in from the side, this path is one-way.
                        mirrorOk = false;
                        dirvec   = new Vector3(trackvec.x, 0f, trackvec.z);
                    }
                }
                if ((type == TRACKTYPE && lValue1 == TRACKCORNER) || (type == ScrapTrackType && lValue1 == ScrapCornerVal))
                {
                    if (dirvec == new Vector3(-trackvec.z, 0.0f, trackvec.x))
                    {
                        dirvec = new Vector3(dirvec.z, 0.0f, -dirvec.x);
                    }
                    else if (trackvec == -dirvec)
                    {
                        dirvec = new Vector3(-dirvec.z, 0.0f, dirvec.x);
                    }
                    else
                    {
                        return(false);
                    }
                }
                if ((type == TRACKTYPE && lValue1 == TRACKSLOPE) || (type == ScrapTrackType && lValue1 == ScrapSlopeVal))
                {
                    Vector3 vector3_2 = trackvec;
                    dirvec.y = 0.0f;
                    dirvec.Normalize();
                    if (dirvec == trackvec)
                    {
                        if (foundslope)
                        {
                            return(false);
                        }
                        else
                        {
                            nextY++;
                        }
                    }
                    else if (dirvec == -trackvec)
                    {
                        ;
                    }
                }
                if (type == TRACKTYPE && lValue1 == TRACKBUFFER)
                {
                    dirvec = new Vector3(-dirvec.x, 0f, -dirvec.z);
                }
            }
            //Begin checking special types
            else if (type == CONTROLTYPE)
            {
                if (lValue1 == CONTROLLOAD || lValue1 == CONTROLUNLOAD || lValue1 == CONTROLTURBO)
                {
                    if ((trackvec == dirvec) || (trackvec == -dirvec))
                    {
                        //Do nothing... direction doesn't change
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            //Check for freight stations
            else if (type == FREIGHTSTATIONTYPE)
            {
                if ((trackvec == dirvec) || (trackvec == -dirvec))
                {
                    Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
                    if (segment == null)
                    {
                        segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
                        if (segment == null)
                        {
                            Debug.Log((object)"Track junction track follower did not find segment");
                            return(false);
                        }
                    }
                    FreightCartStation fcs = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as FreightCartStation;
                    if (fcs == null)
                    {
                        Debug.LogWarning("Track Junction Track Follower tried to get a freight cart station but got other mod machine instead?");
                        return(false);
                    }
                    if (!SegmentStations.Contains(fcs))
                    {
                        SegmentStations.Add(fcs);
                    }
                    fcs.ClosestJunction   = this;
                    fcs.JunctionDirection = initialdirection;
                }
                else
                {
                    return(false);
                }
            }
            //Is it a junction?
            else if (type == JUNCTIONTYPE)
            {
                //Debug.LogWarning("Track follower success!  Found another junction!");
                Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
                if (segment == null)
                {
                    segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
                    if (segment == null)
                    {
                        Debug.Log((object)"Track junction track follower did not find segment");
                        return(false);
                    }
                }
                FreightTrackJunction junction = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as FreightTrackJunction;
                if (junction == null)
                {
                    Debug.LogWarning("Track Junction Track Follower tried to get a track junction but got other mod machine instead?");
                    return(false);
                }

                //Mark this segment as a loop coming back to ourselves.
                if (junction == this)
                {
                    this.DirectionResults[initialdirection] = FreightTrackDirectionResults.Self;
                }
                else
                {
                    this.DirectionResults[initialdirection] = FreightTrackDirectionResults.Good;
                }

                this.SegmentDistances[initialdirection]  += 1; // We're going to exit the loop here, messing with loop variable OK'd.
                this.ConnectedJunctions[initialdirection] = junction;
                FreightTrackSegment tracksegment = new FreightTrackSegment(this, junction, this.SegmentDistances[initialdirection]);
                tracksegment.Stations = SegmentStations;
                //Debug.LogWarning("trackseg station count: " + tracksegment.Stations.Count);
                this.ConnectedSegments[initialdirection] = tracksegment;
                this.LinkStatusDirty = true;

                //If path is bi-directional, handle the connection for the other junction so we don't need to double the work
                if (mirrorOk)
                {
                    //Mirror the direction to reflect the correct side of the connecting junction
                    int mirroreddir = direction += 2;
                    if (mirroreddir > 3)
                    {
                        mirroreddir -= 4;
                    }
                    junction.ConnectedJunctions [mirroreddir] = this;
                    junction.ConnectedSegments [mirroreddir]  = tracksegment;
                    junction.SegmentDistances [mirroreddir]   = this.SegmentDistances[initialdirection];
                    junction.DirectionResults[mirroreddir]    = this.DirectionResults[initialdirection];//Either "Good
                    junction.LinkStatusDirty = true;
                }
                return(true);
            }
            else if (type == TOURSTATIONTYPE)
            {
                if (trackvec != -dirvec)
                {
                    return(false);
                }
                Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
                if (segment == null)
                {
                    segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
                    if (segment == null)
                    {
                        Debug.Log((object)"Track junction track follower did not find segment");
                        return(false);
                    }
                }
                TourCartStation station = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as TourCartStation;
                station.TrackNetwork      = this.TrackNetwork;
                station.ClosestJunction   = this;
                station.JunctionDirection = initialdirection;
                this.ConnectedJunctions[initialdirection] = this;
                FreightTrackSegment tracksegment = new FreightTrackSegment(this, this, 2 * this.SegmentDistances[initialdirection] + 1);
                this.SegmentDistances[initialdirection] *= 2;
                this.SegmentDistances[initialdirection] += 1;
                this.ConnectedSegments[initialdirection] = tracksegment;
                this.LinkStatusDirty = true;
                if (!string.IsNullOrEmpty(station.StationName) && !this.TrackNetwork.TourCartStations.ContainsKey(station.StationName))
                {
                    this.TrackNetwork.TourCartStations.Add(station.StationName, station);
                }
                return(true);
            }
            else
            {
                return(false);   //Not a track type
            }
            //Update the direction int based on the changed direction vector
            if (dirvec == Vector3.right)
            {
                direction = 0;
            }
            else if (dirvec == Vector3.forward)
            {
                direction = 1;
            }
            else if (dirvec == Vector3.left)
            {
                direction = 2;
            }
            else if (dirvec == Vector3.back)
            {
                direction = 3;
            }

            //Store a hash of every track piece to check for getting stuck in endless loops.
            //HACK Construct a string to be our HashTable Key.
            //We could implement a fancy struct or something, but this works, and we don't have to make a custom GetHashTag function
            //(Struct.gethash() is apparently very inefficient)
            //And the try/catch construct means we only do one hash lookup even.
            try{
                //Build a string which will be unique for this particular track segment and travel direction. Lots and Lots of implicit casting to string on this line.
                VisitedTracks.Add((nextX - this.mnX) + "," + (nextY - this.mnY) + "," + (nextZ - this.mnZ) + "," + direction, null);
            }
            catch {
                Debug.LogWarning("TrackJunction followed track route and found an infinite loop. Ending search.");
                this.DirectionResults [initialdirection] = FreightTrackDirectionResults.Trap;
                return(false);
            }
        }
        Debug.LogWarning("Track Junction Found track length > 512m -> ending search.");
        return(false);
    }