public SerializedJumpConnection(NavmeshLayserSerializer ns, CellContentPointedConnection connection)
 {
     interconnection    = connection.interconnection;
     connectedCell      = ns.GetCellID(connection.connection);
     enterPoint         = connection.enterPoint;
     lowerStandingPoint = connection.lowerStandingPoint;
     exitPoint          = connection.exitPoint;
     axis      = connection.axis;
     jumpState = (int)connection.jumpState;
 }
        //after deserializing cells
        public void DeserializeConnections(bool deserializeJumpConnections)
        {
            foreach (var cell in serializedGraph.serializedCells)
            {
                Cell fromCell = cellPool[cell.id];

                foreach (var connection in cell.serializedNormalConnections)
                {
                    CellContentGenericConnection newCon = new CellContentGenericConnection(
                        connection.data,
                        fromCell,
                        cellPool[connection.connectedCell],
                        connection.interconnection,
                        connection.costFrom,
                        connection.costTo,
                        connection.intersection);

                    fromCell.SetContent(newCon);
                }

                if (deserializeJumpConnections)
                {
                    foreach (var connection in cell.serializedJumpConnections)
                    {
                        CellContentPointedConnection newCon = new CellContentPointedConnection(
                            connection.enterPoint,
                            connection.lowerStandingPoint,
                            connection.exitPoint,
                            connection.axis,
                            (ConnectionJumpState)connection.jumpState,
                            fromCell,
                            cellPool[connection.connectedCell],
                            connection.interconnection);

                        fromCell.SetContent(newCon);
                    }
                }
            }
        }
        protected void FunnelPath(CellPath path, Vector3 endV3)
        {
            List <Cell> cellPath = path.path;

            List <CellContent> cellPathConnections = path.connections;

#if UNITY_EDITOR
            if (Debuger_K.debugPath)
            {
                for (int i = 0; i < cellPath.Count - 1; i++)
                {
                    Debuger_K.AddPath(cellPath[i].centerV3 + Vector3.up, cellPath[i + 1].centerV3 + Vector3.up, Color.magenta, 0.1f);
                }
            }
#endif
            int keyGate = 0;

            while (true)
            {
                if (keyGate >= cellPathConnections.Count)
                {
                    break;
                }

                int curKeyGate;

                List <CellContentGenericConnection> gateSiquence = new List <CellContentGenericConnection>();
                for (curKeyGate = keyGate; curKeyGate < cellPathConnections.Count; curKeyGate++)
                {
                    var c = cellPathConnections[curKeyGate];
                    if (c is CellContentGenericConnection)
                    {
                        gateSiquence.Add((CellContentGenericConnection)c);
                    }
                    else
                    {
                        break;
                    }
                }

                if (keyGate != curKeyGate)
                {
                    DoFunnelIteration(funnelPath.lastV3, curKeyGate == cellPathConnections.Count ? endV3 : (cellPathConnections[curKeyGate] as CellContentPointedConnection).enterPoint, gateSiquence);
                }

                if (curKeyGate != cellPathConnections.Count)
                {
                    if (cellPathConnections[curKeyGate] is CellContentPointedConnection)
                    {
                        CellContentPointedConnection ju = cellPathConnections[curKeyGate] as CellContentPointedConnection;
                        if (ju.jumpState == ConnectionJumpState.jumpUp)
                        {
                            funnelPath.AddMove(ju.lowerStandingPoint, (MoveState)(int)ju.from.passability);
                            funnelPath.AddJumpUp(ju.lowerStandingPoint, ju.axis);
                            funnelPath.AddMove(ju.exitPoint, (MoveState)(int)ju.connection.passability);
                        }
                        else
                        {
                            funnelPath.AddMove(ju.enterPoint, (MoveState)(int)ju.from.passability);
                            funnelPath.AddMove(ju.axis, (MoveState)(int)ju.from.passability);
                            funnelPath.AddJumpDown(ju.axis, ju.lowerStandingPoint);
                            funnelPath.AddMove(ju.exitPoint, (MoveState)(int)ju.connection.passability);
                        }
                    }
                    else
                    {
                        Debug.LogErrorFormat("idk type of CellConnectionAbstract node {0}", cellPathConnections[curKeyGate].GetType().Name);
                    }
                }

                keyGate = curKeyGate + 1;
            }

            funnelPath.AddMove(endV3, (MoveState)(int)cellPath[cellPath.Count - 1].passability);

#if UNITY_EDITOR
            if (Debuger_K.debugPath)
            {
                var resultNodes = funnelPath.nodes;
                for (int i = 0; i < resultNodes.Count - 1; i++)
                {
                    Debuger_K.AddPath(resultNodes[i].positionV3, resultNodes[i + 1].positionV3, Color.green);
                }
                //for (int i = 0; i < resultNodes.Count; i++) {
                //    Debuger3.AddDot(resultNodes[i].positionV3, Color.green, 0.03f, DebugGroup.path);
                //    Debuger3.AddLabel(resultNodes[i].positionV3, resultNodes[i].ToString(), DebugGroup.path);
                //}
            }
#endif
            funnelPath.RemoveFirst();
        }
示例#4
0
        public override void PerformWork(object context)
        {
            WorkContext con = (WorkContext)context;

            agent            = con.agent;
            properties       = agent.properties;
            target           = con.target;
            maxSearchCost    = con.maxSearchCost;
            searchToArea     = con.searchToArea;
            start            = con.start;
            snapToNavMesh    = con.snapToNavMesh;
            callBack         = con.callBack;
            applyRaycast     = con.applyRaycast;
            ignoreCrouchCost = con.ignoreCrouchCost;

            path       = Path.pathPool.Rent();
            path.owner = agent;

            //start parameters
            Cell    cellStart;
            bool    outsideCell;
            Vector3 closestPoint;

            #region start parameters
            //find start point
            if (PathFinder.TryGetClosestCell(start, properties, out cellStart, out outsideCell, out closestPoint) == false)
            {
                if (con.callBack != null)
                {
                    con.callBack.Invoke();
                }

                path.pathType = PathResultType.InvalidAgentOutsideNavmesh;
                Finish();
                return;
            }


            //Debuger_K.ClearGeneric();
            //Debuger_K.AddLine(start, cellStart.centerVector3, Color.red);

            AddMove(start, cellStart);

            if (snapToNavMesh && outsideCell)
            {
                start = closestPoint;
                AddMove(closestPoint, cellStart);
            }

            if (searchToArea)
            {
                if (cellStart.area == target)
                {
                    path.pathType = PathResultType.Valid;    //if there is no issues at this point then path is valid
                    Finish();
                    return;
                }
            }
            else
            {
                if (cellStart.area != target)
                {
                    Finish();
                    return;
                }
            }
            #endregion

            closed.Add(cellStart);
            foreach (var ccon in cellStart.connections)
            {
                AddNode(-1, ccon, ccon.Cost(start, properties, ignoreCrouchCost));
            }

            for (int i = 0; i < 5000; i++)
            {
                if (heapCount == 0)
                {
                    break;
                }

                Node curNode = HeapRemoveFirst();
                if (curNode.g > maxSearchCost)
                {
                    path.pathType = PathResultType.InvalidNoPath;
                    Finish();
                    return;
                }

                Cell curNodeCell = curNode.content.connection;

                //var c = curNode.content;
                //Cell c1 = c.from;
                //Cell c2 = c.connection;
                //Vector3 v1 = c1.centerVector3;
                //Vector3 v2 = c2.centerVector3;
                //Debuger_K.AddLine(v1, v2, Color.red, 0.1f);
                //Debuger_K.AddLabel(SomeMath.MidPoint(v1, v2), curNode.gh);

                if (searchToArea)
                {
                    if (curNodeCell.area == target)
                    {
                        Restore(curNode.index);
                        break;
                    }
                }
                else
                {
                    if (curNodeCell.area != target)
                    {
                        Restore(curNode.index);
                        break;
                    }
                }

                if (closed.Add(curNodeCell) == false)
                {
                    continue;
                }

                foreach (var ccon in curNodeCell.connections)
                {
                    AddNode(curNode.index, ccon, curNode.g + ccon.Cost(properties, ignoreCrouchCost));
                }
            }

            if (cellPath.Count == 0)
            {
                if (con.callBack != null)
                {
                    con.callBack.Invoke();
                }

                path.pathType = PathResultType.InvalidNoPath;
                Finish();
                return;
            }

            //funnel
            cellPath.Reverse();

#if UNITY_EDITOR
            //for (int cpi = 0; cpi < cellPath.Count; cpi++) {
            //    var f = cellPath[cpi];
            //    Vector3 ff = f.from.centerVector3;
            //    Vector3 fc = f.connection.centerVector3;
            //    Debuger_K.AddLine(ff, fc, Color.blue, 0.2f);
            //}
#endif
            Vector3 targetPos = cellPath[cellPath.Count - 1].connection.centerVector3;

            int keyGate = 0;
            while (true)
            {
                if (keyGate >= cellPath.Count)
                {
                    break;
                }

                int curKeyGate;

                gateSiquence.Clear();
                for (curKeyGate = keyGate; curKeyGate < cellPath.Count; curKeyGate++)
                {
                    var c = cellPath[curKeyGate];
                    if (c is CellContentGenericConnection)
                    {
                        gateSiquence.Add((CellContentGenericConnection)c);
                    }
                    else
                    {
                        break;
                    }
                }

                if (keyGate != curKeyGate)
                {
                    DoFunnelIteration(path.lastV3, curKeyGate == cellPath.Count ? targetPos : (cellPath[curKeyGate] as CellContentPointedConnection).enterPoint, gateSiquence);
                }

                if (curKeyGate != cellPath.Count)
                {
                    if (cellPath[curKeyGate] is CellContentPointedConnection)
                    {
                        CellContentPointedConnection ju = cellPath[curKeyGate] as CellContentPointedConnection;
                        if (ju.jumpState == ConnectionJumpState.jumpUp)
                        {
                            AddMove(ju.lowerStandingPoint, ju.from);
                            AddJumpUp(ju.lowerStandingPoint, ju.axis);
                            AddMove(ju.exitPoint, ju.connection);
                        }
                        else
                        {
                            AddMove(ju.enterPoint, ju.from);
                            AddMove(ju.axis, ju.from);
                            AddJumpDown(ju.axis, ju.lowerStandingPoint);
                            AddMove(ju.exitPoint, ju.connection);
                        }
                    }
                    else
                    {
                        Debug.LogErrorFormat("idk type of CellConnectionAbstract node {0}", cellPath[curKeyGate].GetType().Name);
                    }
                }

                keyGate = curKeyGate + 1;
            }

            AddMove(targetPos, cellPath[cellPath.Count - 1].connection);

            path.pathType = PathResultType.Valid;    //if there is no issues at this point then path is valid
            Finish();
        }