Jump to content


Photo
- - - - -

Ai Command Stack


  • Please log in to reply
52 replies to this topic

#1 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 25 October 2007 - 04:15 AM

Summary




I would like my AI to go some place, hunt down enemies and then come back and if there is an enemy along the way, I would like the AI to hunt him down but if a boss or too many enemies are around, I want the AI to run to a safe place....


How many such post have you seen... Is it possible? Yes. Is it easy? No. But the command stack makes it relatively easy.

I want to click a place on the screen and want my player to go there. I want him to follow everywhere I clicked. But I want him to defend himself along the way.


Is it hard to do? Not very but the command stack makes it dead easy.

HERE IS A SIMPLE SCRIPT SWITCHING SETUP WHICH IS MUCH LESS HARD TO GRASP

It's the foundation of this command stack system. I strongly recommend you try this simpler setup first because jumping from a case based AI to this command stack based AI may be too much of a jump for some. This is the in-between step. Hope it helps...


How it works:

The Command Stack uses the GM ds_list and ds_stack and the ability to call a script by reference to add in a sequence of commands for an AI to perform.

The Command Stack executes the script at the top of the command stack until the script is done. When the script is done, is it popped out of the stack and the next script in the stack is executed.

What does this mean?

It means you can code relatively small bits of command scripts to do very specific actions instead of writing a huge decision making script in the step event and loose yourself in all the local and global variables needed to remember where you are. That means less code is executed and the code is more easily created and fixed.

It means your AI becomes task oriented instead of state oriented.

It means your AI can change the AI behavior at anytime with little concern on the state it is currently in.

It means you can write very intelligent AIs with ease.


Here is an example of a command stack. A simplified example.

CommandStackPush PatrolToXY hunting for enemy along the way

The stack look likes this now
<instack>PatrolToXY <executing>

Now, PatrolToXY will push another script in the stack if it finds an enemy along the way AttackEnemy

Now the stack looks like this
<instack>AttackEnemy <executing>
<instack>PatrolToXY <on hold>

Now you hit the enemy and need to back up. Now the stack looks like
<instack>BackUp<executing>
<instack>AttackEnemy<on hold>
<instack>PatrolToXY <on hold>

Backing up is done so the attack resumes
<instack>AttackEnemy <executing>
<instack>PatrolToXY <on hold>

The attack is over so the patrolling resumes
<instack>PatrolToXY <executing>

When the stack is empty,
<instack><empty>
you add another set of instructions to the stack to make it do something else.


I also added a CommandList.

The command list is a sequence of pending tasks to perform once the command stack is empty. When the stack is empty, the first command list is pushed in the stack and executed. This allows to define a whole sequence of actions without affecting the current behavior (urgent tasks). Since adding a command on the stack stops the current script, the list is required to add a series of commands to be executed in order. That way, the stack will fill and empty up between each element in the command list, It's hard to word right.

Here is a sample patrolling command list:
<inlist>PatrolToXY 0.0
<inlist>PatrolToXY 100,100
<inlist>PatrolToXY 1000,1000

When the system starts, it will push the fist list item into the stack so the stack will be
<instack>PatrolToXY 0,0 <executing>

And the list is now
<inlist>PatrolToXY 100,100
<inlist>PatrolToXY 1000,1000

Your AI encounters an enemy so now the stack is
<instack>AttackEnemy <executing>
<instack>PatrolToXY 0,0 <on hold>

The list is still
<inlist>PatrolToXY 100,100
<inlist>PatrolToXY 1000,1000

The attack is over, the stack is popped
<instack>PatrolToXY 0,0 <executing>

The patrol point is done the stack is popped
<instack><empty>

so now the next list item is pushed
<instack>PatrolToXY 100,100 <executing>

And the list is now
<inlist>PatrolToXY 1000,1000

While the AI is attacking and moving, you can add another item in the list. Adding it to the stack would cancel the attack

Lets say we add another patrol point to 50,50 so now the list is
<inlist>PatrolToXY 1000,1000
<inlist>PatrolToXY 50,50


Simplified, Imagine the list is the things you need to do, in order, during the day.
And the stack is the things you need to do right now. If another more urgent thing happens, it goes on top of the stack and is removed when it's handled.


So, what goes in the stack and list?

