Advertisement
eea

evolution simulator

eea
Oct 1st, 2022 (edited)
1,210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.76 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 = 2
  8. local fps = 1
  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, 3, 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(false, false, .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 = {}
  135.  for i = 1,#orgs do
  136.   local c_org = orgs[i]
  137.   grid[c_org[1]][c_org[2]] = 0
  138.   blocks[c_org[1]][c_org[2]].Color = Color3.new()
  139.  
  140.   local neighbor = getneighbors(c_org, c_grid)
  141.   local neighbors = {}
  142.   for j = 1,8 do
  143.    local c = neighbor[j]
  144.    neighbors[j] = type(c) == "table" and 1 or 0
  145.   end
  146.   table.insert(neighbors, c_org[1])
  147.   table.insert(neighbors, c_org[2])
  148.   table.insert(neighbors, 1)
  149.   local out = c_org[3]:Forward(neighbors)
  150.   c_org[5][3] -= .5
  151.   local dir = handleout(out, c_org, c_grid)
  152.  
  153.   if c_org[5][3] <= 0 then
  154.    grid[c_org[1]][c_org[2]] = 0
  155.    blocks[c_org[1]][c_org[2]].Color = Color3.new()
  156.    --table.remove(orgs, i)
  157.    rem[#rem+1] = i
  158.   end
  159.  
  160.   blocks[c_org[1]][c_org[2]].Color = c_org[4]
  161.   grid[c_org[1]][c_org[2]] = c_org
  162.  end
  163.  for i = 1,#rem do
  164.      table.remove(orgs, rem[i])
  165.  end
  166.  _G.orgs = orgs
  167.  task.wait(1/fps)
  168. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement