private bool findShortestPath(uint unit, bool skipQueue)
        {
            int[]          maxPathLength = { 10000000 };
            CustomPathFind pathFind      = null;

            foreach (var pathFind2 in _replacementPathFinds.Where(pathFind2 => pathFind2.IsAvailable && pathFind2.m_queuedPathFindCount < maxPathLength[0]))
            {
                maxPathLength[0] = pathFind2.m_queuedPathFindCount;
                pathFind         = pathFind2;
            }

            if (pathFind != null && pathFind.CalculatePath(unit, skipQueue))
            {
                return(true);
            }
            ReleasePath(unit);
            return(false);
        }
Example #2
0
        private bool findShortestPath(uint unit, bool skipQueue)
        {
            int            maxPathLength = 10000000;
            CustomPathFind pathFind      = null;

            for (int i = 0; i < this.replacementPathFinds.Length; i++)
            {
                CustomPathFind pathFind2 = this.replacementPathFinds[i];
                if (pathFind2.IsAvailable && pathFind2.m_queuedPathFindCount < maxPathLength)
                {
                    maxPathLength = pathFind2.m_queuedPathFindCount;
                    pathFind      = pathFind2;
                }
            }

            if (pathFind != null && pathFind.CalculatePath(unit, skipQueue))
            {
                return(true);
            }
            this.ReleasePath(unit);
            return(false);
        }
        public bool CreatePath(out uint unit, ref Randomizer randomizer, uint buildIndex, PathUnit.Position startPosA, PathUnit.Position startPosB, PathUnit.Position endPosA, PathUnit.Position endPosB, PathUnit.Position vehiclePosition, NetInfo.LaneType laneTypes, VehicleInfo.VehicleType vehicleTypes, float maxLength, bool isHeavyVehicle, bool ignoreBlocked, bool stablePath, bool skipQueue, RoadManager.VehicleType vehicleType)
        {
            while (!Monitor.TryEnter(this.m_bufferLock, SimulationManager.SYNCHRONIZE_TIMEOUT))
            {
            }
            uint num;

            try
            {
                if (!this.m_pathUnits.CreateItem(out num, ref randomizer))
                {
                    unit = 0u;
                    bool result = false;
                    return(result);
                }
                this.m_pathUnitCount = (int)(this.m_pathUnits.ItemCount() - 1u);
            }
            finally
            {
                Monitor.Exit(this.m_bufferLock);
            }
            unit = num;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_simulationFlags = 1;
            if (isHeavyVehicle)
            {
                PathUnit[] expr_92_cp_0 = this.m_pathUnits.m_buffer;
                UIntPtr    expr_92_cp_1 = (UIntPtr)unit;
                expr_92_cp_0[(int)expr_92_cp_1].m_simulationFlags = (byte)(expr_92_cp_0[(int)expr_92_cp_1].m_simulationFlags | 16);
            }
            if (ignoreBlocked)
            {
                PathUnit[] expr_BB_cp_0 = this.m_pathUnits.m_buffer;
                UIntPtr    expr_BB_cp_1 = (UIntPtr)unit;
                expr_BB_cp_0[(int)expr_BB_cp_1].m_simulationFlags = (byte)(expr_BB_cp_0[(int)expr_BB_cp_1].m_simulationFlags | 32);
            }
            if (stablePath)
            {
                PathUnit[] expr_E4_cp_0 = this.m_pathUnits.m_buffer;
                UIntPtr    expr_E4_cp_1 = (UIntPtr)unit;
                expr_E4_cp_0[(int)expr_E4_cp_1].m_simulationFlags = (byte)(expr_E4_cp_0[(int)expr_E4_cp_1].m_simulationFlags | 64);
            }
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_pathFindFlags  = 0;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_buildIndex     = buildIndex;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position00     = startPosA;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position01     = endPosA;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position02     = startPosB;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position03     = endPosB;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position11     = vehiclePosition;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_nextPathUnit   = 0u;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_laneTypes      = (byte)laneTypes;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_vehicleTypes   = (byte)vehicleTypes;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_length         = maxLength;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_positionCount  = 20;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_referenceCount = 1;
            int            num2     = 10000000;
            CustomPathFind pathFind = null;

            for (int i = 0; i < this.m_pathFinds.Length; i++)
            {
                CustomPathFind pathFind2 = this.m_pathFinds[i];
                if (pathFind2.IsAvailable && pathFind2.m_queuedPathFindCount < num2)
                {
                    num2     = pathFind2.m_queuedPathFindCount;
                    pathFind = pathFind2;
                }
            }
            if (pathFind != null && pathFind.CalculatePath(unit, skipQueue, vehicleType))
            {
                return(true);
            }
            this.ReleasePath(unit);
            return(false);
        }
        public bool CreatePath(out uint unit, ref Randomizer randomizer, PathCreationArgs args)
        {
            uint pathUnitId;

            try {
                Monitor.Enter(this.m_bufferLock);

                int numIters = 0;
                while (true)                   // NON-STOCK CODE
                {
                    ++numIters;

                    if (!this.m_pathUnits.CreateItem(out pathUnitId, ref randomizer))
                    {
                        unit = 0u;
                        return(false);
                    }

                    this.m_pathUnits.m_buffer[pathUnitId].m_simulationFlags = 1;
                    this.m_pathUnits.m_buffer[pathUnitId].m_referenceCount  = 1;
                    this.m_pathUnits.m_buffer[pathUnitId].m_nextPathUnit    = 0u;

                    // NON-STOCK CODE START
                    if (queueItems[pathUnitId].queued)
                    {
                        ReleasePath(pathUnitId);

                        if (numIters > 10)
                        {
                            unit = 0u;
                            return(false);
                        }

                        continue;
                    }
                    break;
                }

                queueItems[pathUnitId].vehicleType = args.extVehicleType;
                queueItems[pathUnitId].vehicleId   = args.vehicleId;
                queueItems[pathUnitId].pathType    = args.extPathType;
                queueItems[pathUnitId].spawned     = args.spawned;
                queueItems[pathUnitId].queued      = true;
                // NON-STOCK CODE END

                this.m_pathUnitCount = (int)(this.m_pathUnits.ItemCount() - 1u);
            } finally {
                Monitor.Exit(this.m_bufferLock);
            }
            unit = pathUnitId;

            if (args.isHeavyVehicle)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_IS_HEAVY;
            }
            if (args.ignoreBlocked || args.ignoreFlooded)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_IGNORE_BLOCKED;
            }
            if (args.stablePath)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_STABLE_PATH;
            }
            if (args.randomParking)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_RANDOM_PARKING;
            }
            if (args.ignoreFlooded)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_IGNORE_FLOODED;
            }
            if (args.hasCombustionEngine)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_COMBUSTION;
            }
            if (args.ignoreCosts)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_IGNORE_COST;
            }
            this.m_pathUnits.m_buffer[unit].m_pathFindFlags = 0;
            this.m_pathUnits.m_buffer[unit].m_buildIndex    = args.buildIndex;
            this.m_pathUnits.m_buffer[unit].m_position00    = args.startPosA;
            this.m_pathUnits.m_buffer[unit].m_position01    = args.endPosA;
            this.m_pathUnits.m_buffer[unit].m_position02    = args.startPosB;
            this.m_pathUnits.m_buffer[unit].m_position03    = args.endPosB;
            this.m_pathUnits.m_buffer[unit].m_position11    = args.vehiclePosition;
            this.m_pathUnits.m_buffer[unit].m_laneTypes     = (byte)args.laneTypes;
            this.m_pathUnits.m_buffer[unit].m_vehicleTypes  = (ushort)args.vehicleTypes;
            this.m_pathUnits.m_buffer[unit].m_length        = args.maxLength;
            this.m_pathUnits.m_buffer[unit].m_positionCount = 20;
            int minQueued = 10000000;

