Patrolling Attackers

Hi guys, I’ve set up some enemies with Inform Attack and I’m trying to make them move a square or two every turn, within set limits, and stop to attack if they share a room with the player. At the moment I’m trying to uses Patrollers by Michael Callaghan to do the movement, but I can’t seem to make a person move on their own aimlessly. Can anyone help? I’ve set the Turn Frequency to 100 and the frequency to 1, but they stay where they are.

I would also like to try and make them track the player in certain areas, but not go directly to them, if possible.

pfft im bad at helping lol

From a quick scan of the documentation, I think you want to set the Drive to 100 and the Turn Frequency to 1–if you get these backwards you might wind up with a 1% chance of movement every 100 turns, when what you need is the opposite.

This code will make all NPCs move one room per turn, randomly, stopping if they find the player.

Definition: a person is other if they are not the player.

Every turn (this is the move patrollers rule):
    repeat with the subject running through other people:
        if the subject can see the player, next;
        let the place be the location of the subject;
        let the destination be a random room adjacent to the place;
        let the way be the best route from the place to the destination;
        if the way is a direction, try the subject going way.

Thanks a lot! There seems to be a bug with the last line though:

		if the way is the direction, try the subject going way.

Gives me this error:

Try “if the way is a direction”. If there’s no way to get from the place to the destination, then “the way” will come out as “nothing” and having the subject going the way will result in an annoying error message (“You must supply a direction,” I think). So this checks to make sure that the way is a direction (rather than “nothing”) before having the subject go that way.

You might also want to check whether any rooms are adjacent to the place before the random room line–if it’s possible for any NPCs to be in rooms that don’t have any adjacent rooms, the “random” line will give a run-time error (because you’re trying to randomly select an adjacent room, and there are no adjacent rooms).

Oops, my mistake–fixed that in the code now.

Thanks a lot, that really works well! Is there any way to make it so that if they leave a room they don’t immediately go back to it unless it’s their only option? And to make it so that they can’t enter certain rooms?

A room can be patrollable. A room is usually patrollable.

A person has a room called the previous location.
First carry out an actor going from a room (called the place):
    now the previous location of the actor is the place.

Every turn (this is the move patrollers rule):
    repeat with the subject running through other people:
        if the subject can see the player, next;
        let the place be the location of the subject;
        let the destination be a random patrollable room adjacent to the place;
        if the destination is the previous location of the subject:
            let the destination be a random patrollable room adjacent to the place;
        let the way be the best route from the place to the destination;
        if the way is a direction, try the subject going way.

You can mark a room as “not patrollable” to keep people from going into it. It’s now much less likely (though not impossible, so that they don’t get stuck in dead ends) that a person will choose to go back the way they came: if the previous location is selected, they ‘roll the dice’ a second time. If there are two exits from the room, they’ll choose to go back 1/4 of the time and forward 3/4 of the time. If there are six exits, they’ll go back with probability 1/36 and choose any of the other exits with probability 7/36 (going forward with probability 35/36). If there is only one exit from the room, they’ll go back with probaility 1.

Thanks a lot, that really helps, though they still seem to stay in very small areas. Is there a way to check the number of exits and pick one that hasn’t just been used?

When you say “just used”, do you mean not pick a door another patroller has gone through?

No, I mean, is there a way to prevent them going into a room they have just been in unless they are sitting in a dead end and there is not other option? Something like:

if number of available exits is 1: try going that way; otherwise: try going a different way;

I just wish I could get patrollers to work as I’m sure it has probably already solved this, but every time I try to insert it into my own game the examples just stop working. :stuck_out_tongue: I know you can set rooms to be off limits in it.

I suppose you could do something like this.

To decide which room is the next destination for (subject - a person):
    let the place be the previous location of the subject;
    if the number of rooms adjacent to the location of the subject is 1:
        let the place be a random room adjacent to the location of the subject;
    otherwise:
        while the place is the previous location of the subject:
            let the place be a random room adjacent to the location of the subject;
    decide on the place.

Then change your code so that they try moving towards “the next destination for the subject”.

Thanks a lot, but I think I might have to abandon this as making even a single NPC move every turn drastically slows the game down. :stuck_out_tongue: Unless it’s just something about that script in particular. I dunno. I didn’t have any problems with the patrollers script in a blank file. Maybe I’m just making it do too much per turn, but it’s not like it’s a big game, or a slow computer.

What’s your code at the moment?

Well, I’m running a few extensions that have ‘every turn’ and ‘after printing output’ rules in them, but it’s only when I try to run that move script that it slows down to a crawl. I think it may be running through every single person in the game trying to apply that script to it, even though I changed the definition to move only one person.

Is there a script I can use to teleport them to a random location within a region? I think that would pretty much do the same thing and I can set when it happens.

For your second question:

Every turn:
    move Alice to a random room in the Forest.

For your first question:

It might be. Can you post your movement code?

Thanks! Is there a way to make sure it isn’t the location of the player so that I can put it in the my ‘enemy reset’ script?

The script that was causing the slowdown was something like this:

[code]A room can be patrollable. A room is usually patrollable.

A person has a room called the previous location.
First carry out an actor going from a room (called the place):
now the previous location of the actor is the place.

TestGuy is a person.

Definition: a person is patrol if they are TestGuy.

Every turn (this is the move patrol rule):
repeat with the subject running through patrol:
if the subject can see the player, next;
let the place be the location of the subject;
let the destination be a random patrollable room adjacent to the place;
if the destination is the previous location of the subject:
let the destination be a random patrollable room adjacent to the place;
let the way be the best route from the place to the destination;
if the way is a direction, try the subject going way.[/code]

Try this.

A room can be patrollable. A room is usually patrollable.

Alice is a woman.

Alice has a room called the previous location.
First carry out Alice going from a room (called the place):
    now the previous location of Alice is the place.

Definition: a room is explorable if it is patrollable and it is not the previous location of Alice.

Every turn (this is the move patrol rule):
    let the destination be the previous location of Alice;
    let the place be the location of Alice;
    if the number of rooms adjacent to the place is 1:
        let the destination be a random patrollable room adjacent to the place;
    otherwise:
        let the destination be a random explorable room adjacent to the place;
    let the way be the best route from the place to the destination;
    if the way is a direction, try Alice going way.