lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> On the other hand, it might be the question why you would use a separate 
> "microthread" for each and every agent in an AI system. Simply looping 
> over all agents in a big (timed) loop, or using some kind
> of message passing and/or callbacks can be just as efficient,
> not to mention a lot simpler, as you have no pains with mutexes and
> semaphores.

The reason is simple. With a microthread per agent, scripts look
something like this:

function dostuff()
  walk_to(A)
  wait(10)
  walk_to(B)
  if (something_happened()) then
    do_this()
  else
    do_that()
  end
end

If you don't have microthreads, you have to manage state explicitly,
and they often look like this:

# states...
INIT=0
WALKING_TO_A=1
WAITING=2
WALKING_TO_B=3
DOING_SOMETHING=4

function update_state()
  if (state==INIT) then
    state=WALKING_TO_A
    start_walk_to(A)

  elseif (state==WALKING_TO_A) then
    if walk_done() then
      state=WAITING
      start_wait(10)
    end

  elseif (state==WAITING) then
    if wait_done() then
      state=WALKING_TO_B
      start_walk_to(B)
    end

  elseif (state==WALKING_TO_B) then
    if walk_done() then
      if (something_happened()) then
        start_doing_this()
        state = DOING_SOMETHING
      else
        start_doing_that()
        state = DOING_SOMETHING
      end
    end

  elseif (state==DOING_SOMETHING) then
    if (finished_something()) then
      set_flag_saying_this_task_finished()
      return
    end
  end
end

Obviously there are different ways to approach this, some
of them cleaner for certain problems, but it still comes
out to a coding style where your only flow control is a
'goto' and if you want a stack you have to manage it
yourself.

--Jeff