Scripts (with their arguments) go in the stack or list, very specific scripts that follow a few simple rules

A script returns 1 when done -1 when done and urge the other script to continue right away and 0 when the script is not done.
This allows for the script to be called until it's done (such as for a moving scripts) or a script to run and quit right away.

A script receives a data object to stuff variables into for use in the execution. Each script has its own data object.

A script is called for notification and for execution. The top of each script has a decision handling code to determine why the script is called. The script is called when pushed into the stack where you can initialize variables, It's called when popped out of the stack where you free your data (some data like ds_lists and paths need to be freed if you create them). Finally, the scrip is called for execution. You can tell if it's the fist execution or the subsequent ones from the flag given.

The arguments passed to the CommandStackPush are actually stuffed in the data object. This was done to save on the number of stacks created. Since you get a data object to stuff your variables in, I copied over the arguments into the data object variables named arg1,arg2,�€�,arg15.

Some rules about adding command scripts:
Make your scripts as independent as possible.
Try to rely on passed arguments (you have 15 to work with) and not on global or instance variables. I broke this rule for the tank example scripts.
Since it's possible you can have the same script many times in the same stack, you should rely only on passed arguments and the data you stuffed in the data object as the other script may overwrite global variables.
Though you can still use instance variables to pass on information. Nothing stops you so be careful.
If you can make script generic enough, you will be able to share them with other people.
The scripts push other scripts in the stack. For example, the PatrollToXY will push the AttackEnemy script in the stack, passing on related arguments.

The demo has 3 rooms, you have to move the room to the top of the room list in GM to see it in action. The tanks are from our game (The Tank Game by Tiby312 and I)

I also include 2 path functions to generate a bunch of move commands based on a path. The good thing is, after you added your path to the command list, you can delete it right away if it is no longer needed or reuse it. The functions are smart enough to take in any kind of move scripts as long as x,y,speed are the first, second and third arguments in the command script. All other arguments are passed along.

That's all I can think of at the moment. Please give some feedback. And please see if you can create some cool scripts to help others control their AIs.


:ph34r: Just merge this project with yours and remove all the objects but cmdDataObj or do what the :GM6: users need to do...

:GM6: (Deprecated, you can merge with the included gm6 file now) Import the scripts from the script file (you will loose the script folders I used the segregate the functions) and create a cmdDataObj with this information.

Information about object: cmdDataObj

Sprite: <no sprite>
Solid: false
Visible: false
Depth: 0
Persistent: true
Parent: <no parent>
Mask: <same as sprite>

Create Event:
execute code:

action = 0;
arg1 = 0;
arg2 = 0;
arg3 = 0;
arg4 = 0;
arg5 = 0;
arg6 = 0;
arg7 = 0;
arg8 = 0;
arg9 = 0;
arg10 = 0;
arg11 = 0;
arg12 = 0;
arg13 = 0;
arg14 = 0;
arg15 = 0;
arg13 = 0;
arg14 = 0;
arg15 = 0;

I just realized you should probably set the persistent flag to true for the cmdDataObj for the system to work properly in case you make your AI persistent... I have updated the code.

Also, you must be careful if you use instance_deactivate in your game.
You should reactivate the cmdDataObj(s) every time you instance_deactivate(all)

Press N for next room

Set cmdDataObj to visible to see it in action

Use the merge feature to add all required components to your game.
Tweaks to your movement system will be required for the system to work properly so merge early.

If you have trouble setting it up, you may PM me.





Example Game using the command stack
Desktop Deathmatch


Search Tags
  • GMCTUTORIAL
  • GMCENGINE
  • GMCAI

  • 2

#2 Indecom4000

Indecom4000

    GMC Member

  • GMC Member
  • 1153 posts
  • Version:GM:Studio

Posted 25 October 2007 - 06:14 PM

Okay, so this sounds really good. Are there any features is that gmk that are specific to gm7? Can i easily convert it to gm6, since i'm still using that?
  • 0

#3 funcravers

funcravers

    GMC Member

  • New Member
  • 1087 posts

Posted 26 October 2007 - 01:13 AM

Freking awesome!

This wouldn't eb so hard to do but the way I would do it takes way more time and space.

I would use arrays for the commands.
  • 0

#4 x-death

x-death

    Flash Developer

  • GMC Member
  • 3402 posts

