@name Creaturevolution
@inputs 
@outputs ARC
@persist GEN E:entity MDC T
@persist [AR]:table [MODELS TMPL]:array
@persist CRS MINRAN RAN SPEED JITTER ADAPT UP NP
@model models/props_c17/FurnitureFridge001a.mdl

interval(50)

LT = T
T = systime()
DT = T-LT

if(first()){
    
    E = entity()
    
    #SETTINGS
    
    CRS = 20    # specimen count
    MINRAN = 30     # facing wall minimum dist or die
    RAN = 150       # forward view range
    SPEED = 600      # movement speed
    JITTER = 1    # initial jitter to values
    ADAPT = 1.155   # how fast they adapt
    
    ##########################################Uke_OKT_2015
    
    GEN = 1
    
    DT = 0
    
    UP = 40
    
    MODELS = array(
        "models/Humans/Group01/Male_01.mdl",
        "models/Humans/Group01/Male_02.mdl",
        "models/Humans/Group01/Male_03.mdl",
        "models/Humans/Group01/Male_04.mdl",
        "models/Humans/Group01/Male_05.mdl",
        "models/Humans/Group01/Male_06.mdl",
        "models/Humans/Group01/Male_07.mdl",
        "models/Humans/Group01/Male_08.mdl",
        "models/Humans/Group01/Male_09.mdl"
    )
    
    MDC = MODELS:count()
#                # 0 0 0 0
    TMPL = array(1,1,1,1,1)
    
    #[function number array:getSpawnAngle(){return This[1,number]}
    function number array:getViewDist(){return This[2,number]}  #rangerdist
    function number array:getAgility(){return This[3,number]} #turnspeed
    function number array:getReactStrenghth(){return This[4,number]} #vision based turn exponent
    function number array:getTurnRange(){return This[5,number]}]#
    
    function createcr(ID:number,TMP:array,RDMNS){
        local TMP2 = array()
        for(I = 1,TMP:count()){TMP2:pushNumber(TMP[I,number]+random(-RDMNS,RDMNS))}
        AR[ID:toString(),array] = TMP2
        
        H=holoCreate(ID,E:pos(),vec(1),ang(0,0#[TMP2[1,number]*360]# ,0))
        #H["anim",number] = holoAnimLength(ID)
        holoModel(ID,MODELS[randint(1,MDC),string])
        
        COL = vec(clamp(255-30*TMP2[2,number],0,255),clamp(30*TMP2[4,number],0,255),clamp(30*TMP2[5,number],0,255))
        holoColor(ID,COL,255)
        #ifdef H:setText("")
            H:setText(ID:toString(),COL,255,255,1,vec(0,0,UP))
        #endif
        X = holoCreate(ID+CRS,H:pos()+vec(0,0,UP),vec(0.2),H:angles())
        X:setTrails(20,20,10,"trails/laser",COL,255,5,1)
        holoColor(ID+CRS,COL)
        holoParent(ID+CRS,ID)
        holoMaterial(ID+CRS,"debug/debugportals")
        #entity:setTrails(number startSize,number endSize,number length,string material,vector color,number alpha,number attachmentID,number additive)
    }
    
    
    for(I = 1,CRS){
        createcr(I,TMPL,JITTER)
    }
    
    function killcr(ID){
        holoColor(ID,vec4(255,0,0,70))
        AR:removeArray(ID:toString())
    }
    
    function calccr(ID:number){
        V = AR[ID:toString(),array]
        H = holoEntity(ID)
        POS = H:pos()
        RP = POS+vec(0,0,UP)
        RDK = rangerOffsetHull(MINRAN,RP,H:forward(),vec(0,0,UP/10))
        if(RDK:hit()){killcr(ID)}
        else{
            UP1=UP+40
            RDD = rangerOffsetHull(UP1+DT*600,POS+vec(0,0,UP1),vec(0,0,-1),vec(MINRAN+10,MINRAN+10,0))
            PZ = RDD:pos():z()
            
            RD = rangerOffsetHull(RAN*V[2,number],RP,H:forward(),vec(0,0,10))
            
            holoAng(ID,H:angles()+ang(0,(4*random(-V[5,number],V[5,number])*V[3,number]*((1-RD:fraction())*5)^V[4,number])*DT,0))
            holoPos(ID,H:toWorld(vec(SPEED*DT,0,0)):setZ(PZ))
        }
    }
    rangerPersist(1)
    rangerFilter(E)
    
}

NP = numPlayers()
if($NP){rangerFilter(players())}

KEYS = AR:keys()
ARC = AR:count()
for(I = 1,AR:count()){
    local SID = KEYS[I,string]
    local ID = SID:toNumber()
    print()
    calccr(ID)
    if(AR:count() == 1){
        print(ID+" WON: "+AR:exists(SID))
        TMPL = AR[AR:keys()[1,string],array]
        killcr(ID)
        GEN++
        
        for(I = 1,CRS){
            createcr(I,TMPL,((1+ADAPT)^(I-1))-1)
        }
    }
}
TXT="Evolution\nGeneration: "+GEN+"\nSpecimen: "+AR:count()+"/"+CRS
#ifdef E:setText(TXT)
    E:setText(TXT)
#endif
setName(TXT)
