0

I am trying to implement some form of AI into my Net Logo game.

I am planning on calculating the Manhattan distance from a zombie turtle to a human turtle.

So far I have managed to calculate the Manhattan distance from the two and draw the path and also move the zombie agent along the calculated path towards the human turtle.

The problem I am facing now is that the human agents location is going to be controlled by the user. The Manhattan distance uses a while loop and doesn't break out of the loop until the it has reached the human agent. I would like the zombie agent to move one step towards the human agent and then let the human agent move.

Code so far Net Logo Game

4

1 回答 1

0

I am not sure what you are trying to do in your plot-manhattan-distance procedure. It seems like a complicated approach to something that should be relatively simple, but maybe I misunderstood your purpose. Here is how I would approach the whole problem:

globals [
  zombie
  human
]

to setup
  clear-all
  ask n-of 2 patches [ sprout 1 ]
  set human turtle 0
  ask human [ set shape "person" ]
  set zombie turtle 1
  ask zombie [ pen-down ]
end

to go
  ask human [ flee zombie ]
  ask zombie [ pursue human ]
end

to pursue [ target ]
  face target
  set heading first sort-by [abs (?1 - heading) < abs (?2 - heading)] [0 90 180 270]
  fd 1
end

to flee [ pursuer ]
  face pursuer
  rt 180
  fd 0.5
end

The meat is in the pursue procedure. Since the zombie cannot predict where the human is going to go, it is just trying to move in its general direction. It starts by facing the human directly (with the handy NetLogo face primitive). But since it is presumably only allowed to move in one of the four cardinal directions, it must choose the one that is the most desirable, i.e.: the one that is the least different from its current (ideal) heading. This is what the sort-by [abs (?1 - heading) < abs (?2 - heading)] [0 90 180 270] expression does: it sorts the list of directions by comparing their absolute difference with the current heading. The first item of this sorted list will be the one with the smallest difference, and thus, the one that the zombie needs to use.

In the current implementation, the human just tries to move away from the zombie, but you could easily replace that with player control code.

于 2012-12-14T17:59:50.043 回答