@name Automatic NPC placer
@inputs SaveNPC LoadNPC
@outputs Count Current Map1:string NPCXYZ:array NPCClass:array
@outputs CountXYZ CountClass Map:string Weap:array Ang:array
@outputs W:table Wep:array C1 C2 C3 C4 
@persist CurNPCsOnMap:array Save:array NPCXYZ:array NPCClass:array 
@persist W:table Wep:array Ang:array
@persist Map:string C1 C2 C3 C4 Map1:string
@trigger all

FileName="npcsave.txt" #Stub, fix it later

if (first()) {C3=0}
Time=1000
interval(Time)
#runOnChat(1)
Chat = entity():owner():lastSaid()
K = curtime() - entity():owner():lastSaidWhen() < (Time*1)/1000
#K=chatClk()

if (clk()) {
Map1=map()

W:setString("weapon_smg1","smg1")
W:setString("weapon_shotgun","shotgun")
W:setString("weapon_rpg","rpg")
W:setString("weapon_pistol","pistol")
W:setString("weapon_crowbar","crowbar")
W:setString("weapon_crossbow","crossbow")
W:setString("weapon_ar2","ar2")
W:setString("weapon_annabelle","annabelle")
W:setString("weapon_frag","frag")
#W:setString("gmod_tool","tool")


Suf="NPC"
Scan=Chat:find("Count"+Suf)
Store=Chat:find("Save"+Suf)
Restore=Chat:find("Load"+Suf)
MapName=Chat:find("Map"+Suf)
Saving=Chat:find("UL"+Suf)
Loading=Chat:find("DL"+Suf)

if (K&MapName){print("Saved Map: "+Map)}

#scan for npcs
Size=100000

findInSphere(entity():pos(),100000)
findIncludeClass("npc")
findExcludeClass("sent_npcspawner")
CurNPCsOnMap=findToArray()
Current=CurNPCsOnMap:count()



Count=Save:count()
CountXYZ=NPCXYZ:count()
CountClass=NPCClass:count()
CountWeapon=Wep:count()

if (Scan&K) {print(toString(CurNPCsOnMap:count()))}


#Store Function
if (SaveNPC|(Store&K)) {Save=CurNPCsOnMap:clone() C1=1 C2=1 C4=1
NPCXYZ=array()
NPCClass=array()
Map = map()
print("Saving NPCs and their XYZ's.") 
}#end Store function

#Restore Function
if (((Restore&K)|LoadNPC)&Save:count()){
if (Map==Map1) {C3=1 entity():setColor(0,0,255)}

C3=1}}
if (Map!="" &Map!=Map1) {entity():setColor(255,255,0) 
print("This chip's data is meant for "+Map+" not "+map())
selfDestruct()}
if (Map==""&K&Restore){print("You must use the save function before you can load.")

}#end of Interval clk()
if (C3>Count){C3=0} 
if (Count>CountXYZ) {timer("XYZ",1)}
#if (C3!=0 & C3<CountXYZ) {timer("spawn",1)}



#if (clk("XYZ")) {
if(Count>CountXYZ){
NPCXYZ:setVector(C1,Save:entity(C1):pos())
Ang:setNumber(C1,Save:entity(C1):angles():yaw())
Wep:setString(C1,W:string(Save:entity(C1):weapon():type()))
NPCClass:setString(C1,Save:entity(C1):type())
if (C1==1) {print("Saving: "+toString(C1)+"/"+toString(Count)+Wep:string(C1))}
while (C1<Count) {C1+=1 print("Saving: "+toString(C1)+"/"+toString(Count))}

}

#spawn function

if(C3!=0 & C3<CountXYZ){
NPC=npcSpawn(NPCClass:string(C3),NPCXYZ:vector(C3))
NPC:npcGiveWeapon(Wep:string(C3))
A=Ang:number(C3)
NPC:npcFace(vec(cos(A),sin(A),0))
if (C3==1) {print("Spawning: "+toString(C3)+"/"+toString(NPCXYZ:count()))}
while(Count>C3) {C3+=1 print("Spawning: "+toString(C3)+"/"+toString(NPCXYZ:count()))}
}

#Save to file function
if(K){
    if(Saving){
        S1=";"
        S2="  "
        S=S1+S2
        fileWrite(FileName,
        Map+S+                #1
        glonEncode(NPCXYZ)+S+ #2
        glonEncode(Ang)+S+    #3 
        glonEncode(Wep)+S+    #4 
        glonEncode(NPCClass)+S+ #5
        Count) #6
        
        print("Sending NPCs to file.")
    }
    
    if(Loading){timer("load",10010) print("Preparing to Receive Data...")}
}
if(clk("load")){
    print("Receving Data Now.")
    S1=";"
    S2="  "
    S=S1+S2
    Dump=fileRead(FileName):explode(S)
    #for(C=1,Dump:count()){Dump[C,string]:replace(S2,"")}
    C=1
    Map=Dump[C,string]:replace(S2,"") C++
    NPCXYZ=glonDecode(Dump[C,string]) C++
    Ang=glonDecode(Dump[C,string]) C++
    Wep=glonDecode(Dump[C,string]) C++
    NPCClass=glonDecode(Dump[C,string]) C++
    Count=Dump[C,string]:replace(S2,""):toNumber() C++
    print(Map)
    printTable(NPCXYZ)
    printTable(Ang)
    printTable(Wep)
    printTable(NPCClass)
    
}


