/// <summary> /// Set ForwardItem and BackwardItem in TsNodes /// </summary> private void SetForwardBackward() { foreach (var node in Nodes) { TsItem item = null; if (Items.TryGetValue(node.Value.ForwardItemUID, out item)) { node.Value.ForwardItem = item; } item = null; if (Items.TryGetValue(node.Value.BackwardItemUID, out item)) { node.Value.BackwardItem = item; } } }
public TsNode NodeIteminPrefab(TsMapper _mapper, TsItem item) { foreach (var nodePId in this.Nodes) { var nodeP = _mapper.GetNodeByUid(nodePId); if (nodeP.ForwardItem == item) { return(nodeP); } if (nodeP.BackwardItem == item) { return(nodeP); } } return(null); }
// Can be useful for A* Algorithm public float HeuristicDistance(TsItem item) { return(Math.Abs(this.X - item.X) + Math.Abs(this.Z - item.Z)); }
/// <summary> /// Loads navigation inside TsItem objects /// </summary> private void LoadNavigation() { foreach (var prefab in Prefabs) { foreach (var nodeStr in prefab.Nodes) { var node = GetNodeByUid(nodeStr); TsItem road = null; TsNode precnode = node; TsItem precitem = prefab; TsNode nextnode; TsItem nextitem; List <TsItem> roads = new List <TsItem>(); var totalLength = 0.0f; if (node.ForwardItem != null && node.ForwardItem.Type == TsItemType.Road) { road = node.ForwardItem; } else if (node.BackwardItem != null && node.BackwardItem.Type == TsItemType.Road) { road = node.BackwardItem; } if (road != null) { int direction = 0; if (road.EndNodeUid == node.Uid) { direction = 1; } while (road != null && road.Type != TsItemType.Prefab && !road.Hidden) { var length = (float)Math.Sqrt(Math.Pow(GetNodeByUid(road.StartNodeUid).X - GetNodeByUid(road.EndNodeUid).X, 2) + Math.Pow(GetNodeByUid(road.StartNodeUid).Z - GetNodeByUid(road.EndNodeUid).Z, 2)); TsRoadItem roadObj = (TsRoadItem)road; totalLength += length / roadObj.RoadLook.GetWidth(); /*if (roadObj.RoadLook.IsHighway) totalLength += (length / 2) / roadObj.RoadLook.GetWidth(); * else if (roadObj.RoadLook.IsLocal) totalLength += (length / 1.75f) / roadObj.RoadLook.GetWidth(); * else if (roadObj.RoadLook.IsExpress) totalLength += (length / 1.25f) / roadObj.RoadLook.GetWidth(); * else length += length * 2;*/ roads.Add(road); if (GetNodeByUid(road.StartNodeUid) == precnode) { nextnode = GetNodeByUid(road.EndNodeUid); precnode = GetNodeByUid(road.EndNodeUid); } else { nextnode = GetNodeByUid(road.StartNodeUid); precnode = GetNodeByUid(road.StartNodeUid); } if (nextnode.BackwardItem == road || nextnode.BackwardItem == precitem) { nextitem = nextnode.ForwardItem; precitem = nextnode.ForwardItem; } else { nextitem = nextnode.BackwardItem; precitem = nextnode.BackwardItem; } road = nextitem; } if (road != null && !road.Hidden) { TsPrefabItem prevPrefab = (TsPrefabItem)prefab; TsPrefabItem nextPrefab = (TsPrefabItem)road; TsRoadLook look = ((TsRoadItem)roads.LastOrDefault()).RoadLook; if (prevPrefab.Hidden || nextPrefab.Hidden) { continue; } if (prevPrefab.Navigation.ContainsKey(nextPrefab) == false && (look.IsBidirectional() || direction == 0)) { prevPrefab.Navigation.Add(nextPrefab, new Tuple <float, List <TsItem> >(totalLength, roads)); } if (nextPrefab.Navigation.ContainsKey(prevPrefab) == false && (look.IsBidirectional() || direction == 1)) { var reverse = new List <TsItem>(roads); reverse.Reverse(); nextPrefab.Navigation.Add(prevPrefab, new Tuple <float, List <TsItem> >(totalLength, reverse)); } } } else if (node.ForwardItem != null && node.BackwardItem != null) { TsPrefabItem forwardPrefab = (TsPrefabItem)node.ForwardItem; TsPrefabItem backwardPrefab = (TsPrefabItem)node.BackwardItem; if (forwardPrefab.Hidden || backwardPrefab.Hidden) { continue; } if (forwardPrefab.Navigation.ContainsKey(backwardPrefab) == false) { forwardPrefab.Navigation.Add(backwardPrefab, new Tuple <float, List <TsItem> >(0, null)); } if (backwardPrefab.Navigation.ContainsKey(forwardPrefab) == false) { backwardPrefab.Navigation.Add(forwardPrefab, new Tuple <float, List <TsItem> >(0, null)); } } } } Dictionary <ulong, TsPrefabItem> ferryToPrefab = new Dictionary <ulong, TsPrefabItem>(); foreach (var port in FerryConnections) { float min = float.MaxValue; TsPrefabItem closerPrefab = null; foreach (var prefab in Prefabs) { float distance = (float)Math.Sqrt(Math.Pow(port.X - prefab.X, 2) + Math.Pow(port.Z - prefab.Z, 2)); if (distance < min && prefab.Navigation.Count > 1 && !prefab.Hidden) { min = distance; closerPrefab = prefab; } } ferryToPrefab[port.FerryPortId] = closerPrefab; } foreach (var port in FerryConnections) { foreach (var connection in LookupFerryConnection(port.FerryPortId)) { var ports = new List <TsItem>(); ports.Add(FerryPortbyId[connection.StartPortToken]); ports.Add(FerryPortbyId[connection.EndPortToken]); ferryToPrefab[connection.StartPortToken].Navigation.Add(ferryToPrefab[connection.EndPortToken], new Tuple <float, List <TsItem> >(connection.Distance, ports)); ports.Reverse(); ferryToPrefab[connection.EndPortToken].Navigation.Add(ferryToPrefab[connection.StartPortToken], new Tuple <float, List <TsItem> >(connection.Distance, ports)); } } }