ID:257770
 
//Title: Is Text In Order
//Credit to: DivineO'peanut
//Contributed by: YDivineO'peanut


/*
Returns TRUE if the specified words are found in [txt] in order, and FALSE otherwise.

Format: IsTxtInOrder(text_scanned,words)

Lists are used when multiple words can be found in the text segment.
In example, IsTxtInOrder(text_scanned,list("Hi","Hello"),"World")
would return TRUE if the text is "Hi World!" or "Hello World!".
*/



proc
IsTxtInOrder(txt)
var/cur_pos = 1
for(var/word_pos = 2 to args.len)
var/word = args[word_pos]
if(istype(word,/list))
var/w
var/temp_pos
nest_loop:
// This tag is here because calling "break" in the
// following loop may actually break the main loop.
for(w in word)
temp_pos = findtext(txt,w,cur_pos)
if(temp_pos) break nest_loop
cur_pos = temp_pos
else
cur_pos = findtext(txt,word,cur_pos)
if(!cur_pos) return 0
return 1

///*
//Testing Code/Sample Implementation:

mob/verb/say(text_scanned as text)
src << "-- \"[text_scanned]\""

if(IsTxtInOrder(text_scanned,list("Hi","Hello"),"World"))
/* Example text that would return TRUE:

"Hi World!"
"Hello World!"
"Why hello there, world!"
"Hellow, worldy!" -- If you don't want the scan to accept words like this,
IsTxtInOrder(text_scanned,list(" Hi "," Hello ")," World ") should be used instead,
where spaces surround each scanned text segment.
*/

src << "Hello, [usr.name]!"
if(IsTxtInOrder(text_scanned,list("How's","How"),"it","going","?"))
/* Example text that would return TRUE:

"How's it going, World?"
"How is it going, World?"
"So... How's it going, World? Haven't talked to you in a while."
*/


src << "Oh, just the usual, [usr.name]."

//*/
Dan has a version of this, called FindOrderedText. I'll see if I can't put it up.