Help - Search - Members - Calendar
Full Version: Chhaaiinnssaaww!'s Ultimate Top Down Ai
Game Maker Community > Games > Game Maker Creations > Open Source
Pages: 1, 2, 3
Chhaaiinnssaaww! Get Some!
Chhaaiinnssaaww!'s
Ultimate Top Down AI

seeks cover ~ gun-fights ~ avoids death ~ kills like a pro

Hi there,

This .gm6 file contains two easy to implement and modify AI scripts for use in a Top Down Shooter game.
And you needn't be registered!

All you need to do is type in the code and change the arguments accordingly to suit your game. For example:

CODE
shooter_ai(enemy,block,bullet,e_bullet,3,3,random(10),1)


And that is all you need to put this AI in your game!

Please read the scripts for more information on changing its arguments instead of posting here. Thankyou

(Change the numbers in the script to slow down the AI in the example if you think it moves too fast.)

------------------------------------------------------------


shooter_ai(enemyid,coverid,enemybulletid,bulletid,speedatk,maxspeeddef,shootspd,registered)

The registered variable is whether you are registered or not and basically toggles image_angle on/off

This script makes an AI that:
  • Moves towards the enemy whilst keeping to available cover
  • Shoots at enemy while in cover with the aim of killing or surpressing them
  • When shot at, will attempt to avoid bullets by returning to cover
  • If caught in the open, will dash to cover, away from harm
  • Will advance on the enemy if he falls back from cover


What's great about this script is that if you stay in cover around the same area then you can have a gun fight with the AI by popping in and out of cover!

Uses:
  • Realistic soldiers
  • Boss fights
  • Team games (it can fight itself)


------------------------------------------------------------

beserker_ai(enemyid,coverid,maxspeed,registered)

This script makes an AI that:
  • Moves towards last place the enemy was seen
  • This means that it is possible to lose it if you are sneaky enough


This is great as an add-on script to one of your already existing AIs!

Uses:
  • Dogs
  • Zombies & Monsters
  • Close combatants
  • Lunatics


------------------------------------------------------------

Check out the scipts themselves for more info on using them.

The file itself shows how to implement them into a game.


------------------------------------------------------------






NatyGamer has also edited my example with extra scripts for roaming before actually seeing the player (which is done through a sight cone). It contains...
  • Random roaming
  • Field of view
  • Helpful lines that show how the AI is working
  • My script, only indented and easier to read


Download .gmk




Making AI

Extract from help file (press F1)

The best way to look at AI programming is just to remember the AI doesn't have independant thinking,
you have to split the thought process that determines what the best course of action is into smaller chunks.


Example

You would think:

"Oh crap I'm in the open and the player can shoot me so I need to run for cover!"


So the circumstances for the AI to run to cover are...

Split up:

"The player can see me"

and

"I'm not in or near cover."

So the code would be :

if !collision_line(x,y,player.x,player.y,block,0,1) && distance_to_object(instance_nearest(x,y,block))>20

Translation:

if there isn't a collision with an instance of "block" (an obstruction) on the line between the object and the player, and (&&) the distance to the nearest instance of "block" is more than 20 pixels away

NOW RUN FOR COVER!


I hope that you understood

------------------------------------------------------------

You may find my first AI topic in Tutorials & Examples useful as it has separate, individual scripts for:
  • Seeking cover
  • Seeking the enemy while sticking to cover
  • Avoiding bullets


Here is the link


------------------------------------------------------------

Please comment and credit if used (it took a long time to get right)
And feel free to ask me anything


Thanks!
Lippy
This is awsome,I actually had trouble killing them,8/10.One thing though,they move kinda fast and I'm no gml expert so I wasn't quite sure how to change they're speed.
Chhaaiinnssaaww! Get Some!
You just change the arguments in the script.

So in the step event where it says shooter_ai(....etc) just change the numbers in the bracket to a smaller amount.

It's that simple.

Glad you liked it, anyone else?
True Valhalla
ZOMG I loved this; Sexiest TDS AI I have seen in ages-I will nearly certainly use this!!

9/10
Chhaaiinnssaaww! Get Some!
lol thnx man!
The eleventh plague of Egypt
Units are overlapping, each going over the other.

There are 2 ways to avoid this, :
1) set the also the moving instances as solid, or
2) set the last argument of mp_potential step to 1, so it will avoid all instances (not only the solid ones)

btw random spawns can lead to stuck units, you'll need a collision check for that.
True Valhalla
QUOTE (The eleventh plague of Egypt @ Nov 23 2008, 09:43 PM) *
btw random spawns can lead to stuck units, you'll need a collision check for that.


