Exemplo n.º 1
0
        internal void rebuildSegmentIds()
        {
            var node = TrafficLightTool.GetNetNode(nodeId);

            List <ushort> oldSegmentIds = new List <ushort>(segmentIds);

            maxSegmentLength = 0;
            segmentIds.Clear();
            for (var s = 0; s < 8; s++)
            {
                var segmentId = node.GetSegment(s);

                if (segmentId <= 0)
                {
                    continue;
                }

                float segLength = Singleton <NetManager> .instance.m_segments.m_buffer[segmentId].m_averageLength;
                if (segLength > maxSegmentLength)
                {
                    maxSegmentLength = segLength;
                }

                segmentIds.Add(segmentId);
                oldSegmentIds.Remove(segmentId);
            }

            foreach (var segmentId in oldSegmentIds)
            {
                segmentIds.Add(segmentId);
            }
        }
        public static bool HasForwardSegment(int segmentId, ushort nodeId, bool debug = false)
        {
            if (segmentId <= 0)
            {
                return(false);
            }

            var node = TrafficLightTool.GetNetNode(nodeId);

            for (var s = 0; s < 8; s++)
            {
                var segment = node.GetSegment(s);

                if (segment == 0 || segment == segmentId)
                {
                    continue;
                }
                if (IsRightSegment(segmentId, segment, nodeId) || IsLeftSegment(segmentId, segment, nodeId))
                {
                    continue;
                }

                if (debug)
                {
                    Log.Message("FORWARD: " + segment + " " + GetSegmentDir(segment, nodeId));
                }
                return(true);
            }

            return(false);
        }
        public static bool HasForwardSegment(int segmentID, ushort nodeID, bool debug)
        {
            var node = TrafficLightTool.GetNetNode(nodeID);

            if (node.CountSegments() <= 3)
            {
                var hasLeftSegment  = false;
                var hasRightSegment = false;

                for (int s = 0; s < node.CountSegments(); s++)
                {
                    var segment = node.GetSegment(s);

                    if (segment != 0 && segment != segmentID)
                    {
                        if (isRightSegment(segmentID, segment, nodeID, true) >= 0)
                        {
                            if (debug)
                            {
                                Debug.Log("FORWARD RIGHT: " + segment + " " + GetSegmentDirection(segment, nodeID) + " " + GetSegmentDir(segment, nodeID));
                            }

                            hasRightSegment = true;
                        }

                        if (isLeftSegment(segmentID, segment, nodeID, true) >= 0)
                        {
                            if (debug)
                            {
                                Debug.Log("FORWARDLEFT: " + segment + " " + GetSegmentDirection(segment, nodeID) + " " + GetSegmentDir(segment, nodeID));
                            }
                            hasLeftSegment = true;
                        }
                    }
                }

                if (hasLeftSegment && hasRightSegment)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 4
0
        public void Update()
        {
            uint currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex >> 6;

            if (lastFrame < currentFrameIndex)
            {
                lastFrame = currentFrameIndex;

                foreach (var nodeID in nodeDictionary.Keys)
                {
                    var node = GetNodeSimulation(nodeID);

                    if (node.FlagManualTrafficLights || (node.FlagTimedTrafficLights && node.TimedTrafficLightsActive))
                    {
                        var data = TrafficLightTool.GetNetNode(nodeID);
                        node.SimulationStep(ref data);
                        TrafficLightTool.SetNetNode(nodeID, data);
                    }
                }
            }
        }
Exemplo n.º 5
0
        public TimedTrafficSteps(int num, ushort nodeID)
        {
            this.nodeID   = nodeID;
            this.numSteps = num;

            var node = TrafficLightTool.GetNetNode(nodeID);

            for (int s = 0; s < 8; s++)
            {
                var segment = node.GetSegment(s);

                if (segment != 0)
                {
                    var segmentLight = TrafficLightsManual.GetSegmentLight(nodeID, segment);

                    segments.Add(segment);
                    lightMain.Add(segmentLight.GetLightMain());
                    lightLeft.Add(segmentLight.GetLightLeft());
                    lightRight.Add(segmentLight.GetLightRight());
                    lightPedestrian.Add(segmentLight.GetLightPedestrian());
                }
            }
        }
Exemplo n.º 6
0
        public static bool HasForwardSegment(int segmentID, ushort nodeID, bool debug = false)
        {
            var node = TrafficLightTool.GetNetNode(nodeID);

            for (int s = 0; s < 8; s++)
            {
                var segment = node.GetSegment(s);

                if (segment != 0 && segment != segmentID)
                {
                    if (!isRightSegment(segmentID, segment, nodeID) && !isLeftSegment(segmentID, segment, nodeID))
                    {
                        if (debug)
                        {
                            Debug.Log("FORWARD: " + segment + " " + GetSegmentDir(segment, nodeID));
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
        public static bool HasLeftSegment(int segmentId, ushort nodeId, bool debug = false)
        {
            var node = TrafficLightTool.GetNetNode(nodeId);

            for (var s = 0; s < 8; s++)
            {
                var segment = node.GetSegment(s);

                if (segment != 0 && segment != segmentId)
                {
                    if (IsLeftSegment(segmentId, segment, nodeId))
                    {
                        if (debug)
                        {
                            Log.Message("LEFT: " + segment + " " + GetSegmentDir(segment, nodeId));
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
        public static bool HasRightSegment(int segmentID, ushort nodeID, bool debug)
        {
            var node = TrafficLightTool.GetNetNode(nodeID);

            for (int s = 0; s < node.CountSegments(); s++)
            {
                var segment = node.GetSegment(s);

                if (segment != 0 && segment != segmentID)
                {
                    if (isRightSegment(segmentID, segment, nodeID, true) >= 0)
                    {
                        if (debug)
                        {
                            Debug.Log("RIGHT: " + segment + " " + GetSegmentDirection(segment, nodeID) + " " + GetSegmentDir(segment, nodeID));
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 9
0
        private bool invalid = false;         // TODO rework

        public TimedTrafficStep(int minTime, int maxTime, ushort nodeId, ushort masterNodeId, List <ushort> groupNodeIds)
        {
            this.nodeId    = nodeId;
            this.minTime   = minTime;
            this.maxTime   = maxTime;
            this.timedNode = TrafficLightsTimed.GetTimedLight(nodeId);

            if (nodeId == masterNodeId)
            {
                this.masterNodeId = null;
            }
            else
            {
                this.masterNodeId = masterNodeId;
            }
            this.groupNodeIds = groupNodeIds;

            var node = TrafficLightTool.GetNetNode(nodeId);

            minFlow = Single.NaN;
            maxWait = Single.NaN;

            endTransitionStart = -1;
            stepDone           = false;

            for (var s = 0; s < 8; s++)
            {
                var segmentId = node.GetSegment(s);
                if (segmentId <= 0)
                {
                    continue;
                }

                addSegment(segmentId);
            }
            rebuildSegmentIds();
        }
Exemplo n.º 10
0
        public void Update()
        {
            uint currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex >> 6;

            if (lastFrame < currentFrameIndex)
            {
                lastFrame = currentFrameIndex;

                List <ushort> clearedNodes = new List <ushort>();

                foreach (var nodeID in nodeDictionary.Keys)
                {
                    var data = TrafficLightTool.GetNetNode(nodeID);

                    try
                    {
                        for (var i = 0; i < 8; i++)
                        {
                            var sgmid = data.GetSegment(i);

                            if (sgmid != 0)
                            {
                                if (!TrafficLightsManual.IsSegmentLight(nodeID, sgmid))
                                {
                                    if (nodeDictionary[nodeID].FlagTimedTrafficLights)
                                    {
                                        // TODO: this fails after an upgrade
                                        var timedNode = TrafficLightsTimed.GetTimedLight(nodeID);

                                        for (var j = 0; j < timedNode.nodeGroup.Count; j++)
                                        {
                                            var nodeSim = CustomRoadAI.GetNodeSimulation(timedNode.nodeGroup[j]);

                                            nodeSim.TimedTrafficLightsActive = false;

                                            clearedNodes.Add(timedNode.nodeGroup[j]);
                                            TrafficLightsTimed.RemoveTimedLight(timedNode.nodeGroup[j]);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.Log(e);
                        //Log.Warning("Error on Update: \n" + e.Message + "\n\nStacktrace:\n\n" + e.StackTrace);
                    }
                }

                if (clearedNodes.Count > 0)
                {
                    for (var i = 0; i < clearedNodes.Count; i++)
                    {
                        CustomRoadAI.RemoveNodeFromSimulation(clearedNodes[i]);
                    }
                }

                foreach (var nodeID in nodeDictionary.Keys)
                {
                    var node = GetNodeSimulation(nodeID);

                    if (node.FlagManualTrafficLights || (node.FlagTimedTrafficLights && node.TimedTrafficLightsActive))
                    {
                        var data = TrafficLightTool.GetNetNode(nodeID);

                        node.SimulationStep(ref data);
                        TrafficLightTool.SetNetNode(nodeID, data);

                        if (clearedNodes.Count > 0)
                        {
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 11
0
        public static bool incomingVehicles(ushort targetCar, ushort nodeID)
        {
            uint currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex;
            uint frame             = currentFrameIndex >> 4;
            var  node = TrafficLightTool.GetNetNode(nodeID);

            var fromPrioritySegment = getPrioritySegment(nodeID, vehicleList[targetCar].fromSegment);

            List <ushort> removeCarList = new List <ushort>();

            var numCars = 0;

            // get all cars
            for (int s = 0; s < 8; s++)
            {
                var segment = node.GetSegment(s);

                if (segment != 0 && segment != vehicleList[targetCar].fromSegment)
                {
                    if (isPrioritySegment(nodeID, segment))
                    {
                        var prioritySegment = getPrioritySegment(nodeID, segment);

                        // select outdated cars
                        foreach (var car in prioritySegment.cars)
                        {
                            var frameReduce = vehicleList[car].lastSpeed < 70 ? 4u : 2u;

                            if (vehicleList[car].lastFrame < frame - frameReduce)
                            {
                                removeCarList.Add(car);
                            }
                        }

                        // remove outdated cars
                        foreach (var rcar in removeCarList)
                        {
                            vehicleList[rcar].resetCar();
                            prioritySegment.RemoveCar(rcar);
                        }

                        removeCarList.Clear();

                        if ((node.m_flags & NetNode.Flags.TrafficLights) == NetNode.Flags.None)
                        {
                            if (fromPrioritySegment.type == PrioritySegment.PriorityType.Main)
                            {
                                if (prioritySegment.type == PrioritySegment.PriorityType.Main)
                                {
                                    numCars += prioritySegment.numCars;

                                    foreach (var car in prioritySegment.cars)
                                    {
                                        if (vehicleList[car].lastSpeed > 0.1f)
                                        {
                                            numCars = checkSameRoadIncomingCar(targetCar, car, nodeID)
                                                ? numCars - 1
                                                : numCars;
                                        }
                                        else
                                        {
                                            numCars--;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                numCars += prioritySegment.numCars;

                                foreach (var car in prioritySegment.cars)
                                {
                                    if (prioritySegment.type == PrioritySegment.PriorityType.Main)
                                    {
                                        if (!vehicleList[car].stopped)
                                        {
                                            numCars = checkPriorityRoadIncomingCar(targetCar, car, nodeID)
                                                ? numCars - 1
                                                : numCars;
                                        }
                                        else
                                        {
                                            numCars--;
                                        }
                                    }
                                    else
                                    {
                                        if (vehicleList[car].lastSpeed > 0.1f)
                                        {
                                            numCars = checkSameRoadIncomingCar(targetCar, car, nodeID)
                                                ? numCars - 1
                                                : numCars;
                                        }
                                        else
                                        {
                                            numCars--;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (TrafficLightsManual.IsSegmentLight(nodeID, segment))
                            {
                                var segmentLight = TrafficLightsManual.GetSegmentLight(nodeID, segment);

                                if (segmentLight.GetLightMain() == RoadBaseAI.TrafficLightState.Green)
                                {
                                    numCars += prioritySegment.numCars;

                                    foreach (var car in prioritySegment.cars)
                                    {
                                        if (vehicleList[car].lastSpeed > 1f)
                                        {
                                            numCars = checkSameRoadIncomingCar(targetCar, car, nodeID)
                                                ? numCars - 1
                                                : numCars;
                                        }
                                        else
                                        {
                                            numCars--;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (numCars > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static bool HasIncomingVehicles(ushort targetCar, ushort nodeId)
        {
            var currentFrameIndex = Singleton <SimulationManager> .instance.m_currentFrameIndex;
            var frame             = currentFrameIndex >> 4;
            var node = TrafficLightTool.GetNetNode(nodeId);

            var fromPrioritySegment = GetPrioritySegment(nodeId, VehicleList[targetCar].FromSegment);

            if (fromPrioritySegment == null)
            {
                return(false);
            }

            var removeCarList = new List <ushort>();

            var numCars = 0;

            // get all cars
            for (var s = 0; s < 8; s++)
            {
                var segment = node.GetSegment(s);

                if (segment == 0 || segment == VehicleList[targetCar].FromSegment)
                {
                    continue;
                }
                if (!IsPrioritySegment(nodeId, segment))
                {
                    continue;
                }

                var prioritySegment = GetPrioritySegment(nodeId, segment);
                if (prioritySegment == null)
                {
                    continue;                     // should not happen
                }
                // select outdated cars
                removeCarList.AddRange(from car in prioritySegment.Cars where VehicleList[car].LastFrame < frame - 32u select car);

                // remove outdated cars
                foreach (var rcar in removeCarList)
                {
                    VehicleList[rcar].ResetCar();
                    //Log.Message("Removing *OUTDATED* vehicle " + rcar + " from segment " + segment + " @ node " + nodeId);
                    prioritySegment.RemoveCar(rcar);
                    CustomCarAI.watchedVehicleIds.Remove(rcar);
                }

                removeCarList.Clear();

                if ((node.m_flags & NetNode.Flags.TrafficLights) == NetNode.Flags.None)
                {
                    if (fromPrioritySegment.Type == PrioritySegment.PriorityType.Main)
                    {
                        if (prioritySegment.Type != PrioritySegment.PriorityType.Main)
                        {
                            continue;
                        }

                        numCars += prioritySegment.NumCars;

                        foreach (var car in prioritySegment.Cars)
                        {
                            if (Singleton <VehicleManager> .instance.m_vehicles.m_buffer[car].m_frame0.m_velocity.magnitude > 0.1f)
                            {
                                numCars = CheckSameRoadIncomingCar(targetCar, car, nodeId)
                                                                        ? numCars - 1
                                                                        : numCars;
                            }
                            else
                            {
                                numCars--;
                            }
                        }
                    }
                    else
                    {
                        numCars += prioritySegment.NumCars;

                        foreach (var car in prioritySegment.Cars)
                        {
                            if (prioritySegment.Type == PrioritySegment.PriorityType.Main)
                            {
                                if (!VehicleList[car].Stopped)
                                {
                                    numCars = CheckPriorityRoadIncomingCar(targetCar, car, nodeId)
                                                                                ? numCars - 1
                                                                                : numCars;
                                }
                                else
                                {
                                    numCars--;
                                }
                            }
                            else
                            {
                                if (Singleton <VehicleManager> .instance.m_vehicles.m_buffer[car].m_frame0.m_velocity.magnitude > 0.1f)
                                {
                                    numCars = CheckSameRoadIncomingCar(targetCar, car, nodeId)
                                                                                ? numCars - 1
                                                                                : numCars;
                                }
                                else
                                {
                                    numCars--;
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (!TrafficLightsManual.IsSegmentLight(nodeId, segment))
                    {
                        continue;
                    }

                    var segmentLight = TrafficLightsManual.GetSegmentLight(nodeId, segment);

                    if (segmentLight.GetLightMain() != RoadBaseAI.TrafficLightState.Green)
                    {
                        continue;
                    }

                    numCars += prioritySegment.NumCars;

                    foreach (var car in prioritySegment.Cars)
                    {
                        if (Singleton <VehicleManager> .instance.m_vehicles.m_buffer[car].m_frame0.m_velocity.magnitude > 1f)
                        {
                            numCars = CheckSameRoadIncomingCar(targetCar, car, nodeId)
                                                                ? numCars - 1
                                                                : numCars;
                        }
                        else
                        {
                            numCars--;
                        }
                    }
                }
            }

            return(numCars > 0);
        }