#if PF2
            CustomPathFind2 pathFind = null;
#else
            CustomPathFind pathFind = null;
#endif

#if QUEUEDSTATS
            TotalQueuedPathFinds = 0;
#endif
            for (int i = 0; i < _replacementPathFinds.Length; ++i)
            {
#if PF2
                CustomPathFind2 pathFindCandidate = _replacementPathFinds[i];
#else
                CustomPathFind pathFindCandidate = _replacementPathFinds[i];
#endif

#if QUEUEDSTATS
                TotalQueuedPathFinds += (uint)pathFindCandidate.m_queuedPathFindCount;
#endif
                if (pathFindCandidate.IsAvailable &&
                    pathFindCandidate.m_queuedPathFindCount < minQueued)
                {
                    minQueued = pathFindCandidate.m_queuedPathFindCount;
                    pathFind  = pathFindCandidate;
                }
            }

#if PF2
            if (pathFind != null && pathFind.CalculatePath(unit, args.skipQueue))
            {
                return(true);
            }
#else
            if (pathFind != null && pathFind.ExtCalculatePath(unit, args.skipQueue))
            {
                return(true);
            }
#endif

            // NON-STOCK CODE START
            try {
                Monitor.Enter(this.m_bufferLock);

                queueItems[pathUnitId].queued = false;
                // NON-STOCK CODE END
                this.ReleasePath(unit);

                // NON-STOCK CODE START
                this.m_pathUnitCount = (int)(this.m_pathUnits.ItemCount() - 1u);
            } finally {
                Monitor.Exit(this.m_bufferLock);
            }
            // NON-STOCK CODE END
            return(false);
        }
