public PlottedMapPoint FindNearest(Vector3 location, int zoneId) { MapZone zone = (MapZone)Zones[zoneId]; PlottedMapPoint result = null; double distance = 0; foreach (DictionaryEntry entry in _mappedPoints) { PlottedMapPoint pt = (PlottedMapPoint)entry.Value; if (pt.MapPoint.MapZone == zone) { if (result == null) { result = pt; distance = pt.Location.Distance(location); } else if (pt.Location.Distance(location) < distance) { result = pt; distance = pt.Location.Distance(location); } } } return(result); }
public void Chart(int maxDistanceFromNode) { _maxDistanceFromNode = maxDistanceFromNode; foreach (DictionaryEntry entry in MappedPoints) { (entry.Value as PlottedMapPoint).NonPlayerEntities.Clear(); } //locate all npcs foreach (var npc in _npcs) { NonPlayerEntity entity = npc; _npcLocations.Remove(entity); Vector3 coords = new Vector3(entity.X, entity.Y, entity.Z); PlottedMapPoint pt = FindNearest(coords, entity.ZoneId); if ((pt != null) && (pt.Location.Distance(coords) < maxDistanceFromNode)) { pt.AddNPC(entity); _npcLocations.Add(entity, pt); } } //find shortest routes for each type (caching here so faster when playing game). }
public void AddNPE(NonPlayerEntity entity) { string key = entity.RomId.ToString() + "." + entity.UniqueId.ToString(); if (_npcLookup.ContainsKey(key)) { throw new ArgumentException("Entity Id already exists in the map", "entity"); } _npcLookup.Add(key, entity); if (_npcByRomId.ContainsKey(entity.RomId)) { throw new ArgumentException("Entity RomId already exists in the map", "entity"); } ; _npcByRomId.Add(entity.RomId, entity); Vector3 coords = new Vector3(entity.X, entity.Y, entity.Z); PlottedMapPoint pt = FindNearest(coords, entity.ZoneId); if ((pt != null) && (pt.Location.Distance(coords) < _maxDistanceFromNode)) { pt.AddNPC(entity); _npcLocations.Add(entity, pt); } }
public List <MapLink> BuildRoute(Vector3 start, int startZoneId, MapPoint destination) { PlottedMapPoint pStart = FindNearest(start, startZoneId); if (pStart == null) { return(new List <MapLink>()); } return(BuildRoute(pStart.MapPoint, destination)); }
public List <MapLink> BuildRoute(Vector3 start, int startZoneId, int npeId) { PlottedMapPoint pStart = FindNearest(start, startZoneId); //locate npe NonPlayerEntity entity = GetEntity(npeId); if ((entity == null) || (pStart == null)) { return(new List <MapLink>()); } PlottedMapPoint pEnd = (PlottedMapPoint)_npcLocations[entity]; if (pEnd == null) { return(new List <MapLink>()); } return(BuildRoute(pStart.MapPoint, pEnd.MapPoint)); }
public List <MapLink> BuildRouteToNearestType(MapPoint start, EntityTypes entityType) { List <MapLink> result = new List <MapLink>(); mapNode beginning = null; mapNode ending = null; PlottedMapPoint pStart = (PlottedMapPoint)_mappedPoints[start.Id]; if ((pStart.EntityTypes & entityType) == entityType) { return(result); } Dictionary <int, mapNode> nodes = new Dictionary <int, mapNode>(); beginning = new mapNode(pStart, 0); nodes.Add(pStart.MapPoint.Id, beginning); foreach (DictionaryEntry entry in _mappedPoints) { if (nodes.ContainsKey(((PlottedMapPoint)entry.Value).MapPoint.Id)) { continue; } mapNode node = new mapNode((PlottedMapPoint)entry.Value, double.MaxValue); nodes.Add(node.MapPoint.MapPoint.Id, node); } SortableList openList = new SortableList(new MapNodeComparer()); openList.KeepSorted = true; openList.AddDuplicates = true; openList.Add(beginning); Dictionary <int, mapNode> closedList = new Dictionary <int, mapNode>(); bool found = false; while ((!found) && (openList.Count > 0)) { mapNode current = (mapNode)openList[0]; if ((current.MapPoint.EntityTypes & entityType) == entityType) { found = true; ending = current; } else { closedList.Add(current.MapPoint.MapPoint.Id, current); openList.Remove(current); foreach (MapLink link in current.MapPoint.Links) { PlottedMapPoint end = (PlottedMapPoint)_mappedPoints[link.End.Id]; mapNode nEnd = nodes[link.End.Id]; double distance = 0; if (link.LinkType == LinkType.Teleport) { distance = 2000 + current.distance; } else { distance = current.Location.Distance(end.Location) + current.distance; } if (distance < nEnd.distance) { nEnd.distance = distance; nEnd.Parent = current; nEnd.link = link; if ((!closedList.ContainsKey(end.MapPoint.Id)) && (!_sortedListContains(openList, nEnd))) { openList.Add(nEnd); } } } } } if (found) { mapNode node = ending; while (node != beginning) { result.Insert(0, node.link); node = node.Parent; } } return(result); }
public mapNode(PlottedMapPoint mapPoint, double distance) { MapPoint = mapPoint; Location = mapPoint.Location; this.distance = distance; }