@name WayPoint Test
@inputs EGP:wirelink
@persist [E O Prop]:entity [Chat Points Pos Store]:array Go
@persist Save [Mode FN Str]:string Ok Load K Count Ang:angle
@trigger all

if(first() | duped()){
runOnTick(1)
runOnChat(1)
runOnFile(1)

propSpawnEffect(0)
E = entity()
O = owner()  
Mode = "Edit" 
P = 1
Ok = 1
K = 0
Go = 1
holoCreate(1)
holoModel(1,"hqicosphere") 
holoScaleUnits(1,vec(10,10,10))

}

if(chatClk(O)){
Chat = O:lastSaid():explode(" ")    

if(Chat[1,string] == "-edit"){
Mode = "Edit"    
}

if(Chat[1,string] == "-start"){
Mode = "Play"    

}

if(Chat[1,string] == "-save" & Load == 0 & Save == 0){
FN = Chat[2,string]
Save = 1   
}  
  
if(Chat[1,string] == "-load" & Load == 0 & Save == 0){
FN = Chat[2,string]
Load = 1   
}  

}


# Modes

 
# edit mode
if(Mode == "Edit" & Save == 0){
#pointer
holoPos(1,O:aimPos() + holoEntity(1):up()*30)
holoAng(1,(O:aimNormal()):toAngle() + ang(90,0,0))
    
#create
if(changed(O:keyUse()) & O:keyUse() & Ok){
Prop = propSpawn("models/Holograms/hq_icosphere.mdl",holoEntity(1):pos(),holoEntity(1):angles(),1)
Prop:setColor(vec(255,255,255))
Points:pushEntity(Prop)
}

if(Points:count() > 0){
for(I = 1,Points:count()){
    
if(Points[I,entity]:isFrozen() == 0){
Points[I,entity]:setColor(vec(255,150,0))
Ok = 0
}elseif(Points[I,entity]:isFrozen() == 1){
Points[I,entity]:setColor(vec(255,255,255))
Ok = 1
}

}
}

}else{
holoAlpha(1,0)
}

if(Save & Ok == 1){

for(I=1,Points:count()){
Pos:pushVector(Points[I,entity]:pos())
Points[I,entity]:propDelete()
if(I >=Points:count()){ 
Save = 2  
break 
}
  
}

if(Save == 2){
Str = glonEncode(Pos)
FN = FN + ".txt"
fileWrite(FN,Str)
printColor(vec(0,255,0),"File " + FN + " Created and Saved!")
Save = 0
Pos:clear()
Points:clear()
}

}


if(Load){
Str = ""
fileLoad(FN+".txt")
Load = 0
}

if(fileClk()&fileLoaded()){     
Store:clear()
setName("WayPoint Test : " + fileStatus():toString())
Store = glonDecode(fileRead())

if(fileLoaded()){
print(FN + " loaded!")
Load = 2   
}

}

if(Load == 2){
    
for(I=1,Store:count()){
holoCreate(I+2)
holoModel(I+2,"cube")
holoScaleUnits(I+2,vec(10,10,10))
holoColor(I+2,vec(255,0,0))
holoAlpha(I+2,100)
holoPos(I+2,Store[I,vector])  

if(I >= Store:count()){
Count = Store:count()
holoPos(1,Store[1,vector])
Load = 0
break    
}

}
   
}

if(Mode == "Play"){
holoAlpha(1,255)

if(K <= Count & Go == 1){
K+=1
Go = 0
}

if(holoEntity(1):pos():distance(Store[K,vector])<= 3){
Go = 1

holoColor(K+2,vec(0,0,255))
holoAlpha(K+2,255)

if(K >= Count){
K = 0
Go = 0
holoAlpha(1,0)
Mode = ""    
}

}

Ang = (holoEntity(1):pos() - Store[K,vector]):toAngle()

holoColor(1,vec(0,255,0))
holoAng(1,Ang)
holoPos(1,holoEntity(1):pos() + -holoEntity(1):forward()*3)
}
