Movement in sphere?

Say we have one person, called Jim, and we want to move him down
We do:-

QueuePersonCommand("Jim", COMMAND_MOVE_SOUTH, true);

And Jim will move south by 1 pixel.

Ok, so we want Jim to move 1 whole tile north then?

for (var i = 0; i < GetTileHeight(); ++i)
  QueuePersonCommand("Jim", COMMAND_MOVE_NORTH, false);

And Jim will move 1 tile north

... But say we want him to turn around before he moves north, we do:-

QueuePersonCommand("Jim", COMMAND_FACE_NORTH, true);
for (var i = 0; i < GetTileHeight(); ++i)
  QueuePersonCommand("Jim", COMMAND_MOVE_NORTH, false);

And by now we have something annoying to type out...
So we do something like:-

function Move(name, command, tiles, immediate)
{
  switch (command)
  {
    case COMMAND_MOVE_NORTH:
      QueuePersonCommand(name, COMMAND_FACE_NORTH, immediate);
      for (var i = 0; i < GetTileHeight() * tiles; ++i)
        QueuePersonCommand(name, command, false);
    break;

    case COMMAND_MOVE_SOUTH:
      QueuePersonCommand(name, COMMAND_FACE_SOUTH, immediate);
      for (var i = 0; i < GetTileHeight() * tiles; ++i)
        QueuePersonCommand(name, command, false);
    break;

    case COMMAND_MOVE_EAST:
      QueuePersonCommand(name, COMMAND_FACE_EAST, immediate);
      for (var i = 0; i < GetTileWidth() * tiles; ++i)
        QueuePersonCommand(name, command, false);
    break;

    case COMMAND_MOVE_WEST:
      QueuePersonCommand(name, COMMAND_FACE_WEST, immediate);
      for (var i = 0; i < GetTileWidth() * tiles; ++i)
        QueuePersonCommand(name, command, false);
    break;
  }
}

So now we do:-

Move("Jim", COMMAND_MOVE_NORTH, 5);

And Jim will move north 5 tiles.

Say we want to spawn a whole army, and move all of them...

function SpawnArmy()
{
  var base_x = 100;
  var base_y = 100;

  for (var x = 0; x < 5; ++i)
  {
    for (var y = 0; y < 5; ++i)
    {
      var c = "army_" + x + "_" + y;
      CreatePerson(c, "army.rss", true);
      SetPersonX(c, base_x + x * 20);
      SetPersonY(c, base_y + y * 20);
      Move(c, COMMAND_MOVE_SOUTH, y, false);
    }
  }


  for (var x = 0; x < 5; ++i)
  {
    for (var y = 0; y < 5; ++i)
    {
      var c = "army_" + x + "_" + y;
      // now issue commands to them all
      Move(c, COMMAND_MOVE_SOUTH, 5, false);
      Move(c, COMMAND_MOVE_EAST, 2, false);
      Move(c, COMMAND_MOVE_SOUTH, 10, false);
    }
  }
}

Huh?
Well, the first bit of that code is designed to create the whole army and move them all into a line...

YYYYY = 4
YYYYY = 3
YYYYY = 2
YYYYY = 1
YYYYY = 0

The ones at the back, have to move 4 tiles to get in line with those at the front...
Once they are all in line (they aren't actually in a line, just in terms of commands we give them) we can issue commands to all of them.
Thus the second bit of the code.
Move south 5 tiles, move east 2 tiles and then move south 10 tiles...

Then when they've all finished their commands, they're all back where they started.
(I mean in their formations :))

Okay, so we've spawned an army and made them walk along..

"I want an guy to wander around" I hear you cry...

Easy.

function GenerateRandomMovement()
{
  var person = GetCurrentPerson();
  var commands = new Array(COMMAND_MOVE_NORTH, COMMAND_MOVE_EAST, COMMAND_MOVE_SOUTH, COMMAND_MOVE_WEST);
  var r1 = Math.floor(Math.random() * commands.length);
  var r2 = Math.floor(Math.random() * 3); // 3 being the number of tiles

  Move(person, commands[r1], r2, false);
}

And then:-
SetPersonScript("Jim", SCRIPT_COMMAND_GENERATOR, "GenerateRandomMovement()");

Jim will now wander randomly.


As an added note, you can't do anything to a person that doesn't exist (apart from create that person.)
How do you tell if a person exists?

function DoesPersonExist(name) {
  var person_list = GetPersonList();
  for (var i = 0; i < person_list.length; ++i)
    if (person_list[i] == name)
      return true;
  return false;
}

You just call that function like so:
if (DoesPersonExist("Jim"))
  // code only if Jim exists