The units don't spawn on top of bricks as they only spawn on the x axis at the top, where there happen to be no walls.
Chhaaiinnssaaww! Get Some!
The objects do overlap but i find the ai doesnt work so well when you apply the methods that you suggest. Asthetically though it works, so follow eleventh's advice if you want to stop overlapping.

feel free to comment anyone else smile.gif

oh and thanks for sticking up for the example True Valhalla!
FireWire Games
Meh, it's ok. Good for unregistered people and those who are just starting out in TDS AI, anyway.

I give it a 8/10. I'd like to see something even more advanced from you, because you seem to have an excellent grip on AI.
Chhaaiinnssaaww! Get Some!
Thanks mate but more advanced in what way?

I designed this so that it reacts realistically, takes cover when in danger and fights back when not.
I decided when i had finished not because my ability had fallen short but because i felt it worked how i wanted.

Unless you mean more specific changes like using A* algorithm to path find (something i was considering doing)?

FireWire Games
Don't get me wrong, the gun fights are very realistic. But you should go more in-depth with it. For example, give the enemies a field of view. In yours the enemies always know where you are, which is a serious flaw. Also, you should make the enemies able to interact smartly with each other and their environment. By that I mean they should be able to "tell" each other to, say, distract the player while the other flanks him. Just make it more advanced, get it?
Chhaaiinnssaaww! Get Some!
alright i see what you mean about depth now. Unless anyone really wants me to, I dont really intend to add to the ai in this example, but I am using this ai in a game i am making so i will be sure to add more depth.
Thanks for the great feedback though!
Anyone else?
IQbrew
This is great.
You may want to think about moving up to complex platform AI.
Chhaaiinnssaaww! Get Some!
thanks for the compliment.
Yeah i was thinking about trying to make some platform ai but its pretty difficult right? Really though Im focusing on top down since i have developed a fake 3d engine.
but on the topic of making platform ai, i have never tried it, how would you go about it? I assume you have to height check with the target and check for collisions in front to know when to jump etc.
On another note, does anyone like or find useful my tutorial on making ai in the first post?
IQbrew
I didn't see arrays in this (If there was any) at a quick glance.
A platform AI would need plenty of 2D arrays, and have it checking things like
--------
Is there a platform under me?
Can I move right?
Can I move left?
Is a bullet coming at me?
Etc
---------
If so...
--------
Should I move to the right?
The left?
Should I jump out of the way now?
Things like that...
--------
And...
--------
the players next to me-
Should I flee? Jump? Move towards him? Shoot or Whatever else he can do?
I say if you can make an AI this great for topdown, you can do it for platform.
Chhaaiinnssaaww! Get Some!
so its all about checking the current position. It doesnt seem too bad but famous last words, eh? wink1.gif
Ill definately give it a go after my current project, but im really in that top-down-shooter phase at the moment. smile.gif
IQbrew
Yeah, but making it avoid or collide with other NPCs is an entirely new problem.
Chhaaiinnssaaww! Get Some!
OK. Thanks for the advice.

Anyone else? This has got quite a lot of downloads already.
I'd just appreciate comments you know? smile.gif
Fede-lasse
It really misses a major part: smooth coding.

But it's good for a first time AI coder, it has good potential smile.gif
Chhaaiinnssaaww! Get Some!
thanks its nice to have a compliment from an ai veteran. Would you care to elaborate on smooth coding please? When I made it I felt it was as efficent as possible.
Fede-lasse
QUOTE (Chhaaiinnssaaww! Get Some @ Dec 7 2008, 01:46 AM) *
thanks its nice to have a compliment from an ai veteran.
Thanks laugh.gif

QUOTE (Chhaaiinnssaaww! Get Some @ Dec 7 2008, 01:46 AM) *
Would you care to elaborate on smooth coding please? When I made it I felt it was as efficent as possible.
Well, not only smooth coding, but it also misses some smooth behaviour. By "smooth coding" I mean indenting your code. I used to format my code like this:
CODE
if i="are weird" {
do_something_plz() } else {
hmm("lemme think") }

But now, I write it like this:
CODE
if (I == "am weird") {
  do_something_please();
} else {
  hmm("Lemme think.");
}
You can clearly tell the difference between those two. Indenting makes code more organized and easier to read. Just practice, I got used to it in the end! smile.gif

By smooth behaviour, I mean... Well, things like rotating smoothly or moving smoothly. It all makes the AI seem more easy on the user, not just some hardcore killer that makes no mistakes and comes right up at your face and then kills you in two seconds. I hope you understand what I mean by that.
Chhaaiinnssaaww! Get Some!
ah Ok. Thanks for the great links! Ill be sure to implement them. I'd guess you'd want to use them when making 3d ai in particular.
Chhaaiinnssaaww! Get Some!
bump
Lord Alfred
image_angle isnt unreg and I believe lengh_dir is reg too. Looks good, but until I get my main comp back up, I can't test it. =/ It sounds like it's just what I've been needing for about 2 years though, so Im looking forward to testing it!
Chhaaiinnssaaww! Get Some!
QUOTE (Lord Alfred @ Dec 24 2008, 03:49 AM) *
image_angle isnt unreg and I believe lengh_dir is reg too. Looks good, but until I get my main comp back up, I can't test it. =/ It sounds like it's just what I've been needing for about 2 years though, so Im looking forward to testing it!


I'm pretty sure lengthdir isn't a registerdd function but if you want to run this unreg, then go into the step event for the AI and change the last number in the script to 0.
Basically in the script you can set whether to use the registered features or not by changing the last argument. (Just check the script for more detail).

Glad you liked it though
maneatingmoo
This is so cool! Is it alright if I use it to make zombies in a game I'm making?
True Valhalla
I was mucking around with a TDS prototype and decided to see exactly how easy this is to use...and, really, there was nothing to implementing this...I still maintain that this is the best AI I have seen on these forums, and if I ever do actually get around to a TDS, this will be the first thing credit =)
lachlan438
Very Nice work, I like it lots but the AI should try to move them apart I am going to use most of your code in a new game of mine so I will credit you good work I liked it a lot
x-death
you need to make this a little more complex. complex in the sence that if the ai is low on health or just needs to get a better position on the player then he lifts his gun up while his ducking for cover and does some misfires (misfires mean pointing gun towards something without looking so they are just meaningless warning shots) so it then gives him the opportunity to run for his position or health while his team mates gets up and covers him. and the team mate can get up safely due to the misfires towards the player causing him to get hurt and duck or just duck.

hope you like the idea. with this and some other suggestions made before about a more advance ai it will make this awesome. but you should condier difficulty levels see no matter is its easy or hard the player should always have a chance of winning its just how big of a chance your giving the player.
Chhaaiinnssaaww! Get Some!
wow thanks for the nice comments guys!
With regards to the varying skill level i was thinking about making a v2. In this the ai will have stats which can be decided as it is created. This will include things like shoot skill and regularity, health and speed. The problem with stats more complex than those though is it makes the ai act strangely unless accompanied by appropriate visuals. Basically simplicity is the key to a good ai. I tried things like you suggested, they sound gr8!, but don't look good tho
True Valhalla
I think 'shoot skill' and other variables is a step in the right direction.
NatyGamer
Wow, Chainsaw, I've been looking through this and various other TDS ai scripts, and I must say, yours is definitely the most thorough (and toughtest)! I'll definitely be using this and combining it with my A* pathfinding and other ai scripts I've downloaded.

Have you seen the AI_example_wip? (Yes I know that sounds very vague but that's what it's called). That has some very good coding for an enemy that's just roaming around and attacks once it sees you. It also takes distance and a kind of "view cone" into account so it's pretty realistic. I'm gonna try to combine yours and that and see what the results look like (if I ever get to it in the near future...).

And THANK YOU for all the commenting! Too few people do that! All I would suggest is to follow Fede-lasse's advice and work on your code formatting. It is a bit tough to read.

I can't wait to see just how good this gets in the future!
Chhaaiinnssaaww! Get Some!
Thanks a lot! If you get this AI melded with something else please post it here or tell me about it cos I'd love to see it!
Unrivaledneo

___________________________________________
ERROR in
action number 1
of Step Event
for object zombie:

In script beserker_ai:
Error in code at line 36:
mp_potential_step(ex-argument2*3+random((argument2*6)),ey-argument2*3+random((argument2*6)),argument2,0)

at position 20: Unknown variable ex


