Adding AI (Part 1)


NEEDS ENEMIES

Starboard has the minimal mechanics in place for a playable game, like shooting your boat's cannons, spending ammo, replenishing ammo, and capturing the enemy's flag, but without any enemies, the game is a bit flat. So it's time to add some AI, but the problem is, I've never done coded AI before! So let's see how it goes.

RAYCASTING

First, I needed to do some research, and this tutorial about adding ray-casting to a turret from KidsCanCode was very useful. Much like in the tutorial, my first idea was to add an Area2D to the bot to determine whether or not the bot "sees" an enemy. But I discounted that idea, thinking the Area2D was too naive of a solution and that bot AI needs to be more clever. While that may be true, an Area2D works for now so let's go with it.

Ship with Area2D

With the Area2D added, my bot is now alerted when then player enters its range. Once this happens, the bot will cast rays out in the player's direction to determine if it can see the player. If the player hides behind a rock or the shore, the ray will not reach the player and the ship will move toward the last known player location, until it sees the player again, or gives up and returns to its patrol. When a ray collides the player the ship needs to rotate towards the player, plus an extra 90 degrees or so, to the starboard side where the cannons reside. I'm currently having some trouble though with the enemy rotating 180 degrees in one direction, then suddenly flipping to the other direction:

STATE MACHINE

Enemies have four states: patrolling, attacking, retreating, and lifeless. I may create AI to capture the flag in the future which would involve another state, but let's keep these bots dumb for now. Each bot will have some predefined path to follow which will be spread out throughout the map and when they're following this path, they'll be in the Patrol state. When an enemy is spotted, the bot will transition to its attack state, where it'll move toward the enemy, and rotate to shoot at the enemy when the enemy is in range and the cannons are loaded. If the bot runs low on cannon balls, or health, or both, it'll enter the retreat state and cruise back to their home base to seek out a repair ship and ammo ship before going back on patrol. If the bot is destroyed, it enters the lifeless state and starts a timer to respawn and start the process over again.

THOUGHTS

I like the idea of a bot retreating until it sees a friendly ship, then turning, to attack as a team. Maybe bots will do this at random. And perhaps some bots will lure the player chasing it toward a friendly ship instead of sailing directly home.

Bots should prioritize ships that have stolen their team's treasure.

Maybe bots don't have to return to their predefined patrol path, but instead, any path, like the one closest to them will do.

Can I get away with not making predefined patrol paths? I've been thinking 3-6 paths per team, but maybe instead I could just create one large path. Ideally, no path at all would be necessary, and instead, the bots negotiate positioning amongst themselves, with some random grouping at times.

I look forward to posting part 2 once more progress has been made.

Leave a comment

Log in with itch.io to leave a comment.