开发者

Modeling asynchronous game-like environment

The idea is to model an environment for agents. In the most basic case it looks likes this:

  1. System asks the agent for the next action
  2. Agent responds (e.g. "move left!")
  3. System moves the agent to the appropriate state

However, I am having trouble implementing this in an asynchronous manner (with threading and such).

Currently my system looks like this:

void Start(){
开发者_开发技巧   while(true && !gameOver){
       askAgent()
       moveAgent()

       if(agentState == terminalState){ 
          gameOver = True;
       }

   }
 }

Clearly, this blocks the thread this is running on. (What's more embarrassing is I am using OSGi, so any single bundle should not be hogging all the processing time!)

Also, I would like the system to react to new agents appearing in the environment, and engage with them (my runtime, OSGi, already has the facility of notifying me if something appears or disappears from the system) something like:

void setAgent(Agent agent){
       system.addAgentToEnvironment(agent);
       system.simulateAgent(agent);
}

Instead of just running from main straight away...

I know this is very confusing, and I am not sure if I am even posing the question correctly - so any tips on the architecture or approaches I can look at are greatly appreciated.


You will definitely need some data protection (perhaps on a master list of agents, and some kind of protection on each individual agent and its data).

Other than that, I would follow this kind of model:

while (waiting for events)
  spawn thread to respond to event // add agent, calculate and perform move, etc.
  // even better would be to enqueue the event into a thread pool

  if (terminal)
    break // end game

HTH


In order to help think about the future of the application, I would urge you to use two loops.

long then = System.currentTimeMillis();
for(Agent a : agents) {
    agent.calcuateNextMove(getEnvironment());
}

for(Agent a : agents) {
    agent.performNextMove(getEnvironment());
}
long now = System.currentTimeMillis();
sleep(TIME_STEP_DURATION + now - then); // give you a steady frame rate in relation to real time

This snippet gives you two things.

  1. Moves are made independently of other moves on the same step. This way you do not have your current move influenced by those who happened to move before you.
  2. An agent merely exists, and is simply told to calculate his next move based on the environment you give it. This makes it incredibly easy to change states, copy agents into multiple environments, and give the illusion that the environment is different than it really is. For example, you may have a filterFor(Environment e, Agent a) that makes a mocked up version of the environment for that particular agent. Like wearing drunk-goggles or a blindfold or something.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