# Roaming AI

• 03-23-2009
strokebow
Roaming AI
Hi, I want to cause an object to roam.

I was thinking in a figure of 8, but with straight edges.

_
|_|
|_|

How could I go about mplementing such a roaming pattern?
• 03-23-2009
VirtualAce
What are some of your ideas and then we can discuss how they may or may not work?
• 03-23-2009
strokebow
Hi,

I basically have an character that moves only in one direction at a time. Basically, I want it to have random movements within the game screen but perhaps some pattern to it. I thought a figure of 8 would be nicely suited.

I just dont know where to start in coding this one
• 03-23-2009
CodeMonkey
Code:

```enum dir { up, down, left, right }; extern void object::move(dir which_way); void object::make_move( . . .) {   //if nothing interesting happens...   dir direction = (std::rand() >> 2) % 4;  //random mod 4, but first shifted                                             //to use higher-order bits. They're                                             //sometimes "more random."   move(direction); }```
*edit* How does a figure eight go? right, down, left, down, right, up, left, up? + -' - -' + +' - +' ?
• 03-24-2009
strokebow
Thanks.

P.S. Nice quote
• 03-24-2009
strokebow
Code:

```    int[][] zigzag = {{1, 1}, {-1, 1}};     aX += zigzag[patStep][0];     aY += zigzag[patStep][1];```
This code shows how to implement a very simple vertical zigzag pattern. The int array zigzag contains pairs of XY offsets used to apply the pattern to the alien. The patStep variable is an integer representing the current step in the pattern. When this pattern is applied, the alien moves in a vertical direction while zigzagging back and forth horizontally.

I found this on the net (Internet Games Tutorial - Softlookup.com)

It seems a novel idea. How would I implement patStep without immediately having a call to draw afterwards?
• 03-24-2009
DavidP
You probably don't want to generate a new random direction every time make_move() is called though. Have some kind of counter or timer that allows some time to pass before a new random direction is generated. That way he is not flipping directions every frame of motion....for if he did he would probably go nowhere.
• 03-24-2009
Perspective
Random movement usually looks pretty odd as DavidP points out. You may also consider hard-coding a bunch of different patterns (like the figure 8 using a method like strokebow has shown), then randomly choose a pattern from the set every x steps.
• 03-30-2009
quzah
You could just store movements as paths. Stick them in strings, and just run through the string until you get to the end. Then make something parse the current character.
Code:

```you.path = "enwseswn"; switch( you.path[ you.here++ ] ) {     case 'n': you.y--; break;     case 'e': you.x++; break;     case 's': you.y++; break;     case 'w': you.x--; break;     case '*':     {         switch( rand() % 4 )         {             case 0: you.y--; break;             case 1: you.x++; break;             case 2: you.y++; break;             case 3: you.x--; break;         }     }     break; } randomsoutheastroamer.path = "s*e*";```
Then just throw in any other neat exceptions you want, as I did with *. Naturally you will want to do some boundary checks.