Enemy AI help needed

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
AgentEnder
Prole
Posts: 5
Joined: Wed Mar 05, 2014 7:30 am

Enemy AI help needed

Post by AgentEnder »

ok, admittedly i am new to both lua and love2d but am used to other languages and am fluent in JavaScript which is somewhat similar to LUA in terminology and vocabulary. My newest project is mainly for an exercise to learn more and accomplish something i will enjoy. This is a top down game where the player goes stage to stage shooting zombies that are in front of it. Development of the game has went well so far except i have ran into an issue.

Code: Select all

local zombie1Img = love.graphics.newImage("textures/zombie.png")
local zombie2Img = love.graphics.newImage("textures/zombie2.png")
local zombie3Img = love.graphics.newImage("textures/zombie3.png")
local zombie4Img = love.graphics.newImage("textures/zombie4.png")
local zombieImg = zombie1Img

local function collide ()
	Z1integer = math.random(0,100)
	Z1integer = Z1integer/25
	Z1direction = math.floor(Z1integer + 0.5)
	if Z1direction == 0 then
		Z1integer = math.random(0,100)
		Z1integer = Z1integer/25
		Z1direction = math.floor(Z1integer + 0.5)
	end
	zombie.collideTimer = 0
end

function zombie.spawn(x, y, a, scale)
	if not x then
		x = 0
	end
	
	if not y then 
		y = 0
	end
	
	if not a then
		a = 0
	end
	if not scale then
		scale = 2
	end
	if a < 0 then a = 360 + a end
	if a < 45 and a > 315 then 
		a = 0 
		zombieImg = zombie1Img
	end
	if a < 135 and a > 45 then
		a = 90
		zombieImg = zombie2Img
	end
	if a < 225 and a >135  then
		a = 180
		zombieImg = zombie3Img
	end
	if a < 315 and a > 225 then
		a = 270 
		zombieImg = zombie4Img
	end
	zombie.x = x
	zombie.y = y
	zombie.a = a
	zombie.scale = scale
	zombie.i = 1
	collide()
	zombie.collideTimer = 0
	Z1intTimer = 0
end

zombie.update(dt)
	Z1intTimer = Z1intTimer + math.ceil(dt)
	if Z1direction == 1 then
		zombieImg = zombie1Img
		if zombie.y <= 32 then
			collide()
		end
		zombie.y = zombie.y - 96*dt
		zombie.collideTimer=zombie.collideTimer + 2.5*dt
		if zombie.collideTimer >= 20 then
			collide()
		end
	end
	if Z1direction ==2 then
		zombieImg = zombie2Img
		zombie.x = zombie.x + 96*dt
		if zombie.x >= 1200-32 then
			collide()
		end
		zombie.collideTimer=zombie.collideTimer + 2.5*dt
		if zombie.collideTimer >= 20 then
			collide()
		end
	end
	if Z1direction == 3 then
		zombie.y = zombie.y + 96*dt
		if zombie.y >= 1200-32 then
			collide()
		end
		zombieImg = zombie3Img
		zombie.collideTimer=zombie.collideTimer + 2.5*dt
		if zombie.collideTimer >= 20 then
			collide()
		end
	end
	if Z1direction == 4 then
		zombie.x = zombie.x - 96*dt
		if zombie.x  <= 32 then
			collide()
		end
		zombieImg = zombie4Img
		zombie.collideTimer=zombie.collideTimer + 2.5*dt
		if zombie.collideTimer >= 20 then
			collide()
		end
	end
end
there are 4 zombie images, each facing a different way as i couldn't seem to get the rotation system to work ._.

The issue is with my code math.random generates the direction for the zombie to path, however each time you run the game the zombie follows the same path as the last and never a randomly generated one. ATM i am not implementing a full ai as that will come later when i have the zombie chasing the player. before it discovers the player though it will go in random pathways searching for the player. Here is my issue. I cannot get random pathing to work.