Example #5
0
        public bool CreatePath(ExtVehicleType vehicleType, out uint unit, ref Randomizer randomizer, uint buildIndex, PathUnit.Position startPosA, PathUnit.Position startPosB, PathUnit.Position endPosA, PathUnit.Position endPosB, PathUnit.Position vehiclePosition, NetInfo.LaneType laneTypes, VehicleInfo.VehicleType vehicleTypes, float maxLength, bool isHeavyVehicle, bool ignoreBlocked, bool stablePath, bool skipQueue)
        {
            uint num;

            try {
                Monitor.Enter(this.m_bufferLock);
                if (!this.m_pathUnits.CreateItem(out num, ref randomizer))
                {
                    unit = 0u;
                    bool result = false;
                    return(result);
                }
                this.m_pathUnitCount = (int)(this.m_pathUnits.ItemCount() - 1u);
            } finally {
                Monitor.Exit(this.m_bufferLock);
            }
            unit = num;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_simulationFlags = 1;
            if (isHeavyVehicle)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 16;
            }
            if (ignoreBlocked)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 32;
            }
            if (stablePath)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 64;
            }
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_pathFindFlags  = 0;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_buildIndex     = buildIndex;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position00     = startPosA;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position01     = endPosA;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position02     = startPosB;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position03     = endPosB;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position11     = vehiclePosition;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_nextPathUnit   = 0u;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_laneTypes      = (byte)laneTypes;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_vehicleTypes   = (byte)vehicleTypes;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_length         = maxLength;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_positionCount  = 20;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_referenceCount = 1;
            int            num2     = 10000000;
            CustomPathFind pathFind = null;

            for (int i = 0; i < _replacementPathFinds.Length; i++)
            {
                CustomPathFind pathFindCandidate = _replacementPathFinds[i];
                if (pathFindCandidate.IsAvailable && pathFindCandidate.m_queuedPathFindCount < num2)
                {
                    num2     = pathFindCandidate.m_queuedPathFindCount;
                    pathFind = pathFindCandidate;
                }
            }
            if (pathFind != null && pathFind.CalculatePath(vehicleType, unit, skipQueue))
            {
                return(true);
            }
            this.ReleasePath(unit);
            return(false);
        }
Example #6
0
        public bool CreatePath(ExtVehicleType vehicleType, ushort vehicleId, ExtCitizenInstance.ExtPathType pathType, out uint unit, ref Randomizer randomizer, uint buildIndex, PathUnit.Position startPosA, PathUnit.Position startPosB, PathUnit.Position endPosA, PathUnit.Position endPosB, PathUnit.Position vehiclePosition, NetInfo.LaneType laneTypes, VehicleInfo.VehicleType vehicleTypes, float maxLength, bool isHeavyVehicle, bool ignoreBlocked, bool stablePath, bool skipQueue, bool randomParking, bool ignoreFlooded)
        {
            uint pathUnitId;

            try {
                Monitor.Enter(this.m_bufferLock);

                int numIters = 0;
                while (true)                   // NON-STOCK CODE
                {
                    ++numIters;

                    if (!this.m_pathUnits.CreateItem(out pathUnitId, ref randomizer))
                    {
                        unit = 0u;
                        return(false);
                    }

                    this.m_pathUnits.m_buffer[pathUnitId].m_simulationFlags = 1;
                    this.m_pathUnits.m_buffer[pathUnitId].m_referenceCount  = 1;
                    this.m_pathUnits.m_buffer[pathUnitId].m_nextPathUnit    = 0u;

                    // NON-STOCK CODE START
                    if (queueItems[pathUnitId].queued)
                    {
                        ReleasePath(pathUnitId);

                        if (numIters > 10)
                        {
                            unit = 0u;
                            return(false);
                        }

                        continue;
                    }
                    break;
                }

                queueItems[pathUnitId].vehicleType = vehicleType;
                queueItems[pathUnitId].vehicleId   = vehicleId;
                queueItems[pathUnitId].pathType    = pathType;
                queueItems[pathUnitId].queued      = true;
                // NON-STOCK CODE END

                this.m_pathUnitCount = (int)(this.m_pathUnits.ItemCount() - 1u);
            } finally {
                Monitor.Exit(this.m_bufferLock);
            }
            unit = pathUnitId;

            if (isHeavyVehicle)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 16;
            }
            if (ignoreBlocked || ignoreFlooded)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 32;
            }
            if (stablePath)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 64;
            }
            if (randomParking)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 128;
            }
            this.m_pathUnits.m_buffer[unit].m_pathFindFlags = 0;
            this.m_pathUnits.m_buffer[unit].m_buildIndex    = buildIndex;
            this.m_pathUnits.m_buffer[unit].m_position00    = startPosA;
            this.m_pathUnits.m_buffer[unit].m_position01    = endPosA;
            this.m_pathUnits.m_buffer[unit].m_position02    = startPosB;
            this.m_pathUnits.m_buffer[unit].m_position03    = endPosB;
            this.m_pathUnits.m_buffer[unit].m_position11    = vehiclePosition;
            this.m_pathUnits.m_buffer[unit].m_laneTypes     = (byte)laneTypes;
            this.m_pathUnits.m_buffer[unit].m_vehicleTypes  = (ushort)vehicleTypes;
            this.m_pathUnits.m_buffer[unit].m_length        = maxLength;
            this.m_pathUnits.m_buffer[unit].m_positionCount = 20;
            int            minQueued = 10000000;
            CustomPathFind pathFind  = null;

#if QUEUEDSTATS
            TotalQueuedPathFinds = 0;
#if EXTRAPF
            ExtraQueuedPathFinds = 0;