Posted 26 October 2007 - 04:56 AM

just downloaded...this is relatively good i have been waiting for something like this i wanted to know just how to do it and this explains it enough thanks
  • 0

#5 Tarik

Tarik

    GMC Member

  • GMC Member
  • 2483 posts
  • Version:GM:Studio

Posted 26 October 2007 - 05:17 AM

Interesting stuff, nicely programmed. It works well.

Only let's say it's attacking and there's an incoming bullet, you'd have to make seperate scripts for just strafing and just attacking, and strafing and attacking at the same time when that is required, as the stack doesn't allow you to perform two tasks at the same time, unless you introduce another stack for sub-tasks.

Everytime you want two tasks to be executed at the same time, you have to make two scripts with the task seperate, and another script when you want to perform both tasks at the same time.

So I'm still quite a fan of stimuli and behavior (though they're not much different), there can be specific tasks stacked too and given priority, just without the limits of a single offical stack and automated scripts through which everything is handled, allowing you to perform a multiple of tasks with an AI because everything isn't handled by a single-task-executing stack Datastructure.

Whatever my preferences are, this is an excellent engine. Great job.
  • 0

#6 Storm:.

Storm:.

    GMC Member

  • New Member
  • 690 posts

Posted 26 October 2007 - 11:37 AM

Very good tutorial. This can be great use to somebody.
PS: YAY!!!! First post!!!!!!!!
  • 0

#7 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1070 posts

Posted 26 October 2007 - 03:39 PM

Wow. I can tell this will be very useful. Good work!
  • 1

#8 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 26 October 2007 - 07:03 PM

Okay, so this sounds really good. Are there any features is that gmk that are specific to gm7? Can i easily convert it to gm6, since i'm still using that?

<{POST_SNAPBACK}>


Just follow the GM 6 instruction at the bottom of the post.

Freking awesome!

This wouldn't eb so hard to do but the way I would do it takes way more time and space.

I would use arrays for the commands.

<{POST_SNAPBACK}>

The reason I don't use arrays is because I have no way of knowing what data you will need to store. That is why I give you a data object... Which is a safe place to stuff all the variables you would ever need.

just downloaded...this is relatively good i have been waiting for something like this i wanted to know just how to do it and this explains it enough thanks

<{POST_SNAPBACK}>

Cool stuff

Interesting stuff, nicely programmed. It works well.

Only let's say it's attacking and there's an incoming bullet, you'd have to make seperate scripts for just strafing and just attacking, and strafing and attacking at the same time when that is required, as the stack doesn't allow you to perform two tasks at the same time, unless you introduce another stack for sub-tasks.

Everytime you want two tasks to be executed at the same time, you have to make two scripts with the task seperate, and another script when you want to perform both tasks at the same time.

So I'm still quite a fan of stimuli and behavior (though they're not much different), there can be specific tasks stacked too and given priority, just without the limits of a single offical stack and automated scripts through which everything is handled, allowing you to perform a multiple of tasks with an AI because everything isn't handled by a single-task-executing stack Datastructure.

Whatever my preferences are, this is an excellent engine. Great job.

<{POST_SNAPBACK}>


You can, if you want, call another "regular" script within your script or call a command script that always return -1 from another script so you have many commands running that way... You can also modify the code to pass on/create another stack so you could have many stacks called from the step event... Given you change my code a little.

In any case, having two task adds the difficulty of having 2 concurent tasks fighting over something as simple as moving the AI around...

The tanks have 2 AIs running per tank. The tank base for moving and the tank turret for aiming and shooting.

Though one of the point is to limit the number of decision in your main execution so I would simply create a command that does the 2 wanted tasks.

I idea of prioritised multi stacks is interesting but I gather the difficulty level increases drastically.

Very good tutorial. This can be great use to somebody.
PS: YAY!!!! First post!!!!!!!!

<{POST_SNAPBACK}>

Cool and welcome

Wow.  I can tell this will be very useful.  Good work!

<{POST_SNAPBACK}>


Thank you.



Thanks to everyone trying this. I hope you will find it useful.
  • 0

#9 jabelar

jabelar

    GMC Member

  • New Member
  • 2980 posts

Posted 27 October 2007 - 04:11 AM

