Example #1
0
        public ISegmentEnd GetOrAddSegmentEnd(ushort segmentId, bool startNode)
        {
            ISegmentEnd end = GetSegmentEnd(segmentId, startNode);

            if (end != null)
            {
                return(end);
            }

            SegmentGeometry segGeo = SegmentGeometry.Get(segmentId);

            if (segGeo == null)
            {
                Log.Warning($"SegmentEndManager.GetOrAddSegmentEnd({segmentId}, {startNode}): Refusing to add segment end for invalid segment.");
                return(null);
            }

            SegmentEndGeometry endGeo = segGeo.GetEnd(startNode);

            if (endGeo == null)
            {
                Log.Warning($"SegmentEndManager.GetOrAddSegmentEnd({segmentId}, {startNode}): Refusing to add segment end for invalid segment end.");
                return(null);
            }

            return(SegmentEnds[GetIndex(segmentId, startNode)] = new SegmentEnd(segmentId, startNode));
        }
        public ISegmentEnd GetOrAddSegmentEnd(ushort segmentId, bool startNode)
        {
            ISegmentEnd end = GetSegmentEnd(segmentId, startNode);

            if (end != null)
            {
                return(end);
            }

            ref NetSegment netSegment = ref segmentId.ToSegment();
Example #3
0
        /*private void Reset(bool unlink=true) { // TODO this is called in wrong places!
         *      if (unlink)
         *              Unlink();
         *
         *      Valid = false;
         *      totalLength = 0f;
         *      //VehicleType = ExtVehicleType.None;
         *      waitTime = 0;
         *      JunctionTransitState = VehicleJunctionTransitState.None;
         *      lastStateUpdate = 0;
         * }*/

        /*public ExtCitizenInstance GetDriverExtInstance() {
         *      ushort driverInstanceId = CustomPassengerCarAI.GetDriverInstance(vehicleId, ref Singleton<VehicleManager>.instance.m_vehicles.m_buffer[vehicleId]);
         *      if (driverInstanceId != 0) {
         *              return ExtCitizenInstanceManager.Instance.GetExtInstance(driverInstanceId);
         *      }
         *      return null;
         * }*/

        internal void Unlink()
        {
#if DEBUG
            if (GlobalConfig.Instance.Debug.Switches[9])
            {
                Log._Debug($"VehicleState.Unlink({vehicleId}) called: Unlinking vehicle from all segment ends\nstate:{this}");
            }
#endif

            IVehicleStateManager vehStateManager = Constants.ManagerFactory.VehicleStateManager;

            lastPositionUpdate = Now();

            if (previousVehicleIdOnSegment != 0)
            {
                vehStateManager.SetNextVehicleIdOnSegment(previousVehicleIdOnSegment, nextVehicleIdOnSegment);                // VehicleStates[previousVehicleIdOnSegment].nextVehicleIdOnSegment = nextVehicleIdOnSegment;
            }
            else if (currentSegmentId != 0)
            {
                ISegmentEnd curEnd = Constants.ManagerFactory.SegmentEndManager.GetSegmentEnd(currentSegmentId, currentStartNode);
                if (curEnd != null && curEnd.FirstRegisteredVehicleId == vehicleId)
                {
                    curEnd.FirstRegisteredVehicleId = nextVehicleIdOnSegment;
                }
            }

            if (nextVehicleIdOnSegment != 0)
            {
                vehStateManager.SetPreviousVehicleIdOnSegment(nextVehicleIdOnSegment, previousVehicleIdOnSegment);                // .VehicleStates[nextVehicleIdOnSegment].previousVehicleIdOnSegment = previousVehicleIdOnSegment;
            }

            nextVehicleIdOnSegment     = 0;
            previousVehicleIdOnSegment = 0;

            currentSegmentId = 0;
            currentStartNode = false;
            currentLaneIndex = 0;

            lastPathId            = 0;
            lastPathPositionIndex = 0;

#if DEBUG
            if (GlobalConfig.Instance.Debug.Switches[9])
            {
                Log._Debug($"VehicleState.Unlink({vehicleId}) finished: Unlinked vehicle from all segment ends\nstate:{this}");
            }
#endif
        }
Example #4
0
        public bool UpdateSegmentEnd(ushort segmentId, bool startNode)
        {
            SegmentGeometry segGeo = SegmentGeometry.Get(segmentId);

            if (segGeo == null)
            {
#if DEBUGGEO
                Log._Debug($"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): Segment {segmentId} is invalid. Removing all segment ends.");
#endif
                RemoveSegmentEnds(segmentId);
                return(false);
            }

            SegmentEndGeometry endGeo = segGeo.GetEnd(startNode);
            if (endGeo == null)
            {
                Log._Debug($"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): Segment end {segmentId} @ {startNode} is invalid. Removing segment end.");
                RemoveSegmentEnd(segmentId, startNode);
                return(false);
            }

            if (TrafficPriorityManager.Instance.HasSegmentPrioritySign(segmentId, startNode) ||
                TrafficLightSimulationManager.Instance.HasTimedSimulation(endGeo.NodeId()))
            {
                Log._Debug($"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): Segment {segmentId} @ {startNode} has timed light or priority sign. Adding segment end {segmentId} @ {startNode}");
                ISegmentEnd end = GetOrAddSegmentEnd(segmentId, startNode);
                if (end == null)
                {
                    Log.Warning($"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): Failed to add segment end.");
                    return(false);
                }
                else
                {
                    Log._Debug($"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): Added segment end. Updating now.");
                    end.Update();
                    Log._Debug($"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): Update of segment end finished.");
                    return(true);
                }
            }
            else
            {
#if DEBUGGEO
                Log._Debug($"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): Segment {segmentId} @ {startNode} neither has timed light nor priority sign. Removing segment end {segmentId} @ {startNode}");
#endif
                RemoveSegmentEnd(segmentId, startNode);
                return(false);
            }
        }
Example #5
0
        public ISegmentEnd GetOrAddSegmentEnd(ushort segmentId, bool startNode)
        {
            ISegmentEnd end = GetSegmentEnd(segmentId, startNode);

            if (end != null)
            {
                return(end);
            }

            if (!Services.NetService.IsSegmentValid(segmentId))
            {
                Log.Warning(
                    $"SegmentEndManager.GetOrAddSegmentEnd({segmentId}, {startNode}): Refusing to " +
                    "add segment end for invalid segment.");
                return(null);
            }

            return(SegmentEnds[GetIndex(segmentId, startNode)] = new SegmentEnd(segmentId, startNode));
        }
Example #6
0
        private void Link(ISegmentEnd end)
        {
#if DEBUG
            if (GlobalConfig.Instance.Debug.Switches[9])
            {
                Log._Debug($"VehicleState.Link({vehicleId}) called: Linking vehicle to segment end {end}\nstate:{this}");
            }
#endif

            ushort oldFirstRegVehicleId = end.FirstRegisteredVehicleId;
            if (oldFirstRegVehicleId != 0)
            {
                Constants.ManagerFactory.VehicleStateManager.SetPreviousVehicleIdOnSegment(oldFirstRegVehicleId, vehicleId);                // VehicleStates[oldFirstRegVehicleId].previousVehicleIdOnSegment = vehicleId;
                nextVehicleIdOnSegment = oldFirstRegVehicleId;
            }
            end.FirstRegisteredVehicleId = vehicleId;

#if DEBUG
            if (GlobalConfig.Instance.Debug.Switches[9])
            {
                Log._Debug($"VehicleState.Link({vehicleId}) finished: Linked vehicle to segment end {end}\nstate:{this}");
            }
#endif
        }
Example #7
0
        internal void UpdatePosition(ref Vehicle vehicleData, ref PathUnit.Position curPos, ref PathUnit.Position nextPos, bool skipCheck = false)
        {
#if DEBUG
            if (GlobalConfig.Instance.Debug.Switches[9])
            {
                Log._Debug($"VehicleState.UpdatePosition({vehicleId}) called: {this}");
            }
#endif

            if ((flags & Flags.Spawned) == Flags.None)
            {
#if DEBUG
                if (GlobalConfig.Instance.Debug.Switches[9])
                {
                    Log._Debug($"VehicleState.UpdatePosition({vehicleId}): Vehicle is not yet spawned.");
                }
#endif
                OnSpawn(ref vehicleData);
            }

            if (nextSegmentId != nextPos.m_segment || nextLaneIndex != nextPos.m_lane)
            {
                nextSegmentId = nextPos.m_segment;
                nextLaneIndex = nextPos.m_lane;
            }

            bool        startNode = IsTransitNodeCurStartNode(ref curPos, ref nextPos);
            ISegmentEnd end       = Constants.ManagerFactory.SegmentEndManager.GetSegmentEnd(curPos.m_segment, startNode);

            if (end == null || currentSegmentId != end.SegmentId || currentStartNode != end.StartNode || currentLaneIndex != curPos.m_lane)
            {
#if DEBUG
                if (GlobalConfig.Instance.Debug.Switches[9])
                {
                    Log._Debug($"VehicleState.UpdatePosition({vehicleId}): Current segment end changed. seg. {currentSegmentId}, start {currentStartNode}, lane {currentLaneIndex} -> seg. {end?.SegmentId}, start {end?.StartNode}, lane {curPos.m_lane}");
                }
#endif
                if (currentSegmentId != 0)
                {
#if DEBUG
                    if (GlobalConfig.Instance.Debug.Switches[9])
                    {
                        Log._Debug($"VehicleState.UpdatePosition({vehicleId}): Unlinking from current segment end");
                    }
#endif
                    Unlink();
                }

                lastPathId            = vehicleData.m_path;
                lastPathPositionIndex = vehicleData.m_pathPositionIndex;

                currentSegmentId = curPos.m_segment;
                currentStartNode = startNode;
                currentLaneIndex = curPos.m_lane;

                waitTime = 0;
                if (end != null)
                {
#if DEBUGVSTATE
                    if (GlobalConfig.Instance.Debug.Switches[9])
                    {
                        Log._Debug($"VehicleState.UpdatePosition({vehicleId}): Linking vehicle to segment end {end.SegmentId} @ {end.StartNode} ({end.NodeId}). Current position: Seg. {curPos.m_segment}, lane {curPos.m_lane}, offset {curPos.m_offset} / Next position: Seg. {nextPos.m_segment}, lane {nextPos.m_lane}, offset {nextPos.m_offset}");
                    }
#endif
                    Link(end);
                    JunctionTransitState = VehicleJunctionTransitState.Approach;
                }
                else
                {
                    JunctionTransitState = VehicleJunctionTransitState.None;
                }
            }
#if DEBUG
            if (GlobalConfig.Instance.Debug.Switches[9])
            {
                Log._Debug($"VehicleState.UpdatePosition({vehicleId}) finshed: {this}");
            }
#endif
        }
Example #8
0
        public bool UpdateSegmentEnd(ushort segmentId, bool startNode)
        {
#if DEBUG
            bool logPriority = DebugSwitch.PriorityRules.Get() &&
                               (DebugSettings.SegmentId <= 0 ||
                                segmentId == DebugSettings.SegmentId);
#else
            const bool logPriority = false;
#endif

            if (!Services.NetService.IsSegmentValid(segmentId))
            {
                if (logPriority)
                {
                    Log._Debug(
                        $"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): Segment " +
                        $"{segmentId} is invalid. Removing all segment ends.");
                }

                RemoveSegmentEnds(segmentId);
                return(false);
            }

            if (TrafficPriorityManager.Instance.HasSegmentPrioritySign(segmentId, startNode) ||
                TrafficLightSimulationManager.Instance.HasTimedSimulation(
                    Services.NetService.GetSegmentNodeId(
                        segmentId,
                        startNode)))
            {
                if (logPriority)
                {
                    Log._DebugFormat(
                        "SegmentEndManager.UpdateSegmentEnd({0}, {1}): Segment {2} @ {3} has timed " +
                        "light or priority sign. Adding segment end {4} @ {5}",
                        segmentId, startNode, segmentId, startNode, segmentId, startNode);
                }

                ISegmentEnd end = GetOrAddSegmentEnd(segmentId, startNode);
                if (end == null)
                {
                    Log.Warning($"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): " +
                                "Failed to add segment end.");
                    return(false);
                }

                if (logPriority)
                {
                    Log._Debug($"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): " +
                               "Added segment end. Updating now.");
                }

                end.Update();
                if (logPriority)
                {
                    Log._Debug($"SegmentEndManager.UpdateSegmentEnd({segmentId}, {startNode}): " +
                               "Update of segment end finished.");
                }

                return(true);
            }
            else
            {
                if (logPriority)
                {
                    Log._DebugFormat(
                        "SegmentEndManager.UpdateSegmentEnd({0}, {1}): Segment {2} @ {3} neither has " +
                        "timed light nor priority sign. Removing segment end {4} @ {5}",
                        segmentId, startNode, segmentId, startNode, segmentId, startNode);
                }

                RemoveSegmentEnd(segmentId, startNode);
                return(false);
            }
        }