#endif
#endif
            for (int i = 0; i < _replacementPathFinds.Length; ++i)
            {
                CustomPathFind pathFindCandidate = _replacementPathFinds[i];
#if QUEUEDSTATS
                TotalQueuedPathFinds += (uint)pathFindCandidate.m_queuedPathFindCount;
#if EXTRAPF
                if (pathFindCandidate.IsExtraPathFind)
                {
                    ExtraQueuedPathFinds += (uint)pathFindCandidate.m_queuedPathFindCount;
                }
#endif
#endif
                if (pathFindCandidate.IsAvailable
#if EXTRAPF
                    && (!pathFindCandidate.IsExtraPathFind)
#endif
                    && pathFindCandidate.m_queuedPathFindCount < minQueued)
                {
                    minQueued = pathFindCandidate.m_queuedPathFindCount;
                    pathFind  = pathFindCandidate;
                }
            }

            if (pathFind != null && pathFind.ExtCalculatePath(unit, skipQueue))
            {
                return(true);
            }


            // NON-STOCK CODE START
            try {
                Monitor.Enter(this.m_bufferLock);

                queueItems[pathUnitId].queued = false;
                // NON-STOCK CODE END
                this.ReleasePath(unit);

                // NON-STOCK CODE START
                this.m_pathUnitCount = (int)(this.m_pathUnits.ItemCount() - 1u);
            } finally {
                Monitor.Exit(this.m_bufferLock);
            }
            // NON-STOCK CODE END
            return(false);
        }
Example #7
0
        public override void Start()
        {
            this.backgroundSprite = "MenuPanel2";
            this.width            = 455;
            this.height           = 320;
            this.relativePosition = new Vector3(220f, 60f);

            UITitlePanel titlePanel = this.AddUIComponent <UITitlePanel>();

            titlePanel.Parent           = this;
            titlePanel.relativePosition = Vector3.zero;
            titlePanel.IconSprite       = "ToolbarIconRoads";
            titlePanel.TitleText        = "Improved AI";

            float yVal = 57;

            minLaneSpace = this.AddUIComponent <UISliderInput>();
            minLaneSpace.Slider.eventValueChanged += delegate(UIComponent sender, float value) {
                while (!Monitor.TryEnter(minLaneSpace, SimulationManager.SYNCHRONIZE_TIMEOUT))
                {
                }
                CustomPathFind.minLaneSpace = value;
                Monitor.Exit(minLaneSpace);
            };
            minLaneSpace.Parent                   = this;
            minLaneSpace.relativePosition         = new Vector3(3, yVal);
            minLaneSpace.MinValue                 = CustomPathFind.MIN_MIN_LANE_SPACE;
            minLaneSpace.MaxValue                 = CustomPathFind.MAX_MIN_LANE_SPACE;
            minLaneSpace.StepSize                 = 0.1f;
            minLaneSpace.Slider.scrollWheelAmount = 0.1f;
            minLaneSpace.LabelText                = "minimum lane space";
            minLaneSpace.SliderValue              = CustomPathFind.minLaneSpace;

            yVal += 56;
            congestionCostFactor = this.AddUIComponent <UISliderInput>();
            congestionCostFactor.Slider.eventValueChanged += delegate(UIComponent sender, float value) {
                while (!Monitor.TryEnter(congestionCostFactor, SimulationManager.SYNCHRONIZE_TIMEOUT))
                {
                }
                CustomPathFind.congestionCostFactor = value;
                Monitor.Exit(congestionCostFactor);
            };
            congestionCostFactor.Parent                   = this;
            congestionCostFactor.relativePosition         = new Vector3(3, yVal);
            congestionCostFactor.MinValue                 = CustomPathFind.MIN_CONGESTION_COST_FACTOR;
            congestionCostFactor.MaxValue                 = CustomPathFind.MAX_CONGESTION_COST_FACTOR;
            congestionCostFactor.StepSize                 = 0.1f;
            congestionCostFactor.Slider.scrollWheelAmount = 0.1f;
            congestionCostFactor.LabelText                = "congestion cost factor";
            congestionCostFactor.SliderValue              = CustomPathFind.congestionCostFactor;

            yVal          += 56;
            lookaheadLanes = this.AddUIComponent <UISliderInput>();
            lookaheadLanes.Slider.eventValueChanged += delegate(UIComponent sender, float value) {
                while (!Monitor.TryEnter(lookaheadLanes, SimulationManager.SYNCHRONIZE_TIMEOUT))
                {
                }
                CustomPathFind.lookaheadLanes = (int)value;
                Monitor.Exit(lookaheadLanes);
            };
            lookaheadLanes.Parent                   = this;
            lookaheadLanes.relativePosition         = new Vector3(3, yVal);
            lookaheadLanes.MinValue                 = CustomPathFind.MIN_LOOKAHEAD_LANES;
            lookaheadLanes.MaxValue                 = CustomPathFind.MAX_LOOKAHEAD_LANES;
            lookaheadLanes.StepSize                 = 1;
            lookaheadLanes.Slider.scrollWheelAmount = 1;
            lookaheadLanes.LabelText                = "lookahead lanes";
            lookaheadLanes.SliderValue              = CustomPathFind.lookaheadLanes;

            yVal += 56;
            congestedLaneThreshold = this.AddUIComponent <UISliderInput>();
            congestedLaneThreshold.Slider.eventValueChanged += delegate(UIComponent sender, float value) {
                while (!Monitor.TryEnter(congestedLaneThreshold, SimulationManager.SYNCHRONIZE_TIMEOUT))
                {
                }
                CustomPathFind.congestedLaneThreshold = (int)value;
                Monitor.Exit(congestedLaneThreshold);
            };
            congestedLaneThreshold.Parent                   = this;
            congestedLaneThreshold.relativePosition         = new Vector3(3, yVal);
            congestedLaneThreshold.MinValue                 = CustomPathFind.MIN_CONGESTED_LANE_THRESHOLD;
            congestedLaneThreshold.MaxValue                 = CustomPathFind.MAX_CONGESTED_LANE_THRESHOLD;
            congestedLaneThreshold.StepSize                 = 1;
            congestedLaneThreshold.Slider.scrollWheelAmount = 1;
            congestedLaneThreshold.LabelText                = "congested lane threshold";
            congestedLaneThreshold.SliderValue              = CustomPathFind.congestedLaneThreshold;

            yVal += 58;

            /*
             * obeyTMLaneFlags = this.AddUIComponent<UILabelledBox>();
             * obeyTMLaneFlags.Parent = this;
             * obeyTMLaneFlags.relativePosition = new Vector3(0, yVal);
             * obeyTMLaneFlags.LabelText = "obey traffic manager lane flags";
             * obeyTMLaneFlags.CheckBox.isChecked = CustomPathFind.obeyTMLaneFlags;
             * obeyTMLaneFlags.CheckBox.eventCheckChanged += delegate(UIComponent component, bool value) {
             *  while (!Monitor.TryEnter(obeyTMLaneFlags, SimulationManager.SYNCHRONIZE_TIMEOUT)) {
             *  }
             *  CustomPathFind.obeyTMLaneFlags = value;
             *  Monitor.Exit(obeyTMLaneFlags);
             * };
             */

            resetButton                  = this.AddUIComponent <UIButton>();
            resetButton.text             = "reset";
            resetButton.width            = 57;
            resetButton.height           = 33;
            resetButton.normalBgSprite   = "ButtonMenu";
            resetButton.hoveredBgSprite  = "ButtonMenuHovered";
            resetButton.focusedBgSprite  = "ButtonMenu";
            resetButton.pressedBgSprite  = "ButtonMenu";
            resetButton.playAudioEvents  = true;
            resetButton.relativePosition = new Vector3(378f, yVal - 8.5f);
            resetButton.eventClick      += delegate(UIComponent component, UIMouseEventParameter eventParam) {
                CustomPathFind.ResetAIParameters();
            };
        }