this error would pop up on me when a zombie eat a human and tunred him tongue.gif Still learning GML just not sure how it says there is no variable
Chhaaiinnssaaww! Get Some!
huh.gif This doesn't happen to me, did you initialise the variable in the zombies create event or did you change the script in some way?
NatyGamer
Unrivaledneo, you must have set the performevents argument to false in the instance_change function of the collision event with a zombie in the ai object (that's definitely a mouthful). What that does is when the ai collides with a zombie, it turns into a zombie object but doesn't perform the create event of the zombie, thereby not initializing ex or ey with will give you an unknown variable error.
NatyGamer
Well, I've just spent five hours trying to integrate ai that looks for the player and it's been quite a pain. And unfortunately I'm pretty sure it'll only work with pro.

However, if you can accept some spastic turning and movement, it works pretty good! I'll work on it some more and post it in the near future for comments and advice.
Unrivaledneo
Well i figure it out forgot to put this is as the create step, though idk why i gotta have it in there..

enemy=hero

ex=random(room_width)
ey=random(room_height)
Chhaaiinnssaaww! Get Some!
NatyGamer, if you're talking about this AI being only for pro, all you have to do is set the last argument in the script to 0. That turns all of the registered functions off. (which is image_angle)
PivotGamer84
QUOTE (FireWire Games @ Nov 25 2008, 01:15 PM) *
Don't get me wrong, the gun fights are very realistic. But you should go more in-depth with it. For example, give the enemies a field of view. In yours the enemies always know where you are, which is a serious flaw. Also, you should make the enemies able to interact smartly with each other and their environment. By that I mean they should be able to "tell" each other to, say, distract the player while the other flanks him. Just make it more advanced, get it?

That's actually a really good idea I wouldn't mind if "Chhaaiinnssaaww! Get Some!" released a version of this with those ideas. Also since Chhaaiinnssaaww! Get Some! is your post name, why not throw a chainsaw in this game? That would be really cool (with the option of blood and gore spurting out) and it would be an epic signature in your game/engine.
NatyGamer
To Unrivaledneo, you need that in the create event because the ai uses those variables in the script, and if you ever use variables, they have to be initialized somewhere (creation event is always best).

To Chainsaw, sorry I was referring to what I've added to your ai. It's pretty much impossible to create ai that will look for you without using image_angle; otherwise, the player has no idea which way the enemy is facing and can't sneak past for example. There is one way to do it without pro, but it's a pain and can lead to lots of problems so I'm not even going to mention it.

And that field of view PivotGamer84 quoted is just what I'm working on (since I found some really good code that does that). And yes, a chainsaw in your ai would be awesome, though absolutely horrible to try and use in a TDS. It would still be awesome though, since that's your name. You could call it...the Chhaaiinnssaaww!
Chhaaiinnssaaww! Get Some!
Haha you guys are really strange tongue.gif

Maybe I might add in a random chainsaw at some point.

The reason why I didn't add in view cones etc. is because not everyone wants one. This AI is really combat orientated.

However in my next version, I am going to make an extra optional script for placid or searching behaviours, not just combat.
NatyGamer
Hey, Chainsaw, I think I'm done integrating my code with yours. It works pretty well! I kept your original combat ai and simply made my own roamAI. It's a little too easy to sneak up on them, but it feels really cool to take 'em out from behind their back! And it's really cool when you have the two different ai at once, so your fighting guys that are contantly attacking, and then you suddenly find other guys just walking around.

Do you want me to post a link on here if you wanna check it out? It's still a majority of your work, and I'm amazed at how little AI I had to do, since you made it all very easy to expand on! Let me know!!
NatyGamer
Oops, sorry; Didn't mean to add another post!
Chhaaiinnssaaww! Get Some!
yeah id love to see it! Just post it here
LowGlowGames
QUOTE (NatyGamer @ Jan 10 2009, 09:09 PM) *
Hey, Chainsaw, I think I'm done integrating my code with yours. It works pretty well! I kept your original combat ai and simply made my own roamAI. It's a little too easy to sneak up on them, but it feels really cool to take 'em out from behind their back! And it's really cool when you have the two different ai at once, so your fighting guys that are contantly attacking, and then you suddenly find other guys just walking around.

Do you want me to post a link on here if you wanna check it out? It's still a majority of your work, and I'm amazed at how little AI I had to do, since you made it all very easy to expand on! Let me know!!


That seems cool

(MY 100th Post!!!!!)
NatyGamer
Alright here it is! I think I've got everything I want in it.



Note that you may want to mess around with the speeds, view cone size, and sight distance. And I've added in a few extra fun things, totally unrelated to ai, but show off the power of being able to sneak up on enemies.

Hope you can build off of this as well!!
IQbrew
Hah, I get to be the first to try.
Chhaaiinnssaaww! Get Some!
haha thanks for indenting my script and making it tidier.

Yeah it's great! I really like it. The thing is, they don't act with each other. For example, if they are alerted to your presence then they don't tell each other, I feel that is important. I think they should also react to bullets.

I'm going to have a go at this myself, oh and I'm going to put your example on the first page for everyone to look at.
NatyGamer
I'm glad you like it!

And yeah, I was thinking about having them communicate, telling nearby ai their last sighted position of you but I just didn't get to it. It shouldn't be that difficult (tell all ai within distance of 500 to set enemy x and y to their last enemy x and y or something like that). I was also thinking about having them alerted when you shoot. For example, when you shoot, if they're close they turn towards you, and if they don't see you (cover in the way or something) they go investigate. I just didn't get to it though.

So tackle those problems yourself if you want. I may try out my own solutions in the future.

And thanks for posting it on the front page! I'm sure plenty more people will see it there then way over on page three.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.