@name Artificial Intelligence
@outputs Distance BreedGene:vector
@persist Counter AI Warning Gate:entity [HoloGene HoloScore]:array

if (first()) {
    runOnTick(1)
    Gate = entity()
    AI = 5 #Number of AI's, Around 75 it works, at 80 it reach tickquota.
    for (I=1,AI) {
        holoCreate(I)
        
        holoEntity(I):setTrails(10,1,10,"cable/redlaser",vec(255,255,255),255)
    }
}

Goal = round(Gate:pos(),3)

if (Counter == 2) {
    Counter = 0
    
    Distance = HoloScore[1,number]
    Breed = 1
    for (I=2,AI) {
        Score = HoloScore[I,number]
        if (Score < Distance) {
            Distance = Score
            Breed = I
        }
    }
    
    BreedGene = round(HoloGene[Breed,vector],3)
    
    for (I=1,AI) {
        if (Breed != I) {
            HoloGene[I,vector] = BreedGene
        }
        
        Gene = HoloGene[I,vector]
        HoloGene[I,vector] = Gene+randvec(-Distance,Distance) #Change distance to a constant if you wish
        
		#Remove this if you dont like that each new generation start from the gate, it works just as well anyway
		#It is here to help you find 0,0,0 of map as a player.
        holoPos(I,Goal)
    }
    HoloGene[Breed,vector] = BreedGene
    
    if (!Distance & !Warning) {
        Str = "There AI is done. The target pos was "+Goal
        Str += " and the AI figured out it was "+BreedGene+"."
        print(Str)
        Warning = 1
    } elseif (Distance & Warning) {
        Warning = 0
    }
} else {
    for (I=1,AI) {
        Holo = holoEntity(I)
        Pos = Holo:pos()
        Dir = HoloGene[I,vector]
        holoPos(I,Pos+(Dir-Pos)/2)
        HoloScore[I,number]=Pos:distance(Goal)
    }
    Counter++
}
