Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Basic AI Functions - Simple but a good start to AI

Author
Message
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 5th Nov 2006 18:06 Edited at: 25th Jan 2007 02:17
Theres quite a few indi developers in the community that dont know where to start when it comes to AI. It makes sense, how do you make a computer do what a human would do? It seems difficult, but the basis behind AI is pretty straight forward. If event A happens, do event B. If im shot, run in a random direction. If im close to an enemy, follow the enemy. If Im very close to an enemy, shoot the enemy. Etc.

To ease the process a bit I made some basic functions to handle the main things you'll find AI "bots" doing. Those being;

- Traveling to a destination
- Turning to look at a destination
- Running from a destination

With the 3 functions you can expand them to do a lot of different things. Instead of travelling to a static point in 3D space, you could travel to a player's old position, making the bot follow 2the player. You then wouldn't have to take into concideration collisions, as the player the bot is following would do so for the bot (ofcourse there are exceptions). You could use the evade function in unison with the travel function to set up a race track, making the bots travel towards the waypoints on the track, while "evading" or staying away from other players and the walls.

These aren't meant for ground breaking intelligence in AI, its just a starter kit for newcommers that are trying to figure out the logic behind how some versions of AI work. I might expand on it later, but for now the system is fairly small.

The functions;



Included in that library is 2 math functions for getting 2D/3D distances, as the AI functions use this. Make sure to use the setup math function before calling any of the math functions.

