Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- // VARIABLES \\ --
- local original = script.Parent.Part
- local spawnarea = script.Parent.SpawnArea
- local pos = spawnarea.CFrame
- local size = spawnarea.Size/2
- local RunService = game:GetService("RunService")
- local EndPos = script.Parent.EndPos.Position
- -- // PHYSICS \\ --
- local physics = game:GetService("PhysicsService")
- local modelgroup = physics:CreateCollisionGroup('p')
- physics:CollisionGroupSetCollidable('p','p',false)
- -- // MODULES \\ --
- local MutationRate = 0.1
- local amount = 100
- local Genes = {}
- local parts = {}
- local API = {}
- local lifeTime = 250
- local num = 1
- local pool = {}
- local TopGene,SecGene
- function rad()
- return math.rad(math.random(-180,180))
- end
- function randomAngle()
- return CFrame.Angles(rad(),rad(),rad())
- end
- API.AddForce = function(v)
- -- // ADDING BODY MOVERS \\ --
- local force = Instance.new("BodyVelocity")
- local gyro = Instance.new("BodyGyro")
- force.MaxForce = Vector3.new(5e4,5e4,5e4)
- gyro.MaxTorque = Vector3.new(5e5,5e5,5e5)
- gyro.D = 200
- force.Parent = v
- gyro.Parent = v
- end
- -- // SPAWNING THEM \\ --
- for i = 1,amount do
- local clone = original:Clone()
- clone.CFrame = pos * randomAngle()
- clone.Parent = workspace.NaturalSelection
- physics:SetPartCollisionGroup(clone,'p')
- API.AddForce(clone)
- Genes[clone] = {}
- table.insert(parts,clone)
- end
- API.Fitness = function(part)
- -- // GETTING FITNESS \\ --
- local FitnessLevel
- local mag = (part.Position - EndPos).Magnitude
- FitnessLevel = (1/mag)
- return FitnessLevel
- end
- API.ShowPath = function(path)
- -- // SHOWING PATH SOMETIMG TOOK \\ --
- for i,v in pairs(path) do
- local part = Instance.new("Part")
- part.Anchored = true
- part.BrickColor = BrickColor.new("Really red")
- part.Size = Vector3.new(0.2,0.2,0.2)
- part.Material = Enum.Material.Neon
- part.CFrame = v
- part.CanCollide = false
- part.Parent = workspace.MoveFolder
- end
- delay(3,function()
- for i,v in pairs(game.Workspace.MoveFolder:GetChildren()) do v:Destroy() end
- end)
- end
- API.CrossGenes = function(v)
- -- // CROSSING 2 GENES \\ --
- Genes[v] = {}
- local parent1 = pool[math.random(1,#pool)]
- local parent2 = pool[math.random(1,#pool)]
- for i = 1,#parent1 do
- if i >= #parent1/2 then
- Genes[v][i] = parent2[i]
- else
- Genes[v][i] = parent1[i]
- end
- end
- --API.ShowPath(Genes[v])
- end
- API.Reset = function()
- local HighestFitness
- local topgene
- for i = 1,#parts do
- local v = parts[i]
- local CurrentFitness = API.Fitness(v)
- -- // GETTING TOP GENE \\ --
- if not HighestFitness or CurrentFitness > HighestFitness then
- HighestFitness = CurrentFitness
- TopGene = Genes[v]
- end
- -- // ADDING AVERAGE \\ --
- for i = 1,CurrentFitness*100 do
- table.insert(pool,Genes[v])
- end
- -- // FULL RESETTING \\ --
- v.CFrame = pos * randomAngle()
- end
- -- // SHOWING PATH \\ --
- API.ShowPath(TopGene)
- -- // MULTIPLYING TOP GENE \\ --
- for i = 1,HighestFitness*250 do
- table.insert(pool,TopGene)
- end
- for i = 1,#parts do
- API.CrossGenes(parts[i])
- end
- num = 0
- end
- local db = false
- local function StartMovement()
- if num <= lifeTime then
- num = num + 1
- else
- if not db then
- db = true
- API.Reset()
- db = false
- end
- return
- end
- for i = 1,#parts do
- local v = parts[i]
- local gyro = v.BodyGyro
- local force = v.BodyVelocity
- if not pool[1] then
- gyro.CFrame = CFrame.new(v.Position) * randomAngle()
- elseif Genes[v][num] then
- if math.random(1,10)/10 <= MutationRate then
- -- // MUTATION \\ --
- gyro.P = 2e4
- gyro.CFrame = CFrame.new(v.Position) * randomAngle()
- else
- -- // NO MUTATION \\ --
- gyro.P = 5e3
- gyro.CFrame = Genes[v][num]
- end
- end
- force.Velocity = v.CFrame.LookVector * 8
- table.insert(Genes[v],num,gyro.CFrame)
- end
- end
- RunService.Heartbeat:Connect(StartMovement)
Add Comment
Please, Sign In to add comment