@name Hulla-Colony[By Danus]
@inputs 
@outputs Stone Spare 
@persist H:table PE:array Vector:array Mine:entity IsMining:array Chosen Counter
@trigger
@model models/props_wasteland/controlroom_filecabinet001a.mdl
interval(100)
if(first())
{
    #cell 1 - Hulla dolls
    #cell 2 is Mining array
    #Vecotr - Mine pos
    H:pushArray(PE)
    Stone = 50
    Spare = 1
    Counter = 3
    Breed = 0
    Chosen = -1
    H:pushArray(IsMining)
    PE:pushEntity(propSpawn("models/props_lab/huladoll.mdl",entity():pos()+vec(entity():boxSize():x(),0,-14),0))  IsMining:pushNumber(0)
    Vector[1,vector] = entity():pos()+vec(0,0,-14) #Home
    Mine = noentity()
    timer("immagrent",25000)
    
    function void buildMine(H:table,Index)
    {
        if(Vector[2,vector] == vec(0,0,0))
        {
            Vector[2,vector] = entity():pos()+randvec(-255,255):setZ(-14) #Mine
        }
        
        H[1,array][Index,entity]:setAng((Vector[2,vector]-H[1,array][Index,entity]:pos()):toAngle())
        
        if(H[1,array][Index,entity]:pos():distance(Vector[2,vector]) > 15)
        {
            H[1,array][Index,entity]:setPos(H[1,array][Index,entity]:pos()+H[1,array][Index,entity]:forward()*2)
        }
        else
        {
           Stone -= 30
           Mine = propSpawn("models/props_c17/gravestone004a.mdl",Vector[2,vector]+vec(0,0,14),(Vector[2,vector]-H[1,array][Index,entity]:pos()):toAngle(),1)
        }
    }
    
    function void createHulla(A:array,B:array)
    {
        A:pushEntity(propSpawn("models/props_lab/huladoll.mdl",entity():pos()+vec(entity():boxSize():x(),0,-14),0))      B:pushNumber(0)
    }
        
    function void mine(H:table,Index)
    { 
        if(H[1,array][Index,entity]:pos():distance(Vector[2,vector]) > 15 & H[2,array][Index,number] < 5)
        {
            H[1,array][Index,entity]:setAng((Vector[2,vector]-H[1,array][Index,entity]:pos()):toAngle())
            H[1,array][Index,entity]:setPos(H[1,array][Index,entity]:pos()+H[1,array][Index,entity]:forward()*2)
        }
        elseif(H[1,array][Index,entity]:pos():distance(Vector[2,vector]) < 15 & H[2,array][Index,number] < 5)
        { 
            holoCreate(Index)
            holoModel(Index,"icosphere")
            holoMaterial(Index,"models/props_foliage/tree_deciduous_01a_trunk")
            holoPos(Index,H[1,array][Index,entity]:pos()+vec(0,0,H[1,array][Index,entity]:boxSize():z()))
            holoParent(Index,H[1,array][Index,entity])
            
            if(changed(realtime()) & H[2,array][Index,number] < 5)
            {
                H[2,array][Index,number] = H[2,array][Index,number] + 1
                holoScale(Index,vec(H[2,array][Index,number]/15,H[2,array][Index,number]/15,H[2,array][Index,number]/15))
            }
        }
        
        if(H[1,array][Index,entity]:pos():distance(Vector[1,vector]) > 15 & H[2,array][Index,number] == 5)
        {
            H[1,array][Index,entity]:setAng((Vector[1,vector]-H[1,array][Index,entity]:pos()):toAngle())
            H[1,array][Index,entity]:setPos(H[1,array][Index,entity]:pos()+H[1,array][Index,entity]:forward()*2)
        }
        elseif(H[1,array][Index,entity]:pos():distance(Vector[1,vector]) < 15 & H[2,array][Index,number] == 5)
        {
            Stone+=1
            holoDelete(Index)
            H[2,array][Index,number] = 0
        }    
    } 
    function void buildHouse(H:table,Index)
    {
        if(Vector[Counter,vector] == vec(0,0,0))
        {
            Vector[Counter,vector] = entity():pos()+randvec(-255,255):setZ(-14)
        }
        
        if(H[1,array][Index,entity]:pos():distance(Vector[Counter,vector]) > 15)
        {
            H[1,array][Index,entity]:setAng((Vector[Counter,vector]-H[1,array][Index,entity]:pos()):toAngle())
            H[1,array][Index,entity]:setPos(H[1,array][Index,entity]:pos()+H[1,array][Index,entity]:forward()*2)
        }
        else
        {
            if(Chosen != -1)
            {
                Chosen = -1
                Spare = 2
                Stone -= 10
                Counter++
                propSpawn("models/props_wasteland/controlroom_filecabinet002a.mdl",PE[Index,entity]:pos()+vec(0,0,35),1)
            }
        }
    }
    
}



for(I = 1, PE:count())
{
    noCollideAll(PE[I,entity],1)
    
    if(Spare == 0 & Stone >= 20)
    {
        if(Chosen == -1)
        {
            Chosen = randint(1,PE:count())
        }
            
        buildHouse(H,Chosen)
    }
    
    
       
    if(Mine == noentity())
    {
        buildMine(H,I)
    }
    else
    {
        if(Chosen != I)
        {
            mine(H,I)
        }
    }
}

if(clk("immagrent"))
{
    if(Spare > 0)
    {
        createHulla(PE,IsMining)
        Spare--
    }
    timer("immagrent",25000)
}