Overview of the commands:
Quote: "
AI_Travel(bot,x#,y#,z#,distance#,speed#)

o bot: the object you want to travel
o x#/y#/z#: the position you want the bot to travel to
o distance#: the distance the bot will stop at from the travel destination
o speed#: the speed of travel

AI_look(bot,x#,y#,z#,cycles,allowance#)

o bot: the object you want to look
o x#/y#/z#: the location you want the bot to look towards
o cycles: the amount of loops it should take the bot to finish the task
o allowance#: a "leeway" amount, its just a way of saying "if the bot is kind of close to looking directly at the position, it's good enough", as you'll rarely ever get the bot looking perfectly at a location since the angles are floats. Allowance is the range below and above the ending angle you decide is "ok".

AI_evade(bot,x#,y#,z#,distance#,speed#)
o bot: you get the idea.
o x#/y#/z#: the position the bot should run away from
o distance#: how close to the x#/y#/z# the bot will hold its ground until it decides to run.
o speed#: the speed the bot runs away at"


The math commands are pretty generic so Ill skip explaining them.

Demo Program;



Ill make up a more complex demo a bit later on showing some cool stuff you can get working (Im a bit surprised how something that simple can do quite a lot :/).

Anyways, hope it helps,
- RUC'


:: Check out the new FPS Tutorial's Progress at http://www.freewebs.com/ruccus
along with code, media, and more.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 5th Nov 2006 18:11
Good, now let's see some A*!

dj blackdragon3710
20
Years of Service
User Offline
Joined: 5th Nov 2004
Location: In LaLa land
Posted: 5th Nov 2006 23:27
Ansolutely stunning! This runs waaaaay better than my random waypoint checking system.... I think i'll use it!

<<<<<Used to be "djblackdragon" with being registered in January, 2003, no matter what it says on the left<<<<<
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 6th Nov 2006 08:33
what amazes me is people can produce AI with such little code. outstanding.

Terrorist Zero
18
Years of Service
User Offline
Joined: 29th Aug 2006
Location: Teh YouKai
Posted: 6th Nov 2006 22:28
You are a legend RUCCUS haha. I really do find this code very nice, and very helpful. I wish you luck, and look forwards to you creating more functions, and making more complex demos

Keep up the good work!

www.tornupgaming.com
The home of free games and game projects.
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 7th Nov 2006 22:24 Edited at: 12th Nov 2006 18:24
The demo's almost finished. If you've ever played Starwars: Republic Commando, Im going for that type of system. Where you select which team members you want, and then give orders to those team members.

Screenie;



In that shot, Im the medium-sized tank beside the big one (calling him lugger). I've ordered lugger to be at ease, so he'll keep an eye on me when Im roaming around, and if I stray too far he'll follow behind me to ensure my safety. At the same time, I've selected "mini" and "tech" to take a look at the big, random title object. They do so, making sure to avoid each other and to stop before they run into any polygons of their target. If I were to order them to go behind the tanks object, they'd do so as well, avoiding any collisions at the same time.

Im really liking the controls in the game , I might take it further and release an open-source complete game later on, everythings animated so it looks really cool.

Oh, and in the game, each tank serves a purpose. Each one can do certain things. Tech can use computers to open doors or disarm alarms, Lugger is equipped with a huge chaingun along with some of the best armour money can buy, but he's terribly slow. He'll hold his own in a battle. Mini on the other hand is quick and agile, he's not the strongest of the group, but his fast speed and his duel-machine guns mounted on his turret make him a force to reckon with. Watch out if mini decides to use lugger as cover.

And ofcourse the main character is well rounded, equipped with a machine gun and a rocket launcher. If you havent guessed, you play the leader of the group.

Im not sure if Ill be done by tonight, Ive got a good chunk of homework, but I wont be starting until after dinner which is in another few hours. If I dont get bored of programming in the next 10 minutes Ill ty to pump out a demo... of the demo .

Oh and thanks for all of the comments, Zoto, A* might be the most commonly used system for AI as of now, but it's popularity is lessening as time progresses and games demand larger more complex maps. Plus, this is for beginners, A* isnt the most beginner-oriented thing to take on.

- RUC'

Kohaku
20
Years of Service
User Offline
Joined: 3rd May 2004
Location: The not very United Kingdom
Posted: 7th Nov 2006 23:15 Edited at: 7th Nov 2006 23:18
Hey RUCCUS! Long time no see. I remember when you were making SHADE - still have a copy here somewhere.

That code's pretty neat. Reminds me of an AI like system that I made before leaving - not sure if I post it or not. It can basically move around, patrolling the area - taking other objects into consideration so it won't go through a wall - looking for the player object. If it spots you then it follows you to wherever it saw you last.

It's rather messy code. I should probably update it with my new look on how to code stuff.





I didn't just let it sit in a folder though. I have a script example in AGE now that uses this same system. I thought it might be a good test for the engine.




I then used it again to test entities. Works a dream, even on 3D terrains full of trees and buildings.



But now I'm just getting lost in AGE.


That screen looks sweet RUCCUS and I can't wait to play the demo.


You are not alone.
slicer 4 ever
18
Years of Service
User Offline
Joined: 11th Jul 2006
Location: currently: planet gamma3295
Posted: 12th Nov 2006 10:49
outstanding ruccus looks amazing i hope you finish the game it looks sweet i like the tank designes did you design them?

my smiles love me =-)=-)=-)(-=(-=(-=
biblerama
18
Years of Service
User Offline
Joined: 10th Nov 2006
Location: Georgia
Posted: 12th Nov 2006 15:57


I do have a question with this. I am a C# developer and I am considering buying the DGDK.Net to help me create my games. Taking a look at your code, I was wondering if you have made use of the DGDK.Net, and what kind of translation would be done to preform the same thing with the DGDK.Net?

I am basically trying to find out really what DGDK.Net gives me that I would not need to develop on my own?

- Software with a smile!
www.biblerama.com
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 12th Nov 2006 18:07
I dont have DGSDK, but from what I understand, DGSDK is basically a C++ compiler, that has all of darkbasic pro's commands as plugins. So essentially you can call any DBP command. IIRC, the syntax is dbCommandName, so in DBP if you wanted to move a 3D object you'd use MOVE OBJECT, in DGSDK, I think it would be dbMoveObject.

I noticed you posted in the DGSDK forum, its better just to wait for responses there, the users of DGSDK will know more about how it works than I.

Ankillito
17
Years of Service
User Offline
Joined: 10th Dec 2006
Location: Litigious California
Posted: 17th Jan 2007 06:01
RUCCUS - Great AI, now I'm greedy for more. What logic would I use to get the object to not hit a wall/asteroid/enemy/etc?

"There will always be evil, for, without evil, the good shall lose their virtue."
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 17th Jan 2007 18:44
I would basically use raycasting from the player to a point directly infront of him, and if that ray hits a wall you make him turn away.

RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 17th Jan 2007 21:14
That or use the evade function. In the parameters, for position use the asteroid's position, and for the radius, dont use the asteroid's radius, instead use something bigger, like the radius times 2 for example. You could then use the look function to make the bots turn smoothly as they evade the astroids.


Just keep in mind, I think there may be a few bugs with one of the functions, I cant remember what the problem was, it may have been with the look function messing up when used on multiple axises.

Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 17th Jan 2007 23:14
Can't wait for the tank demo. From what you've written it sounds like they'll be very smart. I haven't touched AI yet, but with a collection of functions like you have it doesn't seems hard at all.

The functions certainly make it easy, but I can't see them being very useful without some core AI that calls them. Something like a finite state machine with AI modes.

- Idle mode (every minute or so pick a random location and walk to it)
- Follow mode (follow an entity until close enough)
- Patrol mode (walk between a set of points)
- Attack mode (attack enemies)

Quote: "Gimme teh code!"

Thank you for not flooding the forums with posts like this.
Ankillito
17
Years of Service
User Offline
Joined: 10th Dec 2006
Location: Litigious California
Posted: 17th Jan 2007 23:41
Quote: "That or use the evade function. "


Hmmm... It seems to work!

Another question, if I may: What is A*? I'm assuming it's not a certain three-letter word spelled wrong.

"There will always be evil, for, without evil, the good shall lose their virtue."
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 18th Jan 2007 00:28
Quote: "- Idle mode (every minute or so pick a random location and walk to it)
- Follow mode (follow an entity until close enough)
- Patrol mode (walk between a set of points)
- Attack mode (attack enemies)"


All of that can be pretty much done with those 3 core commands and a little tweakge,

- Following: Use the AI_travel function sending them towards the player's position you want them to follow. The command takes a distance param to tell the bot when they can stop at a certain distance from their target, so just have them stop far enough that they dont run into your player.

- Patrol: Store a few positions in an array, cycle through the array using the AI_travel command. When a bot reaches a position, call the AI look command to make it look at the next position, then make it move there by having it walk towards that new position.

- Attack: Check if a player is close to an enemy using the distance function provided, if so call the travel command, once the AI has completed the travel and is close enough, damage the player.

I left out Idle as thats pretty much the same as Patrol.

Quote: "Another question, if I may: What is A*? I'm assuming it's not a certain three-letter word spelled wrong.
"


Do a search for "A Star" on the TGC forums, or on google, you'll get plenty of info.

- RUC'

Ankillito
17
Years of Service
User Offline
Joined: 10th Dec 2006
Location: Litigious California
Posted: 18th Jan 2007 00:53
Thanks.

"There will always be evil, for, without evil, the good shall lose their virtue."
Vampiric
18
Years of Service
User Offline
Joined: 30th Oct 2006
Location:
Posted: 20th Jan 2007 11:35
Cool nice AI, I'll be using this for the basic AI in my rpg, looks like those dumb rats might actually fight back for once

Computer says n00bed
Ankillito
17
Years of Service
User Offline
Joined: 10th Dec 2006
Location: Litigious California
Posted: 22nd Jan 2007 05:32 Edited at: 22nd Jan 2007 06:15
I modified RUCCUS's AI to point an object at a target(4), but away from danger(1) in a 4:1 ratio. It seems to work well, but the object avoids "asteroids" behind it too. Haven't had time to think about that yet.

AI object (green) goes from 0,0,0 to 1000,1000,1000 without hitting any asteroids. A red asteroid is placed directly in it's path and a green box is made to mark the destination.



Edit: As I walked away i realized I should check field of veiw...

"There will always be evil, for, without evil, the good shall lose their virtue."
sneaky smith12
18
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 23rd Jan 2007 05:03
Awesome code there.
Maybe for my awesome game of destruction I'll have a sphere cashing the other one Mwhahaha...haha.
Seriously though, I love functions. I make functions for single dot's. (Almost, I try to make everything as portible as possible)

If at first you dont succeed, LOWER YOUR STANDARDS.
Darth Vader
19
Years of Service
User Offline
Joined: 10th May 2005
Location: Adelaide SA, I am the only DB user here!
Posted: 23rd Jan 2007 05:27
Great AI functions. There so easy to understand!
I hope you don't mind if I edit them a little so they work best for me?

Also do you have another email I can contact you on? I tried the one in your sig but it doesn't work!


Login to post a reply

Server time is: 2024-11-23 00:24:32
Your offset time is: 2024-11-23 00:24:32