Exemplo n.º 1
0
//		private void PrecalcultateBridges()
//		{
//			for(int i = 0;i < Bridges.Count;i++)
//			{
//				foreach(WalkType walkType in Enum.GetValues(typeof(WalkType)).Cast<WalkType>().ToList())
//				{
//					for(int j = i;j < Bridges.Count;j++)
//					{
//						if(Bridges[i].GetParameter(walkType).GroupBridge.Count > 0 && Bridges[j].GetParameter(walkType).GroupBridge.Count > 0 )
//						{
//							AStartPathFinding(Bridges[i].Point,Bridges[j].Point);
//						}
//					}
//				}
//			}
//		}

        public bool HasBridgeBetween(PathfindNode startPoint, PathfindNode endPoint, WalkType walkType)
        {
//			if(m_linkedGroup == null)
//				m_linkedGroup = new Dictionary<int, List<int>>();
//
//			if(m_linkedGroup.ContainsKey(startPoint.GetParameter(walkType).Group) )
//				return m_linkedGroup[startPoint.GetParameter(walkType).Group].Contains(endPoint.GetParameter(walkType).Group);

            if (m_linkedGroupInfo == null)
            {
                m_linkedGroupInfo = new Dictionary <KeyValuePair <int, int>, bool>();
            }

            KeyValuePair <int, int> pair = new KeyValuePair <int, int>(startPoint.GetParameter(walkType).Group, endPoint.GetParameter(walkType).Group);

            if (m_linkedGroupInfo.ContainsKey(pair))
            {
                //Debug.Log("pair in dico");
                return(m_linkedGroupInfo[pair]);
            }

            List <int>          walkedGroup   = new List <int>();
            List <PathfindNode> intermediates = new List <PathfindNode>();

            intermediates.Add(startPoint);
            List <int> linkedGroups = new List <int>();

            linkedGroups.Add(startPoint.GetParameter(walkType).Group);
            bool hasLink = HasLinkBetween(startPoint.GetParameter(walkType).Group, endPoint.GetParameter(walkType).Group, walkType, ref linkedGroups);

//			if(m_linkedGroup.ContainsKey(startPoint.GetParameter(walkType).Group) == false)
//				m_linkedGroup.Add(startPoint.GetParameter(walkType).Group,linkedGroups);
//			else
//				m_linkedGroup[startPoint.GetParameter(walkType).Group] = linkedGroups;
            if (m_linkedGroupInfo.ContainsKey(pair) == false)
            {
                m_linkedGroupInfo.Add(pair, hasLink);
            }
            else
            {
                m_linkedGroupInfo[pair] = hasLink;
            }
            return(hasLink);
//			return GetIntermediatesPoint (startPoint.GetParameter(walkType).Group,
//			                              endPoint.GetParameter(walkType).Group, walkType,ref walkedGroup,ref intermediates,endPoint);
        }
Exemplo n.º 2
0
        Color GetColorWalk(PathfindNode node)
        {
            Color GizmoColor = NotWalkableColor;
            PathfindNodeGroupParameter param = node.GetParameter(drawWalkType);

            if (param.Enable)
            {
                GizmoColor = WalkableColor;
            }
            return(GizmoColor);
        }
