# How to teach the AI use this specific move properly? (Read 7732 times)

Started by WastedCoder, April 01, 2022, 01:32:29 PM

#### WastedCoder

##### How to teach the AI use this specific move properly?
#1  April 01, 2022, 01:32:29 PM
Hello, everyone.

A character I have has a special move, where he, while in the air, shoots a projectile, which flies diagonally. The higher is the character in the air, the further is the reach of a projectile, then the lower he is in the air, the shorter is the reach. How do I teach the AI to use it properly? I've been trying to come up with some kind of formula using p2bodydist x and p2bodydist y, but I failed to create something universal that will make it use it properly. If you have any ideas, examples and/or triggers I should use for such a move, please, let me know!

#### altoiddealer

##### Re: How to teach the AI use this specific move properly?
#2  April 01, 2022, 04:11:04 PM
• Robotics Engineer
Someone will probably pop in with a better answer... but here is my input.

If you want your character to jump a certain direction/height before performing the attack, then I suggest you continuously monitor P2 / adjust a Var, so that your jump trigger and the attack trigger can rely on what the Var is.

If you just want the AI to decide which variation of the attack to perform while in the air, then you should be able to just take a quick location check and set a Variable.... or possibly make a big conditional parameter on your changestate.

As for positioning, I recommend reviewing the code I use for my character Jailbot, which constantly monitors P2's location in order to choose which face image to use (He looks at the enemy throughout the fight).

Relavent code:

Example usage... chooses correct "face" when idle.
Code:
[State 0, Stand face]
type = Explod
trigger1 = anim = 0
ID = 1000
anim = 10000 + var(7)

And this is the monitoring system I use in State -3... There are 18 different faces he chooses from for 18 different regions P2 is on the screen.
So, you could probably consolidate your definitions to larger, fewer regions.  You could also factor in stuff like enemy's current x and y vel to maybe select a different var than just "current location".
Note that I use Null controllers to better organize varsets, especially when there are numerous ones.
Also note my comments such as "R" and "RR" are just a quick reference as to how far away the enemy is.  So, "UURRR" refers to when the enemy is far above and at the far opposite side of the screen.

Code:
[State -3, Normal Face Changer - Look Neutral]
type = Null
triggerall = numenemy
triggerall = EnemyNear,statetype != C && (EnemyNear,StateNo != [10,12]) ;Enemy not crouching
trigger1 = p2dist x < 120 && p2dist y > -20 ;NEAR
trigger1 = var(7) := 0
trigger2 = (p2dist x >= 120 && p2dist x < 200) && p2dist y > -30 ;R
trigger2 = var(7) := 4
trigger3 = p2dist x >= 200 && p2dist y > -40 ;RR
trigger3 = var(7) := 8

[State -3, Normal Face Changer - Looking Up]
type = Null
triggerall = numenemy
triggerall = p2dist x < 120
trigger1 = p2dist y < -20 && p2dist y >= -40 ;U
trigger1 = var(7) := 1
trigger2 = p2dist y < -40 && p2dist y >= -60 ;UU
trigger2 = var(7) := 2
trigger3 = p2dist y < -60 ;UUU
trigger3 = var(7) := 3

[State -3, Normal Face Changer - Looking Up / Right]
type = Null
triggerall = numenemy
triggerall = p2dist x >= 120 && p2dist x < 200
trigger1 = p2dist y < -30 && p2dist y >= -50 ;UR
trigger1 = var(7) := 5
trigger2 = p2dist y < -50 && p2dist y >= -70 ;UUR
trigger2 = var(7) := 6
trigger3 = p2dist y < -70 ;UUUR
trigger3 = var(7) := 7

[State -3, Normal Face Changer - Looking Up / Right Right]
type = Null
triggerall = numenemy
triggerall = p2dist x >= 200
trigger1 = p2dist y < -40 && p2dist y >= -60 ;URR
trigger1 = var(7) := 9
trigger2 = p2dist y < -60 && p2dist y >= -80 ;UURR
trigger2 = var(7) := 10
trigger3 = p2dist y < -80 ;UURRR
trigger3 = var(7) := 11

[State -3, Normal Face Changer - Start Looking Down]
type = Null
triggerall = numenemy
triggerall = (EnemyNear,anim = 10 || EnemyNear,anim = 12) || p2dist y = [21,40] ;Starting to crouch, or low
trigger1 = p2dist x < 120 ;NEAR
trigger1 = var(7) := 12
trigger2 = p2dist x >= 120 && p2dist x < 200 ;R
trigger2 = var(7) := 14
trigger3 = p2dist x >= 200 ;RR
trigger3 = var(7) := 16

[State -3, Normal Face Changer - Looking Down]
type = Null
triggerall = numenemy
triggerall = EnemyNear,anim = 11 || p2dist y > 40;Enemy is crouching, or very low
trigger1 = p2dist x < 120 ;DD
trigger1 = var(7) := 13
trigger2 = (p2dist x >= 120 && p2dist x < 200) ;DDR
trigger2 = var(7) := 15
trigger3 = p2dist x >= 200 ;DDRR
trigger3 = var(7) := 17

[State -3, Normal Face Changer - No Enemy]
type = varset
trigger1 = !numenemy
var(7) = 0

Spoiler: Video showing my technique in action (click to see content)

#### WastedCoder

##### Re: How to teach the AI use this specific move properly?
#3  April 02, 2022, 10:07:35 AM
Wow! Thank you for such a big reply.
My main goal's to make the AI use this move when it's certain to hit the target. I already have a set of triggers for the jump he uses, I don't want him to jump purposely to use this move, but to use this move just when it's certain to hit.

I want to find the way for him to calculate if the projectile can hit the target or not, not to waste a meter, because it takes 1500 power, half of the max amount. He often misses it, so I wonder if there is a way to teach the AI to see that trajectory, like any human can just 'imagine' where it's going to fly to use it in a right moment.

Now that I think of it, maybe I can use a helper or something, and when this helper or maybe explod is on the enemy, then he uses this move... Hm...

I'll study what you've sent me, thank you so much, I love the fact you made all of this just for the sake of your character looking at the enemy, this is so cute ^^

#### altoiddealer

##### Re: How to teach the AI use this specific move properly?
#4  April 02, 2022, 06:22:35 PM
• Robotics Engineer
What are the vels of your projectiles?

You should be able to just factor them together for a ratio…  for instance if the vel is x=10 y=10 then you’ll want this to tigger when the enemy’s pos x/y = 1…. because regardless of their distance this ratio will be in target.

You could have a tolerance like enemy,pos x/enemy,pos y = [0.9,1.1] or something.  I’m wayyy out of practice

Edit, another example… assuming a variation of your projectile has a velocity of x=10 y=3 (10/3=3.33)
triggerall=enemy,pos x/enemy,pos y = [3.0 , 3.6]

Maybe add something else along the lines of
trigger1= enemy,pos x/enemy,pos y >= 3.3
trigger1= enemy,vel x >= 0
trigger2= enemy,pos x/enemy,pos y < 3.3
trigger2= enemy,vel x < 0

Last Edit: April 02, 2022, 07:05:54 PM by altoiddealer

#### WastedCoder

##### Re: How to teach the AI use this specific move properly?
#5  April 03, 2022, 01:39:47 PM
What are the vels of your projectiles?

You should be able to just factor them together for a ratio…  for instance if the vel is x=10 y=10 then you’ll want this to tigger when the enemy’s pos x/y = 1…. because regardless of their distance this ratio will be in target.

You could have a tolerance like enemy,pos x/enemy,pos y = [0.9,1.1] or something.  I’m wayyy out of practice

Edit, another example… assuming a variation of your projectile has a velocity of x=10 y=3 (10/3=3.33)
triggerall=enemy,pos x/enemy,pos y = [3.0 , 3.6]

Maybe add something else along the lines of
trigger1= enemy,pos x/enemy,pos y >= 3.3
trigger1= enemy,vel x >= 0
trigger2= enemy,pos x/enemy,pos y < 3.3
trigger2= enemy,vel x < 0

Oh, it works a bit differently than that. I should have specified this, sorry. It's a laser beam which hits the place it has been fired to immediately and disappears fast. It has no vel x and y, it just has an angledraw and a hitbox assigned to the animation of the laser, but I still am grateful for the piece of advice you've given. I will remember that for my future works.

I like your first reply, I am going to make it that way. Thank you so much, again
I will mark this as 'solved'

#### altoiddealer

##### Re: How to teach the AI use this specific move properly?
#6  April 04, 2022, 03:17:00 PM
• Robotics Engineer
Ok!  However the attack you are describing (an instant straight line) sounds like it would best be handled using the “compare x/y ratio” method I said most recently.  For all intents and purposes this would be checking a straight line, with whatever degree of tolerance you choose.
The first method I described is checking a series of box shaped regions of the screen, which will require more and more code to define smaller more precise areas, depending on how accurate you want this to be