Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I handle the communication between AI threads and the main game loop?

I'm working on a turn-based strategy game in Java (in the Android framework). Following the structure in Beginning Android Games, I have a render thread and a UI thread. The render thread repeatedly updates the world state and then redraws the world. When the user interacts with the screen, the GUI sends Actions to the world (Command pattern). Now I'm adding AI players, and here is my plan:

Each AI player will have an AI that runs on a separate thread.

When the world updates on an AI turn, it checks to see if there is a pending action. If so, it executes it. Then it asks the AI player for its next action.

The AI player will send the request for an action to the AI thread, and then return.

Eventually, the AI will come up with an action, and post it back to the World, which will see it on the next update.

Two questions:

1) Does this design seem sound?

2) How do I handle the communication to and from the AI thread? If I have the AI thread call world.queueAction(action), that seems like it would work, but if the render thread calls ai.chooseAction(world) that will run the action choosing on the render thread, which is not what I want.

like image 218
Perikles Avatar asked Dec 09 '11 17:12

Perikles


2 Answers

I would have an ExecutorService for the AIs and add tasks to it for things you want it to perform. For the UI you can have a queue of things which have changed and may need to be re-drawn. I would be tempted to use a single thread for all AIs until you know this would help. Most Android devices only have 1-2 CPUs anyway.

like image 78
Peter Lawrey Avatar answered Nov 14 '22 21:11

Peter Lawrey


Are you sure the system is going to be able to handle a large number of threads? Keep in mind that the 'optimal' number of running threads is usually n + 1, where n is the number of cores the system has. If this is running on a phone, there's going to be fewer of those available, although unless you're running more than 10 you may be fine...

For the connection between your game and your AI, it may be helpful to consider the difference between the (human) player and the AIs: there's only one important distinction - one is a computer, the other isn't. It would be best to submit the actions the AIs take to the exact same queue (or through the same general mechanism) as the human players (so, call world.queueAction(action);). It also makes swapping out between AIs and human players simpler, as your world update code doesn't need to know the difference...

I'm assuming that you already have some sort of eventing to notify the player/update the screen when it's the human player's turn. Do the exact same thing with the AI thread(s) - have it listen for the appropriate "It's your turn" event, causing it to finish (this iterations) calculations and place an action on the queue. Please note that this needs to be done from the 'world update' thread; the render thread should be solely concerned with rendering the output to the screen (and is only tangentially concerned with player input).

like image 42
Clockwork-Muse Avatar answered Nov 14 '22 21:11

Clockwork-Muse