TrafficManager_ImprovedAI.CustomPathFind.ProcessItem C# (CSharp) Method

ProcessItem() private method

private ProcessItem ( CustomPathFind item, ushort targetNode, ushort segmentID, NetSegment &segment, int &currentTargetIndex, byte connectOffset, bool enableVehicle, bool enablePedestrian ) : bool
item CustomPathFind
targetNode ushort
segmentID ushort
segment NetSegment
currentTargetIndex int
connectOffset byte
enableVehicle bool
enablePedestrian bool
return bool
        private bool ProcessItem(CustomPathFind.BufferItem item, ushort targetNode, ushort segmentID, ref NetSegment segment, ref int currentTargetIndex, byte connectOffset, bool enableVehicle, bool enablePedestrian)
        {
            bool result = false;
            if ((segment.m_flags & (NetSegment.Flags.PathFailed | NetSegment.Flags.Flooded)) != NetSegment.Flags.None)
            {
                return result;
            }
            NetManager instance = Singleton<NetManager>.instance;
            NetInfo info = segment.Info;
            NetInfo info2 = instance.m_segments.m_buffer[(int)item.m_position.m_segment].Info;
            int num = info.m_lanes.Length;
            uint num2 = segment.m_lanes;
            NetInfo.Direction direction = (targetNode != segment.m_startNode) ? NetInfo.Direction.Forward : NetInfo.Direction.Backward;
            NetInfo.Direction direction2 = ((segment.m_flags & NetSegment.Flags.Invert) == NetSegment.Flags.None) ? direction : NetInfo.InvertDirection(direction);
            float num3 = 0.01f - Mathf.Min(info.m_maxTurnAngleCos, info2.m_maxTurnAngleCos);
            if (num3 < 1f)
            {
                Vector3 vector;
                if (targetNode == instance.m_segments.m_buffer[(int)item.m_position.m_segment].m_startNode)
                {
                    vector = instance.m_segments.m_buffer[(int)item.m_position.m_segment].m_startDirection;
                }
                else
                {
                    vector = instance.m_segments.m_buffer[(int)item.m_position.m_segment].m_endDirection;
                }
                Vector3 vector2;
                if ((byte)(direction & NetInfo.Direction.Forward) != 0)
                {
                    vector2 = segment.m_endDirection;
                }
                else
                {
                    vector2 = segment.m_startDirection;
                }
                float num4 = vector.x * vector2.x + vector.z * vector2.z;
                if (num4 >= num3)
                {
                    return result;
                }
            }
            float num5 = 1f;
            float num6 = 1f;
            NetInfo.LaneType laneType = NetInfo.LaneType.None;
            VehicleInfo.VehicleType vehicleType = VehicleInfo.VehicleType.None;
            if ((int)item.m_position.m_lane < info2.m_lanes.Length)
            {
                NetInfo.Lane lane = info2.m_lanes[(int)item.m_position.m_lane];
                laneType = lane.m_laneType;
                vehicleType = lane.m_vehicleType;
                num5 = lane.m_speedLimit;
                num6 = this.CalculateLaneSpeed(connectOffset, item.m_position.m_offset, ref instance.m_segments.m_buffer[(int)item.m_position.m_segment], lane);
            }
            float num7 = instance.m_segments.m_buffer[(int)item.m_position.m_segment].m_averageLength;
            if (!this.m_stablePath)
            {
                Randomizer randomizer = new Randomizer(this.m_pathFindIndex << 16 | (uint)item.m_position.m_segment);
                num7 *= (float)(randomizer.Int32(900, 1000 + (int)(instance.m_segments.m_buffer[(int)item.m_position.m_segment].m_trafficDensity * 10)) + this.m_pathRandomizer.Int32(20u)) * 0.001f;
            }
            if (this.m_isHeavyVehicle && (instance.m_segments.m_buffer[(int)item.m_position.m_segment].m_flags & NetSegment.Flags.HeavyBan) != NetSegment.Flags.None)
            {
                num7 *= 10f;
            }
            if (this.m_transportVehicle && laneType == NetInfo.LaneType.TransportVehicle)
            {
                num7 *= 0.95f;
            }
            if ((laneType & (NetInfo.LaneType.Vehicle | NetInfo.LaneType.TransportVehicle)) != 0)
            {
                laneType = (NetInfo.LaneType.Vehicle | NetInfo.LaneType.TransportVehicle);
            }
            float num8 = (float)Mathf.Abs((int)(connectOffset - item.m_position.m_offset)) * 0.003921569f * num7;
            float num9 = item.m_methodDistance + num8;
            float num10 = item.m_comparisonValue + num8 / (num6 * this.m_maxLength);
            Vector3 b = instance.m_lanes.m_buffer[(int)((UIntPtr)item.m_laneID)].CalculatePosition((float)connectOffset * 0.003921569f);
            int num11 = currentTargetIndex;
            bool flag = (instance.m_nodes.m_buffer[(int)targetNode].m_flags & NetNode.Flags.Transition) != NetNode.Flags.None;
            NetInfo.LaneType laneType2 = this.m_laneTypes;
            VehicleInfo.VehicleType vehicleType2 = this.m_vehicleTypes;
            if (!enableVehicle)
            {
                vehicleType2 &= VehicleInfo.VehicleType.Bicycle;
                if (vehicleType2 == VehicleInfo.VehicleType.None)
                {
                    laneType2 &= ~(NetInfo.LaneType.Vehicle | NetInfo.LaneType.TransportVehicle);
                }
            }
            if (!enablePedestrian)
            {
                laneType2 &= ~NetInfo.LaneType.Pedestrian;
            }
            int num12 = 0;
            while (num12 < num && num2 != 0u)
            {
                NetInfo.Lane lane2 = info.m_lanes[num12];
            //                if ((byte)(lane2.m_finalDirection & direction2) != 0 && (!m_obeyTMLanes || CSL_Traffic.RoadManager.CheckLaneConnection(num2, item.m_laneID, targetNode)))
                if ((byte)(lane2.m_finalDirection & direction2) != 0 && ((this.m_vehicleTypes & VehicleInfo.VehicleType.Tram) != 0 || CSL_Traffic.RoadManager.CheckLaneConnection(num2, item.m_laneID, targetNode)))
                {
                    if (lane2.CheckType(laneType2, vehicleType2) && (segmentID != item.m_position.m_segment || num12 != (int)item.m_position.m_lane) && (byte)(lane2.m_finalDirection & direction2) != 0)
                    {
                        Vector3 a;
                        if ((byte)(direction & NetInfo.Direction.Forward) != 0)
                        {
                            a = instance.m_lanes.m_buffer[(int)((UIntPtr)num2)].m_bezier.d;
                        }
                        else
                        {
                            a = instance.m_lanes.m_buffer[(int)((UIntPtr)num2)].m_bezier.a;
                        }
                        float num13 = Vector3.Distance(a, b);
                        if (flag)
                        {
                            num13 *= 2f;
                        }
                        /* ----- Congestion Changes ----- */
                        // Checks if the lane has space for a vehicle of length 5. If not, increase its cost to avoid it.
                        if (!instance.m_lanes.m_buffer[num2].CheckSpace(m_minLaneSpace)) // the length used here can be tweaked for different results. Haven't had time to test it yet
                        {
                            num13 *= m_congestionCostFactor; // the factor of cost increase can also be tweaked to achieve different results
                        }
                        /* ------------------------------ */
                        float num14 = num13 / ((num5 + lane2.m_speedLimit) * 0.5f * this.m_maxLength);
                        CustomPathFind.BufferItem item2;
                        item2.m_position.m_segment = segmentID;
                        item2.m_position.m_lane = (byte)num12;
                        item2.m_position.m_offset = (byte)(((direction & NetInfo.Direction.Forward) == 0) ? 0 : 255);
                        if ((lane2.m_laneType & laneType) == 0)
                        {
                            item2.m_methodDistance = 0f;
                        }
                        else
                        {
                            item2.m_methodDistance = num9 + num13;
                        }
                        if (lane2.m_laneType != NetInfo.LaneType.Pedestrian || item2.m_methodDistance < 1000f)
                        {
                            item2.m_comparisonValue = num10 + num14;
                            if (num2 == this.m_startLaneA)
                            {
                                float num15 = this.CalculateLaneSpeed(this.m_startOffsetA, item2.m_position.m_offset, ref segment, lane2);
                                float num16 = (float)Mathf.Abs((int)(item2.m_position.m_offset - this.m_startOffsetA)) * 0.003921569f;
                                item2.m_comparisonValue += num16 * segment.m_averageLength / (num15 * this.m_maxLength);
                            }
                            if (num2 == this.m_startLaneB)
                            {
                                float num17 = this.CalculateLaneSpeed(this.m_startOffsetB, item2.m_position.m_offset, ref segment, lane2);
                                float num18 = (float)Mathf.Abs((int)(item2.m_position.m_offset - this.m_startOffsetB)) * 0.003921569f;
                                item2.m_comparisonValue += num18 * segment.m_averageLength / (num17 * this.m_maxLength);
                            }
                            if (!this.m_ignoreBlocked && (segment.m_flags & NetSegment.Flags.Blocked) != NetSegment.Flags.None && (lane2.m_laneType & (NetInfo.LaneType.Vehicle | NetInfo.LaneType.TransportVehicle)) != 0)
                            {
                                item2.m_comparisonValue += 0.1f;
                                result = true;
                            }
                            item2.m_direction = direction;
                            item2.m_lanesUsed = (item.m_lanesUsed | lane2.m_laneType);
                            item2.m_laneID = num2;
                            if ((lane2.m_laneType & laneType) != 0 && lane2.m_vehicleType == vehicleType)
                            {
                                int firstTarget = (int)instance.m_lanes.m_buffer[(int)((UIntPtr)num2)].m_firstTarget;
                                int lastTarget = (int)instance.m_lanes.m_buffer[(int)((UIntPtr)num2)].m_lastTarget;
                                if (currentTargetIndex < firstTarget || currentTargetIndex >= lastTarget)
                                {
                                    item2.m_comparisonValue += Mathf.Max(1f, num13 * 3f - 3f) / ((num5 + lane2.m_speedLimit) * 0.5f * this.m_maxLength);
                                }
                                if (!this.m_transportVehicle && lane2.m_laneType == NetInfo.LaneType.TransportVehicle)
                                {
                                    item2.m_comparisonValue += 20f / ((num5 + lane2.m_speedLimit) * 0.5f * this.m_maxLength);
                                }
                            }
                            this.AddBufferItem(item2, item.m_position);
                        }
                    }
                }
                else if ((lane2.m_laneType & laneType) != 0 && lane2.m_vehicleType == vehicleType)
                {
                    num11++;
                }
                num2 = instance.m_lanes.m_buffer[(int)((UIntPtr)num2)].m_nextLane;
                num12++;
            }
            currentTargetIndex = num11;
            return result;
        }

Same methods

CustomPathFind::ProcessItem ( CustomPathFind item, ushort nodeID, NetNode &node, byte connectOffset, bool isMiddle ) : void
CustomPathFind::ProcessItem ( CustomPathFind item, ushort targetNode, bool targetDisabled, ushort segmentID, NetSegment &segment, uint lane, byte offset, byte connectOffset ) : void
CustomPathFind::ProcessItem ( CustomPathFind item, ushort targetNode, ushort segmentID, NetSegment &segment, byte connectOffset, int laneIndex, uint lane ) : void