Guest User

Codea artifical life

a guest
Nov 4th, 2011
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.64 KB | None | 0 0
  1. -- alife
  2. -- inspiration http://herselfsai.com/files/Bugs.java (pls keep)
  3. -- @ipad41001
  4.  
  5. function setup()
  6.     --iparameter("size",1,10)
  7.     fw = 10             --food width
  8.     fe = fw / math.pi   --food edge
  9.     fa = 1000           --food amount
  10.     fp = .5            --food percent
  11.     mf = math.floor((HEIGHT*WIDTH)/(math.pow(fw,2))*fp/100) --max food
  12.     bw = 20             --bug width
  13.     be = bw / math.pi   --bug edge
  14.     bp = 1              --bug percent
  15.     mb = math.floor((HEIGHT*WIDTH)/(math.pow(bw,2))*bp/100) --max bugs
  16.     ma = 1000           --max age
  17.     ml = 1000
  18.     --yes, it's a table of food because a object of food isn't quite as funny
  19.     foods = {}   
  20.     for i=1,mf do
  21.          x=math.random() * WIDTH
  22.          y=math.random() * HEIGHT
  23.          foods[i] =  {x=x,y=y}       
  24.     end  
  25.     --known bug table, unknown bugs are not handled
  26.     bugs = {}
  27.     for i=1,mb do
  28.          x=math.random() * WIDTH
  29.          y=math.random() * HEIGHT
  30.          bugs[i] = {x=x,y=y,path ={},life=ml,age=0}
  31.          x=(math.random() * 2) - 1
  32.          y=(math.random() * 2) - 1
  33.          bugs[i].path[1] = {x=x,y=y}
  34.          --print(x,y)
  35.          
  36.     end  
  37.     --print(bugs[1]:len())
  38. end
  39.  
  40. function draw()
  41.     background(0, 0, 0, 0)
  42.     ellipseMode(CENTER)
  43.     stroke(0, 255, 0, 135)
  44.     strokeWidth(3)
  45.     noFill()
  46.     noSmooth()
  47.     --ellipse(100,100,20,20)
  48.     for i,v in ipairs(foods) do
  49.         ellipse(foods[i].x,foods[i].y,fw,fw)  
  50.     end
  51.     stroke(255, 136, 0, 255)
  52.     --ellipse(200,200,80,80)
  53.     for i,v in ipairs(bugs) do
  54.         ellipse(bugs[i].x,bugs[i].y,bw,bw)
  55.         --line(bugs[i].x-be,bugs[i].y-be,bugs[i].x-be-8,bugs[i].y-be-8)
  56.     end
  57.     stroke(255, 0, 0, 255)  
  58.     lineCapMode(ROUND)
  59.     --line(100+(20/3),100+(20/3),200-(80/3),200-(80/3))
  60.     for i,v in ipairs(bugs) do
  61.         for j,v in ipairs(foods) do
  62.             fv = vec2(foods[j].x,foods[j].y)
  63.             bv = vec2(bugs[i].x,bugs[i].y)
  64.             if fv:dist(bv) < (fw / math.pi) + (bw / math.pi) then
  65.                  table.remove(foods,j)
  66.                  bugs[i].life = bugs[i].life + (fa * mb / table.maxn(bugs))
  67.                 --print(fa * table.maxn(bugs) / mb, table.maxn(bugs), mb, table.maxn(bugs)/mb)
  68.             end
  69.         end
  70.         bugs[i].life = bugs[i].life - 1
  71.          bugs[i].age = bugs[i].age + 1
  72.         if (bugs[i].life < 0) or (bugs[i].age > ma * 2) then
  73.             table.remove(bugs,i)
  74.         elseif bugs[i].age < ma then
  75.             bugs[i].x = bugs[i].x + (bugs[i].path[1].x )--* (ma - bugs[i].age) / ma)
  76.             bugs[i].y = bugs[i].y + (bugs[i].path[1].y )--* (ma - bugs[i].age) / ma)
  77.             if bugs[i].x > WIDTH + bw then
  78.                 bugs[i].x = -bw
  79.             end    
  80.             if bugs[i].y > HEIGHT + bw then
  81.                 bugs[i].y = -bw
  82.             end
  83.             if bugs[i].x < -bw then
  84.                 bugs[i].x = WIDTH + bw
  85.             end    
  86.             if bugs[i].y < -bw then
  87.                 bugs[i].y = HEIGHT + bw
  88.             end
  89.            
  90.             if bugs[i].life > ml then
  91.                  nb = table.maxn(bugs) + 1 --new bug
  92.                  nl = bugs[i].life / 2     --new life
  93.                  -- make new bug
  94.                  x=bugs[i].x
  95.                  y=bugs[i].y
  96.                  bugs[nb] = {x=x,y=y,path ={},life=nl,age=0}
  97.                  x=bugs[i].path[1].x * (1+(((math.random() * 2) - 1) / 5))
  98.                  y=bugs[i].path[1].y * (1+(((math.random() * 2) - 1) / 5))
  99.                  bugs[nb].path[1] = {x=x,y=y}
  100.                  -- make old bug new
  101.                  bugs[i].life = nl
  102.                  bugs[i].age = 0
  103.                  bugs[i].path[1].x=bugs[i].path[1].x * (1+(((math.random() * 2) - 1) / 5))
  104.                  bugs[i].path[1].y=bugs[i].path[1].y * (1+(((math.random() * 2) - 1) / 5))       
  105.             end 
  106.         end  
  107.     end
  108.     nf = table.maxn(foods) -- number of foods
  109.     bf = math.floor((mf-nf)*math.random())*(mb / table.maxn(bugs))
  110.     for i=1,bf do
  111.          x=math.random() * WIDTH
  112.          y=math.random() * HEIGHT
  113.          foods[nf+i] =  {x=x,y=y}       
  114.     end  
  115. end
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment