This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ben657's profile picture

[wip/fun] Ant Farm [v0.2]

Started by ben657, 16 November 2012 - 04:58 AM
ben657 #1
Posted 16 November 2012 - 05:58 AM
Hello!

Recently I've been wanting to try out some basic game AI programming, but I've had no formal education in anything like that, so I decided what better way to give it a try than in computer craft?

Update:
The ants now prefer to dig tunnels, this gives a much nicer look to the program.
Once a certain percentage of the screen is tunnel, it will reset.

Pics:
Spoiler

Code:
Spoiler


--[[
Name: Ant Farm
Author: ben657
Description: Just a simple little ant farm to test out some
AI ideas, and basic objects.
--]]

--Global variables
local tArgs = {...}
local ants = {}
local width,height = term.getSize()
local map = {}
local speed = tonumber(tArgs[1]) or 0.1
local numDug = 0

--initialisation
for x = 1, width do
map[x] = {}
for y = 1, height do
map[x][y] = 1
end
end

--Ant Functions
function addAnt(antX,antY)
local ant = {
x = antX,
y = antY,
distance = 0,
directionX = 0,
directionY = 0,
--find a direction to go in
newDirection = function(self)
self.distance = math.random(1,5)
local upLeft = math.random(1,2)
local chance = 50
if upLeft == 1 then
if map[self.x][self.y+1] == 0 then chance = chance - 25 end
if map[self.x][self.y-1] == 0 then chance = chance + 25 end
local moveUp = (math.random(0,100) < chance)
if moveUp then
self.directionY = -1
else
self.directionY = 1
end
elseif upLeft == 2 then
if map[self.x+1][self.y] == 0 then chance = chance - 25 end
if map[self.x-1][self.y] == 0 then chance = chance + 25 end
local moveLeft = (math.random(0,100) < chance)
if moveLeft then
self.directionX = -1
else
self.directionX = 1
end
end
end,
--update function for ant to choose direction and move
update = function(self)
if self.distance <= 0 then
self.directionX = 0
self.directionY = 0
self:newDirection()
end
self.x = self.x + self.directionX
self.y = self.y + self.directionY
if self.x <= 2 then self.x = 2; self.distance = 0
elseif self.x >= width - 2 then self.x = width-2; self.distance = 0 end
if self.y <= 2 then self.y = 2; self.distance = 0
elseif self.y >= height - 2 then self.y = height - 2; self.distance = 0 end
self.distance = self.distance - 1
end,
--draw function for ant to be shown on the screen
draw = function(self)
term.setCursorPos(self.x,self.y)
term.setBackgroundColor(colors.yellow)
write(" ")
end
}
table.insert(ants,ant)
end

--Game Functions
function draw()
for x = 1, width do
for y = 1, height do
term.setCursorPos(x,y)
local tile = map[x][y]
if tile == 0 then
term.setBackgroundColor(colors.black)
elseif tile == 1 then
term.setBackgroundColor(colors.brown)
end
write(" ")
end
end
term.setCursorPos(1,1)
term.setTextColor(colors.red)
write("Virtual ant farm - by ben657")
for i=1,#ants do
local ant = ants[i]
ant:draw()
end
end

function update()
for i=1,#ants do
local ant = ants[i]
if map[ant.x][ant.y] ~= 0 then
map[ant.x][ant.y] = 0
numDug = numDug + 1
end
if numDug >= (width-3)*(height-3) then shell.run(shell.getRunningProgram()); exit() end
ant:update()
end
end

--Game loop
addAnt(2,2)
addAnt(4,4)
while true do
term.setBackgroundColor(colors.black)
term.clear()
update()
draw()
sleep(speed)
end

Or a pastebin link for an easier to read version: http://pastebin.com/kpimQphs

All it does at the moment, is create two ants, which will wander around their farm, and dig up trails as they go along.
But this is only the very first step, i'm planning on having a nest with breeding and food mechanics, as well as maybe some predators.

Also, I'm looking on suggestions on what would be a nice way to interact with the farm? My main idea is to have red stone inputs which will allow you to add ants and such manually.

I suppose this could also be a good example of using basic objects, as the ants are all the same, but run different code for each of them.

Next up:
Some form of breeding to introduce new ants
KaoS #2
Posted 16 November 2012 - 06:34 AM
nice man. looks quite good. I have always been fascinated with simulating randomness, introducing trends and seeing if there is no way to make it predictable or even intelligent when it is really just a bunch of random chances. I built a basic game code where there are friendly, hostile and neutral mobs, friendly mobs follow you (at a small distance so they do not get in the way much) and run at hostile mobs, trying to surround them. hostile and neutral mobs run around randomly until a hostile mob sees a friendly one or the player (I implemented sight ranges and inability to see through opaque items) in which case he runs at them, there are spawn zones and everything but I got over it before I implemented item drops, inventories and damage etc. you are welcome to take a look if you like. here is the code…

