From NPC combat to path following and all AI related tasks.

Steering Behaviours in Lua

Steering Behaviours in Lua

Postby Shando » 14 Mar 2015, 02:48

Hi All,

Over the past week or so (when I've had some spare time) I've been trying to create some basic Steering Behaviours in Lua. So far, I have Seek, Pursue and Arrive working, and hopefully in the next few days I'll have Evade, Flee & Wander. After that, I'm going to try to implement some basic Obstacle Avoidance.

To use these behaviours, it's simply a case of setting a few Variables in "onInit", and then calling the required Behaviour:

Code: Select all
function onInit ( objID )
   ai_obj = objID;
   rayID = physics.createRay ( );
   
   AI_NONE = true;

   ai_movtForce = 5.0;
   ai_maxForce = 10.0;
   ai_mass = 75.0;
   ai_arrivalDistance = 20.0;
   ai_arrivalStop = 5.0;

   ai_pursueObj = entity.getIDFromTag ( "mZirax" );
   ai_pursueWeight = 0.9;

   aiPursue ( );
   aiArrival ( );
end


As you can see above, the Behaviours can have "weights" attached to them for when you have multiple Behaviours at the same time (i.e. "pursuing" one entity, while "evading" another).

For the technically minded, the code I've used is based on the Steering Behaviours from Panda3D (modified BSD Licence) as that was the best C++ version I could find.

Once I've completed these Steering Behaviours, I have plans to implement a few other AI bits & pieces in Lua (mostly from the book "Learning Game AI Programming with Lua" by David Young), such as:

Behaviour/Decision Trees
Blackboard
FSMs
Knowledge Based Reasoning

and maybe:

Neural Networks
Genetic Algorithms

As you can probably tell, I'm a bit of an "AI nerd" :mrgreen:

Obviously, all these functions will be relatively slow in Lua, but they can easily be converted to C++ later ;)

Regards

Shando

PS: I'll provide my code once I've got Flee, Evade and Wander working
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby ant0N » 14 Mar 2015, 05:30

looking forward to this!!! :)
Sorry for my English. :)
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: Steering Behaviours in Lua

Postby SolarPortal » 14 Mar 2015, 12:06

nice, this should be interesting to see your take on it all :)

Note: On Steering behaviors. Have a look at the "entity.inverseTransform()" to get the amount of steer you need to point at another object.
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby StarFire » 14 Mar 2015, 14:48

Looking forward to seeing your approach to the AI ;)
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby Shando » 19 Mar 2015, 06:03

Hi All,

Well, I think that my code is now in a state where it can be donated to the Skyline Community 8-)

I have made a (very) quick video that demonstrates "Seek", "Follow" and "Obstacle Avoidance":



There are 3 files that I have put together (the links are to my own OneDrive account):

1) the main code (AI Steering Code.lua) http://1drv.ms/1Cy3pw1
2) comments on how to use the main code (AI Steering Code - Comments on Usage.txt) http://1drv.ms/1EtlB54
3) some example onInit functions (AI Steering Code - example init functions.txt) http://1drv.ms/1Cy3X4X

At the moment, the full AI code needs to be added to every entity that needs to use it, but this will change with the next update, when we will be able to "include" a lua file in a script.

The AI uses some of Skyline's physics functions, so all entities will need to have Rigid-Body Physics or the AI code will not work.

If you have any questions, or problems with implementing, let me know and I'll try to help.

Shando

PS: to turn on the "feeler" rays as shown in the video, just uncomment "physics.rayDebugLines" in the "aiObstacleAvoid" function.
PPS: the code is not commented, but hopefully should be relatively easy to follow.
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby SolarPortal » 19 Mar 2015, 12:36

woah, thats alot of code lol :P
nice one m8, will have to have a play at some point :)
have you thought about making it an AI pack on the store, as it will certainly boost the content.
Will get the include to everyone asap. Remember though that skyline event functions like onInit, onUpdate etc.. cannot be used in the included file as they will not be called. :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby Shando » 19 Mar 2015, 12:45

Yeah, it is a lot of code :mrgreen: but I thought I'd try to make it as comprehensive as possible :D

With regards to making a pack for the store, it might be better to wait until the "include" is available, because then all the User will have to do is set up the relevant data in the onInit of the entity, add the relevant AI update code to the onUpdate, and add the include??

Shando

PS: The code that I based this on is Copyright (c) 2008, Carnegie Mellon University, under this modified BSD Licence:

Copyright (c) 2008, Carnegie Mellon University.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Carnegie Mellon University nor the names of
other contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(This is the Modified BSD License. See also
http://www.opensource.org/licenses/bsd-license.php )
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby SolarPortal » 19 Mar 2015, 13:23

great plan, fair license :)

Again nice work, its great to see new stuff made with skyline that is quite comprehensive. :)
Looking forward to seeing some games you and other users are going to make with this new AI code.
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby StarFire » 19 Mar 2015, 14:32

Nice one Shando :D
Going to have a play with the ai later ;)
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby Shando » 23 Mar 2015, 04:33

Hi All,

I've just been looking at splitting my code out using the new "sky.include" command:

New include command in lua, allows you to split your code up into more readable chunks using the "sky.include(string filename)" method.


After a couple of tests, it looks like we can't name the incoming file? For example I would like to do this:

Code: Select all
aiMod = sky.include ( "ai_steer.lua" );

aiMod.ai_obj = objID;


but it looks like we can only use the functions/variables as named in the "included" file??

Code: Select all
sky.include ( "ai_steer.lua" );

ai_obj = objID;


Does this mean that we have to be really careful with our Global Variable and Function naming in the "included" file?

Thanks

Shando

PS: forgot to mention that the new "include" works great btw :mrgreen:
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby SolarPortal » 23 Mar 2015, 10:17

i didn't know it was possible to name the file when entering and use it like in your example.
Even loadfile() i thought just gave you the ability of executing the function using "()" instead of accessing through "."

I will look into this at some point :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby Shando » 23 Mar 2015, 14:19

Hi SP,

Yes, it's a good way to rename a file that might be long, or strangely named :ugeek:

This quote is from: http://lua-users.org/wiki/ModulesTutorial

Another nice thing is that since they're ordinary tables stored in a variable, modules can be named arbitrarily. Say we think that "mymodule" is too long to type every time we want to use a function from it:

> m = require "mymodule"
> m.foo()
Hello Module!


Regards

Shando
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Steering Behaviours in Lua

Postby SolarPortal » 23 Mar 2015, 14:32

i don't think its possible from the c++ side as there is no data to return. Unless there is a way of grabbing the last results from the state etc... I think this is going to need some research to solve.

Possibly just make a baseclass and instantiate at the top of your script or inside the lua to do the same sort of thing :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer


Return to AI

Who is online

Users browsing this forum: No registered users and 0 guests