@name Prop-to-holo
@persist [Ent Model Color Material Pos Ang]:array #Array
@persist [Base]:entity #Entity
@persist [Version Wep Name ModelStr ColorStr MatStr PosStr AngStr FinalText]:string
@persist Ready ArrayAdd Debug Int
interval(50)
if(first())
{
    Int = 1
    Debug = 1
    
    runOnChat(1)
    print("] Welome to Prop-to-holo!")
    print("] All files are saved in garrysmod/garrysmod/data/Expression2/e2shared")
    print("] To save files, type \"-save\". To clear everything selected, type \"-clear\".")
    print("] For the following functions to work, you must have your Gravity Gun out.")
    print("] -Press E to select/de-select props.")
    print("] -Press R to select your base prop.")
    print("] When saving files, you must have a base prop selected, or your file will not be saved.")
    
    Wep = "weapon_physcannon"
}

if(!Ready)
{
    #Chat commands
    if(chatClk(owner()))
    {
        CR = owner():lastSaid():explode(" ")
        if(CR[1,string]=="-save")
        {
            hideChat(1)
            if(!CR[2,string]:lower():find(".txt"))
            {
                Name = CR[2,string]+".txt"
            }else
            {
                Name = CR[2,string]
            }
            Ready = 1
            print("] Preparing save")
        }
        if(CR[1,string]=="-clear")
        {
            hideChat(1)
            for(I=1,Ent:count())
            {
                ATE = Ent[I,entity]
                ATE:setColor(Color[I,vector4])
            }
            Ent:clear()
            Model:clear()
            Color:clear()
            Material:clear()
            Pos:clear()
            Ang:clear()
            Base = noentity()
            print("] Everything cleared")
        }
    }
    
    #Selecting
    if(owner():weapon():type()==Wep)
    {
        Pressed = changed(owner():keyUse())||changed(owner():keyReload())
        if(Pressed)
        {
            TE = owner():aimEntity()
            if(owner():keyUse())
            {
                if(Ent:count()>0)
                {
                    for(I=1,Ent:count())
                    {
                        ATE = Ent[I,entity]
                        if(TE == ATE)
                        {
                            ArrayAdd = 0
                            ATE:setColor(Color[I,vector4])
                            Ent:remove(I)
                            Model:remove(I)
                            Color:remove(I)
                            Material:remove(I)
                            Pos:remove(I)
                            Ang:remove(I)
                            print("] Deselecting prop")
                            if(Debug)
                            {
                                hint(Ent:count():toString(),1)
                            }
                            break
                        }else
                        {
                            ArrayAdd = 1
                        }
                    }
                }else
                {
                    ArrayAdd = 1
                }
                if(ArrayAdd)
                {
                    if(Base:isValid())
                    {
                        if(TE:type()=="prop_physics")
                        {
                            Ent:pushEntity(TE)
                            Model:pushString(TE:model())
                            Color:pushVector4(TE:getColor4())
                            if(TE:getMaterial():length()<2)
                            {
								                        Material:pushString(" ")
							                     }else
							                     {
								                        Material:pushString(TE:getMaterial())
							                     }
                            Pos:pushVector(Base:toLocal(TE:pos()))
                            Ang:pushAngle(Base:toLocal(TE:angles()))
                            TE:setColor(0,255,0,200)
                            print("] Selecting prop")
                            if(Debug)
                            {
                                hint(Ent:count():toString(),1)
                            }
                        }
                    }else
                    {
                        print("] You must select a base prop before you can select a prop.")
                    }
                }else
                {
                }
            }
            if(owner():keyReload())
            {
                if(TE!=Base)
                {
                    if(!TE:isPlayer()&!TE:isWeapon()&!TE:isNPC())
                    {
                        Base:setColor(255,255,255,255)
                        Base = TE
                        Base:setColor(255,0,0,200)
                        print("] Selecting base prop")
                    }
                    else
                    {
                        print("] You cannot select props that are players, weapons, or NPCs. You may select E2s, props, etc instead.")
                    }
                }else
                {
                    Base:setColor(255,255,255,255)
                    Base = noentity()
                    print("] Deselecting base prop")
                }
            }
        }
    }
}else
{
    #Readiness... fuck
    if(Int<=Ent:count())
    {
        print("] Preparing entity "+Int+"/"+Ent:count())
        if(Int==1)
        {
            ModelStr = ModelStr+"\""+Model[Int,string]+"\""
            ColorStr = ColorStr+"vec4("+round(Color[Int,vector4],4):toString():replace("[",""):replace("]","")+")"
            MatStr = MatStr+"\""+Material[Int,string]+"\""
            PosStr = PosStr+"vec("+round(Pos[Int,vector],4):toString():replace("[",""):replace("]","")+")"
            AngStr = AngStr+"ang("+round(Ang[Int,angle],4):toString():replace("[",""):replace("]","")+")"
        }else
        {
            ModelStr = ModelStr+",\""+Model[Int,string]+"\""
            ColorStr = ColorStr+","+"vec4("+round(Color[Int,vector4],4):toString():replace("[",""):replace("]","")+")"
            MatStr = MatStr+",\""+Material[Int,string]+"\""
            PosStr = PosStr+","+"vec("+round(Pos[Int,vector],4):toString():replace("[",""):replace("]","")+")"
            AngStr = AngStr+","+"ang("+round(Ang[Int,angle],4):toString():replace("[",""):replace("]","")+")"
        }
        Int++
    }else
    {
        
FinalText = "@name "+Name:replace(".txt","")+"
@model "+Base:model()+"
@persist [Mod Color Mat Pos Ang]:array Int Offset Ready

if(first())
{
    Int = 0
    Offset = 0
    runOnTick(1)
    Mod = array("+ModelStr+")
    Color = array("+ColorStr+")
    Mat = array("+MatStr+")
    Pos = array("+PosStr+")
    Ang = array("+AngStr+")
}

if(!Ready)
{
    while(holoCanCreate() & perf() & Int < Mod:count())
    {
        Int++
        local TempInt = Int+Offset
        holoCreate(TempInt,entity():toWorld(Pos[Int,vector]),vec(1),entity():toWorld(Ang[Int,angle]))
        holoColor(TempInt,Color[Int,vector4])
        holoModel(TempInt,Mod[Int,string])
        holoMaterial(TempInt,Mat[Int,string])
        holoParent(TempInt,entity())
    }
    Ready = Int >= Mod:count()
}"
        
        fileWrite(">e2shared/"+Name,FinalText)
        print("] Ready! Document saved as \""+Name+"\" in garrysmod/garrysmod/data/Expression2/e2shared")
        for(I=1,Ent:count())
        {
            ATE = Ent[I,entity]
            ATE:setColor(Color[I,vector4])
        }
        Base:setColor(255,255,255,255)
        selfDestruct()
    }
}

