some time ago, i was bored.(thats how most of my projects start)
I decided to make a simulation of how ants would gather food (meaning, how i think they would).
i made it like this:
- A nest in the middle which sends 'searchers' that search for food. (gray sphere)
- Searcher ants (red spheres)
- "Food" (orange spheres)
- Worker ants (gray spheres)
The nest sends out workers, they just randomly move around the nest, until they collide with food, or they decide to return.
If they collide with food, they return to the nest to get worker ants, and make them pick up the food, piece by piece. when a piece of food is completely gathered, the gray ants should stay at the nest, and the searcher should continue searching for new food.
Controls:
Controlling ants> nothing really, they control themselves
Moving the camera> the mouse.
Tweaking:
searchers=3 ` The number of searchers
candies=100 ` the amount of food
global antspeed=4 ` speed multiplier
Oh and, i noticed that some pc load this quite slow.. mine loads it under 5 sec but some people had it load a minute.. so be patient!
and have fun
Rem Project: theants
Rem Created: 5-9-2006 15:34:38
Rem ***** Main Source File *****
randomize timer()
global stuff
global debugme$
global debugme2$
global debugme3$
global debugme4$
global debugme5$
hide mouse
minfoodsize=1
searchers=3
candies=100
global antspeed=4
sync on
sync rate 100
`sync sleep 1
debugme$ ="No debugging text avaliable"
autocam off
global dim objects(200,10)
global dim mobs(200)
global dim objects$(200,2)
global dim movequeue(1000,5)
global dim objectdata(1000,10)
make object plain 1,10000,10000
color object 1,RGB(0,50,0)
set object collision off 1
xrotate object 1,270
position camera 0,0,10,0
makeobj("nest",0,0)
nestobj=placeobj("nest",0,0,0)
`color object nestobj,RGB(165,118,22)
`scale object nestobj,140,140,140
makeobj("candy",0,10)
nestid=getnest()
nx = object position x(nestid)
nz = object position z(nestid)
for x=1 to candies
repeat
fx=RND(4000)-2000
fz=RND(4000)-2000
until abs(fx-nx)>200 and abs(fz-nz)>200
object=placeobj("candy",fx,0,fz)
color object object,RGB(255,128,0)
next x
for x=1 to searchers
ant(0,0,0,0)
next x
color backdrop RGB(38,123,174)
do
position mouse 100,100
if mouseclick()=1 then move camera 3
if mouseclick()=2 then move camera -3
mousemy=mousemovey()
if abs(mousemy)>0
xrotate camera camera angle x()+mousemy
endif
mousemx=mousemovex()
if abs(mousemx)>0
yrotate camera camera angle y()+mousemx
endif
if inkey$()="q" then position camera camera position x(),camera position y()+1,camera position z()
if inkey$()="a" then position camera camera position x(),camera position y()-1,camera position z()
text 10,10,str$(camera position x())+" "+str$(camera position z())+" FPS: "+str$(screen fps())
text 10,20,str$(stuff)
text 10,30,debugme$
text 10,40,debugme2$
text 10,50,debugme3$
text 10,60,debugme4$
text 10,70,debugme5$
text 10,80,debugme6$
text 10,90,debugme7$
`control camera using arrowkeys 0,3,2
for currentobj=1 to 1000
if objectdata(currentobj,0)=255
set object collision on currentobj
thisant = currentobj
`repeat
`shuthim=0
`antcoll=object collision(thisant,collobj)
`if objectdata(antcoll,0)=255 then shuthim=1
`if shuthim=1 then set object collision off antcoll : oldcoll=antcoll
`until shuthim=0
`if oldcoll>0 then set object collision on oldcoll : oldcoll=0
antcoll=object collision(thisant,collobj)
ta=totalants()
debugme4$ = str$(ta)
myjob=objectdata(thisant,1)
if antcoll<>0 and objectdata(antcoll,0)<>255
objtype=mobs(antcoll)
sweetness = objects(objtype,0)
object$ = objects$(objtype,0)
debugme$ = "Ant " + str$(thisant) + " collided into " + object$ + "("+str$(objtype)+")"
if object$="nest"
if myjob=0 and (objectdata(thisant,4)<>0 or objectdata(thisant,5)<>0)
if objectdata(thisant,2)<>0
moveto(thisant,objectdata(thisant,4),objectdata(thisant,5),antspeed,0)
objectdata(thisant,2)=0
`bwait(ta*50)
cant=ant(objectdata(thisant,4),objectdata(thisant,5),1,ta*100)
objectdata(cant,4)=objectdata(thisant,4)
objectdata(cant,5)=objectdata(thisant,5)
endif
endif
if myjob=1
if objectdata(thisant,3) = 1
objectdata(thisant,3) = 0
moveto(thisant,objectdata(thisant,4),objectdata(thisant,5),antspeed,0)
endif
if objectdata(thisant,3) = 2
killobj(thisant)
endif
endif
endif
if sweetness>0
debugme6$ = "CollObj Scale: "+str$(objectdata(antcoll,0))
y=getnest()
if myjob=0
if objectdata(antcoll,0) = minfoodsize
killobj(antcoll)
objectdata(thisant,4)=0
objectdata(thisant,5)=0
else
moveto(thisant,object position x(y),object position z(y),antspeed,0)
weagree=1
for x=1 to 1000
if objectdata(x,7)=antcoll then weagree=0
next x
if weagree=1
objectdata(thisant,4)=object position x(antcoll)
objectdata(thisant,5)=object position z(antcoll)
objectdata(thisant,7)=antcoll
endif
endif
endif
if myjob = 1 and objectdata(thisant,3)=0
moveto(thisant,object position x(y),object position z(y),antspeed,0)
if objectdata(antcoll,0) = minfoodsize
objectdata(thisant,3) = 2
` killobj(antcoll)
else
objectdata(thisant,3) = 1
scale object antcoll,objectdata(antcoll,0),objectdata(antcoll,0),objectdata(antcoll,0)
dec objectdata(antcoll,0)
endif
endif
objectdata(thisant,2) = sweetness
x = 0 : y = 0
endif
endif
if object exist(currentobj)
set object collision off currentobj
imovin=0
for x=0 to 1000
if movequeue(x,0)=currentobj then imovin=1
next x
if myjob=1
if imovin=0 then y=getnest() : moveto(thisant,object position x(y),object position z(y),antspeed,0) : objectdata(thisant,3)=2
endif
if myjob=0
if imovin=0
if objectdata(thisant,6)=<30
movexto=(RND(1000)-500)
movezto=(RND(1000)-500)
moveto(thisant,object position x(thisant)+movexto,object position z(thisant)+movezto,antspeed,0)
objectdata(thisant,7)=(movexto+(objectdata(thisant,6)*objectdata(thisant,7)))/(objectdata(thisant,6)+1)
objectdata(thisant,8)=(movezto+(objectdata(thisant,6)*objectdata(thisant,8)))/(objectdata(thisant,6)+1)
inc objectdata(thisant,6)
debugme7$="Searcher rnd. move: "+str$(objectdata(thisant,6))+" AVG X:"+str$(objectdata(thisant,7))+" AVX Z:"+str$(objectdata(thisant,8))
else
objectdata(thisant,6)=0
y=getnest()
moveto(thisant,object position x(y),object position z(y),antspeed,0)
randomize timer()
endif
endif
endif
endif
endif
next currentobj
moveit()
sync
loop
function killobj(ant)
for x=0 to 1000
if movequeue(x,0)=ant then clearqueue(x,100)
next x
for x=0 to 10
objectdata(ant,x)=0
next x
mobs(ant)=0
delete object ant
endfunction
function totalants()
for x=1 to 1000
if objectdata(x,0)=255 then inc total
next x
endfunction total
function getnest()
repeat : inc x : until objects$(x,0) = "nest"
repeat : inc y : until mobs(y) = x
endfunction y
function bwait(time)
starttime=timer()
debugme5$="Waiting "+str$(time)
repeat
inc z
text 10,10,str$(camera position x())+" "+str$(camera position z())
text 10,20,str$(stuff)
text 10,30,debugme$
text 10,40,debugme2$
text 10,50,debugme3$
text 10,60,debugme4$
text 10,70,debugme5$
control camera using arrowkeys 0,3,2
moveit()
sync
`until timer()-starttime=>time
until z=time
debugme5$="Waited "+str$(time)
endfunction
function moveit()
for x=0 to 1000
if movequeue(x,0)<>0
if movequeue(x,4)=0
move object movequeue(x,0),movequeue(x,2)
inc movequeue(x,3)
if movequeue(x,3) = movequeue(x,1) then clearqueue(x,0)
else
dec movequeue(x,4)
endif
endif
next x
endfunction
function clearqueue(x,param)
for y = 0 to 5
if y<>4-param
movequeue(x,y) = 0
endif
next y
endfunction
function ant(xx,zz,param,delay)
ant=freeobj()
make object sphere ant,10
if param=0 then color object ant,RGB(100,0,0)
if param=1 then color object ant,RGB(0,0,0)
objectdata(ant,1)=param
objectdata(ant,0)=255
repeat : inc x : until objects$(x,0) = "nest"
repeat : inc y : until mobs(y) = x
position object ant,object position x(y),0,object position z(y)
if x<>0 or y<>0
if (xx<>0 or zz<>0)
moveto(ant,xx,zz,antspeed,delay)
endif
set object collision off ant
endif
endfunction ant
function moveto(obj,x,z,speed,delay)
point object obj,x,0,z
for y=0 to 1000
if movequeue(y,0)=obj then clearqueue(y,0)
next y
y=0
repeat : inc y : until movequeue(y,0) = 0
movequeue(y,0) = obj
steps = sqrt((object position x(obj)-x)^2+(object position z(obj)-z)^2)/speed
movequeue(y,1) = steps
movequeue(y,2) = speed
inc movequeue(y,4),delay/antspeed
if objectdata(obj,1)=0 then type$="Searcher"
if objectdata(obj,1)=1 then type$="Gatherer"
debugme2$ = "Ant " + str$(obj) + "("+type$+")" + " moved to " + str$(x)+" "+str$(z)+" Speed:"+str$(speed)
debugme3$ = "Moves w/ Steps: "+str$(steps)+" Queue No: "+str$(y)+" Delay: "+str$(delay)+"lps"
endfunction
function placeobj(name$,x,y,z)
for i=1 to 200
if lower$(objects$(i,0))=lower$(name$) then obj=i
next i
if obj=0 then exitfunction
inc stuff
xobj=freeobj()
make object sphere xobj,100
`load object objects$(obj,0),xobj
position object xobj,x,y,z
mobs(xobj) = obj
objectdata(xobj,0) = 100
endfunction xobj
function makeobj(name$,model,sweetness)
repeat : inc x : until objects$(x,0)=""
objects$(x,0)=lower$(name$)
objects$(x,1)=model$
objects(x,0)=sweetness
endfunction
function freeobj()
repeat : inc x : until object exist(x)=0
endfunction x