TL;DR great idea man, keep working at it
ben657 #3
Posted 16 November 2012 - 06:42 AM
-snip-

TL;DR great idea man, keep working at it

Thanks! Like you, I've always loved looking at turning randomness into some sort of pattern, and simple little AI's like this are great fun to make!
Your program sounds pretty cool, I'll definitely take a look through it :P/>/>
KaoS #4
Posted 16 November 2012 - 06:52 AM
you know what you should do? try algodoo. It is a physics simulation. disable the default downwards gravity and add quadratic gravity effects to all matter, paste a few circles in and remove all simulation borders…. when I say a few I mean millions :P/>/>… what you get:

a simulation of a 2d universe with multiple circles acting as molecules, bundling together into planets and smashing into other planets, stealing their matter etc. also add a fairly high restitution (bounciness) to the matter to stop everything from forming into 1 big planet too quickly. it is a very fascinating program… also try liquefying matter and making planets with water atmopheres, if you can get a high enough rotation on those water particles you can make them orbit etc… all very fascinating

if only such complex things could be programmed in CC :)/>/>

EDIT: it never fails to fascinate me how out of random movement a stable orbit can be formed… physics and maths my friend, amazing stuff
Edited on 16 November 2012 - 06:11 AM
Leo Verto #5
Posted 16 November 2012 - 07:41 AM
Nice program, going to try it out.
I like your idea of running this on a monitor and pretending it was a real ant farm.
ben657 #6
Posted 16 November 2012 - 07:51 AM
Nice program, going to try it out.
I like your idea of running this on a monitor and pretending it was a real ant farm.
Thanks, It's just me messing about really, but I thought it'd be nice for a screensaver when i'm not online on the server I play :P/>/>
Cranium #7
Posted 16 November 2012 - 08:39 AM
This is really neat. I like the basic concept behind it. It usually is the most simplistic things that make us happy.
anonimo182 #8
Posted 16 November 2012 - 09:41 AM
Nice program! Is just so random…
Shnupbups #9
Posted 16 November 2012 - 10:14 AM
And I was one of the first to know that they could dig now…. I was one of the guys on that server when you did it!
ben657 #10
Posted 16 November 2012 - 10:21 AM
This is really neat. I like the basic concept behind it. It usually is the most simplistic things that make us happy.
Nice program! Is just so random…
And I was one of the first to know that they could dig now…. I was one of the guys on that server when you did it!

Haha thanks all! :P/>/> by the way, shnupbups, if you see ikk on the server, could you ask him to delete my player file from the world? I can't log in due to me getting an error when i spawn :/
Shnupbups #11
Posted 16 November 2012 - 12:12 PM
Haha thanks all! :P/>/> by the way, shnupbups, if you see ikk on the server, could you ask him to delete my player file from the world? I can't log in due to me getting an error when i spawn :/

Lol, that was literally just posted after he left the server!
junithorn #12
Posted 17 November 2012 - 07:50 AM
Hello, this looks really cool and I'd love to try it out but I seem to be getting a "attempt to index ? (a nil value)"

at line 24

halp?
ben657 #13
Posted 17 November 2012 - 08:31 AM
Hello, this looks really cool and I'd love to try it out but I seem to be getting a "attempt to index ? (a nil value)"

at line 24

halp?

Hmm, very odd… When does it happen? Are you trying on a monitor or just a pc?

I'm not on my computer right now, but I'll have a proper look soon once I'm on.
junithorn #14
Posted 17 November 2012 - 08:39 AM
Using a monitor on top just like your picture, advanced computer, advanced monitors :)/>/>
ben657 #15
Posted 17 November 2012 - 12:10 PM
Using a monitor on top just like your picture, advanced computer, advanced monitors :)/>/>

Hmm, still works fine for me… Well I'm planning on doing some work on it tomorrow, so I'll make sure to have a good poke around that part of the code and see if I can come up with anything.

Is anyone else having a problem?
BigSHinyToys #16
Posted 17 November 2012 - 12:18 PM
This could be a awesome screen saver. I have one set up in my world resets ever time a start the game interesting to watch.
This is a cool program.
ben657 #17
Posted 23 November 2012 - 09:30 PM
Updated! see update section.
Laserman34170 #18
Posted 26 November 2012 - 05:51 AM
Awesome, I never expected for something like this to be so simple to code. Great job.