Ejemplo n.º 1
0
        /// <summary>
        /// Checks to see if the AI has direct line of sight of an object it want s to use as cover
        /// </summary>
        /// <param name="coverObject"> Object to be checked for cover </param>
        /// <returns> True if the AI has direct line of sight </returns>
        private bool CheckSightOfCoverObject(Transform coverObject)
        {
            Vector3 start     = GetSightCheckPosition(trans, CoverNormal, CurrCoverType);
            Vector3 direction = coverObject.position - start;

            direction.Normalize();

            float distance = Vector3.Distance(start, coverObject.position);

            Ray        ray = new Ray(start, direction);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, distance))
            {
                if (hit.collider.transform.GetInstanceID() == coverObject.GetInstanceID())
                {
                    return(true);
                }
                else if (hit.collider.gameObject.layer != LayerMask.NameToLayer("Scenery"))
                {
                    RaycastHit rehit;
                    Vector3    hitPoint     = hit.point;
                    float      distanceLeft = distance - Vector3.Distance(hitPoint, start) - 0.05f;
                    int        iterations   = 0;
                    while (MissionInput.RaycastThroughObject(hitPoint, direction, distanceLeft, out rehit))
                    {
                        if (hit.collider.transform.GetInstanceID() == coverObject.GetInstanceID())
                        {
                            return(true);
                        }
                        else if (hit.collider.gameObject.layer != LayerMask.NameToLayer("Scenery"))
                        {
                            hitPoint     = rehit.point;
                            distanceLeft = distance - Vector3.Distance(hitPoint, start) - 0.05f;

                            iterations++;

                            if (iterations > 50)
                            {
                                break;
                            }

                            continue;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    return(false);
                }
                else
                {
                    return(false);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        public void StartMission_OutOfBoundariesEast_Exception()
        {
            // Arrange
            var missionController = this.CreateMissionController();
            var missionInput      = new MissionInput
            {
                Input = "5 5" + Environment.NewLine +
                        "5 0 E" + Environment.NewLine +
                        "M" + Environment.NewLine
            };

            // Act
            missionController.StartMission(missionInput);

            // Assert - Exception
        }
Ejemplo n.º 3
0
        public MissionResult StartMission(MissionInput missionInput)
        {
            var inputLines = ParseInput(missionInput).ToArray();
            var gridSize   = inputLines[0];

            SetupGrid(gridSize);

            var roverInstructions = inputLines.ToList();

            roverInstructions.RemoveAt(0);
            var output = ProcessRoverInstructions(roverInstructions);

            return(new MissionResult {
                MissionIsSuccess = true, RoversOutput = output
            });
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Perform validation on the input string, and returns the input as lines of input
        /// </summary>
        /// <param name="missionInput"></param>
        /// <returns></returns>
        private static IEnumerable <string> ParseInput(MissionInput missionInput)
        {
            if (string.IsNullOrEmpty(missionInput.Input.Trim()))
            {
                throw new MissionInputException("Empty Input String");
            }

            var lines = missionInput.Input.Split(new[] { Environment.NewLine },
                                                 StringSplitOptions.RemoveEmptyEntries);


            if (lines.Length == 0 || lines.Length % 2 != 1)
            {
                throw new MissionInputException("Wrong Format Input String");
            }

            // todo:  validation on
            return(lines);
        }
Ejemplo n.º 5
0
        public void StartMission_HappyPath()
        {
            // Arrange
            var missionController = this.CreateMissionController();
            var missionInput      = new MissionInput
            {
                Input = "5 5" + Environment.NewLine +
                        "1 2 N" + Environment.NewLine +
                        "LMLMLMLMM" + Environment.NewLine +
                        "3 3 E" + Environment.NewLine +
                        "MMRMMRMRRM"
            };

            // Act
            var result = missionController.StartMission(missionInput);

            // Assert

            Assert.IsTrue(result.RoversOutput.ContainsValue("1 3 N") &&
                          result.RoversOutput.ContainsValue("5 1 E"));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Checks whether there is a line of sight between this AI and their target
        /// </summary>
        /// <param name="target"> The target of the line of sight check </param>
        /// <param name="normal"> The normal of the cover of this AI </param>
        /// <returns> True if there is line of sight </returns>
        protected bool CheckLineOfSight(AbstractCombatPerson target, Vector3 normal)
        {
            Vector3 start     = GetSightCheckPosition(trans, normal, CurrCoverType);
            Vector3 end       = GetSightCheckPosition(target.Trans, target.CoverNormal, target.CurrCoverType);
            Vector3 direction = end - start;

            direction.Normalize();

            float distance = Vector3.Distance(start, end);

            Ray        ray = new Ray(start, direction);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, distance))
            {
                if (hit.collider.CompareTag(target.tag))
                {
                    return(true);
                }
                else if (hit.collider.CompareTag(tag))
                {
                    RaycastHit rehit;
                    Vector3    hitPoint     = hit.point;
                    float      distanceLeft = distance - Vector3.Distance(hitPoint, start) - 0.05f;
                    int        iterations   = 0;
                    while (MissionInput.RaycastThroughObject(hitPoint, direction, distanceLeft, out rehit))
                    {
                        if (hit.collider.CompareTag(target.tag))
                        {
                            return(true);
                        }
                        else if (hit.collider.CompareTag(tag))
                        {
                            hitPoint     = rehit.point;
                            distanceLeft = distance - Vector3.Distance(hitPoint, start) - 0.05f;

                            iterations++;

                            if (iterations > 50)
                            {
                                break;
                            }

                            continue;
                        }
                        else
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }