# What's the best way to go about making an AI for a game

This question goes beyond simply Having an enemy that’s attracted to the players location and swipes it’s weapon when in range…im trying to make an enemy ai that behaves exactly like a player and not making the super human reflexes over power the ai…could anyone give me some pointers?

In the dungeon roller example there is a simple AI for the bugs.

For your own AI you could:
Give it a maximum speed and then it should move towards the player at that speed or less. You could use a* pathfinding to get around walls/obstacles.

look for @ignatz posts and blog. He has published a number of good things about AI.

Thank you…I’m writing a tron like game where the ai wants to stay alive as much as possible. Without hitting any boundaries

@Invad3rZIM, for that kind of situation you would want to use a path finding algorithm. Assuming you use a grid system (you probably should for this kind of thing) you could set a variable for each grid square to true if a boundaries has been placed there. Then you simply code your AI to look ahead to the grid squares coming up and to turn if a boundary has been placed there. This would make your AI never hit a boundary unless it is boxed in.

@JakAttak Mind if I pm you with my code?

I found I wanted the board to be vast so I used a physics chain, and gravityScale=0 balls for the first player and for the ai. I set boundaries and upon collision, I’ll make it so that if the player circle is part of the collision, ai will win…if ai is part of the collision, player will win. If both, tie. And I have a reasonably clever method of adding to the chain as to track the players past movements and simplifying the table history of values so it doesn’t crash

@Invad3rZIM, is there any specific reason you are using physics? if you don’t really need them it is best to avoid using them because they can really slow down your program.

I put together a little example of how you could do something similar to what I think you are trying to do without using physics. If you run it, you should see the ai (green) cleverly avoid all the boundaries (red squares) at the beginning, then start running along the edge of the board until it ends up being boxed in and crashes.

Here it is:

``````-- Tron Ex

displayMode(FULLSCREEN)
function setup()
squares = 20
grid = {}
for c = 1, squares do
grid[c] = {}
for r = 1, squares do
local x = WIDTH * (c / (squares + 1))
local y = HEIGHT * (r / (squares + 1))
grid[c][r] = { x = x, y = y, boundary = false }
end
end

grid[1][6].boundary = true
grid[6][5].boundary = true
grid[5][2].boundary = true
grid[2][3].boundary = true
grid[20][1].boundary = true
grid[19][2].boundary = true

gridSize = vec2(WIDTH / (squares + 1), HEIGHT / (squares + 1))

ai = { grid = vec2(1,1), dir = vec2(0, 1) }

tween.delay(0.1, moveAi)
end

function draw()
background(40, 40, 50)

strokeWidth(2)

for c = 1, #grid do
for r = 1, #grid[c] do
if grid[c][r].boundary then
fill(255,0,0)
elseif ai.grid.x == c and ai.grid.y == r then
fill(0,255,0)
else
fill(255)
end

rect(grid[c][r].x - gridSize.x / 2, grid[c][r].y - gridSize.y / 2, gridSize.x, gridSize.y)
end
end
end

function moveAi()
local n, next = grid[ai.grid.x + ai.dir.x]
if n ~= nil then next = n[ai.grid.y + ai.dir.y] end
if n == nil or next == nil or next.boundary == true then     -- next space is off grid or has a boundary
local rpos, lpos
-- Figure out which way to look right and left
if ai.dir == vec2(0, 1) then rpos = vec2(1, 0)
elseif ai.dir == vec2(1, 0) then rpos = vec2(0, -1)
elseif ai.dir == vec2(0, -1) then rpos = vec2(-1, 0)
elseif ai.dir == vec2(-1, 0) then rpos = vec2(0, 1) end
lpos = vec2(rpos.x * -1, rpos.y * -1)

local r, l, right, left = grid[ai.grid.x + rpos.x], grid[ai.grid.x + lpos.x]
if r ~= nil then right = r[ai.grid.y + rpos.y] end
if l~= nil then left = l[ai.grid.y + lpos.y] end

if r ~= nil and right ~= nil and right.boundary == false then   -- check if right is clear first
ai.dir = rpos    -- if it is, set direction to right
elseif l ~= nil and left ~= nil and left.boundary == false then   -- if right isnt clear, check if left is clear
ai.dir = lpos    -- if it is, set direction to left
else    -- if both right and left are blocked, you will crash because you cant go backwards
ai.dir = vec2(0,0)
displayMode(OVERLAY) print("CRASHED")
end
end

ai.grid = ai.grid + ai.dir  -- move in decided direction

tween.delay(0.1, moveAi)
end

``````

And yes, feel free to PM me your code