Improve the AI
The AI that controls the enemy ship in this example project is very simple, as can bee seen when trying out the game. In this tutorial, your task is to improve and refine the AI to the level that you think is appropriate. To your help, you have the reference sections in the help system.
A good way to get started would be to create a new AI block by copying the inputs and outputs of the existing AI block:
- Open Main.sps, the top-level schematic. You can see that the model is divided into two main parts: The physics engine (Plant1), which calculates the ships' positions, the score and other outputs given the control actions of the two players, and the AI (AI1), which calculates the control actions for ship 2.
- Double-click the AI1 block to open AI.sps.
- Choose Edit:Select All or press Ctrl-A to select all elements.
- Choose Edit:Copy or press Ctrl-C to copy the elements to the clipboard.
- Choose File:New Schematic or press Ctrl-N to create a new schematic.
- Choose Edit:Paste or press Ctrl-V to paste the elements into the new schematic.
- Delete all elements except the ports by selecting them and pressing the Del key.
- Choose Project:Compile or press F11 to compile the new schematic into a new block. Name the new block appropriately, for example AI2 or MyAI. Don't worry about the compiler errors for now.
- Go back to Main.sps by clicking its tab.
- Depending on how you want to test your AI, you now have two options:
- If you want to test your AI by playing against it yourself, then you should replace the existing AI with your own. You do this by selecting the AI1 block, pressing delete, dragging in your own AI block from the blocks tree and recreate the connections.
- If you want to test your AI by letting the existing AI play against it, then you should replace the input ports of Main.sps with your AI, so that your AI controls ship 1 instead of the player. You do this by selecting the four input ports, pressing delete, dragging in your own AI block from the blocks tree and connect it appropriately. Notice that the AI by convention should consider the inputs with names ending with "1" be the enemy ship information, and inputs with names ending with "2" be its own ship information. Therefore, the "1" outputs of Plant1 should be connected to the "2" inputs of your AI, and vice versa.
Your own AI block is now ready to be filled out with logic. At any time, you can press F10 to test the game. Notice, however, that the code generator will fail as long as any of the four output ports of your AI schematic is unconnected. At least you will have to connect them to Value blocks to be able to start the game.