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

I am working on a turn-based strategy in Java (within Android). Following the structure in “Getting Started for Android,” I have a rendering stream and a user interface stream. The rendering stream re-updates the state of the world, and then redraws the world. When the user interacts with the screen, the graphical interface sends actions to the world (command template). Now I add AI players, and here is my plan:

Each AI player will have an AI that runs in a separate stream.

When the world updates when the AI ​​turns, it checks to see if there is a pending action. If so, he fulfills it. He then asks the AI ​​player for the next action.

The AI ​​player sends an action request to the AI ​​stream and then returns.

In the end, AI will come up with an action and send it back to the world, which will see it in the next update.

Two questions:

1) Does this design sound?

2) How to handle communication with and from the AI ​​stream? If I have a stream of AI calls world.queueAction (action), it looks like this will work, but if the stream render calls ai.chooseAction (world), which will trigger an action that selects in the render stream, which is not what I want ,

+5
source share
3 answers

ExecutorService , . , , , . , , . Android- 1-2 .

+4

, ? , "" n + 1, n - , . , , , 10, ...

() : - - , - . , ( ), (, world.queueAction(action);). AI , ...

, , / , . AI - "It your turn", ( ) . , "world update"; ( ).

+2

:

  • . , .
  • " ". .
    • , ;
    • AI

It would seem that you could safely run each of the logical AI flows as Futurewell as collect your results together, doing everything that the "world" stream does on each frame.

+1
source

All Articles