Example #8
0
        public static void OnLoadDataTimed(System.Object source, ElapsedEventArgs e)
        {
            _timer.Enabled = false;
            byte[] data = SerializableData.LoadData(dataID);

            uniqueID = 0u;
            var i = 0;

            for (i = 0; i < data.Length - 3; i++)
            {
                uniqueID = BitConverter.ToUInt32(data, i);
            }

            var filepath = Path.Combine(Application.dataPath, "trafficManagerSave_" + uniqueID + ".xml");

            if (!File.Exists(filepath))
            {
                Debug.Log("Traffic manager save file " + filepath + " not found!");
                return;
            }

            var configuration = Configuration.Deserialize(filepath);

            try {
                for (i = 0; i < configuration.prioritySegments.Count; i++)
                {
                    if (!TrafficPriority.isPrioritySegment((ushort)configuration.prioritySegments[i][0],
                                                           configuration.prioritySegments[i][1]))
                    {
                        TrafficPriority.addPrioritySegment((ushort)configuration.prioritySegments[i][0],
                                                           configuration.prioritySegments[i][1],
                                                           (PrioritySegment.PriorityType)configuration.prioritySegments[i][2]);
                    }
                }
            } catch (Exception ex) {
                Debug.Log("prio segments exception at " + i + " - " + ex);
            }


            try {
                for (i = 0; i < configuration.nodeDictionary.Count; i++)
                {
                    if (CustomRoadAI.GetNodeSimulation((ushort)configuration.nodeDictionary[i][0]) == null)
                    {
                        CustomRoadAI.AddNodeToSimulation((ushort)configuration.nodeDictionary[i][0]);
                        var nodeDict = CustomRoadAI.GetNodeSimulation((ushort)configuration.nodeDictionary[i][0]);

                        nodeDict._manualTrafficLights     = Convert.ToBoolean(configuration.nodeDictionary[i][1]);
                        nodeDict._timedTrafficLights      = Convert.ToBoolean(configuration.nodeDictionary[i][2]);
                        nodeDict.TimedTrafficLightsActive = Convert.ToBoolean(configuration.nodeDictionary[i][3]);
                    }
                }
            } catch (Exception ex) {
                Debug.Log("nodes exception at " + i + " - " + ex);
            }

            try {
                for (i = 0; i < configuration.manualSegments.Count; i++)
                {
                    var segmentData = configuration.manualSegments[i];

                    if (!TrafficLightsManual.IsSegmentLight((ushort)segmentData[0], segmentData[1]))
                    {
                        TrafficLightsManual.AddSegmentLight((ushort)segmentData[0], segmentData[1],
                                                            RoadBaseAI.TrafficLightState.Green);
                        var segment = TrafficLightsManual.GetSegmentLight((ushort)segmentData[0], segmentData[1]);
                        segment.currentMode       = (ManualSegmentLight.Mode)segmentData[2];
                        segment.lightLeft         = (RoadBaseAI.TrafficLightState)segmentData[3];
                        segment.lightMain         = (RoadBaseAI.TrafficLightState)segmentData[4];
                        segment.lightRight        = (RoadBaseAI.TrafficLightState)segmentData[5];
                        segment.lightPedestrian   = (RoadBaseAI.TrafficLightState)segmentData[6];
                        segment.lastChange        = (uint)segmentData[7];
                        segment.lastChangeFrame   = (uint)segmentData[8];
                        segment.pedestrianEnabled = Convert.ToBoolean(segmentData[9]);
                    }
                }
            } catch (Exception ex) {
                Debug.Log("traf lights manual exception at " + i + " - " + ex);
            }

            var timedStepCount        = 0;
            var timedStepSegmentCount = 0;

            try {
                for (i = 0; i < configuration.timedNodes.Count; i++)
                {
                    var nodeid = (ushort)configuration.timedNodes[i][0];

                    var nodeGroup = new List <ushort>();
                    for (var j = 0; j < configuration.timedNodeGroups[i].Length; j++)
                    {
                        nodeGroup.Add(configuration.timedNodeGroups[i][j]);
                    }

                    if (!TrafficLightsTimed.IsTimedLight(nodeid))
                    {
                        TrafficLightsTimed.AddTimedLight(nodeid, nodeGroup);
                        var timedNode = TrafficLightsTimed.GetTimedLight(nodeid);

                        timedNode.currentStep = configuration.timedNodes[i][1];

                        for (var j = 0; j < configuration.timedNodes[i][2]; j++)
                        {
                            var cfgstep = configuration.timedNodeSteps[timedStepCount];

                            timedNode.addStep(cfgstep[0]);

                            var step = timedNode.steps[j];

                            for (var k = 0; k < cfgstep[1]; k++)
                            {
                                step.lightLeft[k]       = (RoadBaseAI.TrafficLightState)configuration.timedNodeStepSegments[timedStepSegmentCount][0];
                                step.lightMain[k]       = (RoadBaseAI.TrafficLightState)configuration.timedNodeStepSegments[timedStepSegmentCount][1];
                                step.lightRight[k]      = (RoadBaseAI.TrafficLightState)configuration.timedNodeStepSegments[timedStepSegmentCount][2];
                                step.lightPedestrian[k] = (RoadBaseAI.TrafficLightState)configuration.timedNodeStepSegments[timedStepSegmentCount][3];

                                timedStepSegmentCount++;
                            }

                            timedStepCount++;
                        }

                        if (Convert.ToBoolean(configuration.timedNodes[i][3]))
                        {
                            timedNode.start();
                        }
                    }
                }
            } catch (Exception ex) {
                Debug.Log("timed traf lights exception at " + i + " - " + ex);
            }


            var j1 = 0;
            var i1 = 0;

            try {
                for (i1 = 0; i1 < 32768; i1++)
                {
                    if (Singleton <NetManager> .instance.m_nodes.m_buffer[i1].Info.m_class.m_service ==
                        ItemClass.Service.Road && Singleton <NetManager> .instance.m_nodes.m_buffer[i1].m_flags != 0)
                    {
                        var trafficLight = configuration.nodeTrafficLights[j1];

                        if (trafficLight == '1')
                        {
                            Singleton <NetManager> .instance.m_nodes.m_buffer[i1].m_flags |= NetNode.Flags.TrafficLights;
                        }
                        else
                        {
                            Singleton <NetManager> .instance.m_nodes.m_buffer[i1].m_flags &= ~NetNode.Flags.TrafficLights;
                        }

                        j1++;
                    }
                }
            } catch (Exception ex) {
                Debug.Log("traf lights exception at i1 = " + i1 + " j1 = " + j1 + " - " + ex);
            }

            var j2 = 0;
            var i2 = 0;

            try {
                for (i2 = 0; i2 < 32768; i2++)
                {
                    if (Singleton <NetManager> .instance.m_nodes.m_buffer[i2].Info.m_class.m_service ==
                        ItemClass.Service.Road && Singleton <NetManager> .instance.m_nodes.m_buffer[i2].m_flags != 0)
                    {
                        var crossWalk = configuration.nodeCrosswalk[j2];

                        if (crossWalk == '1')
                        {
                            Singleton <NetManager> .instance.m_nodes.m_buffer[i2].m_flags |= NetNode.Flags.Junction;
                        }
                        else
                        {
                            Singleton <NetManager> .instance.m_nodes.m_buffer[i2].m_flags &= ~NetNode.Flags.Junction;
                        }

                        j2++;
                    }
                }
            } catch (Exception ex) {
                Debug.Log("crosswalk exception at i2 = " + i2 + " j2 = " + j2 + " - " + ex);
            }

            var lanes = configuration.laneFlags.TrimEnd(',').Split(',');

            try {
                for (i = 0; i < lanes.Length; i++)
                {
                    var  split  = lanes[i].Split(':');
                    uint laneId = Convert.ToUInt32(split[0]);
                    //NetLane lane = Singleton<NetManager>.instance.m_lanes.m_buffer [laneId];
                    //ushort segmentId = lane.m_segment;
                    //NetSegment segment = Singleton<NetManager>.instance.m_segments.m_buffer [segmentId];
                    //segment.Info.m_netAI.UpdateLanes(segmentId, ref segment, false);

                    Singleton <NetManager> .instance.m_lanes.m_buffer[laneId].m_flags  = Convert.ToUInt16(split[1]);
                    Singleton <NetManager> .instance.m_lanes.m_buffer[laneId].m_flags |= CONTROL_BIT;
                }
            } catch {
                // Empty config, ignore exception.
                //Debug.Log("exception reading lane flags at lane " + i + " lanes[] = [" + lanes[i] + "] - " + ex);
            }

            if (configuration.aiConfig != null && configuration.aiConfig.congestionCostFactor > 0)
            {
                Debug.Log("setting AI values from config");
                CustomPathFind.LoadAIParameters(configuration.aiConfig);
            }
            else
            {
                Debug.Log("AI parameters not found in config, using default values");
                CustomPathFind.ResetAIParameters();
            }

            CSL_Traffic.RoadManager.sm_lanes = new CSL_Traffic.RoadManager.Lane[NetManager.MAX_LANE_COUNT];
            int dupes = 0, zero = 0;

            if (configuration.laneMarkers == null || configuration.laneMarkers.Count == 0)
            {
                Debug.Log("no lane markers found");
            }
            else
            {
                Debug.Log("found " + configuration.laneMarkers.Count + " lane markers");
                foreach (var lane in configuration.laneMarkers.ToArray())
                {
                    if (lane != null)
                    {
                        if (CSL_Traffic.RoadManager.sm_lanes[lane.m_laneId] == null)
                        {
                            if (lane.ConnectionCount() > 0)
                            {
                                CSL_Traffic.RoadManager.sm_lanes[lane.m_laneId] = lane;
                            }
                            else
                            {
                                zero++;
                            }
                        }
                        else
                        {
                            dupes++;
                        }
                    }
                    else
                    {
                        Debug.Log("null lane marker!");
                    }
                }
                Debug.Log("loaded lane markers - " + dupes + " duplicates, " + zero + " unconnected");
            }

            configLoaded = true;
        }
        public bool CreatePath(bool recalc, ExtVehicleType vehicleType, ushort vehicleId, ExtCitizenInstance.ExtPathType pathType, out uint unit, ref Randomizer randomizer, uint buildIndex, PathUnit.Position startPosA, PathUnit.Position startPosB, PathUnit.Position endPosA, PathUnit.Position endPosB, PathUnit.Position vehiclePosition, NetInfo.LaneType laneTypes, VehicleInfo.VehicleType vehicleTypes, float maxLength, bool isHeavyVehicle, bool ignoreBlocked, bool stablePath, bool skipQueue, bool randomParking, bool ignoreFlooded)
        {
            uint pathUnitId;

            try {
                Monitor.Enter(this.m_bufferLock);
                if (!this.m_pathUnits.CreateItem(out pathUnitId, ref randomizer))
                {
                    unit = 0u;
                    bool result = false;
                    return(result);
                }
                this.m_pathUnitCount = (int)(this.m_pathUnits.ItemCount() - 1u);
            } finally {
                Monitor.Exit(this.m_bufferLock);
            }
            unit = pathUnitId;

            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_simulationFlags = 1;
            if (isHeavyVehicle)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 16;
            }
            if (ignoreBlocked || ignoreFlooded)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 32;
            }
            if (stablePath)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 64;
            }
            if (randomParking)
            {
                this.m_pathUnits.m_buffer[unit].m_simulationFlags |= 128;
            }
            this.m_pathUnits.m_buffer[unit].m_pathFindFlags = 0;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_buildIndex     = buildIndex;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position00     = startPosA;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position01     = endPosA;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position02     = startPosB;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position03     = endPosB;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_position11     = vehiclePosition;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_nextPathUnit   = 0u;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_laneTypes      = (byte)laneTypes;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_vehicleTypes   = (byte)vehicleTypes;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_length         = maxLength;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_positionCount  = 20;
            this.m_pathUnits.m_buffer[(int)((UIntPtr)unit)].m_referenceCount = 1;
            int            minQueued = 10000000;
            CustomPathFind pathFind  = null;

