Intro to Room AI

INTRO TO ROOM AI

Are your fingers tired of dragging those little reticles to shoot enemy rooms? Have a tiny simulated computer do it for you!

Maybe grab a drink for this…
(credit xkcd.com)

AI is how you control what your ship does, and is the only way to control your ship on defense. Build a CMD room as soon as you reach ship level four and program that AI! You can remove the CMD room when you’re not actively editing your AI; everything will still function. Just remember that if you manually target a room, the AI will turn off and it will not retarget for the rest of the battle unless you do it manually. Also, if you manually change the power for a room, the power AI for ALL rooms will turn off. Avoid manual power control, since once a room is damaged, it won’t fill back up with power unless you do it manually. 

Definitions

Pixel starships has its own way to “program” rooms. Understanding this is the key to designing your own AI, and to getting your ship to actually do something complicated.  

Command is a whole line in the AI 
Condition is the first half of the command, the part that is evaluated as being either true or false
Action is the second half of the command, the part that is performed if the condition is true

True – a command is true when the condition is true. For example, if the condition is “target room has enemy” and the room is currently targeting a room with an enemy, then the command is true.
Valid – a command is valid when the action can be completed. If the action is “target enemy lasers” then it is only valid if there is at least one laser room on the opponent’s ship that has HP. 

Actions for rooms are in three groups: 

Power – for example, set max power, and increase power by one. There is no way to set anything other than 0, 1, or max power. 

Targeting – for example, target enemy reactors, and target condition room. No targeting commands allow you to pick specific rooms, only room types. You can’t differentiate between a SHL and SB, or ION and EMP.

Ammo selection – for example, set cheapest items, or set highest fire items (rooms without ammo ignore this group) 

Every frame (so 40 times a second) the game engine loops through all the commands, from the top to the bottom, and takes the first true and valid command FROM EACH GROUP. Once a true and valid command for a group is found, all other commands below that in the same group are ignored. For simplicity, I like to put my groups of commands together, but you don’t have to – the functionality is the same. 

Confused?

Let’s do an example

Here is a basic set of commands for our MSL: 

1) None – Increase power by one 
2) None – Set cheapest items
3) Enemy ship has full HP – Target enemy teleport
4) None – Target enemy reactor

In the first frame of the game, the valid and true commands for power, targeting, and ammo are chosen. For power, it’s of course command 1 – so the MSL will get one power assuming there is free power to give it. For targeting, if the enemy has a teleport, it will target the teleport (since the enemy ship always starts with full HP). If the enemy does not have a teleport, a random enemy reactor will be targeted per command 4. For ammo selection, command 2 will run and choose the cheapest missile we have constructed – usually a rocket, though this depends on your research level.

In the second frame of the game, not much has changed, but our MSL will gain one more power. There’s no way our lasers or missiles have hit the enemy ship yet, so the enemy ship still has full HP.

In the third frame of the game, the MSL will be fully powered up. (This repeated execution of power commands means that the action “decrease power by one” is equivalent to “set zero power”, it just takes a few frames to execute.)

The next change we can expect is once we do our first hull damage. Let’s assume all our other weapons are shooting the teleport too, so we destroy the room and then do hull damage. In the next frame after that happens, the enemy ship will not have full HP and command 3 will be false. The next true and valid command is command 4, so our MSL will target a random enemy reactor.

After that reactor is destroyed, command 4 will still be true and valid, but the targeted reactor will no longer satisfy that command since it doesn’t have HP. A different random reactor that has HP will be chosen. 

The AI will keep choosing reactors and shooting them until they are all destroyed. After this happens, there are no true and valid targeting commands. The MSL won’t stop functioning, it will just keep shooting the last reactor it was shooting at until it runs out of missiles. If an opponent crew repairs one of the reactors, the MSL will use command 4 to target that reactor. Note that the MSL will never shoot the teleport again even if it’s repaired, since the condition of command 3 will never be true again after the opponent’s ship has taken damage. 

Continue reading about AI on the More AI page.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top