In my last post, I mentioned that two of the things I wanted to add to the AI was logic on when to sprint and when to use power ups. That logic now exists! You can see some of it in the video below!
You can see the goblin’s controlled by the AI sprinting now! Sometimes they even use power ups! Wow! Whoa! Now, when do they do that?
There are two scenarios when the AI goblins will check if they should sprint:
- When the goblin has the ball
- When the goblin doesn’t have the ball but is chasing after a target
When the goblin has the ball, sprinting is basically the default. If they aren’t currently sprinting, the goblin determines how they will start sprinting. There is a random “fatigue chance” that determines if the goblin will “inadvertently” sprint until they are fatigued. Right now, there is a 10% chance of that happening. If the goblin will not sprint to fatigue, there is a “minimum stamina to sprint to” value set. This is a percentage of the goblins max stamina they will sprint down to. After those are set, the goblin will keep sprinting until they reach the minimum stamina amount (or become fatigued) or if a touchdown is scored/the half ends. Right now, the goblin will set that randomly between 5% and 15% of their max stamina. This is all to simulate a player accidentally sprinting to fatigue, and when a player would stop sprinting to avoid fatigue.
When the goblin doesn’t have the ball and is chasing a target, there are some simple checks for when to sprint. If the target is more than 10 units away, start sprinting. If they are chasing the ball carrier, they will also check if the ball carrier is sprinting. If yes, sprint after the sprinting ball carrier! (the “is the ball carrier sprinting” check wasn’t added in the video yet…) If either of those checks are met, then the goblin sets the sprinting parameters same as though they had the ball to get their fatigue chance and minimum stamina to sprint down too.
Power Up Usage Logic
For power ups, the AI needs to figure out the “scenario” they are in and see if that is appropriate to use specific power ups. During gameplay, the AI checks every 0.5 seconds to see if a power up should be used. The logic flows like this:
- Do you have the lightning blue shell? If yes, use
- Does the AI have the ball?
- If AI has invincibility blue shell, use it
- Use speed power up if you have it
- If ball carrier stamina is below certain level, use it
- If AI has a “Defense” power up (inc. def., banana, glue, bomb) and an enemy is nearby use it
- Does the opposing player have the ball?
- If any AI goblin is near an opposing goblin, and the AI has the increase attack damage powerup, use it
- If the AI has the bottle throwing power up:
- Loop through each goblin and check if an opposing goblin is directly in front of them using Physics2D.CircleCastAll in the direction the goblin is facing
- if an opposing goblin is hit by the cast, switch to that goblin and use the bottle power up
- If no one has the ball:
- If AI has Heal power up
- Check health of all goblins
- if any goblin is below 20% health, use Heal power up
- if the average health of the goblin team is below 50%, use Heal power up
- If AI has stamina power up
- if any goblin is below 15% stamina, use stamina power up
- if the average stamina of goblin team is below 50%, use stamina power up
- If AI has Heal power up
Once one power up is used, the AI stops making the other power up checks for 1-1.5 seconds before using another power up. This prevents the AI from spamming a bunch of power ups really quickly. I may need to lower the power up rate though, since a player can and will use power ups quickly if they are useful. Maybe make the cool down just that 0.5 seconds? I should also probably add that Heal power up check to the ball carrier checks as well, with a priority for ball carrier health.
There are still quite a few issues with the AI I need to fix. I’m still beating them pretty badly. One issue is that the goblin AI does have a tendency to “block” their own teammates, which can prevent the AI ball carrier from moving down the field. This can cause a cluster of goblins all running into each other and not moving anywhere to form. I might need to add some checks to have goblins dive out of that, or to change direction if they haven’t moved anywhere in a while
The AI ball carrier also sometimes will run “backwards” away from the endzone they score in. This appears to be caused by the way their run “direction” is calculated. The AI tries to run away from opponent goblins. If a bunch of opposing goblins are directly in front of the AI goblin and close by, they try to run away from them backwards. I might try and make it so if the ball carrier’s direction is backwards, to set the direction to be vertical instead, or maybe like a 45 degree angle backwards, or to just kick the ball if that happens. I don’t know…
Smell ya later nerds!