#if QUEUEDSTATS
            TotalQueuedPathFinds = 0;
#if EXTRAPF
            ExtraQueuedPathFinds = 0;
#endif
#endif
            for (int i = 0; i < _replacementPathFinds.Length; ++i)
            {
                CustomPathFind pathFindCandidate = _replacementPathFinds[i];
#if QUEUEDSTATS
                TotalQueuedPathFinds += (uint)pathFindCandidate.m_queuedPathFindCount;
#if EXTRAPF
                if (pathFindCandidate.IsExtraPathFind)
                {
                    ExtraQueuedPathFinds += (uint)pathFindCandidate.m_queuedPathFindCount;
                }
#endif
#endif
                if (pathFindCandidate.IsAvailable
#if EXTRAPF
                    && (!pathFindCandidate.IsExtraPathFind || recalc)
#endif
                    && pathFindCandidate.m_queuedPathFindCount < minQueued)
                {
                    minQueued = pathFindCandidate.m_queuedPathFindCount;
                    pathFind  = pathFindCandidate;
                }
            }

            pathUnitExtVehicleType[unit] = vehicleType;
            pathUnitVehicleIds[unit]     = vehicleId == 0 ? (ushort?)null : vehicleId;
            pathUnitPathTypes[unit]      = pathType;

            if (pathFind != null && pathFind.ExtCalculatePath(unit, skipQueue))
            {
                return(true);
            }
            this.ReleasePath(unit);
            return(false);
        }