While I'm not convinced it would be best for the main enemy AI, I think it is great for any "background AI" such as computer characters doing things in the background of an RPG or RTS.

Also, for simulation games I think this could be a real boon. For example if you're a railway tycoon or something, you could have all the elements processing these sort of stacks.

These stacks are similar to a timeline but without specifying specific moments in time, and so is possibly more flexible.
  • 0

#10 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 27 October 2007 - 05:46 AM

I just realised you should probably set the persistent flag to true for the cmdDataObj for the system to work properly in case you make your AI persistent I will update the code.

Also, you must be careful if you use instance_deactivate in your game. You should reactivate the cmdDataObj everytime you deactivate(all)

Edited by icuurd12b42, 28 October 2007 - 12:28 AM.

  • 0

#11 Tiby312

Tiby312

    GMC Member

  • New Member
  • 406 posts

Posted 27 October 2007 - 09:24 AM

Excellent tutorial! Very well documented and organized. So this is how you make good AI. :)
  • 0

#12 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 09 December 2007 - 08:09 PM

Please update to the more sturdy Dec 06 2007 version that fixes the
"Data structure with index does not exist" problem.

:) New! GM6 Project included for easier merge with your gm6 game. No need for the extra steps at bottom of first post.
  • 0

#13 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 18 December 2007 - 01:26 AM

I was finally able to access my account on host-a.net. So the host-a and the hostforfood are now the same file
  • 0

#14 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 14 January 2008 - 05:36 AM

I added ds_stack and ds_list emulation so the system can be used by non registered/lite users.

I cannot unregister my GM so please report any registered call I may have left in the unreg versions.
  • 0

#15 Monkeywrench

Monkeywrench

    GMC Member

  • New Member
  • 12 posts

Posted 17 January 2008 - 06:44 PM

Yeah, I've found a pro-only call in the lite version:

draw_sprite_ext(sprite_index,-1,x,y,image_xscale,image_yscale,
image_angle,image_blend,image_alpha);
It doesn't show an error for anything else, I think. It does this for both versions.
  • 0

#16 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 18 January 2008 - 12:00 AM

Yeah, I've found a pro-only call in the lite version:

draw_sprite_ext(sprite_index,-1,x,y,image_xscale,image_yscale,
image_angle,image_blend,image_alpha);
It doesn't show an error for anything else, I think. It does this for both versions.

<{POST_SNAPBACK}>


Thanks. I fixed it and added image_index based rotation for the little round AI... I totally forgot about it...
  • 0

#17 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 16 February 2008 - 07:37 PM

Significant Update:
Added Platform AI support which also includes platform pathfinding which requires little effort to setup and tweak
Added pathfinding to top down (was already there but without any examples)

Sample now has 13 demo rooms. Press N for next room

Set cmdDataObj to visible to see it in action

Use the merge feature to add all required components to your game.
Tweaks to your movement system will be required for the system to work properly so merge early.

If you have trouble setting it up, you may PM me.

Edited by icuurd12b42, 19 February 2008 - 10:46 AM.

  • 0

#18 Qwertyman

Qwertyman

    C&D Games

  • New Member
  • 1178 posts

Posted 17 February 2008 - 10:15 PM

This seems really helpful. I never even thought of putting scripts into a data structure to be executed in an orderly fashion. Just the idea helps.
  • 0

#19 deceit13

deceit13

    GMC Member

  • New Member
  • 56 posts

Posted 24 March 2008 - 06:47 PM

i dont get it, i guess i have to look into stacks more... why 2 stacks needed? what reeds the return? i assume when i figure out the steps of the code and the logic behind puting it together and what calls what ill understand but untill then i guess its trial and error for me

what does (!ds_stack(ds_stacklist) return? the state of script at the top of the stack?
  • 0

#20 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 24 March 2008 - 08:55 PM

i dont get it, i guess i have to look into stacks more... why 2 stacks needed? what reeds the return? i assume when i figure out the steps of the code and the logic behind puting it together and what calls what ill understand but untill then i guess its trial and error for me

what does (!ds_stack(ds_stacklist) return? the state of script at the top of the stack?


That not even in my code... You should not even look at the core functions code and only concentrate on the command scripts and duplicating the one that looks like it could be used as the starting block for your scripts.

read the
So, what goes in the stack and list?
part in the main post.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users