public static List <Item> FindKnownItems(StringWords input, MOB mob, int start, int end) { input.ValidateEndIndex(ref end); Item[] items = GeneralUtilities.VisibleItems(mob, mob.Body); List <Item> matchingItems = new List <Item>(); foreach (Item item in items) { if (mob.CanRecognize(item, input, start, end)) { matchingItems.Add(item); } } return(matchingItems); }
/// <summary> /// Find the first word in an input that matches a preposition from a list of prepositions /// </summary> /// <param name="text">Input to search</param> /// <param name="typeOfPreposition">Type/List of prepositions</param> /// <param name="prepositionValue">Which preposition was found (can be cast to that preposition). -1 if no preposition found.</param> /// <param name="startIndex">Which word in input to start searching on (inclusive). Defaults to 0 (start of input)</param> /// <param name="endIndex">Which word in input to stop searching on (exclusive). Defaults to -1 (end of input)</param> /// <returns>Index of the first word of the preposition</returns> public static int FindAPreposition(StringWords text, Type typeOfPreposition, out int prepositionValue, int startIndex = 0, int endIndex = -1) //TODO: Replace typeOfPreposition for an easier-to-use type of variable. An enum of enums? { text.ValidateEndIndex(ref endIndex); //Array values = Enum.GetValues(typeOfPreposition); string[][] options = OptionsForPreposition(typeOfPreposition); for (int i = startIndex; i < endIndex; i++) { int j = i; prepositionValue = MatchString(options, text.Segments, ref j); //NOTE: This assumes enums always have default-assigned values (0, 1, 2, etc.) if (prepositionValue != -1) { return(i); } } prepositionValue = -1; return(-1); }
/// <summary> /// Check if 'input' (or a specific part of it) can be found inside of 'text'. /// </summary> /// <param name="input">Text to search for</param> /// <param name="text">Text to look inside for matches</param> /// <param name="startIndex">First word of 'input' to start searching for</param> /// <param name="endIndex">After last word of 'input' to search for (exclusive end)</param> /// <returns>True if text contains input in the same order.</returns> public static bool CheckAutoCompleteText(StringWords input, string text, int startIndex = 0, int endIndex = -1) { input.ValidateEndIndex(ref endIndex); if (startIndex >= endIndex) { throw new ArgumentException("startIndex must be before endIndex"); } int textIndex = 0; for (int inputIndex = 0; textIndex < text.Length; textIndex++) { string wordToFind = input.Segments[inputIndex]; textIndex = text.IndexOf(wordToFind, textIndex); if (textIndex == -1) { return(false); //Input word was not found. } if (textIndex != 0) { //Check to make sure this could reasonably be the start of a word. If not, continue at the next character. char prevChar = text[textIndex - 1]; //TODO: Finetune below checks. //if (!Char.IsWhiteSpace(prevChar)) continue; if (Char.IsLetterOrDigit(prevChar)) { continue; } } //Found a match. Continue with the next word. inputIndex++; if (inputIndex == endIndex) { return(true); //No next word, found all the words. } textIndex += wordToFind.Length; //Skip the matched text (plus 1 character from loop counter) } //Ran out of letters before matching all the words. return(false); }
/// <summary> /// Attempt to parse user's input text as a direction / distance. /// </summary> /// <param name="text">Full input to parse</param> /// <param name="startIndex">Index of first word to try to parse (inclusive). </param> /// <param name="endIndex">Index of last word to try to parse (exclusive). If the text is found, this will be updated /// to after the last word found (first index not belonging to the MovementDirection)</param> /// <param name="requireDirection">If true, parsing will return failure if no direction component is found.</param> /// <param name="requireDistance">If true, parsing will return failure if no distance component is found.</param> /// <param name="getDistance">If true, distance may be parsed. This is ignored (assumed true) if requireDistance is true.</param> /// <returns>The direction and distance parsed from the input text.</returns> public static MovementDirection ParseAsDirection(StringWords text, int startIndex, ref int endIndex, bool requireDirection = false, bool requireDistance = false, bool getDirection = true, bool getDistance = true) { text.ValidateEndIndex(ref endIndex); getDistance |= requireDistance; Directions foundDirection = Directions.NoDirection; //List<Directions> directions = new List<Directions>(); KeyValuePair <string[][], Directions[]> directionOptions = DirectionCommand.GetDirectionOptions(); string[][] directionStrings = directionOptions.Key; Directions[] directionValues = directionOptions.Value; int foundDistance = -1; MovementUnit foundUnit = MovementUnit.NoDistance; int i = startIndex; for (; i < endIndex; i++) { string word = text.Segments[i]; if (foundDirection == Directions.NoDirection) { int found = MatchString(directionStrings, text.Segments, ref i); if (found != -1) { foundDirection = directionValues[found]; //Loop will increment but i is already the correct value after MatchString, i--; //decrement here to 'skip' the increment. continue; } } if (getDistance && foundUnit == MovementUnit.NoDistance) { int distanceWords = ParseAsDistance(text, out foundDistance, out foundUnit, i, Math.Min(i + 1, endIndex - 1)); if (distanceWords != 0) { if (distanceWords == 2) { i++; } continue; } } break; } if (foundDirection == Directions.NoDirection && requireDirection) { return(null); } if (foundUnit == MovementUnit.NoDistance && requireDistance) { return(null); } if (foundDirection == Directions.NoDirection && foundUnit == MovementUnit.NoDistance) { return(null); } endIndex = i; return(new MovementDirection() { direction = foundDirection, distanceCount = foundDistance, distanceUnit = foundUnit }); }