Example #10
0
        public bool CustomCreatePath(out uint unit,
                                     ref Randomizer randomizer,
                                     PathCreationArgs args)
        {
            uint pathUnitId;

            lock (m_bufferLock) {
                int numIters = 0;
                while (true)
                {
                    // NON-STOCK CODE
                    ++numIters;

                    if (!m_pathUnits.CreateItem(out pathUnitId, ref randomizer))
                    {
                        unit = 0u;
                        return(false);
                    }

                    m_pathUnits.m_buffer[pathUnitId].m_simulationFlags = 1;
                    m_pathUnits.m_buffer[pathUnitId].m_referenceCount  = 1;
                    m_pathUnits.m_buffer[pathUnitId].m_nextPathUnit    = 0u;

                    // NON-STOCK CODE START
                    if (QueueItems[pathUnitId].queued)
                    {
                        CustomReleasePath(pathUnitId);

                        if (numIters > 10)
                        {
                            unit = 0u;
                            return(false);
                        }

                        continue;
                    }

                    break;
                }

                QueueItems[pathUnitId].vehicleType = args.extVehicleType;
                QueueItems[pathUnitId].vehicleId   = args.vehicleId;
                QueueItems[pathUnitId].pathType    = args.extPathType;
                QueueItems[pathUnitId].spawned     = args.spawned;
                QueueItems[pathUnitId].queued      = true;
                // NON-STOCK CODE END

                m_pathUnitCount = (int)(m_pathUnits.ItemCount() - 1u);
            }

            unit = pathUnitId;

            if (args.isHeavyVehicle)
            {
                m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_IS_HEAVY;
            }

            if (args.ignoreBlocked || args.ignoreFlooded)
            {
                m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_IGNORE_BLOCKED;
            }

            if (args.stablePath)
            {
                m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_STABLE_PATH;
            }

            if (args.randomParking)
            {
                m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_RANDOM_PARKING;
            }

            if (args.ignoreFlooded)
            {
                m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_IGNORE_FLOODED;
            }

            if (args.hasCombustionEngine)
            {
                m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_COMBUSTION;
            }

            if (args.ignoreCosts)
            {
                m_pathUnits.m_buffer[unit].m_simulationFlags |= PathUnit.FLAG_IGNORE_COST;
            }

            m_pathUnits.m_buffer[unit].m_pathFindFlags = 0;
            m_pathUnits.m_buffer[unit].m_buildIndex    = args.buildIndex;
            m_pathUnits.m_buffer[unit].m_position00    = args.startPosA;
            m_pathUnits.m_buffer[unit].m_position01    = args.endPosA;
            m_pathUnits.m_buffer[unit].m_position02    = args.startPosB;
            m_pathUnits.m_buffer[unit].m_position03    = args.endPosB;
            m_pathUnits.m_buffer[unit].m_position11    = args.vehiclePosition;
            m_pathUnits.m_buffer[unit].m_laneTypes     = (byte)args.laneTypes;
            m_pathUnits.m_buffer[unit].m_vehicleTypes  = (uint)args.vehicleTypes;
            m_pathUnits.m_buffer[unit].m_length        = args.maxLength;
            m_pathUnits.m_buffer[unit].m_positionCount = 20;

            int            minQueued = 10000000;
            CustomPathFind pathFind  = null;

#if QUEUEDSTATS
            TotalQueuedPathFinds = 0;
#endif
            foreach (CustomPathFind pathFindCandidate in _replacementPathFinds)
            {
#if QUEUEDSTATS
                TotalQueuedPathFinds += (uint)pathFindCandidate.m_queuedPathFindCount;
#endif
                if (!pathFindCandidate.IsAvailable ||
                    pathFindCandidate.m_queuedPathFindCount >= minQueued)
                {
                    continue;
                }

                minQueued = pathFindCandidate.m_queuedPathFindCount;
                pathFind  = pathFindCandidate;
            }

            if (pathFind != null && pathFind.CalculatePath(unit, args.skipQueue))
            {
                return(true);
            }

            // NON-STOCK CODE START
            lock (m_bufferLock) {
                QueueItems[pathUnitId].queued = false;
                // NON-STOCK CODE END
                CustomReleasePath(unit);

                // NON-STOCK CODE START
                m_pathUnitCount = (int)(m_pathUnits.ItemCount() - 1u);
            }

            // NON-STOCK CODE END
            return(false);
        }