Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- alife
- -- inspiration http://herselfsai.com/files/Bugs.java (pls keep)
- -- @ipad41001
- function setup()
- --iparameter("size",1,10)
- fw = 10 --food width
- fe = fw / math.pi --food edge
- fa = 1000 --food amount
- fp = .5 --food percent
- mf = math.floor((HEIGHT*WIDTH)/(math.pow(fw,2))*fp/100) --max food
- bw = 20 --bug width
- be = bw / math.pi --bug edge
- bp = 1 --bug percent
- mb = math.floor((HEIGHT*WIDTH)/(math.pow(bw,2))*bp/100) --max bugs
- ma = 1000 --max age
- ml = 1000
- --yes, it's a table of food because a object of food isn't quite as funny
- foods = {}
- for i=1,mf do
- x=math.random() * WIDTH
- y=math.random() * HEIGHT
- foods[i] = {x=x,y=y}
- end
- --known bug table, unknown bugs are not handled
- bugs = {}
- for i=1,mb do
- x=math.random() * WIDTH
- y=math.random() * HEIGHT
- bugs[i] = {x=x,y=y,path ={},life=ml,age=0}
- x=(math.random() * 2) - 1
- y=(math.random() * 2) - 1
- bugs[i].path[1] = {x=x,y=y}
- --print(x,y)
- end
- --print(bugs[1]:len())
- end
- function draw()
- background(0, 0, 0, 0)
- ellipseMode(CENTER)
- stroke(0, 255, 0, 135)
- strokeWidth(3)
- noFill()
- noSmooth()
- --ellipse(100,100,20,20)
- for i,v in ipairs(foods) do
- ellipse(foods[i].x,foods[i].y,fw,fw)
- end
- stroke(255, 136, 0, 255)
- --ellipse(200,200,80,80)
- for i,v in ipairs(bugs) do
- ellipse(bugs[i].x,bugs[i].y,bw,bw)
- --line(bugs[i].x-be,bugs[i].y-be,bugs[i].x-be-8,bugs[i].y-be-8)
- end
- stroke(255, 0, 0, 255)
- lineCapMode(ROUND)
- --line(100+(20/3),100+(20/3),200-(80/3),200-(80/3))
- for i,v in ipairs(bugs) do
- for j,v in ipairs(foods) do
- fv = vec2(foods[j].x,foods[j].y)
- bv = vec2(bugs[i].x,bugs[i].y)
- if fv:dist(bv) < (fw / math.pi) + (bw / math.pi) then
- table.remove(foods,j)
- bugs[i].life = bugs[i].life + (fa * mb / table.maxn(bugs))
- --print(fa * table.maxn(bugs) / mb, table.maxn(bugs), mb, table.maxn(bugs)/mb)
- end
- end
- bugs[i].life = bugs[i].life - 1
- bugs[i].age = bugs[i].age + 1
- if (bugs[i].life < 0) or (bugs[i].age > ma * 2) then
- table.remove(bugs,i)
- elseif bugs[i].age < ma then
- bugs[i].x = bugs[i].x + (bugs[i].path[1].x )--* (ma - bugs[i].age) / ma)
- bugs[i].y = bugs[i].y + (bugs[i].path[1].y )--* (ma - bugs[i].age) / ma)
- if bugs[i].x > WIDTH + bw then
- bugs[i].x = -bw
- end
- if bugs[i].y > HEIGHT + bw then
- bugs[i].y = -bw
- end
- if bugs[i].x < -bw then
- bugs[i].x = WIDTH + bw
- end
- if bugs[i].y < -bw then
- bugs[i].y = HEIGHT + bw
- end
- if bugs[i].life > ml then
- nb = table.maxn(bugs) + 1 --new bug
- nl = bugs[i].life / 2 --new life
- -- make new bug
- x=bugs[i].x
- y=bugs[i].y
- bugs[nb] = {x=x,y=y,path ={},life=nl,age=0}
- x=bugs[i].path[1].x * (1+(((math.random() * 2) - 1) / 5))
- y=bugs[i].path[1].y * (1+(((math.random() * 2) - 1) / 5))
- bugs[nb].path[1] = {x=x,y=y}
- -- make old bug new
- bugs[i].life = nl
- bugs[i].age = 0
- bugs[i].path[1].x=bugs[i].path[1].x * (1+(((math.random() * 2) - 1) / 5))
- bugs[i].path[1].y=bugs[i].path[1].y * (1+(((math.random() * 2) - 1) / 5))
- end
- end
- end
- nf = table.maxn(foods) -- number of foods
- bf = math.floor((mf-nf)*math.random())*(mb / table.maxn(bugs))
- for i=1,bf do
- x=math.random() * WIDTH
- y=math.random() * HEIGHT
- foods[nf+i] = {x=x,y=y}
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment