@name Treasure Hunt
@inputs 
@outputs Location:vector Clues:table Current:array Clue On
@persist Clues:table Clue
@trigger 

#make sure concmd is enabled or only the owner will get a message
Range=200 #How close does a player need to be to trigger a clue
runOnFile(1)
runOnTick(1)
runOnChat(1)
O=owner()
Chat=O:lastSaid()
Com="TH"
Mark="!H" #mark a hint
Erase="!erase" #erase all hints
Start="!start" #start the treasure hunt
Stop="!stop" #stop the treasure hunt
Save="!save" #save a hunt to file
Load="!load" #load a hunt
Reset="!reset" #start clue count at 1
Brk="<" #break character, used between load/save and file name


COM=Chat:find(Com)
MARK=Chat:find(Mark)
ERASE=Chat:find(Erase)
START=Chat:find(Start)
STOP=Chat:find(Stop)
SAVE=Chat:find(Save)
LOAD=Chat:find(Load)
BRK=Chat:find(Brk)
RESET=Chat:find(Reset)

Players=players()
hideChat(0)

if(chatClk()&COM){
    if(MARK){
        Location=round(O:pos())
        Clues:pushArray(array(Location,Chat:explode(Mark)[2,string]))
        print(Chat)
        hideChat(1)
    }
    if(ERASE){
        Clues=table()
        Clue=1
        print("All clues erased!")
    }
    if(RESET){
        Clue=1
        print("Current clue reset to first clue.")
    }
    if(START){On=1 print("Let the hunt begin!")}
    if(STOP){On=0 print("Let the hunt come to an end!")}
    if(SAVE&BRK){
        File="TreasureHunts/"+map()+"__"+Chat:explode(Brk)[2,string]+".txt"
        fileWrite(File,glonEncode(Clues))
        print("Saving "+Clues:count()+" clues to file: "+File)
    }
    if(LOAD&BRK){
        Clue=1
        File="TreasureHunts/"+map()+"__"+Chat:explode(Brk)[2,string]+".txt"
        fileLoad(File)
        print("Loading Clue File")
    }
    
}
if(fileClk()){
    Clues=glonDecodeTable(fileRead())
    print("Loading "+Clues:count()+" clues from file: "+File)
    
}

Current=array(Clues[Clue,array][1,vector],Clues[Clue,array][2,string])

if(first()){Clue=1}


if(On){
    
    for(C=1,Players:count()){
        if(Players[C,entity]!=O){
            if((round(Players[C,entity]:pos()):distance(Clues[Clue,array][1,vector])<Range)){
                concmd("say "+Clues[Clue,array][2,string])
                print(Clues[Clue,array][2,string])
                Clue++
            }
        }
    }
}


#[
    An option to copy with bbcode color syntax
    highlighting has been added (for use on forums).
    Right click anywhere in the editor to use it.

    User defined function support has been added.

    The find and replace windows have been combined into
    a single menu, and a go to line function has
    been added. Simply press ctrl+f/g/h to open it.

    Documentation and examples are available at:
    http://wiki.garrysmod.com/?title=Wire_Expression2
    The community is available at http://www.wiremod.com
]#
