@name Multi Exit Teleporter
@inputs 
@outputs 
@persist [Loc Names]:array File:string
@trigger 
#[ ]#
runOnChat(1)
runOnFile(1)
O=owner()
Chat=lastSaid()
Spoke=lastSpoke()
Cmd="MET"
Menu="menu"
Help="help"
Save="save"
Load="load"
Set="set"
Clear="clear"
Brk="<"
Break=" "
#[]#
#"
CMD=Chat:find(Cmd)
MENU=Chat:find(Menu)
HELP=Chat:find(Help)
SAVE=Chat:find(Save)
LOAD=Chat:find(Load)
SET=Chat:find(Set)
CLEAR=Chat:find(Clear)#[ ]#
BRK=Chat:find(Brk)
BREAK=Chat:find(Break)
Input=Chat:explode(Brk)[2,string]
INPUT=Chat:explode(Break)[2,string]

if(first()|duped()){
    #ifdef entity:setPos(vector)
    #else
        print("E:setPos(V) not found! Self Destructing!")
        selfDestruct()
    #endif
}
function string entity:teleport(Num){
        This:setPos(Loc[Num,vector])
        return Names[Num,string]
}

function void locate(Vec:vector,Name:string){
    Names:pushString(Name)
    Loc:pushVector(round(Vec))
}
function string save(File:string){
    T=table()
    T[1,array]=Loc
    T[2,array]=Names
    Glon=glonEncode(T)
    fileWrite("MET/"+map()+"__"+File,Glon)
    return "File "+File+" saved with "+Names:count()+" locations"
    
}
function string load(File:string){
    fileLoad("MET/"+map()+"__"+File)
    Glon=fileRead()
    T=table()
    T=glonDecodeTable(Glon)   
    Names=array() Loc=array()
    Loc=T[1,array]
    Names=T[2,array]
    return "File "+File+" loaded with "+Names:count()+" locations"
}
function string menu(){
    Menuitems=array()
    for(C=1,Loc:count()){
        Menuitems[C,string]=C+") "+Names[C,string]+"\n"
    }
    return Menuitems:concat()
}
#"
if(fileClk()){print(load(File))}
if(chatClk()&CMD){
    if(MENU){
        print(menu())
    }
    if(HELP){
        print("All commands must contain "+Cmd)
        print("'"+Brk+"' is used to separate certain commands from inputs")
        print(Load+Brk+" is used to spesify a teleport network for the current map")
        print(Save+Brk+" is used to save the current teleport network for the current map")
        print(Set+Brk+" input the name of a location after the "+Brk+" to save the location")
        print(Menu+" display a list of locations with their menu numbers")
        print("'"+Break+"' (a space) is used to jump to a location by number if '"+Brk+"' is not present")
        print(Clear+Brk+"(some number) will remove that location from the list.")
        print(Help+" this list")
        
    }
    if(BRK){    
        if(SET){
            locate(Spoke:pos(),Input)
            print("Setting location number "+Loc:count()+" to "+Input)
        }
        if(SAVE){
            print(save(Input))
        }
        if(LOAD){
            print(load(Input))
        }
        if(CLEAR){
            Number=Input:toNumber()
            print("Location: '"+Names[Number,string]+"' has been removed.") 
            Names:remove(Number)
            Loc:remove(Number)
            
        }
        
        
    }elseif(BREAK){
        Report=Spoke:teleport(INPUT:toNumber())
        print(Report)
    }
}



