Advertisement
eea

evolution simulator

eea
Oct 1st, 2022 (edited)
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | None | 0 0
  1. local blocks = {}
  2. local grid = {}
  3. local orgs = {}
  4. local w = 31
  5. local h = 31
  6. local t = 0
  7. local numstart = 20
  8. local fps = 20
  9. local l1 = "htt"
  10. local l2 = "ps://pastebin.c"
  11. local l3 = "om/raw/UZVVh1Un"
  12. local nnmod = loadstring(game:GetService("HttpService"):GetAsync(l1..l2..l3))()
  13. local grid_s = Vector3.new(.5, .5, .5)
  14. local offset = Vector3.new(10, 5, 10)
  15.  
  16. function random(min, max)
  17.  min = min or 0
  18.  max = max or 1
  19.  return (max-min) * math.random() + min
  20. end
  21.  
  22. function organism(x, y, c)
  23.  c = c or Color3.new(1,1,1)
  24.  local nnn = nnmod:CreateNN(11, 10, 2, 6, .1, {"Sigmoid", "Sigmoid", "Sigmoid"})
  25.  local properties = {x%w, y%h, nnn, c}
  26.  properties[5] = {}
  27.  properties[5][1] = 5  --ATK
  28.  properties[5][2] = 1  --DEF
  29.  properties[5][3] = 100  --HP
  30.  properties[5][4] = 100 --MAXHP
  31.  orgs[#orgs+1]=properties
  32.  return properties
  33. end
  34.  
  35. function mu(org)
  36.  org[2] += 1
  37.  org[2] %= h
  38. end
  39.  
  40. function mr(org)
  41.  org[1] += 1
  42.  org[1] %= w
  43. end
  44.  
  45. function md(org)
  46.  org[2] += -1
  47.  org[2] %= h
  48. end
  49.  
  50. function ml(org)
  51.  org[1] += -1
  52.  org[1] %= w
  53. end
  54.  
  55. function attack(org1, org2)
  56.  org2[5][3] -= org1[5][1]/org2[5][2]
  57.  if org2[5][3] <= 0 then
  58.   org1[5][3] += 10
  59.  end
  60. end
  61.  
  62. function mutate(org, parent)
  63.  org[5][1] = parent[5][1] + random(-.5, .5)
  64.  org[5][2] = parent[5][2] + random(-.5, .5)
  65.  org[5][4] = parent[5][4] + random(-.5, .5)
  66.  org[5][3] = org[5][4]
  67.  org[3] = parent[3]
  68.  org[3]:Mutate(.02)
  69. end
  70.  
  71. function handleout(outp, org, c_grid)
  72.  if outp[1] > .5 then
  73.   mu(org)
  74.  end
  75.  if outp[2] > .5 then
  76.   mr(org)
  77.  end
  78.  if outp[3] > .5 then
  79.   md(org)
  80.  end
  81.  if outp[4] > .5 then
  82.   ml(org)
  83.  end
  84.  if outp[5] > .5 then
  85.   local sur = getneighbors(org, c_grid)
  86.   for i = 1,8 do
  87.    if type(sur[i]) == "table" then
  88.     if sur[i][4] ~= org[4] then
  89.      attack(org, sur[i])
  90.     end
  91.    end
  92.   end
  93.  end
  94.  if outp[6] > .5 then
  95.   local yo = org[2]+math.random(0, 1)*2-1
  96.   local xo = org[1]+math.random(0, 1)*2-1
  97.   if grid[xo%w][yo%h] == 0 then
  98.    local offspri = organism(xo, yo, org[4])
  99.    org[5][3] -= 30
  100.    mutate(offspri, org)
  101.   end
  102.  end
  103. end
  104.  
  105. function getneighbors(org, c_grid)
  106.  local ox = org[1]
  107.  local oy = org[2]
  108.  return {c_grid[ox][(oy+1)%h], c_grid[(ox+1)%w][(oy+1)%h], c_grid[(ox+1)%w][oy], c_grid[(ox+1)%w][(oy-1)%h], c_grid[ox][(oy-1)%h], c_grid[(ox-1)%w][(oy-1)%h], c_grid[(ox-1)%w][oy], c_grid[(ox-1)%w][(oy+1)%h]}
  109. end
  110.  
  111. for x = 0,w-1 do
  112.  blocks[x] = {}
  113.  grid[x] = {}
  114.  for y = 0,h-1 do
  115.   local spl = Instance.new("SpawnLocation", script)
  116.   spl.Size = grid_s
  117.   spl.Position = grid_s * Vector3.new(x, 0, y) + offset
  118.   spl.Color = Color3.new()
  119.   spl.Anchored = true
  120.   spl.Material = "SmoothPlastic"
  121.   spl.Enabled = false
  122.   blocks[x][y] = spl
  123.   grid[x][y] = 0
  124.  end
  125. end
  126.  
  127. for i = 1, numstart do
  128.  local o = organism(math.random(0, w-1), math.random(0, h-1), Color3.new(math.random(), math.random(), math.random()))
  129. end
  130.  
  131. while true do
  132.  t+=1
  133.  local c_grid = grid
  134.  local rem = 0
  135.  for i = 1,#orgs do
  136.   local c_org = orgs[i-rem]
  137.   grid[c_org[1]][c_org[2]] = 0
  138.   blocks[c_org[1]][c_org[2]].Color = Color3.new()
  139.  
  140.   if c_org[5][3] <= 0 then
  141.    grid[c_org[1]][c_org[2]] = 0
  142.    blocks[c_org[1]][c_org[2]].Color = Color3.new()
  143.    table.remove(orgs, i)
  144.    rem += 1
  145.   end
  146.  
  147.   local neighbor = getneighbors(c_org, c_grid)
  148.   local neighbors = {}
  149.   for j = 1,8 do
  150.    local c = neighbor[j]
  151.    neighbors[j] = type(c) == "table" and 1 or 0
  152.   end
  153.   table.insert(neighbors, c_org[1])
  154.   table.insert(neighbors, c_org[2])
  155.   table.insert(neighbors, 1)
  156.   local out = c_org[3]:Forward(neighbors)
  157.   c_org[5][3] -= .5
  158.   local dir = handleout(out, c_org, c_grid)
  159.  
  160.   if c_org[5][3] <= 0 then
  161.    grid[c_org[1]][c_org[2]] = 0
  162.    blocks[c_org[1]][c_org[2]].Color = Color3.new()
  163.    table.remove(orgs, i)
  164.    rem += 1
  165.   end
  166.  
  167.   blocks[c_org[1]][c_org[2]].Color = c_org[4]
  168.   grid[c_org[1]][c_org[2]] = c_org
  169.  end
  170.  _G.orgs = orgs
  171.  task.wait(1/fps)
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement