getting started with basic AI [scripting, events, newbie]

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
xjermx
Prole
Posts: 10
Joined: Tue May 08, 2012 7:44 pm

getting started with basic AI [scripting, events, newbie]

Post by xjermx »

Soooooooo, this seems incredibly elementary, and yet I'm just not sure how to get started.

I've used the great tutorial about a Gridlocked Player. But lets say I want that 'player' to do something. Perhaps to sometimes move a space in a random direction.

Is there a tutorial or some info on this? Would I just create some lua logic to pick a direction, and then cause it to redraw? How would I call this code?
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: getting started with basic AI [scripting, events, newbie

Post by veethree »

I have an idea on how to pull this off, It's all about generating a bunch of random numbers (math.random()).

I'd generate a random number between for example 0-100 every second or so, Then if the number is say less than 20 i'd call the function to move the player in a random direction.

In that function i'd generate another random number between 0-4 then if that number is 0 i'd move the player up, if it's 1 i'd move the player down etc.

To move the player in the gridlock example you'd just have to change the act_x and act_y variables i think. If you'd want the player to go up you'd subtract 32 from act_y etc.

If i get bored later i might put this whole thing into code for you, But for now i have other stuff to do.
User avatar
xjermx
Prole
Posts: 10
Joined: Tue May 08, 2012 7:44 pm

Re: getting started with basic AI [scripting, events, newbie

Post by xjermx »

Yes - from a logic standpoint I'm on the same page with you.

I just don't know how to trigger the code.

I assume I'd create a function,

function wander()
-- generate random number
-- if its in a certain range, do something
-- determine direction
-- etc
end

But how do I call this? How do I call it every X seconds?
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: getting started with basic AI [scripting, events, newbie

Post by veethree »

xjermx wrote:Yes - from a logic standpoint I'm on the same page with you.

I just don't know how to trigger the code.

I assume I'd create a function,

function wander()
-- generate random number
-- if its in a certain range, do something
-- determine direction
-- etc
end

But how do I call this? How do I call it every X seconds?
to call a function you just do "wander()" wherever you wanna call it

To call it every X seconds you do something like this.

In love.load you create a variable, 2 preferably. One would be 0, the other however many seconds you want to call the function

something like this:
interval = 3
tick = 0

Then in love.update you do something like this

tick = tick + dt
if tick > interval then
--Here you call the function ,generate random number and all that stuff
tick = 0
end

That would call the code roughly every 3 seconds. And you have to make sure to include the set tick back to 0 so it doesn't just keep going up.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: getting started with basic AI [scripting, events, newbie

Post by Roland_Yonaba »

Right.
Or just use cron.
User avatar
xjermx
Prole
Posts: 10
Joined: Tue May 08, 2012 7:44 pm

Re: getting started with basic AI [scripting, events, newbie

Post by xjermx »

Great success!

So the square wanders randomly around, and every once in awhile complains about being lost.

Code: Select all

-- tutorial.gridlock
-- from https://love2d.org/wiki/Tutorial:Gridlocked_Player


function love.load()

	math.randomseed(os.time())
	
	interval = 2
	tick = 0

    player = {
        grid_x = 256,
        grid_y = 256,
        act_x = 200,
        act_y = 200,
		rand_number = 0,
        speed = 10,
		text = ""
    }
    map = {
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
        { 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
        { 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
    }
end

function love.update(dt)
    player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
    player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
	
	tick = tick + dt
	if tick > interval then
		--Here you call the function ,generate random number and all that stuff
		wander()
		tick = 0
	end
end

function love.draw()
	love.graphics.print("Our random number is (" .. player.rand_number .. ")", 500, 500)
	
	if player.text ~= "" then
		love.graphics.print(player.text, 500, 550)
	end
	
    love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
    for y=1, #map do
        for x=1, #map[y] do
            if map[y][x] == 1 then
                love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
            end
        end
    end
end

function love.keypressed(key)
    if key == "up" then
        if testMap(0, -1) then
            player.grid_y = player.grid_y - 32
        end
    elseif key == "down" then
        if testMap(0, 1) then
            player.grid_y = player.grid_y + 32
        end
    elseif key == "left" then
        if testMap(-1, 0) then
            player.grid_x = player.grid_x - 32
        end
    elseif key == "right" then
        if testMap(1, 0) then
            player.grid_x = player.grid_x + 32
        end
    end
end

function testMap(x, y)
    if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
        return false
    end
    return true
end

function wander()

player.rand_number = math.random(100)

if player.rand_number < 99 then
	-- do something
	rand2 = math.random(4)
	
	if rand2 == 1 then
		if testMap(0, -1) then
            player.grid_y = player.grid_y - 32
        end
	elseif rand2 == 2 then
		if testMap(0, 1) then
            player.grid_y = player.grid_y + 32
        end
	elseif rand2 == 3 then
		if testMap(-1, 0) then
            player.grid_x = player.grid_x - 32
        end
	elseif rand2 == 4 then
		if testMap(1, 0) then
            player.grid_x = player.grid_x + 32
        end
	end
	
	if player.rand_number < 20 then
		player.text = "Man, I am SO lost!"
	else player.text = ""
	end
	
end


end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests