Ejemplo n.º 1
0
        // Returns true on success
        private static bool GetClosestInteractionPoint(RoomRunner pRoomRunner, Room pRoom, PointTileNode pStartTile, IntPoint[] pPossiblePoints, out IntPoint closestPoint, Character pCharacter, bool pIgnoreCharacters)
        {
            D.isNull(pRoom, "pRoom is null");
            D.isNull(pPossiblePoints, "possiblePoints is null");

            if (pRoom != pCharacter.room) {
                throw new Exception("Error for " + pCharacter.name + "! Can only pathfind to closest interaction point in the same room: " + pCharacter.room.name + ", tried to do it in: " + pRoom.name);
            }

            closestPoint = IntPoint.Zero;
            float shortestDistance = float.MaxValue;
            bool foundSomething = false;

            #if LOG
            s_logger.Log("Trying to find closest interaction point for " + pCharacter + ", nr of possible points: " + pPossiblePoints.Length);
            #endif

            foreach(IntPoint p in pPossiblePoints)
            {
                PointTileNode tileNode = pRoom.GetTile(p);
                if(tileNode == null) {
            #if LOG
                    s_logger.Log("Node at " + p + " was null, ignoring it");
            #endif
                    continue;
                }

                var ignoreList = notTrueObstacles;

                if (pIgnoreCharacters) {
                    ignoreList = notTrueObstaclesIncludingCharacters;
                }

                if(tileNode.HasOccupantsButIgnoreSomeTypes(ignoreList)) {
            #if LOG
                    s_logger.Log("Will ignore node at " + p + " since it has occupants: " + tileNode.GetOccupantsAsString());
            #endif
                    continue;
                }

            #if LOG
                s_logger.Log("Checking tile node " + tileNode);
            #endif

                pRoom.Reset();
                var path = _tilePathSolver.FindPath(pStartTile, tileNode, pRoomRunner, false);

            #if LOG
                s_logger.Log("RESULT Path from " + pStartTile + " to " + tileNode + ": " + path.status);
            #endif
                //D.Log("RESULT Path from " + pStartTile + " to " + tileNode + ": " + path.status);

                D.isNull(path, "path is null");
                if((path.status == PathStatus.FOUND_GOAL || path.status == PathStatus.ALREADY_THERE) && path.pathLength < shortestDistance) {
                    closestPoint = p;
                    shortestDistance = path.pathLength;
                    foundSomething = true;
                }

            #if LOG
                s_logger.Log("path.status = " + path.status);
            #endif
            }

            if(!foundSomething) {
            #if LOG
                s_logger.Log(pCharacter + " at position " + pCharacter.position + " can't find an interaction point for final target " + pCharacter.finalTargetTing);
            #endif
                return false;
            }

            return true;
        }