If anybody can help or even just link me to a tutorial i would be INCREDIBLY grateful
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Enemy AI help needed

Post by Plu »

The reason they follow the same path might be because you are using math.random, and löve by default doesn't seed that.

Try using love.math.random() instead?

https://love2d.org/wiki/love.math.random
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Enemy AI help needed

Post by micha »

If you want to generate a random integer between 1 and 4, then better use

Code: Select all

love.math.random(1,4)
instead of the method you have with math.floor and so on.

Your code is a bit incomplete, so instead of correcting it, I rewrote it to give the same result. I hope that helps:

Code: Select all

function love.load()
  zombie = {x = 300, y = 300, angle = 0,speed = 50}
  timer = 1
end

function newDirection()
  -- This line allows arbitrary angles
  --  zombie.angle = love.math.random()*2*math.pi

  -- This line only allows up,down,left,right
  zombie.angle = love.math.random(0,3)*0.5*math.pi

  timer = 1
end

function moveZombie(dt)
  zombie.x = zombie.x + math.cos(zombie.angle)*zombie.speed*dt
  zombie.y = zombie.y + math.sin(zombie.angle)*zombie.speed*dt  
end

function love.update(dt)
  timer = timer - dt
  if timer < 0 then
    newDirection()
  end
  moveZombie(dt)
end

function love.draw()
  love.graphics.circle('fill',zombie.x,zombie.y,10)
end
In the function newDirection I put two different possible lines for generating a new angle. One allows arbitrary angles and the other one allows four directions only.
User avatar
AgentEnder
Prole
Posts: 5
Joined: Wed Mar 05, 2014 7:30 am

Re: Enemy AI help needed

Post by AgentEnder »

Plu wrote:The reason they follow the same path might be because you are using math.random, and löve by default doesn't seed that.

Try using love.math.random() instead?

https://love2d.org/wiki/love.math.random
Thank you so much Plu, i was use to JS and other languages and didnt realize love didnt seed it.
micha wrote:If you want to generate a random integer between 1 and 4, then better use

Code: Select all

love.math.random(1,4)
instead of the method you have with math.floor and so on.

Your code is a bit incomplete, so instead of correcting it, I rewrote it to give the same result. I hope that helps:

Code: Select all

function love.load()
  zombie = {x = 300, y = 300, angle = 0,speed = 50}
  timer = 1
end

function newDirection()
  -- This line allows arbitrary angles
  --  zombie.angle = love.math.random()*2*math.pi

  -- This line only allows up,down,left,right
  zombie.angle = love.math.random(0,3)*0.5*math.pi

  timer = 1
end

function moveZombie(dt)
  zombie.x = zombie.x + math.cos(zombie.angle)*zombie.speed*dt
  zombie.y = zombie.y + math.sin(zombie.angle)*zombie.speed*dt  
end

function love.update(dt)
  timer = timer - dt
  if timer < 0 then
    newDirection()
  end
  moveZombie(dt)
end

function love.draw()
  love.graphics.circle('fill',zombie.x,zombie.y,10)
end
In the function newDirection I put two different possible lines for generating a new angle. One allows arbitrary angles and the other one allows four directions only.
Micha thank you so so much for the help. i knew i could use angles to only have to use one player img but i couldnt figure out how to use the angles. Each time it would give a very strange angle result and i think it was me not multiplying by 2pi, thank you all for the help :) do you mind if i use this code?
Last edited by AgentEnder on Wed Mar 05, 2014 8:14 pm, edited 1 time in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Enemy AI help needed

Post by micha »

Yes, keep in mind that angles are measured in radians. 180 degrees correspond to an angle of pi.
User avatar
AgentEnder
Prole
Posts: 5
Joined: Wed Mar 05, 2014 7:30 am

Re: Enemy AI help needed

Post by AgentEnder »

micha wrote:Yes, keep in mind that angles are measured in radians. 180 degrees correspond to an angle of pi.
I honestly had no idea that they were measured in radians that was what was throwing me off thank you so much
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests