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
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