private static bool CheckInfoCompatibility(Vector3 pos, ItemClass.Service service, ItemClass.SubService subService, TransferManager.TransferReason[] allowedTypes, Building.Flags flagsRequired, Building.Flags flagsForbidden, BuildingManager bm, ref ushort result, ref float lastNearest, ushort buildingId, BuildingInfo info)
 {
     //doErrorLog($"CheckInfoCompatibility 0  {pos}, {service}, {subService}, {allowedTypes},  {flagsRequired},  {flagsForbidden}, {bm},  {result},  {lastNearest},  {buildingId}, {info}");
     if (info != null && (info.m_class.m_service == service || service == ItemClass.Service.None) && (info.m_class.m_subService == subService || subService == ItemClass.SubService.None))
     {
         //doErrorLog("CheckInfoCompatibility 1");
         Building.Flags flags = bm.m_buildings.m_buffer[buildingId].m_flags;
         //doErrorLog("CheckInfoCompatibility 2");
         if ((flags & (flagsRequired | flagsForbidden)) == flagsRequired)
         {
             //doErrorLog("CheckInfoCompatibility 3");
             if (allowedTypes == null ||
                 allowedTypes.Length == 0 ||
                 !(info.GetAI() is DepotAI depotAI) ||
                 (depotAI.m_transportInfo != null && allowedTypes.Contains(depotAI.m_transportInfo.m_vehicleReason)) ||
                 (depotAI.m_secondaryTransportInfo != null && allowedTypes.Contains(depotAI.m_secondaryTransportInfo.m_vehicleReason)))
             {
                 //doErrorLog("CheckInfoCompatibility 4");
                 float dist = Vector3.SqrMagnitude(pos - bm.m_buildings.m_buffer[buildingId].m_position);
                 //doErrorLog("CheckInfoCompatibility 5");
                 if (dist < lastNearest)
                 {
                     result      = buildingId;
                     lastNearest = dist;
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
        public static ushort FindBuilding(Vector3 pos, float maxDistance, ItemClass.Service service, ItemClass.SubService subService, TransferManager.TransferReason[] allowedTypes, Building.Flags flagsRequired, Building.Flags flagsForbidden)
        {
            BuildingManager bm = Singleton <BuildingManager> .instance;
            //if (allowedTypes == null || allowedTypes.Length == 0)
            //{
            //    return bm.FindBuilding(pos, maxDistance, service, subService, flagsRequired, flagsForbidden);
            //}


            int    num             = Mathf.Max((int)(((pos.x - maxDistance) / 64f) + 135f), 0);
            int    num2            = Mathf.Max((int)(((pos.z - maxDistance) / 64f) + 135f), 0);
            int    num3            = Mathf.Min((int)(((pos.x + maxDistance) / 64f) + 135f), 269);
            int    num4            = Mathf.Min((int)(((pos.z + maxDistance) / 64f) + 135f), 269);
            ushort result          = 0;
            float  currentDistance = maxDistance * maxDistance;

            for (int i = num2; i <= num4; i++)
            {
                for (int j = num; j <= num3; j++)
                {
                    ushort buildingId = bm.m_buildingGrid[(i * 270) + j];
                    int    num7       = 0;
                    while (buildingId != 0)
                    {
                        BuildingInfo info = bm.m_buildings.m_buffer[buildingId].Info;
                        if (!CheckInfoCompatibility(pos, service, subService, allowedTypes, flagsRequired, flagsForbidden, bm, ref result, ref currentDistance, buildingId, info) && info.m_subBuildings?.Length > 0)
                        {
                            foreach (BuildingInfo.SubInfo subBuilding in info.m_subBuildings)
                            {
                                if (subBuilding != null && CheckInfoCompatibility(pos, service, subService, allowedTypes, flagsRequired, flagsForbidden, bm, ref result, ref currentDistance, buildingId, subBuilding.m_buildingInfo))
                                {
                                    break;
                                }
                            }
                        }
                        buildingId = bm.m_buildings.m_buffer[buildingId].m_nextGridBuilding;
                        if (++num7 >= 49152)
                        {
                            CODebugBase <LogChannel> .Error(LogChannel.Core, "Invalid list detected!\n" + Environment.StackTrace);

                            break;
                        }
                    }
                }
            }
            return(result);
        }
        public static string GetBuildingName(ushort buildingId, out ItemClass.Service serviceFound, out ItemClass.SubService subserviceFound)
        {
            BuildingManager bm = Singleton <BuildingManager> .instance;

            while (bm.m_buildings.m_buffer[buildingId].m_parentBuilding > 0)
            {
                LogUtils.DoLog("getBuildingName(): building id {0} - parent = {1}", buildingId, bm.m_buildings.m_buffer[buildingId].m_parentBuilding);
                buildingId = bm.m_buildings.m_buffer[buildingId].m_parentBuilding;
                bm.m_buildings.m_buffer[buildingId] = bm.m_buildings.m_buffer[buildingId];
            }
            InstanceID iid = default;

            iid.Building    = buildingId;
            serviceFound    = bm.m_buildings.m_buffer[buildingId].Info?.GetService() ?? default;
            subserviceFound = bm.m_buildings.m_buffer[buildingId].Info?.GetSubService() ?? default;

            return(bm.GetBuildingName(buildingId, iid));
        }
Exemplo n.º 4
0
        public static string GetStationName(ushort stopId, ushort lineId, ItemClass.SubService ss, out ItemClass.Service serviceFound, out ItemClass.SubService subserviceFound, out string prefix, out ushort buildingID, out NamingType resultNamingType, bool excludeCargo = false, bool useRestrictionForAreas = false, bool useRoadMainNameOnAddress = false)
        {
            if (lineId == 0)
            {
                buildingID = stopId;
                return(GetBuildingNameForStation(lineId, out serviceFound, out subserviceFound, out prefix, stopId, out resultNamingType));
            }
            string savedName = GetStopName(stopId);
            var    tsd       = TransportSystemDefinition.From(lineId);

            if (savedName != null)
            {
                serviceFound     = ItemClass.Service.PublicTransport;
                subserviceFound  = Singleton <TransportManager> .instance.m_lines.m_buffer[lineId].Info.m_class.m_subService;
                prefix           = tsd.GetConfig().NamingPrefix?.TrimStart();
                buildingID       = 0;
                resultNamingType = NamingTypeExtensions.From(tsd);
                return(savedName);
            }

            NetManager nm       = Singleton <NetManager> .instance;
            NetNode    nn       = nm.m_nodes.m_buffer[stopId];
            Vector3    location = nn.m_position;

            if (tsd.VehicleType == VehicleInfo.VehicleType.Car || tsd.VehicleType == VehicleInfo.VehicleType.Tram || tsd.VehicleType == VehicleInfo.VehicleType.Trolleybus)
            {
                List <ushort> nearStops = StopSearchUtils.FindNearStops(location, nn.Info.GetService(), true, 50f, out _, out _);

                foreach (ushort otherStop in nearStops)
                {
                    if (otherStop != stopId)
                    {
                        savedName = GetStopName(otherStop);
                        ;
                        if (savedName != null)
                        {
                            ushort targetLineId = NetManager.instance.m_nodes.m_buffer[otherStop].m_transportLine;
                            var    tsd2         = TransportSystemDefinition.From(targetLineId);

                            serviceFound     = ItemClass.Service.PublicTransport;
                            subserviceFound  = Singleton <TransportManager> .instance.m_lines.m_buffer[targetLineId].Info.m_class.m_subService;
                            prefix           = tsd2.GetConfig().NamingPrefix?.TrimStart();
                            buildingID       = 0;
                            resultNamingType = NamingTypeExtensions.From(tsd2);
                            return(savedName);
                        }
                    }
                }
            }


            buildingID = GetStationBuilding(stopId, ss, excludeCargo);

            if (buildingID > 0)
            {
                return(GetBuildingNameForStation(lineId, out serviceFound, out subserviceFound, out prefix, buildingID, out resultNamingType));
            }


            prefix = "";
            byte parkId = DistrictManager.instance.GetPark(location);

            if (parkId > 0)
            {
                var idx       = DistrictManager.instance.m_parks.m_buffer[parkId].GetNamingClass();
                var idxConfig = idx.GetConfig();
                if (!useRestrictionForAreas || idxConfig.UseInAutoName)
                {
                    prefix           = idxConfig.NamingPrefix?.TrimStart();
                    serviceFound     = 0;
                    subserviceFound  = 0;
                    resultNamingType = idx.ToNamingType();
                    return(DistrictManager.instance.GetParkName(parkId));
                }
            }
            int number = 0;

            if ((useRoadMainNameOnAddress ? TransportLinesManagerMod.Controller.ConnectorADR.GetStreetSuffix(location, location, out string streetName) : TransportLinesManagerMod.Controller.ConnectorADR.GetAddressStreetAndNumber(location, location, out number, out streetName)) &&
                !streetName.IsNullOrWhiteSpace() &&
                (!useRestrictionForAreas || TLMSpecialNamingClass.Address.GetConfig().UseInAutoName))
            {
                prefix           = TLMSpecialNamingClass.Address.GetConfig().NamingPrefix?.TrimStart();
                serviceFound     = ItemClass.Service.Road;
                subserviceFound  = ItemClass.SubService.PublicTransportBus;
                resultNamingType = NamingType.ADDRESS;
                return(useRoadMainNameOnAddress ? streetName : streetName + ", " + number);
            }