Exemplo n.º 3
0
        public bool ContainsGroupOf(PathfindNode node, WalkType walks)
        {
//			if(node.Point == new Point(45,23))
//			{
//				int dsd=0;
//				dsd +=1;
//			}
            if (ContainsGroup(node.GetParameter(WalkType).Group))
            {
                return(true);
            }
            foreach (int group in node.GetParameter(WalkType).GroupBridge)
            {
                if (ContainsGroup(group))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 4
0
        static bool BelongSameGroup(PathfindNode startPoint, PathfindNode endPoint, WalkType walktype)
        {
            List <int> StartsGroups = new List <int>();

            foreach (int b in startPoint.GetParameter(walktype).GroupBridge)
            {
                StartsGroups.Add(b);
            }
            StartsGroups.Add(startPoint.GetParameter(walktype).Group);
            if (StartsGroups.Contains(endPoint.GetParameter(walktype).Group))
            {
                return(true);
            }
            foreach (int b in endPoint.GetParameter(walktype).GroupBridge)
            {
                if (StartsGroups.Contains(b))
                {
                    return(true);
                }
            }
            return(false);
            //return (startPoint.GetParameter(walkType).Group != endPoint.GetParameter (walkType).Group);
        }
Exemplo n.º 5
0
        Color GetColorGroup(PathfindNode node)
        {
            Color GizmoColor = Color.white;

            GizmoColor.a = 0;
            PathfindNodeGroupParameter param = node.GetParameter(drawWalkType);

            if (param.Enable)
            {
                GizmoColor = GetColorFromGoupID(param.Group);
                if (param.GroupBridge.Count > 0)
                {
                    GizmoColor = Color.white;
                }
            }
            return(GizmoColor);
        }
Exemplo n.º 6
0
        private bool HasLinkBetween(int startGroup, int endGroup, WalkType walkType, ref List <int> linkedGroups)
        {
            bool HasNewGroups = false;

            for (int bri = instance.Bridges.Count - 1; bri >= 0; bri--)
            {
                PathfindNode bridge = instance.Bridges [bri];
                PathfindNodeGroupParameter groupParameter = bridge.GetParameter(walkType);
                bool containsPreviousGroup = linkedGroups.Contains(groupParameter.Group);
                for (int i = 0; i < groupParameter.GroupBridge.Count; i++)
                {
                    int bridgeGroup = groupParameter.GroupBridge [i];
                    if (linkedGroups.Contains(bridgeGroup))
                    {
                        containsPreviousGroup = true;
                    }
                }

                if (containsPreviousGroup)
                {
                    if (linkedGroups.Contains(groupParameter.Group) == false)
                    {
                        HasNewGroups = true;
                        linkedGroups.Add(groupParameter.Group);
                    }
                    for (int i = 0; i < groupParameter.GroupBridge.Count; i++)
                    {
                        int bridgeGroup = groupParameter.GroupBridge [i];
                        if (linkedGroups.Contains(bridgeGroup) == false)
                        {
                            HasNewGroups = true;
                            linkedGroups.Add(bridgeGroup);
                        }
                    }
                }
            }
            if (HasNewGroups)
            {
                return(HasLinkBetween(startGroup, endGroup, walkType, ref linkedGroups));
            }
            else
            {
                return(linkedGroups.Contains(endGroup));
            }
            return(false);
        }
Exemplo n.º 7
0
        static List <PathfindNode> GetAllBridges(int from, int to, WalkType walkType)
        {
            List <PathfindNode> tmpIntermediate = new List <PathfindNode>();

            for (int bri = instance.Bridges.Count - 1; bri >= 0; bri--)
            {
                PathfindNode bridge = instance.Bridges [bri];
                PathfindNodeGroupParameter groupParameter = bridge.GetParameter(walkType);
                if (groupParameter.Group == from || groupParameter.Group == to)
                {
                    for (int i = 0; i < groupParameter.GroupBridge.Count; i++)
                    {
                        int bridgeGroup = groupParameter.GroupBridge [i];
                        if (bridgeGroup == to || bridgeGroup == from)
                        {
                            tmpIntermediate.Add(bridge);
                        }
                    }
                }
            }
            return(tmpIntermediate);
        }
Exemplo n.º 8
0
        //return succes
        private static bool GetIntermediatesPoint(int startGroup, int endGroup, WalkType walkType, ref List <int> WalkedGroup, ref List <PathfindNode> intermediate, PathfindNode targetPoint)
        {
            List <PathfindNode> tmpIntermediate  = new List <PathfindNode>();
            List <PathfindNode> stepIntermediate = new List <PathfindNode>();

            for (int bri = instance.Bridges.Count - 1; bri >= 0; bri--)
            {
                PathfindNode bridge = instance.Bridges [bri];
                PathfindNodeGroupParameter groupParameter = bridge.GetParameter(walkType);
                if (groupParameter.Group == startGroup || groupParameter.Group == endGroup)
                {
                    for (int i = 0; i < groupParameter.GroupBridge.Count; i++)
                    {
                        int bridgeGroup = groupParameter.GroupBridge [i];
                        if (bridgeGroup == endGroup || bridgeGroup == startGroup)
                        {
                            groupParameter.GroupBridge [i] = endGroup;
                            groupParameter.Group           = startGroup;
                            tmpIntermediate.Add(bridge);
                        }
                    }
                }
            }
            if (tmpIntermediate.Count > 0)
            {
                int bestBridge = GetBestBridgeIn(walkType, tmpIntermediate, intermediate [intermediate.Count - 1], targetPoint);
                intermediate.Add(tmpIntermediate [bestBridge]);
                PathfindNodeGroupParameter NextPointIntermediate = tmpIntermediate [bestBridge].GetParameter(walkType);
                return(true);
            }
            else
            {
                WalkedGroup.Add(startGroup);
                for (int bri = instance.Bridges.Count - 1; bri >= 0; bri--)
                {
                    PathfindNode bridge = instance.Bridges [bri];
                    PathfindNodeGroupParameter groupParameter = bridge.GetParameter(walkType);
                    List <int> bridgeGroups = new List <int>(groupParameter.GroupBridge);
                    bridgeGroups.Add(groupParameter.Group);
                    if (bridgeGroups.Contains(startGroup))
                    {
                        for (int i = 0; i < bridgeGroups.Count; i++)
                        {
                            if (WalkedGroup.Contains(bridgeGroups[i]) == false)
                            {
                                int newGroupIndex = bridgeGroups[i];
                                List <PathfindNode> localBridges = GetAllBridges(bridgeGroups[i], startGroup, walkType);
                                int          bestBridgeIndex     = GetBestBridgeIn(walkType, localBridges, intermediate [intermediate.Count - 1], targetPoint);
                                PathfindNode bestBridge          = null;
                                if (bestBridgeIndex >= 0)
                                {
                                    bestBridge = localBridges[bestBridgeIndex];
                                }
                                else
                                {
                                    Debug.Log("can't find bestbrige");
                                    Debug.Break();
                                }
                                intermediate.Add(bestBridge);
                                if (GetIntermediatesPoint(bridgeGroups[i], endGroup, walkType, ref WalkedGroup, ref intermediate, targetPoint))
                                {
                                    return(true);
                                }
                                else
                                {
                                    intermediate.Remove(bestBridge);
                                }
                            }
                        }
                    }
                }
                return(false);
            }
        }
Exemplo n.º 9
0
        private static IEnumerator AStartPathFinding(PathfindNode startPoint, PathfindNode endPoint, PathComplete Callback, Dictionary <Point, PathfindNode> walkableArea, WalkType walkType, int loopMaxCount = int.MaxValue)
        {
            if (startPoint.Point == endPoint.Point)
            {
                Callback.Invoke(null);
            }
            int startGroup = startPoint.GetParameter(walkType).Group;

            if (PathFindingManager.PrecalculatedPaths != null)
            {
//                foreach(PointCouple couple in PathFindingManager.PrecalculatedPaths.Keys)
//                {
//                    //Debug.Log(couple);
//                    if(couple.StartPoint == startPoint.Point && couple.EndPoint == endPoint.Point)
//                    {
//                      //  Debug.Log("FIND COUPLE");
//                    }
//                }
                if (PathFindingManager.PrecalculatedPaths.ContainsKey(new PointCouple(startPoint.Point, endPoint.Point)))
                {
                    //instance.Processing = false;
                    Callback.Invoke(PathFindingManager.PrecalculatedPaths[new PointCouple(startPoint.Point, endPoint.Point)].Clone());
                    yield break;
                }
            }

            Dictionary <Point, PathfindNode> reachablePoints = walkableArea;          // s_instance.CompleteArea;

            if (reachablePoints.ContainsKey(endPoint.Point) == false)
            {
                //Debug.Log("Node not reachable: "+endPoint.Point);
                //instance.Processing = false;
                Callback.Invoke(null);
                yield break;
            }
//			if (BelongSameGroup (startPoint,endPoint,walkType) == false) {
//
//				int endGroup = endPoint.GetParameter (walkType).Group;
//				List<int> walkedGroup = new List<int>();
//				List<PathfindNode> intermediates = new List<PathfindNode>();
//				intermediates.Insert(0,startPoint);
//				bool succes = GetIntermediatesPoint (startGroup, endGroup, walkType,ref walkedGroup,ref intermediates,endPoint);
//				if(succes == false)
//				{
//					//instance.Processing = false;
//					Callback.Invoke(null);
//					yield break;
//				}
//				intermediates.Add(endPoint);
//				Enemy PathOwner = Callback.Target as Enemy;
//				PathOwner.ClearStepedPath();
//				for(int i =0;i<intermediates.Count-1;i++)
//				{
//					yield return instance.StartCoroutine (AStartPathFinding (intermediates [i], intermediates [i+1], PathOwner.BuildPathFromStepCallback, walkableArea, walkType, loopMaxCount));
//				}
//
//				//instance.Processing = false;
//				Callback.Invoke(PathOwner.GetStepedPath());
//				yield break;
//			}
            List <AStarNode> openSet = new List <AStarNode>();
            Dictionary <Point, AStarNode> openSetDico = new Dictionary <Point, AStarNode>();

            openSet.Add(new AStarNode(startPoint));
            openSetDico.Add(startPoint.Point, openSet[openSet.Count - 1]);
            Dictionary <Point, AStarNode> closedSet = new Dictionary <Point, AStarNode>();

            float fscore;                                                    // = (endPoint-startPoint).MagnitudeZero();

            fscore = Point.MagnitudeZero(endPoint.Point - startPoint.Point); //+(1 -Vector2.Dot(toNeighbor,toEnd))*20;

            int loopLimit = 0;

            while (openSet.Count > 0 && loopMaxCount > 0)
            {
                //Debug.Log(openSet.Count);
                loopMaxCount--;
                loopLimit++;
                openSet.Sort();
                AStarNode current = openSet[0];
                if (current.Position == endPoint)
                {
                    Path path = new Path(endPoint);
                    while (current.Parent != null)
                    {
                        path.steps.Add(current.Position);
                        current = current.Parent;
                    }
                    path.steps.Reverse();



                    if (PathFindingManager.PrecalculatedPaths == null)
                    {
                        PathFindingManager.PrecalculatedPaths = new Dictionary <PointCouple, Path>();
                    }
                    PointCouple couple = new PointCouple(startPoint.Point, endPoint.Point);
                    if (PathFindingManager.PrecalculatedPaths.ContainsKey(couple))
                    {
                        Debug.Log("Try add existing couple: " + couple);
                    }
                    else
                    {
                        PathFindingManager.PrecalculatedPaths.Add(couple, path);
                    }

                    //instance.Processing = false;
                    Callback.Invoke(path.Clone());
                    yield break;
                }
                openSetDico.Remove(openSet[0].Position.Point);
                openSet.RemoveAt(0);
                closedSet.Add(current.Position.Point, current);
                if (loopLimit > 10)
                {
                    yield return(new WaitForEndOfFrame());

                    loopLimit = 0;
                }
                for (int i = 0; i < Neighbourhood.Length; i++)
                {
                    PathFindNeighbourg neighbourPath        = Neighbourhood [i];
                    PathfindNode       neighbour            = new PathfindNode(neighbourPath.offset);
                    PathfindNode       neigbourRealPosition = neighbour + current.Position;
                    if (reachablePoints.ContainsKey(neigbourRealPosition.Point) == false)
                    {
                        continue;
                    }
                    AStarNode AstarNeighbor = new AStarNode(neigbourRealPosition);
                    AstarNeighbor.Position = neigbourRealPosition;
                    //current.Position;
                    AstarNeighbor.Parent = current;
                    if (closedSet.ContainsKey(AstarNeighbor.Position.Point))
                    {
                        continue;
                    }
                    bool containsStartGroup = instance.CompleteNodeArea.GetNodeAt(neigbourRealPosition.Point).GetParameter(walkType).ContainsGroupOf(endPoint, walkType);
                    if (containsStartGroup == false)
                    {
                        continue;
                    }
                    float     tentativeGscore = current.gscore + neighbourPath.cost;
                    AStarNode InSetNeighbor   = null;
                    //GetNodeInList(AstarNeighbor,openSet);
                    if (openSetDico.ContainsKey(AstarNeighbor.Position.Point))
                    {
                        InSetNeighbor = openSetDico [AstarNeighbor.Position.Point];
                    }
                    if (InSetNeighbor == null || tentativeGscore < InSetNeighbor.gscore)
                    {
                        bool needAddToSet = false;
                        if (InSetNeighbor == null)
                        {
                            InSetNeighbor = new AStarNode(neigbourRealPosition);
                            needAddToSet  = true;
                        }
                        InSetNeighbor.Parent = current;
                        InSetNeighbor.gscore = tentativeGscore;
                        InSetNeighbor.fscore = InSetNeighbor.gscore + heuristique(AstarNeighbor, endPoint.Point, current.Position.Point);
                        if (needAddToSet)
                        {
                            AstarNeighbor.fscore = InSetNeighbor.fscore;
                            AstarNeighbor.gscore = InSetNeighbor.gscore;
                            openSet.Add(AstarNeighbor);
                            openSetDico.Add(AstarNeighbor.Position.Point, openSet [openSet.Count - 1]);
                        }
                    }
                }
            }

            //instance.Processing = false;
            Callback.Invoke(null);
            //return null;
        }